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