From cd9c8e3303a89e5be3eebe85fa1c385acb07f73a Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 29 Apr 2026 19:57:14 +0200 Subject: [PATCH] fix: skip self-mention notification in private notes (#14318) When an agent mentions themselves in a private note, they no longer receive a redundant notification for their own mention. Closes: #4096 # Pull Request Template ## Description Agents who mention themselves in a private note no longer receive a conversation_mention notification. Previously, the mention service would generate a notification for every mentioned user without checking whether the sender and the mentioned user were the same person. --- app/services/messages/mention_service.rb | 6 ++++ .../services/messages/mention_service_spec.rb | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/app/services/messages/mention_service.rb b/app/services/messages/mention_service.rb index 43bc17fb8..8171f8168 100644 --- a/app/services/messages/mention_service.rb +++ b/app/services/messages/mention_service.rb @@ -50,6 +50,8 @@ class Messages::MentionService def generate_notifications_for_mentions(validated_mentioned_ids) validated_mentioned_ids.each do |user_id| + next if self_mention?(user_id) + NotificationBuilder.new( notification_type: 'conversation_mention', user: User.find(user_id), @@ -60,6 +62,10 @@ class Messages::MentionService end end + def self_mention?(user_id) + message.sender_type == 'User' && user_id.to_i == message.sender_id + end + def add_mentioned_users_as_participants(validated_mentioned_ids) validated_mentioned_ids.each do |user_id| message.conversation.conversation_participants.find_or_create_by(user_id: user_id) diff --git a/spec/services/messages/mention_service_spec.rb b/spec/services/messages/mention_service_spec.rb index 7cdb8ffe7..a7bddcc4e 100644 --- a/spec/services/messages/mention_service_spec.rb +++ b/spec/services/messages/mention_service_spec.rb @@ -165,6 +165,36 @@ describe Messages::MentionService do end end + context 'when the message sender mentions themselves' do + it 'skips the sender notification while notifying other mentioned users' do + message = build( + :message, + conversation: conversation, + account: account, + content: "hey (mention://user/#{first_agent.id}/#{first_agent.name}) and (mention://user/#{second_agent.id}/#{second_agent.name})", + private: true, + sender: first_agent + ) + + described_class.new(message: message).perform + + expect(NotificationBuilder).not_to have_received(:new).with( + notification_type: 'conversation_mention', + user: first_agent, + account: account, + primary_actor: message.conversation, + secondary_actor: message + ) + expect(NotificationBuilder).to have_received(:new).with( + notification_type: 'conversation_mention', + user: second_agent, + account: account, + primary_actor: message.conversation, + secondary_actor: message + ) + end + end + context 'when mentioned user is not an inbox member' do let!(:non_member_user) { create(:user, account: account) }