FEATURE: add site setting leader_links_no_follow to control whether users with trust level 3 or higher have nofollow on their links

This commit is contained in:
Neil Lalonde 2014-07-14 13:34:21 -04:00
parent cce7cf8c85
commit 42dcb77d93
6 changed files with 44 additions and 19 deletions

View File

@ -140,7 +140,7 @@ class Post < ActiveRecord::Base
return raw if cook_method == Post.cook_methods[:raw_html]
# Default is to cook posts
cooked = if !self.user || !self.user.has_trust_level?(:leader)
cooked = if !self.user || SiteSetting.leader_links_no_follow || !self.user.has_trust_level?(:leader)
post_analyzer.cook(*args)
else
# At trust level 3, we don't apply nofollow to links

View File

@ -48,7 +48,7 @@ class UserProfile < ActiveRecord::Base
def cook
if self.bio_raw.present?
self.bio_cooked = PrettyText.cook(self.bio_raw, omit_nofollow: user.has_trust_level?(:leader)) if bio_raw_changed?
self.bio_cooked = PrettyText.cook(self.bio_raw, omit_nofollow: user.has_trust_level?(:leader) && !SiteSetting.leader_links_no_follow) if bio_raw_changed?
else
self.bio_cooked = nil
end

View File

@ -841,6 +841,7 @@ en:
leader_requires_posts_read_all_time: "The minimum total number of posts a user must have read to qualify for leader (3) trust level."
leader_requires_max_flagged: "User must not have had more than x posts flagged by x different users in the last 100 days to qualify for promotion to leader (3) trust level, where x is this setting's value. (0 or higher)"
leader_promotion_min_duration: "The minimum number of days that a promotion to leader lasts before a user can be demoted."
leader_links_no_follow: "Whether links from leaders have the nofollow attribute."
min_trust_to_create_topic: "The minimum trust level required to create a new topic."

View File

@ -491,6 +491,7 @@ trust:
leader_promotion_min_duration:
default: 14
min: 0
leader_links_no_follow: false
security:
use_https: false

View File

@ -763,11 +763,19 @@ describe Post do
post.cooked.should =~ /nofollow/
end
it "should not add nofollow for trust level 3 and higher" do
it "when leader_links_no_follow is false, should not add nofollow for trust level 3 and higher" do
SiteSetting.stubs(:leader_links_no_follow).returns(false)
post.user.trust_level = 3
post.save
(post.cooked =~ /nofollow/).should be_false
end
it "when leader_links_no_follow is true, should add nofollow for trust level 3 and higher" do
SiteSetting.stubs(:leader_links_no_follow).returns(true)
post.user.trust_level = 3
post.save
(post.cooked =~ /nofollow/).should be_true
end
end
describe "calculate_avg_time" do

View File

@ -84,25 +84,40 @@ describe UserProfile do
expect(user_profile.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_profile.send(:cook)
expect(user_profile.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com'>http://ponycorns.com</a>")
expect(user_profile.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\">http://ponycorns.com</a></p>")
context 'leader_links_no_follow is false' do
before { SiteSetting.stubs(:leader_links_no_follow).returns(false) }
it 'includes the link without nofollow if the user is trust level 3 or higher' do
user.trust_level = TrustLevel.levels[:leader]
user_profile.send(:cook)
expect(user_profile.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com'>http://ponycorns.com</a>")
expect(user_profile.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
created_user.change_trust_level!(:leader)
expect(created_user.user_profile.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com'>http://ponycorns.com</a>")
expect(created_user.user_profile.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
created_user.trust_level = TrustLevel.levels[:leader]
created_user.save
created_user.change_trust_level!(:regular)
expect(created_user.user_profile.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com' rel='nofollow'>http://ponycorns.com</a>")
expect(created_user.user_profile.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\" rel=\"nofollow\">http://ponycorns.com</a></p>")
end
end
it 'removes nofollow from links in bio when trust level is increased' do
created_user.change_trust_level!(:leader)
expect(created_user.user_profile.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com'>http://ponycorns.com</a>")
expect(created_user.user_profile.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\">http://ponycorns.com</a></p>")
end
context 'leader_links_no_follow is true' do
before { SiteSetting.stubs(:leader_links_no_follow).returns(true) }
it 'adds nofollow to links in bio when trust level is decreased' do
created_user.trust_level = TrustLevel.levels[:leader]
created_user.save
created_user.change_trust_level!(:regular)
expect(created_user.user_profile.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com' rel='nofollow'>http://ponycorns.com</a>")
expect(created_user.user_profile.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\" rel=\"nofollow\">http://ponycorns.com</a></p>")
it 'includes the link with nofollow if the user is trust level 3 or higher' do
user.trust_level = TrustLevel.levels[:leader]
user_profile.send(:cook)
expect(user_profile.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com' rel='nofollow'>http://ponycorns.com</a>")
expect(user_profile.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
end