* Add SiteConfig.feed_pinned_article and validation * Display pinned article at the top of feed * Add (basic) functionality to pin/unpin post * Admins can pin other users posts as well * Hide the button if looking at the non pinned post * Add pinned/unpinned snackbar message * Rename SiteConfig usage to Settings::General * Add pinned article to the Admin articles index * Show the pin post button when there's no pinned article * Move pinning to a separate controller * Fix SiteConfig reference * Hide PinController actions to unauthorized users * PinnedArticlesController#show action and refactor some of the code * Add Modal interaction * Fix modal-pinned checkbox interaction * Fixed pin/unpin post * Add ArticleDecorator#pinned? specs * Add PinnedArticlePolicy and PinnedArticlesController specs * Add ability to actually pin an article from the admin after submit * Add partial Cypress pin/unpin spec * Fix pinned article and add basic Cypress interaction tests * Add Crayons styling to modal * Only render the pinned article on the default Feed page * Use persisted? * Add some comments * Update app/javascript/articles/Article.jsx Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Update app/javascript/packs/homePageFeed.jsx Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Fix Cypress tests * Update app/javascript/admin/controllers/article_controller.js Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com> * Fix pinning in article show page * Used PinnedArticle domain model * Fix spec * Update cypress/integration/adminFlows/articles/pinArticle.spec.js Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Update cypress/integration/adminFlows/articles/pinArticle.spec.js Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Update cypress/integration/adminFlows/articles/pinArticle.spec.js Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Update cypress/integration/adminFlows/articles/pinArticle.spec.js Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Update cypress/integration/adminFlows/articles/pinArticle.spec.js Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Update cypress/integration/articleFlows/pinArticle.spec.js Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Update cypress/integration/articleFlows/pinArticle.spec.js Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Update app/views/admin/articles/index.html.erb Co-authored-by: Suzanne Aitchison <suzanne@forem.com> * Fix merge woes * Add missing article pin post flows * Add missing admin article flows * Add Unpin to Admin as well * Add Audit::Log entries for pin/unpin actions * Update app/controllers/stories/feeds_controller.rb Co-authored-by: Michael Kohl <citizen428@dev.to> * Do not rate limit in E2E tests * Use .find instead of .filter * Rename ArticleIdValidator to ExistingArticleIdValidator * Treat draft and deleted articles the same * Make sure posts can be pinned after the pinned article is unpublished or deleted * Use .get directly * Fix spec and fix PinnedArticlesController#show * Strengthen pinArticle Cypress tests * Add Cypress test heading guard * Add another Cypress test heading guard * Remove duplicate validator * Try using the Tools: header instead of the article title Co-authored-by: Suzanne Aitchison <suzanne@forem.com> Co-authored-by: Ridhwana <Ridhwana.Khan16@gmail.com> Co-authored-by: Michael Kohl <citizen428@dev.to>
125 lines
3.9 KiB
Ruby
125 lines
3.9 KiB
Ruby
class RateLimitChecker
|
|
attr_reader :user, :action
|
|
|
|
# retry_after values are the seconds until a user can retry an action
|
|
ACTION_LIMITERS = {
|
|
article_update: { retry_after: 30 },
|
|
feedback_message_creation: { retry_after: 300 },
|
|
image_upload: { retry_after: 30 },
|
|
listing_creation: { retry_after: 60 },
|
|
organization_creation: { retry_after: 300 },
|
|
published_article_creation: { retry_after: 30 },
|
|
published_article_antispam_creation: { retry_after: 300 },
|
|
reaction_creation: { retry_after: 30 },
|
|
send_email_confirmation: { retry_after: 120 },
|
|
user_subscription_creation: { retry_after: 30 },
|
|
user_update: { retry_after: 30 },
|
|
comment_antispam_creation: { retry_after: 300 }
|
|
}.with_indifferent_access.freeze
|
|
|
|
def initialize(user = nil)
|
|
@user = user
|
|
end
|
|
|
|
class LimitReached < StandardError
|
|
attr_reader :retry_after
|
|
|
|
def initialize(retry_after) # rubocop:disable Lint/MissingSuper
|
|
@retry_after = retry_after
|
|
end
|
|
|
|
def message
|
|
"Rate limit reached, try again in #{retry_after} seconds"
|
|
end
|
|
end
|
|
|
|
def check_limit!(action)
|
|
return if ApplicationConfig["E2E"]
|
|
return unless limit_by_action(action)
|
|
|
|
retry_after = ACTION_LIMITERS.dig(action, :retry_after)
|
|
raise LimitReached, retry_after
|
|
end
|
|
|
|
def limit_by_action(action)
|
|
return false if ApplicationConfig["E2E"]
|
|
|
|
check_method = "check_#{action}_limit"
|
|
result = respond_to?(check_method, true) ? __send__(check_method) : false
|
|
|
|
if result
|
|
@action = action
|
|
log_to_datadog
|
|
end
|
|
result
|
|
end
|
|
|
|
def track_limit_by_action(action)
|
|
expires_in = ACTION_LIMITERS.dig(action, :retry_after).seconds
|
|
Rails.cache.increment(limit_cache_key(action), 1, expires_in: expires_in, raw: true)
|
|
end
|
|
|
|
def limit_by_email_recipient_address(address)
|
|
# This is related to the recipient, not the "user" initiator, like in action.
|
|
EmailMessage.where(to: address).where("sent_at > ?", 2.minutes.ago).size >
|
|
Settings::RateLimit.email_recipient
|
|
end
|
|
|
|
private
|
|
|
|
ACTION_LIMITERS.each_key do |action|
|
|
define_method("check_#{action}_limit") do
|
|
Rails.cache.read(limit_cache_key(action), raw: true).to_i > action_rate_limit(action)
|
|
end
|
|
end
|
|
|
|
def limit_cache_key(action)
|
|
unique_key_component = @user&.id || @user&.ip_address
|
|
raise "Invalid Cache Key: no unique component present" if unique_key_component.blank?
|
|
|
|
"#{unique_key_component}_#{action}"
|
|
end
|
|
|
|
def action_rate_limit(action)
|
|
Settings::RateLimit.public_send(action)
|
|
end
|
|
|
|
def check_comment_creation_limit
|
|
user.comments.where("created_at > ?", 30.seconds.ago).size >
|
|
Settings::RateLimit.comment_creation
|
|
end
|
|
|
|
def check_published_article_creation_limit
|
|
# TODO: We should make this time frame configurable.
|
|
user.articles.published.where("created_at > ?", 30.seconds.ago).size >
|
|
Settings::RateLimit.published_article_creation
|
|
end
|
|
|
|
def check_published_article_antispam_creation_limit
|
|
# TODO: We should make this time frame configurable.
|
|
user.articles.published.where("created_at > ?", 5.minutes.ago).size >
|
|
Settings::RateLimit.published_article_antispam_creation
|
|
end
|
|
|
|
def check_comment_antispam_creation_limit
|
|
# TODO: We should make this time frame configurable.
|
|
user.comments.where(created_at: 5.minutes.ago...).size >
|
|
Settings::RateLimit.comment_antispam_creation
|
|
end
|
|
|
|
def check_follow_account_limit
|
|
user_today_follow_count > Settings::RateLimit.follow_count_daily
|
|
end
|
|
|
|
def user_today_follow_count
|
|
following_users_count = user.following_users_count
|
|
return following_users_count if following_users_count < Settings::RateLimit.follow_count_daily
|
|
|
|
now = Time.zone.now
|
|
user.follows.where(created_at: (now.beginning_of_day..now)).size
|
|
end
|
|
|
|
def log_to_datadog
|
|
ForemStatsClient.increment("rate_limit.limit_reached", tags: ["user:#{user.id}", "action:#{action}"])
|
|
end
|
|
end
|