FEATURE: don't demote trust level 3 users who were promoted less than SiteSetting.leader_promotion_min_duration days ago

This commit is contained in:
Neil Lalonde 2014-07-08 17:39:36 -04:00
parent 04e5c59598
commit df8b25d2f5
9 changed files with 63 additions and 10 deletions

View File

@ -86,9 +86,6 @@
{{/unless}} {{/unless}}
{{else}} {{else}}
<i class="fa fa-times"></i> {{i18n admin.user.tl3_requirements.does_not_qualify}} <i class="fa fa-times"></i> {{i18n admin.user.tl3_requirements.does_not_qualify}}
{{#if isLeader}}
{{i18n admin.user.tl3_requirements.will_be_demoted}}
{{/if}}
{{/if}} {{/if}}
</p> </p>
</div> </div>

View File

@ -7,6 +7,13 @@ module Jobs
# Demotions # Demotions
demoted_user_ids = [] demoted_user_ids = []
User.real.where(trust_level: TrustLevel.levels[:leader]).find_each do |u| User.real.where(trust_level: TrustLevel.levels[:leader]).find_each do |u|
# Don't demote too soon after being promoted
next if UserHistory.for(u, :auto_trust_level_change)
.where('created_at >= ?', SiteSetting.leader_promotion_min_duration.to_i.days.ago)
.where(previous_value: TrustLevel.levels[:regular].to_s)
.where(new_value: TrustLevel.levels[:leader].to_s)
.exists?
unless Promotion.leader_met?(u) unless Promotion.leader_met?(u)
demoted_user_ids << u.id demoted_user_ids << u.id
Promotion.new(u).change_trust_level!(:regular) Promotion.new(u).change_trust_level!(:regular)

View File

@ -25,7 +25,8 @@ class UserHistory < ActiveRecord::Base
:unsuspend_user, :unsuspend_user,
:facebook_no_email, :facebook_no_email,
:grant_badge, :grant_badge,
:revoke_badge) :revoke_badge,
:auto_trust_level_change)
end end
# Staff actions is a subset of all actions, used to audit actions taken by staff users. # Staff actions is a subset of all actions, used to audit actions taken by staff users.

View File

@ -1829,7 +1829,6 @@ en:
qualifies: "Qualifies for trust level 3." qualifies: "Qualifies for trust level 3."
will_be_promoted: "Will be promoted within 24 hours." will_be_promoted: "Will be promoted within 24 hours."
does_not_qualify: "Doesn't qualify for trust level 3." does_not_qualify: "Doesn't qualify for trust level 3."
will_be_demoted: "Will be demoted within 24 hours."
site_content: site_content:
none: "Choose a type of content to begin editing." none: "Choose a type of content to begin editing."

View File

@ -816,6 +816,7 @@ en:
leader_requires_topics_viewed_all_time: "The minimum total number of topics a user must have viewed to qualify for leader (3) trust level." leader_requires_topics_viewed_all_time: "The minimum total number of topics a user must have viewed to qualify for leader (3) trust level."
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_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_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."
min_trust_to_create_topic: "The minimum trust level required to create a new topic." min_trust_to_create_topic: "The minimum trust level required to create a new topic."

View File

@ -483,6 +483,9 @@ trust:
leader_requires_max_flagged: leader_requires_max_flagged:
default: 5 default: 5
min: 0 min: 0
leader_promotion_min_duration:
default: 14
min: 0
security: security:
use_https: false use_https: false

View File

@ -62,6 +62,11 @@ class Promotion
@user.transaction do @user.transaction do
if admin if admin
StaffActionLogger.new(admin).log_trust_level_change(@user, old_level, new_level) StaffActionLogger.new(admin).log_trust_level_change(@user, old_level, new_level)
else
UserHistory.create!( action: UserHistory.actions[:auto_trust_level_change],
target_user_id: @user.id,
previous_value: old_level,
new_value: new_level)
end end
@user.save! @user.save!
@user.user_profile.recook_bio @user.user_profile.recook_bio

View File

@ -118,6 +118,12 @@ describe Promotion do
promotion.review_regular promotion.review_regular
}.to_not change { user.reload.trust_level } }.to_not change { user.reload.trust_level }
end end
it "doesn't log a trust level change" do
expect {
promotion.review_regular
}.to_not change { UserHistory.count }
end
end end
context "qualifies for promotion" do context "qualifies for promotion" do
@ -133,6 +139,12 @@ describe Promotion do
promotion.review_regular.should == true promotion.review_regular.should == true
user.reload.trust_level.should == TrustLevel.levels[:leader] user.reload.trust_level.should == TrustLevel.levels[:leader]
end end
it "logs a trust level change" do
expect {
promotion.review_regular
}.to change { UserHistory.where(action: UserHistory.actions[:auto_trust_level_change]).count }.by(1)
end
end end
end end

View File

@ -19,10 +19,38 @@ describe Jobs::LeaderPromotions do
run_job run_job
end end
it "demotes tl3 user who doesn't qualify for tl3 anymore" do context "tl3 user who doesn't qualify for tl3 anymore" do
tl3_user = Fabricate(:user, trust_level: TrustLevel.levels[:leader]) def create_leader_user
LeaderRequirements.any_instance.stubs(:requirements_met?).returns(false) user = Fabricate(:user, trust_level: TrustLevel.levels[:regular])
Promotion.any_instance.expects(:change_trust_level!).with(:regular, anything).once LeaderRequirements.any_instance.stubs(:requirements_met?).returns(true)
run_job Promotion.new(user).review_regular.should == true
user
end
before do
SiteSetting.stubs(:leader_promotion_min_duration).returns(3)
end
it "demotes if was promoted more than X days ago" do
user = nil
Timecop.freeze(4.days.ago) do
user = create_leader_user
end
LeaderRequirements.any_instance.stubs(:requirements_met?).returns(false)
run_job
user.reload.trust_level.should == TrustLevel.levels[:regular]
end
it "doesn't demote if user was promoted recently" do
user = nil
Timecop.freeze(1.day.ago) do
user = create_leader_user
end
LeaderRequirements.any_instance.stubs(:requirements_met?).returns(false)
run_job
user.reload.trust_level.should == TrustLevel.levels[:leader]
end
end end
end end