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.rbindex 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 Chatoption :thread_id, optional: truedef 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,+ )+ endend- 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 > 0endendenddiff --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.rbindex 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 dosubject(: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) doFabricate(:user_chat_channel_membership, user: current_user, chat_channel: channel)@@ -112,6 +112,19 @@ RSpec.describe Chat::UpdateUserChannelLastRead doexpect(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" domembership.update!(last_viewed_at: 1.day.ago)old_last_viewed_at = membership.last_viewed_atdiff --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.rbindex 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 dosubject(: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+ endendendend