From 42dcb77d938c32d0355af201270cd12a2f10d2a7 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 14 Jul 2014 13:34:21 -0400 Subject: [PATCH] FEATURE: add site setting leader_links_no_follow to control whether users with trust level 3 or higher have nofollow on their links --- app/models/post.rb | 2 +- app/models/user_profile.rb | 2 +- config/locales/server.en.yml | 1 + config/site_settings.yml | 1 + spec/models/post_spec.rb | 10 ++++++- spec/models/user_profile_spec.rb | 47 +++++++++++++++++++++----------- 6 files changed, 44 insertions(+), 19 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 89b76d4a63a..2cb112279c5 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/models/user_profile.rb b/app/models/user_profile.rb index 1740e3413a2..d7c0bb343a4 100644 --- a/app/models/user_profile.rb +++ b/app/models/user_profile.rb @@ -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 diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ca32399e414..99a9b744c67 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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." diff --git a/config/site_settings.yml b/config/site_settings.yml index 93275554540..ce8f1247a09 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -491,6 +491,7 @@ trust: leader_promotion_min_duration: default: 14 min: 0 + leader_links_no_follow: false security: use_https: false diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index c4ea3551145..7c0ff726d2d 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -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 diff --git a/spec/models/user_profile_spec.rb b/spec/models/user_profile_spec.rb index 72943292e72..fb3beeccf47 100644 --- a/spec/models/user_profile_spec.rb +++ b/spec/models/user_profile_spec.rb @@ -84,25 +84,40 @@ describe UserProfile do expect(user_profile.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") 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 http://ponycorns.com") - expect(user_profile.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") + 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 http://ponycorns.com") + expect(user_profile.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") + 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 http://ponycorns.com") + expect(created_user.user_profile.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") + 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 http://ponycorns.com") + expect(created_user.user_profile.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") + 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 http://ponycorns.com") - expect(created_user.user_profile.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") - 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 http://ponycorns.com") - expect(created_user.user_profile.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") + 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 http://ponycorns.com") + expect(user_profile.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") + end end end end