From 267e8ebaa64c6171c8119e3efafdd4145321d584 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 13 Sep 2023 15:43:54 +1000 Subject: [PATCH] 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 --- config/locales/server.en.yml | 6 +++--- lib/post_creator.rb | 6 +++++- lib/validators/post_validator.rb | 2 +- spec/lib/post_creator_spec.rb | 21 ++++++++++++++++++++- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index c934b49568f..abbdc770e53 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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." diff --git a/lib/post_creator.rb b/lib/post_creator.rb index f1afa7aac72..787787b2edc 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -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? diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb index 737c37c1dbf..766424d13bd 100644 --- a/lib/validators/post_validator.rb +++ b/lib/validators/post_validator.rb @@ -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( diff --git a/spec/lib/post_creator_spec.rb b/spec/lib/post_creator_spec.rb index 4dc8bdde0d1..b2209df8e2d 100644 --- a/spec/lib/post_creator_spec.rb +++ b/spec/lib/post_creator_spec.rb @@ -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