Skip to content

Commit

Permalink
FIX: nofollow was being added during post processing when it shouldn't
Browse files Browse the repository at this point in the history
  • Loading branch information
eviltrout committed Aug 12, 2016
1 parent 7427209 commit aef9547
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ def post_analyzer
end
end

def add_nofollow?
return user.blank? || SiteSetting.tl3_links_no_follow? || !user.has_trust_level?(TrustLevel[3])

This comment has been minimized.

Copy link
@ZogStriP

ZogStriP Aug 12, 2016

Member

return ?

This comment has been minimized.

Copy link
@eviltrout

eviltrout Aug 15, 2016

Author Contributor

Too much Javascript these days :0

19959c6

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.
Expand All @@ -203,7 +211,7 @@ def cook(*args)
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
Expand Down
3 changes: 3 additions & 0 deletions lib/cooked_post_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
class CookedPostProcessor
include ActionView::Helpers::NumberHelper

attr_reader :cooking_options

def initialize(post, opts={})
@dirty = false
@opts = opts
Expand All @@ -17,6 +19,7 @@ def initialize(post, opts={})
@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))
Expand Down
20 changes: 20 additions & 0 deletions spec/components/cooked_post_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@

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) }
Expand Down

0 comments on commit aef9547

Please sign in to comment.