DEV: When deleting a chat message, do not delete mention records (#21486)

A chat message may be restored later, so we shouldn't be deleting `chat_mentions` records for it.

But we still have to remove notifications (see 082cd139).
This commit is contained in:
Andrei Prigorshnev 2023-05-11 20:05:59 +04:00 committed by GitHub
parent 92bb845db2
commit f4fde4e49b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions

View File

@ -22,7 +22,7 @@ module Chat
policy :invalid_access
transaction do
step :trash_message
step :destroy_mentions
step :destroy_notifications
step :update_tracking_state
step :update_thread_reply_cache
end
@ -53,8 +53,10 @@ module Chat
message.trash!
end
def destroy_mentions(message:, **)
Chat::Mention.where(chat_message: message).destroy_all
def destroy_notifications(message:, **)
ids = Chat::Mention.where(chat_message: message).pluck(:notification_id)
Notification.where(id: ids).destroy_all
Chat::Mention.where(chat_message: message).update_all(notification_id: nil)
end
def update_tracking_state(message:, **)

View File

@ -43,10 +43,15 @@ RSpec.describe Chat::TrashMessage do
expect(Chat::Message.find_by(id: message.id)).to be_nil
end
it "destroys associated mentions" do
mention = Fabricate(:chat_mention, chat_message: message)
it "destroys notifications for mentions" do
notification = Fabricate(:notification)
mention = Fabricate(:chat_mention, chat_message: message, notification: notification)
result
expect(Chat::Mention.find_by(id: mention.id)).to be_nil
mention = Chat::Mention.find_by(id: mention.id)
expect(mention).to be_present
expect(mention.notification_id).to be_nil
end
it "publishes associated Discourse and MessageBus events" do