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.rbindex 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::ApiControlleron_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.rbindex 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 Chatparams doattribute :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 Chatattribute :fetch_from_first_message, :boolean # (optional)attribute :target_date, :string # (optional)- validates :thread_id, presence: true+ validates :thread_id, :channel_id, presence: truevalidates :page_size,numericality: {greater_than_or_equal_to: 1,@@ -56,6 +57,7 @@ module Chatmodel :threadpolicy :can_view_thread+ policy :threading_enabled_for_channelmodel :membership, optional: truemodel :target_message_id, optional: truepolicy :target_message_exists, class_name: Chat::Thread::Policy::MessageExistence@@ -65,13 +67,20 @@ module Chatprivatedef 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)enddef 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)enddiff --git a/plugins/chat/lib/chat_sdk/thread.rb b/plugins/chat/lib/chat_sdk/thread.rbindex 324affad913..30ff2ec8c77 100644--- a/plugins/chat/lib/chat_sdk/thread.rb+++ b/plugins/chat/lib/chat_sdk/thread.rb@@ -73,11 +73,13 @@ module ChatSDKnew.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 ChatSDKon_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"+ endon_failed_policy(:target_message_exists) { raise "Target message doesn't exist" }on_failure { raise "Unexpected error" }enddiff --git a/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb b/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rbindex 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 dolet(: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 dolet(: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 doit { 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" dofab!(:thread) { Fabricate(:chat_thread, channel: Fabricate(:private_category_channel)) }