Skip to content

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

FIX: Update `last_read_message_id` when moving chat messages (#22643)

Jan Cernikd258bda2fe94
diff --git a/plugins/chat/lib/chat/message_mover.rb b/plugins/chat/lib/chat/message_mover.rb
index 9103b2bf832..bdca6b39772 100644
--- a/plugins/chat/lib/chat/message_mover.rb
+++ b/plugins/chat/lib/chat/message_mover.rb
@@ -66,6 +66,7 @@ module Chat
update_references
delete_source_messages
update_reply_references
+ update_tracking_state
update_thread_references
end
@@ -208,6 +209,10 @@ module Chat
SQL
end
+ def update_tracking_state
+ ::Chat::Action::ResetUserLastReadChannelMessage.call(@source_message_ids, @source_channel.id)
+ end
+
def update_thread_references
threads_to_update = []
@source_messages
diff --git a/plugins/chat/spec/lib/chat/message_mover_spec.rb b/plugins/chat/spec/lib/chat/message_mover_spec.rb
index aef48ddb241..c458a7d749f 100644
--- a/plugins/chat/spec/lib/chat/message_mover_spec.rb
+++ b/plugins/chat/spec/lib/chat/message_mover_spec.rb
@@ -183,6 +183,31 @@ describe Chat::MessageMover do
expect(message3.reload.thread).to eq(thread)
end
+ it "updates the tracking to the last non-deleted channel message for users whose last_read_message_id was the moved message" do
+ membership_1 =
+ Fabricate(
+ :user_chat_channel_membership,
+ chat_channel: source_channel,
+ last_read_message: message1,
+ )
+ membership_2 =
+ Fabricate(
+ :user_chat_channel_membership,
+ chat_channel: source_channel,
+ last_read_message: message2,
+ )
+ membership_3 =
+ Fabricate(
+ :user_chat_channel_membership,
+ chat_channel: source_channel,
+ last_read_message: message3,
+ )
+ move!([message2.id])
+ expect(membership_1.reload.last_read_message_id).to eq(message1.id)
+ expect(membership_2.reload.last_read_message_id).to eq(message3.id)
+ expect(membership_3.reload.last_read_message_id).to eq(message3.id)
+ end
+
context "when a thread original message is moved" do
it "creates a new thread for the messages left behind in the old channel" do
message4 =