FEATURE: Hook up chat bulk delete for threads (#21109)

Followup to bd5c5c4b5f7b33a64cc12e2ba13e81767ac00edc,
this commit hooks up the bulk delete events for chat
messages inside the thread panel, by fanning out the
deleted message IDs based on whether they belong to
a thread or not.

Also adds a system spec to cover this case, as previously
the bulk delete event would have been broken with an incorrect
`typ` rather than `type` hash key.
This commit is contained in:
Martin Brennan
2023-04-18 08:28:20 +10:00
committed by GitHub
parent b869d35f94
commit 1e85de36e2
10 changed files with 222 additions and 24 deletions

View File

@ -34,6 +34,21 @@ module Chat
original_message.excerpt(max_length: EXCERPT_LENGTH)
end
def self.grouped_messages(thread_ids: nil, message_ids: nil, include_original_message: true)
DB.query(<<~SQL, message_ids: message_ids, thread_ids: thread_ids)
SELECT thread_id,
array_agg(chat_messages.id ORDER BY chat_messages.created_at, chat_messages.id) AS thread_message_ids,
chat_threads.original_message_id
FROM chat_messages
INNER JOIN chat_threads ON chat_threads.id = chat_messages.thread_id
WHERE thread_id IS NOT NULL
#{thread_ids ? "AND thread_id IN (:thread_ids)" : ""}
#{message_ids ? "AND chat_messages.id IN (:message_ids)" : ""}
#{include_original_message ? "" : "AND chat_messages.id != chat_threads.original_message_id"}
GROUP BY thread_id, chat_threads.original_message_id;
SQL
end
def self.ensure_consistency!
update_counts
end

View File

@ -167,11 +167,31 @@ module Chat
end
def self.publish_bulk_delete!(chat_channel, deleted_message_ids)
# TODO (martin) Handle sending this through for all the threads that
# may contain the deleted messages as well.
Chat::Thread
.grouped_messages(message_ids: deleted_message_ids)
.each do |group|
MessageBus.publish(
thread_message_bus_channel(chat_channel.id, group.thread_id),
{
type: "bulk_delete",
deleted_ids: group.thread_message_ids,
deleted_at: Time.zone.now,
},
permissions(chat_channel),
)
# Don't need to publish to the main channel if the messages deleted
# were a part of the thread (except the original message ID, since
# that shows in the main channel).
deleted_message_ids =
deleted_message_ids - (group.thread_message_ids - [group.original_message_id])
end
return if deleted_message_ids.empty?
MessageBus.publish(
root_message_bus_channel(chat_channel.id),
{ typ: "bulk_delete", deleted_ids: deleted_message_ids, deleted_at: Time.zone.now },
{ type: "bulk_delete", deleted_ids: deleted_message_ids, deleted_at: Time.zone.now },
permissions(chat_channel),
)
end