DEV: Add 'chat_message_trashed' Discourse event (#20170)

Triggers a DiscourseEvent when a message is deleted, similar to
`:chat_message_created` and `:chat_message_edited`. This is not used
in this plugin, but can be used by other plugins to act when a message
is trashed.
This commit is contained in:
Dan Ungureanu 2023-02-06 22:52:43 +02:00 committed by GitHub
parent 0ca2541b74
commit 25f2fb61b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 3 deletions

View File

@ -15,6 +15,7 @@ class ChatMessageDestroyer
ChatMessage.transaction do
message.trash!(actor)
ChatMention.where(chat_message: message).destroy_all
DiscourseEvent.trigger(:chat_message_trashed, message, message.chat_channel, actor)
# FIXME: We should do something to prevent the blue/green bubble
# of other channel members from getting out of sync when a message

View File

@ -567,10 +567,17 @@ RSpec.describe Chat::ChatController do
it "Allows admin to delete others' messages" do
sign_in(admin)
expect { delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json" }.to change {
ChatMessage.count
}.by(-1)
events = nil
expect do
events =
DiscourseEvent.track_events do
delete "/chat/#{chat_channel.id}/#{ChatMessage.last.id}.json"
end
end.to change { ChatMessage.count }.by(-1)
expect(response.status).to eq(200)
expect(events.size).to eq(1)
expect(events.first[:event_name]).to eq(:chat_message_trashed)
end
it "does not allow message delete when chat channel is read_only" do

View File

@ -86,5 +86,13 @@ RSpec.describe ChatMessageDestroyer do
expect(message_data[:deleted_id]).to eq(message_1.id)
expect(message_data[:deleted_at]).to be_present
end
it "triggers a DiscourseEvent" do
delete_event =
DiscourseEvent.track_events { described_class.new.trash_message(message_1, actor) }.first
expect(delete_event[:event_name]).to eq(:chat_message_trashed)
expect(delete_event[:params]).to eq([message_1, message_1.chat_channel, actor])
end
end
end