Skip to content

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

FIX: handle thread participants limit on the frontend (#23839)

David Battersby79ececd976d6
diff --git a/plugins/chat/app/queries/chat/thread_participant_query.rb b/plugins/chat/app/queries/chat/thread_participant_query.rb
index b0e64e3533a..82381f9c284 100644
--- a/plugins/chat/app/queries/chat/thread_participant_query.rb
+++ b/plugins/chat/app/queries/chat/thread_participant_query.rb
@@ -17,10 +17,12 @@ module Chat
# and if there is a delay in updating them based on message
# count it is not a big deal.
class ThreadParticipantQuery
+ MAX_PARTICIPANTS = 10
+
# @param thread_ids [Array<Integer>] The IDs of the threads to query.
# @param preview [Boolean] Determines the number of participants to return.
# @return [Hash<Integer, Hash>] A hash of thread IDs to participant data.
- def self.call(thread_ids:, preview: true)
+ def self.call(thread_ids:)
return {} if thread_ids.blank?
# We only want enough data for BasicUserSerializer, since the participants
@@ -64,8 +66,6 @@ module Chat
hash
end
- total_participants = preview ? 2 : 9
-
thread_participants = {}
thread_participant_stats.each do |thread_participant_stat|
thread_id = thread_participant_stat.thread_id
@@ -75,7 +75,7 @@ module Chat
# If we want to return more of the top N users in the thread we
# can just increase the number here.
- if thread_participants[thread_id][:users].length < total_participants &&
+ if thread_participants[thread_id][:users].length < (MAX_PARTICIPANTS - 1) &&
thread_participant_stat.user_id != most_recent_participants[thread_id][:id]
thread_participants[thread_id][:users].push(
{
diff --git a/plugins/chat/app/services/chat/lookup_channel_threads.rb b/plugins/chat/app/services/chat/lookup_channel_threads.rb
index 8b4a120a501..83bb9b7b81e 100644
--- a/plugins/chat/app/services/chat/lookup_channel_threads.rb
+++ b/plugins/chat/app/services/chat/lookup_channel_threads.rb
@@ -135,8 +135,7 @@ module Chat
end
def fetch_participants(threads:, **)
- context.participants =
- ::Chat::ThreadParticipantQuery.call(thread_ids: threads.map(&:id), preview: false)
+ context.participants = ::Chat::ThreadParticipantQuery.call(thread_ids: threads.map(&:id))
end
def build_load_more_url(contract:, **)
diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/thread/participants.gjs b/plugins/chat/assets/javascripts/discourse/components/chat/thread/participants.gjs
index bac8b3152c6..441cba1ed42 100644
--- a/plugins/chat/assets/javascripts/discourse/components/chat/thread/participants.gjs
+++ b/plugins/chat/assets/javascripts/discourse/components/chat/thread/participants.gjs
@@ -37,11 +37,17 @@ export default class ChatThreadParticipants extends Component {
}
get participantsUsers() {
+ const users = this.args.thread.preview.participantUsers;
+
if (this.includeOriginalMessageUser) {
- return this.args.thread.preview.participantUsers;
+ if (users.length > 3) {
+ return users.slice(0, 2).concat(users[users.length - 1]);
+ } else {
+ return users;
+ }
}
- return this.args.thread.preview.participantUsers.filter((user) => {
+ return users.filter((user) => {
return user.id !== this.args.thread.originalMessage.user.id;
});
}
diff --git a/plugins/chat/spec/queries/chat/thread_participant_query_spec.rb b/plugins/chat/spec/queries/chat/thread_participant_query_spec.rb
index 52ae3127c79..1c40ac8528f 100644
--- a/plugins/chat/spec/queries/chat/thread_participant_query_spec.rb
+++ b/plugins/chat/spec/queries/chat/thread_participant_query_spec.rb
@@ -35,16 +35,15 @@ RSpec.describe Chat::ThreadParticipantQuery do
)
end
- it "does not return more than 3 thread participants by default" do
- thread_1.add(Fabricate(:user))
+ it "returns up to 10 thread participants" do
result = described_class.call(thread_ids: [thread_1.id])
- expect(result[thread_1.id][:users].length).to eq(3)
+ expect(result[thread_1.id][:users].length).to eq(4)
end
it "calculates the top messagers in a thread as well as the last messager" do
result = described_class.call(thread_ids: [thread_1.id, thread_2.id])
- expect(result[thread_1.id][:users].map { |u| u[:id] }).to eq(
- [user_1.id, user_2.id, user_3.id],
+ expect(result[thread_1.id][:users].map { |u| u[:id] }).to match_array(
+ [thread_1.original_message_user_id, user_1.id, user_2.id, user_3.id],
)
end
@@ -82,8 +81,8 @@ RSpec.describe Chat::ThreadParticipantQuery do
Fabricate(:chat_message, thread: thread_2, user: user_2)
Fabricate(:chat_message, thread: thread_2, user: user_2)
result = described_class.call(thread_ids: [thread_1.id, thread_2.id])
- expect(result[thread_1.id][:users].map { |u| u[:id] }).to eq(
- [user_1.id, user_2.id, user_3.id],
+ expect(result[thread_1.id][:users].map { |u| u[:id] }).to match_array(
+ [thread_1.original_message_user_id, user_1.id, user_2.id, user_3.id],
)
expect(result[thread_2.id][:users].map { |u| u[:id] }).to eq(
[thread_2.original_message_user_id, user_2.id],
@@ -119,11 +118,11 @@ RSpec.describe Chat::ThreadParticipantQuery do
end
end
- it "does not return more than 10 thread participants when preview is false" do
+ it "does not return more than 10 thread participants" do
other_user = Fabricate(:user)
thread_3.add(other_user)
Fabricate(:chat_message, thread: thread_3, user: other_user)
- result = described_class.call(thread_ids: [thread_3.id], preview: false)
+ result = described_class.call(thread_ids: [thread_3.id])
expect(result[thread_3.id][:users].length).to eq(10)
end
end