Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add Plugin modifier for reviewable creation (bot posts) (#33161)

Mark VanLandinghame39c7b4cbb0a
diff --git a/lib/post_action_creator.rb b/lib/post_action_creator.rb
index d971569fe2b..84df425f681 100644
--- a/lib/post_action_creator.rb
+++ b/lib/post_action_creator.rb
@@ -384,7 +384,17 @@ class PostActionCreator
def create_reviewable(result)
return unless flagging_post?
- return if @post.user_id.to_i < 0
+
+ # Return early if the reviewable is being created for a user with a negative user_id.
+ # Plugin apply_modifier can remove this early return for special cases.
+ is_bot_post = @post.user_id.to_i < 0
+ if DiscoursePluginRegistry.apply_modifier(
+ :post_action_creator_block_reviewable_for_bot,
+ is_bot_post,
+ @post,
+ )
+ return
+ end
result.reviewable =
ReviewableFlaggedPost.needs_review!(
diff --git a/spec/lib/post_action_creator_spec.rb b/spec/lib/post_action_creator_spec.rb
index 54460fc7e63..4d31b62ce51 100644
--- a/spec/lib/post_action_creator_spec.rb
+++ b/spec/lib/post_action_creator_spec.rb
@@ -196,6 +196,31 @@ RSpec.describe PostActionCreator do
end
end
+ describe "non-human user being flagged" do
+ fab!(:system_post) { Fabricate(:post, user: Discourse.system_user) }
+
+ it "doesn't create reviewable" do
+ result = PostActionCreator.create(user, system_post, :inappropriate)
+ expect(result.success?).to eq(true)
+
+ expect(result.reviewable).to be_blank
+ end
+
+ it "applies modifier and can allow reviewable creation for non-human users" do
+ plugin = Plugin::Instance.new
+ modifier = :post_action_creator_block_reviewable_for_bot
+ proc = Proc.new { false }
+ DiscoursePluginRegistry.register_modifier(plugin, modifier, &proc)
+
+ result = PostActionCreator.create(user, system_post, :inappropriate)
+ expect(result.success?).to eq(true)
+
+ expect(result.reviewable).to be_present
+ ensure
+ DiscoursePluginRegistry.unregister_modifier(plugin, modifier, &proc)
+ end
+ end
+
context "with existing reviewable" do
let!(:reviewable) do
PostActionCreator.create(