A platform for community discussion. Free, open, simple.
FEATURE: Expose topic status changes as workflow building blocks
Régis Hanol4e01a6f3631c
diff --git a/app/models/topic.rb b/app/models/topic.rbindex e527d204efe..33779f63aad 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -939,7 +939,6 @@ class Topic < ActiveRecord::Basedef update_status(status, enabled, user, opts = {})TopicStatusUpdater.new(self, user).update!(status, enabled, opts)- DiscourseEvent.trigger(:topic_status_updated, self, status, enabled)if status == "closed"StaffActionLogger.new(user).log_topic_closed(self, closed: enabled)diff --git a/app/services/topic_status_updater.rb b/app/services/topic_status_updater.rbindex 5d613141666..cb2a2ba1068 100644--- a/app/services/topic_status_updater.rb+++ b/app/services/topic_status_updater.rb@@ -4,16 +4,27 @@ TopicStatusUpdater =Struct.new(:topic, :user) dodef update!(status, enabled, opts = {})status = Status.new(status, enabled)+ previous_pin = topic.slice(:pinned_at, :pinned_globally, :pinned_until) if status.pinned? ||+ status.pinned_globally?@topic_timer = topic.public_topic_timer- updated = nil- Topic.transaction do+ Topic.transaction(requires_new: true) doupdated = change(status, opts)create_moderator_post_for(status, opts) if updated- end- updated+ changed = previous_pin ? previous_pin != topic.slice(*previous_pin.keys) : updated+ event_status = status.name+ if previous_pin && status.disabled?+ event_status = previous_pin["pinned_globally"] ? "pinned_globally" : "pinned"+ end++ if changed+ DiscourseEvent.trigger(:topic_status_updated, topic, event_status, status.enabled?)+ end++ updated+ endendprivatediff --git a/plugins/discourse-workflows/USE_CASES.md b/plugins/discourse-workflows/USE_CASES.mdindex 8c4f35459d0..00cd5ff67c6 100644--- a/plugins/discourse-workflows/USE_CASES.md+++ b/plugins/discourse-workflows/USE_CASES.md@@ -59,6 +59,7 @@ For each run, record:## Known edge cases to keep testing- `action:topic_tags` version `2.0` defaults to `mode: modify`: `add_tag_names` and `remove_tag_names` apply together, preserving unrelated tags and allowing implicit parent tags. `mode: replace` sets the complete tag set from `replace_tag_names`; an empty value clears all tags. Replacement uses the current topic state under a lock and rejects category or parent-tag rules that change the requested final set. Only the selected mode's fields are evaluated; invalid names and expression errors stop the action before any tag changes. Both modes enforce the actor’s permission to edit the topic’s tags and return the final `tag_names`; rejected changes neither persist nor emit tag-change events. Existing tag names remain usable after the maximum tag length is reduced. Version `1.0` retains `operation` and `tag_names`.+- Topic status triggers are separate nodes: `topic_closed`, `topic_reopened`, `topic_archived`, `topic_unarchived`, `topic_listed`, `topic_unlisted`, `topic_pinned`, `topic_unpinned`, `topic_pinned_globally`, and `topic_unpinned_globally` (all prefixed with `trigger:`). They share category and tag filters. Closing includes automatic closes; unpin events use the previous pin scope. Workflow and AI actions use the same status updater.- Topic-only triggers that need author/post fields should use `action:topic` get before filtering or messaging.- Generic prompts like "when someone posts" should use `trigger:post_created` for all regular posts; do not ask whether to include replies unless the prompt explicitly narrows the scope.- Actions that replace item JSON require downstream nodes to use the action output schema, not the original trigger schema.diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex 7caaea2975b..6f0ebb118be 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -214,7 +214,17 @@ en:one: "%{count} item"other: "%{count} items"nodes:+ "trigger:topic_status_changed": "Topic status changed""trigger:topic_closed": "Topic closed"+ "trigger:topic_reopened": "Topic reopened"+ "trigger:topic_archived": "Topic archived"+ "trigger:topic_unarchived": "Topic unarchived"+ "trigger:topic_listed": "Topic listed"+ "trigger:topic_unlisted": "Topic unlisted"+ "trigger:topic_pinned": "Topic pinned"+ "trigger:topic_unpinned": "Topic unpinned"+ "trigger:topic_pinned_globally": "Topic pinned globally"+ "trigger:topic_unpinned_globally": "Topic unpinned globally""trigger:post_created": "Post created""trigger:post_edited": "Post edited""trigger:post_moved": "Post moved"@@ -277,7 +287,17 @@ en:"action:topic_moderation": "Topic moderation""action:flag_user": "Flag user"node_descriptions:+ "trigger:topic_status_changed": "Fires when a topic is closed, reopened, archived, listed, or pinned, or those changes are reversed""trigger:topic_closed": "Fires when a topic is closed by a user or auto-close timer"+ "trigger:topic_reopened": "Fires when a topic is reopened"+ "trigger:topic_archived": "Fires when a topic is archived"+ "trigger:topic_unarchived": "Fires when a topic is unarchived"+ "trigger:topic_listed": "Fires when a topic is listed"+ "trigger:topic_unlisted": "Fires when a topic is unlisted"+ "trigger:topic_pinned": "Fires when a topic is pinned in its category"+ "trigger:topic_unpinned": "Fires when a topic is unpinned from its category"+ "trigger:topic_pinned_globally": "Fires when a topic is pinned globally"+ "trigger:topic_unpinned_globally": "Fires when a globally pinned topic is unpinned""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"@@ -853,7 +873,20 @@ en:category_ids_description: "Only consider topics in these categories. Leave empty to match all categories."tag_names: "Tags"tag_names_description: "Only consider topics that have at least one of these tags"- topic_closed:+ topic_status_changed:+ statuses: "Status changes"+ statuses_description: "Leave empty to match any status change."+ statuses_placeholder: "Any status change"+ statuses_closed: "Closed"+ statuses_reopened: "Reopened"+ statuses_archived: "Archived"+ statuses_unarchived: "Unarchived"+ statuses_listed: "Listed"+ statuses_unlisted: "Unlisted"+ statuses_pinned: "Pinned"+ statuses_unpinned: "Unpinned"+ statuses_pinned_globally: "Pinned globally"+ statuses_unpinned_globally: "Unpinned globally"tag_names: "Tags"tag_names_description: "Only trigger for topics that have at least one of these tags"post_created:diff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rbindex 74cdd20317b..cde504959fc 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb@@ -391,6 +391,14 @@ module DiscourseWorkflowsself.class.category_ids_parameter(trigger_ctx)end+ def matches_topic_filters?(topic, trigger_ctx)+ 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+def matches_category_ids?(topic_category_id, category_ids, include_subcategories: true)return true if category_ids.empty?diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_archived/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_archived/v1.rbnew file mode 100644index 00000000000..ce2f40f4b4e--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_archived/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicArchived+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("archived")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rbindex 7da1d81401c..6b98777b986 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rb@@ -3,46 +3,20 @@module DiscourseWorkflowsmodule Nodesmodule TopicClosed- class V1 < NodeType+ class V1 < TopicStatusChanged::V1description(- name: "trigger:topic_closed",- version: "1.0",- defaults: {- icon: "lock",- color: "grey",- },- group: "discourse_triggers",- event: :topic_status_updated,- output_contracts: [{ schema: Schema::TOPIC_LIST_ITEM_SCHEMA }],- properties: {- **CATEGORY_FILTER_PROPERTIES,- **TAG_FILTER_PROPERTIES,- },+ TopicStatusChanged::V1.description_for_change("closed").merge(+ defaults: {+ icon: "lock",+ color: "grey",+ },+ output_contracts: [{ schema: Schema::TOPIC_LIST_ITEM_SCHEMA }],+ ),)- def initialize(topic, status, enabled)- super(parameters: {})- @topic = topic- @status = status- @enabled = enabled- end-- def valid?- @status.to_s == "closed" && @enabled- end-def output{ topic: topic_data(@topic) }end-- def matches?(trigger_ctx)- 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")))- endendendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_listed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_listed/v1.rbnew file mode 100644index 00000000000..08f05b9b1a8--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_listed/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicListed+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("listed")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pinned/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pinned/v1.rbnew file mode 100644index 00000000000..4d888f28f85--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pinned/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicPinned+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("pinned")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pinned_globally/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pinned_globally/v1.rbnew file mode 100644index 00000000000..e64bedec695--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pinned_globally/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicPinnedGlobally+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("pinned_globally")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_reopened/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_reopened/v1.rbnew file mode 100644index 00000000000..6d9c773ec2b--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_reopened/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicReopened+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("reopened")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_status_changed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_status_changed/v1.rbnew file mode 100644index 00000000000..41db5b0bbb1--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_status_changed/v1.rb@@ -0,0 +1,99 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicStatusChanged+ class V1 < NodeType+ STATUS_CHANGES = {+ "closed" => %w[reopened closed],+ "archived" => %w[unarchived archived],+ "visible" => %w[unlisted listed],+ "pinned" => %w[unpinned pinned],+ "pinned_globally" => %w[unpinned_globally pinned_globally],+ }.freeze++ OUTPUT_SCHEMA =+ Schema.merge(+ Schema::TOPIC_LIST_ITEM_SCHEMA,+ Schema.document(+ "status" => {+ "type" => "string",+ },+ "enabled" => {+ "type" => "boolean",+ },+ "change" => {+ "type" => "string",+ },+ ),+ ).freeze++ description(+ name: "trigger:topic_status_changed",+ version: "1.0",+ defaults: {+ icon: "arrows-rotate",+ color: "grey",+ },+ group: "discourse_triggers",+ palette_visible: false,+ event: :topic_status_updated,+ output_contracts: [{ schema: OUTPUT_SCHEMA }],+ properties: {+ statuses: {+ type: :multi_options,+ default: [],+ options: STATUS_CHANGES.values.flatten,+ },+ **CATEGORY_FILTER_PROPERTIES,+ **TAG_FILTER_PROPERTIES,+ },+ )++ def self.description_for_change(change)+ description.merge(+ name: "trigger:topic_#{change}",+ palette_visible: true,+ fixed_change: change,+ i18n_scope: "topic_status_changed",+ properties: properties.except(:statuses),+ )+ end++ def initialize(topic, status, enabled)+ super(parameters: {})+ @topic = topic+ @status = status.to_s == "autoclosed" ? "closed" : status.to_s+ @enabled = enabled+ end++ def valid?+ fixed_change = self.class.description[:fixed_change]+ @topic.present? && STATUS_CHANGES.key?(@status) &&+ (fixed_change.nil? || fixed_change == change)+ end++ def output+ { topic: topic_data(@topic), status: @status, enabled: @enabled, change: change }+ end++ def matches?(trigger_ctx)+ statuses =+ Array.wrap(+ self.class.description[:fixed_change] ||+ trigger_ctx.get_node_parameter("statuses", []),+ ).compact_blank+ return false if statuses.present? && statuses.exclude?(change)++ matches_topic_filters?(@topic, trigger_ctx)+ end++ private++ def change+ STATUS_CHANGES.fetch(@status)[@enabled ? 1 : 0]+ end+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unarchived/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unarchived/v1.rbnew file mode 100644index 00000000000..cc9f98a4f2e--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unarchived/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicUnarchived+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("unarchived")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unlisted/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unlisted/v1.rbnew file mode 100644index 00000000000..7e9734226e8--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unlisted/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicUnlisted+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("unlisted")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unpinned/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unpinned/v1.rbnew file mode 100644index 00000000000..76faea570a8--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unpinned/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicUnpinned+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("unpinned")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unpinned_globally/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unpinned_globally/v1.rbnew file mode 100644index 00000000000..0423acc92e8--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_unpinned_globally/v1.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicUnpinnedGlobally+ class V1 < TopicStatusChanged::V1+ description TopicStatusChanged::V1.description_for_change("unpinned_globally")+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/spec/integration/status_event_triggers_spec.rb b/plugins/discourse-workflows/spec/integration/status_event_triggers_spec.rbnew file mode 100644index 00000000000..f897aa00d83--- /dev/null+++ b/plugins/discourse-workflows/spec/integration/status_event_triggers_spec.rb@@ -0,0 +1,63 @@+# frozen_string_literal: true++RSpec.describe "Workflow status event triggers" do+ fab!(:admin)+ fab!(:topic, :topic_with_op)++ before { DiscourseWorkflows::WorkflowDependency.clear_cache! }++ it "dispatches automatic topic closes to the existing and status triggers once" do+ create_workflow("closed", "trigger:topic_closed")+ create_workflow("status", "trigger:topic_status_changed", "statuses" => ["closed"])+ create_workflow("reopened", "trigger:topic_reopened", "statuses" => ["closed"])++ 2.times { TopicStatusUpdater.new(topic, admin).update!("autoclosed", true) }+ 2.times { TopicStatusUpdater.new(topic, admin).update!("closed", false) }++ expect(enqueued_jobs.map { |job| job["trigger_node_id"] }).to contain_exactly(+ "closed",+ "status",+ "reopened",+ )+ expect(+ enqueued_jobs.find { |job| job["trigger_node_id"] == "status" }["trigger_data"],+ ).to include("status" => "closed", "enabled" => true, "change" => "closed")+ expect(+ enqueued_jobs.find { |job| job["trigger_node_id"] == "reopened" }["trigger_data"],+ ).to include("status" => "closed", "enabled" => false, "change" => "reopened")+ end++ it "dispatches unpinning according to the previous pin scope" do+ topic.update_status("pinned_globally", true, admin)+ create_workflow("global", "trigger:topic_unpinned_globally", "statuses" => ["unpinned"])+ create_workflow("category", "trigger:topic_unpinned", "statuses" => ["unpinned_globally"])++ topic.update_status("pinned", false, admin)+ topic.update_status("pinned", true, admin)+ topic.update_status("pinned_globally", false, admin)++ expect(enqueued_jobs.map { |job| job["trigger_node_id"] }).to eq(%w[global category])+ expect(enqueued_jobs.first["trigger_data"]).to include(+ "status" => "pinned_globally",+ "enabled" => false,+ "change" => "unpinned_globally",+ )+ expect(enqueued_jobs.last["trigger_data"]).to include(+ "status" => "pinned",+ "enabled" => false,+ "change" => "unpinned",+ )+ end++ def create_workflow(trigger_id, trigger_type, configuration = {})+ graph =+ build_workflow_graph do |graph_builder|+ graph_builder.node trigger_id, trigger_type, configuration: configuration+ end+ Fabricate(:discourse_workflows_workflow, created_by: admin, published: true, **graph)+ end++ def enqueued_jobs+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.map { |job| job["args"].first }+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_closed_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_closed_spec.rbindex 03d0d3fb593..9a4ab3ae4b5 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_closed_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_closed_spec.rb@@ -9,23 +9,6 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicClosed::V1 dotopic.tags << tagend- describe "#valid?" do- it "returns true when topic is closed" do- trigger = described_class.new(topic, "closed", true)- expect(trigger).to be_valid- end-- it "returns false when topic is reopened" do- trigger = described_class.new(topic, "closed", false)- expect(trigger).not_to be_valid- end-- it "returns false for non-closed status changes" do- trigger = described_class.new(topic, "visible", true)- expect(trigger).not_to be_valid- end- end-describe "#output" doit "returns topic_id and tags" dotrigger = described_class.new(topic, "closed", true)@@ -44,9 +27,10 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicClosed::V1 doexpect(trigger.matches?(trigger_context("category_ids" => []))).to eq(true)end- it "matches topics in any of the configured categories" do+ it "matches topics by category IDs or the legacy category parameter" dotrigger = described_class.new(topic, "closed", true)+ expect(trigger.matches?(trigger_context("category_id" => topic.category_id.to_s))).to eq(true)expect(trigger.matches?(trigger_context("category_ids" => [Fabricate(:category).id.to_s, topic.category_id.to_s]),@@ -57,11 +41,12 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicClosed::V1 do).to eq(false)end- it "matches subcategories by default but not when excluded" do+ it "matches subcategories for current and legacy category filters unless excluded" dosubcategory = Fabricate(:category, parent_category: topic.category)subcategory_topic = Fabricate(:topic, category: subcategory)trigger = described_class.new(subcategory_topic, "closed", true)+ expect(trigger.matches?(trigger_context("category_id" => topic.category_id.to_s))).to eq(true)expect(trigger.matches?(trigger_context("category_ids" => [topic.category_id.to_s]))).to eq(true,)@@ -74,20 +59,6 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicClosed::V1 do),).to eq(false)end-- it "supports the legacy scalar category_id parameter" do- trigger = described_class.new(topic, "closed", true)-- expect(trigger.matches?(trigger_context("category_id" => topic.category_id.to_s))).to eq(true)- end-- it "matches subcategories by default for legacy category_id-only nodes" do- subcategory = Fabricate(:category, parent_category: topic.category)- subcategory_topic = Fabricate(:topic, category: subcategory)- trigger = described_class.new(subcategory_topic, "closed", true)-- expect(trigger.matches?(trigger_context("category_id" => topic.category_id.to_s))).to eq(true)- endenddef trigger_context(parameters)diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_status_changed_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_status_changed_spec.rbnew file mode 100644index 00000000000..d715b54fb26--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_status_changed_spec.rb@@ -0,0 +1,114 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::TopicStatusChanged::V1 do+ TOPIC_STATUS_EVENTS = [+ ["closed", "closed", true],+ ["reopened", "closed", false],+ ["archived", "archived", true],+ ["unarchived", "archived", false],+ ["listed", "visible", true],+ ["unlisted", "visible", false],+ ["pinned", "pinned", true],+ ["unpinned", "pinned", false],+ ["pinned_globally", "pinned_globally", true],+ ["unpinned_globally", "pinned_globally", false],+ ].freeze++ fab!(:category)+ fab!(:topic) { Fabricate(:topic, category: category) }++ it "exposes fixed triggers while retaining the configurable generic trigger" do+ expect(described_class).not_to be_palette_visible+ expect(described_class.properties).to have_key(:statuses)+ expect(DiscourseWorkflows::Registry.find_node_type(described_class.identifier)).to eq(+ described_class,+ )++ TOPIC_STATUS_EVENTS.each do |change, status, enabled|+ node_class = DiscourseWorkflows::Registry.find_node_type("trigger:topic_#{change}")+ expect(node_class).to be_palette_visible+ expect(node_class.properties).not_to have_key(:statuses)+ expect(node_class.i18n_scope).to eq("topic_status_changed")++ trigger = node_class.new(topic, status, enabled)+ output = trigger.output+ expect(output).to match_node_output_schema(node_class)+ if change == "closed"+ expect(output.keys).to eq([:topic])+ else+ expect(output).to include(status: status, enabled: enabled, change: change)+ end++ TOPIC_STATUS_EVENTS.each do |other_change, other_status, other_enabled|+ event = node_class.new(topic, other_status, other_enabled)+ expect(event.valid?).to eq(change == other_change)+ expect(event.matches?(trigger_context("statuses" => [other_change]))).to eq(+ change == other_change,+ )+ end++ expect(trigger.matches?(trigger_context("statuses" => ["unknown"]))).to eq(true)+ expect(trigger.matches?(trigger_context("category_ids" => [category.id]))).to eq(true)+ end+ end++ describe "#valid?" do+ it "accepts supported statuses on existing topics" do+ %w[closed autoclosed archived visible pinned pinned_globally].each do |status|+ expect(described_class.new(topic, status, true)).to be_valid+ end++ expect(described_class.new(topic, "unknown", true)).not_to be_valid+ expect(described_class.new(nil, "closed", true)).not_to be_valid+ end+ end++ describe "#output" do+ it "normalizes automatic closing and reopening" do+ closed_output = described_class.new(topic, "autoclosed", true).output+ reopened_output = described_class.new(topic, "autoclosed", false).output++ expect(closed_output).to include(status: "closed", enabled: true, change: "closed")+ expect(reopened_output).to include(status: "closed", enabled: false, change: "reopened")+ expect(closed_output[:topic][:id]).to eq(topic.id)+ expect(closed_output).to match_node_output_schema(described_class)+ expect(reopened_output).to match_node_output_schema(described_class)+ end+ end++ describe "#matches?" do+ it "matches selected changes including automatic changes" do+ trigger = described_class.new(topic, "autoclosed", false)++ expect(trigger.matches?(trigger_context({}))).to eq(true)+ expect(trigger.matches?(trigger_context("statuses" => ["reopened"]))).to eq(true)+ expect(trigger.matches?(trigger_context("statuses" => ["closed"]))).to eq(false)+ end++ it "matches categories, subcategories, and tags" do+ SiteSetting.tagging_enabled = true+ tag = Fabricate(:tag)+ other_tag = Fabricate(:tag)+ topic.tags << tag+ parent_category = Fabricate(:category)+ topic.category.update!(parent_category: parent_category)+ trigger = described_class.new(topic, "archived", true)++ expect(+ trigger.matches?(+ trigger_context("category_ids" => [parent_category.id], "tag_names" => [tag.name]),+ ),+ ).to eq(true)+ expect(+ trigger.matches?(+ trigger_context("category_ids" => [parent_category.id], "include_subcategories" => false),+ ),+ ).to eq(false)+ expect(trigger.matches?(trigger_context("tag_names" => [other_tag.name]))).to eq(false)+ end+ end++ def trigger_context(parameters)+ DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters })+ end+enddiff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rbindex 17cdc1da106..e6dd0e3fcb4 100644--- a/spec/models/topic_spec.rb+++ b/spec/models/topic_spec.rb@@ -1604,6 +1604,51 @@ RSpec.describe Topic dobefore { user.admin = true }+ it "emits a status event only when the status changes" do+ events =+ DiscourseEvent.track_events(:topic_status_updated) do+ 2.times { topic.update_status("closed", true, user) }+ end++ expect(events.map { |event| event[:params] }).to eq([[topic, "closed", true]])+ end++ it "emits pin status events only when pin attributes change" do+ freeze_time++ events =+ DiscourseEvent.track_events(:topic_status_updated) do+ 2.times { topic.update_status("pinned", false, user) }+ 2.times { topic.update_status("pinned", true, user) }+ 2.times { topic.update_status("pinned", false, user) }+ end++ expect(events.map { |event| event[:params] }).to eq(+ [[topic, "pinned", true], [topic, "pinned", false]],+ )+ end++ it "emits pin status events when re-pinning or changing the pin scope or expiry" do+ freeze_time+ topic.update_status("pinned", true, user)+ freeze_time(1.hour.from_now)++ events =+ DiscourseEvent.track_events(:topic_status_updated) do+ topic.update_status("pinned", true, user)+ topic.update_status("pinned_globally", true, user)+ topic.update_status("pinned_globally", true, user, until: 1.day.from_now.iso8601)+ end++ expect(events.map { |event| event[:params] }).to eq(+ [+ [topic, "pinned", true],+ [topic, "pinned_globally", true],+ [topic, "pinned_globally", true],+ ],+ )+ end+context "with visibility" dolet(:category) { Fabricate(:category_with_definition) }diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rbindex 9fa11196775..c8bfc2fa4e0 100644--- a/spec/models/web_hook_spec.rb+++ b/spec/models/web_hook_spec.rb@@ -196,8 +196,8 @@ RSpec.describe WebHook dopayload = JSON.parse(job_args["payload"])expect(payload["id"]).to eq(topic_id)- %w[archived closed visible].each do |status|- post.topic.update_status(status, true, topic.user)+ { "archived" => true, "closed" => true, "visible" => false }.each do |status, enabled|+ post.topic.update_status(status, enabled, topic.user)job_args = Jobs::EmitWebHookEvent.jobs.last["args"].firstexpect(job_args["event_name"]).to eq("topic_#{status}_status_updated")diff --git a/spec/services/topic_status_updater_spec.rb b/spec/services/topic_status_updater_spec.rbindex 5efcd3d6eca..dbd7c6aa5eb 100644--- a/spec/services/topic_status_updater_spec.rb+++ b/spec/services/topic_status_updater_spec.rb@@ -7,6 +7,27 @@ RSpec.describe TopicStatusUpdater dofab!(:user) { Fabricate(:user, refresh_auto_groups: true) }fab!(:admin)+ it "retries a status change when an event listener fails" do+ topic = create_topic+ handler = proc { raise "Listener unavailable" }+ DiscourseEvent.on(:topic_status_updated, &handler)++ expect do described_class.new(topic, admin).update!("closed", true) end.to raise_error(+ "Listener unavailable",+ )+ expect(topic.reload).not_to be_closed++ DiscourseEvent.off(:topic_status_updated, &handler)+ events =+ DiscourseEvent.track_events(:topic_status_updated) do+ described_class.new(topic, admin).update!("closed", true)+ end++ expect(events.map { |event| event[:params] }).to eq([[topic, "closed", true]])+ ensure+ DiscourseEvent.off(:topic_status_updated, &handler) if handler+ end+it "does not advance read state when a topic is automatically closed" dopost =PostCreator.create(