A platform for community discussion. Free, open, simple.
FEATURE: adds edit post operation to the post node (#40825)
Joffrey JAFFEUX86dd515474d2
diff --git a/db/structure.sql b/db/structure.sqlindex ac4aaee428b..ff4ad99360f 100644--- a/db/structure.sql+++ b/db/structure.sql@@ -21913,6 +21913,7 @@ ALTER TABLE ONLY public.ad_plugin_house_ads_groupsSET search_path TO "$user", public;INSERT INTO "schema_migrations" (version) VALUES+('20260612092612'),('20260610075829'),('20260609050938'),('20260607161322'),diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex f4486e4b148..002f082846e 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -145,7 +145,6 @@ en:"action:code": "Code""action:topic": "Topic""action:post": "Post"- "action:create_post": "Create post""action:set_fields": "Set fields""trigger:stale_topic": "Stale topic""trigger:schedule": "Schedule"@@ -183,8 +182,7 @@ en:"action:topic_tags": "Add or remove tags on a topic""action:code": "Run custom JavaScript code""action:topic": "Create, retrieve, list, close, or set custom fields on topics"- "action:post": "Create, retrieve, or list posts"- "action:create_post": "Create a reply in an existing topic"+ "action:post": "Create, edit, retrieve, or list posts""action:set_fields": "Set or modify data fields in the workflow context""trigger:stale_topic": "Fires when a topic has had no activity for a period""trigger:schedule": "Fires on a recurring schedule"@@ -268,17 +266,11 @@ en:badge_id: "Badge"badge_id_placeholder: "Select a badge"actor_username: "Performed by user"- create_post:- topic_id: "Topic ID"- raw: "Body"- raw_description: "Raw content for the reply"- reply_to_post_number: "Reply to post number"- reply_to_post_number_description: "Reply to a specific post number in the topic"- author_username: "Author"post:operation: "Operation"operations:create: "Create post"+ edit: "Edit post"get: "Get post"list: "List posts"topic_id: "Topic ID"@@ -288,6 +280,7 @@ en:reply_to_post_number_description: "Reply to a specific post number in the topic"author_username: "Author"post_id: "Post ID"+ editor_username: "Editor"include_raw: "Include raw"include_cooked: "Include cooked"query: "Query"diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.ymlindex ef008bd132c..67fadc5a2b7 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -37,12 +37,12 @@ en:item:inconsistent_format_title: "Inconsistent item format"inconsistent_format: "Every returned item must use the same format."- create_post:- topic_closed_or_archived: "Cannot create a post in a closed or archived topic."topic:create_failed: "Failed to create topic."unknown_operation: "Unknown operation: %{operation}."post:+ topic_closed_or_archived: "Cannot create a post in a closed or archived topic."+ edit_failed: "Failed to edit post."unknown_operation: "Unknown operation: %{operation}."invalid_filter: "Invalid post filter fragment: %{fragments}."credential:diff --git a/plugins/discourse-workflows/db/migrate/20260612092612_merge_create_post_workflow_node_into_post.rb b/plugins/discourse-workflows/db/migrate/20260612092612_merge_create_post_workflow_node_into_post.rbnew file mode 100644index 00000000000..9b11cccd532--- /dev/null+++ b/plugins/discourse-workflows/db/migrate/20260612092612_merge_create_post_workflow_node_into_post.rb@@ -0,0 +1,104 @@+# frozen_string_literal: true++class MergeCreatePostWorkflowNodeIntoPost < ActiveRecord::Migration[8.0]+ def up+ migrate_node_arrays("discourse_workflows_workflows", "nodes")+ migrate_node_arrays("discourse_workflows_workflow_versions", "nodes")+ migrate_snapshot_nodes("discourse_workflows_execution_data", "workflow_data")+ migrate_snapshot_nodes("discourse_workflows_webhooks", "workflow_snapshot")++ execute <<~SQL+ UPDATE discourse_workflows_workflow_dependencies+ SET dependency_key = 'action:post'+ WHERE dependency_type = 'node_type'+ AND dependency_key = 'action:create_post'+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end++ private++ def migrate_node_arrays(table_name, column_name)+ execute <<~SQL+ UPDATE #{table_name}+ SET #{column_name} = (+ SELECT COALESCE(+ jsonb_agg(#{transformed_node_sql} ORDER BY nodes.ordinality),+ '[]'::jsonb+ )+ FROM jsonb_array_elements(#{table_name}.#{column_name}) WITH ORDINALITY AS nodes(node, ordinality)+ )+ WHERE jsonb_typeof(#{column_name}) = 'array'+ AND #{nodes_need_migration_sql(column_name)}+ SQL+ end++ def migrate_snapshot_nodes(table_name, column_name)+ execute <<~SQL+ UPDATE #{table_name}+ SET #{column_name} = jsonb_set(+ #{column_name},+ '{nodes}',+ (+ SELECT COALESCE(+ jsonb_agg(#{transformed_node_sql} ORDER BY nodes.ordinality),+ '[]'::jsonb+ )+ FROM jsonb_array_elements(#{table_name}.#{column_name}->'nodes') WITH ORDINALITY AS nodes(node, ordinality)+ ),+ false+ )+ WHERE jsonb_typeof(#{column_name}->'nodes') = 'array'+ AND #{nodes_need_migration_sql("#{column_name}->'nodes'")}+ SQL+ end++ def transformed_node_sql+ <<~SQL.squish+ CASE+ WHEN nodes.node->>'type' = 'action:create_post' THEN+ nodes.node || jsonb_build_object(+ 'type', 'action:post',+ 'parameters', #{node_parameters_sql} || jsonb_build_object('operation', 'create')+ )+ WHEN nodes.node->>'type' = 'action:post'+ AND NOT (#{node_parameters_sql} ? 'operation') THEN+ nodes.node || jsonb_build_object(+ 'parameters', #{node_parameters_sql} || jsonb_build_object('operation', 'list')+ )+ ELSE nodes.node+ END+ SQL+ end++ def node_parameters_sql+ <<~SQL.squish+ CASE+ WHEN jsonb_typeof(nodes.node->'parameters') = 'object' THEN nodes.node->'parameters'+ ELSE '{}'::jsonb+ END+ SQL+ end++ def nodes_need_migration_sql(nodes_expression)+ <<~SQL.squish+ EXISTS (+ SELECT 1+ FROM jsonb_array_elements(#{nodes_expression}) AS existing_nodes(node)+ WHERE existing_nodes.node->>'type' = 'action:create_post'+ OR (+ existing_nodes.node->>'type' = 'action:post'+ AND NOT (+ CASE+ WHEN jsonb_typeof(existing_nodes.node->'parameters') = 'object' THEN existing_nodes.node->'parameters'+ ELSE '{}'::jsonb+ END ? 'operation'+ )+ )+ )+ SQL+ end+enddiff --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 e2d4fa19a36..f6bbe5608f9 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@@ -295,7 +295,7 @@ module DiscourseWorkflowsif topic.closed? || topic.archived?raise DiscourseWorkflows::NodeError,- I18n.t("discourse_workflows.errors.create_post.topic_closed_or_archived")+ I18n.t("discourse_workflows.errors.post.topic_closed_or_archived")endpost_args = {@@ -308,6 +308,19 @@ module DiscourseWorkflowsPostCreator.new(user, post_args).create!end+ def edit_post(user:, post_id:, raw:)+ post = ::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)+ errors = post.errors.full_messages.presence+ raise DiscourseWorkflows::NodeError,+ errors&.join(", ") || I18n.t("discourse_workflows.errors.post.edit_failed")+ end++ post.reload+ end+def serialize_post(post,guardian: Discourse.system_user.guardian,diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/create_post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/create_post/v1.rbdeleted file mode 100644index 3e7dc81347a..00000000000--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/create_post/v1.rb+++ /dev/null@@ -1,78 +0,0 @@-# frozen_string_literal: true--module DiscourseWorkflows- module Nodes- module CreatePost- class V1 < NodeType- description(- name: "action:create_post",- version: "1.0",- defaults: {- icon: "reply",- color: "teal",- },- group: "discourse_actions",- capabilities: {- run_scope: "per_item",- },- properties: {- topic_id: {- type: :string,- required: true,- },- raw: {- type: :string,- required: true,- ui: {- control: :textarea,- },- },- reply_to_post_number: {- type: :integer,- required: false,- },- author_username: {- type: :string,- required: false,- default: "system",- ui: {- control: :user,- },- },- },- )-- def execute(exec_ctx)- items =- exec_ctx.input_items.map.with_index do |_item, item_index|- config = {- "topic_id" => exec_ctx.get_node_parameter("topic_id", item_index),- "raw" => exec_ctx.get_node_parameter("raw", item_index),- "reply_to_post_number" =>- exec_ctx.get_node_parameter("reply_to_post_number", item_index),- }-- wrap(process(exec_ctx, config, item_index))- end-- [items]- end-- private-- def process(exec_ctx, config, item_index)- author = exec_ctx.actor_from_parameter("author_username", item_index)- post =- exec_ctx.create_post(- user: author,- raw: config["raw"],- topic_id: config["topic_id"],- reply_to_post_number: config["reply_to_post_number"],- )-- { post: exec_ctx.serialize_post(post, guardian: author.guardian, include_cooked: true) }- end- end- end- end-enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rbindex 2a8c49e2bd3..ad1d57bc6e7 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 get list].freeze+ OPERATIONS = %w[create edit get list].freezeSTATUS_OPTIONS = %w[any open closed archived noreplies single_user].freezePOST_TYPE_OPTIONS = %w[all first reply].freezeORDER_OPTIONS = %w[latest oldest latest_topic oldest_topic likes].freeze@@ -46,7 +46,7 @@ module DiscourseWorkflowstype: :options,required: true,options: OPERATIONS,- default: "list",+ default: "create",},topic_id: {type: :string,@@ -65,7 +65,7 @@ module DiscourseWorkflows},display_options: {show: {- operation: ["create"],+ operation: %w[create edit],},},},@@ -96,7 +96,20 @@ module DiscourseWorkflowsrequired: true,display_options: {show: {- operation: ["get"],+ operation: %w[edit get],+ },+ },+ },+ editor_username: {+ type: :string,+ required: false,+ default: "system",+ ui: {+ control: :user,+ },+ display_options: {+ show: {+ operation: ["edit"],},},},@@ -271,12 +284,13 @@ module DiscourseWorkflowsdef config_for(exec_ctx, item_index){- "operation" => exec_ctx.get_node_parameter("operation", item_index, default: "list"),+ "operation" => exec_ctx.get_node_parameter("operation", item_index, default: "create"),"topic_id" => exec_ctx.get_node_parameter("topic_id", item_index),"raw" => exec_ctx.get_node_parameter("raw", item_index),"reply_to_post_number" =>exec_ctx.get_node_parameter("reply_to_post_number", item_index),"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),@@ -310,6 +324,8 @@ module DiscourseWorkflowscase config["operation"]when "create"wrap(create_post(exec_ctx, config, item_index))+ when "edit"+ wrap(edit_post(exec_ctx, config, item_index))when "get"wrap(get_post(exec_ctx, config, item_index))when "list"@@ -345,6 +361,21 @@ module DiscourseWorkflows}end+ def edit_post(exec_ctx, config, item_index)+ editor = exec_ctx.actor_from_parameter("editor_username", item_index)+ post = exec_ctx.edit_post(user: editor, post_id: config["post_id"], raw: config["raw"])++ {+ post:+ exec_ctx.serialize_post(+ post,+ guardian: editor.guardian,+ include_raw: true,+ include_cooked: true,+ ),+ }+ 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_edited/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rbindex 271d7b7b717..f86a236af83 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rb@@ -21,6 +21,7 @@ module DiscourseWorkflowscolor: "violet",},group: "discourse_triggers",+ events: [:post_edited],properties: {post_scope: {type: :options,@@ -50,14 +51,16 @@ module DiscourseWorkflows},)- def initialize(post, cooked, *)+ def initialize(post, topic_changed_or_cooked = nil, revisor = nil, *)super(parameters: {})@post = post- @cooked = cooked+ @cooked = topic_changed_or_cooked.is_a?(String) ? topic_changed_or_cooked : post&.cooked+ @revisor = revisorenddef valid?- @post.present? && @post.topic.present? && @post.post_type == ::Post.types[:regular]+ @post.present? && @post.topic.present? && @post.post_type == ::Post.types[:regular] &&+ !@revisor&.opts&.dig(:skip_workflows)enddef outputdiff --git a/plugins/discourse-workflows/plugin.rb b/plugins/discourse-workflows/plugin.rbindex e79db644c31..c94f875a4b6 100644--- a/plugins/discourse-workflows/plugin.rb+++ b/plugins/discourse-workflows/plugin.rb@@ -85,16 +85,6 @@ after_initialize doself,)- Plugin::Filter.register(:after_post_cook) do |post, cooked|- DiscourseWorkflows::EventListener.handle(- DiscourseWorkflows::Nodes::PostEdited::V1,- post,- cooked,- )-- cooked- end-add_to_serializer :site,:topic_admin_button_workflows,include_condition: -> { scope.is_admin? } dodiff --git a/plugins/discourse-workflows/spec/integration/post_created_create_post_spec.rb b/plugins/discourse-workflows/spec/integration/post_created_create_post_spec.rbindex 921440919da..f87b614f0cf 100644--- a/plugins/discourse-workflows/spec/integration/post_created_create_post_spec.rb+++ b/plugins/discourse-workflows/spec/integration/post_created_create_post_spec.rb@@ -14,8 +14,9 @@ RSpec.describe "Workflow: post created -> create post" dobuild_workflow_graph do |g|g.node "trigger-1", "trigger:post_created"g.node "action-1",- "action:create_post",+ "action:post",configuration: {+ "operation" => "create","topic_id" => "={{ $trigger.topic.id }}","raw" => "Automated reply",}diff --git a/plugins/discourse-workflows/spec/integration/topic_category_changed_create_post_spec.rb b/plugins/discourse-workflows/spec/integration/topic_category_changed_create_post_spec.rbindex 8e8784ad88a..a4a291b495a 100644--- a/plugins/discourse-workflows/spec/integration/topic_category_changed_create_post_spec.rb+++ b/plugins/discourse-workflows/spec/integration/topic_category_changed_create_post_spec.rb@@ -13,8 +13,9 @@ RSpec.describe "Workflow: topic category changed -> create post" dobuild_workflow_graph do |g|g.node "trigger-1", "trigger:topic_category_changed"g.node "action-1",- "action:create_post",+ "action:post",configuration: {+ "operation" => "create","topic_id" => "={{ $trigger.topic.id }}","raw" =>"=Category changed from {{ $trigger.old_category_id }} to {{ $trigger.topic.category_id }}",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 6acdcd29b2e..98ae8bb4ee5 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@@ -168,6 +168,24 @@ RSpec.describe DiscourseWorkflows::EventListener doexpect(enqueued_trigger_node_ids).not_to include("trust-level-mismatch", "tag-mismatch")end+ it "enqueues post edited workflows from the post edited event" do+ post = create_post(user: Fabricate(:user, trust_level: TrustLevel[1]), category: category)+ create_published_workflow("post-edited-trigger", "trigger:post_edited")++ PostRevisor.new(post).revise!(admin, raw: "Edited by admin")++ expect(enqueued_trigger_node_ids).to include("post-edited-trigger")+ end++ it "does not enqueue post edited workflows when the revision skips workflows" do+ post = create_post(user: Fabricate(:user, trust_level: TrustLevel[1]), category: category)+ create_published_workflow("post-edited-trigger", "trigger:post_edited")++ PostRevisor.new(post).revise!(admin, { raw: "Edited by workflow" }, skip_workflows: true)++ expect(enqueued_trigger_node_ids).not_to include("post-edited-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/executor/node_execution_context_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rbindex c726555c21a..bfd71fff7f8 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb@@ -607,7 +607,7 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do"actor_username" => "={{ $json.actor }}",},resolver: resolver,- node_identifier: "action:create_post",+ node_identifier: "action:post",)expect(ctx.actor_from_parameter("actor_username")).to eq(user)@@ -616,7 +616,7 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext dofield: "actor_username",item_index: 0,source: :expression,- purpose: "action:create_post",+ purpose: "action:post",)ensureresolver&.dispose@@ -714,6 +714,34 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext doendend+ describe "#edit_post" do+ fab!(:admin)+ fab!(:user)+ fab!(:post) { Fabricate(:post, user: user, raw: "Original post") }++ it "edits a post as the provided user" do+ ctx = described_class.new(input_items: [], resolver: nil)++ edited_post = ctx.edit_post(user: admin, post_id: post.id, raw: "Edited body")++ expect(edited_post).to eq(post)+ expect(post.reload.raw).to eq("Edited body")+ end++ it "requires the user to edit the post" do+ group = Fabricate(:group)+ private_category = Fabricate(:private_category, group: group)+ hidden_post = create_post(user: admin, category: private_category)+ ctx = described_class.new(input_items: [], resolver: nil)++ expect do+ ctx.edit_post(user: user, post_id: hidden_post.id, raw: "Hidden edit")+ end.to raise_error(Discourse::InvalidAccess)++ expect(hidden_post.reload.raw).not_to eq("Hidden edit")+ end+ end+describe "#serialize_post" dofab!(:user)fab!(:post) { Fabricate(:post, user: user, raw: "Workflow post body") }diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_create_post_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_create_post_spec.rbindex c11cb20339a..3973aede3f1 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_create_post_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_create_post_spec.rb@@ -12,9 +12,10 @@ RSpec.describe DiscourseWorkflows::Executor dobuild_workflow_graph do |g|g.node "trigger-1", "trigger:manual"g.node "action-1",- "action:create_post",+ "action:post",name: "Create Post",configuration: {+ "operation" => "create","topic_id" => "={{ $trigger.topic_id }}","raw" => "={{ $trigger.raw }}","reply_to_post_number" => "={{ $trigger.reply_to_post_number }}",diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/create_post_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/create_post_spec.rbdeleted file mode 100644index 775ca7e5872..00000000000--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/create_post_spec.rb+++ /dev/null@@ -1,193 +0,0 @@-# frozen_string_literal: true--RSpec.describe DiscourseWorkflows::Nodes::CreatePost::V1 do- fab!(:admin)- fab!(:topic_owner) { Fabricate(:user, refresh_auto_groups: true) }- fab!(:first_post) { create_post(user: topic_owner, raw: "First post") }- fab!(:topic) { first_post.topic }-- describe "#execute" do- let(:item) { { "json" => {} } }-- it "creates a reply for the configured user" do- result = nil-- expect do- result =- execute_node(- configuration: {- "topic_id" => topic.id.to_s,- "raw" => "Workflow reply",- "author_username" => admin.username,- },- item: item,- )- end.to change { topic.posts.count }.by(1)-- reply = topic.posts.order(:id).last-- expect(reply.raw).to eq("Workflow reply")- expect(reply.user_id).to eq(admin.id)- expect(result["post"]).to include(- "id" => reply.id,- "topic_id" => topic.id,- "topic_title" => topic.title,- "topic_slug" => topic.slug,- "post_number" => reply.post_number,- "post_url" => reply.url,- "raw" => "Workflow reply",- "cooked" => reply.cooked,- "user_id" => admin.id,- "username" => admin.username,- "category_id" => topic.category_id,- "tags" => [],- )- end-- it "falls back to the system user when no user is configured" do- execute_node(- configuration: {- "topic_id" => topic.id.to_s,- "raw" => "Created by workflows",- },- item: item,- )-- expect(topic.posts.order(:id).last.user_id).to eq(Discourse.system_user.id)- end-- it "creates a reply to a specific post number" do- execute_node(- configuration: {- "topic_id" => topic.id.to_s,- "raw" => "Threaded reply",- "reply_to_post_number" => first_post.post_number.to_s,- },- item: item,- )-- expect(topic.posts.order(:id).last.reply_to_post_number).to eq(first_post.post_number)- end-- it "raises when the author_username cannot be found" do- expect do- execute_node(- configuration: {- "topic_id" => topic.id.to_s,- "raw" => "Workflow reply",- "author_username" => "nonexistent_user",- },- item: item,- )- end.to raise_error(DiscourseWorkflows::NodeError, "User 'nonexistent_user' not found")- end-- it "raises when the author cannot see the topic" do- group = Fabricate(:group)- category = Fabricate(:private_category, group: group)- hidden_topic = create_post(user: admin, category: category).topic- author = Fabricate(:user)-- expect do- execute_node(- configuration: {- "topic_id" => hidden_topic.id.to_s,- "raw" => "Workflow reply",- "author_username" => author.username,- },- item: item,- )- end.to raise_error(Discourse::InvalidAccess).and not_change { hidden_topic.posts.count }- end-- it "raises when the topic is closed" do- topic.update!(closed: true)-- expect do- execute_node(- configuration: {- "topic_id" => topic.id.to_s,- "raw" => "Workflow reply",- },- item: item,- )- end.to raise_error(- DiscourseWorkflows::NodeError,- /Cannot create a post in a closed or archived topic/,- )- end-- it "raises when the topic is archived" do- topic.update!(archived: true)-- expect do- execute_node(- configuration: {- "topic_id" => topic.id.to_s,- "raw" => "Workflow reply",- },- item: item,- )- end.to raise_error(- DiscourseWorkflows::NodeError,- /Cannot create a post in a closed or archived topic/,- )- end-- it "raises when topic does not exist" do- expect do- execute_node(configuration: { "topic_id" => "-1", "raw" => "Workflow reply" }, item: item)- end.to raise_error(ActiveRecord::RecordNotFound)- end-- it "resolves expressions for each input item" do- config = {- "topic_id" => "={{ $trigger.topic_id }}",- "raw" => "=Hello {{ $json.name }}",- "reply_to_post_number" => "={{ $trigger.reply_to_post_number }}",- "author_username" => "={{ $trigger.author_username }}",- }- resolver_context = {- "$json" => {- "name" => "Ada",- },- "$trigger" => {- "topic_id" => topic.id,- "reply_to_post_number" => first_post.post_number,- "author_username" => admin.username,- },- }- sandbox = DiscourseWorkflows::JsSandbox.new(resolver_context)- resolver = DiscourseWorkflows::ExpressionResolver.new(resolver_context, sandbox: sandbox)- result =- described_class.new(parameters: config).execute(- DiscourseWorkflows::Executor::NodeExecutionContext.new(- input_items: [{ "json" => { "name" => "Ada" } }],- node_context: {- },- resolver: resolver,- parameters: config,- property_schema: described_class.property_schema,- ),- )[- 0- ]-- reply = topic.posts.order(:id).last-- expect(reply.raw).to eq("Hello Ada")- expect(reply.reply_to_post_number).to eq(first_post.post_number)- expect(reply.user_id).to eq(admin.id)- expect(result.first["json"]["post"]).to include(- "id" => reply.id,- "topic_id" => topic.id,- "post_number" => reply.post_number,- "raw" => "Hello Ada",- "cooked" => reply.cooked,- )- ensure- resolver&.dispose- sandbox&.dispose- end- end-enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v1_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v1_spec.rbindex edeb49ad3af..651bef48850 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v1_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v1_spec.rb@@ -33,6 +33,13 @@ RSpec.describe DiscourseWorkflows::Nodes::PostEdited::V1 doexpect(trigger).not_to be_validend++ it "returns false when workflow execution requested the edit to be skipped" do+ revisor = instance_double(PostRevisor, opts: { skip_workflows: true })+ trigger = described_class.new(first_post, false, revisor)++ expect(trigger).not_to be_valid+ endenddescribe "#output" dodiff --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 28c280a29fe..d6568fad46f 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,159 @@ RSpec.describe DiscourseWorkflows::Nodes::Post::V1 do)end+ it "defaults to creating a post" do+ first_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)+ topic = first_post.topic++ expect do+ execute_node(+ configuration: {+ "topic_id" => topic.id.to_s,+ "raw" => "Default create reply",+ "author_username" => admin.username,+ },+ item: item,+ )+ end.to change { topic.posts.count }.by(1)++ expect(topic.posts.order(:id).last.raw).to eq("Default create reply")+ end++ it "falls back to the system user when no author is configured" do+ first_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)+ topic = first_post.topic++ execute_node(+ configuration: {+ "operation" => "create",+ "topic_id" => topic.id.to_s,+ "raw" => "Created by workflows",+ },+ item: item,+ )++ expect(topic.posts.order(:id).last.user_id).to eq(Discourse.system_user.id)+ end++ it "creates a reply to a specific post number" do+ first_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)+ topic = first_post.topic++ execute_node(+ configuration: {+ "operation" => "create",+ "topic_id" => topic.id.to_s,+ "raw" => "Threaded reply",+ "reply_to_post_number" => first_post.post_number.to_s,+ },+ item: item,+ )++ expect(topic.posts.order(:id).last.reply_to_post_number).to eq(first_post.post_number)+ end++ it "raises when the create author cannot be found" do+ first_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)++ expect do+ execute_node(+ configuration: {+ "operation" => "create",+ "topic_id" => first_post.topic_id.to_s,+ "raw" => "Workflow reply",+ "author_username" => "nonexistent_user",+ },+ item: item,+ )+ end.to raise_error(DiscourseWorkflows::NodeError, "User 'nonexistent_user' not found")+ end++ it "raises when the create author cannot see the topic" do+ group = Fabricate(:group)+ private_category = Fabricate(:private_category, group: group)+ hidden_topic = create_post(user: admin, category: private_category).topic++ expect do+ execute_node(+ configuration: {+ "operation" => "create",+ "topic_id" => hidden_topic.id.to_s,+ "raw" => "Workflow reply",+ "author_username" => user.username,+ },+ item: item,+ )+ end.to raise_error(Discourse::InvalidAccess).and not_change { hidden_topic.posts.count }+ end++ it "raises when creating in a closed or archived topic" do+ first_post = Fabricate(:post, user: user, raw: "First post", post_number: 1)+ topic = first_post.topic+ topic.update!(closed: true)++ expect do+ execute_node(+ configuration: {+ "operation" => "create",+ "topic_id" => topic.id.to_s,+ "raw" => "Workflow reply",+ },+ item: item,+ )+ end.to raise_error(+ DiscourseWorkflows::NodeError,+ /Cannot create a post in a closed or archived topic/,+ )+ end++ it "edits a post for the configured editor" do+ post = Fabricate(:post, user: user, raw: "Original post", post_number: 1)+ result = nil++ expect do+ result =+ execute_node(+ configuration: {+ "operation" => "edit",+ "post_id" => post.id.to_s,+ "raw" => "Edited by workflow",+ "editor_username" => admin.username,+ },+ item: item,+ )+ end.not_to change { Post.count }++ post.reload+ expect(post.raw).to eq("Edited by workflow")+ expect(result["post"]).to include(+ "id" => post.id,+ "topic_id" => post.topic_id,+ "username" => user.username,+ "raw" => "Edited by workflow",+ "cooked" => post.cooked,+ )+ end++ it "raises when the editor cannot edit the post" do+ group = Fabricate(:group)+ private_category = Fabricate(:private_category, group: group)+ hidden_post = create_post(user: admin, category: private_category)++ expect do+ execute_node(+ configuration: {+ "operation" => "edit",+ "post_id" => hidden_post.id.to_s,+ "raw" => "Hidden edit",+ "editor_username" => user.username,+ },+ item: item,+ )+ end.to raise_error(Discourse::InvalidAccess)++ expect(hidden_post.reload.raw).not_to eq("Hidden edit")+ 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 8c69c2c846a..c95f0e2df3d 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@@ -26,12 +26,13 @@ RSpec.describe DiscourseWorkflows::NodeTypesController doexpect(identifiers).to include("trigger:topic_closed")expect(identifiers).to include("action:topic_tags")- expect(identifiers).to include("action:create_post")+ expect(identifiers).to include("action:post")expect(identifiers).to include("action:topic")expect(identifiers).to include("condition:if")+ expect(identifiers).not_to include("action:create_post")end- it "returns List posts query control metadata" do+ it "returns Post operation and List posts query control metadata" doget "/admin/plugins/discourse-workflows/node-types.json"post_node =@@ -40,10 +41,20 @@ RSpec.describe DiscourseWorkflows::NodeTypesController doendproperties = post_node["properties"]+ expect(properties["operation"]).to include(+ "type" => "options",+ "default" => "create",+ "options" => %w[create edit get list],+ )expect(properties["query"]).to include("type" => "string","ui" => include("control" => "filter_query", "filter" => "posts"),)+ expect(properties["editor_username"]).to include(+ "type" => "string",+ "default" => "system",+ "ui" => include("control" => "user"),+ )expect(properties["categories"]["ui"]).to include("hidden" => true)expect(properties["advanced_filter"]["ui"]).to include("hidden" => true)enddiff --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 d8e61da611f..8a260916091 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@@ -26,14 +26,15 @@ RSpec.describe DiscourseWorkflows::NodeType::List doexpect(identifiers).to include("trigger:topic_closed","action:topic_tags",- "action:create_post",+ "action:post","action:topic","condition:if",)+ expect(identifiers).not_to include("action:create_post")endit "includes expected schema fields for each node type" do- node_type = result[:node_types].find { |nt| nt[:identifier] == "action:create_post" }+ node_type = result[:node_types].find { |nt| nt[:identifier] == "action:post" }expect(node_type).to include(:displayName,:name,@@ -45,18 +46,28 @@ RSpec.describe DiscourseWorkflows::NodeType::List do:credentials,:webhooks,)- expect(node_type[:name]).to eq("action:create_post")+ expect(node_type[:name]).to eq("action:post")expect(node_type[:kind]).to eq("action")- expect(node_type[:properties].keys).to contain_exactly(- :topic_id,+ expect(node_type[:properties].keys).to include(+ :operation,:raw,+ :topic_id,+ :post_id,:reply_to_post_number,:author_username,+ :editor_username,+ :include_raw,+ :include_cooked,+ :query,+ )+ expect(node_type.dig(:properties, :operation, :default)).to eq("create")+ expect(node_type[:operations].map { |operation| operation[:value] }).to eq(+ %w[create edit get list],)endit "returns UI hints for schema-driven configurators" do- node_type = result[:node_types].find { |nt| nt[:identifier] == "action:create_post" }+ node_type = result[:node_types].find { |nt| nt[:identifier] == "action:post" }expect(node_type.dig(:properties, :raw, :ui)).to eq(control: :textarea)end@@ -93,10 +104,10 @@ RSpec.describe DiscourseWorkflows::NodeType::List doendit "serializes ui palette group for action nodes" do- create_post = result[:node_types].find { |nt| nt[:identifier] == "action:create_post" }+ post = result[:node_types].find { |nt| nt[:identifier] == "action:post" }form = result[:node_types].find { |nt| nt[:identifier] == "action:form" }- expect(create_post.dig(:ui, :palette_group, :id)).to eq("discourse_actions")+ expect(post.dig(:ui, :palette_group, :id)).to eq("discourse_actions")expect(form.dig(:ui, :palette_group, :id)).to eq("human_review")enddiff --git a/plugins/discourse-workflows/spec/system/expression_editor_spec.rb b/plugins/discourse-workflows/spec/system/expression_editor_spec.rbindex f13c6b63c87..8c728c7d2b7 100644--- a/plugins/discourse-workflows/spec/system/expression_editor_spec.rb+++ b/plugins/discourse-workflows/spec/system/expression_editor_spec.rb@@ -13,7 +13,7 @@ RSpec.describe "Workflow Expression Editor" doeditor_page.click_empty_state_add_nodeeditor_page.select_node_type("trigger:topic_created")editor_page.click_add_node- editor_page.select_node_type("action:create_post")+ editor_page.select_node_type("action:post", operation: "create")editor_page.double_click_node(1)enddiff --git a/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb b/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rbindex 5a1ca5411bf..5d608a23951 100644--- a/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb+++ b/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb@@ -73,7 +73,7 @@ module PageObjects"action:topic_tags" => "Topic tags","action:code" => "Code","action:topic" => "Topic",- "action:create_post" => "Create post",+ "action:post" => "Post","action:set_fields" => "Set fields","action:split_out" => "Split Out","action:http_request" => "HTTP Request",@@ -107,6 +107,12 @@ module PageObjects"get" => "Get topic","list" => "List topics",},+ "action:post" => {+ "create" => "Create post",+ "edit" => "Edit post",+ "get" => "Get post",+ "list" => "List posts",+ },}.freezedef select_node_type(identifier, operation: nil)diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-data-schema-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-data-schema-test.jsindex 16329bb0646..d4efff2d280 100644--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-data-schema-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-data-schema-test.js@@ -288,9 +288,9 @@ module("Unit | lib | discourse-workflows | data-schema", function () {test("schemaFieldsForNodeInput previews connected upstream output before the current node succeeds", function (assert) {const currentNode = {- id: "create-post",- name: "Create post",- type: "action:create_post",+ id: "post",+ name: "Post",+ type: "action:post",};const sourceNode = {id: "template",@@ -312,10 +312,10 @@ module("Unit | lib | discourse-workflows | data-schema", function () {],},],- "Create post": [+ Post: [{- node_id: "create-post",- node_type: "action:create_post",+ node_id: "post",+ node_type: "action:post",status: "skipped",inputs: [{@@ -330,7 +330,7 @@ module("Unit | lib | discourse-workflows | data-schema", function () {};assert.deepEqual(- schemaFieldsForNodeInput(runData, "Create post", {+ schemaFieldsForNodeInput(runData, "Post", {node: currentNode,sourceNode,outputIndex: 0,