DEV: Refactor the Chat::CreateThread service a bit

Follow best practices from our docs.
This commit is contained in:
Loïc Guitaut
2024-10-31 18:00:34 +01:00
committed by Loïc Guitaut
parent 45ecb34aec
commit 6158a1ae29
4 changed files with 50 additions and 57 deletions

View File

@ -34,12 +34,7 @@ RSpec.describe Chat::CreateThread do
expect {
result
message_1.reload
}.to change { message_1.thread_id }.from(nil).to(result.thread.id)
end
it "fetches the membership" do
result
expect(result.membership).to eq(result.thread.membership_for(current_user))
}.to change { message_1.thread }.from(nil).to(result.thread)
end
it "publishes a `thread_created` MessageBus event for public channels" do
@ -67,6 +62,22 @@ RSpec.describe Chat::CreateThread do
result
expect(result.thread.title).to eq(params[:title])
end
context "when a thread is already present" do
before do
Chat::CreateThread.call(
guardian: current_user.guardian,
params: {
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
context "when params are not valid" do
@ -75,18 +86,10 @@ RSpec.describe Chat::CreateThread do
it { is_expected.to fail_a_contract }
end
context "when original message is not found" do
fab!(:channel_2) { Fabricate(:chat_channel, threading_enabled: true) }
context "when channel does not exist" do
before { params[:channel_id] = 0 }
before { params[:channel_id] = channel_2.id }
it { is_expected.to fail_to_find_a_model(:original_message) }
end
context "when original message is not found" do
before { message_1.destroy! }
it { is_expected.to fail_to_find_a_model(:original_message) }
it { is_expected.to fail_to_find_a_model(:channel) }
end
context "when user cannot see channel" do
@ -103,20 +106,18 @@ 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,
params: {
original_message_id: message_1.id,
channel_id: channel_1.id,
},
)
end
context "when original message is not found" do
fab!(:channel_2) { Fabricate(:chat_channel, threading_enabled: true) }
it "uses the existing thread" do
expect { result }.not_to change { Chat::Thread.count }
end
before { params[:channel_id] = channel_2.id }
it { is_expected.to fail_to_find_a_model(:original_message) }
end
context "when original message is not found" do
before { message_1.destroy! }
it { is_expected.to fail_to_find_a_model(:original_message) }
end
end
end