Add RateLimitCheckerHelper (#7705)

* Implement review comments

* Add RateLimitCheckerHelper
This commit is contained in:
Michael Kohl 2020-05-07 21:25:48 +07:00 committed by GitHub
parent 6979209cd2
commit d2a929017d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 12 deletions

View file

@ -0,0 +1,48 @@
module RateLimitCheckerHelper
CONFIGURABLE_RATES = {
rate_limit_follow_count_daily: {
min: 0,
placeholder: 500,
description: "The number of users a person can follow daily"
},
rate_limit_comment_creation: {
min: 0,
placeholder: 9,
description: "The number of comments a user can create within 30 seconds"
},
rate_limit_listing_creation: {
min: 1,
placeholder: 1,
description: "The number of listings a user can create in 1 minute"
},
rate_limit_published_article_creation: {
min: 0,
placeholder: 9,
description: "The number of articles a user can create within 30 seconds"
},
rate_limit_image_upload: {
min: 0,
placeholder: 9,
description: "The number of images a user can upload within 30 seconds"
},
rate_limit_email_recipient: {
min: 0,
placeholder: 5,
description: "The number of emails we send to a user within 2 minutes"
},
rate_limit_organization_creation: {
min: 1,
placeholder: 1,
description: "The number of organizations a user can create within a 5 minute period"
},
rate_limit_reaction_creation: {
min: 1,
placeholder: 10,
description: "The number of times a user can react in a 30 second period"
}
}.freeze
def configurable_rate_limits
CONFIGURABLE_RATES
end
end

View file

@ -11,17 +11,6 @@ class RateLimitChecker
reaction_creation: { retry_after: 30 }
}.with_indifferent_access.freeze
CONFIGURABLE_RATES = {
rate_limit_follow_count_daily: { min: 0, placeholder: 500, description: "The number of users a person can follow daily" },
rate_limit_comment_creation: { min: 0, placeholder: 9, description: "The number of comments a user can create within 30 seconds" },
rate_limit_listing_creation: { min: 1, placeholder: 1, description: "The number of listings a user can create in 1 minute" },
rate_limit_published_article_creation: { min: 0, placeholder: 9, description: "The number of articles a user can create within 30 seconds" },
rate_limit_image_upload: { min: 0, placeholder: 9, description: "The number of images a user can upload within 30 seconds" },
rate_limit_email_recipient: { min: 0, placeholder: 5, description: "The number of emails we send to a user within 2 minutes" },
rate_limit_organization_creation: { min: 1, placeholder: 1, description: "The number of organizations a user can create within a 5 minute period" },
rate_limit_reaction_creation: { min: 1, placeholder: 10, description: "The number of times a user can react in a 30 second period" }
}.freeze
def initialize(user = nil)
@user = user
end

View file

@ -396,7 +396,7 @@
expanded: "false"
} %>
<div id="rateLimitsBodyContainer" class="card-body collapse hide" aria-labelledby="rateLimitsBodyContainer">
<% RateLimitChecker::CONFIGURABLE_RATES.each do |key, field_hash| %>
<% configurable_rate_limits.each do |key, field_hash| %>
<div class="form-group">
<%= f.label key %>
<%= f.number_field key,

View file

@ -0,0 +1,12 @@
require "rails_helper"
describe RateLimitCheckerHelper, type: :helper do
describe "#configurable_rate_limits" do
it "returns a hash with the right structure" do
helper.configurable_rate_limits.each do |key, value_hash|
expect(key).to match(/\Arate_limit/)
expect(value_hash.keys).to match_array(%i[min placeholder description])
end
end
end
end