Record the reason that a post was put into the queue

Record the reason that a post was put into the queue

diff --git a/lib/new_post_manager.rb b/lib/new_post_manager.rb
index b5de841..d0eca26 100644
--- a/lib/new_post_manager.rb
+++ b/lib/new_post_manager.rb
@@ -73,16 +73,31 @@ class NewPostManager
   def self.post_needs_approval?(manager)
     user = manager.user
 
-    return false if exempt_user?(user)
+    return :skip if exempt_user?(user)
 
-    (user.trust_level <= TrustLevel.levels[:basic] && user.post_count < SiteSetting.approve_post_count) ||
-    (user.trust_level < SiteSetting.approve_unless_trust_level.to_i) ||
-    (manager.args[:title].present? && user.trust_level < SiteSetting.approve_new_topics_unless_trust_level.to_i) ||
-    is_fast_typer?(manager) ||
-    matches_auto_silence_regex?(manager) ||
-    WordWatcher.new("#{manager.args[:title]} #{manager.args[:raw]}").requires_approval? ||
-    (SiteSetting.approve_unless_staged && user.staged) ||
-    post_needs_approval_in_its_category?(manager)
+    return :post_count if (
+      user.trust_level <= TrustLevel.levels[:basic] &&
+      user.post_count < SiteSetting.approve_post_count
+    )
+
+    return :trust_level if user.trust_level < SiteSetting.approve_unless_trust_level.to_i
+
+    return :new_topics_unless_trust_level if (
+      manager.args[:title].present? &&
+      user.trust_level < SiteSetting.approve_new_topics_unless_trust_level.to_i
+    )
+
+    return :fast_typer if is_fast_typer?(manager)
+
+    return :auto_silence_regex if matches_auto_silence_regex?(manager)
+
+    return :watched_word if WordWatcher.new("#{manager.args[:title]} #{manager.args[:raw]}").requires_approval?
+
+    return :staged if SiteSetting.approve_unless_staged? && user.staged?
+
+    return :category if post_needs_approval_in_its_category?(manager)
+
+    :skip
   end
 
   def self.post_needs_approval_in_its_category?(manager)
@@ -98,44 +113,46 @@ class NewPostManager
   end
 
   def self.default_handler(manager)
-    if post_needs_approval?(manager)
-      validator = Validators::PostValidator.new
-      post = Post.new(raw: manager.args[:raw])
-      post.user = manager.user
-      validator.validate(post)
 
-      if post.errors[:raw].present?
+    reason = post_needs_approval?(manager)
+    return if reason == :skip
+
+    validator = Validators::PostValidator.new
+    post = Post.new(raw: manager.args[:raw])
+    post.user = manager.user
+    validator.validate(post)
+
+    if post.errors[:raw].present?
+      result = NewPostResult.new(:created_post, false)
+      result.errors[:base] << post.errors[:raw]
+      return result
+    elsif manager.args[:topic_id]
+      topic = Topic.unscoped.where(id: manager.args[:topic_id]).first
+
+      unless manager.user.guardian.can_create_post_on_topic?(topic)
         result = NewPostResult.new(:created_post, false)
-        result.errors[:base] << post.errors[:raw]
+        result.errors[:base] << I18n.t(:topic_not_found)
         return result
-      elsif manager.args[:topic_id]
-        topic = Topic.unscoped.where(id: manager.args[:topic_id]).first
-
-        unless manager.user.guardian.can_create_post_on_topic?(topic)
-          result = NewPostResult.new(:created_post, false)
-          result.errors[:base] << I18n.t(:topic_not_found)
-          return result
-        end
-      elsif manager.args[:category]
-        category = Category.find_by(id: manager.args[:category])
-
-        unless manager.user.guardian.can_create_topic_on_category?(category)
-          result = NewPostResult.new(:created_post, false)
-          result.errors[:base] << I18n.t("js.errors.reasons.forbidden")
-          return result
-        end
       end
+    elsif manager.args[:category]
+      category = Category.find_by(id: manager.args[:category])
 
