discourse/lib/rate_limiter/on_create_record.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
2.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2013-02-05 14:16:51 -05:00
class RateLimiter
# A mixin we can use on ActiveRecord Models to automatically rate limit them
# based on a SiteSetting.
#
# It expects a SiteSetting called `rate_limit_create_{model_name}` where
2013-02-25 11:42:20 -05:00
# `model_name` is the class name of your model, underscored.
2013-02-05 14:16:51 -05:00
#
module OnCreateRecord
# Over write to define your own rate limiter
def default_rate_limiter
return @rate_limiter if @rate_limiter.present?
limit_key = "create_#{self.class.name.underscore}"
max_setting =
if user && user.new_user? && SiteSetting.has_setting?("rate_limit_new_user_#{limit_key}")
SiteSetting.get("rate_limit_new_user_#{limit_key}")
else
SiteSetting.get("rate_limit_#{limit_key}")
end
2013-02-25 11:42:20 -05:00
@rate_limiter = RateLimiter.new(user, limit_key, 1, max_setting)
2013-02-05 14:16:51 -05:00
end
def self.included(base)
2013-02-25 11:42:20 -05:00
base.extend(ClassMethods)
end
2015-04-21 13:35:00 -04:00
# For the lifetime of this instance, don't enforce rate limits.
def disable_rate_limits!
@rate_limits_disabled = true
2013-02-05 14:16:51 -05:00
end
module ClassMethods
def rate_limit(limiter_method = nil)
limiter_method = limiter_method || :default_rate_limiter
self.after_create do |*args|
next if @rate_limits_disabled
if rate_limiter = public_send(limiter_method)
2013-12-13 01:04:45 -05:00
rate_limiter.performed!
@performed ||= {}
@performed[limiter_method] = true
end
2013-02-05 14:16:51 -05:00
end
self.after_destroy do
next if @rate_limits_disabled
if rate_limiter = public_send(limiter_method)
2013-12-13 01:04:45 -05:00
rate_limiter.rollback!
end
2013-02-05 14:16:51 -05:00
end
self.after_rollback do
next if @rate_limits_disabled
if rate_limiter = public_send(limiter_method)
2013-12-13 01:04:45 -05:00
if @performed.present? && @performed[limiter_method]
rate_limiter.rollback!
@performed[limiter_method] = false
end
2013-02-05 14:16:51 -05:00
end
end
end
end
end
2013-02-25 11:42:20 -05:00
end