A platform for community discussion. Free, open, simple.
FEATURE: Add post deleted and restored workflow triggers
Régis Hanolc89a26fbb49f
diff --git a/plugins/discourse-workflows/USE_CASES.md b/plugins/discourse-workflows/USE_CASES.mdindex a7e2a7c0c8a..0e0537647d5 100644--- a/plugins/discourse-workflows/USE_CASES.md+++ b/plugins/discourse-workflows/USE_CASES.md@@ -51,6 +51,9 @@ For each run, record:| `badge-grant` | `when a TL1 user posts in General with the word helpful, grant them the Basic badge` | `trigger:post_created -> condition:filter -> action:badge` | Uses declarative filter; resolves badge; no Code node. || `group-add` | `when a TL1 user creates a topic in Support, add them to the helpers group` | `trigger:topic_created -> condition:filter -> action:group` | Uses `$json.post.username`; resolves group; no Code node. || `tag-group-add` | `every week, add the current-campaign tag to the Marketing tag group` | `trigger:schedule -> action:tag_group` | Resolves the tag group; creates the tag if needed; no Code node. |+| `deleted-post-chat` | `when a post in Support is deleted, message General chat with the post title` | `trigger:post_destroyed -> action:send_chat_message` | Uses `trigger:post_destroyed`; remembers `$json.user` is whoever deleted the post, not the author. |+| `mirror-post-delete` | `when a post in Support is deleted, delete post 123` | `trigger:post_destroyed -> action:post (delete)` | Uses `action:post` with `operation: delete`, not `action:flag_post` with a review-delete flag type. |+| `restore-mirror-post` | `when a deleted post in Support is restored, restore post 123` | `trigger:post_recovered -> action:post (recover)` | Uses `action:post` with `operation: recover`. || `http-request-warning` | `when a new topic is created in Support, send the topic title to https://example.com/webhook` | `trigger:topic_created -> action:http_request` | Includes external HTTP risk; validates URL/method/body; no Code unless necessary. |## Known edge cases to keep testing@@ -64,5 +67,7 @@ For each run, record:- Condition builder entries must use `leftValue` and `rightValue`; `left`/`right` will not execute correctly.- Connections leaving `condition:filter` or `condition:if` use `connection_type: "main"`; select the passing branch with `output_index: 0` and the rejected branch with `output_index: 1`.- Group membership checks should use `action:group` with `operation: check_membership` and the resolved `group_id` instead of Code nodes. Branch with `condition:if` on `$json.group_membership.in_group` when different member and non-member paths are needed.+- Deleting a post is `action:post` with `operation: delete`; `action:flag_post` with `review_delete` is for moderation flows that also need a reviewable. Restoring one is `operation: recover`.+- `trigger:post_destroyed` and `trigger:post_recovered` expose `$json.user` as the acting moderator, not the post author; use `$json.post.username` for the author.- DM/personal-message notifications should use `action:send_personal_message` instead of chat or topic reply nodes.- Forum `search`/`read` should not be used for node/schema discovery; use `workflow_node_catalog` and `workflow_validate_patch`.diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex 42c073c1097..afc6e26c8cd 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -218,6 +218,8 @@ en:"trigger:post_created": "Post created""trigger:post_edited": "Post edited""trigger:post_moved": "Post moved"+ "trigger:post_destroyed": "Post deleted"+ "trigger:post_recovered": "Post recovered""trigger:topic_created": "Topic created""trigger:topic_category_changed": "Topic category changed""trigger:topic_tag_changed": "Topic tag changed"@@ -279,6 +281,8 @@ en:"trigger:post_created": "Fires when a new post or reply is created""trigger:post_edited": "Fires after the first post in a topic is edited and cooked""trigger:post_moved": "Fires when a post is moved to another topic"+ "trigger:post_destroyed": "Fires when a post is deleted. The user is whoever deleted it, not the author."+ "trigger:post_recovered": "Fires when a deleted post is restored. The user is whoever restored it, not the author.""trigger:topic_created": "Fires when a new topic is created""trigger:topic_category_changed": "Fires when a topic is moved to a different category""trigger:topic_tag_changed": "Fires when tags are added to or removed from a topic"@@ -302,7 +306,7 @@ en:"action:tag_group": "Add or remove tags in a tag group""action:code": "Run custom JavaScript code""action:topic": "Create, retrieve, list, close, archive, or set custom fields on topics"- "action:post": "Create, edit, retrieve, or list posts"+ "action:post": "Create, edit, delete, recover, retrieve, or list posts""action:flag_post": "Flag a post as spam or add it to the review queue, optionally hiding or deleting the post and silencing its author""action:send_personal_message": "Send a personal message to users or groups""action:set_fields": "Set or modify data fields in the workflow context"@@ -524,6 +528,8 @@ en:edit: "Edit post"get: "Get post"list: "List posts"+ delete: "Delete post"+ recover: "Recover post"topic_id: "Topic ID"raw: "Body"raw_description: "Raw content for the post"@@ -847,6 +853,15 @@ en:category_ids_description: "Only trigger for posts in these categories. Leave empty to match all categories."tag_names: "Tags"tag_names_description: "Only trigger for posts in topics that have at least one of these tags"+ post_lifecycle:+ topic_type: "Topic type"+ topic_type_description: "Choose whether topics, personal messages, or both should trigger the workflow"+ topic_type_all: "All"+ topic_type_topics: "Topics"+ topic_type_personal_messages: "Personal messages"+ category_ids_description: "Only trigger for posts in these categories. Leave empty to match all categories."+ tag_names: "Tags"+ tag_names_description: "Only trigger for posts in topics that have at least one of these tags"post_edited:category_ids_description: "Only trigger for posts in these categories. Leave empty to match all categories."tag_names: "Tags"diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.ymlindex 4cdc89b948c..52f1f3d309b 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -183,6 +183,8 @@ en:execution_failed: "Called workflow finished with status '%{status}'."execution_failed_with_error: "Called workflow finished with status '%{status}': %{error}"payload_too_large: "Workflow call payload exceeds the maximum size of %{max} bytes."+ post:+ destroy_context: "Deleted by a workflow"flag_post:flagged_by_workflow: "Flagged by the '%{workflow_name}' workflow."user:diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rbindex e2b04b20f03..254f1168603 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb@@ -139,6 +139,22 @@ module DiscourseWorkflowsauthor_username: "system",},},+ {+ name: "Delete the trigger post",+ parameters: {+ operation: "delete",+ post_id: "={{ $json.post.id }}",+ actor_username: "system",+ },+ },+ {+ name: "Restore a deleted post",+ parameters: {+ operation: "recover",+ post_id: "={{ $json.post.id }}",+ actor_username: "system",+ },+ },],"action:send_personal_message" => [{@@ -247,6 +263,8 @@ module DiscourseWorkflows"review queue flag flagged spam moderation pending needs approval queued post akismet","trigger:badge_granted" => "badge award achievement medal granted earned","trigger:tag_created" => "taxonomy label keyword created new",+ "trigger:post_destroyed" => "post deleted removed destroyed trashed",+ "trigger:post_recovered" => "post recovered restored undeleted untrashed","action:user" =>"user profile bio title trust level lock groups fields lookup edit update","action:flag_post" =>diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rbindex 79ee11371f6..c08005be7ee 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb@@ -8,6 +8,10 @@ module DiscourseWorkflowsWORKFLOW_VERSION_ID_FIELD = "discourse_workflows_workflow_version_id"NODE_ID_FIELD = "discourse_workflows_node_id"+ # PostDestroyer leaves an author-deleted post as a stub unless this is below 1;+ # a workflow delete should always actually delete+ ALWAYS_TRASH = 0+MISSING = ParameterResolver::MISSINGRUN_CODE = CodeRunner::RUN_CODERUN_ONCE_FOR_ALL_ITEMS = CodeRunner::RUN_ONCE_FOR_ALL_ITEMS@@ -357,6 +361,7 @@ module DiscourseWorkflowsraw: raw,reply_to_post_number: reply_to_post_number.presence,skip_workflows: true,+ skip_rate_limits: true,}.compactif ActiveModel::Type::Boolean.new.cast(whisper)@@ -385,7 +390,9 @@ module DiscourseWorkflowspost = ::Post.find(post_id)raise Discourse::InvalidAccess if !user.guardian.can_edit_post?(post)- if !PostRevisor.new(post).revise!(user, { raw: raw }, skip_workflows: true)+ opts = { skip_workflows: true, force_new_version: true }++ if !PostRevisor.new(post).revise!(user, { raw: raw }, opts)errors = post.errors.full_messages.presenceraise DiscourseWorkflows::NodeError,errors&.join(", ") || I18n.t("discourse_workflows.errors.post.edit_failed")@@ -394,6 +401,22 @@ module DiscourseWorkflowspost.reloadend+ def destroy_post(user:, post_id:)+ post = ::Post.find(post_id)+ raise Discourse::InvalidAccess if !user.guardian.can_delete_post_or_topic?(post)++ post_destroyer(user, post).destroy+ post+ end++ def recover_post(user:, post_id:)+ post = ::Post.with_deleted.find(post_id)+ raise Discourse::InvalidAccess if !user.guardian.can_recover_post?(post)++ post_destroyer(user, post).recover+ post+ end+def serialize_post(post,guardian: Discourse.system_user.guardian,@@ -492,6 +515,16 @@ module DiscourseWorkflowsprivate+ def post_destroyer(user, post)+ PostDestroyer.new(+ user,+ post,+ context: I18n.t("discourse_workflows.post.destroy_context"),+ skip_workflows: true,+ delete_removed_posts_after: ALWAYS_TRASH,+ )+ end+def record_permission_bypass!(post)post.custom_fields[BYPASSED_PERMISSION_CHECKS_FIELD] = "true"post.custom_fields[WORKFLOW_ID_FIELD] = @workflow.id if @workflow&.iddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rbindex d859cb86357..8342a916ff7 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb@@ -4,7 +4,7 @@ module DiscourseWorkflowsmodule Nodesmodule Postclass V1 < NodeType- OPERATIONS = %w[create edit get list].freeze+ OPERATIONS = %w[create edit get list delete recover].freezeSTATUS_OPTIONS = %w[anyopen@@ -135,7 +135,7 @@ module DiscourseWorkflowsrequired: true,display_options: {show: {- operation: %w[edit get],+ operation: %w[edit get delete recover],},},},@@ -314,7 +314,7 @@ module DiscourseWorkflows},display_options: {show: {- operation: %w[get list],+ operation: %w[get list delete recover],},},},@@ -344,7 +344,6 @@ module DiscourseWorkflows"bypass_permission_checks" =>exec_ctx.get_node_parameter("bypass_permission_checks", item_index, default: false),"post_id" => exec_ctx.get_node_parameter("post_id", item_index),- "editor_username" => exec_ctx.get_node_parameter("editor_username", item_index),"include_raw" => exec_ctx.get_node_parameter("include_raw", item_index, default: true),"include_cooked" =>exec_ctx.get_node_parameter("include_cooked", item_index, default: false),@@ -386,6 +385,10 @@ module DiscourseWorkflowswrap(get_post(exec_ctx, config, item_index))when "list"list_posts(exec_ctx, config, item_index).map { |data| wrap(data) }+ when "delete"+ wrap(delete_post(exec_ctx, config, item_index))+ when "recover"+ wrap(recover_post(exec_ctx, config, item_index))elseraise_node_error!(I18n.t(@@ -434,6 +437,20 @@ module DiscourseWorkflows}end+ def delete_post(exec_ctx, config, item_index)+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)+ post = exec_ctx.destroy_post(user: actor, post_id: config["post_id"])++ { post: exec_ctx.serialize_post(post, guardian: actor.guardian) }+ end++ def recover_post(exec_ctx, config, item_index)+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)+ post = exec_ctx.recover_post(user: actor, post_id: config["post_id"])++ { post: exec_ctx.serialize_post(post, guardian: actor.guardian) }+ end+def get_post(exec_ctx, config, item_index)post = ::Post.find(config["post_id"])actor = exec_ctx.actor_from_parameter("actor_username", item_index)diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_destroyed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_destroyed/v1.rbnew file mode 100644index 00000000000..16f7ea53c50--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_destroyed/v1.rb@@ -0,0 +1,25 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module PostDestroyed+ class V1 < NodeType+ include PostLifecycle++ description(+ name: "trigger:post_destroyed",+ version: "1.0",+ defaults: {+ icon: "trash-can",+ color: "deep-orange",+ },+ group: "discourse_triggers",+ event: :post_destroyed,+ i18n_scope: PostLifecycle::I18N_SCOPE,+ output_contracts: PostLifecycle::OUTPUT_CONTRACTS,+ properties: PostLifecycle::PROPERTIES,+ )+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rbnew file mode 100644index 00000000000..12492458b36--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rb@@ -0,0 +1,87 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module PostLifecycle+ I18N_SCOPE = "post_lifecycle"+ PROPERTIES = {+ topic_type: {+ type: :options,+ required: true,+ default: "topics",+ options: NodeType::TOPIC_TYPE_OPTIONS,+ },+ category_ids: {+ type: :array,+ required: false,+ ui: {+ control: :category,+ multiple: true,+ },+ },+ include_subcategories: {+ type: :boolean,+ required: false,+ default: true,+ ui: {+ control: :checkbox,+ },+ display_options: {+ show: {+ category_ids: [{ condition: { exists: true } }],+ },+ },+ },+ tag_names: {+ type: :string,+ required: false,+ ui: {+ control: :tags,+ },+ },+ }.freeze++ OUTPUT_CONTRACTS = [+ {+ schema:+ Schema.merge(Schema::POST_SCHEMA, Schema::TOPIC_LIST_ITEM_SCHEMA, Schema::USER_SCHEMA),+ },+ ].freeze++ def initialize(post, opts = nil, user = nil, *)+ super(parameters: {})+ @post = post+ @opts = opts+ @user = user+ end++ def valid?+ @post.present? && @post.post_type == ::Post.types[:regular] &&+ !@opts&.dig(:skip_workflows) && topic.present?+ end++ def output+ { post: serialize_post(@post), topic: topic_data(topic), user: serialize_user(@user) }+ end++ def matches?(trigger_ctx)+ matches_topic_type?(topic, trigger_ctx.get_node_parameter("topic_type", "topics")) &&+ matches_category_ids?(+ topic.category_id,+ category_ids_parameter(trigger_ctx),+ include_subcategories: trigger_ctx.get_node_parameter("include_subcategories", true),+ ) &&+ matches_tags?(topic, normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))+ end++ private++ def topic+ return @topic if defined?(@topic)++ @topic =+ @post.association(:topic).target || ::Topic.with_deleted.find_by(id: @post.topic_id)+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v1.rbnew file mode 100644index 00000000000..fc60b1f71ef--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v1.rb@@ -0,0 +1,25 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module PostRecovered+ class V1 < NodeType+ include PostLifecycle++ description(+ name: "trigger:post_recovered",+ version: "1.0",+ defaults: {+ icon: "arrow-rotate-right",+ color: "green",+ },+ group: "discourse_triggers",+ event: :post_recovered,+ i18n_scope: PostLifecycle::I18N_SCOPE,+ output_contracts: PostLifecycle::OUTPUT_CONTRACTS,+ properties: PostLifecycle::PROPERTIES,+ )+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/spec/integration/post_destroyed_delete_post_spec.rb b/plugins/discourse-workflows/spec/integration/post_destroyed_delete_post_spec.rbnew file mode 100644index 00000000000..21f80cc7dd8--- /dev/null+++ b/plugins/discourse-workflows/spec/integration/post_destroyed_delete_post_spec.rb@@ -0,0 +1,45 @@+# frozen_string_literal: true++RSpec.describe "Workflow: post destroyed -> delete post" do+ fab!(:admin)+ fab!(:author) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:source_post) { create_post(user: author, raw: "Source post") }+ fab!(:mirror_post) { create_post(user: author, raw: "Mirror post") }++ before do+ graph =+ build_workflow_graph do |g|+ g.node "trigger-1", "trigger:post_destroyed"+ g.node "action-1",+ "action:post",+ configuration: {+ "operation" => "delete",+ "post_id" => mirror_post.id.to_s,+ "actor_username" => "system",+ }+ g.chain "trigger-1", "action-1"+ end++ Fabricate(+ :discourse_workflows_workflow,+ created_by: admin,+ published: true,+ name: "Delete the mirror post",+ **graph,+ )+ end++ it "deletes the mirror post without enqueueing another workflow run" do+ PostDestroyer.new(admin, source_post).destroy++ expect(Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.size).to eq(1)++ job_args = Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.last["args"].first.symbolize_keys++ expect { Jobs::DiscourseWorkflows::ExecuteWorkflow.new.execute(job_args) }.to change {+ mirror_post.reload.deleted_at+ }.from(nil)++ expect(Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.size).to eq(1)+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rbindex 06e57a508a0..458b3eee7ad 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb@@ -367,6 +367,8 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog do)expect(post_node[:examples]).to contain_exactly(include(parameters: include(operation: "create", topic_id: "={{ $json.topic.id }}")),+ include(parameters: include(operation: "delete", post_id: "={{ $json.post.id }}")),+ include(parameters: include(operation: "recover", post_id: "={{ $json.post.id }}")),)expect(group_node[:examples]).to contain_exactly(include(diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rbindex 39bcbc60076..a3b4fc09829 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb@@ -210,6 +210,27 @@ RSpec.describe DiscourseWorkflows::EventListener doexpect(enqueued_trigger_node_ids).not_to include("post-edited-trigger")end+ it "enqueues post destroyed workflows only when the deletion does not skip them" do+ post = create_post(category: category)+ skipped = create_post(category: category)+ create_published_workflow("post-destroyed-trigger", "trigger:post_destroyed")++ PostDestroyer.new(admin, skipped, skip_workflows: true).destroy+ PostDestroyer.new(admin, post).destroy++ expect(enqueued_trigger_node_ids).to contain_exactly("post-destroyed-trigger")+ end++ it "enqueues post recovered workflows from the post recovered event" do+ post = create_post(category: category)+ PostDestroyer.new(admin, post).destroy+ create_published_workflow("post-recovered-trigger", "trigger:post_recovered")++ PostDestroyer.new(admin, post.reload).recover++ expect(enqueued_trigger_node_ids).to include("post-recovered-trigger")+ end+it "only enqueues reviewable approved workflows matching the reviewable type" docreate_published_workflow("matching-trigger",diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_lifecycle_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_lifecycle_spec.rbnew file mode 100644index 00000000000..cdd270808a8--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_lifecycle_spec.rb@@ -0,0 +1,91 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::PostLifecycle do+ subject(:trigger) { klass.new(post, {}, actor) }++ fab!(:category)+ fab!(:tag) { Fabricate(:tag, name: "availability") }+ fab!(:topic) { Fabricate(:topic, category: category) }+ fab!(:first_post) { Fabricate(:post, topic: topic, raw: "Topic opener") }+ fab!(:post) { Fabricate(:post, topic: topic, raw: "Out next week") }+ fab!(:small_action) { Fabricate(:post, topic: topic, post_type: Post.types[:small_action]) }+ fab!(:actor, :admin)++ let(:klass) { DiscourseWorkflows::Nodes::PostDestroyed::V1 }++ before do+ SiteSetting.tagging_enabled = true+ topic.tags << tag+ end++ def trigger_context(parameters)+ DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters })+ end++ describe "#valid?" do+ it "accepts a regular post, including once it is deleted" do+ expect(trigger).to be_valid++ PostDestroyer.new(actor, post).destroy++ expect(klass.new(post.reload, {}, actor)).to be_valid+ end++ it "rejects a missing post, a non-regular post, and workflow-driven changes",+ :aggregate_failures do+ expect(klass.new(nil, {}, actor)).not_to be_valid+ expect(klass.new(small_action, {}, actor)).not_to be_valid+ expect(klass.new(post, { skip_workflows: true }, actor)).not_to be_valid+ end+ end++ describe "#output" do+ it "returns the post, its topic, and the acting user", :aggregate_failures do+ output = trigger.output++ expect(output[:post]).to include(+ id: post.id,+ raw: post.raw,+ topic_id: topic.id,+ username: post.user.username,+ category_id: category.id,+ tags: ["availability"],+ )+ expect(output[:topic]).to include(id: topic.id, title: topic.title)+ expect(output[:user]).to include(id: actor.id, username: actor.username)+ expect(output).to match_node_output_schema(klass)+ end++ it "keeps topic-derived post fields when the topic went down with the post",+ :aggregate_failures do+ PostDestroyer.new(actor, first_post).destroy++ output = klass.new(first_post.reload, {}, actor).output++ expect(output[:post]).to include(+ topic_title: topic.title,+ category_id: category.id,+ post_url: Post.url(topic.slug, topic.id, first_post.post_number),+ tags: ["availability"],+ )+ end+ end++ describe "#matches?" do+ it "matches the configured category and tags" do+ expect(+ trigger.matches?(+ trigger_context("category_ids" => [category.id.to_s], "tag_names" => [tag.name]),+ ),+ ).to eq(true)+ end++ it "excludes personal messages unless they are asked for", :aggregate_failures do+ pm_trigger = klass.new(Fabricate(:private_message_post), {}, actor)++ expect(pm_trigger.matches?(trigger_context({}))).to eq(false)+ expect(pm_trigger.matches?(trigger_context("topic_type" => "personal_messages"))).to eq(true)+ expect(trigger.matches?(trigger_context("topic_type" => "personal_messages"))).to eq(false)+ end+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rbindex 635ecb65899..239fed8ff83 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_spec.rb@@ -44,6 +44,29 @@ RSpec.describe DiscourseWorkflows::Nodes::Post::V1 do)end+ it "creates as an author who just posted, without tripping their rate limit" do+ author = Fabricate(:user, refresh_auto_groups: true, trust_level: TrustLevel[4])+ topic = Fabricate(:post, user: user, raw: "First post", post_number: 1).topic++ RateLimiter.enable+ SiteSetting.rate_limit_create_post = 5+ PostCreator.create!(author, topic_id: topic.id, raw: "The author's own post")++ expect do+ execute_node(+ configuration: {+ "operation" => "create",+ "topic_id" => topic.id.to_s,+ "raw" => "Mirrored moments later",+ "author_username" => author.username,+ },+ item: item,+ )+ end.to change { topic.posts.count }.by(1)++ expect(topic.posts.order(:id).last.user_id).to eq(author.id)+ end+it "raises when creating a post as the anonymous actor" dofirst_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)topic = first_post.topic@@ -349,6 +372,95 @@ RSpec.describe DiscourseWorkflows::Nodes::Post::V1 do)end+ it "deletes a post as the configured actor" do+ post = Fabricate(:post, user: user, raw: "Original post")+ topic_title = post.topic.title+ result = nil++ expect do+ result =+ execute_node(+ configuration: {+ "operation" => "delete",+ "post_id" => post.id.to_s,+ "actor_username" => admin.username,+ },+ item: item,+ )+ end.to change { post.reload.deleted_at }.from(nil)++ expect(post.deleted_by_id).to eq(admin.id)+ expect(result["post"]).to include(+ "id" => post.id,+ "topic_id" => post.topic_id,+ "topic_title" => topic_title,+ )+ end++ it "really deletes even when the actor is the post author" do+ post = Fabricate(:post, user: user, raw: "Original post")+ reply = Fabricate(:post, topic: post.topic, user: user)++ execute_node(+ configuration: {+ "operation" => "delete",+ "post_id" => reply.id.to_s,+ "actor_username" => user.username,+ },+ item: item,+ )++ expect(reply.reload.deleted_at).to be_present+ end++ it "recovers a deleted post as the configured actor" do+ post = Fabricate(:post, user: user, raw: "Original post")+ PostDestroyer.new(admin, post).destroy+ result = nil++ expect do+ result =+ execute_node(+ configuration: {+ "operation" => "recover",+ "post_id" => post.id.to_s,+ "actor_username" => admin.username,+ },+ item: item,+ )+ end.to change { post.reload.deleted_at }.to(nil)++ expect(result["post"]).to include("id" => post.id, "raw" => "Original post")+ end++ it "raises when the actor may not delete or recover the post", :aggregate_failures do+ post = Fabricate(:post, user: admin, raw: "Original post")++ expect do+ execute_node(+ configuration: {+ "operation" => "delete",+ "post_id" => post.id.to_s,+ "actor_username" => other_user.username,+ },+ item: item,+ )+ end.to raise_error(Discourse::InvalidAccess)++ PostDestroyer.new(admin, post).destroy++ expect do+ execute_node(+ configuration: {+ "operation" => "recover",+ "post_id" => post.id.to_s,+ "actor_username" => other_user.username,+ },+ item: item,+ )+ end.to raise_error(Discourse::InvalidAccess)+ end+it "edits a post for the configured editor" dopost = Fabricate(:post, user: user, raw: "Original post", post_number: 1)result = nil@@ -397,6 +509,24 @@ RSpec.describe DiscourseWorkflows::Nodes::Post::V1 doexpect(hidden_post.reload.raw).not_to eq("Hidden edit")end+ it "records a separate revision for each edit" do+ post = Fabricate(:post, user: user, raw: "Original post")++ expect do+ 2.times do+ execute_node(+ configuration: {+ "operation" => "edit",+ "post_id" => post.id.to_s,+ "raw" => "Workflow edit #{SecureRandom.hex(4)}",+ "editor_username" => admin.username,+ },+ item: item,+ )+ end+ end.to change { post.reload.version }.by(2)+ end+it "gets a visible post with selected body fields" dopost = Fabricate(:post, user: user, raw: "Visible post body", post_number: 1)diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rbindex c4a7f8de3be..7563def9822 100644--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb@@ -47,7 +47,7 @@ RSpec.describe DiscourseWorkflows::NodeTypesController doexpect(properties["operation"]).to include("type" => "options","default" => "create",- "options" => %w[create edit get list],+ "options" => %w[create edit get list delete recover],)expect(properties["query"]).to include("type" => "string",diff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/node_type/list_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/node_type/list_spec.rbindex 1c8ab6fc260..6ea6de76a61 100644--- a/plugins/discourse-workflows/spec/services/discourse_workflows/node_type/list_spec.rb+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/node_type/list_spec.rb@@ -66,7 +66,7 @@ RSpec.describe DiscourseWorkflows::NodeType::List doexpect(node_type.dig(:properties, :operation, :default)).to eq("create")expect(node_type.dig(:properties, :limit)).to include(default: 30, min: 1, max: 800)expect(node_type[:operations].map { |operation| operation[:value] }).to eq(- %w[create edit get list],+ %w[create edit get list delete recover],)end