Skip to content

A platform for community discussion. Free, open, simple.

FIX: CreateThread allows thread creation in closed/read-only/archived channels (#37661)

Joffrey JAFFEUX0c8f52f420ab
diff --git a/plugins/chat/app/services/chat/create_thread.rb b/plugins/chat/app/services/chat/create_thread.rb
index 889a9a71482..ca39ff8cbca 100644
--- a/plugins/chat/app/services/chat/create_thread.rb
+++ b/plugins/chat/app/services/chat/create_thread.rb
@@ -28,6 +28,7 @@ module Chat
model :channel
policy :can_view_channel
+ policy :can_create_thread_in_channel
policy :threading_enabled_for_channel
model :original_message
@@ -49,6 +50,10 @@ module Chat
guardian.can_preview_chat_channel?(channel)
end
+ def can_create_thread_in_channel(guardian:, channel:)
+ guardian.can_create_channel_message?(channel)
+ end
+
def threading_enabled_for_channel(channel:)
channel.threading_enabled?
end
diff --git a/plugins/chat/spec/services/chat/create_thread_spec.rb b/plugins/chat/spec/services/chat/create_thread_spec.rb
index dd7d1e58876..345006265fa 100644
--- a/plugins/chat/spec/services/chat/create_thread_spec.rb
+++ b/plugins/chat/spec/services/chat/create_thread_spec.rb
@@ -106,6 +106,32 @@ RSpec.describe Chat::CreateThread do
it { is_expected.to fail_a_policy(:can_view_channel) }
end
+ context "when channel is not open" do
+ context "when channel is read_only" do
+ before { channel_1.update!(status: :read_only) }
+
+ it { is_expected.to fail_a_policy(:can_create_thread_in_channel) }
+ end
+
+ context "when channel is closed" do
+ before { channel_1.update!(status: :closed) }
+
+ it { is_expected.to fail_a_policy(:can_create_thread_in_channel) }
+
+ context "when user is staff" do
+ let(:guardian) { Guardian.new(Fabricate(:admin)) }
+
+ it { is_expected.to run_successfully }
+ end
+ end
+
+ context "when channel is archived" do
+ before { channel_1.update!(status: :archived) }
+
+ it { is_expected.to fail_a_policy(:can_create_thread_in_channel) }
+ end
+ end
+
context "when threading is not enabled for the channel" do
before { channel_1.update!(threading_enabled: false) }