From 4f2dfd3857322fd4000f3c2f961a4a867e6d2bd9 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Wed, 8 Mar 2023 17:28:39 +0100 Subject: [PATCH] FIX: correctly syncs current user message in multiple sessions (#20584) --- .../discourse/components/chat-live-pane.js | 54 +++++++++---------- plugins/chat/spec/system/chat_channel_spec.rb | 18 +++++++ 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-live-pane.js b/plugins/chat/assets/javascripts/discourse/components/chat-live-pane.js index 8a4410f2e6c..817a1c4cc2f 100644 --- a/plugins/chat/assets/javascripts/discourse/components/chat-live-pane.js +++ b/plugins/chat/assets/javascripts/discourse/components/chat-live-pane.js @@ -517,7 +517,8 @@ export default class ChatLivePane extends Component { } } - handleMessage(data) { + @bind + onMessage(data) { switch (data.type) { case "sent": this.handleSentMessage(data); @@ -555,29 +556,26 @@ export default class ChatLivePane extends Component { } } - _handleOwnSentMessage(data) { - const stagedMessage = this.args.channel.findStagedMessage(data.staged_id); - if (stagedMessage) { - stagedMessage.error = null; - stagedMessage.id = data.chat_message.id; - stagedMessage.staged = false; - stagedMessage.excerpt = data.chat_message.excerpt; - stagedMessage.threadId = data.chat_message.thread_id; - stagedMessage.channelId = data.chat_message.chat_channel_id; - stagedMessage.createdAt = data.chat_message.created_at; + _handleStagedMessage(stagedMessage, data) { + stagedMessage.error = null; + stagedMessage.id = data.chat_message.id; + stagedMessage.staged = false; + stagedMessage.excerpt = data.chat_message.excerpt; + stagedMessage.threadId = data.chat_message.thread_id; + stagedMessage.channelId = data.chat_message.chat_channel_id; + stagedMessage.createdAt = data.chat_message.created_at; - const inReplyToMsg = this.args.channel.findMessage( - data.chat_message.in_reply_to?.id - ); - if (inReplyToMsg && !inReplyToMsg.threadId) { - inReplyToMsg.threadId = data.chat_message.thread_id; - } + const inReplyToMsg = this.args.channel.findMessage( + data.chat_message.in_reply_to?.id + ); + if (inReplyToMsg && !inReplyToMsg.threadId) { + inReplyToMsg.threadId = data.chat_message.thread_id; + } - // some markdown is cooked differently on the server-side, e.g. - // quotes, avatar images etc. - if (data.chat_message?.cooked !== stagedMessage.cooked) { - stagedMessage.cooked = data.chat_message.cooked; - } + // some markdown is cooked differently on the server-side, e.g. + // quotes, avatar images etc. + if (data.chat_message?.cooked !== stagedMessage.cooked) { + stagedMessage.cooked = data.chat_message.cooked; } } @@ -587,7 +585,10 @@ export default class ChatLivePane extends Component { } if (data.chat_message.user.id === this.currentUser.id && data.staged_id) { - return this._handleOwnSentMessage(data); + const stagedMessage = this.args.channel.findStagedMessage(data.staged_id); + if (stagedMessage) { + return this._handleStagedMessage(stagedMessage, data); + } } if (this.args.channel.canLoadMoreFuture) { @@ -1138,13 +1139,6 @@ export default class ChatLivePane extends Component { ); } - @bind - onMessage(busData) { - if (!this.args.channel.canLoadMoreFuture || busData.type !== "sent") { - this.handleMessage(busData); - } - } - @bind _forceBodyScroll() { // when keyboard is visible this will ensure body diff --git a/plugins/chat/spec/system/chat_channel_spec.rb b/plugins/chat/spec/system/chat_channel_spec.rb index b2b83693974..455a5c17ace 100644 --- a/plugins/chat/spec/system/chat_channel_spec.rb +++ b/plugins/chat/spec/system/chat_channel_spec.rb @@ -33,6 +33,24 @@ RSpec.describe "Chat channel", type: :system, js: true do end end + context "with two sessions opened on same channel" do + it "syncs the messages" do + using_session(:tab_1) do + sign_in(current_user) + chat.visit_channel(channel_1) + end + + using_session(:tab_2) do + sign_in(current_user) + chat.visit_channel(channel_1) + end + + using_session(:tab_1) { channel.send_message("test_message") } + + using_session(:tab_2) { expect(channel).to have_message(text: "test_message") } + end + end + it "allows to edit this message once persisted" do chat.visit_channel(channel_1) channel.send_message("aaaaaaaaaaaaaaaaaaaa")