Skip to content

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

FIX: Publish notification state when chat mentions are marked read (#44263)

Régis Hanolbb3e4e64e806
diff --git a/plugins/chat/app/services/chat/action/mark_mentions_read.rb b/plugins/chat/app/services/chat/action/mark_mentions_read.rb
index 45c22672c72..48142fee10b 100644
--- a/plugins/chat/app/services/chat/action/mark_mentions_read.rb
+++ b/plugins/chat/app/services/chat/action/mark_mentions_read.rb
@@ -18,29 +18,33 @@ module Chat
option :thread_id, optional: true
def call
- ::Notification
- .where(notification_type: Notification.types[:chat_mention])
- .where(user: user)
- .where(read: false)
- .joins(
- "INNER JOIN chat_mention_notifications ON chat_mention_notifications.notification_id = notifications.id",
- )
- .joins(
- "INNER JOIN chat_mentions ON chat_mentions.id = chat_mention_notifications.chat_mention_id",
- )
- .joins("INNER JOIN chat_messages ON chat_mentions.chat_message_id = chat_messages.id")
- .where("chat_messages.chat_channel_id IN (?)", channel_ids)
- .then do |notifications|
- break notifications if message_id.blank? && thread_id.blank?
- break notifications.where("chat_messages.id <= ?", message_id) if message_id.present?
- if thread_id.present?
- notifications.where(
- "chat_messages.id IN (SELECT id FROM chat_messages WHERE thread_id = ?)",
- thread_id,
- )
+ marked_count =
+ ::Notification
+ .where(notification_type: Notification.types[:chat_mention])
+ .where(user: user)
+ .where(read: false)
+ .joins(
+ "INNER JOIN chat_mention_notifications ON chat_mention_notifications.notification_id = notifications.id",
+ )
+ .joins(
+ "INNER JOIN chat_mentions ON chat_mentions.id = chat_mention_notifications.chat_mention_id",
+ )
+ .joins("INNER JOIN chat_messages ON chat_mentions.chat_message_id = chat_messages.id")
+ .where("chat_messages.chat_channel_id IN (?)", channel_ids)
+ .then do |notifications|
+ break notifications if message_id.blank? && thread_id.blank?
+ break notifications.where("chat_messages.id <= ?", message_id) if message_id.present?
+ if thread_id.present?
+ notifications.where(
+ "chat_messages.id IN (SELECT id FROM chat_messages WHERE thread_id = ?)",
+ thread_id,
+ )
+ end
end
- end
- .update_all(read: true)
+ .update_all(read: true)
+
+ # update_all skips Notification callbacks, so clients wouldn't learn about the change
+ DB.after_commit { user.reload.publish_notifications_state } if marked_count > 0
end
end
end
diff --git a/plugins/chat/spec/services/chat/update_user_channel_last_read_spec.rb b/plugins/chat/spec/services/chat/update_user_channel_last_read_spec.rb
index fe15dbacbc3..f633c53b7c7 100644
--- a/plugins/chat/spec/services/chat/update_user_channel_last_read_spec.rb
+++ b/plugins/chat/spec/services/chat/update_user_channel_last_read_spec.rb
@@ -10,7 +10,7 @@ RSpec.describe Chat::UpdateUserChannelLastRead do
subject(:result) { described_class.call(params:, **dependencies) }
fab!(:chatters, :group)
- fab!(:current_user) { Fabricate(:user, group_ids: [chatters.id]) }
+ fab!(:current_user) { Fabricate(:user, group_ids: [chatters.id], last_seen_at: 1.minute.ago) }
fab!(:channel, :chat_channel)
let(:membership) do
Fabricate(:user_chat_channel_membership, user: current_user, chat_channel: channel)
@@ -112,6 +112,19 @@ RSpec.describe Chat::UpdateUserChannelLastRead do
expect(messages.map(&:channel)).to include("/chat/user-tracking-state/#{current_user.id}")
end
+ it "publishes the updated notifications state" do
+ message = messages.find { |m| m.channel == "/notification/#{current_user.id}" }
+ expect(message.data[:recent]).to include([notification.id, true])
+ end
+
+ context "when there are no unread mentions" do
+ before { notification.update!(read: true) }
+
+ it "does not publish the notifications state" do
+ expect(messages.map(&:channel)).not_to include("/notification/#{current_user.id}")
+ end
+ end
+
it "updates the channel membership last_viewed_at datetime" do
membership.update!(last_viewed_at: 1.day.ago)
old_last_viewed_at = membership.last_viewed_at
diff --git a/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb b/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb
index 3cf9895483c..7fa1817d2b3 100644
--- a/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb
+++ b/plugins/chat/spec/services/chat/update_user_thread_last_read_spec.rb
@@ -10,7 +10,7 @@ RSpec.describe Chat::UpdateUserThreadLastRead do
subject(:result) { described_class.call(params:, **dependencies) }
fab!(:chatters, :group)
- fab!(:current_user) { Fabricate(:user, group_ids: [chatters.id]) }
+ fab!(:current_user) { Fabricate(:user, group_ids: [chatters.id], last_seen_at: 1.minute.ago) }
fab!(:thread) { Fabricate(:chat_thread, old_om: true) }
fab!(:reply_1) { Fabricate(:chat_message, thread: thread, chat_channel_id: thread.channel.id) }
@@ -129,6 +129,13 @@ RSpec.describe Chat::UpdateUserThreadLastRead do
.count
}.by(-1)
end
+
+ it "publishes the updated notifications state" do
+ params[:message_id] = reply_3.id
+ messages = MessageBus.track_publish("/notification/#{current_user.id}") { result }
+
+ expect(messages).not_to be_empty
+ end
end
end
end