Skip to content

Commit

Permalink
Posts by trust level 3 users do not have nofollow on their external l…
Browse files Browse the repository at this point in the history
…inks.
  • Loading branch information
nlalonde committed Jan 15, 2014
1 parent 5a238c6 commit 4f6b208
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 8 deletions.
11 changes: 10 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,16 @@ def cook(*args)
return raw if cook_method == Post.cook_methods[:raw_html]

# Default is to cook posts
Plugin::Filter.apply(:after_post_cook, self, post_analyzer.cook(*args))
cooked = if !self.user || !self.user.has_trust_level?(:leader)
post_analyzer.cook(*args)
else
# At trust level 3, we don't apply nofollow to links
cloned = args.dup
cloned[1] ||= {}
cloned[1][:omit_nofollow] = true
post_analyzer.cook(*cloned)
end
Plugin::Filter.apply( :after_post_cook, self, cooked )
end

# Sometimes the post is being edited by someone else, for example, a mod.
Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ def admin?
def change_trust_level!(level)
raise "Invalid trust level #{level}" unless TrustLevel.valid_level?(level)
self.trust_level = TrustLevel.levels[level]
self.bio_raw_will_change! # So it can get re-cooked based on the new trust level
transaction do
self.save!
Group.user_trust_level_change!(self.id, self.trust_level)
Expand Down Expand Up @@ -526,7 +527,7 @@ def find_email

def cook
if bio_raw.present?
self.bio_cooked = PrettyText.cook(bio_raw) if bio_raw_changed?
self.bio_cooked = PrettyText.cook(bio_raw, omit_nofollow: self.has_trust_level?(:leader)) if bio_raw_changed?
else
self.bio_cooked = nil
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pretty_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def self.cook(text, opts={})
# we have a minor inconsistency
cloned[:topicId] = opts[:topic_id]
sanitized = markdown(text.dup, cloned)
sanitized = add_rel_nofollow_to_user_content(sanitized) if SiteSetting.add_rel_nofollow_to_user_content
sanitized = add_rel_nofollow_to_user_content(sanitized) if !cloned[:omit_nofollow] && SiteSetting.add_rel_nofollow_to_user_content
sanitized
end

Expand Down
4 changes: 4 additions & 0 deletions spec/components/pretty_text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
it "should not inject nofollow for bar.foo.com" do
(PrettyText.cook("<a href='http://bar.foo.com/test.html'>cnn</a>") !~ /nofollow/).should be_true
end

it "should not inject nofollow if omit_nofollow option is given" do
(PrettyText.cook('<a href="http://cnn.com">cnn</a>', omit_nofollow: true) !~ /nofollow/).should be_true
end
end

describe "Excerpt" do
Expand Down
16 changes: 16 additions & 0 deletions spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -771,4 +771,20 @@ def post_with_body(body, user=nil)
end
end

describe "cooking" do
let(:post) { Fabricate.build(:post, post_args.merge(raw: "please read my blog http://blog.example.com")) }

it "should add nofollow to links in the post for trust levels below 3" do
post.user.trust_level = 2
post.save
post.cooked.should =~ /nofollow/
end

it "should not add nofollow for trust level 3 and higher" do
post.user.trust_level = 3
post.save
(post.cooked =~ /nofollow/).should be_false
end
end

end
29 changes: 24 additions & 5 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -762,21 +762,40 @@
context "with a user that has a link in their bio" do
let(:user) { Fabricate.build(:user, bio_raw: "im sissy and i love http://ponycorns.com") }

before do
# Let's cook that bio up good
it "includes the link as nofollow if the user is not new" do
user.send(:cook)
end

it "includes the link if the user is not new" do
expect(user.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com' rel='nofollow'>http://ponycorns.com</a>")
expect(user.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\" rel=\"nofollow\">http://ponycorns.com</a></p>")
end

it "removes the link if the user is new" do
user.trust_level = TrustLevel.levels[:newuser]
user.send(:cook)
expect(user.bio_excerpt).to eq("im sissy and i love http://ponycorns.com")
expect(user.bio_processed).to eq("<p>im sissy and i love http://ponycorns.com</p>")
end

it "includes the link without nofollow if the user is trust level 3 or higher" do
user.trust_level = TrustLevel.levels[:leader]
user.send(:cook)
expect(user.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com'>http://ponycorns.com</a>")
expect(user.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\">http://ponycorns.com</a></p>")
end

it "removes nofollow from links in bio when trust level is increased" do
user.save
user.change_trust_level!(:leader)
expect(user.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com'>http://ponycorns.com</a>")
expect(user.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\">http://ponycorns.com</a></p>")
end

it "adds nofollow to links in bio when trust level is decreased" do
user.trust_level = TrustLevel.levels[:leader]
user.save
user.change_trust_level!(:regular)
expect(user.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com' rel='nofollow'>http://ponycorns.com</a>")
expect(user.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\" rel=\"nofollow\">http://ponycorns.com</a></p>")
end
end

end
Expand Down

0 comments on commit 4f6b208

Please sign in to comment.