Skip to content

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

DEV: Share workflow entity serializers across nodes (#44551)

Régis Hanol33151d38fa80
diff --git a/.skills/discourse-workflow-nodes/SKILL.md b/.skills/discourse-workflow-nodes/SKILL.md
new file mode 100644
index 00000000000..35860397af9
--- /dev/null
+++ b/.skills/discourse-workflow-nodes/SKILL.md
@@ -0,0 +1,27 @@
+---
+name: discourse-workflow-nodes
+description: Use when creating, changing, or reviewing Discourse Workflows nodes (triggers, actions, conditions) in plugins/discourse-workflows or plugins/*/lib/discourse_workflows, including their output schemas and specs
+---
+
+# Discourse Workflow Nodes
+
+## Entities in output
+
+Shape entities only with `DiscourseWorkflows::EntitySerializers` (`lib/discourse_workflows/entity_serializers.rb`), available in `NodeType` instance and class methods:
+
+| Entity | Builder | Schema |
+|---|---|---|
+| User | `serialize_user(user, guardian:)` (nil for nil user) | `Schema::USER_SCHEMA` / `USER_PROPERTIES` |
+| Topic (triggers) | `serialize_topic_list_item(topic, scope:)` | `Schema::TOPIC_LIST_ITEM_SCHEMA` / `TOPIC_PROPERTIES` |
+| Topic (actions) | `serialize_topic(topic, guardian:, custom_field_names:)` | `TOPIC_LIST_ITEM_SCHEMA` |
+| Post | `serialize_post(post, guardian:, include_raw:, include_cooked:)` (nil for nil post) | `Schema::POST_SCHEMA` / `POST_PROPERTIES` |
+| Reviewable | `serialize_reviewable(reviewable)` | `Schema::REVIEWABLE_EVENT_SCHEMA` |
+| Assignee (user or group) | `serialize_assignee(assignee, guardian:)` | |
+| Group (basic) | `serialize_basic_group(group)` | `BASIC_GROUP_PROPERTIES` |
+| Any serializer | `serialize_record(record, SerializerClass, scope:)` | |
+
+- Name every helper that shapes an entity for output `serialize_<entity>` (`serialize_badge`, `serialize_group`, `serialize_timer`, `serialize_assignee`, `serialize_channel`, `serialize_event`...), never `<entity>_data`.
+- Don't wrap a shared builder in a node-private helper; call it directly.
+- `serialize_topic_list_item` (`DiscourseWorkflows::TopicListItemSerializer`) carries plugin-added list fields (assign `assigned_to_*`, solved `has_accepted_answer`, voting `can_vote`/`vote_count`...); `serialize_topic` (`DiscourseWorkflows::TopicSerializer`) adds posters, participants and custom fields.
+- Outside a `NodeType`, call them on the module: `DiscourseWorkflows::EntitySerializers.serialize_user(...)`.
+- Groups: `BasicGroupSerializer` (`BASIC_GROUP_PROPERTIES`) or `WebHookGroupSerializer` (`GROUP_SCHEMA`).
diff --git a/AI-AGENTS.md b/AI-AGENTS.md
index bb8a7d93279..a9528b60e78 100644
--- a/AI-AGENTS.md
+++ b/AI-AGENTS.md
@@ -34,6 +34,7 @@ Discourse is large with long history. Understand context before changes.
## Testing
- Use the skill at `.skills/discourse-writing-rspec-tests` when writing RSpec tests
+- Use the skill at `.skills/discourse-workflow-nodes` when writing or reviewing Discourse Workflows nodes
## Commands
diff --git a/plugins/boards/lib/discourse_workflows/nodes/card_moved/v1.rb b/plugins/boards/lib/discourse_workflows/nodes/card_moved/v1.rb
index a1ebcb5e48b..352e46d8d72 100644
--- a/plugins/boards/lib/discourse_workflows/nodes/card_moved/v1.rb
+++ b/plugins/boards/lib/discourse_workflows/nodes/card_moved/v1.rb
@@ -87,8 +87,8 @@ if defined?(DiscourseWorkflows)
methods: [:unicode_title],
),
card_moved: {
- old_column: column_data(@old_column),
- new_column: column_data(@new_column),
+ old_column: serialize_column(@old_column),
+ new_column: serialize_column(@new_column),
},
acting_user: serialize_record(@acting_user, BasicUserSerializer),
}
@@ -96,7 +96,7 @@ if defined?(DiscourseWorkflows)
private
- def column_data(column)
+ def serialize_column(column)
column&.as_json(
only: BoardsSchema::COLUMN_PROPERTIES.keys - ["unicode_title"],
methods: [:unicode_title],
diff --git a/plugins/chat/lib/discourse_workflows/nodes/chat_message_created/v1.rb b/plugins/chat/lib/discourse_workflows/nodes/chat_message_created/v1.rb
index c086f9f7f16..77aa0cafcaa 100644
--- a/plugins/chat/lib/discourse_workflows/nodes/chat_message_created/v1.rb
+++ b/plugins/chat/lib/discourse_workflows/nodes/chat_message_created/v1.rb
@@ -128,7 +128,7 @@ if defined?(DiscourseWorkflows)
thread_id: @message.thread_id,
chat_channel_id: @message.chat_channel_id,
},
- channel: channel_data,
+ channel: serialize_channel,
user: user_data,
}
end
@@ -141,7 +141,7 @@ if defined?(DiscourseWorkflows)
serialize_record(@user, BasicUserSerializer)
end
- def channel_data
+ def serialize_channel
scope = @user&.guardian || Discourse.system_user.guardian
serialize_record(@channel, Chat::WorkflowChannelSerializer, scope:)
end
diff --git a/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb b/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
index 5aaf23a7f8b..674e3771a8f 100644
--- a/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
+++ b/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
@@ -114,7 +114,10 @@ if defined?(DiscourseWorkflows)
when "unassign"
assigner.unassign
- { previously_assigned: assignee_data(previously_assigned, actor.guardian) }
+ {
+ previously_assigned:
+ serialize_assignee(previously_assigned, guardian: actor.guardian),
+ }
else
assignee = find_assignee(config["assignee"])
if config["replace_existing"] != false && target.assignment
@@ -134,8 +137,9 @@ if defined?(DiscourseWorkflows)
end
{
- assignee: assignee_data(assignee, actor.guardian),
- previously_assigned: assignee_data(previously_assigned, actor.guardian),
+ assignee: serialize_assignee(assignee, guardian: actor.guardian),
+ previously_assigned:
+ serialize_assignee(previously_assigned, guardian: actor.guardian),
}
end
end
@@ -147,27 +151,6 @@ if defined?(DiscourseWorkflows)
def find_assignee(identifier)
::User.find_by(username: identifier) || ::Group.find_by!(name: identifier)
end
-
- def assignee_data(assignee, guardian)
- case assignee
- when ::User
- {
- type: "user",
- user: serialize_record(assignee, BasicUserSerializer, scope: guardian),
- group: {
- },
- }
- when ::Group
- {
- type: "group",
- user: {
- },
- group: serialize_record(assignee, BasicGroupSerializer, scope: guardian),
- }
- else
- { type: nil, user: {}, group: {} }
- end
- end
end
end
end
diff --git a/plugins/discourse-assign/lib/discourse_workflows/nodes/assigned/v1.rb b/plugins/discourse-assign/lib/discourse_workflows/nodes/assigned/v1.rb
index 50aef7da6ad..ae7fc656605 100644
--- a/plugins/discourse-assign/lib/discourse_workflows/nodes/assigned/v1.rb
+++ b/plugins/discourse-assign/lib/discourse_workflows/nodes/assigned/v1.rb
@@ -42,7 +42,7 @@ if defined?(DiscourseWorkflows)
def output
{
- assignment: assignment_data,
+ assignment: serialize_assignment,
post: serialize_post(post),
topic: serialize_record(@assignment.topic, TopicListItemSerializer),
}
@@ -54,7 +54,7 @@ if defined?(DiscourseWorkflows)
private
- def assignment_data
+ def serialize_assignment
{
id: @assignment.id,
target_type: @assignment.target_type,
@@ -63,24 +63,13 @@ if defined?(DiscourseWorkflows)
topic_assignment: topic_assignment?,
assigned_to_id: @assignment.assigned_to_id,
assigned_to_type: @assignment.assigned_to_type,
- assigned_to: assignee_data(@assignment.assigned_to),
+ assigned_to: serialize_assignee(@assignment.assigned_to),
assigned_by_user: serialize_record(@assignment.assigned_by_user, BasicUserSerializer),
note: @assignment.note,
status: @assignment.status,
}
end
- def assignee_data(assignee)
- case assignee
- when ::User
- { type: "user", user: serialize_record(assignee, BasicUserSerializer), group: {} }
- when ::Group
- { type: "group", user: {}, group: serialize_record(assignee, BasicGroupSerializer) }
- else
- { type: nil, user: {}, group: {} }
- end
- end
-
def post
@post ||= @assignment.post
end
diff --git a/plugins/discourse-assign/lib/discourse_workflows/nodes/unassigned/v1.rb b/plugins/discourse-assign/lib/discourse_workflows/nodes/unassigned/v1.rb
index b362bbab83e..7fb2dfb077b 100644
--- a/plugins/discourse-assign/lib/discourse_workflows/nodes/unassigned/v1.rb
+++ b/plugins/discourse-assign/lib/discourse_workflows/nodes/unassigned/v1.rb
@@ -42,7 +42,7 @@ if defined?(DiscourseWorkflows)
def output
{
- assignment: assignment_data,
+ assignment: serialize_assignment,
post: serialize_post(post),
topic: serialize_record(@assignment.topic, TopicListItemSerializer),
}
@@ -54,7 +54,7 @@ if defined?(DiscourseWorkflows)
private
- def assignment_data
+ def serialize_assignment
{
id: @assignment.id,
target_type: @assignment.target_type,
@@ -63,24 +63,13 @@ if defined?(DiscourseWorkflows)
topic_assignment: topic_assignment?,
assigned_to_id: @assignment.assigned_to_id,
assigned_to_type: @assignment.assigned_to_type,
- assigned_to: assignee_data(@assignment.assigned_to),
+ assigned_to: serialize_assignee(@assignment.assigned_to),
assigned_by_user: serialize_record(@assignment.assigned_by_user, BasicUserSerializer),
note: @assignment.note,
status: @assignment.status,
}
end
- def assignee_data(assignee)
- case assignee
- when ::User
- { type: "user", user: serialize_record(assignee, BasicUserSerializer), group: {} }
- when ::Group
- { type: "group", user: {}, group: serialize_record(assignee, BasicGroupSerializer) }
- else
- { type: nil, user: {}, group: {} }
- end
- end
-
def post
@post ||= @assignment.post
end
diff --git a/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assign_topic/v1_spec.rb b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assign_topic/v1_spec.rb
index 7e683d71dad..2cf7f6adabe 100644
--- a/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assign_topic/v1_spec.rb
+++ b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assign_topic/v1_spec.rb
@@ -97,6 +97,8 @@ RSpec.describe DiscourseWorkflows::Nodes::AssignTopic::V1 do
expect(result["assignee"]["type"]).to eq("group")
expect(result["assignee"]["group"]["id"]).to eq(group.id)
expect(result["assignee"]["group"]["name"]).to eq(group.name)
+ expect(result["assignee"]["group"]).to include("user_count" => group.user_count)
+ expect(result["assignee"]["group"]).not_to include("incoming_email", "can_admin_group")
expect(Assignment.exists?(target: topic, assigned_to: group)).to eq(true)
expect(result).to match_node_output_schema(described_class)
end
diff --git a/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assigned/v1_spec.rb b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assigned/v1_spec.rb
index 20c8eb82a5f..1a302773afe 100644
--- a/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assigned/v1_spec.rb
+++ b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assigned/v1_spec.rb
@@ -77,7 +77,11 @@ RSpec.describe DiscourseWorkflows::Nodes::Assigned::V1 do
assigned_to_type: "Group",
)
expect(output[:assignment][:assigned_to][:type]).to eq("group")
- expect(output[:assignment][:assigned_to][:group][:name]).to eq(group.name)
+ expect(output[:assignment][:assigned_to][:group]).to include(
+ name: group.name,
+ user_count: group.user_count,
+ )
+ expect(output[:assignment][:assigned_to][:group]).not_to include(:incoming_email, :bio_raw)
expect(output[:post][:id]).to eq(topic.first_post.id)
expect(output).to match_node_output_schema(described_class)
end
diff --git a/plugins/discourse-events/lib/discourse_workflows/nodes/event_ended/v1.rb b/plugins/discourse-events/lib/discourse_workflows/nodes/event_ended/v1.rb
index c7027da6bf3..89b1e58ff6b 100644
--- a/plugins/discourse-events/lib/discourse_workflows/nodes/event_ended/v1.rb
+++ b/plugins/discourse-events/lib/discourse_workflows/nodes/event_ended/v1.rb
@@ -39,10 +39,11 @@ if defined?(DiscourseWorkflows)
def output
{
- event: event_data(starts_at: @event_date.starts_at, ends_at: @event_date.ends_at),
- post: post_data,
- topic: topic_data(topic),
- stats: stats_data,
+ event:
+ serialize_event(starts_at: @event_date.starts_at, ends_at: @event_date.ends_at),
+ post: serialize_event_post,
+ topic: serialize_topic_list_item(topic),
+ stats: serialize_event_stats,
}
end
diff --git a/plugins/discourse-events/lib/discourse_workflows/nodes/event_participation_changed/v1.rb b/plugins/discourse-events/lib/discourse_workflows/nodes/event_participation_changed/v1.rb
index 7bbf14a8fc9..c8c67605411 100644
--- a/plugins/discourse-events/lib/discourse_workflows/nodes/event_participation_changed/v1.rb
+++ b/plugins/discourse-events/lib/discourse_workflows/nodes/event_participation_changed/v1.rb
@@ -41,9 +41,9 @@ if defined?(DiscourseWorkflows)
def output
{
- event: event_data(starts_at: event.starts_at, ends_at: event.ends_at),
- post: post_data,
- topic: topic_data(topic),
+ event: serialize_event(starts_at: event.starts_at, ends_at: event.ends_at),
+ post: serialize_event_post,
+ topic: serialize_topic_list_item(topic),
user: serialize_user(user),
participation: {
status: removed? ? nil : status_name(@invitee.status),
@@ -51,7 +51,7 @@ if defined?(DiscourseWorkflows)
removed: removed?,
recurring: !removed? && [email protected],
},
- stats: stats_data,
+ stats: serialize_event_stats,
}
end
diff --git a/plugins/discourse-events/lib/discourse_workflows/nodes/post_event_scoping.rb b/plugins/discourse-events/lib/discourse_workflows/nodes/post_event_scoping.rb
index dc8b4b2ab4d..0162d4405cf 100644
--- a/plugins/discourse-events/lib/discourse_workflows/nodes/post_event_scoping.rb
+++ b/plugins/discourse-events/lib/discourse_workflows/nodes/post_event_scoping.rb
@@ -28,7 +28,7 @@ if defined?(DiscourseWorkflows)
topic_id.blank? || topic_id.to_i == topic&.id
end
- def event_data(starts_at:, ends_at:)
+ def serialize_event(starts_at:, ends_at:)
{
id: event.id,
name: event.name,
@@ -46,14 +46,14 @@ if defined?(DiscourseWorkflows)
}
end
- def post_data
+ def serialize_event_post
post = event&.post
return nil if post.blank?
{ id: post.id, post_number: post.post_number, url: post.url }
end
- def stats_data
+ def serialize_event_stats
serialize_record(event, ::DiscourseEvents::Events::EventStatsSerializer)
end
end
diff --git a/plugins/discourse-reactions/lib/discourse_workflows/nodes/post_reaction_changed/v1.rb b/plugins/discourse-reactions/lib/discourse_workflows/nodes/post_reaction_changed/v1.rb
index e9b93d57433..b8883cb0f35 100644
--- a/plugins/discourse-reactions/lib/discourse_workflows/nodes/post_reaction_changed/v1.rb
+++ b/plugins/discourse-reactions/lib/discourse_workflows/nodes/post_reaction_changed/v1.rb
@@ -75,7 +75,7 @@ if defined?(DiscourseWorkflows)
reaction:,
previous_reaction: @previous_reaction,
post: serialize_post(@post),
- topic: topic_data(@post.topic),
+ topic: serialize_topic_list_item(@post.topic),
user: serialize_user(@user),
}
end
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.rb
index 7a8bd0684d0..28fb047ac23 100644
--- a/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rb
+++ b/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rb
@@ -57,7 +57,11 @@ if defined?(DiscourseWorkflows)
end
def output
- { change: @change, post: serialize_post(@post), topic: topic_data(topic) }
+ {
+ change: @change,
+ post: serialize_post(@post),
+ topic: serialize_topic_list_item(topic),
+ }
end
def matches?(trigger_ctx)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/search_chat_channels.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/search_chat_channels.rb
index 02451ea6530..c6a16ed663d 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/search_chat_channels.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/search_chat_channels.rb
@@ -42,7 +42,7 @@ module DiscourseWorkflows
{
status: "success",
query: query,
- matches: channels_matching(query).map { |channel| channel_data(channel) },
+ matches: channels_matching(query).map { |channel| serialize_channel(channel) },
}
end
@@ -67,7 +67,7 @@ module DiscourseWorkflows
channels.order("LOWER(#{CHANNEL_NAME_SQL}) ASC").limit(MAX_RESULTS)
end
- def channel_data(channel)
+ def serialize_channel(channel)
category = channel.chatable if channel.chatable.is_a?(::Category)
{
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/entity_serializers.rb b/plugins/discourse-workflows/lib/discourse_workflows/entity_serializers.rb
new file mode 100644
index 00000000000..f10b27ac5cb
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/entity_serializers.rb
@@ -0,0 +1,90 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module EntitySerializers
+ module_function
+
+ def serialize_record(
+ record,
+ serializer,
+ scope: Discourse.system_user.guardian,
+ root: false,
+ **opts
+ )
+ MultiJson.load(serializer.new(record, scope:, root:, **opts).to_json).deep_symbolize_keys
+ end
+
+ def serialize_post(
+ post,
+ guardian: Discourse.system_user.guardian,
+ include_raw: true,
+ include_cooked: false
+ )
+ return if post.blank?
+
+ serialize_record(
+ post,
+ DiscourseWorkflows::PostSerializer,
+ scope: guardian,
+ include_raw:,
+ include_cooked:,
+ )
+ end
+
+ def serialize_topic(topic, guardian: Discourse.system_user.guardian, custom_field_names: [])
+ serialize_record(
+ topic,
+ DiscourseWorkflows::TopicSerializer,
+ scope: guardian,
+ custom_field_names:,
+ )
+ end
+
+ def serialize_topic_list_item(topic, scope: Discourse.system_user.guardian)
+ serialize_record(topic, DiscourseWorkflows::TopicListItemSerializer, scope:)
+ end
+
+ def serialize_user(user, guardian: Discourse.system_user.guardian)
+ return if user.blank?
+
+ serialize_record(user, DiscourseWorkflows::UserSerializer, scope: guardian)
+ end
+
+ def serialize_basic_group(group)
+ { id: group.id, name: group.name, full_name: group.full_name, automatic: group.automatic? }
+ end
+
+ def serialize_assignee(assignee, guardian: Discourse.system_user.guardian)
+ base = { type: nil, user: {}, group: {} }
+
+ case assignee
+ when ::User
+ base.merge(
+ type: "user",
+ user: serialize_record(assignee, BasicUserSerializer, scope: guardian),
+ )
+ when ::Group
+ base.merge(
+ type: "group",
+ group: serialize_record(assignee, BasicGroupSerializer, scope: Guardian.new),
+ )
+ else
+ base
+ end
+ end
+
+ def serialize_reviewable(reviewable)
+ {
+ id: reviewable.id,
+ type: reviewable.type,
+ status: reviewable.status,
+ target_type: reviewable.target_type,
+ target_id: reviewable.target_id,
+ topic_id: reviewable.topic_id,
+ category_id: reviewable.category_id,
+ score: reviewable.score,
+ created_at: reviewable.created_at&.iso8601,
+ }
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
index be1193fe883..d6b44857e4c 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
@@ -27,46 +27,6 @@ module DiscourseWorkflows
end
end
- def self.serialize_post(
- post,
- guardian: Discourse.system_user.guardian,
- include_raw: true,
- include_cooked: false
- )
- MultiJson.load(
- DiscourseWorkflows::PostSerializer.new(
- post,
- scope: guardian,
- root: false,
- include_raw: include_raw,
- include_cooked: include_cooked,
- ).to_json,
- ).deep_symbolize_keys
- end
-
- def self.serialize_topic(
- topic,
- guardian: Discourse.system_user.guardian,
- custom_field_names: []
- )
- MultiJson.load(
- DiscourseWorkflows::TopicSerializer.new(
- topic,
- scope: guardian,
- root: false,
- custom_field_names: custom_field_names,
- ).to_json,
- ).deep_symbolize_keys
- end
-
- def self.serialize_user(user, guardian: Discourse.system_user.guardian)
- return if user.blank?
-
- MultiJson.load(
- DiscourseWorkflows::UserSerializer.new(user, scope: guardian, root: false).to_json,
- ).deep_symbolize_keys
- end
-
class RuntimeState
MAX_ITEM_ERROR_GROUPS = 20
@@ -486,19 +446,6 @@ module DiscourseWorkflows
post
end
- def serialize_post(
- post,
- guardian: Discourse.system_user.guardian,
- include_raw: true,
- include_cooked: false
- )
- self.class.serialize_post(post, guardian:, include_raw:, include_cooked:)
- end
-
- def serialize_topic(topic, guardian: Discourse.system_user.guardian, custom_field_names: [])
- self.class.serialize_topic(topic, guardian:, custom_field_names:)
- end
-
def put_execution_to_wait(
waiting_until = nil,
kind: "default",
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
index 1c05154ae31..84aeb7f8adb 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
@@ -3,7 +3,9 @@
module DiscourseWorkflows
class NodeType
include NodeErrorHandling
+ include EntitySerializers
extend NodeTypeDescriptor
+ extend EntitySerializers
TOPIC_TYPE_OPTIONS = %w[all topics personal_messages].freeze
@@ -515,64 +517,10 @@ module DiscourseWorkflows
reviewable_types.empty? || reviewable_types.include?(reviewable.class.sti_name)
end
- def reviewable_data(reviewable)
- {
- id: reviewable.id,
- type: reviewable.type,
- status: reviewable.status,
- target_type: reviewable.target_type,
- target_id: reviewable.target_id,
- topic_id: reviewable.topic_id,
- category_id: reviewable.category_id,
- score: reviewable.score,
- created_at: reviewable.created_at&.iso8601,
- }
- end
-
def wrap(data, paired_item: nil)
Item.wrap(data, paired_item:)
end
- def serialize_record(
- record,
- serializer,
- scope: Discourse.system_user.guardian,
- root: false,
- **opts
- )
- MultiJson.load(serializer.new(record, scope:, root:, **opts).to_json).deep_symbolize_keys
- end
-
- def serialize_post(
- post,
- guardian: Discourse.system_user.guardian,
- include_raw: true,
- include_cooked: false
- )
- DiscourseWorkflows::Executor::NodeExecutionContext.serialize_post(
- post,
- guardian:,
- include_raw:,
- include_cooked:,
- )
- end
-
- def serialize_topic(topic, guardian: Discourse.system_user.guardian, custom_field_names: [])
- DiscourseWorkflows::Executor::NodeExecutionContext.serialize_topic(
- topic,
- guardian:,
- custom_field_names:,
- )
- end
-
- def topic_data(topic, scope: Discourse.system_user.guardian)
- serialize_record(topic, DiscourseWorkflows::TopicListItemSerializer, scope:)
- end
-
- def serialize_user(user, guardian: Discourse.system_user.guardian)
- DiscourseWorkflows::Executor::NodeExecutionContext.serialize_user(user, guardian:)
- end
-
def with_paired_item(item, paired_item)
Item.with_paired_item(item, paired_item)
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/badge_granted/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/badge_granted/v1.rb
index 1044b64d1da..e28fa5b1457 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/badge_granted/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/badge_granted/v1.rb
@@ -58,7 +58,7 @@ module DiscourseWorkflows
end
def output
- { user: serialize_user(@user), badge: badge_data }
+ { user: serialize_user(@user), badge: serialize_badge }
end
def matches?(trigger_ctx)
@@ -68,7 +68,7 @@ module DiscourseWorkflows
private
- def badge_data
+ def serialize_badge
{
id: @badge.id,
name: @badge.name,
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/group/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/group/v1.rb
index 653cf46754c..2a117c9c0fa 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/group/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/group/v1.rb
@@ -135,7 +135,7 @@ module DiscourseWorkflows
actor = exec_ctx.actor_from_parameter("actor_username", item_index)
guardian = actor.guardian
guardian.ensure_can_see_group!(group)
- serialized_group = group_data(group, guardian)
+ serialized_group = serialize_group(group, guardian)
return { group: serialized_group } if config["operation"] == "get"
@@ -184,7 +184,7 @@ module DiscourseWorkflows
)
end
- def group_data(group, guardian)
+ def serialize_group(group, guardian)
serialize_record(group, WebHookGroupSerializer, scope: guardian)
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
index 455f3a11f1f..1c9794a431b 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb
@@ -378,7 +378,7 @@ module DiscourseWorkflows
{
post:
- exec_ctx.serialize_post(
+ serialize_post(
post,
guardian: actor.guardian,
include_raw: true,
@@ -393,7 +393,7 @@ module DiscourseWorkflows
{
post:
- exec_ctx.serialize_post(
+ serialize_post(
post,
guardian: editor.guardian,
include_raw: true,
@@ -406,14 +406,14 @@ module DiscourseWorkflows
actor = exec_ctx.actor_from_parameter("actor_username", item_index)
post = exec_ctx.destroy_post(user: actor, post_id: config["post_id"])
- { post: exec_ctx.serialize_post(post, guardian: actor.guardian) }
+ { post: serialize_post(post, guardian: actor.guardian) }
end
def recover_post(exec_ctx, config, item_index)
actor = exec_ctx.actor_from_parameter("actor_username", item_index)
post = exec_ctx.recover_post(user: actor, post_id: config["post_id"])
- { post: exec_ctx.serialize_post(post, guardian: actor.guardian) }
+ { post: serialize_post(post, guardian: actor.guardian) }
end
def get_post(exec_ctx, config, item_index)
@@ -421,7 +421,7 @@ module DiscourseWorkflows
actor = exec_ctx.actor_from_parameter("actor_username", item_index)
raise Discourse::InvalidAccess if !actor.guardian.can_see?(post)
- { post: serialized_post(exec_ctx, post, guardian: actor.guardian, config: config) }
+ { post: serialized_post(post, guardian: actor.guardian, config: config) }
end
def list_posts(exec_ctx, config, item_index)
@@ -442,13 +442,13 @@ module DiscourseWorkflows
posts = filter.search.includes(:user, topic: %i[category tags])
posts.map do |post|
- { post: serialized_post(exec_ctx, post, guardian: actor.guardian, config: config) }
+ { post: serialized_post(post, guardian: actor.guardian, config: config) }
end
end
- def serialized_post(exec_ctx, post, guardian:, config:)
+ def serialized_post(post, guardian:, config:)
data =
- exec_ctx.serialize_post(
+ serialize_post(
post,
guardian: guardian,
include_raw: config["include_raw"],
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_button/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_button/v1.rb
index b606375dae5..92a8f8088d9 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_button/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_button/v1.rb
@@ -179,13 +179,7 @@ module DiscourseWorkflows
end
def output
- { post: post_data(@post), topic: topic_data(@post.topic) }
- end
-
- private
-
- def post_data(post)
- serialize_post(post)
+ { post: serialize_post(@post), topic: serialize_topic_list_item(@post.topic) }
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb
index 685f40a8f43..ac59c25e790 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb
@@ -73,7 +73,11 @@ module DiscourseWorkflows
end
def output
- { post: post_data(@post), topic: topic_data(@post.topic), user: user_data(@post.user) }
+ {
+ post: serialize_post(@post),
+ topic: serialize_topic_list_item(@post.topic),
+ user: serialize_user(@post.user),
+ }
end
def matches?(trigger_ctx)
@@ -84,16 +88,6 @@ module DiscourseWorkflows
matches_group_inbox?(topic, trigger_ctx.get_node_parameter("group_inbox_id"))
end
-
- private
-
- def post_data(post)
- serialize_post(post)
- end
-
- def user_data(user)
- serialize_user(user)
- 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 c50f449bea0..7e7f3c1e39c 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
@@ -63,7 +63,7 @@ module DiscourseWorkflows
def output
{
post: serialize_post(@post, include_cooked: true).merge(cooked: @cooked),
- topic: topic_data(@post.topic),
+ topic: serialize_topic_list_item(@post.topic),
user: serialize_user(@post.user),
editor: serialize_user(@revisor&.editor),
}
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 8be7dba1f1c..a66cc2926a2 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_lifecycle.rb
@@ -29,7 +29,11 @@ module DiscourseWorkflows
end
def output
- { post: serialize_post(@post), topic: topic_data(topic), user: serialize_user(@user) }
+ {
+ post: serialize_post(@post),
+ topic: serialize_topic_list_item(topic),
+ user: serialize_user(@user),
+ }
end
def matches?(trigger_ctx)
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 3f32c63b2da..ce60bfbdc68 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
@@ -58,7 +58,7 @@ module DiscourseWorkflows
{
change: @change,
post: serialize_post(@post),
- topic: topic_data(@post.topic),
+ topic: serialize_topic_list_item(@post.topic),
user: serialize_user(@user),
}
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_moved/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_moved/v1.rb
index 1236a12da34..025a46a310b 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_moved/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_moved/v1.rb
@@ -46,9 +46,9 @@ module DiscourseWorkflows
def output
{
- post: post_data(@post),
- topic: topic_data(destination_topic),
- original_topic: topic_data(original_topic),
+ post: serialize_post(@post),
+ topic: serialize_topic_list_item(destination_topic),
+ original_topic: serialize_topic_list_item(original_topic),
}
end
@@ -58,10 +58,6 @@ module DiscourseWorkflows
private
- def post_data(post)
- serialize_post(post)
- end
-
def destination_topic
@destination_topic ||= ::Topic.find_by(id: @post&.topic_id)
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_approved/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_approved/v1.rb
index fc299a56fb1..9e767c42714 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_approved/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_approved/v1.rb
@@ -21,7 +21,7 @@ module DiscourseWorkflows
end
def output
- { reviewable: reviewable_data(@reviewable) }
+ { reviewable: serialize_reviewable(@reviewable) }
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_created/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_created/v1.rb
index 738c1f8fb38..96023943cf1 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_created/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_created/v1.rb
@@ -45,7 +45,7 @@ module DiscourseWorkflows
end
def output
- { reviewable: reviewable_data(@reviewable) }
+ { reviewable: serialize_reviewable(@reviewable) }
end
def matches?(trigger_ctx)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_status_changed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_status_changed/v1.rb
index c8fc5b6449f..a9a82fbe151 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_status_changed/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/reviewable_status_changed/v1.rb
@@ -56,7 +56,7 @@ module DiscourseWorkflows
end
def output
- { reviewable: reviewable_data(@reviewable).merge(status: @status) }
+ { reviewable: serialize_reviewable(@reviewable).merge(status: @status) }
end
def matches?(trigger_ctx)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/send_personal_message/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/send_personal_message/v1.rb
index 7e55f8e3dbf..1addd2c24ec 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/send_personal_message/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/send_personal_message/v1.rb
@@ -104,9 +104,9 @@ module DiscourseWorkflows
post = PostCreator.new(sender, post_args).create!
{
- topic: topic_data(post.topic, scope: sender.guardian),
+ topic: serialize_topic_list_item(post.topic, scope: sender.guardian),
post:
- exec_ctx.serialize_post(
+ serialize_post(
post,
guardian: sender.guardian,
include_raw: true,
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/stale_topic/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/stale_topic/v1.rb
index e35e0485af2..ca40e488657 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/stale_topic/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/stale_topic/v1.rb
@@ -42,7 +42,7 @@ module DiscourseWorkflows
include_subcategories: include_subcategories,
tag_names: tag_names,
limit: MAX_TOPICS_PER_RUN,
- ).map { |topic| { topic: topic_data(topic) } }
+ ).map { |topic| { topic: serialize_topic_list_item(topic) } }
end
def self.stale_topics(
@@ -75,16 +75,6 @@ module DiscourseWorkflows
scope.limit(limit).to_a
end
-
- def self.topic_data(topic)
- MultiJson.load(
- DiscourseWorkflows::TopicListItemSerializer.new(
- topic,
- scope: Discourse.system_user.guardian,
- root: false,
- ).to_json,
- ).deep_symbolize_keys
- 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 fc0e6cc6597..07989ed04aa 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb
@@ -301,8 +301,8 @@ module DiscourseWorkflows
topic = post.topic
{
- topic: exec_ctx.serialize_topic(topic, guardian: guardian),
- post: post_data(post),
+ topic: serialize_topic(topic, guardian: guardian),
+ post: serialize_topic_post(post),
post_id: post.id,
post_number: post.post_number,
}
@@ -317,12 +317,12 @@ module DiscourseWorkflows
{
topic:
- exec_ctx.serialize_topic(
+ serialize_topic(
topic,
guardian: actor.guardian,
custom_field_names: custom_field_names,
),
- post: post_data(topic.first_post),
+ post: serialize_topic_post(topic.first_post),
}
end
@@ -361,12 +361,12 @@ module DiscourseWorkflows
topics.map do |topic|
{
topic:
- exec_ctx.serialize_topic(
+ serialize_topic(
topic,
guardian: actor.guardian,
custom_field_names: custom_field_names,
),
- post: post_data(topic.first_post),
+ post: serialize_topic_post(topic.first_post),
}
end
end
@@ -380,8 +380,8 @@ module DiscourseWorkflows
topic.reload
{
- topic: exec_ctx.serialize_topic(topic, guardian: actor.guardian),
- post: post_data(topic.first_post),
+ topic: serialize_topic(topic, guardian: actor.guardian),
+ post: serialize_topic_post(topic.first_post),
}
end
@@ -395,8 +395,8 @@ module DiscourseWorkflows
topic.reload
{
- topic: exec_ctx.serialize_topic(topic, guardian: guardian),
- post: post_data(topic.first_post),
+ topic: serialize_topic(topic, guardian: guardian),
+ post: serialize_topic_post(topic.first_post),
}
end
@@ -421,8 +421,8 @@ module DiscourseWorkflows
topic.reload
{
- topic: exec_ctx.serialize_topic(topic, guardian: guardian),
- post: post_data(topic.first_post),
+ topic: serialize_topic(topic, guardian: guardian),
+ post: serialize_topic_post(topic.first_post),
}
end
@@ -440,11 +440,7 @@ module DiscourseWorkflows
{
topic:
- exec_ctx.serialize_topic(
- topic,
- guardian: guardian,
- custom_field_names: custom_fields.keys,
- ),
+ serialize_topic(topic, guardian: guardian, custom_field_names: custom_fields.keys),
}
end
@@ -457,7 +453,7 @@ module DiscourseWorkflows
end
end
- def post_data(post)
+ def serialize_topic_post(post)
return if post.blank?
serialize_record(post, WebHookPostSerializer)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_admin_button/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_admin_button/v1.rb
index 872cb4f4d6d..4581cdaf676 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_admin_button/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_admin_button/v1.rb
@@ -35,7 +35,7 @@ module DiscourseWorkflows
end
def output
- { topic: topic_data(@topic) }
+ { topic: serialize_topic_list_item(@topic) }
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_category_changed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_category_changed/v1.rb
index 3181874cc0f..53b5eb0498f 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_category_changed/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_category_changed/v1.rb
@@ -41,7 +41,7 @@ module DiscourseWorkflows
end
def output
- { topic: topic_data(@topic), old_category_id: @old_category.id }
+ { topic: serialize_topic_list_item(@topic), old_category_id: @old_category.id }
end
private
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rb
index 6b98777b986..29e161674a2 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
@@ -15,7 +15,7 @@ module DiscourseWorkflows
)
def output
- { topic: topic_data(@topic) }
+ { topic: serialize_topic_list_item(@topic) }
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb
index 1c3ba95d0c7..0149453ccb8 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb
@@ -66,7 +66,7 @@ module DiscourseWorkflows
end
def output
- { post: post_data(@topic.first_post), topic: topic_data(@topic) }
+ { post: serialize_post(@topic.first_post), topic: serialize_topic_list_item(@topic) }
end
def matches?(trigger_ctx)
@@ -75,12 +75,6 @@ module DiscourseWorkflows
matches_group_inbox?(@topic, trigger_ctx.get_node_parameter("group_inbox_id"))
end
-
- private
-
- def post_data(post)
- serialize_post(post)
- end
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_moderation/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_moderation/v1.rb
index a19c0483d4d..aa3b6a1016f 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_moderation/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_moderation/v1.rb
@@ -69,7 +69,7 @@ module DiscourseWorkflows
)
topic.reload
- { topic: exec_ctx.serialize_topic(topic, guardian: actor.guardian) }
+ { topic: serialize_topic(topic, guardian: actor.guardian) }
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_published/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_published/v1.rb
index a184bd87fec..85c8f63b241 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_published/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_published/v1.rb
@@ -44,7 +44,7 @@ module DiscourseWorkflows
end
def output
- { topic: topic_data(@topic), published_at: @published_at.iso8601 }
+ { topic: serialize_topic_list_item(@topic), published_at: @published_at.iso8601 }
end
def matches?(trigger_ctx)
diff --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.rb
index 41db5b0bbb1..04ff4a4d6a1 100644
--- 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.rb
@@ -74,7 +74,12 @@ module DiscourseWorkflows
end
def output
- { topic: topic_data(@topic), status: @status, enabled: @enabled, change: change }
+ {
+ topic: serialize_topic_list_item(@topic),
+ status: @status,
+ enabled: @enabled,
+ change: change,
+ }
end
def matches?(trigger_ctx)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tag_changed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tag_changed/v1.rb
index 893b903be42..64cf40555d0 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tag_changed/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tag_changed/v1.rb
@@ -45,7 +45,7 @@ module DiscourseWorkflows
def output
{
- topic: topic_data(@topic),
+ topic: serialize_topic_list_item(@topic),
old_tags: @old_tag_names,
new_tags: @new_tag_names,
added_tags: added_tags,
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_timer_changed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_timer_changed/v1.rb
index 27ab6d805d1..94e04ba2d23 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_timer_changed/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_timer_changed/v1.rb
@@ -93,10 +93,10 @@ module DiscourseWorkflows
def output
{
- topic: topic_data(@topic),
+ topic: serialize_topic_list_item(@topic),
change: @change,
- timer: timer_data(@timer.attributes),
- previous_timer: @previous_attributes && timer_data(@previous_attributes),
+ timer: serialize_timer(@timer.attributes),
+ previous_timer: @previous_attributes && serialize_timer(@previous_attributes),
}
end
@@ -114,7 +114,7 @@ module DiscourseWorkflows
private
- def timer_data(attributes)
+ def serialize_timer(attributes)
attributes.slice(*::TopicTimer::EVENT_ATTRIBUTES).merge(
"id" => @timer.id,
"status_type" => ::TopicTimer.types[attributes["status_type"]].to_s,
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb
index fc941cf6ae3..71c5701b7fe 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb
@@ -152,7 +152,7 @@ module DiscourseWorkflows
def get_user(user, guardian, extensions:)
guardian.ensure_can_see_profile!(user)
- { user: user_data(user, guardian, extensions:) }
+ { user: serialize_user_details(user, guardian, extensions:) }
end
def edit_user(exec_ctx, user, actor, item_index, extensions:)
@@ -169,7 +169,7 @@ module DiscourseWorkflows
update_trust_level_lock(user, actor, guardian, trust_level_locked)
end
- { user: user_data(user.reload, guardian, extensions:) }
+ { user: serialize_user_details(user.reload, guardian, extensions:) }
end
def update_parameters(exec_ctx, item_index)
@@ -263,7 +263,7 @@ module DiscourseWorkflows
)
end
- def user_data(user, guardian, extensions:)
+ def serialize_user_details(user, guardian, extensions:)
profile = user.user_profile if include_profile_details?(user, guardian)
serialize_user(user, guardian: guardian).merge(
@@ -275,7 +275,7 @@ module DiscourseWorkflows
manual_locked_trust_level: user.manual_locked_trust_level,
trust_level_locked: !user.manual_locked_trust_level.nil?,
user_fields: profile ? user.user_fields(guardian.allowed_user_field_ids(user)) : {},
- groups: groups_data(user, guardian),
+ groups: serialize_groups(user, guardian),
).merge(extensions_data(user, guardian, extensions))
end
@@ -360,20 +360,11 @@ module DiscourseWorkflows
guardian.public_can_see_profiles? && !guardian.restrict_user_fields?(user)
end
- def groups_data(user, guardian)
+ def serialize_groups(user, guardian)
groups = user.groups.visible_groups(guardian.user)
groups = groups.members_visible_groups(guardian.user) if guardian.user != user
- groups
- .order(:name)
- .map do |group|
- {
- id: group.id,
- name: group.name,
- full_name: group.full_name,
- automatic: group.automatic,
- }
- end
+ groups.order(:name).map { |group| serialize_basic_group(group) }
end
def validation_errors_for(user)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user_added_to_group/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user_added_to_group/v1.rb
index c0c2d105849..ce66344f2e0 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user_added_to_group/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user_added_to_group/v1.rb
@@ -60,7 +60,7 @@ module DiscourseWorkflows
def output
{
user: serialize_user(@user),
- group: group_data,
+ group: serialize_basic_group(@group),
membership: {
action: "added",
automatic: @automatic == true,
@@ -72,17 +72,6 @@ module DiscourseWorkflows
group_id = trigger_ctx.get_node_parameter("group_id")
group_id.present? && @group.id == group_id.to_i
end
-
- private
-
- def group_data
- {
- id: @group.id,
- name: @group.name,
- full_name: @group.full_name,
- automatic: @group.automatic?,
- }
- end
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user_removed_from_group/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user_removed_from_group/v1.rb
index de52e3cdee1..4f68b8bcea2 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user_removed_from_group/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user_removed_from_group/v1.rb
@@ -58,7 +58,7 @@ module DiscourseWorkflows
def output
{
user: serialize_user(@user),
- group: group_data,
+ group: serialize_basic_group(@group),
membership: {
action: "removed",
automatic: nil,
@@ -70,17 +70,6 @@ module DiscourseWorkflows
group_id = trigger_ctx.get_node_parameter("group_id")
group_id.present? && @group.id == group_id.to_i
end
-
- private
-
- def group_data
- {
- id: @group.id,
- name: @group.name,
- full_name: @group.full_name,
- automatic: @group.automatic?,
- }
- 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 bc64d778761..12f738a47e6 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/schema.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/schema.rb
@@ -165,7 +165,7 @@ module DiscourseWorkflows
}
JSON
- TOPIC_LIST_ITEM_SCHEMA = entity("topic", TOPIC_PROPERTIES, "TopicListItemSerializer payload")
+ TOPIC_LIST_ITEM_SCHEMA = entity("topic", TOPIC_PROPERTIES, "Workflow topic payload")
POST_SCHEMA = entity("post", POST_PROPERTIES, "DiscourseWorkflows::PostSerializer payload")
BASIC_USER_SCHEMA = entity("user", BASIC_USER_PROPERTIES, "BasicUserSerializer payload")
USER_SCHEMA = entity("user", USER_PROPERTIES, "Basic safe user attributes")
@@ -187,7 +187,9 @@ module DiscourseWorkflows
}
JSON
- USER_ACTION_PROPERTIES = JSON.parse(<<~JSON).freeze
+ USER_ACTION_PROPERTIES =
+ JSON
+ .parse(<<~JSON)
{
"title": { "type": ["string", "null"] },
"bio_raw": { "type": ["string", "null"] },
@@ -196,21 +198,19 @@ module DiscourseWorkflows
"card_background_upload_id": { "type": ["integer", "null"] },
"manual_locked_trust_level": { "type": ["integer", "null"] },
"trust_level_locked": { "type": "boolean" },
- "user_fields": { "type": "object" },
- "groups": {
- "type": "array",
- "items": {
- "type": "object",
- "properties": {
- "id": { "type": "integer" },
- "name": { "type": "string" },
- "full_name": { "type": ["string", "null"] },
- "automatic": { "type": "boolean" }
- }
- }
- }
+ "user_fields": { "type": "object" }
}
JSON
+ .merge(
+ "groups" => {
+ "type" => "array",
+ "items" => {
+ "type" => "object",
+ "properties" => BASIC_GROUP_PROPERTIES,
+ },
+ },
+ )
+ .freeze
USER_STATS_PROPERTIES = JSON.parse(<<~JSON).freeze
{
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/entity_serializers_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/entity_serializers_spec.rb
new file mode 100644
index 00000000000..68224ca9392
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/entity_serializers_spec.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::EntitySerializers do
+ describe ".serialize_post" do
+ fab!(:user)
+ fab!(:post) { Fabricate(:post, user: user, raw: "Workflow post body") }
+
+ it "serializes a post for workflow output" do
+ result = described_class.serialize_post(post, guardian: user.guardian, include_cooked: true)
+
+ expect(result).to include(
+ id: post.id,
+ topic_id: post.topic_id,
+ topic_title: post.topic.title,
+ post_number: post.post_number,
+ username: user.username,
+ raw: "Workflow post body",
+ cooked: post.cooked,
+ )
+ end
+
+ it "can omit raw and cooked body fields" do
+ result =
+ described_class.serialize_post(
+ post,
+ guardian: user.guardian,
+ include_raw: false,
+ include_cooked: false,
+ )
+
+ expect(result).not_to include(:raw, :cooked)
+ end
+
+ it "returns nil for a missing post" do
+ expect(described_class.serialize_post(nil)).to be_nil
+ end
+ end
+
+ describe ".serialize_topic" do
+ fab!(:user)
+ fab!(:topic) { Fabricate(:topic, user: user) }
+
+ it "serializes a topic for workflow output" do
+ topic.custom_fields["workflow_key"] = "workflow value"
+ topic.custom_fields["other_key"] = "other value"
+ topic.save_custom_fields
+ result =
+ described_class.serialize_topic(
+ topic.reload,
+ guardian: user.guardian,
+ custom_field_names: ["workflow_key"],
+ )
+
+ expect(result).to include(
+ id: topic.id,
+ title: topic.title,
+ category_id: topic.category_id,
+ custom_fields: {
+ workflow_key: "workflow value",
+ },
+ )
+ expect(result[:custom_fields]).not_to include(:other_key)
+ end
+
+ it "omits custom fields by default" do
+ topic.custom_fields["workflow_key"] = "workflow value"
+ topic.save_custom_fields
+ result = described_class.serialize_topic(topic, guardian: user.guardian)
+
+ expect(result).not_to include(:custom_fields)
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
index 922b2f13c00..0bb14f4ea17 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/node_execution_context_spec.rb
@@ -1001,75 +1001,6 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext do
end
end
- describe "#serialize_post" do
- fab!(:user)
- fab!(:post) { Fabricate(:post, user: user, raw: "Workflow post body") }
-
- it "serializes a post for workflow output" do
- ctx = described_class.new(input_items: [], resolver: nil)
-
- result = ctx.serialize_post(post, guardian: user.guardian, include_cooked: true)
-
- expect(result).to include(
- id: post.id,
- topic_id: post.topic_id,
- topic_title: post.topic.title,
- post_number: post.post_number,
- username: user.username,
- raw: "Workflow post body",
- cooked: post.cooked,
- )
- end
-
- it "can omit raw and cooked body fields" do
- ctx = described_class.new(input_items: [], resolver: nil)
-
- result =
- ctx.serialize_post(post, guardian: user.guardian, include_raw: false, include_cooked: false)
-
- expect(result).not_to include(:raw, :cooked)
- end
- end
-
- describe "#serialize_topic" do
- fab!(:user)
- fab!(:topic) { Fabricate(:topic, user: user) }
-
- it "serializes a topic for workflow output" do
- topic.custom_fields["workflow_key"] = "workflow value"
- topic.custom_fields["other_key"] = "other value"
- topic.save_custom_fields
- ctx = described_class.new(input_items: [], resolver: nil)
-
- result =
- ctx.serialize_topic(
- topic.reload,
- guardian: user.guardian,
- custom_field_names: ["workflow_key"],
- )
-
- expect(result).to include(
- id: topic.id,
- title: topic.title,
- category_id: topic.category_id,
- custom_fields: {
- workflow_key: "workflow value",
- },
- )
- expect(result[:custom_fields]).not_to include(:other_key)
- end
-
- it "omits custom fields by default" do
- topic.custom_fields["workflow_key"] = "workflow value"
- topic.save_custom_fields
- ctx = described_class.new(input_items: [], resolver: nil)
-
- result = ctx.serialize_topic(topic, guardian: user.guardian)
-
- expect(result).not_to include(:custom_fields)
- end
- end
-
describe "#http_request" do
it "returns a parsed response object" do
stub_request(:get, "https://api.example.com/data").to_return(