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

Workaround for an issue we are experiencing on thread index frontend where thread loads participants correctly (up to 10), then refreshes the threads and then limits to 3 participants.

There is an issue with storing threads for the main channel view and the thread list in the same store so handling the max participants on the frontend is a workaround until channel.threadsManager is updated.

I've adjusted the tests to handle the additional data being returned from ThreadParticipantQuery.
This commit is contained in:
David Battersby 2023-10-09 14:04:59 +08:00 committed by GitHub
parent c468110929
commit 79ececd976
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 17 deletions

View File

@ -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(
{

View File

@ -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:, **)

View File

@ -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;
});
}

View File

@ -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