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.ymlindex 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: Tagsdescription: 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 messagesrestricted_user_group:label: Restricted user groupdescription: 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: Creatededited: Edited+ topics: Topics+ personal_messages: Personal Messagesuser_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.rbnew file mode 100644index 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+enddiff --git a/plugins/automation/lib/discourse_automation/event_handlers.rb b/plugins/automation/lib/discourse_automation/event_handlers.rbindex 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 DiscourseAutomationDiscourseAutomation::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 DiscourseAutomationnext 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)endrestricted_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.rbindex 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: :booleanfield :restricted_tags, component: :tagsfield :restricted_groups, component: :groupsfield :restricted_user_group, component: :groupdiff --git a/plugins/automation/spec/triggers/post_created_edited_spec.rb b/plugins/automation/spec/triggers/post_created_edited_spec.rbindex 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" doendend- 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 doautomation.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" dobefore doautomation.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" dolist =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" dolist =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)endend+ 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" dobefore doautomation.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" dolist =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)endend- 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" dolist =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)+ endendend