From 351d212e8a9a3e78afcdb886128714d631b6a95d Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Fri, 26 Apr 2024 12:32:06 +0200 Subject: [PATCH] FIX: do not increment reply count manually (#26769) That could cause flakeyness in specs depending in which timing message bus would arrive and it's not necessary as it should be updated with `handleThreadOriginalMessageUpdate`. --- plugins/chat/app/services/chat/publisher.rb | 2 + .../components/user-threads/index.gjs | 49 +++++++++++++++++++ .../discourse/models/chat-thread-preview.js | 14 ------ .../services/chat-subscriptions-manager.js | 1 - .../system/reply_to_message/smoke_spec.rb | 5 +- plugins/chat/spec/system/user_threads_spec.rb | 2 - 6 files changed, 54 insertions(+), 19 deletions(-) diff --git a/plugins/chat/app/services/chat/publisher.rb b/plugins/chat/app/services/chat/publisher.rb index a368fa2f041..4955edf50fd 100644 --- a/plugins/chat/app/services/chat/publisher.rb +++ b/plugins/chat/app/services/chat/publisher.rb @@ -92,6 +92,8 @@ module Chat { type: :update_thread_original_message, original_message_id: thread.original_message_id, + thread_id: thread.id, + channel_id: thread.channel.id, preview: preview.as_json, }, ) diff --git a/plugins/chat/assets/javascripts/discourse/components/user-threads/index.gjs b/plugins/chat/assets/javascripts/discourse/components/user-threads/index.gjs index 564f08d26a9..e311e2f4923 100644 --- a/plugins/chat/assets/javascripts/discourse/components/user-threads/index.gjs +++ b/plugins/chat/assets/javascripts/discourse/components/user-threads/index.gjs @@ -9,18 +9,32 @@ import List from "discourse/plugins/chat/discourse/components/chat/list"; import ThreadIndicator from "discourse/plugins/chat/discourse/components/chat-message-thread-indicator"; import ThreadTitle from "discourse/plugins/chat/discourse/components/thread-title"; import ThreadPreview from "discourse/plugins/chat/discourse/components/user-threads/preview"; +import ChatThreadPreview from "discourse/plugins/chat/discourse/models/chat-thread-preview"; export default class UserThreads extends Component { @service chat; @service chatApi; @service chatChannelsManager; + @service messageBus; @service site; + trackedChannels = {}; + @cached get threadsCollection() { return this.chatApi.userThreads(this.handleLoadedThreads); } + willDestroy() { + super.willDestroy(...arguments); + + Object.keys(this.trackedChannels).forEach((id) => { + this.messageBus.unsubscribe(`/chat/${id}`, this.onMessage); + }); + + this.trackedChannels = {}; + } + @bind handleLoadedThreads(result) { return result.threads.map((threadObject) => { @@ -32,10 +46,45 @@ export default class UserThreads extends Component { thread.tracking.mentionCount = tracking.mention_count; thread.tracking.unreadCount = tracking.unread_count; } + + this.trackChannel(thread.channel); return thread; }); } + trackChannel(channel) { + if (this.trackedChannels[channel.id]) { + return; + } + + this.trackedChannels[channel.id] = channel; + + this.messageBus.subscribe( + `/chat/${channel.id}`, + this.onMessage, + channel.channelMessageBusLastId + ); + } + + @bind + onMessage(data) { + if (data.type === "update_thread_original_message") { + const channel = this.trackedChannels[data.channel_id]; + + if (!channel) { + return; + } + + const thread = channel.threadsManager.threads.find( + (t) => t.id === data.thread_id + ); + + if (thread) { + thread.preview = ChatThreadPreview.create(data.preview); + } + } + } +