FIX: ensures we reuse existing thread if existing (#23618)

This is a special case to ensure that if two users start a thread at the same time, we won't attempt to create a second thread.
This commit is contained in:
Joffrey JAFFEUX 2023-09-26 10:15:16 +02:00 committed by GitHub
parent fa243484ca
commit e3e94aec95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -22,7 +22,7 @@ module Chat
policy :threading_enabled_for_channel
model :original_message
transaction do
step :create_thread
step :find_or_create_thread
step :associate_thread_to_message
step :fetch_membership
step :publish_new_thread
@ -59,7 +59,11 @@ module Chat
channel.threading_enabled
end
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,

View File

@ -97,5 +97,19 @@ RSpec.describe Chat::CreateThread do
it { 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
end
end
end