discourse/plugins/chat/app/services/chat/message_destroyer.rb
Martin Brennan b1978e7ad8
DEV: Add last_message_id to channel and thread (#22488)
Initial migration and changes to models as well as
changing the following services to update last_message_id:

* Chat::MessageCreator
* Chat::RestoreMessage
* Chat::TrashMessage

The data migration will set the `last_message_id` for all existing
threads and channels in the database.

When we query the thread list as well as the channel,
we look at the last message ID for the following:

* Channel - Sorting DM channels, and channel metadata for the list of channels
* Thread - Last reply details for thread indicators and thread list
2023-07-13 10:28:11 +10:00

43 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module Chat
class MessageDestroyer
def destroy_in_batches(chat_messages_query, batch_size: 200)
chat_messages_query
.in_batches(of: batch_size)
.each do |relation|
destroyed_ids = relation.destroy_all.pluck(:id, :chat_channel_id)
destroyed_message_ids = destroyed_ids.map(&:first).uniq
destroyed_message_channel_ids = destroyed_ids.map(&:second).uniq
# This needs to be done before reset_last_read so we can lean on the last_message_id
# there.
reset_last_message_ids(destroyed_message_ids, destroyed_message_channel_ids)
reset_last_read(destroyed_message_ids, destroyed_message_channel_ids)
delete_flags(destroyed_message_ids)
end
end
private
def reset_last_message_ids(destroyed_message_ids, destroyed_message_channel_ids)
::Chat::Action::ResetChannelsLastMessageIds.call(
destroyed_message_ids,
destroyed_message_channel_ids,
)
end
def reset_last_read(destroyed_message_ids, destroyed_message_channel_ids)
::Chat::Action::ResetUserLastReadChannelMessage.call(
destroyed_message_ids,
destroyed_message_channel_ids,
)
end
def delete_flags(message_ids)
Chat::ReviewableMessage.where(target_id: message_ids).destroy_all
end
end
end