Skip to content

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

FIX: Scope thread lookup by channel_id in chat thread messages service (#37978)

Alan Guo Xiang Tan029519ce1542
diff --git a/plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rb b/plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rb
index 8c1cd17b179..e67d37c46e3 100644
--- a/plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rb
+++ b/plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rb
@@ -22,6 +22,7 @@ class Chat::Api::ChannelThreadMessagesController < Chat::ApiController
on_failure { render(json: failed_json, status: :unprocessable_entity) }
on_failed_policy(:target_message_exists) { raise Discourse::NotFound }
on_failed_policy(:can_view_thread) { raise Discourse::InvalidAccess }
+ on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }
on_model_not_found(:thread) { raise Discourse::NotFound }
on_failed_contract do |contract|
render(json: failed_json.merge(errors: contract.errors.full_messages), status: :bad_request)
diff --git a/plugins/chat/app/services/chat/list_channel_thread_messages.rb b/plugins/chat/app/services/chat/list_channel_thread_messages.rb
index 3d19113bdcb..e4332ab05bd 100644
--- a/plugins/chat/app/services/chat/list_channel_thread_messages.rb
+++ b/plugins/chat/app/services/chat/list_channel_thread_messages.rb
@@ -20,6 +20,7 @@ module Chat
params do
attribute :thread_id, :integer
+ attribute :channel_id, :integer
# If this is not present, then we just fetch messages with page_size
# and direction.
attribute :target_message_id, :integer # (optional)
@@ -30,7 +31,7 @@ module Chat
attribute :fetch_from_first_message, :boolean # (optional)
attribute :target_date, :string # (optional)
- validates :thread_id, presence: true
+ validates :thread_id, :channel_id, presence: true
validates :page_size,
numericality: {
greater_than_or_equal_to: 1,
@@ -56,6 +57,7 @@ module Chat
model :thread
policy :can_view_thread
+ policy :threading_enabled_for_channel
model :membership, optional: true
model :target_message_id, optional: true
policy :target_message_exists, class_name: Chat::Thread::Policy::MessageExistence
@@ -65,13 +67,20 @@ module Chat
private
def fetch_thread(params:)
- ::Chat::Thread.strict_loading.includes(channel: :chatable).find_by(id: params.thread_id)
+ ::Chat::Thread
+ .strict_loading
+ .includes(channel: :chatable)
+ .find_by(id: params.thread_id, channel_id: params.channel_id)
end
def can_view_thread(guardian:, thread:)
guardian.user == Discourse.system_user || guardian.can_preview_chat_channel?(thread.channel)
end
+ def threading_enabled_for_channel(thread:)
+ thread.channel.threading_enabled || thread.force
+ end
+
def fetch_membership(thread:, guardian:)
thread.membership_for(guardian.user)
end
diff --git a/plugins/chat/lib/chat_sdk/thread.rb b/plugins/chat/lib/chat_sdk/thread.rb
index 324affad913..30ff2ec8c77 100644
--- a/plugins/chat/lib/chat_sdk/thread.rb
+++ b/plugins/chat/lib/chat_sdk/thread.rb
@@ -73,11 +73,13 @@ module ChatSDK
new.update(...)
end
- def messages(thread_id:, guardian:, direction: "future", **params)
+ def messages(thread_id:, guardian:, channel_id: nil, direction: "future", **params)
+ channel_id ||= ::Chat::Thread.where(id: thread_id).pick(:channel_id)
Chat::ListChannelThreadMessages.call(
guardian:,
params: {
thread_id:,
+ channel_id:,
direction:,
**params,
},
@@ -85,6 +87,9 @@ module ChatSDK
on_success { |messages:| messages }
on_failed_contract { |contract| raise contract.errors.full_messages.join(", ") }
on_failed_policy(:can_view_thread) { raise "Guardian can't view thread" }
+ on_failed_policy(:threading_enabled_for_channel) do
+ raise "Threading is not enabled for this channel"
+ end
on_failed_policy(:target_message_exists) { raise "Target message doesn't exist" }
on_failure { raise "Unexpected error" }
end
diff --git a/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb b/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb
index aecbc6a2928..7cfd7fd47b9 100644
--- a/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb
+++ b/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb
@@ -7,6 +7,7 @@ RSpec.describe Chat::ListChannelThreadMessages do
let(:options) { OpenStruct.new(max_page_size: Chat::MessagesQuery::MAX_PAGE_SIZE) }
it { is_expected.to validate_presence_of(:thread_id) }
+ it { is_expected.to validate_presence_of(:channel_id) }
it { is_expected.to allow_values(1, options.max_page_size, nil).for(:page_size) }
it { is_expected.not_to allow_values(0).for(:page_size) }
it do
@@ -90,7 +91,7 @@ RSpec.describe Chat::ListChannelThreadMessages do
let(:guardian) { user.guardian }
let(:thread_id) { thread.id }
let(:optional_params) { {} }
- let(:params) { { thread_id:, **optional_params } }
+ let(:params) { { thread_id:, channel_id: thread.channel_id, **optional_params } }
let(:dependencies) { { guardian: } }
before { thread.channel.add(user) }
@@ -107,6 +108,20 @@ RSpec.describe Chat::ListChannelThreadMessages do
it { is_expected.to fail_to_find_a_model(:thread) }
end
+ context "when thread exists but belongs to a different channel" do
+ fab!(:other_thread, :chat_thread)
+
+ let(:thread_id) { other_thread.id }
+
+ it { is_expected.to fail_to_find_a_model(:thread) }
+ end
+
+ context "when threading is not enabled for the channel" do
+ before { thread.channel.update!(threading_enabled: false) }
+
+ it { is_expected.to fail_a_policy(:threading_enabled_for_channel) }
+ end
+
context "when user cannot view the thread" do
fab!(:thread) { Fabricate(:chat_thread, channel: Fabricate(:private_category_channel)) }