FIX: Make sure marking channels read propagates to all tabs (#20802)

Instead of just marking the state read in JS for each channel
after the AJAX call, we can instead just rely on the MessageBus
user-tracking-state chat channel, and publish the state to all
the channels affected in MarkAllUserChannelsRead. This will make
it so the blue dots for the channels are cleared across all tabs.
This commit is contained in:
Martin Brennan 2023-03-24 19:12:35 +10:00 committed by GitHub
parent d2617c4904
commit 55ef4d9a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 11 deletions

View File

@ -17,6 +17,7 @@ module Chat
transaction do transaction do
step :update_last_read_message_ids step :update_last_read_message_ids
step :mark_associated_mentions_as_read step :mark_associated_mentions_as_read
step :publish_user_tracking_state
end end
private private
@ -51,5 +52,15 @@ module Chat
channel_ids: updated_memberships.map(&:channel_id), channel_ids: updated_memberships.map(&:channel_id),
) )
end end
def publish_user_tracking_state(guardian:, updated_memberships:, **)
updated_memberships.each do |membership|
Chat::Publisher.publish_user_tracking_state(
guardian.user,
membership.channel_id,
membership.last_read_message_id,
)
end
end
end end
end end

View File

@ -85,17 +85,8 @@ export default class ChatChannelsManager extends Service {
@debounce(300) @debounce(300)
async markAllChannelsRead() { async markAllChannelsRead() {
return this.chatApi.markAllChannelsAsRead().then((response) => { // The user tracking state for each channel marked read will be propagated by MessageBus
response.updated_memberships.forEach((membership) => { return this.chatApi.markAllChannelsAsRead();
let channel = this.channels.findBy("id", membership.channel_id);
if (channel) {
channel.currentUserMembership.unread_count = 0;
channel.currentUserMembership.unread_mentions = 0;
channel.currentUserMembership.last_read_message_id =
membership.last_read_message_id;
}
});
});
} }
remove(model) { remove(model) {

View File

@ -128,6 +128,13 @@ RSpec.describe Chat::MarkAllUserChannelsRead do
).count ).count
}.by(-2) }.by(-2)
end end
it "publishes tracking state for all affected channels" do
messages = MessageBus.track_publish { result }
expect(
messages.select { |m| m.channel == "/chat/user-tracking-state/#{current_user.id}" }.count,
).to eq(3)
end
end end
end end
end end