A platform for community discussion. Free, open, simple.
FIX: ensures we reuse existing thread if existing (#23618)
Joffrey JAFFEUXe3e94aec95c0
diff --git a/plugins/chat/app/services/chat/create_thread.rb b/plugins/chat/app/services/chat/create_thread.rbindex 3b83352ac37..9895d0dc85d 100644--- a/plugins/chat/app/services/chat/create_thread.rb+++ b/plugins/chat/app/services/chat/create_thread.rb@@ -22,7 +22,7 @@ module Chatpolicy :threading_enabled_for_channelmodel :original_messagetransaction do- step :create_thread+ step :find_or_create_threadstep :associate_thread_to_messagestep :fetch_membershipstep :publish_new_thread@@ -59,7 +59,11 @@ module Chatchannel.threading_enabledend- def create_thread(channel:, original_message:, contract:, **)+ def find_or_create_thread(channel:, original_message:, contract:, **)+ if original_message.thread_id.present?+ return context.thread = ::Chat::Thread.find_by(id: original_message.thread_id)+ end+context.thread =channel.threads.create(title: contract.title,diff --git a/plugins/chat/spec/services/chat/create_thread_spec.rb b/plugins/chat/spec/services/chat/create_thread_spec.rbindex f687c67e5b8..c73a202fa89 100644--- a/plugins/chat/spec/services/chat/create_thread_spec.rb+++ b/plugins/chat/spec/services/chat/create_thread_spec.rb@@ -97,5 +97,19 @@ RSpec.describe Chat::CreateThread doit { is_expected.to fail_a_policy(:threading_enabled_for_channel) }end++ context "when a thread is already present" do+ before do+ Chat::CreateThread.call(+ guardian: current_user.guardian,+ original_message_id: message_1.id,+ channel_id: channel_1.id,+ )+ end++ it "uses the existing thread" do+ expect { result }.not_to change { Chat::Thread.count }+ end+ endendend