From 895898b363df8dbd060345f384de04734c2410ce Mon Sep 17 00:00:00 2001
From: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Date: Mon, 14 Nov 2022 08:16:09 +0100
Subject: [PATCH] FIX: correctly opens drawer to message id when given (#18994)

---
 .../discourse/components/topic-chat-float.js  |  4 +--
 plugins/chat/spec/system/navigation_spec.rb   | 27 +++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/plugins/chat/assets/javascripts/discourse/components/topic-chat-float.js b/plugins/chat/assets/javascripts/discourse/components/topic-chat-float.js
index d13ccf1eef7..7df6d32ec4b 100644
--- a/plugins/chat/assets/javascripts/discourse/components/topic-chat-float.js
+++ b/plugins/chat/assets/javascripts/discourse/components/topic-chat-float.js
@@ -203,6 +203,7 @@ export default Component.extend({
 
   @action
   openURL(URL = null) {
+    this.chat.setActiveChannel(null);
     this.set("hidden", false);
     this.set("expanded", true);
 
@@ -214,12 +215,10 @@ export default Component.extend({
 
     switch (route.name) {
       case "chat":
-        this.chat.setActiveChannel(null);
         this.set("view", LIST_VIEW);
         this.appEvents.trigger("chat:float-toggled", false);
         return;
       case "chat.draft-channel":
-        this.chat.setActiveChannel(null);
         this.set("view", DRAFT_CHANNEL_VIEW);
         this.appEvents.trigger("chat:float-toggled", false);
         return;
@@ -227,6 +226,7 @@ export default Component.extend({
         return this.chat
           .getChannelBy("id", route.params.channelId)
           .then((channel) => {
+            this.chat.set("messageId", route.queryParams.messageId);
             this.chat.setActiveChannel(channel);
             this.set("view", CHAT_VIEW);
             this.appEvents.trigger("chat:float-toggled", false);
diff --git a/plugins/chat/spec/system/navigation_spec.rb b/plugins/chat/spec/system/navigation_spec.rb
index 59f239cac6f..3c48cb2620e 100644
--- a/plugins/chat/spec/system/navigation_spec.rb
+++ b/plugins/chat/spec/system/navigation_spec.rb
@@ -99,6 +99,33 @@ RSpec.describe "Navigation", type: :system, js: true do
     end
   end
 
+  context "when opening full page with a link containing a message id" do
+    it "highlights correct message" do
+      visit("/chat/channel/#{category_channel.id}/#{category_channel.slug}?messageId=#{message.id}")
+
+      expect(page).to have_css(
+        ".full-page-chat .chat-message-container.highlighted[data-id='#{message.id}']",
+      )
+    end
+  end
+
+  context "when opening drawer with a link containing a message id" do
+    it "highlights correct message" do
+      Fabricate(
+        :post,
+        topic: topic,
+        raw:
+          "<a href=\"/chat/channel/#{category_channel.id}/#{category_channel.slug}?messageId=#{message.id}\">foo</a>",
+      )
+      visit("/t/-/#{topic.id}")
+      find("a", text: "foo").click
+
+      expect(page).to have_css(
+        ".topic-chat-container.expanded.visible .chat-message-container.highlighted[data-id='#{message.id}']",
+      )
+    end
+  end
+
   context "when sidebar is enabled" do
     before do
       SiteSetting.enable_experimental_sidebar_hamburger = true