From ced5f54500b2688a942c5c1afbb4fca3882b4f51 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Fri, 11 Nov 2016 14:55:05 -0500 Subject: Fixing new messages indicator (#4531) --- webapp/actions/global_actions.jsx | 24 +++++++----- webapp/stores/channel_store.jsx | 17 +++++++- webapp/utils/async_client.jsx | 82 ++++++++++++++++++++++----------------- webapp/utils/constants.jsx | 1 + 4 files changed, 77 insertions(+), 47 deletions(-) (limited to 'webapp') diff --git a/webapp/actions/global_actions.jsx b/webapp/actions/global_actions.jsx index 6a63b5630..f7c4c455c 100644 --- a/webapp/actions/global_actions.jsx +++ b/webapp/actions/global_actions.jsx @@ -43,16 +43,20 @@ export function emitChannelClickEvent(channel) { ); } function switchToChannel(chan) { - AsyncClient.getChannelStats(chan.id, true); - AsyncClient.updateLastViewedAt(chan.id); - loadPosts(chan.id); - trackPage(); - - AppDispatcher.handleViewAction({ - type: ActionTypes.CLICK_CHANNEL, - name: chan.name, - id: chan.id, - prev: ChannelStore.getCurrentId() + const getMyChannelMembersPromise = AsyncClient.getChannelMember(chan.id, UserStore.getCurrentId()); + + getMyChannelMembersPromise.then(() => { + AsyncClient.getChannelStats(chan.id, true); + AsyncClient.updateLastViewedAt(chan.id); + loadPosts(chan.id); + trackPage(); + + AppDispatcher.handleViewAction({ + type: ActionTypes.CLICK_CHANNEL, + name: chan.name, + id: chan.id, + prev: ChannelStore.getCurrentId() + }); }); } diff --git a/webapp/stores/channel_store.jsx b/webapp/stores/channel_store.jsx index 13a5c4574..136423d45 100644 --- a/webapp/stores/channel_store.jsx +++ b/webapp/stores/channel_store.jsx @@ -263,6 +263,12 @@ class ChannelStoreClass extends EventEmitter { }); } + setUnreadCountsByCurrentMembers() { + Object.keys(this.myChannelMembers).forEach((key) => { + this.setUnreadCountByChannel(this.myChannelMembers[key].channel_id); + }); + } + setUnreadCountsByChannels(channels) { channels.forEach((c) => { this.setUnreadCountByChannel(c.id); @@ -368,6 +374,15 @@ ChannelStore.dispatchToken = AppDispatcher.register((payload) => { ChannelStore.setUnreadCountsByMembers(action.members); ChannelStore.emitChange(); break; + case ActionTypes.RECEIVED_CHANNEL_MEMBER: + ChannelStore.storeMyChannelMember(action.member); + currentId = ChannelStore.getCurrentId(); + if (currentId && window.isActive) { + ChannelStore.resetCounts(currentId); + } + ChannelStore.setUnreadCountsByCurrentMembers(); + ChannelStore.emitChange(); + break; case ActionTypes.RECEIVED_MORE_CHANNELS: ChannelStore.storeMoreChannels(action.channels); ChannelStore.emitChange(); @@ -385,4 +400,4 @@ ChannelStore.dispatchToken = AppDispatcher.register((payload) => { } }); -export default ChannelStore; \ No newline at end of file +export default ChannelStore; diff --git a/webapp/utils/async_client.jsx b/webapp/utils/async_client.jsx index 1149c565f..efa9eeb2b 100644 --- a/webapp/utils/async_client.jsx +++ b/webapp/utils/async_client.jsx @@ -111,26 +111,31 @@ export function getChannel(id) { } export function getMyChannelMembers() { - if (isCallInProgress('getMyChannelMembers')) { - return; - } + return new Promise((resolve, reject) => { + if (isCallInProgress('getMyChannelMembers')) { + resolve(); + return; + } - callTracker.getMyChannelMembers = utils.getTimestamp(); + callTracker.getMyChannelMembers = utils.getTimestamp(); - Client.getMyChannelMembers( - (data) => { - callTracker.getMyChannelMembers = 0; + Client.getMyChannelMembers( + (data) => { + callTracker.getMyChannelMembers = 0; - AppDispatcher.handleServerAction({ - type: ActionTypes.RECEIVED_MY_CHANNEL_MEMBERS, - members: data - }); - }, - (err) => { - callTracker.getChannelsUnread = 0; - dispatchError(err, 'getMyChannelMembers'); - } - ); + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_MY_CHANNEL_MEMBERS, + members: data + }); + resolve(); + }, + (err) => { + callTracker.getChannelsUnread = 0; + dispatchError(err, 'getMyChannelMembers'); + reject(); + } + ); + }); } export function updateLastViewedAt(id, active) { @@ -263,28 +268,33 @@ export function getChannelStats(channelId = ChannelStore.getCurrentId(), doVersi } export function getChannelMember(channelId, userId) { - if (isCallInProgress(`getChannelMember${channelId}${userId}`)) { - return; - } + return new Promise((resolve, reject) => { + if (isCallInProgress(`getChannelMember${channelId}${userId}`)) { + resolve(); + return; + } - callTracker[`getChannelMember${channelId}${userId}`] = utils.getTimestamp(); + callTracker[`getChannelMember${channelId}${userId}`] = utils.getTimestamp(); - Client.getChannelMember( - channelId, - userId, - (data) => { - callTracker[`getChannelMember${channelId}${userId}`] = 0; + Client.getChannelMember( + channelId, + userId, + (data) => { + callTracker[`getChannelMember${channelId}${userId}`] = 0; - AppDispatcher.handleServerAction({ - type: ActionTypes.RECEIVED_CHANNEL_MEMBER, - member: data - }); - }, - (err) => { - callTracker[`getChannelMember${channelId}${userId}`] = 0; - dispatchError(err, 'getChannelMember'); - } - ); + AppDispatcher.handleServerAction({ + type: ActionTypes.RECEIVED_CHANNEL_MEMBER, + member: data + }); + resolve(); + }, + (err) => { + callTracker[`getChannelMember${channelId}${userId}`] = 0; + dispatchError(err, 'getChannelMember'); + reject(); + } + ); + }); } export function getUser(userId) { diff --git a/webapp/utils/constants.jsx b/webapp/utils/constants.jsx index 352401142..0da17e4b9 100644 --- a/webapp/utils/constants.jsx +++ b/webapp/utils/constants.jsx @@ -70,6 +70,7 @@ export const ActionTypes = keyMirror({ RECEIVED_CHANNELS: null, RECEIVED_CHANNEL: null, + RECEIVED_CHANNEL_MEMBER: null, RECEIVED_MORE_CHANNELS: null, RECEIVED_CHANNEL_STATS: null, RECEIVED_MY_CHANNEL_MEMBERS: null, -- cgit v1.2.3-1-g7c22