From b706307ac76a5db188d4ef8de7b8823f72158d14 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Thu, 19 Mar 2015 19:47:55 +0530 Subject: [PATCH] FEATURE: new site setting min_first_post_length --- .../javascripts/discourse/models/composer.js.es6 | 7 ++++++- app/models/site_setting.rb | 4 ++++ config/locales/server.en.yml | 1 + config/site_settings.yml | 6 ++++++ lib/validators/post_validator.rb | 12 +++++++++++- script/import_scripts/base.rb | 1 + spec/models/site_setting_spec.rb | 6 ++++++ test/javascripts/models/composer-test.js.es6 | 10 ++++++---- 8 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/models/composer.js.es6 b/app/assets/javascripts/discourse/models/composer.js.es6 index 4a80fdeab04..af68aeda921 100644 --- a/app/assets/javascripts/discourse/models/composer.js.es6 +++ b/app/assets/javascripts/discourse/models/composer.js.es6 @@ -40,6 +40,8 @@ const Composer = Discourse.Model.extend({ return this.get('creatingPrivateMessage') || this.get('topic.archetype') === 'private_message'; }.property('creatingPrivateMessage', 'topic'), + topicFirstPost: Em.computed.or('creatingTopic', 'editingFirstPost'), + editingPost: Em.computed.equal('action', EDIT), replyingToTopic: Em.computed.equal('action', REPLY), @@ -215,10 +217,13 @@ const Composer = Discourse.Model.extend({ minimumPostLength: function() { if( this.get('privateMessage') ) { return Discourse.SiteSettings.min_private_message_post_length; + } else if (this.get('topicFirstPost')) { + // first post (topic body) + return Discourse.SiteSettings.min_first_post_length; } else { return Discourse.SiteSettings.min_post_length; } - }.property('privateMessage'), + }.property('privateMessage', 'topicFirstPost'), /** Computes the length of the title minus non-significant whitespaces diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index 284df6d532b..5b272c0d918 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -48,6 +48,10 @@ class SiteSetting < ActiveRecord::Base min_post_length..max_post_length end + def self.first_post_length + min_first_post_length..max_post_length + end + def self.private_message_post_length min_private_message_post_length..max_post_length end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 9e61260cc64..8d30d9f1daa 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -733,6 +733,7 @@ en: default_locale: "The default language of this Discourse instance (ISO 639-1 Code)" allow_user_locale: "Allow users to choose their own language interface preference" min_post_length: "Minimum allowed post length in characters" + min_first_post_length: "Minimum allowed first post (topic body) length in characters" min_private_message_post_length: "Minimum allowed post length in characters for private messages" max_post_length: "Maximum allowed post length in characters" min_topic_title_length: "Minimum allowed topic title length in characters" diff --git a/config/site_settings.yml b/config/site_settings.yml index 6c9c5a82ae3..a958214d720 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -310,6 +310,12 @@ posting: default: test: 5 default: 20 + min_first_post_length: + client: true + min: 1 + default: + test: 5 + default: 20 min_private_message_post_length: client: true min: 1 diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb index 04b0fb7862a..a5751ccade2 100644 --- a/lib/validators/post_validator.rb +++ b/lib/validators/post_validator.rb @@ -25,7 +25,17 @@ class Validators::PostValidator < ActiveModel::Validator end def stripped_length(post) - range = post.topic.try(:private_message?) ? SiteSetting.private_message_post_length : SiteSetting.post_length + range = if post.topic.try(:private_message?) + # private message + SiteSetting.private_message_post_length + elsif ( post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0) ) + # creating/editing first post + SiteSetting.first_post_length + else + # regular post + SiteSetting.post_length + end + Validators::StrippedLengthValidator.validate(post, :raw, post.raw, range) end diff --git a/script/import_scripts/base.rb b/script/import_scripts/base.rb index a3120edd24e..2b41c6e285c 100644 --- a/script/import_scripts/base.rb +++ b/script/import_scripts/base.rb @@ -99,6 +99,7 @@ class ImportScripts::Base email_domains_blacklist: '', min_topic_title_length: 1, min_post_length: 1, + min_first_post_length: 1, min_private_message_post_length: 1, min_private_message_title_length: 1, allow_duplicate_topic_titles: true, diff --git a/spec/models/site_setting_spec.rb b/spec/models/site_setting_spec.rb index e1466a40006..0cd8691a66c 100644 --- a/spec/models/site_setting_spec.rb +++ b/spec/models/site_setting_spec.rb @@ -35,6 +35,12 @@ describe SiteSetting do end end + describe 'first_post_length' do + it 'returns a range of min/max first post length' do + expect(SiteSetting.first_post_length).to eq(SiteSetting.defaults[:min_first_post_length]..SiteSetting.defaults[:max_post_length]) + end + end + describe 'private_message_title_length' do it 'returns a range of min/max pm topic title length' do expect(SiteSetting.private_message_title_length).to eq(SiteSetting.defaults[:min_private_message_title_length]..SiteSetting.defaults[:max_topic_title_length]) diff --git a/test/javascripts/models/composer-test.js.es6 b/test/javascripts/models/composer-test.js.es6 index cf359e99762..413156077e4 100644 --- a/test/javascripts/models/composer-test.js.es6 +++ b/test/javascripts/models/composer-test.js.es6 @@ -22,13 +22,15 @@ test('replyLength', function() { }); test('missingReplyCharacters', function() { - var missingReplyCharacters = function(val, isPM, expected, message) { - var composer = Discourse.Composer.create({ reply: val, creatingPrivateMessage: isPM }); + Discourse.SiteSettings.min_first_post_length = 40; + var missingReplyCharacters = function(val, isPM, isFirstPost, expected, message) { + var composer = Discourse.Composer.create({ reply: val, creatingPrivateMessage: isPM, creatingTopic: isFirstPost }); equal(composer.get('missingReplyCharacters'), expected, message); }; - missingReplyCharacters('hi', false, Discourse.SiteSettings.min_post_length - 2, 'too short public post'); - missingReplyCharacters('hi', true, Discourse.SiteSettings.min_private_message_post_length - 2, 'too short private message'); + missingReplyCharacters('hi', false, false, Discourse.SiteSettings.min_post_length - 2, 'too short public post'); + missingReplyCharacters('hi', false, true, Discourse.SiteSettings.min_first_post_length - 2, 'too short first post'); + missingReplyCharacters('hi', true, false, Discourse.SiteSettings.min_private_message_post_length - 2, 'too short private message'); }); test('missingTitleCharacters', function() {