[deploy] Add rate limit for article updates (#7166)

This commit is contained in:
Andy Zhao 2020-04-09 09:30:21 -04:00 committed by GitHub
parent 23feb31f36
commit 2d56ec18ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View file

@ -21,9 +21,11 @@ class RateLimitChecker
end
def track_image_uploads
count = Rails.cache.read("#{@user.id}_image_upload").to_i
count += 1
Rails.cache.write("#{@user.id}_image_upload", count, expires_in: 30.seconds)
Rails.cache.increment("#{@user.id}_image_upload", count, expires_in: 30.seconds)
end
def track_article_updates
Rails.cache.increment("#{@user.id}_article_update", count, expires_in: 1.day)
end
def limit_by_email_recipient_address(address)
@ -49,6 +51,11 @@ class RateLimitChecker
SiteConfig.rate_limit_image_upload
end
def check_article_update_limit
Rails.cache.read("#{user.id}_article_update").to_i >
SiteConfig.rate_limit_article_update
end
def check_follow_account_limit
user_today_follow_count > SiteConfig.rate_limit_follow_count_daily
end

View file

@ -51,6 +51,7 @@ class SiteConfig < RailsSettings::Base
field :rate_limit_published_article_creation, type: :integer, default: 9
field :rate_limit_image_upload, type: :integer, default: 9
field :rate_limit_email_recipient, type: :integer, default: 5
field :rate_limit_article_update, type: :integer, default: 150
# Google Analytics Reporting API v4
# <https://developers.google.com/analytics/devguides/reporting/core/v4>

View file

@ -12,6 +12,9 @@ module Articles
end
def call
rate_limiter = RateLimitChecker.new(user)
raise if rate_limiter.limit_by_action("article_update")
article = load_article
was_published = article.published
@ -34,6 +37,7 @@ module Articles
article_params[:edited_at] = Time.current if update_edited_at
article.update!(article_params)
rate_limiter.track_article_updates
# send notification only the first time an article is published
send_notification = article.published && article.saved_change_to_published_at.present?