Skip to content

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

FEATURE: improve post_created_edited automation triggers (#31694)

Sam50310ea96d69
diff --git a/plugins/automation/config/locales/client.en.yml b/plugins/automation/config/locales/client.en.yml
index af32493e951..8b768ba8b46 100644
--- a/plugins/automation/config/locales/client.en.yml
+++ b/plugins/automation/config/locales/client.en.yml
@@ -159,9 +159,15 @@ en:
restricted_tags:
label: Tags
description: Optional, will trigger only if the post has any of these tags
- restricted_category:
- label: Category
- description: Optional, will trigger only if the post's topic is in this category
+ restricted_categories:
+ label: Categories
+ description: Optional, will trigger only if the post's topic is in these categories
+ exclude_subcategories:
+ label: Exclude subcategories
+ description: Optional, will only trigger if the post's topic is exactly in the categories specified above
+ restricted_archetype:
+ label: Topic Type
+ description: Optional, allow to trigger only on topics or personal messages
restricted_user_group:
label: Restricted user group
description: Optional, will trigger only if the post user is in this group
@@ -185,6 +191,8 @@ en:
description: "Skip the trigger if the post was created via email"
created: Created
edited: Edited
+ topics: Topics
+ personal_messages: Personal Messages
user_updated:
fields:
user_profile:
diff --git a/plugins/automation/db/post_migrate/20250307031538_migrate_category_to_categories_post_created.rb b/plugins/automation/db/post_migrate/20250307031538_migrate_category_to_categories_post_created.rb
new file mode 100644
index 00000000000..44f1572ec6b
--- /dev/null
+++ b/plugins/automation/db/post_migrate/20250307031538_migrate_category_to_categories_post_created.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+class MigrateCategoryToCategoriesPostCreated < ActiveRecord::Migration[7.2]
+ def up
+ # this is a safety net, should never happen but we don't want dupes here
+ execute <<~SQL
+ DELETE FROM discourse_automation_fields
+ WHERE name = 'restricted_categories'
+ AND target = 'trigger'
+ AND automation_id IN (
+ SELECT id FROM discourse_automation_automations
+ WHERE trigger = 'post_created_edited'
+ )
+ SQL
+
+ execute <<~SQL
+ INSERT INTO discourse_automation_fields (
+ automation_id, component, name, target, metadata, created_at, updated_at
+ )
+ SELECT
+ f.automation_id,
+ 'categories' as component,
+ 'restricted_categories' as name,
+ f.target,
+ jsonb_build_object('value', ARRAY[CAST(f.metadata->>'value' AS INTEGER)]) as metadata,
+ NOW() as created_at,
+ NOW() as updated_at
+ FROM discourse_automation_fields f
+ JOIN discourse_automation_automations a ON a.id = f.automation_id
+ WHERE f.component = 'category'
+ AND f.name = 'restricted_category'
+ AND f.target = 'trigger'
+ AND f.metadata->>'value' IS NOT NULL
+ AND a.trigger = 'post_created_edited'
+ SQL
+ end
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/automation/lib/discourse_automation/event_handlers.rb b/plugins/automation/lib/discourse_automation/event_handlers.rb
index fb6b686c007..938f47e743e 100644
--- a/plugins/automation/lib/discourse_automation/event_handlers.rb
+++ b/plugins/automation/lib/discourse_automation/event_handlers.rb
@@ -12,6 +12,12 @@ module DiscourseAutomation
DiscourseAutomation::Automation
.where(trigger: name, enabled: true)
.find_each do |automation|
+ restricted_archetype = automation.trigger_field("restricted_archetype")["value"]
+ if restricted_archetype.present?
+ topic_archetype = topic.archetype
+ next if restricted_archetype != topic_archetype
+ end
+
original_post_only = automation.trigger_field("original_post_only")
if original_post_only["value"]
next if topic.posts_count > 1
@@ -38,15 +44,21 @@ module DiscourseAutomation
next if valid_trust_levels["value"].exclude?(post.user.trust_level)
end
- restricted_category = automation.trigger_field("restricted_category")
- if restricted_category["value"]
- category_ids =
- if topic.category_id.blank?
- []
- else
- [topic.category_id, topic.category.parent_category_id]
- end
- next if !category_ids.include?(restricted_category["value"])
+ restricted_categories = automation.trigger_field("restricted_categories").dup
+ if restricted_category_ids = restricted_categories["value"]
+ exclude_subcategories = automation.trigger_field("exclude_subcategories")["value"]
+
+ if !exclude_subcategories
+ # core api is odd, we only support nesting of 3 anyway, this is efficient
+ restricted_category_ids = restricted_category_ids.to_set
+ # for nesting of 3
+ restricted_category_ids +=
+ Category.where(parent_category_id: restricted_category_ids).pluck(:id)
+ restricted_category_ids +=
+ Category.where(parent_category_id: restricted_category_ids).pluck(:id)
+ end
+
+ next if !restricted_category_ids.include?(topic.category_id)
end
restricted_tags = automation.trigger_field("restricted_tags")
diff --git a/plugins/automation/lib/discourse_automation/triggers/post_created_edited.rb b/plugins/automation/lib/discourse_automation/triggers/post_created_edited.rb
index 18056783e15..282c0ab5598 100644
--- a/plugins/automation/lib/discourse_automation/triggers/post_created_edited.rb
+++ b/plugins/automation/lib/discourse_automation/triggers/post_created_edited.rb
@@ -12,7 +12,19 @@ DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::POST_CREATED
{ id: "edited", name: "discourse_automation.triggerables.post_created_edited.edited" },
],
}
- field :restricted_category, component: :category
+ field :restricted_archetype,
+ component: :choices,
+ extra: {
+ content: [
+ { id: "regular", name: "discourse_automation.triggerables.post_created_edited.topics" },
+ {
+ id: "private_message",
+ name: "discourse_automation.triggerables.post_created_edited.personal_messages",
+ },
+ ],
+ }
+ field :restricted_categories, component: :categories
+ field :exclude_subcategories, component: :boolean
field :restricted_tags, component: :tags
field :restricted_groups, component: :groups
field :restricted_user_group, component: :group
diff --git a/plugins/automation/spec/triggers/post_created_edited_spec.rb b/plugins/automation/spec/triggers/post_created_edited_spec.rb
index 39150212ed4..94d1eeaa26d 100644
--- a/plugins/automation/spec/triggers/post_created_edited_spec.rb
+++ b/plugins/automation/spec/triggers/post_created_edited_spec.rb
@@ -414,120 +414,164 @@ describe "PostCreatedEdited" do
end
end
- context "when category is restricted" do
+ context "when using restricted_categories with deeply nested categories" do
+ before_all { SiteSetting.max_category_nesting = 3 }
+
+ fab!(:top_category) { Fabricate(:category) }
+ fab!(:mid_category) { Fabricate(:category, parent_category_id: top_category.id) }
+ fab!(:bottom_category) { Fabricate(:category, parent_category_id: mid_category.id) }
+ fab!(:another_category) { Fabricate(:category) }
+
before do
automation.upsert_field!(
- "restricted_category",
- "category",
- { value: Category.first.id },
+ "restricted_categories",
+ "categories",
+ { value: [top_category.id] },
target: "trigger",
)
end
- context "when category is allowed" do
- it "fires the trigger" do
- list =
- capture_contexts do
- PostCreator.create(user, basic_topic_params.merge({ category: Category.first.id }))
- end
+ it "fires the trigger for posts in any grand child category" do
+ list =
+ capture_contexts do
+ PostCreator.create(user, basic_topic_params.merge({ category: bottom_category.id }))
+ end
- expect(list.length).to eq(1)
- expect(list[0]["kind"]).to eq("post_created_edited")
- end
+ expect(list.length).to eq(1)
+ expect(list[0]["kind"]).to eq("post_created_edited")
+ end
+
+ it "will not fire on unrelated categories" do
+ list =
+ capture_contexts do
+ PostCreator.create(user, basic_topic_params.merge({ category: another_category.id }))
+ end
+ expect(list.length).to eq(0)
+ end
+
+ it "fires the trigger for posts in any child category" do
+ list =
+ capture_contexts do
+ PostCreator.create(user, basic_topic_params.merge({ category: mid_category.id }))
+ end
+
+ expect(list.length).to eq(1)
+ expect(list[0]["kind"]).to eq("post_created_edited")
end
- context "when restricted to a subcategory" do
+ context "when exclude_subcategories is enabled" do
before do
automation.upsert_field!(
- "restricted_category",
- "category",
- { value: subcategory.id },
+ "exclude_subcategories",
+ "boolean",
+ { value: true },
target: "trigger",
)
end
- it "fires the trigger" do
+ it "does not fire for children" do
list =
capture_contexts do
- PostCreator.create(user, basic_topic_params.merge({ category: subcategory.id }))
+ PostCreator.create(user, basic_topic_params.merge({ category: bottom_category.id }))
end
-
- expect(list.length).to eq(1)
- expect(list[0]["kind"]).to eq("post_created_edited")
+ expect(list.length).to eq(0)
end
- it "does not fire the trigger for the parent" do
+ it "fires for the exact category match" do
list =
capture_contexts do
- PostCreator.create(user, basic_topic_params.merge({ category: parent_category.id }))
+ PostCreator.create(user, basic_topic_params.merge({ category: top_category.id }))
end
-
- expect(list.length).to eq(0)
+ expect(list.length).to eq(1)
end
end
+ end
+
+ context "when action_type is set to create" do
+ before do
+ automation.upsert_field!("action_type", "choices", { value: "created" }, target: "trigger")
+ end
+
+ it "fires the trigger only for create" do
+ post = nil
+
+ list = capture_contexts { post = PostCreator.create(user, basic_topic_params) }
+
+ expect(list.length).to eq(1)
+ expect(list[0]["kind"]).to eq("post_created_edited")
+ expect(list[0]["action"].to_s).to eq("create")
+
+ list = capture_contexts { post.revise(post.user, raw: "this is another cool topic") }
+
+ expect(list.length).to eq(0)
+ end
+ end
- context "when restricted to a parent category" do
+ context "when archetype is restricted" do
+ context "when only regular topics are allowed" do
before do
automation.upsert_field!(
- "restricted_category",
- "category",
- { value: parent_category.id },
+ "restricted_archetype",
+ "choices",
+ { value: "regular" },
target: "trigger",
)
end
- it "fires the trigger for a subcategory" do
- list =
- capture_contexts do
- PostCreator.create(user, basic_topic_params.merge({ category: subcategory.id }))
- end
+ it "fires the trigger for regular topics" do
+ list = capture_contexts { PostCreator.create(user, basic_topic_params) }
expect(list.length).to eq(1)
expect(list[0]["kind"]).to eq("post_created_edited")
end
- it "fires the trigger for the parent" do
+ it "doesn't fire the trigger for private messages" do
list =
capture_contexts do
- PostCreator.create(user, basic_topic_params.merge({ category: parent_category.id }))
+ PostCreator.create(
+ user,
+ basic_topic_params.merge(
+ archetype: Archetype.private_message,
+ target_usernames: [Fabricate(:user).username],
+ ),
+ )
end
- expect(list.length).to eq(1)
- expect(list[0]["kind"]).to eq("post_created_edited")
+ expect(list.length).to eq(0)
end
end
- context "when category is not allowed" do
- fab!(:category)
+ context "when only private messages are allowed" do
+ before do
+ automation.upsert_field!(
+ "restricted_archetype",
+ "choices",
+ { value: "private_message" },
+ target: "trigger",
+ )
+ end
- it "doesn’t fire the trigger" do
+ it "fires the trigger for private messages" do
list =
capture_contexts do
- PostCreator.create(user, basic_topic_params.merge({ category: category.id }))
+ PostCreator.create(
+ user,
+ basic_topic_params.merge(
+ archetype: Archetype.private_message,
+ target_usernames: [Fabricate(:user).username],
+ ),
+ )
end
- expect(list).to be_blank
+ expect(list.length).to eq(1)
+ expect(list[0]["kind"]).to eq("post_created_edited")
end
- end
- end
- context "when action_type is set to create" do
- before do
- automation.upsert_field!("action_type", "choices", { value: "created" }, target: "trigger")
- end
-
- it "fires the trigger only for create" do
- post = nil
-
- list = capture_contexts { post = PostCreator.create(user, basic_topic_params) }
-
- expect(list.length).to eq(1)
- expect(list[0]["kind"]).to eq("post_created_edited")
- expect(list[0]["action"].to_s).to eq("create")
-
- list = capture_contexts { post.revise(post.user, raw: "this is another cool topic") }
+ it "doesn't fire the trigger for regular topics" do
+ list = capture_contexts { PostCreator.create(user, basic_topic_params) }
- expect(list.length).to eq(0)
+ expect(list.length).to eq(0)
+ end
end
end