FEATURE: Deleting a user with their posts also deletes chat messages. (#19194)

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.
This commit is contained in:
Roman Rizzi
2022-11-28 13:32:57 -03:00
committed by GitHub
parent 7dfe85fcc7
commit 07a9163ea8
14 changed files with 219 additions and 80 deletions

View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
module Jobs
class DeleteUserMessages < ::Jobs::Base
def execute(args)
return if args[:user_id].nil?
ChatMessageDestroyer.new
.destroy_in_batches(ChatMessage.with_deleted.where(user_id: args[:user_id]))
end
end
end

View File

@@ -9,47 +9,32 @@ module Jobs
delete_dm_channel_messages
end
private
def delete_public_channel_messages
return unless valid_day_value?(:chat_channel_retention_days)
ChatMessage
.in_public_channel
.with_deleted
.created_before(SiteSetting.chat_channel_retention_days.days.ago)
.in_batches(of: 200)
.each do |relation|
destroyed_ids = relation.destroy_all.pluck(:id)
reset_last_read_message_id(destroyed_ids)
delete_flags(destroyed_ids)
end
ChatMessageDestroyer.new.destroy_in_batches(
ChatMessage
.in_public_channel
.with_deleted
.created_before(SiteSetting.chat_channel_retention_days.days.ago)
)
end
def delete_dm_channel_messages
return unless valid_day_value?(:chat_dm_retention_days)
ChatMessage
.in_dm_channel
.with_deleted
.created_before(SiteSetting.chat_dm_retention_days.days.ago)
.in_batches(of: 200)
.each do |relation|
destroyed_ids = relation.destroy_all.pluck(:id)
reset_last_read_message_id(destroyed_ids)
end
ChatMessageDestroyer.new.destroy_in_batches(
ChatMessage
.in_dm_channel
.with_deleted
.created_before(SiteSetting.chat_dm_retention_days.days.ago)
)
end
def valid_day_value?(setting_name)
(SiteSetting.public_send(setting_name) || 0).positive?
end
def reset_last_read_message_id(ids)
UserChatChannelMembership.where(last_read_message_id: ids).update_all(
last_read_message_id: nil,
)
end
def delete_flags(message_ids)
ReviewableChatMessage.where(target_id: message_ids).destroy_all
end
end
end