From 72e46b98a90d7bea2f21138d2c66d2695b915e39 Mon Sep 17 00:00:00 2001
From: Martin Brennan <martin@discourse.org>
Date: Mon, 29 May 2023 17:37:17 +0200
Subject: [PATCH] FIX: Create original message user thread membership (#21808)

When a thread is created / a new message is created in the
thread, we want to make sure that the original message user
has a membership for that thread, otherwise they will not
receive unread indicators for messages in the thread.
---
 .../discourse/components/chat-thread.hbs      |  6 ++++-
 plugins/chat/lib/chat/message_creator.rb      |  7 ++++++
 .../components/chat/message_creator_spec.rb   | 24 ++++++++++++++++++-
 .../system/reply_to_message/full_page_spec.rb |  1 +
 4 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-thread.hbs b/plugins/chat/assets/javascripts/discourse/components/chat-thread.hbs
index 6ef197caa06..e2a1bf669fd 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat-thread.hbs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat-thread.hbs
@@ -1,5 +1,9 @@
 <div
-  class={{concat-class "chat-thread" (if this.loading "loading")}}
+  class={{concat-class
+    "chat-thread"
+    (if this.loading "loading")
+    (if this.thread.staged "staged")
+  }}
   data-id={{this.thread.id}}
   {{did-insert this.setUploadDropZone}}
   {{did-insert this.subscribeToUpdates}}
diff --git a/plugins/chat/lib/chat/message_creator.rb b/plugins/chat/lib/chat/message_creator.rb
index 0f374bec29b..b7cc6f738cd 100644
--- a/plugins/chat/lib/chat/message_creator.rb
+++ b/plugins/chat/lib/chat/message_creator.rb
@@ -233,6 +233,13 @@ module Chat
       return if resolved_thread.blank?
       resolved_thread.increment_replies_count_cache
       Chat::UserChatThreadMembership.find_or_create_by!(user: @user, thread: resolved_thread)
+
+      if resolved_thread.original_message_user != @user
+        Chat::UserChatThreadMembership.find_or_create_by!(
+          user: resolved_thread.original_message_user,
+          thread: resolved_thread,
+        )
+      end
     end
   end
 end
diff --git a/plugins/chat/spec/components/chat/message_creator_spec.rb b/plugins/chat/spec/components/chat/message_creator_spec.rb
index d21ab7356de..ba4f15ef830 100644
--- a/plugins/chat/spec/components/chat/message_creator_spec.rb
+++ b/plugins/chat/spec/components/chat/message_creator_spec.rb
@@ -558,13 +558,30 @@ describe Chat::MessageCreator do
               content: "this is a message",
               in_reply_to_id: reply_message.id,
             ).chat_message
-        }.to change { Chat::UserChatThreadMembership.count }
+        }.to change { Chat::UserChatThreadMembership.count }.by(2)
 
         expect(
           Chat::UserChatThreadMembership.exists?(user: user1, thread: message.thread),
         ).to be_truthy
       end
 
+      it "creates a thread membership for the original message user" do
+        message = nil
+        expect {
+          message =
+            described_class.create(
+              chat_channel: public_chat_channel,
+              user: user1,
+              content: "this is a message",
+              in_reply_to_id: reply_message.id,
+            ).chat_message
+        }.to change { Chat::UserChatThreadMembership.count }.by(2)
+
+        expect(
+          Chat::UserChatThreadMembership.exists?(user: reply_message.user, thread: message.thread),
+        ).to be_truthy
+      end
+
       context "when threading is enabled" do
         it "publishes the new thread" do
           public_chat_channel.update!(threading_enabled: true)
@@ -664,6 +681,11 @@ describe Chat::MessageCreator do
 
         it "does not create a thread membership if one exists" do
           Fabricate(:user_chat_thread_membership, user: user1, thread: existing_thread)
+          Fabricate(
+            :user_chat_thread_membership,
+            user: existing_thread.original_message_user,
+            thread: existing_thread,
+          )
           expect {
             described_class.create(
               chat_channel: public_chat_channel,
diff --git a/plugins/chat/spec/system/reply_to_message/full_page_spec.rb b/plugins/chat/spec/system/reply_to_message/full_page_spec.rb
index d2e26192c40..767f1cc0ef3 100644
--- a/plugins/chat/spec/system/reply_to_message/full_page_spec.rb
+++ b/plugins/chat/spec/system/reply_to_message/full_page_spec.rb
@@ -42,6 +42,7 @@ RSpec.describe "Reply to message - channel - full page", type: :system, js: true
         thread_page.click_send_message
 
         expect(thread_page).to have_message(text: "reply to message")
+        expect(channel_page).to have_thread_indicator(original_message)
 
         refresh