From 1d2ecc64f002b515265b085598a5f19cd23fd289 Mon Sep 17 00:00:00 2001 From: Jeremy Friesen Date: Fri, 8 Apr 2022 11:59:23 -0400 Subject: [PATCH] Removing Listing rate limiting when feature disabled (#17181) * Removing Listing rate limiting when feature disabled Closes forem/forem#17175 * Fixing tests --- app/helpers/rate_limit_checker_helper.rb | 18 ++++++++++++++++++ .../admin/settings/forms/_rate_limit.html.erb | 1 + spec/helpers/rate_limit_checker_helper_spec.rb | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/helpers/rate_limit_checker_helper.rb b/app/helpers/rate_limit_checker_helper.rb index 6bc62a309..f3e50f5a6 100644 --- a/app/helpers/rate_limit_checker_helper.rb +++ b/app/helpers/rate_limit_checker_helper.rb @@ -4,99 +4,117 @@ module RateLimitCheckerHelper I18n.t("helpers.rate_limit_checker_helper.general", thing: thing, timeframe: timeframe) end + # @return [Hash>] Each element of the returning hash *must* have the + # following keys: `:min`, `:placeholder`, `:title`, `:description`, and `:enabled`. def configurable_rate_limits { published_article_creation: { + enabled: true, min: 0, placeholder: 9, title: I18n.t("helpers.rate_limit_checker_helper.published.title"), description: I18n.t("helpers.rate_limit_checker_helper.published.description") }, published_article_antispam_creation: { + enabled: true, min: 0, placeholder: 1, title: I18n.t("helpers.rate_limit_checker_helper.antispam.title"), description: RateLimitCheckerHelper.new_user_message(I18n.t("helpers.rate_limit_checker_helper.thing.posts")) }, article_update: { + enabled: true, min: 1, placeholder: 30, title: I18n.t("helpers.rate_limit_checker_helper.update.title"), description: I18n.t("helpers.rate_limit_checker_helper.update.description") }, image_upload: { + enabled: true, min: 0, placeholder: 9, title: I18n.t("helpers.rate_limit_checker_helper.upload.title"), description: I18n.t("helpers.rate_limit_checker_helper.upload.description") }, user_update: { + enabled: true, min: 1, placeholder: 5, title: I18n.t("helpers.rate_limit_checker_helper.user.title"), description: I18n.t("helpers.rate_limit_checker_helper.user.description") }, follow_count_daily: { + enabled: true, min: 0, placeholder: 500, title: I18n.t("helpers.rate_limit_checker_helper.follow.title"), description: I18n.t("helpers.rate_limit_checker_helper.follow.description") }, reaction_creation: { + enabled: true, min: 1, placeholder: 10, title: I18n.t("helpers.rate_limit_checker_helper.reaction.title"), description: I18n.t("helpers.rate_limit_checker_helper.reaction.description") }, feedback_message_creation: { + enabled: true, min: 1, placeholder: 5, title: I18n.t("helpers.rate_limit_checker_helper.feedback.title"), description: I18n.t("helpers.rate_limit_checker_helper.feedback.description") }, comment_creation: { + enabled: true, min: 0, placeholder: 9, title: I18n.t("helpers.rate_limit_checker_helper.comment.title"), description: I18n.t("helpers.rate_limit_checker_helper.comment.description") }, comment_antispam_creation: { + enabled: true, min: 0, placeholder: 1, title: I18n.t("helpers.rate_limit_checker_helper.comment_antispam.title"), description: RateLimitCheckerHelper.new_user_message(I18n.t("helpers.rate_limit_checker_helper.thing.comments")) }, mention_creation: { + enabled: true, min: 0, placeholder: 7, title: I18n.t("helpers.rate_limit_checker_helper.mention.title"), description: I18n.t("helpers.rate_limit_checker_helper.mention.description") }, listing_creation: { + enabled: Listing.feature_enabled?, min: 1, placeholder: 1, title: I18n.t("helpers.rate_limit_checker_helper.listing.title"), description: I18n.t("helpers.rate_limit_checker_helper.listing.description") }, organization_creation: { + enabled: true, min: 1, placeholder: 1, title: I18n.t("helpers.rate_limit_checker_helper.organization.title"), description: I18n.t("helpers.rate_limit_checker_helper.organization.description") }, user_subscription_creation: { + enabled: true, min: 0, placeholder: 3, title: I18n.t("helpers.rate_limit_checker_helper.subscription.title"), description: I18n.t("helpers.rate_limit_checker_helper.subscription.description") }, email_recipient: { + enabled: true, min: 0, placeholder: 5, title: I18n.t("helpers.rate_limit_checker_helper.recipient.title"), description: I18n.t("helpers.rate_limit_checker_helper.recipient.description") }, send_email_confirmation: { + enabled: true, min: 1, placeholder: 2, title: I18n.t("helpers.rate_limit_checker_helper.confirmation.title"), diff --git a/app/views/admin/settings/forms/_rate_limit.html.erb b/app/views/admin/settings/forms/_rate_limit.html.erb index 35adefefa..d17f733d9 100644 --- a/app/views/admin/settings/forms/_rate_limit.html.erb +++ b/app/views/admin/settings/forms/_rate_limit.html.erb @@ -15,6 +15,7 @@ <% configurable_rate_limits.each do |key, field_hash| %> + <% next unless field_hash[:enabled] %>
<%= admin_config_label field_hash[:title], model: Settings::RateLimit %>

diff --git a/spec/helpers/rate_limit_checker_helper_spec.rb b/spec/helpers/rate_limit_checker_helper_spec.rb index ae950ebd3..13a48c67b 100644 --- a/spec/helpers/rate_limit_checker_helper_spec.rb +++ b/spec/helpers/rate_limit_checker_helper_spec.rb @@ -6,7 +6,7 @@ describe RateLimitCheckerHelper, type: :helper do settings_keys = Settings::RateLimit.keys.map(&:to_sym) helper.configurable_rate_limits.each do |key, value_hash| expect(settings_keys).to include(key) - expect(value_hash.keys).to match_array(%i[title min placeholder description]) + expect(value_hash.keys).to match_array(%i[title min placeholder description enabled]) end end end