calculate badges for communities in TagOrderStore

pull/21833/head
Bruno Windels 2019-02-06 14:29:40 +00:00
parent 97db9cd99e
commit a222ee33df
2 changed files with 38 additions and 0 deletions

View File

@ -203,6 +203,14 @@ class GroupStore extends EventEmitter {
return this._ready[id][groupId];
}
getGroupIdsForRoomId(roomId) {
const groupIds = Object.keys(this._state[this.STATE_KEY.GroupRooms]);
return groupIds.filter(groupId => {
const rooms = this._state[this.STATE_KEY.GroupRooms][groupId] || [];
return rooms.some(room => room.roomId === roomId);
});
}
getSummary(groupId) {
return this._state[this.STATE_KEY.Summary][groupId] || {};
}

View File

@ -15,7 +15,10 @@ limitations under the License.
*/
import {Store} from 'flux/utils';
import dis from '../dispatcher';
import GroupStore from './GroupStore';
import Analytics from '../Analytics';
import * as RoomNotifs from "../RoomNotifs";
import MatrixClientPeg from '../MatrixClientPeg';
const INITIAL_STATE = {
orderedTags: null,
@ -47,7 +50,15 @@ class TagOrderStore extends Store {
__onDispatch(payload) {
switch (payload.action) {
// Initialise state after initial sync
case 'view_room': {
const relatedGroupIds = GroupStore.getGroupIdsForRoomId(payload.room_id);
this._updateBadges(relatedGroupIds);
break;
}
case 'MatrixActions.sync': {
if (payload.state === 'SYNCING' || payload.state === 'PREPARED') {
this._updateBadges();
}
if (!(payload.prevState !== 'PREPARED' && payload.state === 'PREPARED')) {
break;
}
@ -164,6 +175,20 @@ class TagOrderStore extends Store {
}
}
_updateBadges(groupIds = this._state.joinedGroupIds) {
if (groupIds && groupIds.length) {
const client = MatrixClientPeg.get();
const changedBadges = {};
groupIds.forEach(groupId => {
const rooms = GroupStore.getGroupRooms(groupId).map(r => client.getRoom(r.roomId));
const badge = rooms && RoomNotifs.aggregateNotificationCount(rooms);
changedBadges[groupId] = (badge && badge.count !== 0) ? badge : undefined;
});
const newBadges = Object.assign({}, this._state.badges, changedBadges);
this._setState({badges: newBadges});
}
}
_updateOrderedTags() {
this._setState({
orderedTags:
@ -190,6 +215,11 @@ class TagOrderStore extends Store {
return tagsToKeep.concat(groupIdsToAdd);
}
getGroupBadge(groupId) {
const badges = this._state.badges;
return badges && badges[groupId];
}
getOrderedTags() {
return this._state.orderedTags;
}