From de9136a8f2d8b29583279d2dfe934ce9592df179 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 18 Apr 2016 22:08:42 +0200 Subject: [PATCH] FIX: bypass TL0-specific validations on posts in a PM --- app/models/user.rb | 10 ++++----- lib/validators/post_validator.rb | 21 ++++++++++++------- .../validators/post_validator_spec.rb | 8 +++++++ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index f5022ca9de3..99b67475ca1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -572,11 +572,11 @@ class User < ActiveRecord::Base end def posted_too_much_in_topic?(topic_id) - - # Does not apply to staff, non-new members or your own topics - return false if staff? || - (trust_level != TrustLevel[0]) || - Topic.where(id: topic_id, user_id: id).exists? + # Does not apply to staff and non-new members... + return false if staff? || (trust_level != TrustLevel[0]) + # ... your own topics or in private messages + topic = Topic.where(id: topic_id).first + return false if topic.try(:private_message?) || (topic.try(:user_id) == self.id) last_action_in_topic = UserAction.last_action_in_topic(id, topic_id) since_reply = Post.where(user_id: id, topic_id: topic_id) diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb index 206d0bfbc98..9d73fe11198 100644 --- a/lib/validators/post_validator.rb +++ b/lib/validators/post_validator.rb @@ -8,7 +8,7 @@ class Validators::PostValidator < ActiveModel::Validator presence(record) return if record.acting_user.try(:staged?) - return if Discourse.static_doc_topic_ids.include?(record.topic_id) && record.acting_user.try(:admin?) + return if record.acting_user.try(:admin?) && Discourse.static_doc_topic_ids.include?(record.topic_id) stripped_length(record) raw_quality(record) @@ -33,7 +33,7 @@ class Validators::PostValidator < ActiveModel::Validator end def stripped_length(post) - range = if post.topic.try(:private_message?) + range = if private_message?(post) # private message SiteSetting.private_message_post_length elsif post.is_first_post? || (post.topic.present? && post.topic.posts_count == 0) @@ -48,7 +48,7 @@ class Validators::PostValidator < ActiveModel::Validator end def raw_quality(post) - sentinel = TextSentinel.body_sentinel(post.raw, private_message: post.topic.try(:private_message?)) + sentinel = TextSentinel.body_sentinel(post.raw, private_message: private_message?(post)) post.errors.add(:raw, I18n.t(:is_invalid)) unless sentinel.valid? end @@ -56,7 +56,7 @@ class Validators::PostValidator < ActiveModel::Validator def max_mention_validator(post) return if post.acting_user.try(:staff?) - if acting_user_is_trusted?(post) + if acting_user_is_trusted?(post) || private_message?(post) add_error_if_count_exceeded(post, :no_mentions_allowed, :too_many_mentions, post.raw_mentions.size, SiteSetting.max_mentions_per_post) else add_error_if_count_exceeded(post, :no_mentions_allowed_newuser, :too_many_mentions_newuser, post.raw_mentions.size, SiteSetting.newuser_max_mentions_per_post) @@ -71,17 +71,20 @@ class Validators::PostValidator < ActiveModel::Validator # Ensure new users can not put too many images in a post def max_images_validator(post) - add_error_if_count_exceeded(post, :no_images_allowed, :too_many_images, post.image_count, SiteSetting.newuser_max_images) unless acting_user_is_trusted?(post) + return if acting_user_is_trusted?(post) || private_message?(post) + add_error_if_count_exceeded(post, :no_images_allowed, :too_many_images, post.image_count, SiteSetting.newuser_max_images) end # Ensure new users can not put too many attachments in a post def max_attachments_validator(post) - add_error_if_count_exceeded(post, :no_attachments_allowed, :too_many_attachments, post.attachment_count, SiteSetting.newuser_max_attachments) unless acting_user_is_trusted?(post) + return if acting_user_is_trusted?(post) || private_message?(post) + add_error_if_count_exceeded(post, :no_attachments_allowed, :too_many_attachments, post.attachment_count, SiteSetting.newuser_max_attachments) end # Ensure new users can not put too many links in a post def max_links_validator(post) - add_error_if_count_exceeded(post, :no_links_allowed, :too_many_links, post.link_count, SiteSetting.newuser_max_links) unless acting_user_is_trusted?(post) + return if acting_user_is_trusted?(post) || private_message?(post) + add_error_if_count_exceeded(post, :no_links_allowed, :too_many_links, post.link_count, SiteSetting.newuser_max_links) end # Stop us from posting the same thing too quickly @@ -104,6 +107,10 @@ class Validators::PostValidator < ActiveModel::Validator post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[1]) end + def private_message?(post) + post.topic.try(:private_message?) + end + def add_error_if_count_exceeded(post, not_allowed_translation_key, limit_translation_key, current_count, max_count) if current_count > max_count if max_count == 0 diff --git a/spec/components/validators/post_validator_spec.rb b/spec/components/validators/post_validator_spec.rb index d7e8e68fc7e..b497d4454c7 100644 --- a/spec/components/validators/post_validator_spec.rb +++ b/spec/components/validators/post_validator_spec.rb @@ -67,6 +67,14 @@ describe Validators::PostValidator do expect(post.errors.count).to be(0) end + it "should be valid when new user exceeds max mentions limit in PM" do + post.acting_user = build(:newuser) + post.topic.expects(:private_message?).returns(true) + post.expects(:raw_mentions).returns(['jake', 'finn', 'jake_old']) + validator.max_mention_validator(post) + expect(post.errors.count).to be(0) + end + it "should be valid when elder user does not exceed max mentions limit" do post.acting_user = build(:trust_level_4) post.expects(:raw_mentions).returns(['jake', 'finn', 'jake_old'])