FIX: Improve chat membership update on deleted message (#21716)

Followup to c908eeacc96e947025176c1744ee4071ee252b91

Instead of using the latest message ID in the channel, which
could cause issues if you have an earlier last read message ID
that matches the deleted one, instead we use the first non-deleted
message that comes before the deleted message by ID.
This commit is contained in:
Martin Brennan
2023-05-24 10:31:15 +02:00
committed by GitHub
parent b2e3084205
commit 177d8dfcd1
5 changed files with 65 additions and 44 deletions

View File

@ -127,11 +127,12 @@ module Chat
SQL
end
def latest_not_deleted_message_id
DB.query_single(<<~SQL, channel_id: self.id).first
def latest_not_deleted_message_id(anchor_message_id: nil)
DB.query_single(<<~SQL, channel_id: self.id, anchor_message_id: anchor_message_id).first
SELECT id FROM chat_messages
WHERE chat_channel_id = :channel_id
AND deleted_at IS NULL
#{anchor_message_id ? "AND id < :anchor_message_id" : ""}
ORDER BY created_at DESC, id DESC
LIMIT 1
SQL

View File

@ -52,15 +52,21 @@ module Chat
original_message.rich_excerpt(max_length: EXCERPT_LENGTH)
end
def latest_not_deleted_message_id
DB.query_single(<<~SQL, channel_id: self.channel_id, thread_id: self.id).first
def latest_not_deleted_message_id(anchor_message_id: nil)
DB.query_single(
<<~SQL,
SELECT id FROM chat_messages
WHERE chat_channel_id = :channel_id
AND thread_id = :thread_id
AND deleted_at IS NULL
#{anchor_message_id ? "AND id < :anchor_message_id" : ""}
ORDER BY created_at DESC, id DESC
LIMIT 1
SQL
channel_id: self.channel_id,
thread_id: self.id,
anchor_message_id: anchor_message_id,
).first
end
def self.grouped_messages(thread_ids: nil, message_ids: nil, include_original_message: true)

View File

@ -140,9 +140,9 @@ module Chat
latest_not_deleted_message_id =
if chat_message.thread_reply? && chat_channel.threading_enabled &&
SiteSetting.enable_experimental_chat_threaded_discussions
chat_message.thread.latest_not_deleted_message_id
chat_message.thread.latest_not_deleted_message_id(anchor_message_id: chat_message.id)
else
chat_channel.latest_not_deleted_message_id
chat_channel.latest_not_deleted_message_id(anchor_message_id: chat_message.id)
end
publish_to_targets!(
message_bus_targets,