FIX: Delete associated notifications when trashing chat messages. (#20144)

Deleting a message with a mention doesn't clear the associated notification, confusing the mentioned user.

There are different chat notification types, but we only care about `chat_mentioned` since `chat_quoted` is associated with a post, and `chat_message` is only for push notifications.

Unfortunately, this change doesn't fix the chat bubble getting out of sync when a message gets deleted since we track unread/mentions count with an integer, making it a bit hard to manipulate. We can follow up later if we consider it necessary.
This commit is contained in:
Roman Rizzi
2023-02-03 12:52:13 -03:00
committed by GitHub
parent 44df5ee7c8
commit 082cd13909
5 changed files with 61 additions and 9 deletions

View File

@ -260,13 +260,9 @@ class Chat::ChatController < Chat::ChatBaseController
def delete
guardian.ensure_can_delete_chat!(@message, @chatable)
updated = @message.trash!(current_user)
if updated
ChatPublisher.publish_delete!(@chat_channel, @message)
render json: success_json
else
render_json_error(@message)
end
ChatMessageDestroyer.new.trash_message(@message, current_user)
head :ok
end
def restore

View File

@ -3,7 +3,7 @@
class ChatMention < ActiveRecord::Base
belongs_to :user
belongs_to :chat_message
belongs_to :notification
belongs_to :notification, dependent: :destroy
end
# == Schema Information

View File

@ -11,6 +11,18 @@ class ChatMessageDestroyer
end
end
def trash_message(message, actor)
ChatMessage.transaction do
message.trash!(actor)
ChatMention.where(chat_message: message).destroy_all
# FIXME: We should do something to prevent the blue/green bubble
# of other channel members from getting out of sync when a message
# gets deleted.
ChatPublisher.publish_delete!(message.chat_channel, message)
end
end
private
def reset_last_read(message_ids)