diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 3dc40255f..fec1cdf16 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -285,10 +285,10 @@ class CommentsController < ApplicationController end def rate_limit_to_use - if current_user.created_at.before?(3.days.ago.beginning_of_day) - :comment_creation - else + if current_user.decorate.considered_new? :comment_antispam_creation + else + :comment_creation end end end diff --git a/app/decorators/user_decorator.rb b/app/decorators/user_decorator.rb index cc60850d2..37040afb2 100644 --- a/app/decorators/user_decorator.rb +++ b/app/decorators/user_decorator.rb @@ -106,4 +106,11 @@ class UserDecorator < ApplicationDecorator def stackbit_integration? access_tokens.any? end + + def considered_new? + min_days = SiteConfig.user_considered_new_days + return false unless min_days.positive? + + created_at.after?(min_days.days.ago) + end end diff --git a/app/helpers/rate_limit_checker_helper.rb b/app/helpers/rate_limit_checker_helper.rb index 7edad726a..f027877f0 100644 --- a/app/helpers/rate_limit_checker_helper.rb +++ b/app/helpers/rate_limit_checker_helper.rb @@ -1,4 +1,12 @@ module RateLimitCheckerHelper + NEW_USER_MESSAGE = "How many %s can a new member (%s or less) " \ + "create within any 5 minute period?".freeze + + def self.new_user_message(thing) + timeframe = "day".pluralize(SiteConfig.user_considered_new_days) + format(NEW_USER_MESSAGE, thing: thing, timeframe: timeframe) + end + CONFIGURABLE_RATES = { rate_limit_published_article_creation: { min: 0, @@ -10,7 +18,7 @@ module RateLimitCheckerHelper min: 0, placeholder: 1, title: "Limit number of posts created by a new member", - description: "How many posts can a new member (3 days or less) create within any 5 minute period?" + description: new_user_message("posts") }, rate_limit_article_update: { min: 1, @@ -58,7 +66,7 @@ module RateLimitCheckerHelper min: 0, placeholder: 1, title: "Limit number of comments created by a new member", - description: "How many comments can a new member (3 days or less) create within any 5 minute period?" + description: new_user_message("comments") }, rate_limit_listing_creation: { min: 1, diff --git a/app/lib/constants/site_config.rb b/app/lib/constants/site_config.rb index 24e79328b..bd99ee250 100644 --- a/app/lib/constants/site_config.rb +++ b/app/lib/constants/site_config.rb @@ -339,6 +339,10 @@ module Constants description: "The \"API secret key\" portion of consumer keys in the Twitter developer portal.", placeholder: "" }, + user_considered_new_days: { + description: "The number of days a user is considered new. The default is 3 days, but you can disable this entirely by inputting 0.", + placeholder: ::SiteConfig.user_considered_new_days + }, video_encoder_key: { description: "Secret key used to allow AWS video encoding through the VideoStatesController", placeholder: "" diff --git a/app/models/site_config.rb b/app/models/site_config.rb index 6a167e562..a8b1e5160 100644 --- a/app/models/site_config.rb +++ b/app/models/site_config.rb @@ -186,6 +186,8 @@ class SiteConfig < RailsSettings::Base field :spam_trigger_terms, type: :array, default: [] + field :user_considered_new_days, type: :integer, default: 3 + # Social Media field :social_media_handles, type: :hash, default: { twitter: nil, diff --git a/app/services/articles/creator.rb b/app/services/articles/creator.rb index 7d5583a5f..ccfdb3d04 100644 --- a/app/services/articles/creator.rb +++ b/app/services/articles/creator.rb @@ -31,7 +31,7 @@ module Articles attr_reader :user, :article_params, :event_dispatcher def rate_limit! - rate_limit_to_use = if user.created_at > 3.days.ago.beginning_of_day + rate_limit_to_use = if user.decorate.considered_new? :published_article_antispam_creation else :published_article_creation diff --git a/app/views/admin/configs/show.html.erb b/app/views/admin/configs/show.html.erb index 86852ebc1..23f2d092c 100644 --- a/app/views/admin/configs/show.html.erb +++ b/app/views/admin/configs/show.html.erb @@ -1092,6 +1092,15 @@ } %>
+
+ <%= admin_config_label :user_considered_new_days %> + <%= admin_config_description Constants::SiteConfig::DETAILS[:user_considered_new_days][:description] %> + <%= f.number_field :user_considered_new_days, + class: "crayons-textfield", + value: SiteConfig.user_considered_new_days, + placeholder: Constants::SiteConfig::DETAILS[:user_considered_new_days][:placeholder] %> +
+ <% configurable_rate_limits.each do |key, field_hash| %>
<%= admin_config_label field_hash[:title] %> diff --git a/spec/decorators/user_decorator_spec.rb b/spec/decorators/user_decorator_spec.rb index 51705db77..29234fc71 100644 --- a/spec/decorators/user_decorator_spec.rb +++ b/spec/decorators/user_decorator_spec.rb @@ -228,4 +228,20 @@ RSpec.describe UserDecorator, type: :decorator do expect(user.decorate.stackbit_integration?).to be(true) end end + + describe "#considered_new?" do + before do + allow(SiteConfig).to receive(:user_considered_new_days).and_return(3) + end + + it "returns true for new users" do + user.created_at = 1.day.ago + expect(user.decorate.considered_new?).to be(true) + end + + it "returns false for new users" do + user.created_at = 1.year.ago + expect(user.decorate.considered_new?).to be(false) + end + end end