From e4d628a931cd9af1a37ce4f269bc691b61e10632 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Sat, 27 May 2023 09:43:29 +0200 Subject: [PATCH] FIX: Move thread storage out of chatApi.thread() call (#21773) In some cases activeChannel can be null so this will error, also it is limiting to have this code in chatApi. Instead move to the threads manager, and also lean on channelsManager.find to get the channel from the cache instead, which will not error. --- .../javascripts/discourse/lib/chat-threads-manager.js | 7 ++++++- .../assets/javascripts/discourse/services/chat-api.js | 8 +------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/plugins/chat/assets/javascripts/discourse/lib/chat-threads-manager.js b/plugins/chat/assets/javascripts/discourse/lib/chat-threads-manager.js index 3e198ab142f..9f5667a3038 100644 --- a/plugins/chat/assets/javascripts/discourse/lib/chat-threads-manager.js +++ b/plugins/chat/assets/javascripts/discourse/lib/chat-threads-manager.js @@ -16,6 +16,7 @@ import { TrackedObject } from "@ember-compat/tracked-built-ins"; export default class ChatThreadsManager { @service chatSubscriptionsManager; @service chatTrackingStateManager; + @service chatChannelsManager; @service chatApi; @service chat; @service currentUser; @@ -83,7 +84,11 @@ export default class ChatThreadsManager { } async #find(channelId, threadId) { - return this.chatApi.thread(channelId, threadId); + return this.chatApi.thread(channelId, threadId).then((result) => { + return this.chatChannelsManager.find(channelId).then((channel) => { + return channel.threadsManager.store(channel, result.thread); + }); + }); } #cache(thread) { diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-api.js b/plugins/chat/assets/javascripts/discourse/services/chat-api.js index e3d836568b5..012b4c03346 100644 --- a/plugins/chat/assets/javascripts/discourse/services/chat-api.js +++ b/plugins/chat/assets/javascripts/discourse/services/chat-api.js @@ -62,13 +62,7 @@ export default class ChatApi extends Service { * this.chatApi.thread(5, 1).then(thread => { ... }) */ thread(channelId, threadId) { - return this.#getRequest(`/channels/${channelId}/threads/${threadId}`).then( - (result) => - this.chat.activeChannel.threadsManager.store( - this.chat.activeChannel, - result.thread - ) - ); + return this.#getRequest(`/channels/${channelId}/threads/${threadId}`); } /**