A platform for community discussion. Free, open, simple.
FEATURE: Add close operation to workflow topic node (#40475)
Joffrey JAFFEUX42454c12f100
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex 7e44e47a937..ecc7608b38f 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -175,7 +175,7 @@ en:"condition:filter": "Continue only if conditions are met""action:topic_tags": "Add or remove tags on a topic""action:code": "Run custom JavaScript code"- "action:topic": "Create, retrieve, or list topics"+ "action:topic": "Create, retrieve, list, or close topics""action:post": "Create, retrieve, or list posts""action:create_post": "Create a reply in an existing topic""action:set_fields": "Set or modify data fields in the workflow context"@@ -337,6 +337,7 @@ en:create: "Create topic"get: "Get topic"list: "List topics"+ close: "Close topic"topic_id: "Topic ID"title: "Title"raw: "Body"diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rbindex e4be4bfeb15..c6bdf714310 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb@@ -4,7 +4,7 @@ module DiscourseWorkflowsmodule Nodesmodule Topicclass V1 < NodeType- OPERATIONS = %w[create get list].freeze+ OPERATIONS = %w[create get list close].freezeMAX_LIMIT = 100DEFAULT_LIMIT = 30@@ -31,7 +31,7 @@ module DiscourseWorkflowsrequired: true,display_options: {show: {- operation: ["get"],+ operation: %w[get close],},},},@@ -155,6 +155,8 @@ module DiscourseWorkflowswrap(get_topic(exec_ctx, config, item_index))when "list"list_topics(exec_ctx, config, item_index).map { |data| wrap(data) }+ when "close"+ wrap(close_topic(exec_ctx, config, item_index))elseraise_node_error!(I18n.t(@@ -220,6 +222,16 @@ module DiscourseWorkflowstopics.map { |topic| { topic: topic_data(topic, actor.guardian) } }end+ def close_topic(exec_ctx, config, item_index)+ topic = ::Topic.find(config["topic_id"])+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)+ actor.guardian.ensure_can_close_topic!(topic)++ topic.update_status("closed", true, actor)++ { topic: topic_data(topic.reload, actor.guardian) }+ end+def topic_data(topic, guardian)serialize_record(topic, TopicListItemSerializer, scope: guardian)enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rbindex 84cb11b483f..8fa354b078b 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb@@ -370,5 +370,40 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 doexpect(result.length).to eq(0)endend++ context "with operation 'close'" do+ fab!(:topic) { Fabricate(:topic, category: category, user: user) }+ fab!(:post) { Fabricate(:post, topic: topic, user: user) }++ it "closes the topic as the configured actor" do+ result =+ execute_node(+ configuration: {+ "operation" => "close",+ "topic_id" => topic.id.to_s,+ "actor_username" => admin.username,+ },+ item: item,+ )++ expect(topic.reload).to be_closed+ expect(result["topic"]).to include("id" => topic.id, "closed" => true)+ end++ it "raises when actor_username cannot close the topic" do+ expect do+ execute_node(+ configuration: {+ "operation" => "close",+ "topic_id" => topic.id.to_s,+ "actor_username" => user.username,+ },+ item: item,+ )+ end.to raise_error(Discourse::InvalidAccess)++ expect(topic.reload).not_to be_closed+ end+ endendend