Skip to content

Commit cd4b6b8

Browse files
authored
DEV: allows chat thread sdk to query more messages (#32914)
The changes are: - Chat Thread SDK can query up to 500 messages - If you are above this number, we don't raise an error anymore but we clamp it to 500 - The controller using this service is forced into the old limit of 50 to avoid abuses
1 parent fa18df1 commit cd4b6b8

File tree

8 files changed

+43
-33
lines changed

8 files changed

+43
-33
lines changed

plugins/chat/app/controllers/chat/api/channel_thread_messages_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# frozen_string_literal: true
22

33
class Chat::Api::ChannelThreadMessagesController < Chat::ApiController
4+
MAX_PAGE_SIZE = 50
5+
46
def index
5-
::Chat::ListChannelThreadMessages.call(service_params) do |result|
7+
::Chat::ListChannelThreadMessages.call(
8+
service_params.deep_merge(params: { page_size: MAX_PAGE_SIZE }),
9+
) do |result|
610
on_success do
711
render_serialized(
812
result,

plugins/chat/app/queries/chat/messages_query.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MessagesQuery
2121
PAST = "past"
2222
FUTURE = "future"
2323
VALID_DIRECTIONS = [PAST, FUTURE]
24-
MAX_PAGE_SIZE = 50
24+
MAX_PAGE_SIZE = 500
2525

2626
# @param channel [Chat::Channel] The channel to query messages within.
2727
# @param guardian [Guardian] The guardian to use for permission checks.

plugins/chat/app/services/chat/list_channel_thread_messages.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ class ListChannelThreadMessages
3434
in: Chat::MessagesQuery::VALID_DIRECTIONS,
3535
},
3636
allow_nil: true
37-
validates :page_size,
38-
numericality: {
39-
less_than_or_equal_to: Chat::MessagesQuery::MAX_PAGE_SIZE,
40-
only_integer: true,
41-
},
42-
allow_nil: true
37+
validates :page_size, numericality: { only_integer: true }, allow_nil: true
4338
end
4439

4540
model :thread
@@ -94,7 +89,7 @@ def fetch_messages(thread:, guardian:, params:)
9489
guardian: guardian,
9590
target_message_id: context.target_message_id,
9691
thread_id: thread.id,
97-
page_size: params.page_size || Chat::MessagesQuery::MAX_PAGE_SIZE,
92+
page_size: params.page_size,
9893
direction: params.direction,
9994
target_date: params.target_date,
10095
include_target_message_id:

plugins/chat/spec/lib/chat_sdk/thread_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,6 @@
126126
end
127127
end
128128

129-
context "when page_size is too large" do
130-
it "fails" do
131-
expect { described_class.messages(**params, page_size: 9999) }.to raise_error(
132-
"Page size must be less than or equal to 50",
133-
)
134-
end
135-
end
136-
137129
context "when target_message doesn’t exist" do
138130
it "fails" do
139131
expect { described_class.messages(**params, target_message_id: -999) }.to raise_error(

plugins/chat/spec/queries/chat/messages_query_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
stub_const(described_class, "MAX_PAGE_SIZE", 1) { expect(query[:messages].length).to eq(1) }
107107
end
108108

109+
it "limits results to MAX_PAGE_SIZE" do
110+
options[:page_size] = 2
111+
options[:target_message_id] = nil
112+
stub_const(described_class, "MAX_PAGE_SIZE", 1) { expect(query[:messages].length).to eq(1) }
113+
end
114+
109115
describe "when some messages are in threads" do
110116
fab!(:thread) { Fabricate(:chat_thread, channel: channel) }
111117

plugins/chat/spec/requests/chat/api/channel_thread_messages_controller_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,22 @@
6060
end
6161
end
6262

63+
context "when page_size is a above limit" do
64+
fab!(:message_3) { Fabricate(:chat_message, thread: thread, chat_channel: thread.channel) }
65+
66+
it "clamps it to the max" do
67+
stub_const(Chat::Api::ChannelThreadMessagesController, "MAX_PAGE_SIZE", 1) do
68+
get "/chat/api/channels/#{thread.channel.id}/threads/#{thread.id}/messages?page_size=9999"
69+
70+
expect(response.status).to eq(200)
71+
expect(response.parsed_body["messages"].length).to eq(1)
72+
end
73+
end
74+
end
75+
6376
context "when params are invalid" do
6477
it "returns a 400" do
65-
get "/chat/api/channels/#{thread.channel.id}/threads/#{thread.id}/messages?page_size=9999"
78+
get "/chat/api/channels/#{thread.channel.id}/threads/#{thread.id}/messages?direction=yolo"
6679

6780
expect(response.status).to eq(400)
6881
end

plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
it do
1212
is_expected.to allow_values(Chat::MessagesQuery::MAX_PAGE_SIZE, 1, "1", nil).for(:page_size)
1313
end
14-
it { is_expected.not_to allow_values(Chat::MessagesQuery::MAX_PAGE_SIZE + 1).for(:page_size) }
1514
end
1615

1716
describe ".call" do

plugins/chat/spec/system/bookmark_message_spec.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,26 @@
2828

2929
expect(channel_page).to have_bookmarked_message(message_1)
3030
end
31+
context "when in a long thread" do
32+
it "supports linking to a bookmark in a long thread" do
33+
category_channel_1.update!(threading_enabled: true)
34+
category_channel_1.add(current_user)
3135

32-
it "supports linking to a bookmark in a long thread" do
33-
category_channel_1.update!(threading_enabled: true)
34-
category_channel_1.add(current_user)
36+
thread =
37+
chat_thread_chain_bootstrap(
38+
channel: category_channel_1,
39+
users: [current_user, Fabricate(:user)],
40+
messages_count: 51,
41+
)
3542

36-
thread =
37-
chat_thread_chain_bootstrap(
38-
channel: category_channel_1,
39-
users: [current_user, Fabricate(:user)],
40-
messages_count: Chat::MessagesQuery::MAX_PAGE_SIZE + 1,
41-
)
42-
43-
first_message = thread.replies.first
43+
first_message = thread.replies.first
4444

45-
bookmark = Bookmark.create!(bookmarkable: first_message, user: current_user)
45+
bookmark = Bookmark.create!(bookmarkable: first_message, user: current_user)
4646

47-
visit bookmark.bookmarkable.url
47+
visit bookmark.bookmarkable.url
4848

49-
expect(thread_page).to have_bookmarked_message(first_message)
49+
expect(thread_page).to have_bookmarked_message(first_message)
50+
end
5051
end
5152

5253
context "in drawer mode" do

0 commit comments

Comments
 (0)