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

Prior to this fix if a user was answering to one of the listed screen it wouldn't update while you look at the list.
This commit is contained in:
Joffrey JAFFEUX 2024-02-29 12:31:20 +01:00 committed by GitHub
parent 49e67d32fb
commit 0bb492c6b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 47 additions and 5 deletions

View File

@ -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 List from "discourse/plugins/chat/discourse/components/chat/list";
import ThreadIndicator from "discourse/plugins/chat/discourse/components/chat-message-thread-indicator"; import ThreadIndicator from "discourse/plugins/chat/discourse/components/chat-message-thread-indicator";
import ThreadTitle from "discourse/plugins/chat/discourse/components/thread-title"; 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 { export default class UserThreads extends Component {
@service chat; @service chat;
@service chatApi; @service chatApi;
@service chatChannelsManager;
@cached @cached
get threadsCollection() { get threadsCollection() {
@ -22,9 +21,10 @@ export default class UserThreads extends Component {
@bind @bind
handleLoadedThreads(result) { handleLoadedThreads(result) {
return result.threads.map((threadObject) => { return result.threads.map((threadObject) => {
const channel = ChatChannel.create(threadObject.channel); const channel = this.chatChannelsManager.store(threadObject.channel);
const thread = ChatThread.create(channel, threadObject); const thread = channel.threadsManager.add(channel, threadObject);
const tracking = result.tracking[thread.id]; const tracking = result.tracking[thread.id];
if (tracking) { if (tracking) {
thread.tracking.mentionCount = tracking.mention_count; thread.tracking.mentionCount = tracking.mention_count;
thread.tracking.unreadCount = tracking.unread_count; thread.tracking.unreadCount = tracking.unread_count;

View File

@ -1,5 +1,6 @@
import { tracked } from "@glimmer/tracking"; import { tracked } from "@glimmer/tracking";
import { TrackedArray } from "@ember-compat/tracked-built-ins"; import { TrackedArray } from "@ember-compat/tracked-built-ins";
import User from "discourse/models/user";
export default class ChatThreadPreview { export default class ChatThreadPreview {
static create(args = {}) { static create(args = {}) {
@ -36,4 +37,17 @@ export default class ChatThreadPreview {
get otherParticipantCount() { get otherParticipantCount() {
return this.participantCount - this.participantUsers.length; 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;
}
} }

View File

@ -265,6 +265,7 @@ export default class ChatSubscriptionsManager extends Service {
busData.thread_id, busData.thread_id,
busData.message.created_at busData.message.created_at
); );
thread.preview.updateFromMessageObject(busData.message);
thread.tracking.unreadCount++; thread.tracking.unreadCount++;
this._updateActiveLastViewedAt(channel); this._updateActiveLastViewedAt(channel);
} }

View File

@ -59,6 +59,7 @@ module PageObjects
def visit_user_threads def visit_user_threads
visit("/chat/threads") visit("/chat/threads")
has_css?(".spinner")
has_no_css?(".spinner") has_no_css?(".spinner")
end end

View File

@ -41,6 +41,14 @@ module PageObjects
find(@context).has_no_css?(".chat-thread-participants") find(@context).has_no_css?(".chat-thread-participants")
end 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 def excerpt
find(@context).find("#{SELECTOR}__last-reply-excerpt") find(@context).find("#{SELECTOR}__last-reply-excerpt")
end end

View File

@ -54,7 +54,7 @@ RSpec.describe "Reply to message - smoke", type: :system do
using_session(:user_1) do using_session(:user_1) do
expect(thread_page.messages).to have_message(text: "user1reply") expect(thread_page.messages).to have_message(text: "user1reply")
expect(thread_page.messages).to have_message(text: "user2reply") 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 end
end end

View File

@ -114,6 +114,24 @@ RSpec.describe "User threads", type: :system do
expect(user_threads_page).to have_threads expect(user_threads_page).to have_threads
end 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 end
context "when in drawer" do context "when in drawer" do