Skip to content

A platform for community discussion. Free, open, simple.

FIX: live updates threads from my threads page (#25955)

Joffrey JAFFEUX0bb492c6b6b7
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 945a876371c..bf9deae6a66 100644
--- a/plugins/chat/assets/javascripts/discourse/components/user-threads/index.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/user-threads/index.gjs
@@ -7,12 +7,11 @@ import ChannelTitle from "discourse/plugins/chat/discourse/components/channel-ti
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 ChatChannel from "discourse/plugins/chat/discourse/models/chat-channel";
-import ChatThread from "discourse/plugins/chat/discourse/models/chat-thread";
export default class UserThreads extends Component {
@service chat;
@service chatApi;
+ @service chatChannelsManager;
@cached
get threadsCollection() {
@@ -22,9 +21,10 @@ export default class UserThreads extends Component {
@bind
handleLoadedThreads(result) {
return result.threads.map((threadObject) => {
- const channel = ChatChannel.create(threadObject.channel);
- const thread = ChatThread.create(channel, threadObject);
+ const channel = this.chatChannelsManager.store(threadObject.channel);
+ const thread = channel.threadsManager.add(channel, threadObject);
const tracking = result.tracking[thread.id];
+
if (tracking) {
thread.tracking.mentionCount = tracking.mention_count;
thread.tracking.unreadCount = tracking.unread_count;
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-thread-preview.js b/plugins/chat/assets/javascripts/discourse/models/chat-thread-preview.js
index 22547a071aa..37325681a54 100644
--- a/plugins/chat/assets/javascripts/discourse/models/chat-thread-preview.js
+++ b/plugins/chat/assets/javascripts/discourse/models/chat-thread-preview.js
@@ -1,5 +1,6 @@
import { tracked } from "@glimmer/tracking";
import { TrackedArray } from "@ember-compat/tracked-built-ins";
+import User from "discourse/models/user";
export default class ChatThreadPreview {
static create(args = {}) {
@@ -36,4 +37,17 @@ export default class ChatThreadPreview {
get otherParticipantCount() {
return this.participantCount - this.participantUsers.length;
}
+
+ updateFromMessageObject(messageObject) {
+ const user = User.create(messageObject.user);
+ if (!this.participantUsers.find((u) => u.id === user.id)) {
+ this.participantUsers.push(user);
+ this.participantCount += 1;
+ }
+ this.replyCount += 1;
+ this.lastReplyAt = messageObject.created_at;
+ this.lastReplyId = messageObject.id;
+ this.lastReplyExcerpt = messageObject.excerpt;
+ this.lastReplyUser = user;
+ }
}
diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js
index 1a6dde0cc52..1ebbc068415 100644
--- a/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js
+++ b/plugins/chat/assets/javascripts/discourse/services/chat-subscriptions-manager.js
@@ -265,6 +265,7 @@ export default class ChatSubscriptionsManager extends Service {
busData.thread_id,
busData.message.created_at
);
+ thread.preview.updateFromMessageObject(busData.message);
thread.tracking.unreadCount++;
this._updateActiveLastViewedAt(channel);
}
diff --git a/plugins/chat/spec/system/page_objects/chat/chat.rb b/plugins/chat/spec/system/page_objects/chat/chat.rb
index f86e1d3660d..24060a02bf7 100644
--- a/plugins/chat/spec/system/page_objects/chat/chat.rb
+++ b/plugins/chat/spec/system/page_objects/chat/chat.rb
@@ -59,6 +59,7 @@ module PageObjects
def visit_user_threads
visit("/chat/threads")
+ has_css?(".spinner")
has_no_css?(".spinner")
end
diff --git a/plugins/chat/spec/system/page_objects/chat/components/thread_indicator.rb b/plugins/chat/spec/system/page_objects/chat/components/thread_indicator.rb
index 5f6a69c2308..bea1ff5ab8b 100644
--- a/plugins/chat/spec/system/page_objects/chat/components/thread_indicator.rb
+++ b/plugins/chat/spec/system/page_objects/chat/components/thread_indicator.rb
@@ -41,6 +41,14 @@ module PageObjects
find(@context).has_no_css?(".chat-thread-participants")
end
+ def has_excerpt?(text)
+ find(@context).find("#{SELECTOR}__last-reply-excerpt", text: text)
+ end
+
+ def has_user?(user)
+ find(@context).find("#{SELECTOR}__last-reply-username", text: user.username)
+ end
+
def excerpt
find(@context).find("#{SELECTOR}__last-reply-excerpt")
end
diff --git a/plugins/chat/spec/system/reply_to_message/smoke_spec.rb b/plugins/chat/spec/system/reply_to_message/smoke_spec.rb
index 70b88b91168..0ae77982b9c 100644
--- a/plugins/chat/spec/system/reply_to_message/smoke_spec.rb
+++ b/plugins/chat/spec/system/reply_to_message/smoke_spec.rb
@@ -54,7 +54,7 @@ RSpec.describe "Reply to message - smoke", type: :system do
using_session(:user_1) do
expect(thread_page.messages).to have_message(text: "user1reply")
expect(thread_page.messages).to have_message(text: "user2reply")
- expect(channel_page.message_thread_indicator(original_message)).to have_reply_count(2)
+ expect(channel_page.message_thread_indicator(original_message)).to have_reply_count(3)
end
end
end
diff --git a/plugins/chat/spec/system/user_threads_spec.rb b/plugins/chat/spec/system/user_threads_spec.rb
index 09f985dd436..6c68554a73d 100644
--- a/plugins/chat/spec/system/user_threads_spec.rb
+++ b/plugins/chat/spec/system/user_threads_spec.rb
@@ -114,6 +114,24 @@ RSpec.describe "User threads", type: :system do
expect(user_threads_page).to have_threads
end
+
+ it "updates the thread when another user replies" do
+ chat_thread_chain_bootstrap(channel: channel_1, users: [current_user, Fabricate(:user)])
+ thread = channel_1.threads.last
+ message = thread.original_message
+ last_user = Fabricate(:user)
+
+ chat_page.visit_user_threads
+
+ last_message = Fabricate(:chat_message, thread: thread, user: last_user, use_service: true)
+
+ indicator = PageObjects::Components::Chat::ThreadIndicator.new(".c-user-thread")
+
+ expect(indicator).to have_reply_count(4)
+ expect(indicator).to have_participant(last_user)
+ expect(indicator).to have_excerpt(last_message.excerpt)
+ expect(indicator).to have_user(last_user)
+ end
end
context "when in drawer" do