A platform for community discussion. Free, open, simple.
DEV: When deleting a chat message, do not delete mention records (#21486)
Andrei Prigorshnevf4fde4e49b03
diff --git a/plugins/chat/app/services/chat/trash_message.rb b/plugins/chat/app/services/chat/trash_message.rbindex 80722b2519b..23ba5f0b212 100644--- a/plugins/chat/app/services/chat/trash_message.rb+++ b/plugins/chat/app/services/chat/trash_message.rb@@ -22,7 +22,7 @@ module Chatpolicy :invalid_accesstransaction dostep :trash_message- step :destroy_mentions+ step :destroy_notificationsstep :update_tracking_statestep :update_thread_reply_cacheend@@ -53,8 +53,10 @@ module Chatmessage.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)enddef update_tracking_state(message:, **)diff --git a/plugins/chat/spec/services/chat/trash_message_spec.rb b/plugins/chat/spec/services/chat/trash_message_spec.rbindex f102ee62224..30eedb5760d 100644--- a/plugins/chat/spec/services/chat/trash_message_spec.rb+++ b/plugins/chat/spec/services/chat/trash_message_spec.rb@@ -43,10 +43,15 @@ RSpec.describe Chat::TrashMessage doexpect(Chat::Message.find_by(id: message.id)).to be_nilend- 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_nilendit "publishes associated Discourse and MessageBus events" do