docbrown/app/labor/rate_limit_checker.rb
rhymes 6553f08d94 Rubocop cleanups (#1415)
* Update rubocop-todo.yml with new violations

* Fix Layout/EmptyLine* rules

* Fix Layout/Indentation* rules

* Fix remaining Layout/* rules

* Fix Lint/DuplicateMethods by removing unused accessor

* Fix Lint/IneffectiveAccessModifier

* Fix Lint/MissingCopEnableDirective

* Re-run rubocop auto gen config

* Fix Layout/RescueEnsureAlignment

* Fix Naming/* rules

* Fix some RSpec/* rules

* Fix typos

* Fix RSpec/LetBeforeExamples

* Series should only be an attr_writer, not an attr_accessor

* Fix RSpec/InstanceVariable

* Fix RSpec/InstanceVariable

* Fix RSpec/RepeatedDescription and RSpec/RepeatedExample

* Fix Style/ClassAndModuleChildren

* Fix Style/ConditionalAssignment

* Fix some Style/* rules

* Trigger Travis CI build because failing tests are not failing locally

* Revert "Fix Style/ClassAndModuleChildren"

This reverts commit 1686801d8a1516ba1894f79e24401a20dea65f99.
2019-01-02 11:20:02 -05:00

39 lines
1.1 KiB
Ruby

class RateLimitChecker
attr_accessor :user
def initialize(user = nil)
@user = user
end
def limit_by_situation(situation)
result = case situation
when "comment_creation"
user.comments.where("created_at > ?", 30.seconds.ago).size > 9
when "published_article_creation"
user.articles.
where(published: true).
where("created_at > ?", 30.seconds.ago).size > 9
else
false
end
ping_admins if result == true
result
end
def limit_by_email_recipient_address(address)
# This is related to the recipient, not the "user" initiator, like in situation.
EmailMessage.where(to: address).
where("sent_at > ?", 2.minutes.ago).size > 5
end
def ping_admins
return unless user && Rails.env.production?
SlackBot.ping(
"Rate limit exceeded. https://dev.to#{user.path}",
channel: "abuse-reports",
username: "rate_limit",
icon_emoji: ":hand:",
)
end
handle_asynchronously :ping_admins
end