mirror of
https://github.com/discourse/discourse.git
synced 2025-05-29 01:31:35 +08:00
FEATURE: Allow excluding uploads from min post length requirement (#31194)
Currently, the markdown for uploads is counted towards post minimum length requirements. This change introduces a site setting `prevent_uploads_only_posts` which can be flipped to exclude upload segments from the calculation.
This commit is contained in:
@ -48,7 +48,13 @@ class PostValidator < ActiveModel::Validator
|
||||
SiteSetting.post_length
|
||||
end
|
||||
|
||||
StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
||||
StrippedLengthValidator.validate(
|
||||
post,
|
||||
:raw,
|
||||
post.raw,
|
||||
range,
|
||||
strip_uploads: SiteSetting.prevent_uploads_only_posts,
|
||||
)
|
||||
end
|
||||
|
||||
def max_posts_validator(post)
|
||||
|
@ -1,7 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class 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::EachValidator
|
||||
length: value.length,
|
||||
)
|
||||
else
|
||||
value = get_sanitized_value(value)
|
||||
value = get_sanitized_value(value, strip_uploads:)
|
||||
|
||||
if value.length < range.begin
|
||||
record.errors.add attribute, I18n.t("errors.messages.too_short", count: range.begin)
|
||||
@ -26,12 +26,14 @@ class StrippedLengthValidator < ActiveModel::EachValidator
|
||||
self.class.validate(record, attribute, value, range)
|
||||
end
|
||||
|
||||
def self.get_sanitized_value(value)
|
||||
def self.get_sanitized_value(value, strip_uploads: false)
|
||||
value = value.dup
|
||||
value.gsub!(/<!--(.*?)-->/, "") # strip HTML comments
|
||||
value.gsub!(/:\w+(:\w+)?:/, "X") # replace emojis with a single character
|
||||
value.gsub!(/\.{2,}/, "…") # replace multiple ... with …
|
||||
value.gsub!(/\,{2,}/, ",") # replace multiple ,,, with ,
|
||||
value.gsub!(/!\[.*\]\(.+\)/, "") if strip_uploads
|
||||
|
||||
value.strip
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user