From 01ad88f1ed5869b0d41311f63269df825a01c588 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 4 Aug 2015 10:55:59 +1000 Subject: [PATCH] FEATURE: min_first_post_typing_time If a user spends less than 3 seconds typing first post they will automatically enter the approval queue --- app/controllers/posts_controller.rb | 4 +++- config/locales/server.en.yml | 1 + config/site_settings.yml | 1 + lib/new_post_manager.rb | 14 +++++++++++--- spec/controllers/posts_controller_spec.rb | 17 +++++++++++++++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index d594d3d719b..f6f34fc7ed8 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -89,6 +89,8 @@ class PostsController < ApplicationController def create @manager_params = create_params + @manager_params[:first_post_checks] = !is_api? + manager = NewPostManager.new(current_user, @manager_params) if is_api? @@ -353,7 +355,7 @@ class PostsController < ApplicationController # If a param is present it uses that result structure. def backwards_compatible_json(json_obj, success) json_obj.symbolize_keys! - if params[:nested_post].blank? && json_obj[:errors].blank? + if params[:nested_post].blank? && json_obj[:errors].blank? && json_obj[:action] != :enqueued json_obj = json_obj[:post] end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 7d59dc31890..232e939ae21 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1077,6 +1077,7 @@ en: num_flags_to_close_topic: "Minimum number of active flags that is required to automatically pause a topic for intervention" auto_respond_to_flag_actions: "Enable automatic reply when disposing a flag." + min_first_post_typing_time: "Minimum amount of time in milliseconds a user must type during first post, if threshold is not met post will automatically enter the needs approval queue. Set to 0 to disable (not recommended)" reply_by_email_enabled: "Enable replying to topics via email." reply_by_email_address: "Template for reply by email incoming email address, for example: %{reply_key}@reply.example.com or replies+%{reply_key}@example.com" diff --git a/config/site_settings.yml b/config/site_settings.yml index 9195ae7187b..9a575177875 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -680,6 +680,7 @@ spam: num_flaggers_to_close_topic: 5 num_flags_to_close_topic: 12 auto_respond_to_flag_actions: true + min_first_post_typing_time: 3000 rate_limits: unique_posts_mins: diff --git a/lib/new_post_manager.rb b/lib/new_post_manager.rb index 7c17135d020..472f5c8bc2f 100644 --- a/lib/new_post_manager.rb +++ b/lib/new_post_manager.rb @@ -28,15 +28,23 @@ class NewPostManager @sorted_handlers.sort_by! {|h| -h[:priority]} end - def self.user_needs_approval?(user) + def self.user_needs_approval?(manager) + user = manager.user + args = manager.args + return false if user.staff? (user.post_count < SiteSetting.approve_post_count) || - (user.trust_level < SiteSetting.approve_unless_trust_level.to_i) + (user.trust_level < SiteSetting.approve_unless_trust_level.to_i) || + ( + args[:first_post_checks] && + user.post_count == 0 && + args[:typing_duration_msecs].to_i < SiteSetting.min_first_post_typing_time + ) end def self.default_handler(manager) - manager.enqueue('default') if user_needs_approval?(manager.user) + manager.enqueue('default') if user_needs_approval?(manager) end def self.queue_enabled? diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index ce2948fd955..a9164e9fba0 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -446,6 +446,10 @@ describe PostsController do describe 'creating a post' do + before do + SiteSetting.min_first_post_typing_time = 0 + end + include_examples 'action requires login', :post, :create context 'api' do @@ -477,6 +481,19 @@ describe PostsController do expect { xhr :post, :create }.to raise_error(ActionController::ParameterMissing) end + it 'queues the post if min_first_post_typing_time is not met' do + + SiteSetting.min_first_post_typing_time = 3000 + + xhr :post, :create, {raw: 'this is the test content', title: 'this is the test title for the topic'} + + expect(response).to be_success + parsed = ::JSON.parse(response.body) + + expect(parsed["action"]).to eq("enqueued") + + end + it 'creates the post' do xhr :post, :create, {raw: 'this is the test content', title: 'this is the test title for the topic'}