mirror of
https://github.com/discourse/discourse.git
synced 2025-06-01 06:27:45 +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:
@ -1743,6 +1743,7 @@ en:
|
|||||||
support_mixed_text_direction: "Support mixed left-to-right and right-to-left text directions"
|
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_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)"
|
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)"
|
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"
|
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."
|
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."
|
||||||
|
@ -916,6 +916,9 @@ posting:
|
|||||||
ja: 8
|
ja: 8
|
||||||
zh_CN: 8
|
zh_CN: 8
|
||||||
zh_TW: 8
|
zh_TW: 8
|
||||||
|
prevent_uploads_only_posts:
|
||||||
|
client: true
|
||||||
|
default: false
|
||||||
min_personal_message_post_length:
|
min_personal_message_post_length:
|
||||||
client: true
|
client: true
|
||||||
min: 1
|
min: 1
|
||||||
|
@ -48,7 +48,13 @@ class PostValidator < ActiveModel::Validator
|
|||||||
SiteSetting.post_length
|
SiteSetting.post_length
|
||||||
end
|
end
|
||||||
|
|
||||||
StrippedLengthValidator.validate(post, :raw, post.raw, range)
|
StrippedLengthValidator.validate(
|
||||||
|
post,
|
||||||
|
:raw,
|
||||||
|
post.raw,
|
||||||
|
range,
|
||||||
|
strip_uploads: SiteSetting.prevent_uploads_only_posts,
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def max_posts_validator(post)
|
def max_posts_validator(post)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class StrippedLengthValidator < ActiveModel::EachValidator
|
class StrippedLengthValidator < ActiveModel::EachValidator
|
||||||
def self.validate(record, attribute, value, range)
|
def self.validate(record, attribute, value, range, strip_uploads: false)
|
||||||
if value.blank?
|
if value.blank?
|
||||||
record.errors.add attribute, I18n.t("errors.messages.blank")
|
record.errors.add attribute, I18n.t("errors.messages.blank")
|
||||||
elsif value.length > range.end
|
elsif value.length > range.end
|
||||||
@ -12,7 +12,7 @@ class StrippedLengthValidator < ActiveModel::EachValidator
|
|||||||
length: value.length,
|
length: value.length,
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
value = get_sanitized_value(value)
|
value = get_sanitized_value(value, strip_uploads:)
|
||||||
|
|
||||||
if value.length < range.begin
|
if value.length < range.begin
|
||||||
record.errors.add attribute, I18n.t("errors.messages.too_short", count: 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)
|
self.class.validate(record, attribute, value, range)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_sanitized_value(value)
|
def self.get_sanitized_value(value, strip_uploads: false)
|
||||||
value = value.dup
|
value = value.dup
|
||||||
value.gsub!(/<!--(.*?)-->/, "") # strip HTML comments
|
value.gsub!(/<!--(.*?)-->/, "") # strip HTML comments
|
||||||
value.gsub!(/:\w+(:\w+)?:/, "X") # replace emojis with a single character
|
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!(/\,{2,}/, ",") # replace multiple ,,, with ,
|
value.gsub!(/\,{2,}/, ",") # replace multiple ,,, with ,
|
||||||
|
value.gsub!(/!\[.*\]\(.+\)/, "") if strip_uploads
|
||||||
|
|
||||||
value.strip
|
value.strip
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -46,7 +46,7 @@ RSpec.describe PostValidator do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "stripped_length" do
|
describe "#stripped_length" do
|
||||||
it "adds an error for short raw" do
|
it "adds an error for short raw" do
|
||||||
post.raw = "abc"
|
post.raw = "abc"
|
||||||
validator.stripped_length(post)
|
validator.stripped_length(post)
|
||||||
@ -103,6 +103,26 @@ RSpec.describe PostValidator do
|
|||||||
validator.stripped_length(post)
|
validator.stripped_length(post)
|
||||||
expect(post.errors.count).to eq(1)
|
expect(post.errors.count).to eq(1)
|
||||||
end
|
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
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "max_posts_validator" do
|
describe "max_posts_validator" do
|
||||||
|
Reference in New Issue
Block a user