Skip to content

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

DEV: Unify `ListChannelMessages`/`ListChannelThreadMessages` behaviors

Loïc Guitautca81e8a5b2e2
diff --git a/plugins/chat/app/controllers/chat/api/channel_messages_controller.rb b/plugins/chat/app/controllers/chat/api/channel_messages_controller.rb
index c754100058c..da3b450c834 100644
--- a/plugins/chat/app/controllers/chat/api/channel_messages_controller.rb
+++ b/plugins/chat/app/controllers/chat/api/channel_messages_controller.rb
@@ -1,8 +1,15 @@
# frozen_string_literal: true
class Chat::Api::ChannelMessagesController < Chat::ApiController
+ MAX_PAGE_SIZE = 50 # Previous limit to avoid abuses
+
def index
- ::Chat::ListChannelMessages.call(service_params) do |result|
+ ::Chat::ListChannelMessages.call(
+ options: {
+ max_page_size: MAX_PAGE_SIZE,
+ },
+ **service_params,
+ ) do |result|
on_success { render_serialized(result, ::Chat::MessagesSerializer, root: false) }
on_failure { render(json: failed_json, status: 422) }
on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }
diff --git a/plugins/chat/app/services/chat/list_channel_messages.rb b/plugins/chat/app/services/chat/list_channel_messages.rb
index f43fb684454..6a0df00aef9 100644
--- a/plugins/chat/app/services/chat/list_channel_messages.rb
+++ b/plugins/chat/app/services/chat/list_channel_messages.rb
@@ -16,6 +16,8 @@ module Chat
# @option params [Integer] :channel_id
# @return [Service::Base::Context]
+ options { attribute :max_page_size, :integer, default: Chat::MessagesQuery::MAX_PAGE_SIZE }
+
params do
attribute :channel_id, :integer
attribute :page_size, :integer
@@ -30,7 +32,6 @@ module Chat
validates :channel_id, presence: true
validates :page_size,
numericality: {
- less_than_or_equal_to: Chat::MessagesQuery::MAX_PAGE_SIZE,
greater_than_or_equal_to: 1,
only_integer: true,
only_numeric: true,
@@ -42,7 +43,10 @@ module Chat
},
allow_nil: true
- after_validation { self.page_size ||= Chat::MessagesQuery::MAX_PAGE_SIZE }
+ after_validation do
+ self.page_size ||= options.max_page_size
+ self.page_size = options.max_page_size if page_size > options.max_page_size
+ end
end
model :channel
diff --git a/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb
index e16e9c9d728..be004e79163 100644
--- a/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb
+++ b/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb
@@ -28,7 +28,7 @@ RSpec.describe Chat::Api::ChannelMessagesController do
context "when params are invalid" do
it "returns a 400" do
- get "/chat/api/channels/#{channel.id}/messages?page_size=9999"
+ get "/chat/api/channels/#{channel.id}/messages?page_size=0"
expect(response.status).to eq(400)
end
@@ -72,6 +72,19 @@ RSpec.describe Chat::Api::ChannelMessagesController do
expect(response.status).to eq(403)
end
end
+
+ context "when page_size is above limit" do
+ fab!(:messages) { Fabricate.times(5, :chat_message, chat_channel: channel) }
+
+ it "clamps it to the max" do
+ stub_const(Chat::Api::ChannelMessagesController, "MAX_PAGE_SIZE", 1) do
+ get "/chat/api/channels/#{channel.id}/messages?page_size=9999"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body["messages"].size).to eq(1)
+ end
+ end
+ end
end
describe "#create" do
diff --git a/plugins/chat/spec/services/chat/list_channel_messages_spec.rb b/plugins/chat/spec/services/chat/list_channel_messages_spec.rb
index 3be003a17fe..61951ad5915 100644
--- a/plugins/chat/spec/services/chat/list_channel_messages_spec.rb
+++ b/plugins/chat/spec/services/chat/list_channel_messages_spec.rb
@@ -2,11 +2,13 @@
RSpec.describe Chat::ListChannelMessages do
describe described_class::Contract, type: :model do
+ subject(:contract) { described_class.new(options:) }
+
+ let(:options) { OpenStruct.new(max_page_size: Chat::MessagesQuery::MAX_PAGE_SIZE) }
+
it { is_expected.to validate_presence_of(:channel_id) }
- it { is_expected.to allow_values(1, Chat::MessagesQuery::MAX_PAGE_SIZE, nil).for(:page_size) }
- it do
- is_expected.not_to allow_values(0, Chat::MessagesQuery::MAX_PAGE_SIZE + 1).for(:page_size)
- end
+ 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
is_expected.to validate_inclusion_of(:direction).in_array(
Chat::MessagesQuery::VALID_DIRECTIONS,
@@ -14,12 +16,10 @@ RSpec.describe Chat::ListChannelMessages do
end
describe "#page_size" do
- let(:contract) { described_class.new }
-
context "when page_size is not set" do
- it "defaults to MAX_PAGE_SIZE" do
+ it "defaults to options.max_page_size" do
contract.validate
- expect(contract.page_size).to eq(Chat::MessagesQuery::MAX_PAGE_SIZE)
+ expect(contract.page_size).to eq(options.max_page_size)
end
end
@@ -28,16 +28,27 @@ RSpec.describe Chat::ListChannelMessages do
it "defaults to MAX_PAGE_SIZE" do
contract.validate
- expect(contract.page_size).to eq(Chat::MessagesQuery::MAX_PAGE_SIZE)
+ expect(contract.page_size).to eq(options.max_page_size)
end
end
context "when page_size is set" do
- before { contract.page_size = 5 }
+ context "when page_size is greater than max_page_size" do
+ before { contract.page_size = options.max_page_size + 1 }
- it "does not change the value" do
- contract.validate
- expect(contract.page_size).to eq(5)
+ it "sets page_size to options.max_page_size" do
+ contract.validate
+ expect(contract.page_size).to eq(options.max_page_size)
+ end
+ end
+
+ context "when page_size is lesser than max_page_size" do
+ before { contract.page_size = 5 }
+
+ it "does not change the value" do
+ contract.validate
+ expect(contract.page_size).to eq(5)
+ end
end
end
end