From ca6719b9b17616037bda4bb26856516e3eba6a12 Mon Sep 17 00:00:00 2001 From: Lewis Sparlin Date: Wed, 17 Nov 2021 04:27:50 -0500 Subject: [PATCH] Issue #9077 has already been resolved, added specs (#15342) * Prove #9077 has been resolved * Do no create mentions for any @mention-like syntax from embedded liquid, add spec --- app/services/mentions/create_all.rb | 6 +++-- spec/services/mentions/create_all_spec.rb | 30 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/app/services/mentions/create_all.rb b/app/services/mentions/create_all.rb index d5675fbc0..2940e76f2 100644 --- a/app/services/mentions/create_all.rb +++ b/app/services/mentions/create_all.rb @@ -41,9 +41,11 @@ module Mentions # The "mentioned-user" css is added by Html::Parser#user_link_if_exists doc = Nokogiri::HTML(notifiable.processed_html) - # Remove any mentions that are embedded within a comment liquid tag + # Remove any mentions that are embedded within any liquid tags non_liquid_tag_mentions = doc.css(".mentioned-user").reject do |tag| - tag.ancestors(".liquid-comment").any? + tag.ancestors(".liquid-comment").any? || + tag.ancestors("[class^=ltag]").any? || + tag.ancestors("[class*=' ltag']").any? end non_liquid_tag_mentions.map { |link| link.text.delete("@").downcase } diff --git a/spec/services/mentions/create_all_spec.rb b/spec/services/mentions/create_all_spec.rb index f31747085..0da35c338 100644 --- a/spec/services/mentions/create_all_spec.rb +++ b/spec/services/mentions/create_all_spec.rb @@ -16,6 +16,31 @@ RSpec.shared_examples "valid notifiable and no mentions" do described_class.call(notifiable) expect(Mention.all.size).to eq(1) end + + it "does not create a mention if notifiable is updated with mention inside code block", :aggregate_failures do + set_markdown_and_save(notifiable, mention_code_markdown) + described_class.call(notifiable) + expect(Mention.all.size).to eq(0) + end + + it "does not create a mention if notifiable is updated with mention inside code snippet", :aggregate_failures do + set_markdown_and_save(notifiable, mention_snippet_markdown) + described_class.call(notifiable) + expect(Mention.all.size).to eq(0) + end + + it "does not create a mention if notifiable is updated liquid tag that would render mention-like text", + :aggregate_failures do + mock_liquid_template = Liquid::Template.new + allow(Liquid::Template).to receive(:parse).and_return(mock_liquid_template) + allow(mock_liquid_template).to receive(:render).and_return( + "

A sample github

Embedded content @#{user.username}
", + ) + + set_markdown_and_save(notifiable, mention_liquid) + described_class.call(notifiable) + expect(Mention.all.size).to eq(0) + end end RSpec.shared_examples "valid notifiable and has mentions" do @@ -115,7 +140,10 @@ RSpec.describe Mentions::CreateAll, type: :service do let(:user2) { create(:user) } let(:article) { create(:article, user_id: user.id) } let(:markdown) { "Hello, you are cool." } - let(:mention_markdown) { "Hello @#{user.username}, you are cool." } + let(:mention_markdown) { "Hello @#{user.username}, you are cool." } + let(:mention_snippet_markdown) { "This is how I would mention `@#{user.username}` without a notification" } + let(:mention_code_markdown) { " ... ``` mention a user @#{user.username} in a code block``` ..." } + let(:mention_liquid) { " A sample github\n {% github https://github.com/forem/forem/pull/ %} ..." } def set_markdown_and_save(notifiable, markdown) notifiable.update(body_markdown: markdown)