Skip to content

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

FEATURE: Add PM and subcategory filters to workflow triggers (#40520)

Joffrey JAFFEUX29d9e8b96ce3
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/category-control.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/category-control.gjs
index 33b1f1673eb..3e6f35d5a6f 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/category-control.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/category-control.gjs
@@ -1,9 +1,14 @@
import Component from "@glimmer/component";
+import { hash } from "@ember/helper";
import { action } from "@ember/object";
import CategoryChooser from "discourse/select-kit/components/category-chooser";
import ExpressionWrapper from "./expression-wrapper";
export default class CategoryControl extends Component {
+ get clearable() {
+ return !this.args.schema?.required;
+ }
+
@action
handleChange(categoryId) {
this.args.field.set(categoryId == null ? "" : String(categoryId));
@@ -20,6 +25,7 @@ export default class CategoryControl extends Component {
<CategoryChooser
@value={{if @field.value @field.value null}}
@onChange={{this.handleChange}}
+ @options={{hash clearable=this.clearable}}
/>
</ExpressionWrapper>
</template>
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/group-select.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/group-select.gjs
index c10ca37471b..af691748a33 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/group-select.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/group-select.gjs
@@ -8,6 +8,10 @@ import ExpressionWrapper from "./expression-wrapper";
export default class GroupSelect extends ComboBoxField {
@service router;
+ get clearable() {
+ return !this.args.schema?.required;
+ }
+
@action
manageGroups() {
this.router.transitionTo("groups");
@@ -33,6 +37,7 @@ export default class GroupSelect extends ComboBoxField {
filterable=this.filterable
none=this.none
castInteger=this.castInteger
+ clearable=this.clearable
}}
/>
{{#unless @field.value}}
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js
index 2e23fe542c3..e336ca6d0d7 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/field-control-registry.js
@@ -59,6 +59,7 @@ const FIELD_CONTROL_REGISTRY = {
},
select: { kind: "field", type: "select", renderer: SelectControl },
icon: { kind: "field", type: "icon", renderer: IconControl },
+ checkbox: { kind: "field", type: "checkbox", renderer: DefaultInputControl },
textarea: { kind: "field", type: "textarea", renderer: DefaultInputControl },
default: {
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 54f1b55b35c..b5e6a6b025b 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -484,8 +484,17 @@ en:
tag_names: "Tags"
tag_names_description: "Only trigger for topics that have at least one of these tags"
post_created:
+ topic_type: "Topic type"
+ topic_type_description: "Choose whether topics, personal messages, or both should trigger the workflow"
+ topic_type_all: "All"
+ topic_type_topics: "Topics"
+ topic_type_personal_messages: "Personal messages"
+ group_inbox_id: "Group inbox"
+ group_inbox_id_description: "Only trigger for personal messages in this group inbox"
+ group_inbox_id_placeholder: "All group inboxes"
category_id: "Category"
category_id_description: "Only trigger for posts in this category"
+ include_subcategories: "Include subcategories"
tag_names: "Tags"
tag_names_description: "Only trigger for posts in topics that have at least one of these tags"
post_edited:
@@ -501,8 +510,17 @@ en:
trust_levels: "Trust levels"
trust_levels_description: "Only trigger for posts created by users with these trust levels"
topic_created:
+ topic_type: "Topic type"
+ topic_type_description: "Choose whether topics, personal messages, or both should trigger the workflow"
+ topic_type_all: "All"
+ topic_type_topics: "Topics"
+ topic_type_personal_messages: "Personal messages"
+ group_inbox_id: "Group inbox"
+ group_inbox_id_description: "Only trigger for personal messages in this group inbox"
+ group_inbox_id_placeholder: "All group inboxes"
category_id: "Category"
category_id_description: "Only trigger for topics in this category"
+ include_subcategories: "Include subcategories"
tag_names: "Tags"
tag_names_description: "Only trigger for topics that have at least one of these tags"
topic_tag_changed:
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 dc4ca9015c5..23ff5f561c7 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
@@ -4,6 +4,8 @@ module DiscourseWorkflows
module Nodes
module PostCreated
class V1 < NodeType
+ TOPIC_TYPE_OPTIONS = %w[all topics personal_messages].freeze
+
description(
name: "trigger:post_created",
version: "1.0",
@@ -14,6 +16,33 @@ module DiscourseWorkflows
group: "discourse_triggers",
events: [:post_created],
properties: {
+ topic_type: {
+ type: :options,
+ required: true,
+ default: "topics",
+ options: TOPIC_TYPE_OPTIONS,
+ },
+ group_inbox_id: {
+ type: :integer,
+ required: false,
+ type_options: {
+ load_options_method: "groups",
+ },
+ display_options: {
+ show: {
+ topic_type: %w[personal_messages],
+ },
+ },
+ ui: {
+ control: :group_select,
+ },
+ control_options: {
+ value_property: "id",
+ name_property: "name",
+ filterable: true,
+ none: "discourse_workflows.post_created.group_inbox_id_placeholder",
+ },
+ },
category_id: {
type: :integer,
required: false,
@@ -21,6 +50,19 @@ module DiscourseWorkflows
control: :category,
},
},
+ include_subcategories: {
+ type: :boolean,
+ required: false,
+ default: true,
+ ui: {
+ control: :checkbox,
+ },
+ display_options: {
+ show: {
+ category_id: [{ _cnd: { exists: true } }],
+ },
+ },
+ },
tag_names: {
type: :string,
required: false,
@@ -31,6 +73,17 @@ module DiscourseWorkflows
},
)
+ def self.load_options_context(context)
+ case context.method_name
+ when "groups"
+ ::Group
+ .order(:name)
+ .pluck(:id, :name)
+ .select { |_, name| context.matches_filter?(name) }
+ .map { |id, name| { id:, name: } }
+ end
+ end
+
def initialize(post, opts = nil, *)
super(parameters: {})
@post = post
@@ -49,7 +102,13 @@ module DiscourseWorkflows
def matches?(trigger_ctx)
topic = @post.topic
- matches_category?(topic, trigger_ctx.get_node_parameter("category_id")) &&
+ matches_topic_type?(topic, trigger_ctx.get_node_parameter("topic_type", "topics")) &&
+ matches_group_inbox?(topic, trigger_ctx.get_node_parameter("group_inbox_id")) &&
+ matches_category?(
+ topic,
+ trigger_ctx.get_node_parameter("category_id"),
+ trigger_ctx.get_node_parameter("include_subcategories", true),
+ ) &&
matches_tags?(topic, normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))
end
@@ -63,8 +122,33 @@ module DiscourseWorkflows
serialize_record(topic, TopicListItemSerializer)
end
- def matches_category?(topic, category_id)
- category_id.blank? || topic.category_id == category_id.to_i
+ def matches_topic_type?(topic, topic_type)
+ case topic_type.presence || "topics"
+ when "all"
+ true
+ when "topics"
+ !topic.private_message?
+ when "personal_messages"
+ topic.private_message?
+ else
+ false
+ end
+ end
+
+ def matches_group_inbox?(topic, group_id)
+ return true if group_id.blank?
+ return false if !topic.private_message?
+
+ topic.allowed_groups.exists?(id: group_id.to_i)
+ end
+
+ def matches_category?(topic, category_id, include_subcategories)
+ return true if category_id.blank?
+
+ category_id = category_id.to_i
+ return topic.category_id == category_id if include_subcategories == false
+
+ ::Category.subcategory_ids(category_id).include?(topic.category_id)
end
def matches_tags?(topic, tag_names)
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 8365acfc030..c9dc9705b11 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
@@ -4,6 +4,8 @@ module DiscourseWorkflows
module Nodes
module TopicCreated
class V1 < NodeType
+ TOPIC_TYPE_OPTIONS = %w[all topics personal_messages].freeze
+
description(
name: "trigger:topic_created",
version: "1.0",
@@ -14,6 +16,33 @@ module DiscourseWorkflows
group: "discourse_triggers",
events: [:topic_created],
properties: {
+ topic_type: {
+ type: :options,
+ required: true,
+ default: "topics",
+ options: TOPIC_TYPE_OPTIONS,
+ },
+ group_inbox_id: {
+ type: :integer,
+ required: false,
+ type_options: {
+ load_options_method: "groups",
+ },
+ display_options: {
+ show: {
+ topic_type: %w[personal_messages],
+ },
+ },
+ ui: {
+ control: :group_select,
+ },
+ control_options: {
+ value_property: "id",
+ name_property: "name",
+ filterable: true,
+ none: "discourse_workflows.topic_created.group_inbox_id_placeholder",
+ },
+ },
category_id: {
type: :integer,
required: false,
@@ -21,6 +50,19 @@ module DiscourseWorkflows
control: :category,
},
},
+ include_subcategories: {
+ type: :boolean,
+ required: false,
+ default: true,
+ ui: {
+ control: :checkbox,
+ },
+ display_options: {
+ show: {
+ category_id: [{ _cnd: { exists: true } }],
+ },
+ },
+ },
tag_names: {
type: :string,
required: false,
@@ -31,6 +73,17 @@ module DiscourseWorkflows
},
)
+ def self.load_options_context(context)
+ case context.method_name
+ when "groups"
+ ::Group
+ .order(:name)
+ .pluck(:id, :name)
+ .select { |_, name| context.matches_filter?(name) }
+ .map { |id, name| { id:, name: } }
+ end
+ end
+
def initialize(topic, opts = nil, *)
super(parameters: {})
@topic = topic
@@ -46,8 +99,12 @@ module DiscourseWorkflows
end
def matches?(trigger_ctx)
- matches_category?(trigger_ctx.get_node_parameter("category_id")) &&
- matches_tags?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))
+ matches_topic_type?(trigger_ctx.get_node_parameter("topic_type", "topics")) &&
+ matches_group_inbox?(trigger_ctx.get_node_parameter("group_inbox_id")) &&
+ matches_category?(
+ trigger_ctx.get_node_parameter("category_id"),
+ trigger_ctx.get_node_parameter("include_subcategories", true),
+ ) && matches_tags?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))
end
private
@@ -60,8 +117,33 @@ module DiscourseWorkflows
serialize_post(post)
end
- def matches_category?(category_id)
- category_id.blank? || @topic.category_id == category_id.to_i
+ def matches_topic_type?(topic_type)
+ case topic_type.presence || "topics"
+ when "all"
+ true
+ when "topics"
+ when "personal_messages"
+ @topic.private_message?
+ else
+ false
+ end
+ end
+
+ def matches_group_inbox?(group_id)
+ return true if group_id.blank?
+ return false if [email protected]_message?
+
+ @topic.allowed_groups.exists?(id: group_id.to_i)
+ end
+
+ def matches_category?(category_id, include_subcategories)
+ return true if category_id.blank?
+
+ category_id = category_id.to_i
+ return @topic.category_id == category_id if include_subcategories == false
+
+ ::Category.subcategory_ids(category_id).include?(@topic.category_id)
end
def matches_tags?(tag_names)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
index 4052e9c373b..b5f8cdfa193 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
@@ -87,6 +87,7 @@ module DiscourseWorkflows
KNOWN_UI_CONTROLS = %i[
boolean
category
+ checkbox
code
combo_box
condition_builder
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_created_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_created_spec.rb
index adb06ea53f2..0bbe4c4de78 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_created_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_created_spec.rb
@@ -5,7 +5,16 @@ RSpec.describe DiscourseWorkflows::Nodes::PostCreated::V1 do
fab!(:reply_user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:first_post) { create_post(user: topic_owner, raw: "First post") }
fab!(:topic) { first_post.topic }
+ fab!(:subcategory) { Fabricate(:category, parent_category: topic.category) }
+ fab!(:subcategory_topic) { Fabricate(:topic, category: subcategory, user: topic_owner) }
+ fab!(:subcategory_post) { Fabricate(:post, topic: subcategory_topic, user: reply_user) }
fab!(:tag) { Fabricate(:tag, name: "test-tag") }
+ fab!(:group_inbox, :group)
+ fab!(:other_group, :group)
+ fab!(:group_pm_topic) do
+ Fabricate(:group_private_message_topic, user: topic_owner, recipient_group: group_inbox)
+ end
+ fab!(:direct_pm_topic) { Fabricate(:private_message_topic, user: topic_owner) }
fab!(:reply) do
PostCreator.create!(
reply_user,
@@ -65,10 +74,52 @@ RSpec.describe DiscourseWorkflows::Nodes::PostCreated::V1 do
end
describe "#matches?" do
- it "returns true when category and tags are blank" do
- trigger = described_class.new(reply)
+ it "matches posts in subcategories by default" do
+ expect(
+ described_class.new(subcategory_post).matches?(
+ trigger_context("category_id" => topic.category_id.to_s),
+ ),
+ ).to eq(true)
+ end
+
+ it "does not match posts in subcategories when subcategories are excluded" do
+ expect(
+ described_class.new(subcategory_post).matches?(
+ trigger_context(
+ "category_id" => topic.category_id.to_s,
+ "include_subcategories" => false,
+ ),
+ ),
+ ).to eq(false)
+ end
+
+ it "matches posts in the selected category when subcategories are excluded" do
+ expect(
+ described_class.new(reply).matches?(
+ trigger_context(
+ "category_id" => topic.category_id.to_s,
+ "include_subcategories" => false,
+ ),
+ ),
+ ).to eq(true)
+ end
+
+ it "matches only topics when topic type is blank" do
+ group_pm_post = Fabricate(:post, topic: group_pm_topic, user: reply_user)
+
+ expect(described_class.new(reply).matches?(trigger_context({}))).to eq(true)
+ expect(described_class.new(group_pm_post).matches?(trigger_context({}))).to eq(false)
+ end
- expect(trigger.matches?(trigger_context({}))).to eq(true)
+ it "matches topics and personal messages when topic type is all" do
+ group_pm_post = Fabricate(:post, topic: group_pm_topic, user: reply_user)
+
+ expect(described_class.new(reply).matches?(trigger_context("topic_type" => "all"))).to eq(
+ true,
+ )
+ expect(
+ described_class.new(group_pm_post).matches?(trigger_context("topic_type" => "all")),
+ ).to eq(true)
end
it "returns true when the post topic matches the configured category and tags" do
@@ -90,6 +141,65 @@ RSpec.describe DiscourseWorkflows::Nodes::PostCreated::V1 do
)
expect(trigger.matches?(trigger_context("tag_names" => ["missing"]))).to eq(false)
end
+
+ it "matches only topics when topic type is topics" do
+ group_pm_post = Fabricate(:post, topic: group_pm_topic, user: reply_user)
+
+ expect(described_class.new(reply).matches?(trigger_context("topic_type" => "topics"))).to eq(
+ true,
+ )
+ expect(
+ described_class.new(group_pm_post).matches?(trigger_context("topic_type" => "topics")),
+ ).to eq(false)
+ end
+
+ it "matches only personal messages when topic type is personal messages" do
+ group_pm_post = Fabricate(:post, topic: group_pm_topic, user: reply_user)
+
+ expect(
+ described_class.new(group_pm_post).matches?(
+ trigger_context("topic_type" => "personal_messages"),
+ ),
+ ).to eq(true)
+ expect(
+ described_class.new(reply).matches?(trigger_context("topic_type" => "personal_messages")),
+ ).to eq(false)
+ end
+
+ it "matches personal messages in the configured group inbox" do
+ group_pm_post = Fabricate(:post, topic: group_pm_topic, user: reply_user)
+
+ expect(
+ described_class.new(group_pm_post).matches?(
+ trigger_context(
+ "topic_type" => "personal_messages",
+ "group_inbox_id" => group_inbox.id.to_s,
+ ),
+ ),
+ ).to eq(true)
+ end
+
+ it "does not match personal messages outside the configured group inbox" do
+ group_pm_post = Fabricate(:post, topic: group_pm_topic, user: reply_user)
+ direct_pm_post = Fabricate(:post, topic: direct_pm_topic, user: reply_user)
+
+ expect(
+ described_class.new(group_pm_post).matches?(
+ trigger_context(
+ "topic_type" => "personal_messages",
+ "group_inbox_id" => other_group.id.to_s,
+ ),
+ ),
+ ).to eq(false)
+ expect(
+ described_class.new(direct_pm_post).matches?(
+ trigger_context(
+ "topic_type" => "personal_messages",
+ "group_inbox_id" => group_inbox.id.to_s,
+ ),
+ ),
+ ).to eq(false)
+ end
end
def trigger_context(parameters)
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_created_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_created_spec.rb
index 69bf2c1e19f..9d526890a32 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_created_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_created_spec.rb
@@ -4,7 +4,15 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicCreated::V1 do
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
fab!(:first_post) { create_post(user: user, raw: "First post") }
fab!(:topic) { first_post.topic }
+ fab!(:subcategory) { Fabricate(:category, parent_category: topic.category) }
+ fab!(:subcategory_topic) { Fabricate(:topic, category: subcategory, user: user) }
fab!(:tag) { Fabricate(:tag, name: "test-tag") }
+ fab!(:group_inbox, :group)
+ fab!(:other_group, :group)
+ fab!(:group_pm_topic) do
+ Fabricate(:group_private_message_topic, user: user, recipient_group: group_inbox)
+ end
+ fab!(:direct_pm_topic) { Fabricate(:private_message_topic, user: user) }
before do
SiteSetting.tagging_enabled = true
@@ -55,4 +63,104 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicCreated::V1 do
expect(output[:topic][:assigned_to_user][:username]).to eq(assignee.username)
end
end
+
+ describe "#matches?" do
+ it "matches topics in subcategories by default" do
+ expect(
+ described_class.new(subcategory_topic).matches?(
+ trigger_context("category_id" => topic.category_id.to_s),
+ ),
+ ).to eq(true)
+ end
+
+ it "does not match topics in subcategories when subcategories are excluded" do
+ expect(
+ described_class.new(subcategory_topic).matches?(
+ trigger_context(
+ "category_id" => topic.category_id.to_s,
+ "include_subcategories" => false,
+ ),
+ ),
+ ).to eq(false)
+ end
+
+ it "matches topics in the selected category when subcategories are excluded" do
+ expect(
+ described_class.new(topic).matches?(
+ trigger_context(
+ "category_id" => topic.category_id.to_s,
+ "include_subcategories" => false,
+ ),
+ ),
+ ).to eq(true)
+ end
+
+ it "matches only topics when topic type is blank" do
+ expect(described_class.new(topic).matches?(trigger_context({}))).to eq(true)
+ expect(described_class.new(group_pm_topic).matches?(trigger_context({}))).to eq(false)
+ end
+
+ it "matches topics and personal messages when topic type is all" do
+ expect(described_class.new(topic).matches?(trigger_context("topic_type" => "all"))).to eq(
+ true,
+ )
+ expect(
+ described_class.new(group_pm_topic).matches?(trigger_context("topic_type" => "all")),
+ ).to eq(true)
+ end
+
+ it "matches only topics when topic type is topics" do
+ expect(described_class.new(topic).matches?(trigger_context("topic_type" => "topics"))).to eq(
+ true,
+ )
+ expect(
+ described_class.new(group_pm_topic).matches?(trigger_context("topic_type" => "topics")),
+ ).to eq(false)
+ end
+
+ it "matches only personal messages when topic type is personal messages" do
+ expect(
+ described_class.new(group_pm_topic).matches?(
+ trigger_context("topic_type" => "personal_messages"),
+ ),
+ ).to eq(true)
+ expect(
+ described_class.new(topic).matches?(trigger_context("topic_type" => "personal_messages")),
+ ).to eq(false)
+ end
+
+ it "matches personal messages in the configured group inbox" do
+ expect(
+ described_class.new(group_pm_topic).matches?(
+ trigger_context(
+ "topic_type" => "personal_messages",
+ "group_inbox_id" => group_inbox.id.to_s,
+ ),
+ ),
+ ).to eq(true)
+ end
+
+ it "does not match personal messages outside the configured group inbox" do
+ expect(
+ described_class.new(group_pm_topic).matches?(
+ trigger_context(
+ "topic_type" => "personal_messages",
+ "group_inbox_id" => other_group.id.to_s,
+ ),
+ ),
+ ).to eq(false)
+ expect(
+ described_class.new(direct_pm_topic).matches?(
+ trigger_context(
+ "topic_type" => "personal_messages",
+ "group_inbox_id" => group_inbox.id.to_s,
+ ),
+ ),
+ ).to eq(false)
+ end
+ end
+
+ def trigger_context(parameters)
+ DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters })
+ end
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/property_schema_validator_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/property_schema_validator_spec.rb
index 35e1ecfb60a..c21b93cbcc7 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/property_schema_validator_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/property_schema_validator_spec.rb
@@ -81,6 +81,12 @@ RSpec.describe DiscourseWorkflows::PropertySchemaValidator do
expect(validate(schema)).to eq([])
end
+ it "accepts checkbox controls" do
+ schema = { enabled: { type: :boolean, ui: { control: :checkbox } } }
+
+ expect(validate(schema)).to eq([])
+ end
+
it "flags unknown ui.control values" do
errors = validate(title: { type: :string, ui: { control: :neon } })
expect(errors.first).to include("title.ui.control")
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.gjs
index 60b50983625..f7c5e0cc907 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
@@ -70,6 +70,183 @@ module("Integration | Component | workflows property engine", function (hooks) {
assert.dom("input").isFocused();
});
+ test("renders checkbox controls from metadata", async function (assert) {
+ this.setProperties({
+ configuration: {
+ category_id: 1,
+ include_subcategories: true,
+ },
+ formApi: null,
+ nodeType: "trigger:topic_created",
+ schema: {
+ category_id: {
+ type: "integer",
+ },
+ include_subcategories: {
+ type: "boolean",
+ ui: {
+ control: "checkbox",
+ },
+ display_options: {
+ show: {
+ category_id: [{ _cnd: { exists: true } }],
+ },
+ },
+ },
+ },
+ 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}}
+ @nodeType={{this.nodeType}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ assert.dom("input[type='checkbox']").isChecked();
+
+ await click("input[type='checkbox']");
+
+ assert.false(this.formApi.get("include_subcategories"));
+ });
+
+ test("can clear optional category controls", async function (assert) {
+ this.setProperties({
+ configuration: {
+ category_id: 2,
+ },
+ formApi: null,
+ nodeType: "trigger:topic_created",
+ schema: {
+ category_id: {
+ type: "integer",
+ required: false,
+ ui: {
+ control: "category",
+ },
+ },
+ },
+ 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}}
+ @nodeType={{this.nodeType}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ const categoryChooser = selectKit(".category-chooser");
+ const header = categoryChooser.header();
+
+ assert.strictEqual(header.value(), "2");
+ assert.dom(".btn-clear", header.el()).exists();
+
+ await click(header.el().querySelector(".btn-clear"));
+
+ assert.strictEqual(this.formApi.get("category_id"), "");
+ });
+
+ test("can clear optional group controls", async function (assert) {
+ this.setProperties({
+ configuration: {
+ group_inbox_id: 2,
+ },
+ formApi: null,
+ nodeType: "trigger:topic_created",
+ nodeTypes: [
+ {
+ identifier: "trigger:topic_created",
+ metadata: {
+ groups: [
+ { id: 1, name: "support" },
+ { id: 2, name: "moderators" },
+ ],
+ },
+ },
+ ],
+ schema: {
+ group_inbox_id: {
+ type: "integer",
+ required: false,
+ type_options: {
+ load_options_method: "groups",
+ },
+ ui: {
+ control: "group_select",
+ },
+ 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}}
+ @nodeType={{this.nodeType}}
+ @nodeTypes={{this.nodeTypes}}
+ @schema={{this.schema}}
+ @session={{this.session}}
+ />
+ </Form>
+ </template>
+ );
+
+ const groupSelector = selectKit(".combo-box");
+ const header = groupSelector.header();
+
+ assert.strictEqual(header.value(), "2");
+ assert.dom(".btn-clear", header.el()).exists();
+
+ await click(header.el().querySelector(".btn-clear"));
+
+ assert.strictEqual(this.formApi.get("group_inbox_id"), null);
+ });
+
test("preserves focus for collection fields while typing", async function (assert) {
this.setProperties({
configuration: {