FEATURE: allow TL3 promotions for overturned penalties

Restore tl3 reachability by calculating penalty counts vs unsuspend/unsilence.

Only count unsuspend or unsilences that have been made by a user other than
the Discourse system user.
This commit is contained in:
Jeff Wong 2019-12-20 15:22:53 -08:00
parent 00477d8f88
commit eca1c643d5
2 changed files with 56 additions and 3 deletions

View File

@ -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

View File

@ -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]
)