From b3802e12f036da0663baa1968ec18bfdf94cda77 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 23 May 2024 11:08:55 +0200 Subject: [PATCH] FIX: correct in_thread? message logic (#27151) A message is in a thread if: - it has a thread_id - it is in threading_enabled channel OR the associated thread is marked as `force` --- plugins/chat/app/models/chat/message.rb | 2 +- plugins/chat/spec/models/chat/message_spec.rb | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/plugins/chat/app/models/chat/message.rb b/plugins/chat/app/models/chat/message.rb index 32cf71b10928..1e92d77f9312 100644 --- a/plugins/chat/app/models/chat/message.rb +++ b/plugins/chat/app/models/chat/message.rb @@ -266,7 +266,7 @@ def upsert_mentions end def in_thread? - self.thread_id.present? && self.chat_channel.threading_enabled + self.thread_id.present? && (self.chat_channel.threading_enabled || self.thread&.force) end def thread_reply? diff --git a/plugins/chat/spec/models/chat/message_spec.rb b/plugins/chat/spec/models/chat/message_spec.rb index 264c3f8bfff5..924aa83ececa 100644 --- a/plugins/chat/spec/models/chat/message_spec.rb +++ b/plugins/chat/spec/models/chat/message_spec.rb @@ -11,6 +11,40 @@ it { is_expected.to validate_length_of(:cooked).is_at_most(20_000) } end + describe ".in_thread?" do + context "when in a thread enabled channel" do + fab!(:message) do + Fabricate( + :chat_message, + thread_id: 1, + chat_channel: Fabricate(:chat_channel, threading_enabled: true), + ) + end + + it "returns true for messages in a thread" do + expect(message.in_thread?).to eq(true) + end + + it "returns false for messages not in a thread" do + message.update!(thread_id: nil) + expect(message.in_thread?).to eq(false) + end + end + + context "when the thread is forced" do + fab!(:message) { Fabricate(:chat_message, thread: Fabricate(:chat_thread, force: true)) } + + it "returns true for messages in a thread" do + expect(message.in_thread?).to eq(true) + end + + it "returns false for messages not in a thread" do + message.update!(thread_id: nil) + expect(message.in_thread?).to eq(false) + end + end + end + describe ".cook" do it "does not support HTML tags" do cooked = described_class.cook("

test

")