diff --git a/app/models/post.rb b/app/models/post.rb index 2273f358a01..d829a646548 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -188,6 +188,14 @@ class Post < ActiveRecord::Base end end + def add_nofollow? + return user.blank? || SiteSetting.tl3_links_no_follow? || !user.has_trust_level?(TrustLevel[3]) + end + + def omit_nofollow? + return !add_nofollow? + end + def cook(*args) # For some posts, for example those imported via RSS, we support raw HTML. In that # case we can skip the rendering pipeline. @@ -203,7 +211,7 @@ class Post < ActiveRecord::Base post_user = self.user cloned[1][:user_id] = post_user.id if post_user - cooked = if !post_user || SiteSetting.tl3_links_no_follow || !post_user.has_trust_level?(TrustLevel[3]) + cooked = if add_nofollow? post_analyzer.cook(*args) else # At trust level 3, we don't apply nofollow to links diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 720d1a6ff01..8c63bc61bcf 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -7,6 +7,8 @@ require_dependency 'pretty_text' class CookedPostProcessor include ActionView::Helpers::NumberHelper + attr_reader :cooking_options + def initialize(post, opts={}) @dirty = false @opts = opts @@ -17,6 +19,7 @@ class CookedPostProcessor @cooking_options = post.cooking_options || opts[:cooking_options] || {} @cooking_options[:topic_id] = post.topic_id @cooking_options = @cooking_options.symbolize_keys + @cooking_options[:omit_nofollow] = true if post.omit_nofollow? analyzer = post.post_analyzer @doc = Nokogiri::HTML::fragment(analyzer.cook(post.raw, @cooking_options)) diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 79ef3a248a2..b430b0a9737 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -20,6 +20,26 @@ describe CookedPostProcessor do end + context "cooking options" do + context "regular user" do + let(:post) { Fabricate(:post) } + + it "doesn't omit nofollow" do + cpp = CookedPostProcessor.new(post) + expect(cpp.cooking_options[:omit_nofollow]).to eq(nil) + end + end + + context "admin user" do + let(:post) { Fabricate(:post, user: Fabricate(:admin) ) } + + it "omits nofollow" do + cpp = CookedPostProcessor.new(post) + expect(cpp.cooking_options[:omit_nofollow]).to eq(true) + end + end + end + context ".keep_reverse_index_up_to_date" do let(:post) { build(:post_with_uploads, id: 123) }