From f9a60676627dbd6facdfbb0104b4e2fbda5c71a5 Mon Sep 17 00:00:00 2001 From: Vaidehi Joshi Date: Wed, 28 Apr 2021 08:27:26 -0700 Subject: [PATCH] Do not create mentions when embedded within a comment liquid tag (#13552) Fixes https://github.com/forem/forem/issues/13348. --- app/services/mentions/create_all.rb | 8 ++++++-- spec/services/mentions/create_all_spec.rb | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/services/mentions/create_all.rb b/app/services/mentions/create_all.rb index a3a51ccfa..a7d3753d6 100644 --- a/app/services/mentions/create_all.rb +++ b/app/services/mentions/create_all.rb @@ -38,9 +38,13 @@ module Mentions def extract_usernames_from_mentions_in_text # The "mentioned-user" css is added by Html::Parser#user_link_if_exists doc = Nokogiri::HTML(notifiable.processed_html) - doc.css(".mentioned-user").map do |link| - link.text.delete("@").downcase + + # Remove any mentions that are embedded within a comment liquid tag + non_liquid_tag_mentions = doc.css(".mentioned-user").reject do |tag| + tag.ancestors(".liquid-comment").any? end + + non_liquid_tag_mentions.map { |link| link.text.delete("@").downcase } end def reject_notifiable_author(users) diff --git a/spec/services/mentions/create_all_spec.rb b/spec/services/mentions/create_all_spec.rb index a46d092c5..f6bc5395f 100644 --- a/spec/services/mentions/create_all_spec.rb +++ b/spec/services/mentions/create_all_spec.rb @@ -39,6 +39,15 @@ RSpec.describe Mentions::CreateAll, type: :service do expect(Mention.all.size).to eq(1) end + it "ignores mentions when embedded within a comment liquid tag" do + markdown = "Check out this comment: {% comment #{comment2.id_code_generated} %}" + comment_with_liquid_tag = create(:comment, user_id: user2.id, commentable: article, body_markdown: markdown) + + comment_with_liquid_tag.save + described_class.call(comment_with_liquid_tag) + expect(Mention.all.size).to eq(0) + end + it "creates multiple mentions for multiple users" do user2 = create(:user) comment.body_markdown = "Hello @#{user.username} @#{user2.username}, you are cool." @@ -59,18 +68,18 @@ RSpec.describe Mentions::CreateAll, type: :service do expect(Mention.all.size).to eq(1) end - it "creates mention on creation of comment (in addition to update)" do + it "creates a mention on creation of comment" do described_class.call(comment2) expect(Mention.all.size).to eq(1) end - it "can only be created with valid mentionable" do + it "does not create a mention without valid mentionable" do comment2.update_column(:body_markdown, "") described_class.call(comment2) expect(Mention.all.size).to eq(0) end - it "cant mention self" do + it "does not create a mention when the user mentions themselves" do comment.body_markdown = "Me, Myself and I @#{user2.username}" comment.save described_class.call(comment)