FIX: min_personal_message_post_length not applying to first post (#23531)

* FIX: min_personal_message_post_length not applying to first post

Due to the way PostCreator is wired, we were not applying min_personal_message_post_length
to the first post.

This meant that admins could not configure it so PMs have different
limits.

The code was already pretending that this works, but had no reliable way
of figuring out if we were dealing with a private message
This commit is contained in:
Sam 2023-09-13 15:43:54 +10:00 committed by GitHub
parent f6326d03f0
commit 267e8ebaa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 6 deletions

View File

@ -1510,9 +1510,9 @@ en:
allow_user_locale: "Allow users to choose their own language interface preference"
set_locale_from_accept_language_header: "set interface language for anonymous users from their web browser's language headers"
support_mixed_text_direction: "Support mixed left-to-right and right-to-left text directions."
min_post_length: "Minimum allowed post length in characters"
min_first_post_length: "Minimum allowed first post (topic body) length in characters"
min_personal_message_post_length: "Minimum allowed post length in characters for 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_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: "Enable posting a link with topics."
show_topic_featured_link_in_digest: "Show the topic featured link in the digest email."

View File

@ -167,7 +167,11 @@ class PostCreator
DiscourseEvent.trigger :before_create_post, @post, @opts
DiscourseEvent.trigger :validate_post, @post
post_validator = PostValidator.new(skip_topic: true)
post_validator =
PostValidator.new(
skip_topic: true,
private_message: @opts[:archetype] == Archetype.private_message,
)
post_validator.validate(@post)
valid = @post.errors.blank?

View File

@ -212,7 +212,7 @@ class PostValidator < ActiveModel::Validator
end
def private_message?(post)
post.topic.try(:private_message?)
post.topic.try(:private_message?) || options[:private_message]
end
def add_error_if_count_exceeded(

View File

@ -208,7 +208,6 @@ RSpec.describe PostCreator do
cat.save
created_post = nil
other_user_tracking_topic = nil
messages =
MessageBus.track_publish do
@ -1099,6 +1098,26 @@ RSpec.describe PostCreator do
)
end
it "respects min_personal_message_post_length" do
SiteSetting.min_personal_message_post_length = 5
SiteSetting.min_first_post_length = 20
SiteSetting.min_post_length = 25
SiteSetting.body_min_entropy = 20
user.update!(trust_level: 3)
Group.refresh_automatic_groups!
expect {
PostCreator.create!(
user,
title: "hi there welcome to my PM",
raw: "sorry",
archetype: Archetype.private_message,
target_usernames: [target_user1.username, target_user2.username].join(","),
category: 1,
)
}.not_to raise_error
end
it "acts correctly" do
freeze_time