Skip to content

A platform for community discussion. Free, open, simple.

FIX: webhook should touch channel’s last_message_sent_at (#19342)

Joffrey JAFFEUX179d15d90edb
diff --git a/plugins/chat/app/controllers/chat_controller.rb b/plugins/chat/app/controllers/chat_controller.rb
index 3f78f15b8ef..57fc55e6da6 100644
--- a/plugins/chat/app/controllers/chat_controller.rb
+++ b/plugins/chat/app/controllers/chat_controller.rb
@@ -110,7 +110,6 @@ class Chat::ChatController < Chat::ChatBaseController
return render_json_error(chat_message_creator.error) if chat_message_creator.failed?
- @chat_channel.touch(:last_message_sent_at)
@user_chat_channel_membership.update(last_read_message_id: chat_message_creator.chat_message.id)
if @chat_channel.direct_message_channel?
diff --git a/plugins/chat/lib/chat_message_creator.rb b/plugins/chat/lib/chat_message_creator.rb
index 629c07e390c..fe7fed04599 100644
--- a/plugins/chat/lib/chat_message_creator.rb
+++ b/plugins/chat/lib/chat_message_creator.rb
@@ -53,6 +53,7 @@ class Chat::ChatMessageCreator
chat_message: @chat_message,
timestamp: @chat_message.created_at,
)
+ @chat_channel.touch(:last_message_sent_at)
DiscourseEvent.trigger(:chat_message_created, @chat_message, @chat_channel, @user)
rescue => error
@error = error
diff --git a/plugins/chat/spec/components/chat_message_creator_spec.rb b/plugins/chat/spec/components/chat_message_creator_spec.rb
index 0520ce31d8b..6f87534fa29 100644
--- a/plugins/chat/spec/components/chat_message_creator_spec.rb
+++ b/plugins/chat/spec/components/chat_message_creator_spec.rb
@@ -112,6 +112,18 @@ describe Chat::ChatMessageCreator do
}.to change { ChatMessage.count }.by(1)
end
+ it "updates the channel’s last message date" do
+ previous_last_message_sent_at = public_chat_channel.last_message_sent_at
+
+ Chat::ChatMessageCreator.create(
+ chat_channel: public_chat_channel,
+ user: user1,
+ content: "this is a message",
+ )
+
+ expect(previous_last_message_sent_at).to be < public_chat_channel.reload.last_message_sent_at
+ end
+
it "sets the last_editor_id to the user who created the message" do
message =
Chat::ChatMessageCreator.create(