diff --git a/app/models/trust_level3_requirements.rb b/app/models/trust_level3_requirements.rb index 3092259aaf1..d40283dbf86 100644 --- a/app/models/trust_level3_requirements.rb +++ b/app/models/trust_level3_requirements.rb @@ -104,18 +104,33 @@ class TrustLevel3Requirements def penalty_counts args = { user_id: @user.id, + system_user_id: Discourse.system_user.id, silence_user: UserHistory.actions[:silence_user], + unsilence_user: UserHistory.actions[:unsilence_user], suspend_user: UserHistory.actions[:suspend_user], + unsuspend_user: UserHistory.actions[:unsuspend_user], since: FORGIVENESS_PERIOD.ago } sql = <<~SQL SELECT - SUM(CASE WHEN action = :silence_user THEN 1 ELSE 0 END) AS silence_count, - SUM(CASE WHEN action = :suspend_user THEN 1 ELSE 0 END) AS suspend_count + SUM( + CASE + WHEN action = :silence_user THEN 1 + WHEN action = :unsilence_user AND acting_user_id != :system_user_id THEN -1 + ELSE 0 + END + ) AS silence_count, + SUM( + CASE + WHEN action = :suspend_user THEN 1 + WHEN action = :unsuspend_user AND acting_user_id != :system_user_id THEN -1 + ELSE 0 + END + ) AS suspend_count FROM user_histories AS uh WHERE uh.target_user_id = :user_id - AND uh.action IN (:silence_user, :suspend_user) + AND uh.action IN (:silence_user, :suspend_user, :unsilence_user, :unsuspend_user) AND uh.created_at > :since SQL diff --git a/spec/models/trust_level3_requirements_spec.rb b/spec/models/trust_level3_requirements_spec.rb index bf48291c396..ede04a1c7b2 100644 --- a/spec/models/trust_level3_requirements_spec.rb +++ b/spec/models/trust_level3_requirements_spec.rb @@ -31,6 +31,17 @@ describe TrustLevel3Requirements do expect(tl3_requirements.penalty_counts.silenced).to eq(1) expect(tl3_requirements.penalty_counts.total).to eq(1) UserSilencer.new(user, moderator).unsilence + expect(tl3_requirements.penalty_counts.silenced).to eq(0) + expect(tl3_requirements.penalty_counts.total).to eq(0) + end + + it "ignores system user unsilences" do + expect(tl3_requirements.penalty_counts.silenced).to eq(0) + expect(tl3_requirements.penalty_counts.total).to eq(0) + UserSilencer.new(user, moderator).silence + expect(tl3_requirements.penalty_counts.silenced).to eq(1) + expect(tl3_requirements.penalty_counts.total).to eq(1) + UserSilencer.new(user, Discourse.system_user).unsilence expect(tl3_requirements.penalty_counts.silenced).to eq(1) expect(tl3_requirements.penalty_counts.total).to eq(1) end @@ -43,6 +54,7 @@ describe TrustLevel3Requirements do UserHistory.create!( target_user_id: user.id, + acting_user_id: moderator.id, action: UserHistory.actions[:suspend_user] ) @@ -51,6 +63,32 @@ describe TrustLevel3Requirements do UserHistory.create!( target_user_id: user.id, + acting_user_id: moderator.id, + action: UserHistory.actions[:unsuspend_user] + ) + + expect(tl3_requirements.penalty_counts.suspended).to eq(0) + expect(tl3_requirements.penalty_counts.total).to eq(0) + end + + it "ignores system user un-suspend" do + user.save! + + expect(tl3_requirements.penalty_counts.suspended).to eq(0) + expect(tl3_requirements.penalty_counts.total).to eq(0) + + UserHistory.create!( + target_user_id: user.id, + acting_user_id: Discourse.system_user.id, + action: UserHistory.actions[:suspend_user] + ) + + expect(tl3_requirements.penalty_counts.suspended).to eq(1) + expect(tl3_requirements.penalty_counts.total).to eq(1) + + UserHistory.create!( + target_user_id: user.id, + acting_user_id: Discourse.system_user.id, action: UserHistory.actions[:unsuspend_user] )