-      result = manager.enqueue
-
-      if is_fast_typer?(manager)
-        UserSilencer.silence(manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.new_user_typed_too_fast"))
-      elsif matches_auto_silence_regex?(manager)
-        UserSilencer.silence(manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.content_matches_auto_silence_regex"))
+      unless manager.user.guardian.can_create_topic_on_category?(category)
+        result = NewPostResult.new(:created_post, false)
+        result.errors[:base] << I18n.t("js.errors.reasons.forbidden")
+        return result
       end
+    end
+
+    result = manager.enqueue(reason)
 
-      result
+    if is_fast_typer?(manager)
+      UserSilencer.silence(manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.new_user_typed_too_fast"))
+    elsif matches_auto_silence_regex?(manager)
+      UserSilencer.silence(manager.user, Discourse.system_user, keep_posts: true, reason: I18n.t("user.content_matches_auto_silence_regex"))
     end
+
+    result
   end
 
   def self.queue_enabled?
@@ -181,9 +198,12 @@ class NewPostManager
   def enqueue(reason = nil)
     result = NewPostResult.new(:enqueued)
 
+    payload = { raw: @args[:raw], tags: @args[:tags] }
+    payload[:reason] = reason.to_s if reason
+
     reviewable = ReviewableQueuedPost.new(
       created_by: @user,
-      payload: { raw: @args[:raw], tags: @args[:tags] },
+      payload: payload,
       topic_id: @args[:topic_id],
       reviewable_by_moderator: true
     )
diff --git a/spec/components/new_post_manager_spec.rb b/spec/components/new_post_manager_spec.rb
index b00def4..837c81d 100644
--- a/spec/components/new_post_manager_spec.rb
+++ b/spec/components/new_post_manager_spec.rb
@@ -78,6 +78,7 @@ describe NewPostManager do
         result = NewPostManager.default_handler(manager)
         expect(NewPostManager.queue_enabled?).to eq(true)
         expect(result.action).to eq(:enqueued)
+        expect(result.reason).to eq(:post_count)
       end
     end
 
@@ -90,6 +91,7 @@ describe NewPostManager do
         result = NewPostManager.default_handler(manager)
         expect(NewPostManager.queue_enabled?).to eq(true)
         expect(result.action).to eq(:enqueued)
+        expect(result.reason).to eq(:post_count)
       end
     end
 
@@ -109,7 +111,7 @@ describe NewPostManager do
         SiteSetting.approve_post_count = 100
         user = Fabricate(:user)
         category_group = Fabricate(:category_group, permission_type: 2)
-        group_user = Fabricate(:group_user, group: category_group.group, user_id: user.id)
+        Fabricate(:group_user, group: category_group.group, user_id: user.id)
 
         manager = NewPostManager.new(
           user,
@@ -130,6 +132,7 @@ describe NewPostManager do
         result = NewPostManager.default_handler(manager)
         expect(NewPostManager.queue_enabled?).to eq(true)
         expect(result.action).to eq(:enqueued)
+        expect(result.reason).to eq(:trust_level)
       end
     end
 
@@ -163,6 +166,7 @@ describe NewPostManager do
         result = NewPostManager.default_handler(manager)
         expect(NewPostManager.queue_enabled?).to eq(true)
         expect(result.action).to eq(:enqueued)
+        expect(result.reason).to eq(:staged)
       end
     end
 
@@ -188,6 +192,7 @@ describe NewPostManager do
         result = NewPostManager.default_handler(manager)
         expect(NewPostManager.queue_enabled?).to eq(true)
         expect(result.action).to eq(:enqueued)
+        expect(result.reason).to eq(:new_topics_unless_trust_level)
       end
     end
 
@@ -325,19 +330,19 @@ describe NewPostManager do
     it "handles post_needs_approval? correctly" do
       u = user
       default = NewPostManager.new(u, {})
-      expect(NewPostManager.post_needs_approval?(default)).to eq(false)
+      expect(NewPostManager.post_needs_approval?(default)).to eq(:skip)
 
       with_check = NewPostManager.new(u, first_post_checks: true)
-      expect(NewPostManager.post_needs_approval?(with_check)).to eq(true)
+      expect(NewPostManager.post_needs_approval?(with_check)).to eq(:fast_typer)
 
       u.user_stat.post_count = 1
       with_check_and_post = NewPostManager.new(u, first_post_checks: true)

[... diff too long, it was truncated ...]

GitHub sha: 87de5d9e

1 Like