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) }