FEATURE: Do not check consecutive replies for original poster. (#6714)

FEATURE: Do not check consecutive replies for original poster. (#6714)

From 3f8fa4ad4e6e27070b213d52618fd66054ab71fb Mon Sep 17 00:00:00 2001
From: Bianca Nenciu <nbianca@users.noreply.github.com>
Date: Mon, 3 Dec 2018 12:32:29 +0200
Subject: [PATCH] FEATURE: Do not check consecutive replies for original
 poster. (#6714)


diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb
index 750c3ac..9c43cf0 100644
--- a/lib/validators/post_validator.rb
+++ b/lib/validators/post_validator.rb
@@ -146,6 +146,7 @@ class Validators::PostValidator < ActiveModel::Validator
     return if SiteSetting.max_consecutive_replies == 0 || post.id || post.acting_user&.staff? || private_message?(post)
 
     topic = post.topic
+    return if topic.posts.first&.user == post.user
 
     last_posts_count = DB.query_single(<<~SQL, topic_id: post.topic_id, user_id: post.acting_user.id, max_replies: SiteSetting.max_consecutive_replies).first
       SELECT COUNT(*)
diff --git a/spec/components/validators/post_validator_spec.rb b/spec/components/validators/post_validator_spec.rb
index cb33aa8..de1df04 100644
--- a/spec/components/validators/post_validator_spec.rb
+++ b/spec/components/validators/post_validator_spec.rb
@@ -229,7 +229,19 @@ describe Validators::PostValidator do
       SiteSetting.max_consecutive_replies = 2
     end
 
+    it "should always allow posting" do
+      [user, user, user, other_user, user, user, user].each_with_index do |u, i|
+        post = Post.new(user: user, topic: topic, raw: "post number #{i}")
+        validator.force_edit_last_validator(post)
+        expect(post.errors.count).to eq(0)
+        post.save
+      end
+    end
+
     it "should not allow posting more than 2 consecutive replies" do
+      post = Post.new(user: other_user, topic: topic, raw: "post number 0")
+      post.save
+
       1.upto(3).each do |i|
         post = Post.new(user: user, topic: topic, raw: "post number #{i}")
         validator.force_edit_last_validator(post)

GitHub

This test doesn’t look right, it is always creating a post with user instead of u

2 Likes

post.save! will be better since we will immediately know what went wrong if the post fails to save.

2 Likes

You can use Post.create so that you don’t have to call Post.new(...).save

2 Likes

This commit has been mentioned on Discourse Meta. There might be relevant details there: