From a222ee33df781597744451209685138735433692 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Wed, 6 Feb 2019 14:29:40 +0000 Subject: [PATCH] calculate badges for communities in TagOrderStore --- src/stores/GroupStore.js | 8 ++++++++ src/stores/TagOrderStore.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/stores/GroupStore.js b/src/stores/GroupStore.js index 4ac1e42e2e..637e87b728 100644 --- a/src/stores/GroupStore.js +++ b/src/stores/GroupStore.js @@ -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] || {}; } diff --git a/src/stores/TagOrderStore.js b/src/stores/TagOrderStore.js index eef078d8da..8ecc47d430 100644 --- a/src/stores/TagOrderStore.js +++ b/src/stores/TagOrderStore.js @@ -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; }