0
0
Fork 0

Add specific rate limits for posting and following (#13172)

This commit is contained in:
Eugen Rochko 2020-03-08 15:17:39 +01:00 committed by GitHub
parent 503eab1c1f
commit 339ce1c4e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 273 additions and 51 deletions

View file

@ -0,0 +1,36 @@
# frozen_string_literal: true
module RateLimitable
extend ActiveSupport::Concern
def rate_limit=(value)
@rate_limit = value
end
def rate_limit?
@rate_limit
end
def rate_limiter(by, options = {})
return @rate_limiter if defined?(@rate_limiter)
@rate_limiter = RateLimiter.new(by, options)
end
class_methods do
def rate_limit(options = {})
after_create do
by = public_send(options[:by])
if rate_limit? && by&.local?
rate_limiter(by, options).record!
@rate_limit_recorded = true
end
end
after_rollback do
rate_limiter(public_send(options[:by]), options).rollback! if @rate_limit_recorded
end
end
end
end