discourse/plugins/chat/app/jobs/regular/chat_channel_delete.rb
Martin Brennan 0924f874bd
DEV: Use UploadReference instead of ChatUpload in chat (#19947)
We've had the UploadReference table for some time now in core,
but it was added after ChatUpload was and chat was just never
moved over to this new system.

This commit changes all chat code dealing with uploads to create/
update/delete/query UploadReference records instead of ChatUpload
records for consistency. At a later date we will drop the ChatUpload
table, but for now keeping it for data backup.

The migration + post migration are the same, we need both in case
any chat uploads are added/removed during deploy.
2023-01-24 13:28:21 +10:00

59 lines
2.3 KiB
Ruby

# frozen_string_literal: true
module Jobs
class ChatChannelDelete < ::Jobs::Base
def execute(args = {})
chat_channel = ::ChatChannel.with_deleted.find_by(id: args[:chat_channel_id])
# this should not really happen, but better to do this than throw an error
if chat_channel.blank?
Rails.logger.warn(
"Chat channel #{args[:chat_channel_id]} could not be found, aborting delete job.",
)
return
end
DistributedMutex.synchronize("delete_chat_channel_#{chat_channel.id}") do
Rails.logger.debug("Deleting webhooks and events for channel #{chat_channel.id}")
ChatMessage.transaction do
webhooks = IncomingChatWebhook.where(chat_channel: chat_channel)
ChatWebhookEvent.where(incoming_chat_webhook_id: webhooks.select(:id)).delete_all
webhooks.delete_all
end
Rails.logger.debug("Deleting drafts and memberships for channel #{chat_channel.id}")
ChatDraft.where(chat_channel: chat_channel).delete_all
UserChatChannelMembership.where(chat_channel: chat_channel).delete_all
Rails.logger.debug(
"Deleting chat messages, mentions, revisions, and uploads for channel #{chat_channel.id}",
)
chat_messages = ChatMessage.where(chat_channel: chat_channel)
delete_messages_and_related_records(chat_channel, chat_messages) if chat_messages.any?
end
end
def delete_messages_and_related_records(chat_channel, chat_messages)
message_ids = chat_messages.pluck(:id)
ChatMessage.transaction do
ChatMention.where(chat_message_id: message_ids).delete_all
ChatMessageRevision.where(chat_message_id: message_ids).delete_all
ChatMessageReaction.where(chat_message_id: message_ids).delete_all
# if the uploads are not used anywhere else they will be deleted
# by the CleanUpUploads job in core
DB.exec("DELETE FROM chat_uploads WHERE chat_message_id IN (#{message_ids.join(",")})")
UploadReference.where(target_id: message_ids, target_type: "ChatMessage").delete_all
# only the messages and the channel are Trashable, everything else gets
# permanently destroyed
chat_messages.update_all(
deleted_by_id: chat_channel.deleted_by_id,
deleted_at: Time.zone.now,
)
end
end
end
end