mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Initial release of Discourse
This commit is contained in:
11
lib/rate_limiter/limit_exceeded.rb
Normal file
11
lib/rate_limiter/limit_exceeded.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class RateLimiter
|
||||
|
||||
# A rate limit has been exceeded.
|
||||
class LimitExceeded < Exception
|
||||
attr_accessor :available_in
|
||||
def initialize(available_in)
|
||||
@available_in = available_in
|
||||
end
|
||||
end
|
||||
|
||||
end
|
61
lib/rate_limiter/on_create_record.rb
Normal file
61
lib/rate_limiter/on_create_record.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
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
|
||||
# `model_name` is the class name of your model, underscored.
|
||||
#
|
||||
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 = SiteSetting.send("rate_limit_#{limit_key}")
|
||||
@rate_limiter = RateLimiter.new(user, limit_key, 1, max_setting)
|
||||
end
|
||||
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def rate_limit(limiter_method=nil)
|
||||
|
||||
limiter_method = limiter_method || :default_rate_limiter
|
||||
|
||||
self.after_create do
|
||||
|
||||
rate_limiter = send(limiter_method)
|
||||
return unless rate_limiter.present?
|
||||
|
||||
rate_limiter.performed!
|
||||
@performed ||= {}
|
||||
@performed[limiter_method] = true
|
||||
end
|
||||
|
||||
self.after_destroy do
|
||||
rate_limiter = send(limiter_method)
|
||||
return unless rate_limiter.present?
|
||||
|
||||
rate_limiter.rollback!
|
||||
end
|
||||
|
||||
self.after_rollback do
|
||||
rate_limiter = send(limiter_method)
|
||||
return unless rate_limiter.present?
|
||||
|
||||
if @performed.present? and @performed[limiter_method]
|
||||
rate_limiter.rollback!
|
||||
@performed[limiter_method] = false
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user