From 41276481a66e22a747527fa6a886a27f04e42b0e Mon Sep 17 00:00:00 2001 From: Midgard Date: Wed, 8 Nov 2023 14:40:13 +0100 Subject: [PATCH] Support new multiple_channels_viewed event --- js/controller.js | 16 ++++++++++++++-- js/mm_client.js | 9 ++++++++- js/pubsub.js | 2 +- js/store.js | 7 +++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/js/controller.js b/js/controller.js index 52eb7cd..4182a26 100644 --- a/js/controller.js +++ b/js/controller.js @@ -223,8 +223,20 @@ pubsub.subscribe("MESSAGES_NEW", post => { client.channelStore.updateLastPostTime(post.channel_id, post["create_at"]); }); -pubsub.subscribe("CHANNEL_READ", ({endpoint, channel_id}) => { - mm_client.getOrCreate(endpoint).channelStore.setUnread(channel_id, 0, 0); +pubsub.subscribe("CHANNEL_READ", ({endpoint, channel_id, time}) => { + const chanStore = mm_client.getOrCreate(endpoint).channelStore; + const store = chanStore.getNoFetch(); + if ( + store !== null && channel_id in store.channels && + time !== null && time < store.channels[channel_id]["last_post_at"] + ) { + // We don't keep track of messages, immediately writing them to the DOM instead, so we don't + // know how many unread messages there are when we get a "channel read up to a certain + // timestamp" if there are newer messages than that. So refetch the channel list. + chanStore.fetch(); + } else { + mm_client.getOrCreate(endpoint).channelStore.setUnread(channel_id, 0, 0); + } }); pubsub.subscribe("WINDOW_FOCUSED", () => { diff --git a/js/mm_client.js b/js/mm_client.js index f70a036..e13a641 100644 --- a/js/mm_client.js +++ b/js/mm_client.js @@ -245,7 +245,14 @@ class MattermostClient { pubsub.publish("MESSAGES_NEW", {...post, endpoint: this.endpoint}); } else if (data.event === "channel_viewed") { - pubsub.publish("CHANNEL_READ", {endpoint: this.endpoint, channel_id: data.data.channel_id}); + pubsub.publish("CHANNEL_READ", {endpoint: this.endpoint, channel_id: data.data.channel_id, time: null}); + + } else if (data.event === "multiple_channels_viewed") { + console.log(data.data.channel_times); + for (let channel_id in data.data.channel_times) { + const time = data.data.channel_times[channel_id]; + pubsub.publish("CHANNEL_READ", {endpoint: this.endpoint, channel_id, time}); + } } else if ( data.event === "user_added" || diff --git a/js/pubsub.js b/js/pubsub.js index 603acf3..fd53287 100644 --- a/js/pubsub.js +++ b/js/pubsub.js @@ -11,7 +11,7 @@ const topics = [ "USERS_CHANGED", "CHANNEL_MEMBERS_NEW", "CHANNEL_MEMBERS_REMOVED", - "CHANNEL_READ", // {endpoint, channel_id} + "CHANNEL_READ", // {endpoint, channel_id, time} "WINDOW_FOCUSED", ]; diff --git a/js/store.js b/js/store.js index 213b08f..f6d3b29 100644 --- a/js/store.js +++ b/js/store.js @@ -62,6 +62,13 @@ class ChannelStore { return {teams: this._teams, channels: this._channels, unread: this._unread}; } + getNoFetch() { + if (this._promise === null) { + return null; + } + return {teams: this._teams, channels: this._channels, unread: this._unread}; + } + async updateChannel(channel) { if (this._promise === null) { // Data does not exist yet and is not being requested