Fix comment author being notified when mentioning itself (#4456)

* added a condition to check if owner of the comment is in mentions

* added a test to check if comment owner can mention self

* fixed metion test by:
  - adding a second user to simulate a owner of a comment
  - change the user on comment to owned by newly created user
This commit is contained in:
aRtoo 2019-10-25 07:23:45 -07:00 committed by Ben Halpern
parent 2447d6df9e
commit 05cfe16e1c
2 changed files with 14 additions and 4 deletions

View file

@ -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

View file

@ -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