Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Make GroupStore use MatrixClientPeg
Browse files Browse the repository at this point in the history
To avoid weirdness with using a cached matrix client
  • Loading branch information
lukebarnard1 committed Nov 28, 2017
1 parent 1a3ad5a commit 7ec4010
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/GroupAddressPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function _onGroupInviteFinished(groupId, addrs) {

function _onGroupAddRoomFinished(groupId, addrs, addRoomsPublicly) {
const matrixClient = MatrixClientPeg.get();
const groupStore = GroupStoreCache.getGroupStore(matrixClient, groupId);
const groupStore = GroupStoreCache.getGroupStore(groupId);
const errorList = [];
return Promise.all(addrs.map((addr) => {
return groupStore
Expand Down
2 changes: 1 addition & 1 deletion src/components/structures/GroupView.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ export default React.createClass({
if (group && group.inviter && group.inviter.userId) {
this._fetchInviterProfile(group.inviter.userId);
}
this._groupStore = GroupStoreCache.getGroupStore(this._matrixClient, groupId);
this._groupStore = GroupStoreCache.getGroupStore(groupId);
this._groupStore.registerListener(() => {
const summary = this._groupStore.getSummary();
if (summary.profile) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/dialogs/AddressPickerDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ module.exports = React.createClass({

_doNaiveGroupRoomSearch: function(query) {
const lowerCaseQuery = query.toLowerCase();
const groupStore = GroupStoreCache.getGroupStore(MatrixClientPeg.get(), this.props.groupId);
const groupStore = GroupStoreCache.getGroupStore(this.props.groupId);
const results = [];
groupStore.getGroupRooms().forEach((r) => {
const nameMatch = (r.name || '').toLowerCase().includes(lowerCaseQuery);
Expand Down
4 changes: 1 addition & 3 deletions src/components/views/groups/GroupMemberInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ module.exports = React.createClass({
},

_initGroupStore(groupId) {
this._groupStore = GroupStoreCache.getGroupStore(
this.context.matrixClient, this.props.groupId,
);
this._groupStore = GroupStoreCache.getGroupStore(this.props.groupId);
this._groupStore.registerListener(this.onGroupStoreUpdated);
},

Expand Down
11 changes: 3 additions & 8 deletions src/components/views/groups/GroupMemberList.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ import sdk from '../../../index';
import GroupStoreCache from '../../../stores/GroupStoreCache';
import GeminiScrollbar from 'react-gemini-scrollbar';
import PropTypes from 'prop-types';
import withMatrixClient from '../../../wrappers/withMatrixClient';

const INITIAL_LOAD_NUM_MEMBERS = 30;

export default withMatrixClient(React.createClass({
export default React.createClass({
displayName: 'GroupMemberList',

contextTypes: {
matrixClient: PropTypes.object.isRequired,
},

propTypes: {
groupId: PropTypes.string.isRequired,
},
Expand All @@ -49,7 +44,7 @@ export default withMatrixClient(React.createClass({
},

_initGroupStore: function(groupId) {
this._groupStore = GroupStoreCache.getGroupStore(this.context.matrixClient, groupId);
this._groupStore = GroupStoreCache.getGroupStore(groupId);
this._groupStore.registerListener(() => {
this._fetchMembers();
});
Expand Down Expand Up @@ -174,4 +169,4 @@ export default withMatrixClient(React.createClass({
</div>
);
},
}));
});
7 changes: 1 addition & 6 deletions src/components/views/groups/GroupPublicityToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.

import React from 'react';
import PropTypes from 'prop-types';
import { MatrixClient } from 'matrix-js-sdk';
import sdk from '../../../index';
import GroupStoreCache from '../../../stores/GroupStoreCache';
import GroupStore from '../../../stores/GroupStore';
Expand All @@ -29,10 +28,6 @@ export default React.createClass({
groupId: PropTypes.string.isRequired,
},

contextTypes: {
matrixClient: PropTypes.instanceOf(MatrixClient),
},

getInitialState() {
return {
busy: false,
Expand All @@ -46,7 +41,7 @@ export default React.createClass({
},

_initGroupStore: function(groupId) {
this._groupStore = GroupStoreCache.getGroupStore(this.context.matrixClient, groupId);
this._groupStore = GroupStoreCache.getGroupStore(groupId);
this._groupStore.registerListener(() => {
this.setState({
isGroupPublicised: this._groupStore.getGroupPublicity(),
Expand Down
4 changes: 1 addition & 3 deletions src/components/views/groups/GroupRoomInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ module.exports = React.createClass({
},

_initGroupStore(groupId) {
this._groupStore = GroupStoreCache.getGroupStore(
this.context.matrixClient, this.props.groupId,
);
this._groupStore = GroupStoreCache.getGroupStore(this.props.groupId);
this._groupStore.registerListener(this.onGroupStoreUpdated);
},

Expand Down
7 changes: 1 addition & 6 deletions src/components/views/groups/GroupRoomList.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,10 @@ import sdk from '../../../index';
import GroupStoreCache from '../../../stores/GroupStoreCache';
import GeminiScrollbar from 'react-gemini-scrollbar';
import PropTypes from 'prop-types';
import {MatrixClient} from 'matrix-js-sdk';

const INITIAL_LOAD_NUM_ROOMS = 30;

export default React.createClass({
contextTypes: {
matrixClient: React.PropTypes.instanceOf(MatrixClient).isRequired,
},

propTypes: {
groupId: PropTypes.string.isRequired,
},
Expand All @@ -46,7 +41,7 @@ export default React.createClass({
},

_initGroupStore: function(groupId) {
this._groupStore = GroupStoreCache.getGroupStore(this.context.matrixClient, groupId);
this._groupStore = GroupStoreCache.getGroupStore(groupId);
this._groupStore.registerListener(() => {
this._fetchRooms();
});
Expand Down
34 changes: 17 additions & 17 deletions src/stores/GroupStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
import EventEmitter from 'events';
import { groupMemberFromApiObject, groupRoomFromApiObject } from '../groups';
import FlairStore from './FlairStore';
import MatrixClientPeg from '../MatrixClientPeg';

/**
* Stores the group summary for a room and provides an API to change it and
Expand All @@ -31,13 +32,12 @@ export default class GroupStore extends EventEmitter {
GroupRooms: 'GroupRooms',
};

constructor(matrixClient, groupId) {
constructor(groupId) {
super();
if (!groupId) {
throw new Error('GroupStore needs a valid groupId to be created');
}
this.groupId = groupId;
this._matrixClient = matrixClient;
this._summary = {};
this._rooms = [];
this._members = [];
Expand All @@ -50,7 +50,7 @@ export default class GroupStore extends EventEmitter {
}

_fetchMembers() {
this._matrixClient.getGroupUsers(this.groupId).then((result) => {
MatrixClientPeg.get().getGroupUsers(this.groupId).then((result) => {
this._members = result.chunk.map((apiMember) => {
return groupMemberFromApiObject(apiMember);
});
Expand All @@ -61,7 +61,7 @@ export default class GroupStore extends EventEmitter {
this.emit('error', err);
});

this._matrixClient.getGroupInvitedUsers(this.groupId).then((result) => {
MatrixClientPeg.get().getGroupInvitedUsers(this.groupId).then((result) => {
this._invitedMembers = result.chunk.map((apiMember) => {
return groupMemberFromApiObject(apiMember);
});
Expand All @@ -78,7 +78,7 @@ export default class GroupStore extends EventEmitter {
}

_fetchSummary() {
this._matrixClient.getGroupSummary(this.groupId).then((resp) => {
MatrixClientPeg.get().getGroupSummary(this.groupId).then((resp) => {
this._summary = resp;
this._ready[GroupStore.STATE_KEY.Summary] = true;
this._notifyListeners();
Expand All @@ -88,7 +88,7 @@ export default class GroupStore extends EventEmitter {
}

_fetchRooms() {
this._matrixClient.getGroupRooms(this.groupId).then((resp) => {
MatrixClientPeg.get().getGroupRooms(this.groupId).then((resp) => {
this._rooms = resp.chunk.map((apiRoom) => {
return groupRoomFromApiObject(apiRoom);
});
Expand Down Expand Up @@ -145,66 +145,66 @@ export default class GroupStore extends EventEmitter {
}

addRoomToGroup(roomId, isPublic) {
return this._matrixClient
return MatrixClientPeg.get()
.addRoomToGroup(this.groupId, roomId, isPublic)
.then(this._fetchRooms.bind(this));
}

updateGroupRoomVisibility(roomId, isPublic) {
return this._matrixClient
return MatrixClientPeg.get()
.updateGroupRoomVisibility(this.groupId, roomId, isPublic)
.then(this._fetchRooms.bind(this));
}

removeRoomFromGroup(roomId) {
return this._matrixClient
return MatrixClientPeg.get()
.removeRoomFromGroup(this.groupId, roomId)
// Room might be in the summary, refresh just in case
.then(this._fetchSummary.bind(this))
.then(this._fetchRooms.bind(this));
}

inviteUserToGroup(userId) {
return this._matrixClient.inviteUserToGroup(this.groupId, userId)
return MatrixClientPeg.get().inviteUserToGroup(this.groupId, userId)
.then(this._fetchMembers.bind(this));
}

acceptGroupInvite() {
return this._matrixClient.acceptGroupInvite(this.groupId)
return MatrixClientPeg.get().acceptGroupInvite(this.groupId)
// The user might be able to see more rooms now
.then(this._fetchRooms.bind(this))
// The user should now appear as a member
.then(this._fetchMembers.bind(this));
}

addRoomToGroupSummary(roomId, categoryId) {
return this._matrixClient
return MatrixClientPeg.get()
.addRoomToGroupSummary(this.groupId, roomId, categoryId)
.then(this._fetchSummary.bind(this));
}

addUserToGroupSummary(userId, roleId) {
return this._matrixClient
return MatrixClientPeg.get()
.addUserToGroupSummary(this.groupId, userId, roleId)
.then(this._fetchSummary.bind(this));
}

removeRoomFromGroupSummary(roomId) {
return this._matrixClient
return MatrixClientPeg.get()
.removeRoomFromGroupSummary(this.groupId, roomId)
.then(this._fetchSummary.bind(this));
}

removeUserFromGroupSummary(userId) {
return this._matrixClient
return MatrixClientPeg.get()
.removeUserFromGroupSummary(this.groupId, userId)
.then(this._fetchSummary.bind(this));
}

setGroupPublicity(isPublished) {
return this._matrixClient
return MatrixClientPeg.get()
.setGroupPublicity(this.groupId, isPublished)
.then(() => { FlairStore.invalidatePublicisedGroups(this._matrixClient.credentials.userId); })
.then(() => { FlairStore.invalidatePublicisedGroups(MatrixClientPeg.get().credentials.userId); })
.then(this._fetchSummary.bind(this));
}
}
4 changes: 2 additions & 2 deletions src/stores/GroupStoreCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class GroupStoreCache {
this.groupStore = null;
}

getGroupStore(matrixClient, groupId) {
getGroupStore(groupId) {
if (!this.groupStore || this.groupStore.groupId !== groupId) {
// This effectively throws away the reference to any previous GroupStore,
// allowing it to be GCd once the components referencing it have stopped
// referencing it.
this.groupStore = new GroupStore(matrixClient, groupId);
this.groupStore = new GroupStore(groupId);
}
this.groupStore._fetchSummary();
return this.groupStore;
Expand Down
2 changes: 1 addition & 1 deletion src/utils/MultiInviter.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class MultiInviter {
let doInvite;
if (this.groupId !== null) {
doInvite = GroupStoreCache
.getGroupStore(MatrixClientPeg.get(), this.groupId)
.getGroupStore(this.groupId)
.inviteUserToGroup(addr);
} else {
doInvite = inviteToRoom(this.roomId, addr);
Expand Down

0 comments on commit 7ec4010

Please sign in to comment.