Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add V2 core workflow nodes with consistent user and actor (#44557)

Régis Hanol57920fa1b7ab
diff --git a/.skills/discourse-workflow-nodes/SKILL.md b/.skills/discourse-workflow-nodes/SKILL.md
index e2b5cd685f5..17eb33fb139 100644
--- a/.skills/discourse-workflow-nodes/SKILL.md
+++ b/.skills/discourse-workflow-nodes/SKILL.md
@@ -54,7 +54,7 @@ Renaming or removing an output key, or changing its meaning, breaks saved workfl
- Core nodes self-register; plugin nodes need `register_discourse_workflows_node(...::V2)` next to V1.
- Services resolve the saved version with `NodeData.node_type(node)`, never a hard-coded `V1`.
- Deprecated versions keep running for saved workflows, are hidden from the palette and AI catalog, and show an editor notice with an Upgrade action. Remove them after a deprecation window.
-- Spec both: `match_node_output_schema(Foo::V1)` pins the old keys, `match_node_output_schema(Foo::V2)` the new ones.
+- Spec both: `match_node_output_schema(Foo::V1)` pins the old keys, `match_node_output_schema(Foo::V2)` the new ones. `node_deprecation_spec` checks `superseded_by`.
## Output conventions
diff --git a/plugins/discourse-workflows/USE_CASES.md b/plugins/discourse-workflows/USE_CASES.md
index 9df44887ae4..468b79edf74 100644
--- a/plugins/discourse-workflows/USE_CASES.md
+++ b/plugins/discourse-workflows/USE_CASES.md
@@ -28,7 +28,7 @@ For each run, record:
| `support-tag-reply` | `when a new topic is created in Support, tag it needs-review and reply with Thanks for contacting support. Our team will review this soon.` | `trigger:topic_created -> action:topic_tags -> action:post (create)` | Resolves category/tag; after `action:topic_tags`, downstream reply uses `$json.topic_id`; no Code node. |
| `closed-support-wait-archive-chat` | `30 days after a topic in Support is closed, archive it and post a message in General chat linking to the topic` | `trigger:topic_closed -> flow:wait -> action:topic (archive) -> action:send_chat_message` | Uses `flow:wait`; no unnecessary forum search/read; topic link template starts with `=`. |
| `manual-maintenance-chat` | `create a manual workflow that sends a message to General chat saying Maintenance window is starting now` | `trigger:manual -> action:send_chat_message` | Static message does not need expression syntax; validates without expression errors. |
-| `closed-general-tl1-chat` | `when a topic by a tl1 or lower is closed in the general category message the general channel with a link to the post` | `trigger:topic_closed -> action:topic (get) -> action:group (check_membership) -> condition:if -> action:send_chat_message` | Uses `action:topic` get to hydrate first-post fields (the topic payload has no `trust_level`). Resolves the `trust_level_2` automatic group and uses `action:group` with `operation: check_membership`, resolved `group_id`, and `username: ={{ $json.post.username }}`, then continues on the condition false branch (`group_membership.in_group == false`) for TL1-or-lower because `trust_level_N` is cumulative for TL >= N. Uses `$json.post.post_url`; no Code node; no clarification needed. |
+| `closed-general-tl1-chat` | `when a topic by a tl1 or lower is closed in the general category message the general channel with a link to the post` | `trigger:topic_closed -> action:group (check_membership) -> condition:if -> action:send_chat_message` | The topic payload carries the author as `$json.topic.username` but has no `trust_level`. Resolves the `trust_level_2` automatic group and uses `action:group` with `operation: check_membership`, resolved `group_id`, and `username: ={{ $json.topic.username }}`, then continues on the condition false branch (`group_membership.in_group == false`) for TL1-or-lower because `trust_level_N` is cumulative for TL >= N. Uses `$json.topic.url`; no `action:topic` get; no Code node; no clarification needed. |
| `friend-group-post-dm-admin` | `when anyone in the friend group posts send admin a dm with a link to the post` | `trigger:post_created -> action:group (check_membership) -> condition:if -> action:send_personal_message` | Resolves the `friend` group and `admin` user; sets resolved `group_id`, uses `$json.post.username` for membership, branches on `$json.group_membership.in_group`, and uses `$json.post.post_url` in a leading-`=` personal message body; no Code node. |
| `tl1-chat-dm` | `when a user reaches trust level 1, send them a chat DM welcoming them to chat` | `trigger:user_trust_level_changed -> action:send_chat_message` | Sets `new_trust_levels: ["1"]` on the trigger. Uses `target: "user"` with `target_usernames: ={{ $json.user.username }}` and default `actor_username: system`; does not call `search_chat_channels` or set `channel_id`; message template starts with `=`. Messages from `system` skip the recipient's DM preferences. A chat DM on `trigger:user_created` fails with default settings because new signups are inactive and TL0 (not in `chat_allowed_groups`); welcome signups with `action:send_personal_message` instead. |
@@ -36,13 +36,13 @@ For each run, record:
| ID | Prompt | Expected shape | Key checks |
| --- | --- | --- | --- |
-| `post-staff-exclusion` | `when a non-staff user posts in General and the post contains urgent, send a message to General chat with a link to the post` | `trigger:post_created -> condition:filter -> action:send_chat_message` | Uses boolean filter on `$json.post.staff` plus string contains on `$json.post.raw`; no Code node. |
-| `topic-created-category-chat` | `when a new topic is created in Support by a TL0 user, message General chat with the topic title and post link` | `trigger:topic_created -> condition:filter -> action:send_chat_message` | Uses `$json.post.trust_level == 0`, `$json.topic.title`, `$json.post.post_url`. |
+| `post-staff-exclusion` | `when a non-staff user posts in General and the post contains urgent, send a message to General chat with a link to the post` | `trigger:post_created -> condition:filter -> action:send_chat_message` | Uses boolean filter on `$json.user.staff` plus string contains on `$json.post.raw`; no Code node. |
+| `topic-created-category-chat` | `when a new topic is created in Support by a TL0 user, message General chat with the topic title and post link` | `trigger:topic_created -> condition:filter -> action:send_chat_message` | Uses `$json.user.trust_level == 0`, `$json.topic.title`, `$json.post.post_url`. |
| `closed-topic-no-author-filter` | `when any topic in General is closed, message General chat with a link to the topic` | `trigger:topic_closed -> action:send_chat_message` or `trigger:topic_closed -> action:topic (get) -> action:send_chat_message` | If only topic link is needed, no author lookup is required; if post link is requested, use `action:topic` get. |
| `wait-chat-reminder` | `when a topic in Support is closed, wait 7 days and message General chat with the topic title` | `trigger:topic_closed -> flow:wait -> action:send_chat_message` | Wait parameters are `wait_amount: 7`, `wait_unit: days`; dynamic chat template starts with `=`. |
| `tag-only` | `when a new topic is created in Support, add the needs-review tag` | `trigger:topic_created -> action:topic_tags` | Resolves category/tag; no reply; no Code node. |
| `reply-only` | `when a new topic is created in Support, reply Thanks, we will review this shortly.` | `trigger:topic_created -> action:post (create)` | Uses `$json.topic.id`; no unnecessary tag lookup. |
-| `post-created-category-filter` | `when a post is created in Support by a TL1 user, message General chat with the post link` | `trigger:post_created -> condition:filter -> action:send_chat_message` | Trigger category scoping or category filter is correct; uses `$json.post.trust_level`. |
+| `post-created-category-filter` | `when a post is created in Support by a TL1 user, message General chat with the post link` | `trigger:post_created -> condition:filter -> action:send_chat_message` | Trigger category scoping or category filter is correct; uses `$json.user.trust_level`. |
| `archive-closed-topic` | `when a topic in Support is closed, wait 30 days and archive it` | `trigger:topic_closed -> flow:wait -> action:topic (archive)` | Uses archive operation, no chat lookup. |
| `manual-topic-create` | `create a manual workflow that creates a topic in General titled Maintenance update with body Maintenance is complete.` | `trigger:manual -> action:topic (create)` | Resolves category; create operation includes title/raw/category; no Code node. |
| `manual-log` | `create a manual workflow that writes a log entry saying Manual workflow ran` | `trigger:manual -> action:log` | No external discovery needed; no Code node. |
@@ -53,7 +53,7 @@ For each run, record:
| `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. |
| `site-setting-yearly` | `every year on 1 January, update the site description to mention the current year` | `trigger:schedule -> action:site_setting` | Uses `$json.year` from the schedule payload; uses the exact setting name; no Code or HTTP 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. |
+| `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`; `$json.user` is the post author; `$json.actor` is whoever deleted it. |
| `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. |
@@ -69,7 +69,7 @@ For each run, record:
- `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.
+- Topic-only triggers expose the author as `$json.topic.username`; use `action:topic` get only when first-post fields are needed.
- 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.
- Dynamic strings containing `{{ }}` must start with `=`.
@@ -80,7 +80,7 @@ For each run, record:
- 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`.
- Flag post reasons and Flag user notes name the nearest AI Agent on the item's executed input path. Conditions preserve that attribution; merged inputs from different Agents or incomplete item links retain Workflow-only attribution. Successful flags persist the attributed Agent ID in a dedicated reviewable-to-agent table, making that Agent available in the Review Queue Reason filter without parsing reviewable content. Agent filtering survives renames and preserves Workflow filtering and score deduplication. Ambiguous or skipped flags do not add an Agent. Existing payload attribution is backfilled only for reviewables with Workflow score provenance; historical data does not establish individual Workflow–Agent pairings.
-- `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.
+- `trigger:post_destroyed` and `trigger:post_recovered` expose the post author as `$json.user` and whoever deleted or restored it as `$json.actor`.
- DM/personal-message notifications should use `action:send_personal_message` instead of chat or topic reply nodes. Only an explicit chat DM uses `action:send_chat_message` with `target: "user"` (or `"group"` for one group DM).
- 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/app/jobs/regular/discourse_workflows/author_with_ai.rb b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb
index d316c86fe41..7f251f29212 100644
--- a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb
+++ b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/author_with_ai.rb
@@ -7,16 +7,16 @@ module Jobs
AI_RESPONSE_STATUSES = %w[needs_clarification proposed_patch explanation error].freeze
TRIGGER_AUTHOR_FIELD_FACTS = [
- "trigger:topic_created exposes the topic creator/first-post author under post.* and does not expose a top-level user object. Use exact paths such as post.username for the username, post.user_id for the user ID, and post.post_url for a link to the post.",
+ "trigger:topic_created exposes the topic under topic.*, the first post under post.*, and the topic author under user.*. Use exact paths such as user.trust_level for trust-level checks, user.username for the username, user.id for the user ID, and topic.url for an absolute link to the topic.",
"trigger:post_created exposes the created post under post.* and the post author under user.*. Use exact paths such as user.trust_level for trust-level checks, user.username for the username, user.id for the user ID, user.admin/user.moderator/user.staff for staff checks, and post.post_url for a link to the post.",
"For generic requests like 'when someone posts' or 'when anyone posts', trigger:post_created covers all regular posts, including first posts and replies. Do not ask to distinguish replies from topic starters unless the request explicitly says replies only or new topics only.",
- "trigger:topic_closed exposes only the closed topic under topic.*. When a closed-topic workflow needs a first-post link, add action:topic with operation get and topic_id ={{ $json.topic.id }} immediately after the trigger. Use the exact fields returned by workflow_validate_patch for any first-post author data; do not assume post.trust_level is available.",
- "Do not ask whether trust level is available for trigger:post_created or trigger:post_edited; it is available as user.trust_level.",
+ "trigger:topic_closed exposes the closed topic under topic.* and whoever closed it under actor.*. topic.url is an absolute link and topic.username is the topic author. When the topic author's trust level or other user fields are needed, add action:topic with operation get and topic_id ={{ $json.topic.id }}, which returns the author under user.*; do not assume post.trust_level is available.",
+ "Do not ask whether trust level is available for trigger:post_created, trigger:post_edited or trigger:topic_created; it is available as user.trust_level.",
"A graph context node with a deprecation entry runs an older version whose output differs from the node catalog. deprecation.changes maps each key of that version to the catalog key holding the same data; for example { \"user\" => \"actor\" } means user.* on that node is whoever acted. Use that node's own keys. You cannot change a node's version; if the newer version is needed, ask the user to click Upgrade on the node.",
"Do not generate fallback chains for undocumented author aliases; use the exact post.* fields from the node catalog output_contracts.",
"trigger:user_added_to_group and trigger:user_removed_from_group expose the affected user under user.*, the selected group under group.*, and event metadata under membership.*. Use user.username for the affected username and membership.action to distinguish added vs removed if needed.",
"trigger:user_created and trigger:user_updated expose the affected user under user.* only, including user.username, user.trust_level, user.staged, and user.created_at. They do not expose a post or topic object. They also fire for staged users (accounts auto-created by incoming email or invites); to skip those, add condition:filter on user.staged equals false after the trigger.",
- "trigger:reviewable_created and trigger:reviewable_approved expose the review queue item under reviewable.* only (reviewable.id, reviewable.type, reviewable.status, reviewable.target_type, reviewable.target_id, reviewable.topic_id, reviewable.category_id, reviewable.score, reviewable.created_at). They do not expose a post, topic, or user object; when a flagged post's content or author is needed, add action:post with operation get and post_id ={{ $json.reviewable.target_id }} after a condition:filter on reviewable.target_type equals Post.",
+ "trigger:reviewable_created and trigger:reviewable_approved expose the review queue item under reviewable.* (reviewable.id, reviewable.type, reviewable.status, reviewable.target_type, reviewable.target_id, reviewable.topic_id, reviewable.category_id, reviewable.score, reviewable.url, reviewable.created_at), with the user it is about under reviewable.target_user.* and the flagger under reviewable.created_by.*; trigger:reviewable_approved also exposes the approver under actor.*. They do not expose a post or topic object; when a flagged post's content is needed, add action:post with operation get and post_id ={{ $json.reviewable.target_id }} after a condition:filter on reviewable.target_type equals Post.",
"For named group membership checks, use workflow_resolve_entity to resolve the group, then use action:group with operation check_membership, group_id set to the resolved group id, and username ={{ $json.user.username }} when the input schema includes user.username, otherwise use the exact username field from the current input schema. It adds group_membership.in_group while preserving the original item fields. Do not use a Code node for simple group membership.",
"For personal messages, private messages, DMs, direct messages, or PM notifications, use action:send_personal_message with recipient_usernames or recipient_group_names as arrays, title, raw, and sender_username. Resolve named users with workflow_resolve_entity(kind: user), and named groups with workflow_resolve_entity(kind: group).",
].freeze
diff --git a/plugins/discourse-workflows/app/serializers/discourse_workflows/post_serializer.rb b/plugins/discourse-workflows/app/serializers/discourse_workflows/post_serializer.rb
index 4661528d91a..8240d00b8b2 100644
--- a/plugins/discourse-workflows/app/serializers/discourse_workflows/post_serializer.rb
+++ b/plugins/discourse-workflows/app/serializers/discourse_workflows/post_serializer.rb
@@ -96,7 +96,7 @@ module DiscourseWorkflows
def tags
return [] if topic.blank? || !SiteSetting.tagging_enabled
- topic.tags.visible(scope).pluck(:name)
+ topic.visible_tags(scope).map(&:name)
end
def upload_ids
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index fcb4d725163..59beb588298 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -57,6 +57,7 @@ en:
node_deprecated:
notice: "A newer version of this node is available. Its output changes: %{changes}. Expressions using the old keys must be updated after upgrading."
upgrade: "Upgrade"
+ user_is_post_author: "In the newer version, user is the post author instead of whoever acted."
topic_v1: "Posts now use a smaller shape: author details such as trust_level, name, avatar_template, admin and staff moved to user, and fields such as topic_archetype, wiki, hidden, edit_reason and reads were removed."
node_issues:
title: "Configuration issues"
@@ -384,9 +385,12 @@ 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:post_like_changed": "Fires when a post is liked or unliked. The user is whoever liked it, not the author."
+ "trigger:post_destroyed": "Fires when a post is deleted. The user is the author; the actor is whoever deleted it."
+ "trigger:post_destroyed_v1": "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 the author; the actor is whoever restored it."
+ "trigger:post_recovered_v1": "Fires when a deleted post is restored. The user is whoever restored it, not the author."
+ "trigger:post_like_changed": "Fires when a post is liked or unliked. The user is the author; the actor is whoever liked it."
+ "trigger:post_like_changed_v1": "Fires when a post is liked or unliked. The user is whoever liked 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"
diff --git a/plugins/discourse-workflows/config/templates/bump-topic.json b/plugins/discourse-workflows/config/templates/bump-topic.json
index 0725b59b2ac..871969b80fd 100644
--- a/plugins/discourse-workflows/config/templates/bump-topic.json
+++ b/plugins/discourse-workflows/config/templates/bump-topic.json
@@ -21,7 +21,7 @@
{
"id": "bump-topic",
"type": "action:topic",
- "typeVersion": "1.0",
+ "typeVersion": "2.0",
"name": "Bump topic",
"parameters": {
"operation": "bump",
diff --git a/plugins/discourse-workflows/config/templates/notify-staff-about-topics-with-no-response.json b/plugins/discourse-workflows/config/templates/notify-staff-about-topics-with-no-response.json
index 2662b9c2789..6dffd8ab9d7 100644
--- a/plugins/discourse-workflows/config/templates/notify-staff-about-topics-with-no-response.json
+++ b/plugins/discourse-workflows/config/templates/notify-staff-about-topics-with-no-response.json
@@ -35,7 +35,7 @@
{
"id": "get-topic",
"type": "action:topic",
- "typeVersion": "1.0",
+ "typeVersion": "2.0",
"name": "Get topic",
"parameters": {
"operation": "get",
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
index f8f5deebcd1..179e5200e0c 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
@@ -99,8 +99,9 @@ module DiscourseWorkflows
- Dynamic parameter values must start with =. Use ={{ $json.topic.id }} for a whole-field expression, and use template strings such as =Archived topic: {{ $json.topic.title }} ({{ $json.topic.url }}) when mixing text and expressions.
- Do not write bare {{ $json.field }} templates without the leading =; without = they are treated as literal text.
- For topic links use $json.topic.url, which is already absolute. Only use $json.post.post_url (relative) when the current node input_fields includes it.
- - Discourse trust levels are automatic groups named trust_level_0 through trust_level_4. Membership is cumulative: the trust_level_N group contains every user whose trust level is N or higher. When a workflow needs a trust-level check but the current input_fields has no trust_level field (for example after trigger:topic_closed or action:topic get, which expose post.username but not the author trust level), resolve the relevant trust_level_N group with workflow_resolve_entity and use action:group with operation "check_membership", the resolved group_id, and the available username field (such as $json.post.username) instead of asking for clarification or adding a Code node. For "TL1 or lower", check membership in trust_level_2 and branch on $json.group_membership.in_group being false; for "TL2 or higher", branch on it being true.
+ - Discourse trust levels are automatic groups named trust_level_0 through trust_level_4. Membership is cumulative: the trust_level_N group contains every user whose trust level is N or higher. When a workflow needs a trust-level check but the current input_fields has no trust_level field (for example after trigger:topic_closed, which exposes topic.username but not the author trust level), resolve the relevant trust_level_N group with workflow_resolve_entity and use action:group with operation "check_membership", the resolved group_id, and the available username field (such as $json.topic.username) instead of asking for clarification or adding a Code node. For "TL1 or lower", check membership in trust_level_2 and branch on $json.group_membership.in_group being false; for "TL2 or higher", branch on it being true.
- Do not assume $json.post.trust_level exists. The post payload never includes trust level; trust level is only available as $json.user.trust_level on triggers that expose a user object (such as trigger:post_created). For topic-only payloads, use the trust_level_N group membership approach above.
+ - $json.user is the subject of the event (post or topic author, affected account) and $json.actor is whoever performed the action (deleter, editor, liker, assigner). action:topic returns the topic author as $json.user. Nodes whose graph context has a deprecation entry run an older version where this may not hold; read their keys from deprecation.changes. You cannot upgrade a node's version; ask the user to click Upgrade on the node.
- Do not ask whether the first post or topic author trust level is available until you have checked the current node schemas with workflow_validate_patch.
- In condition nodes, each condition must use leftValue, operator, and rightValue keys. Do not use left/right keys. Use operator.type "number" for integer/number fields, "string" for string fields, and "boolean" for boolean fields.
- Use Code nodes for data transformation when needed. The server only checks proposed scripts for syntax and mode rules, so testing them with workflow_validate_script is the only runtime check.
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.rb
index 16f7ea53c50..3e3ba28d1ce 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_destroyed/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_destroyed/v1.rb
@@ -19,6 +19,15 @@ module DiscourseWorkflows
output_contracts: PostLifecycle::OUTPUT_CONTRACTS,
properties: PostLifecycle::PROPERTIES,
)
+
+ deprecated(
+ superseded_by: "2.0",
+ changes: {
+ "user" => "actor",
+ },
+ notice_key: "discourse_workflows.node_deprecated.user_is_post_author",
+ description_key: "discourse_workflows.node_descriptions.trigger:post_destroyed_v1",
+ )
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_destroyed/v2.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_destroyed/v2.rb
new file mode 100644
index 00000000000..cb9e8c853a8
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_destroyed/v2.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module PostDestroyed
+ class V2 < V1
+ include PostLifecycle::V2
+
+ description(
+ superclass.description.merge(
+ version: "2.0",
+ output_contracts: PostLifecycle::V2::OUTPUT_CONTRACTS,
+ ),
+ )
+ end
+ end
+ end
+end
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.rb
index 38ba4f5508f..8ee05f0dee6 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
@@ -6,6 +6,44 @@ module DiscourseWorkflows
class V1 < NodeType
POST_SCOPE_OPTIONS = %w[first_post replies all_posts].freeze
+ EDITOR_SCHEMA =
+ Schema.document(
+ "editor" => Schema.nullable_user("User who performed the edit, when available"),
+ )
+
+ BASE_OUTPUT_SCHEMA =
+ Schema.merge(
+ Schema::POST_SCHEMA,
+ Schema::TOPIC_LIST_ITEM_SCHEMA,
+ Schema::USER_SCHEMA,
+ Schema.document(
+ "revision" => {
+ "type" => %w[object null],
+ "description" =>
+ "Revision created by the edit, null for edits within the grace period",
+ "properties" => {
+ "number" => {
+ "type" => "integer",
+ },
+ "edit_reason" => {
+ "type" => %w[string null],
+ },
+ "changes" => {
+ "type" => "array",
+ "description" => "Names of the modified fields",
+ "items" => {
+ "type" => "string",
+ },
+ },
+ "previous_raw" => {
+ "type" => %w[string null],
+ "description" => "Raw before the edit, null when the raw did not change",
+ },
+ },
+ },
+ ),
+ )
+
description(
name: "trigger:post_edited",
version: "1.0",
@@ -15,47 +53,7 @@ module DiscourseWorkflows
},
group: "discourse_triggers",
event: :post_edited,
- output_contracts: [
- {
- schema:
- Schema.merge(
- Schema::POST_SCHEMA,
- Schema::TOPIC_LIST_ITEM_SCHEMA,
- Schema::USER_SCHEMA,
- Schema.document(
- "editor" => {
- "type" => %w[object null],
- "description" => "User who performed the edit, when available",
- "properties" => Schema::USER_PROPERTIES,
- },
- "revision" => {
- "type" => %w[object null],
- "description" =>
- "Revision created by the edit, null for edits within the grace period",
- "properties" => {
- "number" => {
- "type" => "integer",
- },
- "edit_reason" => {
- "type" => %w[string null],
- },
- "changes" => {
- "type" => "array",
- "description" => "Names of the modified fields",
- "items" => {
- "type" => "string",
- },
- },
- "previous_raw" => {
- "type" => %w[string null],
- "description" => "Raw before the edit, null when the raw did not change",
- },
- },
- },
- ),
- ),
- },
- ],
+ output_contracts: [{ schema: Schema.merge(BASE_OUTPUT_SCHEMA, EDITOR_SCHEMA) }],
properties: {
post_scope: {
type: :options,
@@ -72,6 +70,8 @@ module DiscourseWorkflows
},
)
+ deprecated(superseded_by: "2.0", changes: { "editor" => "actor" })
+
def initialize(post, _topic_changed = nil, revisor = nil, *)
super(parameters: {})
@post = post
@@ -88,7 +88,7 @@ module DiscourseWorkflows
post: serialize_post(@post, include_cooked: true),
topic: serialize_topic_list_item(@post.topic),
user: serialize_user(@post.user),
- editor: serialize_user(@revisor&.editor),
+ **serialize_editor,
revision: serialize_revision,
}
end
@@ -102,6 +102,10 @@ module DiscourseWorkflows
private
+ def serialize_editor
+ { editor: serialize_user(@revisor&.editor) }
+ end
+
def serialize_revision
revision = @revisor&.post_revision
return if revision.blank?
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v2.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v2.rb
new file mode 100644
index 00000000000..250ec7621db
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v2.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module PostEdited
+ class V2 < V1
+ description(
+ superclass.description.merge(
+ version: "2.0",
+ output_contracts: [{ schema: Schema.merge(BASE_OUTPUT_SCHEMA, Schema::ACTOR_SCHEMA) }],
+ ),
+ )
+
+ private
+
+ def serialize_editor
+ { actor: serialize_user(@revisor&.editor) }
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rb
index 9930b6ebd94..83057a8af0e 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rb
@@ -21,6 +21,24 @@ module DiscourseWorkflows
},
].freeze
+ module V2
+ OUTPUT_CONTRACTS = [
+ {
+ schema:
+ Schema.merge(
+ PostLifecycle::OUTPUT_CONTRACTS.first[:schema],
+ Schema::POST_AUTHOR_SCHEMA,
+ ),
+ },
+ ].freeze
+
+ private
+
+ def subject_user
+ @post.user
+ end
+ end
+
def initialize(post, opts = nil, actor = nil, *)
super(parameters: {})
@post = post
@@ -37,7 +55,7 @@ module DiscourseWorkflows
{
post: serialize_post(@post),
topic: serialize_topic_list_item(topic),
- user: serialize_user(@actor),
+ user: serialize_user(subject_user),
actor: serialize_user(@actor),
}
end
@@ -49,6 +67,10 @@ module DiscourseWorkflows
private
+ def subject_user
+ @actor
+ end
+
def topic
return @topic if defined?(@topic)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v1.rb
index 27e57f5dca0..c7fa445368a 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v1.rb
@@ -48,6 +48,8 @@ module DiscourseWorkflows
},
)
+ deprecated(superseded_by: "2.0", changes: { "username" => "actor.username" })
+
def execute(exec_ctx)
items =
exec_ctx.input_items.map.with_index do |_item, item_index|
@@ -81,6 +83,10 @@ module DiscourseWorkflows
raise_node_error!(result.errors.full_messages.join(", "), item_index:) if result.failed?
end
+ serialize_result(post, actor, changed)
+ end
+
+ def serialize_result(post, actor, changed)
{ post_id: post.id, username: actor.username, actor: serialize_user(actor), changed: }
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v2.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v2.rb
new file mode 100644
index 00000000000..25e5875cbef
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v2.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module PostLike
+ class V2 < V1
+ description(
+ superclass.description.merge(
+ version: "2.0",
+ output_contracts: [
+ { schema: Schema.document(OUTPUT_SCHEMA["properties"].except("username")) },
+ ],
+ ),
+ )
+
+ private
+
+ def serialize_result(*)
+ super.except(:username)
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v1.rb
index b45ca93f734..b4b9e6e6768 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v1.rb
@@ -36,6 +36,15 @@ module DiscourseWorkflows
},
)
+ deprecated(
+ superseded_by: "2.0",
+ changes: {
+ "user" => "actor",
+ },
+ notice_key: "discourse_workflows.node_deprecated.user_is_post_author",
+ description_key: "discourse_workflows.node_descriptions.trigger:post_like_changed_v1",
+ )
+
def self.from_event(event_name, post_action, *)
new(event_name, post_action)
end
@@ -60,7 +69,7 @@ module DiscourseWorkflows
change: @change,
post: serialize_post(@post),
topic: serialize_topic_list_item(@post.topic),
- user: serialize_user(@user),
+ user: serialize_user(subject_user),
actor: serialize_user(@user),
}
end
@@ -68,6 +77,12 @@ module DiscourseWorkflows
def matches?(trigger_ctx)
matches_changes?(trigger_ctx, @change) && matches_topic_filters?(@post.topic, trigger_ctx)
end
+
+ private
+
+ def subject_user
+ @user
+ end
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v2.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v2.rb
new file mode 100644
index 00000000000..4ee5f407a86
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v2.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module PostLikeChanged
+ class V2 < V1
+ description(
+ superclass.description.merge(
+ version: "2.0",
+ output_contracts: [{ schema: Schema.merge(OUTPUT_SCHEMA, Schema::POST_AUTHOR_SCHEMA) }],
+ ),
+ )
+
+ private
+
+ def subject_user
+ @post.user
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v1.rb
index fc60b1f71ef..1ba5de9d77b 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v1.rb
@@ -19,6 +19,15 @@ module DiscourseWorkflows
output_contracts: PostLifecycle::OUTPUT_CONTRACTS,
properties: PostLifecycle::PROPERTIES,
)
+
+ deprecated(
+ superseded_by: "2.0",
+ changes: {
+ "user" => "actor",
+ },
+ notice_key: "discourse_workflows.node_deprecated.user_is_post_author",
+ description_key: "discourse_workflows.node_descriptions.trigger:post_recovered_v1",
+ )
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v2.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v2.rb
new file mode 100644
index 00000000000..01f5987453d
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_recovered/v2.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module PostRecovered
+ class V2 < V1
+ include PostLifecycle::V2
+
+ description(
+ superclass.description.merge(
+ version: "2.0",
+ output_contracts: PostLifecycle::V2::OUTPUT_CONTRACTS,
+ ),
+ )
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb
index 0d5af4f322e..ac2ff39ebcc 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb
@@ -37,22 +37,12 @@ module DiscourseWorkflows
"WebHookPostSerializer payload",
)
- description(
- name: "action:topic",
- version: "1.0",
- defaults: {
- icon: "comments",
- color: "light-blue",
- },
- group: "discourse_actions",
- capabilities: {
- run_scope: "per_item",
- },
- output_contracts: [
+ def self.output_contracts_for(topic_schema, post_schema)
+ [
{
variants: [
{
- schema: TOPIC_SCHEMA,
+ schema: topic_schema,
display_options: {
show: {
operation: ["set_custom_fields"],
@@ -60,9 +50,23 @@ module DiscourseWorkflows
},
},
],
- schema: Schema.merge(TOPIC_SCHEMA, TOPIC_POST_SCHEMA),
+ schema: Schema.merge(topic_schema, post_schema),
},
- ],
+ ]
+ end
+
+ description(
+ name: "action:topic",
+ version: "1.0",
+ defaults: {
+ icon: "comments",
+ color: "light-blue",
+ },
+ group: "discourse_actions",
+ capabilities: {
+ run_scope: "per_item",
+ },
+ output_contracts: output_contracts_for(TOPIC_SCHEMA, TOPIC_POST_SCHEMA),
properties: {
operation: {
type: :options,
@@ -223,6 +227,14 @@ module DiscourseWorkflows
},
)
+ deprecated(
+ superseded_by: "2.0",
+ changes: {
+ "post.trust_level" => "user.trust_level",
+ },
+ notice_key: "discourse_workflows.node_deprecated.topic_v1",
+ )
+
def self.load_options_context(context)
case context.method_name
when "topic_custom_fields"
@@ -328,8 +340,8 @@ module DiscourseWorkflows
topic = post.topic
{
- topic: serialize_topic(topic, guardian: guardian),
- post: serialize_topic_post(post),
+ **serialize_topic_result(topic, guardian),
+ post: serialize_topic_post(post, guardian),
post_id: post.id,
post_number: post.post_number,
}
@@ -342,15 +354,7 @@ module DiscourseWorkflows
custom_field_names = config["custom_field_names"]
::Topic.preload_custom_fields([topic], custom_field_names) if custom_field_names.present?
- {
- topic:
- serialize_topic(
- topic,
- guardian: actor.guardian,
- custom_field_names: custom_field_names,
- ),
- post: serialize_topic_post(topic.first_post),
- }
+ serialize_topic_with_post(topic, actor.guardian, custom_field_names:)
end
def list_topics(exec_ctx, config, item_index)
@@ -377,25 +381,15 @@ module DiscourseWorkflows
topics = topic_list.topics.slice(offset, limit) || []
ActiveRecord::Associations::Preloader.new(
records: topics,
- associations: {
- first_post: :user,
- },
+ associations: [:user, { first_post: %i[user uploads] }],
).call
+ topics.each { |topic| topic.first_post&.association(:topic)&.target = topic }
custom_field_names = config["custom_field_names"]
::Topic.preload_custom_fields(topics, custom_field_names) if custom_field_names.present?
- topics.map do |topic|
- {
- topic:
- serialize_topic(
- topic,
- guardian: actor.guardian,
- custom_field_names: custom_field_names,
- ),
- post: serialize_topic_post(topic.first_post),
- }
- end
+ guardian = actor.guardian
+ topics.map { |topic| serialize_topic_with_post(topic, guardian, custom_field_names:) }
end
def close_topic(exec_ctx, config, item_index)
@@ -406,10 +400,7 @@ module DiscourseWorkflows
topic.update_status("closed", true, actor)
topic.reload
- {
- topic: serialize_topic(topic, guardian: actor.guardian),
- post: serialize_topic_post(topic.first_post),
- }
+ serialize_topic_with_post(topic, actor.guardian)
end
def archive_topic(exec_ctx, config, item_index)
@@ -421,10 +412,7 @@ module DiscourseWorkflows
topic.update_status("archived", true, actor)
topic.reload
- {
- topic: serialize_topic(topic, guardian: guardian),
- post: serialize_topic_post(topic.first_post),
- }
+ serialize_topic_with_post(topic, guardian)
end
def bump_topic(exec_ctx, config, item_index)
@@ -447,10 +435,7 @@ module DiscourseWorkflows
end
topic.reload
- {
- topic: serialize_topic(topic, guardian: guardian),
- post: serialize_topic_post(topic.first_post),
- }
+ serialize_topic_with_post(topic, guardian)
end
def set_custom_fields(exec_ctx, config, item_index)
@@ -465,12 +450,26 @@ module DiscourseWorkflows
topic.save_custom_fields
end
+ serialize_topic_result(topic, guardian, custom_field_names: custom_fields.keys)
+ end
+
+ def serialize_topic_result(topic, guardian, custom_field_names: [])
+ { topic: serialize_topic(topic, guardian:, custom_field_names:) }
+ end
+
+ def serialize_topic_with_post(topic, guardian, custom_field_names: [])
{
- topic:
- serialize_topic(topic, guardian: guardian, custom_field_names: custom_fields.keys),
+ **serialize_topic_result(topic, guardian, custom_field_names:),
+ post: serialize_topic_post(topic.first_post, guardian),
}
end
+ def serialize_topic_post(post, _guardian)
+ return if post.blank?
+
+ serialize_record(post, WebHookPostSerializer)
+ end
+
def custom_fields_from_entries(entries)
Array(entries).each_with_object({}) do |entry, fields|
key = entry["key"].to_s.strip
@@ -479,12 +478,6 @@ module DiscourseWorkflows
fields[key] = entry["value"]
end
end
-
- def serialize_topic_post(post)
- return if post.blank?
-
- serialize_record(post, WebHookPostSerializer)
- end
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v2.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v2.rb
new file mode 100644
index 00000000000..6bf4bbb42c4
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v2.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module Topic
+ class V2 < V1
+ TOPIC_RESULT_SCHEMA = Schema.merge(TOPIC_SCHEMA, Schema::TOPIC_AUTHOR_SCHEMA)
+
+ description(
+ superclass.description.merge(
+ version: "2.0",
+ output_contracts: output_contracts_for(TOPIC_RESULT_SCHEMA, Schema::POST_SCHEMA),
+ ),
+ )
+
+ private
+
+ def serialize_topic_result(topic, guardian, **)
+ super.merge(user: serialize_user(topic.user, guardian:))
+ end
+
+ def serialize_topic_post(post, guardian)
+ serialize_post(post, guardian:, include_cooked: true)
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/schema.rb b/plugins/discourse-workflows/lib/discourse_workflows/schema.rb
index 0354e6efc42..8180627ac6a 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/schema.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/schema.rb
@@ -171,6 +171,7 @@ module DiscourseWorkflows
USER_SCHEMA = entity("user", USER_PROPERTIES, "Basic safe user attributes")
ACTOR_SCHEMA = document("actor" => nullable_user("User who performed the action"))
POST_AUTHOR_SCHEMA = document("user" => nullable_user("Post author"))
+ TOPIC_AUTHOR_SCHEMA = document("user" => nullable_user("Topic author"))
ASSIGNEE_PROPERTIES = {
"type" => "object",
"properties" => {
diff --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.rb
index e08b87c3b90..6a64f902f58 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
@@ -169,11 +169,10 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog do
"topic.slug" => "string",
"topic.archived" => "boolean",
"post.post_url" => "string",
+ "post.upload_ids" => "array<integer>",
+ "topic.custom_fields" => "object",
)
- expect(output_fields_by_type.fetch("action:topic")).not_to include(
- "post.trust_level",
- "post.upload_ids",
- )
+ expect(output_fields_by_type.fetch("action:topic")).not_to include("post.trust_level")
expect(output_fields_by_type.fetch("action:topic_tags")).to include(
"topic_id" => "integer",
"tag_names" => "array<string>",
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/node_deprecation_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/node_deprecation_spec.rb
new file mode 100644
index 00000000000..b975a366f17
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/node_deprecation_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::NodeType do
+ describe ".deprecation" do
+ it "deprecates the versions superseded by a breaking output change" do
+ deprecated =
+ DiscourseWorkflows::Registry.nodes(include_disabled_plugins: true).select(&:deprecated?)
+
+ expect(deprecated.map(&:name)).to contain_exactly(
+ "DiscourseWorkflows::Nodes::PostDestroyed::V1",
+ "DiscourseWorkflows::Nodes::PostEdited::V1",
+ "DiscourseWorkflows::Nodes::PostLike::V1",
+ "DiscourseWorkflows::Nodes::PostLikeChanged::V1",
+ "DiscourseWorkflows::Nodes::PostRecovered::V1",
+ "DiscourseWorkflows::Nodes::Topic::V1",
+ )
+ end
+
+ it "points every deprecated version to a registered, non-deprecated successor" do
+ DiscourseWorkflows::Registry
+ .nodes(include_disabled_plugins: true)
+ .select(&:deprecated?)
+ .each do |node_class|
+ successor =
+ DiscourseWorkflows::Registry.find_node_type(
+ node_class.identifier,
+ version: node_class.deprecation[:superseded_by],
+ include_disabled_plugins: true,
+ )
+
+ expect(successor).to be_present, "#{node_class.name} has no registered successor"
+ expect(successor).not_to be_deprecated, "#{successor.name} is deprecated"
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v2_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v2_spec.rb
new file mode 100644
index 00000000000..76e6ebe75dd
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v2_spec.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::PostEdited::V2 do
+ fab!(:user) { Fabricate(:user, trust_level: TrustLevel[2]) }
+ fab!(:editor, :admin)
+ fab!(:first_post) { create_post(user: user, raw: "First post") }
+
+ describe "#output" do
+ it "exposes the editor as actor", :aggregate_failures do
+ events =
+ DiscourseEvent.track_events(:post_edited) do
+ first_post.revise(editor, raw: "Edited by another user")
+ end
+
+ output = described_class.new(*events.sole[:params]).output
+
+ expect(output[:user]).to include(id: user.id)
+ expect(output[:actor]).to include(id: editor.id, username: editor.username)
+ expect(output).not_to have_key(:editor)
+ expect(output).to match_node_output_schema(described_class)
+ end
+
+ it "has a null actor when the editor is unknown" do
+ output = described_class.new(first_post).output
+
+ expect(output).to include(actor: nil)
+ expect(output).to match_node_output_schema(described_class)
+ end
+ end
+end
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.rb
index 82d1f504868..835a8fa5418 100644
--- 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.rb
@@ -59,6 +59,22 @@ RSpec.describe DiscourseWorkflows::Nodes::PostLifecycle do
expect(output).to match_node_output_schema(klass)
end
+ context "with V2" do
+ %w[PostDestroyed PostRecovered].each do |node|
+ context "with #{node}" do
+ let(:klass) { "DiscourseWorkflows::Nodes::#{node}::V2".constantize }
+
+ it "returns the author as user and the acting user as actor", :aggregate_failures do
+ output = trigger.output
+
+ expect(output[:user]).to include(id: post.user.id, username: post.user.username)
+ expect(output[:actor]).to include(id: actor.id, username: actor.username)
+ expect(output).to match_node_output_schema(klass)
+ end
+ end
+ end
+ end
+
it "keeps topic-derived post fields when the topic went down with the post",
:aggregate_failures do
PostDestroyer.new(actor, first_post).destroy
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_changed_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_changed_spec.rb
index d999711bc45..cc944dfee9f 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_changed_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_changed_spec.rb
@@ -50,3 +50,18 @@ RSpec.describe DiscourseWorkflows::Nodes::PostLikeChanged::V1 do
end
end
end
+
+RSpec.describe DiscourseWorkflows::Nodes::PostLikeChanged::V2 do
+ fab!(:author, :user)
+ fab!(:post) { Fabricate(:post, user: author) }
+ fab!(:liker, :user)
+
+ it "returns the post author as user and the liker as actor", :aggregate_failures do
+ trigger = described_class.from_event(:like_created, PostAction.new(post:, user: liker))
+ output = trigger.output
+
+ expect(output[:user]).to include(id: author.id)
+ expect(output[:actor]).to include(id: liker.id)
+ expect(output).to match_node_output_schema(described_class)
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_spec.rb
index b306cd7b334..7f32fb0212b 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_spec.rb
@@ -55,3 +55,24 @@ RSpec.describe DiscourseWorkflows::Nodes::PostLike::V1 do
end
end
end
+
+RSpec.describe DiscourseWorkflows::Nodes::PostLike::V2 do
+ fab!(:post)
+ fab!(:actor, :user)
+
+ it "returns the liker as actor", :aggregate_failures do
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "like",
+ "post_id" => post.id.to_s,
+ "actor_username" => actor.username,
+ },
+ )
+
+ expect(result).to include("post_id" => post.id, "changed" => true)
+ expect(result["actor"]).to include("id" => actor.id, "username" => actor.username)
+ expect(result).not_to have_key("username")
+ expect(result).to match_node_output_schema(described_class)
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic/v2_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic/v2_spec.rb
new file mode 100644
index 00000000000..f3d8ea58bc2
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic/v2_spec.rb
@@ -0,0 +1,92 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::Topic::V2 do
+ fab!(:admin)
+ fab!(:author) { Fabricate(:user, trust_level: TrustLevel[3]) }
+ fab!(:category)
+ fab!(:topic) { Fabricate(:topic, category:, user: author) }
+ fab!(:first_post) { Fabricate(:post, topic:, user: author) }
+
+ it "creates a topic and returns the author as user", :aggregate_failures do
+ result =
+ execute_node(
+ configuration: {
+ "operation" => "create",
+ "title" => "Workflow topic",
+ "raw" => "First post body",
+ "category_id" => category.id.to_s,
+ "actor_username" => admin.username,
+ },
+ )
+
+ created = Topic.last
+ expect(result["user"]).to include("id" => admin.id, "trust_level" => admin.trust_level)
+ expect(result["post"]).to include(
+ "id" => created.first_post.id,
+ "url" => created.first_post.full_url,
+ "category_slug" => category.slug,
+ )
+ expect(result["post"]).not_to have_key("trust_level")
+ expect(result).to match_node_output_schema(described_class)
+ end
+
+ it "gets a topic with its author as user", :aggregate_failures do
+ result = execute_node(configuration: { "operation" => "get", "topic_id" => topic.id.to_s })
+
+ expect(result["user"]).to include(
+ "id" => author.id,
+ "trust_level" => TrustLevel[3],
+ "staff" => false,
+ )
+ expect(result.dig("post", "id")).to eq(first_post.id)
+ end
+
+ it "lists topics with tags and uploads in a constant number of queries", :aggregate_failures do
+ SiteSetting.tagging_enabled = true
+ tag = Fabricate(:tag)
+ upload = Fabricate(:upload)
+ topic.update!(tags: [tag])
+ UploadReference.create!(target: first_post, upload:)
+ config = { "operation" => "list", "query" => "category:#{category.slug}", "limit" => "10" }
+
+ execute_node_output(configuration: config)
+ initial = track_sql_queries { execute_node_output(configuration: config) }.size
+
+ others =
+ Array.new(2) do
+ other = Fabricate(:topic, category:, user: Fabricate(:user), tags: [tag])
+ post = Fabricate(:post, topic: other, user: other.user)
+ UploadReference.create!(target: post, upload:)
+ other
+ end
+
+ result = nil
+ queries = track_sql_queries { result = execute_node_output(configuration: config).first }
+
+ entries = result.map { |item| item["json"] }
+ expect(entries.map { |entry| entry.dig("user", "id") }).to contain_exactly(
+ author.id,
+ *others.map(&:user_id),
+ )
+ expect(entries.map { |entry| entry.dig("post", "tags") }).to all(eq([tag.name]))
+ expect(entries.map { |entry| entry.dig("post", "upload_ids") }).to all(eq([upload.id]))
+ expect(entries).to all(match_node_output_schema(described_class, configuration: config))
+ expect(queries.size).to eq(initial)
+ end
+
+ it "returns the author when setting custom fields", :aggregate_failures do
+ config = {
+ "operation" => "set_custom_fields",
+ "topic_id" => topic.id.to_s,
+ "custom_fields" => {
+ "values" => [{ "key" => "foo", "value" => "bar" }],
+ },
+ "actor_username" => admin.username,
+ }
+
+ result = execute_node(configuration: config)
+
+ expect(result["user"]).to include("id" => author.id)
+ expect(result).to match_node_output_schema(described_class, configuration: config)
+ end
+end
diff --git a/plugins/discourse-workflows/spec/system/workflows_spec.rb b/plugins/discourse-workflows/spec/system/workflows_spec.rb
index a922f777e17..3f79ce046e7 100644
--- a/plugins/discourse-workflows/spec/system/workflows_spec.rb
+++ b/plugins/discourse-workflows/spec/system/workflows_spec.rb
@@ -71,6 +71,32 @@ RSpec.describe "Discourse Workflows" do
expect(editor_page).to have_legacy_topic_tag_fields
end
+ it "upgrades a deprecated node to its latest version" do
+ node_id = "post-edited"
+ workflow =
+ Fabricate(
+ :discourse_workflows_workflow,
+ created_by: admin,
+ nodes: [
+ {
+ "id" => node_id,
+ "type" => "trigger:post_edited",
+ "typeVersion" => "1.0",
+ "name" => "Post edited",
+ "parameters" => {
+ },
+ },
+ ],
+ )
+
+ editor_page.visit_node(workflow, node_id)
+ find(".workflows-deprecation-notice__upgrade").click
+
+ expect(editor_page).to have_node_configurator
+ expect(page).to have_no_css(".workflows-deprecation-notice")
+ expect(workflow.reload.nodes.first["typeVersion"]).to eq("2.0")
+ end
+
it "creates a workflow with condition node" do
editor_page.visit_new
editor_page.click_empty_state_add_node