A platform for community discussion. Free, open, simple.
FEATURE: Trigger workflows when accepted answers change
Régis Hanol812cdb61095b
diff --git a/plugins/discourse-solved/config/locales/client.en.yml b/plugins/discourse-solved/config/locales/client.en.ymlindex e06b811a422..f6de51f5aad 100644--- a/plugins/discourse-solved/config/locales/client.en.yml+++ b/plugins/discourse-solved/config/locales/client.en.yml@@ -87,6 +87,21 @@ en:slower: "%{duration} slower"tooltip: "The average response time trend."js:+ discourse_workflows:+ node_unavailable:+ requires_solved: "Requires the Solved plugin to be enabled"+ nodes:+ "trigger:solution_changed": "Solution changed"+ node_descriptions:+ "trigger:solution_changed": "Fires when an answer is accepted as a solution or its acceptance is removed"+ solution_changed:+ changes: "Changes"+ changes_description: "Leave empty to match any change."+ changes_accepted: "Accepted"+ changes_removed: "Removed"+ changes_placeholder: "Any change"+ tag_names: "Tags"+ tag_names_description: "Only trigger for topics that have at least one of these tags"notifications:alt:solved:diff --git a/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rb b/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rbnew file mode 100644index 00000000000..1128123f40c--- /dev/null+++ b/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rb@@ -0,0 +1,82 @@+# frozen_string_literal: true++if defined?(DiscourseWorkflows)+ module DiscourseWorkflows+ module Nodes+ module SolutionChanged+ class V1 < DiscourseWorkflows::NodeType+ CHANGES = { accepted_solution: "accepted", unaccepted_solution: "removed" }.freeze++ OUTPUT_SCHEMA =+ DiscourseWorkflows::Schema.merge(+ DiscourseWorkflows::Schema::POST_SCHEMA,+ DiscourseWorkflows::Schema::TOPIC_LIST_ITEM_SCHEMA,+ DiscourseWorkflows::Schema.document(+ "change" => {+ "type" => "string",+ "enum" => CHANGES.values,+ },+ ),+ ).freeze++ description(+ name: "trigger:solution_changed",+ version: "1.0",+ defaults: {+ icon: "square-check",+ color: "green",+ },+ group: "discourse_triggers",+ event: CHANGES.keys,+ available: -> { SiteSetting.solved_enabled },+ unavailable_reason_key: "discourse_workflows.node_unavailable.requires_solved",+ output_contracts: [{ schema: OUTPUT_SCHEMA }],+ properties: {+ changes: {+ type: :multi_options,+ required: false,+ default: [],+ options: CHANGES.values,+ },+ **CATEGORY_FILTER_PROPERTIES,+ **TAG_FILTER_PROPERTIES,+ },+ )++ def self.from_event(event_name, *args)+ new(event_name, *args)+ end++ def initialize(event_name, post, *)+ super(parameters: {})+ @change = CHANGES[event_name]+ @post = post+ end++ def valid?+ @change.present? && @post.present? && topic.present?+ end++ def output+ { change: @change, post: serialize_post(@post), topic: topic_data(topic) }+ end++ def matches?(trigger_ctx)+ changes =+ Array.wrap(trigger_ctx.get_node_parameter("changes", [])).compact_blank.map(&:to_s)+ return false if changes.present? && changes.exclude?(@change)++ matches_topic_filters?(topic, trigger_ctx)+ end++ private++ def topic+ @topic ||=+ @post.association(:topic).target || ::Topic.with_deleted.find_by(id: @post.topic_id)+ end+ end+ end+ end+ end+enddiff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rbindex 9ce832d20f9..30f34c26594 100644--- a/plugins/discourse-solved/plugin.rb+++ b/plugins/discourse-solved/plugin.rb@@ -43,6 +43,10 @@ endrequire_relative "lib/discourse_solved/engine"after_initialize do+ if respond_to?(:register_discourse_workflows_node)+ register_discourse_workflows_node { DiscourseWorkflows::Nodes::SolutionChanged::V1 }+ end+SeedFu.fixture_paths << Rails.root.join("plugins/discourse-solved/db/fixtures").to_sUserUpdater::OPTION_ATTR.push(:notify_on_solved)diff --git a/plugins/discourse-solved/spec/lib/discourse_workflows/nodes/solution_changed_spec.rb b/plugins/discourse-solved/spec/lib/discourse_workflows/nodes/solution_changed_spec.rbnew file mode 100644index 00000000000..997e868dd74--- /dev/null+++ b/plugins/discourse-solved/spec/lib/discourse_workflows/nodes/solution_changed_spec.rb@@ -0,0 +1,83 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::SolutionChanged::V1, discourse_workflows: true do+ fab!(:admin)+ fab!(:category)+ fab!(:other_category, :category)+ fab!(:topic) { Fabricate(:topic, category: category) }+ fab!(:post) { Fabricate(:post, topic: topic) }+ fab!(:tag)++ before do+ SiteSetting.solved_enabled = true+ SiteSetting.allow_solved_on_all_topics = true+ SiteSetting.tagging_enabled = true+ end++ describe "#matches?" do+ it "filters changes, categories, and tags" do+ post.topic.tags << tag+ trigger = described_class.from_event(:accepted_solution, post)++ expect(trigger.matches?(trigger_context({}))).to eq(true)+ expect(trigger.matches?(trigger_context(changes: ["removed"]))).to eq(false)+ expect(trigger.matches?(trigger_context(category_ids: [other_category.id]))).to eq(false)+ expect(trigger.matches?(trigger_context(tag_names: ["missing"]))).to eq(false)+ expect(+ trigger.matches?(+ trigger_context(+ changes: ["accepted"],+ category_ids: [category.id],+ tag_names: [tag.name],+ ),+ ),+ ).to eq(true)+ end+ end++ describe "event dispatch" do+ it "announces answer removal even when another accepted solution remains" do+ SiteSetting.solved_allow_multiple_solutions = true+ answer = Fabricate(:post, topic: post.topic)+ other_answer = Fabricate(:post, topic: post.topic)+ graph = build_workflow_graph { |builder| builder.node "solution", described_class.identifier }+ Fabricate(:discourse_workflows_workflow, created_by: admin, published: true, **graph)++ DiscourseSolved::AcceptAnswer.call(params: { post_id: answer.id }, guardian: admin.guardian)+ DiscourseSolved::AcceptAnswer.call(+ params: {+ post_id: other_answer.id,+ },+ guardian: admin.guardian,+ )+ DiscourseSolved::UnacceptAnswer.call(params: { post_id: answer.id }, guardian: admin.guardian)++ data =+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.map do |job|+ job["args"].first["trigger_data"]+ end+ expect(data.pluck("change")).to eq(%w[accepted accepted removed])+ expect(data.map { |payload| payload.dig("post", "id") }).to eq(+ [answer.id, other_answer.id, answer.id],+ )+ expect(data.map { |payload| payload.dig("topic", "id") }.uniq).to eq([topic.id])+ expect(data).to all(match_node_output_schema(described_class))+ expect(post.topic.reload.topic_answers.pluck(:answer_post_id)).to eq([other_answer.id])+ end++ it "stops dispatching when Solved is disabled" do+ graph = build_workflow_graph { |builder| builder.node "solution", described_class.identifier }+ Fabricate(:discourse_workflows_workflow, created_by: admin, published: true, **graph)+ SiteSetting.solved_enabled = false++ DiscourseEvent.trigger(:accepted_solution, post)++ expect(Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs).to be_empty+ expect(DiscourseWorkflows::Registry.find_node_type(described_class.identifier)).to be_nil+ end+ end++ def trigger_context(parameters)+ DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters.deep_stringify_keys })+ end+enddiff --git a/plugins/discourse-workflows/USE_CASES.md b/plugins/discourse-workflows/USE_CASES.mdindex 16e3434a8a4..3ca63b81882 100644--- a/plugins/discourse-workflows/USE_CASES.md+++ b/plugins/discourse-workflows/USE_CASES.md@@ -66,6 +66,7 @@ For each run, record:- Connect multiple event triggers to the same downstream node to handle several events. Existing `trigger:topic_status_changed` and `trigger:reviewable_status_changed` nodes remain editable and executable, but are hidden from the picker and AI discovery. Fixed-event nodes have no status selector and ignore any supplied `statuses` configuration.- `trigger:user_trust_level_changed` exposes old and new levels, including demotions. `trigger:user_first_logged_in` follows the first-login event, whose first-visit check uses `last_seen_at`.- `trigger:user_moderation_changed` exposes `change`, the affected `user`, optional `actor` and `reason`, and `expires_at`. Automatic suspension expiry is announced for suspensions scheduled after this functionality is deployed; existing suspensions are not backfilled.+- `trigger:solution_changed` is available with Solved and reports acceptance or removal of an individual answer. Removing one answer can leave other accepted solutions; replacing an answer emits acceptance for its replacement.- 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.