diff --git a/app/services/mentions/create_all.rb b/app/services/mentions/create_all.rb index 505c35adc..a143f31bf 100644 --- a/app/services/mentions/create_all.rb +++ b/app/services/mentions/create_all.rb @@ -16,7 +16,9 @@ module Mentions mentions = [] doc.css(".comment-mentioned-user").each do |link| username = link.text.delete("@").downcase - if (user = User.find_by(username: username)) + user = User.find_by(username: username) + + if user && user.id != notifiable.user_id usernames << username mentions << create_mention(user) end diff --git a/spec/services/mentions/create_all_spec.rb b/spec/services/mentions/create_all_spec.rb index 2f3b36d8f..d309d3369 100644 --- a/spec/services/mentions/create_all_spec.rb +++ b/spec/services/mentions/create_all_spec.rb @@ -2,18 +2,19 @@ require "rails_helper" RSpec.describe Mentions::CreateAll do let(:user) { create(:user) } + let(:user2) { create(:user) } let(:article) { create(:article, user_id: user.id) } - let(:comment) { create(:comment, user_id: user.id, commentable_id: article.id) } + let(:comment) { create(:comment, user_id: user2.id, commentable_id: article.id) } let(:comment2) do create( :comment, body_markdown: "Hello @#{user.username}, you are cool.", - user_id: user.id, + user_id: user2.id, commentable_id: article.id, ) end - it "creates mention if there is a user mentioned" do + it "creates mention if there is a user mentioned and if the user doenst own the comment" do comment.body_markdown = "Hello @#{user.username}, you are cool." comment.save described_class.call(comment) @@ -68,4 +69,11 @@ RSpec.describe Mentions::CreateAll do described_class.call(comment2) expect(Mention.all.size).to eq(0) end + + it "cant mention self" do + comment.body_markdown = "Me, Myself and I @#{user2.username}" + comment.save + described_class.call(comment) + expect(Mention.all.size).to eq(0) + end end