DEV: Introduce bulk channel tracking publisher (#20838)

This commit introduces a Chat::Publisher and MessageBus endpoint
that allows for updating a user's channel tracking state in bulk for
multiple channels, rather than having to do it for one channel
at a time.

This also required an improvement to ChannelUnreadsQuery -- now
multiple channel IDs can be passed to this to get the unread counts
and mention counts for those channels for a user, also increasing
efficiency rather than having to do a query for every individual
channel.

Followup to #20802
This commit is contained in:
Martin Brennan
2023-03-28 09:36:28 +10:00
committed by GitHub
parent 326a7a47e7
commit cab4b2cfba
6 changed files with 184 additions and 64 deletions

View File

@ -213,6 +213,11 @@ export default class ChatSubscriptionsManager extends Service {
this._onUserTrackingStateUpdate,
lastId
);
this.messageBus.subscribe(
`/chat/bulk-user-tracking-state/${this.currentUser.id}`,
this._onBulkUserTrackingStateUpdate,
lastId
);
}
_stopUserTrackingStateSubscription() {
@ -224,20 +229,38 @@ export default class ChatSubscriptionsManager extends Service {
`/chat/user-tracking-state/${this.currentUser.id}`,
this._onUserTrackingStateUpdate
);
this.messageBus.unsubscribe(
`/chat/bulk-user-tracking-state/${this.currentUser.id}`,
this._onBulkUserTrackingStateUpdate
);
}
@bind
_onBulkUserTrackingStateUpdate(busData) {
Object.keys(busData).forEach((channelId) => {
this._updateChannelTrackingData(channelId, busData[channelId]);
});
}
@bind
_onUserTrackingStateUpdate(busData) {
this.chatChannelsManager.find(busData.chat_channel_id).then((channel) => {
this._updateChannelTrackingData(busData.channel_id, busData);
}
@bind
_updateChannelTrackingData(channelId, trackingData) {
this.chatChannelsManager.find(channelId).then((channel) => {
if (
!channel?.currentUserMembership?.last_read_message_id ||
parseInt(channel?.currentUserMembership?.last_read_message_id, 10) <=
busData.chat_message_id
trackingData.last_read_message_id
) {
channel.currentUserMembership.last_read_message_id =
busData.chat_message_id;
channel.currentUserMembership.unread_count = busData.unread_count;
channel.currentUserMembership.unread_mentions = busData.unread_mentions;
trackingData.last_read_message_id;
channel.currentUserMembership.unread_count = trackingData.unread_count;
channel.currentUserMembership.unread_mentions =
trackingData.mention_count;
}
});
}