Posts by trust level 3 users do not have nofollow on their external links.

This commit is contained in:
Neil Lalonde 2014-01-15 11:34:17 -05:00
parent 5a238c62eb
commit 4f6b208e8d
6 changed files with 57 additions and 8 deletions

View File

@ -133,7 +133,16 @@ class Post < ActiveRecord::Base
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.

View File

@ -408,6 +408,7 @@ class User < ActiveRecord::Base
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)
@ -526,7 +527,7 @@ class User < ActiveRecord::Base
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

View File

@ -167,7 +167,7 @@ module PrettyText
# 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

View File

@ -74,6 +74,10 @@ describe PrettyText do
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

View File

@ -771,4 +771,20 @@ describe Post do
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

View File

@ -762,21 +762,40 @@ describe User do
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