Make definition of new user configurable (#12999)

* Make definition of new user configurable

* Add new user configuration to config admin

* Only decorate on-demand

* Adapt new user logic

* Update app/lib/constants/site_config.rb

Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>

Co-authored-by: Vaidehi Joshi <vaidehi.sj@gmail.com>
This commit is contained in:
Michael Kohl 2021-03-23 07:13:06 +00:00 committed by GitHub
parent 086939e7c1
commit 2c1e8443e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -1,4 +1,12 @@
module RateLimitCheckerHelper
NEW_USER_MESSAGE = "How many %<thing>s can a new member (%<timeframe>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,

View file

@ -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: ""

View file

@ -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,

View file

@ -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

View file

@ -1092,6 +1092,15 @@
} %>
<div id="rateLimitsBodyContainer" class="card-body collapse hide" aria-labelledby="rateLimitsBodyContainer">
<fieldset class="grid gap-4">
<div class="crayons-field">
<%= 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] %>
</div>
<% configurable_rate_limits.each do |key, field_hash| %>
<div class="crayons-field">
<%= admin_config_label field_hash[:title] %>

View file

@ -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