mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 14:19:49 +08:00
07a9163ea8
This commit introduce a new API for registering callbacks, which we'll execute when a user gets destroyed, and the `delete_posts` opt is true. The chat plugin registers one callback and queues a job to destroy every message from that user in batches.
24 lines
616 B
Ruby
24 lines
616 B
Ruby
# frozen_string_literal: true
|
|
|
|
class ChatMessageDestroyer
|
|
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)
|
|
reset_last_read(destroyed_ids)
|
|
delete_flags(destroyed_ids)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def reset_last_read(message_ids)
|
|
UserChatChannelMembership.where(last_read_message_id: message_ids).update_all(
|
|
last_read_message_id: nil,
|
|
)
|
|
end
|
|
|
|
def delete_flags(message_ids)
|
|
ReviewableChatMessage.where(target_id: message_ids).destroy_all
|
|
end
|
|
end
|