FEATURE: Increase daily edit limits proportionally to trust level (#13090)

This commit is contained in:
Andrei Prigorshnev 2021-05-19 13:57:21 +04:00 committed by GitHub
parent 98736e15f0
commit 3e0f72f57f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 1 deletions

View File

@ -1582,6 +1582,10 @@ en:
tl2_additional_likes_per_day_multiplier: "Increase limit of likes per day for tl2 (member) by multiplying with this number"
tl3_additional_likes_per_day_multiplier: "Increase limit of likes per day for tl3 (regular) by multiplying with this number"
tl4_additional_likes_per_day_multiplier: "Increase limit of likes per day for tl4 (leader) by multiplying with this number"
tl2_additional_edits_per_day_multiplier: "Increase limit of edits per day for tl2 (member) by multiplying with this number"
tl3_additional_edits_per_day_multiplier: "Increase limit of edits per day for tl3 (regular) by multiplying with this number"
tl4_additional_edits_per_day_multiplier: "Increase limit of edits per day for tl4 (leader) by multiplying with this number"
num_users_to_silence_new_user: "If a new user's posts get num_spam_flags_to_silence_new_user spam flags from this many different users, hide all their posts and prevent future posting. 0 to disable."
num_tl3_flags_to_silence_new_user: "If a new user's posts get this many flags from num_tl3_users_to_silence_new_user different trust level 3 users, hide all their posts and prevent future posting. 0 to disable."
num_tl3_users_to_silence_new_user: "If a new user's posts get num_tl3_flags_to_silence_new_user flags from this many different trust level 3 users, hide all their posts and prevent future posting. 0 to disable."

View File

@ -1746,6 +1746,9 @@ rate_limits:
tl2_additional_likes_per_day_multiplier: 1.5
tl3_additional_likes_per_day_multiplier: 2
tl4_additional_likes_per_day_multiplier: 3
tl2_additional_edits_per_day_multiplier: 1.5
tl3_additional_edits_per_day_multiplier: 2
tl4_additional_edits_per_day_multiplier: 3
alert_admins_if_errors_per_minute:
client: true
default: 0

View File

@ -3,7 +3,15 @@
require 'rate_limiter'
class EditRateLimiter < RateLimiter
def initialize(user)
super(user, "edit-post", SiteSetting.max_edits_per_day, 1.day.to_i)
limit = SiteSetting.max_edits_per_day
if user.trust_level >= 2
multiplier = SiteSetting.get("tl#{user.trust_level}_additional_edits_per_day_multiplier").to_f
multiplier = 1.0 if multiplier < 1.0
limit = (limit * multiplier).to_i
end
super(user, "edit-post", limit, 1.day.to_i)
end
def build_key(type)

View File

@ -485,10 +485,58 @@ describe PostRevisor do
describe 'rate limiter' do
fab!(:changed_by) { Fabricate(:coding_horror) }
before do
RateLimiter.enable
RateLimiter.clear_all!
SiteSetting.editing_grace_period = 0
end
it "triggers a rate limiter" do
EditRateLimiter.any_instance.expects(:performed!)
subject.revise!(changed_by, raw: 'updated body')
end
it "raises error when a user gets rate limited" do
SiteSetting.max_edits_per_day = 1
user = Fabricate(:user, trust_level: 1)
subject.revise!(user, raw: 'body (edited)')
expect do
subject.revise!(user, raw: 'body (edited twice) ')
end.to raise_error(RateLimiter::LimitExceeded)
end
it "edit limits scale up depending on user's trust level" do
SiteSetting.max_edits_per_day = 1
SiteSetting.tl2_additional_edits_per_day_multiplier = 2
SiteSetting.tl3_additional_edits_per_day_multiplier = 3
SiteSetting.tl4_additional_edits_per_day_multiplier = 4
user = Fabricate(:user, trust_level: 2)
expect { subject.revise!(user, raw: 'body (edited)') }.to_not raise_error
expect { subject.revise!(user, raw: 'body (edited twice)') }.to_not raise_error
expect do
subject.revise!(user, raw: 'body (edited three times) ')
end.to raise_error(RateLimiter::LimitExceeded)
user = Fabricate(:user, trust_level: 3)
expect { subject.revise!(user, raw: 'body (edited)') }.to_not raise_error
expect { subject.revise!(user, raw: 'body (edited twice)') }.to_not raise_error
expect { subject.revise!(user, raw: 'body (edited three times)') }.to_not raise_error
expect do
subject.revise!(user, raw: 'body (edited four times) ')
end.to raise_error(RateLimiter::LimitExceeded)
user = Fabricate(:user, trust_level: 4)
expect { subject.revise!(user, raw: 'body (edited)') }.to_not raise_error
expect { subject.revise!(user, raw: 'body (edited twice)') }.to_not raise_error
expect { subject.revise!(user, raw: 'body (edited three times)') }.to_not raise_error
expect { subject.revise!(user, raw: 'body (edited four times)') }.to_not raise_error
expect do
subject.revise!(user, raw: 'body (edited five times) ')
end.to raise_error(RateLimiter::LimitExceeded)
end
end
describe "admin editing a new user's post" do