A platform for community discussion. Free, open, simple.
FEATURE: Add custom fields to workflow topic node (#40544)
Joffrey JAFFEUX63f9a5b389ef
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/multi-combo-box.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/multi-combo-box.gjsindex 2345e6f7c2b..11269250ed1 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/multi-combo-box.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/multi-combo-box.gjs@@ -1,16 +1,128 @@import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";import { hash } from "@ember/helper";+import { action } from "@ember/object";+import { service } from "@ember/service";+import { makeArray } from "discourse/lib/helpers";import MultiSelect from "discourse/select-kit/components/multi-select";import {formatOptionValue,normalizeOptions,propertyOptionLabel,+ propertySelectNoneKey,} from "../../../lib/workflows/property-engine";import ExpressionWrapper from "./expression-wrapper";+export class DynamicOptionsMultiSelect extends MultiSelect {+ search(filter) {+ if (this.loadOptions) {+ return Promise.resolve(this.loadOptions(filter)).then((options) => {+ const selectedValues = new Set(+ makeArray(this.selectedContent).map((item) =>+ String(this.getValue(item))+ )+ );++ return options.filter(+ (option) => !selectedValues.has(String(this.getValue(option)))+ );+ });+ }++ return super.search(filter);+ }+}++function optionValue(option, valueProperty) {+ return option[valueProperty] ?? option.id ?? option.value;+}++function optionName(option, nameProperty) {+ return option[nameProperty] ?? option.name ?? option.label;+}+export default class MultiComboBox extends Component {+ @service workflowsNodeTypes;++ @tracked selectedOptions = [];++ get methodName() {+ return this.args.schema?.type_options?.load_options_method;+ }++ get identifier() {+ return (+ this.args.nodeDefinition?.name || this.args.nodeDefinition?.identifier+ );+ }++ get typeVersion() {+ return this.args.nodeDefinition?.version || this.args.node?.typeVersion;+ }++ get localOptions() {+ return (+ this.args.metadata?.[this.methodName] ||+ this.args.nodeDefinition?.metadata?.[this.methodName]+ );+ }++ get hasLoadOptionsDependencies() {+ const dependencies =+ this.args.schema?.type_options?.load_options_depends_on;++ return Array.isArray(dependencies)+ ? dependencies.length > 0+ : Boolean(dependencies);+ }++ get usesRemoteOptions() {+ return Boolean(+ this.methodName &&+ this.identifier &&+ (!this.localOptions || this.hasLoadOptionsDependencies)+ );+ }++ get metadataOptions() {+ if (!this.methodName) {+ return null;+ }+ if (this.localOptions) {+ return this.localOptions;+ }+ return [];+ }++ get controlOptions() {+ return this.args.schema?.control_options || {};+ }++ get none() {+ return (+ this.controlOptions.none ||+ propertySelectNoneKey(this.args.nodeDefinition, this.args.fieldName)+ );+ }++ get filterable() {+ return Boolean(this.controlOptions.filterable);+ }++ get valueProperty() {+ return this.controlOptions.value_property || "id";+ }++ get nameProperty() {+ return this.controlOptions.name_property || "name";+ }+get options() {- const optionFormat = this.args.schema?.control_options?.option_format;+ if (this.metadataOptions) {+ return this.formatOptions(this.metadataOptions);+ }++ const optionFormat = this.controlOptions.option_format;return normalizeOptions(this.args.schema.options).map((option) => ({id: option.value,name: optionFormat@@ -20,14 +132,77 @@ export default class MultiComboBox extends Component {this.args.fieldName,option),+ original: option,+ }));+ }++ get contentOptions() {+ if (!this.usesRemoteOptions) {+ return this.options;+ }++ const optionsByValue = new Map(+ [+ ...this.formatOptions(this.metadataOptions || []),+ ...this.selectedOptions,+ ].map((option) => [String(option.id), option])+ );++ return this.value.map(+ (value) => optionsByValue.get(String(value)) || { id: value, name: value }+ );+ }++ formatOptions(options) {+ return options.map((option) => ({+ id: optionValue(option, this.valueProperty),+ name:+ optionName(option, this.nameProperty) ||+ optionValue(option, this.valueProperty),+ original: option,}));}+ remoteOptionsContext(filter = null) {+ const context = {+ path: this.args.fieldName,+ currentNodeParameters:+ this.args.nodeParameters || this.args.configuration || {},+ credentials: this.args.credentials || {},+ node: this.args.node,+ filter,+ };++ return this.args.session?.nodeParameterOptionsContext(context) || context;+ }++ @action+ async loadRemoteOptions(filter = null) {+ if (!this.usesRemoteOptions) {+ return null;+ }++ const options = await this.workflowsNodeTypes.loadNodeParameterOptions(+ this.identifier,+ this.methodName,+ this.typeVersion,+ this.remoteOptionsContext(filter)+ );++ return this.formatOptions(options);+ }+get value() {const val = this.args.field.value;return Array.isArray(val) ? val : [];}+ @action+ handleChange(value, selectedOptions) {+ this.selectedOptions = makeArray(selectedOptions);+ this.args.field.set(value);+ }+<template><ExpressionWrapper@field={{@field}}@@ -36,11 +211,14 @@ export default class MultiComboBox extends Component {@dynamicValueHint={{@dynamicValueHint}}@session={{@session}}>- <MultiSelect- @content={{this.options}}+ <DynamicOptionsMultiSelect+ @content={{this.contentOptions}}+ @loadOptions={{if this.usesRemoteOptions this.loadRemoteOptions}}@value={{this.value}}- @options={{hash translatedNone=@placeholder}}- @onChange={{@field.set}}+ @nameProperty="name"+ @valueProperty="id"+ @onChange={{this.handleChange}}+ @options={{hash filterable=this.filterable none=this.none}}/></ExpressionWrapper></template>diff --git a/plugins/discourse-workflows/app/serializers/discourse_workflows/topic_serializer.rb b/plugins/discourse-workflows/app/serializers/discourse_workflows/topic_serializer.rbnew file mode 100644index 00000000000..e55aa49e55c--- /dev/null+++ b/plugins/discourse-workflows/app/serializers/discourse_workflows/topic_serializer.rb@@ -0,0 +1,136 @@+# frozen_string_literal: true++module DiscourseWorkflows+ class TopicSerializer < ApplicationSerializer+ include LocalizedFancyTopicTitleMixin+ include TopicTagsMixin++ attributes :id,+ :title,+ :slug,+ :posts_count,+ :reply_count,+ :highest_post_number,+ :created_at,+ :last_posted_at,+ :bumped,+ :bumped_at,+ :archetype,+ :visible,+ :closed,+ :archived,+ :has_summary,+ :views,+ :like_count,+ :category_id,+ :user_id,+ :last_post_user_id,+ :last_poster_username,+ :first_post_id,+ :pinned_globally,+ :featured_link,+ :featured_link_root_domain,+ :pinned,+ :unpinned,+ :excerpt,+ :posters,+ :participants,+ :custom_fields++ def initialize(object, opts)+ super+ @custom_field_names =+ Array(opts.fetch(:custom_field_names, []))+ .filter_map { |name| name.to_s.strip.presence }+ .uniq+ end++ def created_at+ object.created_at&.utc&.iso8601+ end++ def last_posted_at+ object.last_posted_at&.utc&.iso8601+ end++ def bumped_at+ object.bumped_at&.utc&.iso8601+ end++ def bumped+ object.created_at.present? && object.bumped_at.present? &&+ object.created_at < object.bumped_at+ end++ def last_poster_username+ topic_posters.find { |poster| poster.user&.id == object.last_post_user_id }&.user&.username ||+ object.last_poster&.username+ end++ def first_post_id+ object.first_post&.id+ end++ def pinned+ PinnedCheck.pinned?(object, object.user_data)+ end++ def unpinned+ PinnedCheck.unpinned?(object, object.user_data)+ end++ def posters+ topic_posters.map { |poster| serialize_topic_poster(poster) }+ end++ def participants+ return [] if !object.private_message?++ (object.participants || object.participants_summary || []).map do |participant|+ serialize_topic_poster(participant)+ end+ end++ def include_participants?+ object.private_message?+ end++ def custom_fields+ if object.custom_fields_preloaded?+ return object.preloaded_custom_fields.slice(*@custom_field_names)+ end++ @custom_field_names.each_with_object(+ ActiveSupport::HashWithIndifferentAccess.new,+ ) { |name, fields| fields[name] = object.custom_fields[name] }+ end++ def include_custom_fields?+ @custom_field_names.present?+ end++ def include_featured_link?+ SiteSetting.topic_featured_link_enabled+ end++ def include_featured_link_root_domain?+ SiteSetting.topic_featured_link_enabled && object.featured_link.present?+ end++ private++ def topic_posters+ @topic_posters ||= object.posters || object.posters_summary || []+ end++ def serialize_topic_poster(topic_poster)+ {+ extras: topic_poster.extras,+ description: topic_poster.description,+ user_id: topic_poster.user&.id,+ primary_group_id: topic_poster.primary_group&.id,+ flair_group_id: topic_poster.flair_group&.id,+ }.compact+ end+ end+enddiff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex d290bfa6952..82a4db2e10a 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -181,7 +181,7 @@ en:"condition:filter": "Continue only if conditions are met""action:topic_tags": "Add or remove tags on a topic""action:code": "Run custom JavaScript code"- "action:topic": "Create, retrieve, list, or close topics"+ "action:topic": "Create, retrieve, list, close, or set custom fields on topics""action:post": "Create, retrieve, or list posts""action:create_post": "Create a reply in an existing topic""action:set_fields": "Set or modify data fields in the workflow context"@@ -344,6 +344,7 @@ en:get: "Get topic"list: "List topics"close: "Close topic"+ set_custom_fields: "Set custom fields"topic_id: "Topic ID"title: "Title"raw: "Body"@@ -353,6 +354,12 @@ en:query: "Query"query_description: "Filter query using the same syntax as `/filter` (e.g. category:support status:open order:activity)"query_placeholder: "category:support status:open"+ custom_field_names: "Include custom fields"+ custom_field_names_placeholder: "Select custom fields"+ custom_fields: "Custom fields"+ key: "Key"+ value: "Value"+ add_custom_field: "Add custom field"limit: "Limit"limit_description: "Maximum number of topics to return (1-100, default 30)"offset: "Offset"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.rbindex 50e2cb875ae..e2d4fa19a36 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@@ -28,6 +28,21 @@ module DiscourseWorkflows).deep_symbolize_keysend+ 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+class RuntimeStateattr_reader :condition_step_details, :execution_hints, :log, :metadata, :wait_request@@ -302,6 +317,10 @@ module DiscourseWorkflowsself.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)@runtime_state.request_wait(waiting_until)enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/parameter_resolver.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/parameter_resolver.rbindex 3f153b850e5..f81c6b14199 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/parameter_resolver.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/parameter_resolver.rb@@ -4,6 +4,7 @@ module DiscourseWorkflowsclass Executorclass ParameterResolverMISSING = Object.new.freeze+ BOOLEAN_TYPE = ActiveModel::Type::Boolean.newdef initialize(parameters:, property_schema:, resolver:, input_items:, runtime_state:)@parameters = parameters@@ -78,15 +79,18 @@ module DiscourseWorkflowsreturn value if no_data_expression?(schema)return resolve_fixed_collection_value(value, schema) if fixed_collection_schema?(schema)- case value- when Hash- resolve_hash_parameter_value(value, schema)- when Array- item_schema = schema_value(schema, :item_schema)- value.map { |entry| resolve_parameter_value(entry, item_schema) }- else- @resolver.resolve(value)- end+ resolved_value =+ case value+ when Hash+ resolve_hash_parameter_value(value, schema)+ when Array+ item_schema = schema_value(schema, :item_schema)+ value.map { |entry| resolve_parameter_value(entry, item_schema) }+ else+ @resolver.resolve(value)+ end++ coerce_parameter_value(resolved_value, schema)enddef resolve_hash_parameter_value(value, schema)@@ -184,6 +188,12 @@ module DiscourseWorkflowsschema_value(schema, :no_data_expression) == trueend+ def coerce_parameter_value(value, schema)+ return value if schema_value(schema, :type) != :boolean++ BOOLEAN_TYPE.cast(value) == true+ end+def schema_value(schema, key)return nil unless schema.respond_to?(:[])diff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rbindex adca0364d24..5ea07e94720 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb@@ -208,6 +208,14 @@ module DiscourseWorkflows)end+ def serialize_topic(topic, guardian: Discourse.system_user.guardian, custom_field_names: [])+ DiscourseWorkflows::Executor::NodeExecutionContext.serialize_topic(+ topic,+ guardian: guardian,+ custom_field_names: custom_field_names,+ )+ end+def with_paired_item(item, paired_item)Item.with_paired_item(item, paired_item)enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type_serializer.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type_serializer.rbindex f0c10764f38..e53acbe885e 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type_serializer.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type_serializer.rb@@ -98,7 +98,13 @@ module DiscourseWorkflowsdef load_options_methods_for_field(definition)return [] unless definition.is_a?(Hash)- methods = Array(definition.dig(:type_options, :load_options_method)).compact+ type_options = definition[:type_options] || definition["type_options"] || {}+ methods =+ if load_options_dependencies?(type_options)+ []+ else+ Array(type_options[:load_options_method] || type_options["load_options_method"]).compact+ endnested_definitions = schema_field_definitions(definition[:item_schema])nested_definitions += schema_field_definitions(definition[:extra_item_schema])@@ -111,6 +117,13 @@ module DiscourseWorkflowsmethods + nested_definitions.flat_map { |field| load_options_methods_for_field(field) }end+ def load_options_dependencies?(type_options)+ dependencies =+ type_options[:load_options_depends_on] || type_options["load_options_depends_on"]++ Array(dependencies).present?+ end+def schema_field_definitions(schema)schema.is_a?(Hash) ? schema.values : []enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rbindex 6165d6afbaa..2a8c49e2bd3 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb@@ -355,8 +355,8 @@ module DiscourseWorkflowsexec_ctx.serialize_post(post,guardian: actor.guardian,- include_raw: truthy?(config["include_raw"]),- include_cooked: truthy?(config["include_cooked"]),+ include_raw: config["include_raw"],+ include_cooked: config["include_cooked"],),}end@@ -384,8 +384,8 @@ module DiscourseWorkflowsexec_ctx.serialize_post(post,guardian: actor.guardian,- include_raw: truthy?(config["include_raw"]),- include_cooked: truthy?(config["include_cooked"]),+ include_raw: config["include_raw"],+ include_cooked: config["include_cooked"],),}end@@ -404,13 +404,13 @@ module DiscourseWorkflowsparts,"category",config["categories"],- exact: truthy?(config["exact_category_match"]),+ exact: config["exact_category_match"],)add_category_part(parts,"exclude_category",config["exclude_categories"],- exact: truthy?(config["exact_category_match"]),+ exact: config["exact_category_match"],)add_query_part(parts, "tag", config["tags"])add_query_part(parts, "exclude_tag", config["exclude_tags"])@@ -476,10 +476,6 @@ module DiscourseWorkflowsrescue ArgumentError, TypeErrordefaultend-- def truthy?(value)- value == true || value.to_s == "true" || value.to_s == "1"- endendendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/split_out/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/split_out/v1.rbindex d6469f484be..70d45962846 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/split_out/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/split_out/v1.rb@@ -61,8 +61,6 @@ module DiscourseWorkflowsexec_ctx.get_node_parameter("fields_to_include", item_index),"destination_field_name" =>exec_ctx.get_node_parameter("destination_field_name", item_index),- "disable_dot_notation" =>- exec_ctx.get_node_parameter("disable_dot_notation", item_index),},)@@ -91,7 +89,6 @@ module DiscourseWorkflowsinclude_mode: config.fetch("include") { "no_other_fields" },dest_names: dest_names,fields_to_include: parse_field_names(config["fields_to_include"]),- disable_dot_notation: truthy?(config["disable_dot_notation"]),}end@@ -100,13 +97,7 @@ module DiscourseWorkflowsvalue.to_s.split(",").filter_map { |v| v.strip.sub(/\A\$json\./, "").presence }end- def truthy?(value)- value == true || value.to_s == "true"- end-- def fetch_field(hash, field_name, disable_dot_notation)- return hash.key?(field_name), hash[field_name] if disable_dot_notation-+ def fetch_field(hash, field_name)current = hashfield_name.split(".")@@ -119,12 +110,7 @@ module DiscourseWorkflows[true, current]end- def unset_field(hash, field_name, disable_dot_notation)- if disable_dot_notation- hash.delete(field_name)- return- end-+ def unset_field(hash, field_name)keys = field_name.split(".")current = hashkeys[0..-2].each do |key|@@ -149,7 +135,7 @@ module DiscourseWorkflowsfield_names.each_with_index do |field_name, field_index|field_tracker[field_name] = false unless field_tracker.key?(field_name)- exists, value = fetch_field(item_json, field_name, config[:disable_dot_notation])+ exists, value = fetch_field(item_json, field_name)field_tracker[field_name] = true if existsnext unless exists@@ -188,7 +174,6 @@ module DiscourseWorkflowsconfig[:include_mode],field_names,config[:fields_to_include],- config[:disable_dot_notation],)wrap(json, paired_item:)end@@ -212,18 +197,11 @@ module DiscourseWorkflowsendend- def apply_include_mode(- entry,- item_json,- include_mode,- field_names,- fields_to_include,- disable_dot_notation- )+ def apply_include_mode(entry, item_json, include_mode, field_names, fields_to_include)case include_modewhen "all_other_fields"base = item_json.deep_dup- field_names.each { |f| unset_field(base, f, disable_dot_notation) }+ field_names.each { |f| unset_field(base, f) }base.merge(entry)when "selected_other_fields"if fields_to_include.empty?@@ -233,7 +211,7 @@ module DiscourseWorkflowsendfields_to_include.each do |f|- _, val = fetch_field(item_json, f, disable_dot_notation)+ _, val = fetch_field(item_json, f)entry[f] = valendentrydiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rbindex c6bdf714310..c178b1a7049 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb@@ -4,9 +4,10 @@ module DiscourseWorkflowsmodule Nodesmodule Topicclass V1 < NodeType- OPERATIONS = %w[create get list close].freeze+ OPERATIONS = %w[create get list close set_custom_fields].freezeMAX_LIMIT = 100DEFAULT_LIMIT = 30+ CUSTOM_FIELD_OPTIONS_LIMIT = 100description(name: "action:topic",@@ -31,7 +32,7 @@ module DiscourseWorkflowsrequired: true,display_options: {show: {- operation: %w[get close],+ operation: %w[get close set_custom_fields],},},},@@ -92,6 +93,55 @@ module DiscourseWorkflows},},},+ custom_field_names: {+ type: :multi_options,+ required: false,+ options: [],+ default: [],+ type_options: {+ load_options_depends_on: %w[operation topic_id],+ load_options_method: "topic_custom_fields",+ },+ display_options: {+ show: {+ operation: %w[get list],+ },+ },+ control_options: {+ value_property: "id",+ name_property: "name",+ filterable: true,+ none: "discourse_workflows.topic.custom_field_names_placeholder",+ },+ },+ custom_fields: {+ type: :fixed_collection,+ required: false,+ type_options: {+ multiple_values: true,+ },+ options: [+ {+ name: "values",+ values: {+ key: {+ type: :string,+ required: true,+ no_data_expression: true,+ },+ value: {+ type: :string,+ required: true,+ },+ },+ },+ ],+ display_options: {+ show: {+ operation: ["set_custom_fields"],+ },+ },+ },limit: {type: :integer,required: false,@@ -123,6 +173,27 @@ module DiscourseWorkflows},)+ def self.load_options_context(context)+ case context.method_name+ when "topic_custom_fields"+ custom_field_scope = ::TopicCustomField.where.not(name: [nil, ""])+ if context.filter.present?+ custom_field_scope =+ custom_field_scope.where(+ "name ILIKE ?",+ "%#{ActiveRecord::Base.sanitize_sql_like(context.filter)}%",+ )+ end++ custom_field_scope+ .distinct+ .order(:name)+ .limit(CUSTOM_FIELD_OPTIONS_LIMIT)+ .pluck(:name)+ .map { |name| { id: name, name: name } }+ end+ end+def execute(exec_ctx)items =exec_ctx.input_items.flat_map.with_index do |_item, item_index|@@ -135,6 +206,10 @@ module DiscourseWorkflows"category_id" => exec_ctx.get_node_parameter("category_id", item_index),"tag_names" => exec_ctx.get_node_parameter("tag_names", item_index),"query" => exec_ctx.get_node_parameter("query", item_index),+ "custom_field_names" =>+ exec_ctx.get_node_parameter("custom_field_names", item_index, default: []),+ "custom_fields" =>+ exec_ctx.get_node_parameter("custom_fields.values", item_index, default: []),"limit" => exec_ctx.get_node_parameter("limit", item_index, default: DEFAULT_LIMIT),"offset" => exec_ctx.get_node_parameter("offset", item_index, default: 0),}@@ -157,6 +232,8 @@ module DiscourseWorkflowslist_topics(exec_ctx, config, item_index).map { |data| wrap(data) }when "close"wrap(close_topic(exec_ctx, config, item_index))+ when "set_custom_fields"+ wrap(set_custom_fields(exec_ctx, config, item_index))elseraise_node_error!(I18n.t(@@ -195,14 +272,28 @@ module DiscourseWorkflowstopic = post.topic- { topic: topic_data(topic, guardian), post_id: post.id, post_number: post.post_number }+ {+ topic: exec_ctx.serialize_topic(topic, guardian: guardian),+ post_id: post.id,+ post_number: post.post_number,+ }enddef get_topic(exec_ctx, config, item_index)topic = ::Topic.find(config["topic_id"])actor = exec_ctx.actor_from_parameter("actor_username", item_index)actor.guardian.ensure_can_see!(topic)- { topic: topic_data(topic, actor.guardian) }+ custom_field_names = config["custom_field_names"]+ ::Topic.preload_custom_fields([topic], custom_field_names) if custom_field_names.present?++ {+ topic:+ exec_ctx.serialize_topic(+ topic,+ guardian: actor.guardian,+ custom_field_names: custom_field_names,+ ),+ }enddef list_topics(exec_ctx, config, item_index)@@ -219,7 +310,19 @@ module DiscourseWorkflowsActiveRecord::Associations::Preloader.new(records: posts, associations: :user).callend- topics.map { |topic| { topic: topic_data(topic, actor.guardian) } }+ 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:+ exec_ctx.serialize_topic(+ topic,+ guardian: actor.guardian,+ custom_field_names: custom_field_names,+ ),+ }+ endenddef close_topic(exec_ctx, config, item_index)@@ -229,11 +332,38 @@ module DiscourseWorkflowstopic.update_status("closed", true, actor)- { topic: topic_data(topic.reload, actor.guardian) }+ { topic: exec_ctx.serialize_topic(topic.reload, guardian: actor.guardian) }end- def topic_data(topic, guardian)- serialize_record(topic, TopicListItemSerializer, scope: guardian)+ def set_custom_fields(exec_ctx, config, item_index)+ topic = ::Topic.find(config["topic_id"])+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)+ guardian = actor.guardian+ guardian.ensure_can_edit_topic!(topic)++ custom_fields = custom_fields_from_entries(config["custom_fields"])+ if custom_fields.present?+ custom_fields.each { |key, value| topic.custom_fields[key] = value }+ topic.save_custom_fields+ end++ {+ topic:+ exec_ctx.serialize_topic(+ topic,+ guardian: guardian,+ custom_field_names: custom_fields.keys,+ ),+ }+ end++ def custom_fields_from_entries(entries)+ Array(entries).each_with_object({}) do |entry, fields|+ key = entry["key"].to_s.strip+ next if key.blank?++ fields[key] = entry["value"]+ endendendenddiff --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.rbindex 1d88830212f..c726555c21a 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@@ -744,6 +744,45 @@ RSpec.describe DiscourseWorkflows::Executor::NodeExecutionContext doendend+ 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" doit "returns a parsed response object" dostub_request(:get, "https://api.example.com/data").to_return(diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/parameter_resolver_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/parameter_resolver_spec.rbindex b8a266b6012..8efd8bec902 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/parameter_resolver_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/parameter_resolver_spec.rb@@ -127,4 +127,41 @@ RSpec.describe DiscourseWorkflows::Executor::ParameterResolver doexpression_resolver&.disposesandbox&.disposeend++ it "coerces boolean values using the property schema" do+ resolver, expression_resolver, sandbox =+ build_resolver(+ {+ "enabled" => "={{ $json.enabled }}",+ "disabled" => "false",+ "nested" => {+ "enabled" => "1",+ },+ },+ schema: {+ enabled: {+ type: :boolean,+ },+ disabled: {+ type: :boolean,+ },+ nested: {+ type: :object,+ fields: {+ enabled: {+ type: :boolean,+ },+ },+ },+ },+ items: [{ "json" => { "enabled" => "true" } }],+ )++ expect(resolver.resolve("enabled", 0)).to eq(true)+ expect(resolver.resolve("disabled", 0)).to eq(false)+ expect(resolver.resolve("nested.enabled", 0)).to eq(true)+ ensure+ expression_resolver&.dispose+ sandbox&.dispose+ endenddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/split_out_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/split_out_spec.rbindex e04ae07920c..d2fb8f1ec58 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/split_out_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/split_out_spec.rb@@ -164,13 +164,6 @@ RSpec.describe DiscourseWorkflows::Nodes::SplitOut::V1 doexpect(result.first["json"]).to eq("items" => 1)end-- it "can disable dot notation for literal field names" do- input = [{ "json" => { "data.items" => [1] } }]- result = execute(input, "field" => "data.items", "disable_dot_notation" => true)-- expect(result.first["json"]).to eq("data.items" => 1)- endenddescribe "multiple fields" dodiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rbindex 8fa354b078b..3dd87e8484a 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_spec.rb@@ -10,9 +10,62 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 dobefore { SiteSetting.tagging_enabled = true }+ describe ".load_options_context" do+ fab!(:topic_with_custom_fields) { Fabricate(:topic, category: category) }+ fab!(:other_topic_with_custom_fields) { Fabricate(:topic, category: category) }++ def load_options(filter: nil)+ context =+ DiscourseWorkflows::LoadOptionsContext.new(+ method_name: "topic_custom_fields",+ filter: filter,+ node_class: described_class,+ )++ described_class.load_options_context(context)+ end++ before do+ topic_with_custom_fields.custom_fields["workflow_key"] = "first"+ topic_with_custom_fields.save_custom_fields+ other_topic_with_custom_fields.custom_fields["workflow_key"] = "second"+ other_topic_with_custom_fields.custom_fields["other_key"] = "other"+ other_topic_with_custom_fields.save_custom_fields+ end++ it "returns distinct topic custom field names for the chooser" do+ expect(load_options).to contain_exactly(+ { id: "other_key", name: "other_key" },+ { id: "workflow_key", name: "workflow_key" },+ )+ end++ it "filters topic custom field names by the filter term" do+ expect(load_options(filter: "workflow")).to contain_exactly(+ { id: "workflow_key", name: "workflow_key" },+ )+ end++ it "limits topic custom field names returned to the chooser" do+ 101.times do |index|+ TopicCustomField.create!(+ topic: topic_with_custom_fields,+ name: "workflow_key_#{index}",+ value: "value",+ )+ end++ expect(load_options.length).to eq(described_class::CUSTOM_FIELD_OPTIONS_LIMIT)+ end+ end+describe "#execute" dolet(:item) { { "json" => {} } }+ def custom_field_entries(*rows)+ { "values" => rows }+ end+context "with operation 'create'" doit "creates a topic for the configured actor" doresult = nil@@ -152,6 +205,48 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 do)end+ it "returns selected custom fields when requested" do+ topic.custom_fields["workflow_key"] = "workflow value"+ topic.custom_fields["other_key"] = "other value"+ topic.save_custom_fields++ result =+ execute_node(+ configuration: {+ "operation" => "get",+ "topic_id" => topic.id.to_s,+ "custom_field_names" => ["workflow_key"],+ },+ item: {+ "json" => {+ "topic_id" => topic.id.to_s,+ },+ },+ )++ expect(result["topic"]["custom_fields"]).to eq("workflow_key" => "workflow value")+ end++ it "omits custom fields by default" do+ topic.custom_fields["workflow_key"] = "workflow value"+ topic.save_custom_fields++ result =+ execute_node(+ configuration: {+ "operation" => "get",+ "topic_id" => topic.id.to_s,+ },+ item: {+ "json" => {+ "topic_id" => topic.id.to_s,+ },+ },+ )++ expect(result["topic"]).not_to have_key("custom_fields")+ end+it "raises when topic is not found" doexpect doexecute_node(@@ -305,6 +400,42 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 doexpect(topic_data["bumped_at"]).to be_presentend+ it "preloads selected custom fields when requested" do+ topic_1.custom_fields["workflow_key"] = "first"+ topic_1.custom_fields["other_key"] = "ignored"+ topic_1.save_custom_fields+ topic_2.custom_fields["workflow_key"] = "second"+ topic_2.custom_fields["other_key"] = "ignored"+ topic_2.save_custom_fields++ custom_field_queries =+ track_sql_queries do+ result =+ execute_list(+ configuration: {+ "operation" => "list",+ "query" => "category:#{category.slug}",+ "limit" => "10",+ "custom_field_names" => ["workflow_key"],+ },+ )++ custom_fields =+ result.map { |output_item| output_item.dig("json", "topic", "custom_fields") }+ expect(custom_fields).to include(+ include("workflow_key" => "first"),+ include("workflow_key" => "second"),+ )+ custom_fields.each { |fields| expect(fields).not_to include("other_key") }+ end.select do |query|+ query.include?("topic_custom_fields") && query.include?("workflow_key")+ end++ expect(custom_field_queries.count).to eq(1)+ expect(custom_field_queries.first).to include("topic_id in")+ expect(custom_field_queries.first).to include("name in")+ end+it "returns empty array when no topics match" doresult =execute_list(@@ -405,5 +536,65 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 doexpect(topic.reload).not_to be_closedendend++ context "with operation 'set_custom_fields'" do+ fab!(:topic) { Fabricate(:topic, category: category, user: user) }+ fab!(:post) { Fabricate(:post, topic: topic, user: user) }++ it "sets topic custom fields as the configured actor" do+ result =+ execute_node(+ configuration: {+ "operation" => "set_custom_fields",+ "topic_id" => topic.id.to_s,+ "custom_fields" =>+ custom_field_entries(+ { "key" => "foo", "value" => "bar" },+ { "key" => "answer", "value" => "42" },+ ),+ "actor_username" => admin.username,+ },+ item: item,+ )++ expect(topic.reload.custom_fields["foo"]).to eq("bar")+ expect(topic.custom_fields["answer"]).to eq("42")+ expect(result["topic"]["custom_fields"]).to eq("foo" => "bar", "answer" => "42")+ end++ it "resolves expressions in custom field values" do+ execute_node(+ configuration: {+ "operation" => "set_custom_fields",+ "topic_id" => topic.id.to_s,+ "custom_fields" =>+ custom_field_entries({ "key" => "foo", "value" => "={{ $json.value }}" }),+ },+ item: {+ "json" => {+ "value" => "dynamic value",+ },+ },+ )++ expect(topic.reload.custom_fields["foo"]).to eq("dynamic value")+ end++ it "raises when actor_username cannot edit the topic" do+ expect do+ execute_node(+ configuration: {+ "operation" => "set_custom_fields",+ "topic_id" => topic.id.to_s,+ "custom_fields" => custom_field_entries({ "key" => "foo", "value" => "bar" }),+ "actor_username" => other_user.username,+ },+ item: item,+ )+ end.to raise_error(Discourse::InvalidAccess)++ expect(topic.reload.custom_fields["foo"]).to be_nil+ end+ endendenddiff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rbindex 66ce8b3bdc4..8c69c2c846a 100644--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb@@ -56,6 +56,21 @@ RSpec.describe DiscourseWorkflows::NodeTypesController doexpect(badge_node.dig("metadata", "badges")).to all(include("id", "name"))end+ it "does not preload load options metadata that depends on node parameters" do+ topic = Fabricate(:topic)+ TopicCustomField.create!(topic: topic, name: "workflow_key", value: "value")++ get "/admin/plugins/discourse-workflows/node-types.json"++ topic_node =+ response.parsed_body["node_types"].find { |nt| nt["identifier"] == "action:topic" }++ expect(topic_node.dig("properties", "custom_field_names", "type_options")).to include(+ "load_options_depends_on",+ )+ expect(topic_node.fetch("metadata", {})).not_to have_key("topic_custom_fields")+ end+it "returns descriptor fields used by the admin client" doget "/admin/plugins/discourse-workflows/node-types.json"diff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjsindex b3b8eb0bfeb..39069d6ad39 100644--- a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/property-engine-test.gjs@@ -896,4 +896,96 @@ module("Integration | Component | workflows property engine", function (hooks) {assert.strictEqual(String(this.formApi.get("agent_id")), "1");assert.strictEqual(this.formApi.get("agent_name"), "Support Bot");});++ test("renders remote multi-select options from load options", async function (assert) {+ const requests = [];+ pretender.post(+ "/admin/plugins/discourse-workflows/dynamic-node-parameters/options.json",+ (request) => {+ const body = JSON.parse(request.requestBody);+ requests.push(body);++ return response([{ id: "foo", name: "foo" }]);+ }+ );++ this.setProperties({+ configuration: {+ operation: "get",+ topic_id: "21",+ custom_field_names: [],+ },+ formApi: null,+ node: {+ clientId: "node-1",+ type: "action:topic",+ typeVersion: "1.0",+ },+ nodeType: "action:topic",+ nodeTypes: [+ {+ identifier: "action:topic",+ name: "action:topic",+ version: "1.0",+ metadata: {+ topic_custom_fields: [{ id: "system", name: "system" }],+ },+ },+ ],+ schema: {+ custom_field_names: {+ type: "multi_options",+ options: [],+ type_options: {+ load_options_depends_on: ["operation", "topic_id"],+ load_options_method: "topic_custom_fields",+ },+ control_options: {+ filterable: true,+ name_property: "name",+ value_property: "id",+ },+ },+ },+ registerApi: (api) => {+ this.set("formApi", api);+ },+ });++ await render(+ <template>+ <Form+ @data={{this.configuration}}+ @onRegisterApi={{this.registerApi}}+ as |form transientData|+ >+ <PropertyEngineConfigurator+ @form={{form}}+ @formApi={{this.formApi}}+ @configuration={{transientData}}+ @node={{this.node}}+ @nodeType={{this.nodeType}}+ @nodeTypes={{this.nodeTypes}}+ @schema={{this.schema}}+ @session={{this.session}}+ />+ </Form>+ </template>+ );++ const selector = selectKit(".multi-select");+ await selector.expand();+ await waitFor(".multi-select .select-kit-row[data-value='foo']");+ await selector.selectRowByValue("foo");++ assert.deepEqual(this.formApi.get("custom_field_names"), ["foo"]);+ assert.strictEqual(selector.header().label(), "foo");+ assert.true(requests.length >= 1);+ assert.strictEqual(requests[0].methodName, "topic_custom_fields");+ assert.deepEqual(requests[0].currentNodeParameters, {+ operation: "get",+ topic_id: "21",+ custom_field_names: [],+ });+ });});