FIX: Page size edge case for null last_read_message_id (#21811)

Followup 55ef2d0698627348e4f340f079504419c0939677.

In the cases where the user has no last_read_message_id for
a channel, we want to make sure that a page_size is set for
the ChannelViewBuilder + MessagesQuery, otherwise we end up
loading way more messages than needed (the additional message
loading was fixed in the last commit).
This commit is contained in:
Martin Brennan
2023-05-29 20:12:01 +02:00
committed by GitHub
parent 4ce8ea7496
commit c57ae992b3
2 changed files with 28 additions and 0 deletions

View File

@ -84,6 +84,15 @@ module Chat
def determine_target_message_id(contract:, channel:, guardian:, **)
if contract.fetch_from_last_read
contract.target_message_id = channel.membership_for(guardian.user)&.last_read_message_id
# We need to force a page size here because we don't want to
# load all messages in the channel (since starting from 0
# makes them all unread). When the target_message_id is provided
# page size is not required since we load N messages either side of
# the target.
if contract.target_message_id.blank?
contract.page_size = contract.page_size || Chat::MessagesQuery::MAX_PAGE_SIZE
end
end
end