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

Commit 904c3aa

Browse files
authored
Merge pull request #1452 from matrix-org/luke/groups-store-2
Modify the group store to include group rooms
2 parents 2ba0a80 + 917957c commit 904c3aa

File tree

5 files changed

+68
-45
lines changed

5 files changed

+68
-45
lines changed

src/components/views/dialogs/AddressPickerDialog.js

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import MatrixClientPeg from '../../../MatrixClientPeg';
2323
import AccessibleButton from '../elements/AccessibleButton';
2424
import Promise from 'bluebird';
2525
import { addressTypes, getAddressType } from '../../../UserAddress.js';
26+
import GroupStoreCache from '../../../stores/GroupStoreCache';
2627

2728
const TRUNCATE_QUERY_LIST = 40;
2829
const QUERY_USER_DIRECTORY_DEBOUNCE_MS = 200;
@@ -241,32 +242,25 @@ module.exports = React.createClass({
241242

242243
_doNaiveGroupRoomSearch: function(query) {
243244
const lowerCaseQuery = query.toLowerCase();
244-
MatrixClientPeg.get().getGroupRooms(this.props.groupId).then((resp) => {
245-
const results = [];
246-
resp.chunk.forEach((r) => {
247-
const nameMatch = (r.name || '').toLowerCase().includes(lowerCaseQuery);
248-
const topicMatch = (r.topic || '').toLowerCase().includes(lowerCaseQuery);
249-
const aliasMatch = (r.canonical_alias || '').toLowerCase().includes(lowerCaseQuery);
250-
if (!(nameMatch || topicMatch || aliasMatch)) {
251-
return;
252-
}
253-
results.push({
254-
room_id: r.room_id,
255-
avatar_url: r.avatar_url,
256-
name: r.name || r.canonical_alias,
257-
});
258-
});
259-
this._processResults(results, query);
260-
}).catch((err) => {
261-
console.error('Error whilst searching group users: ', err);
262-
this.setState({
263-
searchError: err.errcode ? err.message : _t('Something went wrong!'),
264-
});
265-
}).done(() => {
266-
this.setState({
267-
busy: false,
245+
const groupStore = GroupStoreCache.getGroupStore(MatrixClientPeg.get(), this.props.groupId);
246+
const results = [];
247+
groupStore.getGroupRooms().forEach((r) => {
248+
const nameMatch = (r.name || '').toLowerCase().includes(lowerCaseQuery);
249+
const topicMatch = (r.topic || '').toLowerCase().includes(lowerCaseQuery);
250+
const aliasMatch = (r.canonical_alias || '').toLowerCase().includes(lowerCaseQuery);
251+
if (!(nameMatch || topicMatch || aliasMatch)) {
252+
return;
253+
}
254+
results.push({
255+
room_id: r.room_id,
256+
avatar_url: r.avatar_url,
257+
name: r.name || r.canonical_alias,
268258
});
269259
});
260+
this._processResults(results, query);
261+
this.setState({
262+
busy: false,
263+
});
270264
},
271265

272266
_doRoomSearch: function(query) {

src/components/views/groups/GroupRoomList.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import React from 'react';
1717
import { _t } from '../../../languageHandler';
1818
import sdk from '../../../index';
1919
import { groupRoomFromApiObject } from '../../../groups';
20+
import GroupStoreCache from '../../../stores/GroupStoreCache';
2021
import GeminiScrollbar from 'react-gemini-scrollbar';
2122
import PropTypes from 'prop-types';
2223
import {MatrixClient} from 'matrix-js-sdk';
@@ -34,7 +35,6 @@ export default React.createClass({
3435

3536
getInitialState: function() {
3637
return {
37-
fetching: false,
3838
rooms: null,
3939
truncateAt: INITIAL_LOAD_NUM_ROOMS,
4040
searchQuery: "",
@@ -43,21 +43,29 @@ export default React.createClass({
4343

4444
componentWillMount: function() {
4545
this._unmounted = false;
46-
this._fetchRooms();
46+
this._initGroupStore(this.props.groupId);
4747
},
4848

49-
_fetchRooms: function() {
50-
this.setState({fetching: true});
51-
this.context.matrixClient.getGroupRooms(this.props.groupId).then((result) => {
49+
_initGroupStore: function(groupId) {
50+
this._groupStore = GroupStoreCache.getGroupStore(this.context.matrixClient, groupId);
51+
this._groupStore.on('update', () => {
52+
this._fetchRooms();
53+
});
54+
this._groupStore.on('error', (err) => {
55+
console.error('Error in group store (listened to by GroupRoomList)', err);
5256
this.setState({
53-
rooms: result.chunk.map((apiRoom) => {
54-
return groupRoomFromApiObject(apiRoom);
55-
}),
56-
fetching: false,
57+
rooms: null,
5758
});
58-
}).catch((e) => {
59-
this.setState({fetching: false});
60-
console.error("Failed to get group room list: ", e);
59+
});
60+
this._fetchRooms();
61+
},
62+
63+
_fetchRooms: function() {
64+
if (this._unmounted) return;
65+
this.setState({
66+
rooms: this._groupStore.getGroupRooms().map((apiRoom) => {
67+
return groupRoomFromApiObject(apiRoom);
68+
}),
6169
});
6270
},
6371

@@ -110,12 +118,7 @@ export default React.createClass({
110118
},
111119

112120
render: function() {
113-
if (this.state.fetching) {
114-
const Spinner = sdk.getComponent("elements.Spinner");
115-
return (<div className="mx_GroupRoomList">
116-
<Spinner />
117-
</div>);
118-
} else if (this.state.rooms === null) {
121+
if (this.state.rooms === null) {
119122
return null;
120123
}
121124

src/components/views/groups/GroupRoomTile.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import PropTypes from 'prop-types';
2121
import sdk from '../../../index';
2222
import dis from '../../../dispatcher';
2323
import { GroupRoomType } from '../../../groups';
24+
import GroupStoreCache from '../../../stores/GroupStoreCache';
2425
import Modal from '../../../Modal';
2526

2627
const GroupRoomTile = React.createClass({
@@ -49,10 +50,10 @@ const GroupRoomTile = React.createClass({
4950

5051
removeRoomFromGroup: function() {
5152
const groupId = this.props.groupId;
53+
const groupStore = GroupStoreCache.getGroupStore(this.context.matrixClient, groupId);
5254
const roomName = this.state.name;
5355
const roomId = this.props.groupRoom.roomId;
54-
this.context.matrixClient
55-
.removeRoomFromGroup(groupId, roomId)
56+
groupStore.removeRoomFromGroup(roomId)
5657
.catch((err) => {
5758
console.error(`Error whilst removing ${roomId} from ${groupId}`, err);
5859
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");

src/stores/GroupStore.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export default class GroupStore extends EventEmitter {
2626
this.groupId = groupId;
2727
this._matrixClient = matrixClient;
2828
this._summary = {};
29+
this._rooms = [];
2930
this._fetchSummary();
31+
this._fetchRooms();
3032
}
3133

3234
_fetchSummary() {
@@ -38,6 +40,15 @@ export default class GroupStore extends EventEmitter {
3840
});
3941
}
4042

43+
_fetchRooms() {
44+
this._matrixClient.getGroupRooms(this.groupId).then((resp) => {
45+
this._rooms = resp.chunk;
46+
this._notifyListeners();
47+
}).catch((err) => {
48+
this.emit('error', err);
49+
});
50+
}
51+
4152
_notifyListeners() {
4253
this.emit('update');
4354
}
@@ -46,9 +57,22 @@ export default class GroupStore extends EventEmitter {
4657
return this._summary;
4758
}
4859

60+
getGroupRooms() {
61+
return this._rooms;
62+
}
63+
4964
addRoomToGroup(roomId) {
5065
return this._matrixClient
51-
.addRoomToGroup(this.groupId, roomId);
66+
.addRoomToGroup(this.groupId, roomId)
67+
.then(this._fetchRooms.bind(this));
68+
}
69+
70+
removeRoomFromGroup(roomId) {
71+
return this._matrixClient
72+
.removeRoomFromGroup(this.groupId, roomId)
73+
// Room might be in the summary, refresh just in case
74+
.then(this._fetchSummary.bind(this))
75+
.then(this._fetchRooms.bind(this));
5276
}
5377

5478
addRoomToGroupSummary(roomId, categoryId) {

src/stores/GroupStoreCache.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class GroupStoreCache {
2828
// referencing it.
2929
this.groupStore = new GroupStore(matrixClient, groupId);
3030
}
31+
this.groupStore._fetchSummary();
3132
return this.groupStore;
3233
}
3334
}

0 commit comments

Comments
 (0)