diff --git a/app/models/post_action.rb b/app/models/post_action.rb index eb72e7678b7..dbd70dd961e 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -320,7 +320,16 @@ class PostAction < ActiveRecord::Base %w(like flag bookmark).each do |type| if send("is_#{type}?") - @rate_limiter = RateLimiter.new(user, "create_#{type}", SiteSetting.send("max_#{type}s_per_day"), 1.day.to_i) + limit = SiteSetting.send("max_#{type}s_per_day") + + if is_like? && user && user.trust_level >= 2 + multiplier = SiteSetting.send("tl#{user.trust_level}_additional_likes_per_day_multiplier").to_f + multiplier = 1.0 if multiplier < 1.0 + + limit = (limit * multiplier ).to_i + end + + @rate_limiter = RateLimiter.new(user, "create_#{type}",limit, 1.day.to_i) return @rate_limiter end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index ef09cf7418b..5a172035731 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -808,6 +808,10 @@ en: max_topics_in_first_day: "The maximum number of topics a user is allowed to create in their first day on the site" max_replies_in_first_day: "The maximum number of replies a user is allowed to create in their first day on the site" + 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" + num_flags_to_block_new_user: "If a new user's posts get this many spam flags from num_users_to_block_new_user different users, hide all their posts and prevent future posting. 0 to disable." num_users_to_block_new_user: "If a new user's posts get num_flags_to_block_new_user spam flags from this many different users, hide all their posts and prevent future posting. 0 to disable." notify_mods_when_user_blocked: "If a user is automatically blocked, send a message to all moderators." diff --git a/config/site_settings.yml b/config/site_settings.yml index aae5ac2b391..3520141c6da 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -675,6 +675,9 @@ rate_limits: max_invites_per_day: 10 max_topics_in_first_day: 5 max_replies_in_first_day: 10 + tl2_additional_likes_per_day_multiplier: 1.5 + tl3_additional_likes_per_day_multiplier: 2 + tl4_additional_likes_per_day_multiplier: 3 developer: force_hostname: diff --git a/lib/rate_limiter.rb b/lib/rate_limiter.rb index b3e544912f8..e8b51f0f499 100644 --- a/lib/rate_limiter.rb +++ b/lib/rate_limiter.rb @@ -4,6 +4,8 @@ require_dependency 'rate_limiter/on_create_record' # A redis backed rate limiter. class RateLimiter + attr_reader :max, :secs, :user, :key + def self.key_prefix "l-rate-limit:" end diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index bb364def59e..2ea72a5dbe8 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -506,6 +506,33 @@ describe PostAction do topic.reload expect(topic.posts.count).to eq(1) end + end + + describe "rate limiting" do + + def limiter(tl) + user = Fabricate.build(:user) + user.trust_level = tl + action = PostAction.new(user: user, post_action_type_id: PostActionType.types[:like]) + action.post_action_rate_limiter + end + + it "should scale up like limits depending on liker" do + expect(limiter(0).max).to eq SiteSetting.max_likes_per_day + expect(limiter(1).max).to eq SiteSetting.max_likes_per_day + expect(limiter(2).max).to eq (SiteSetting.max_likes_per_day * SiteSetting.tl2_additional_likes_per_day_multiplier).to_i + expect(limiter(3).max).to eq (SiteSetting.max_likes_per_day * SiteSetting.tl3_additional_likes_per_day_multiplier).to_i + expect(limiter(4).max).to eq (SiteSetting.max_likes_per_day * SiteSetting.tl4_additional_likes_per_day_multiplier).to_i + + SiteSetting.tl2_additional_likes_per_day_multiplier = -1 + expect(limiter(2).max).to eq SiteSetting.max_likes_per_day + + SiteSetting.tl2_additional_likes_per_day_multiplier = 0.8 + expect(limiter(2).max).to eq SiteSetting.max_likes_per_day + + SiteSetting.tl2_additional_likes_per_day_multiplier = "bob" + expect(limiter(2).max).to eq SiteSetting.max_likes_per_day + end end