A platform for community discussion. Free, open, simple.
FEATURE: Allow excluding uploads from min post length requirement (#31194)
Ted Johansson997a9e3de9e4
diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 5b157264f37..454389a50e7 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1743,6 +1743,7 @@ en:support_mixed_text_direction: "Support mixed left-to-right and right-to-left text directions"min_post_length: "Minimum allowed post length in characters (excluding personal messages)"min_first_post_length: "Minimum allowed first post (topic body) length (excluding personal messages)"+ prevent_uploads_only_posts: "Don't count upload markdown code when checking for min post length"min_personal_message_post_length: "Minimum allowed post length in characters for messages (both first post and replies)"max_post_length: "Maximum allowed post length in characters"topic_featured_link_enabled: "Allows users to associate a feature link with their topics. When turned on, topics can have a highlighted link attached, which is publicly visible and can be edited if the user has sufficient permissions. The feature link can enhance a topic's comprehensibility by providing related additional content."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex 672c42db662..6c48e61b8d7 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -916,6 +916,9 @@ posting:ja: 8zh_CN: 8zh_TW: 8+ prevent_uploads_only_posts:+ client: true+ default: falsemin_personal_message_post_length:client: truemin: 1diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rbindex f0539e72d64..b488eec4ebc 100644--- a/lib/validators/post_validator.rb+++ b/lib/validators/post_validator.rb@@ -48,7 +48,13 @@ class PostValidator < ActiveModel::ValidatorSiteSetting.post_lengthend- StrippedLengthValidator.validate(post, :raw, post.raw, range)+ StrippedLengthValidator.validate(+ post,+ :raw,+ post.raw,+ range,+ strip_uploads: SiteSetting.prevent_uploads_only_posts,+ )enddef max_posts_validator(post)diff --git a/lib/validators/stripped_length_validator.rb b/lib/validators/stripped_length_validator.rbindex 984f0e37da7..df9fd5628c6 100644--- a/lib/validators/stripped_length_validator.rb+++ b/lib/validators/stripped_length_validator.rb@@ -1,7 +1,7 @@# frozen_string_literal: trueclass StrippedLengthValidator < ActiveModel::EachValidator- def self.validate(record, attribute, value, range)+ def self.validate(record, attribute, value, range, strip_uploads: false)if value.blank?record.errors.add attribute, I18n.t("errors.messages.blank")elsif value.length > range.end@@ -12,7 +12,7 @@ class StrippedLengthValidator < ActiveModel::EachValidatorlength: value.length,)else- value = get_sanitized_value(value)+ value = get_sanitized_value(value, strip_uploads:)if value.length < range.beginrecord.errors.add attribute, I18n.t("errors.messages.too_short", count: range.begin)@@ -26,12 +26,14 @@ class StrippedLengthValidator < ActiveModel::EachValidatorself.class.validate(record, attribute, value, range)end- def self.get_sanitized_value(value)+ def self.get_sanitized_value(value, strip_uploads: false)value = value.dupvalue.gsub!(/<!--(.*?)-->/, "") # strip HTML commentsvalue.gsub!(/:\w+(:\w+)?:/, "X") # replace emojis with a single charactervalue.gsub!(/\.{2,}/, "…") # replace multiple ... with …value.gsub!(/\,{2,}/, ",") # replace multiple ,,, with ,+ value.gsub!(/!\[.*\]\(.+\)/, "") if strip_uploads+value.stripendenddiff --git a/spec/lib/validators/post_validator_spec.rb b/spec/lib/validators/post_validator_spec.rbindex 0ec5f947627..5f5033fb1ee 100644--- a/spec/lib/validators/post_validator_spec.rb+++ b/spec/lib/validators/post_validator_spec.rb@@ -46,7 +46,7 @@ RSpec.describe PostValidator doendend- describe "stripped_length" do+ describe "#stripped_length" doit "adds an error for short raw" dopost.raw = "abc"validator.stripped_length(post)@@ -103,6 +103,26 @@ RSpec.describe PostValidator dovalidator.stripped_length(post)expect(post.errors.count).to eq(1)end++ context "when configured to count uploads" do+ before { SiteSetting.prevent_uploads_only_posts = false }++ it "counts image tags" do+ post.raw = ""+ validator.stripped_length(post)+ expect(post.errors.count).to eq(0)+ end+ end++ context "when configured to not count uploads" do+ before { SiteSetting.prevent_uploads_only_posts = true }++ it "doesn't count image tags" do+ post.raw = ""+ validator.stripped_length(post)+ expect(post.errors.count).to eq(1)+ end+ endenddescribe "max_posts_validator" do