Skip to content

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

FEATURE: topic category node (#41947)

Joffrey JAFFEUX9f59557cfb97
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 2d8d2babb9a..ec93b193f65 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
@@ -3,6 +3,7 @@ import { tracked } from "@glimmer/tracking";
import { hash } from "@ember/helper";
import { action } from "@ember/object";
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
+import { service } from "@ember/service";
import Category from "discourse/models/category";
import CategoryChooser from "discourse/select-kit/components/category-chooser";
import CategorySelector from "discourse/select-kit/components/category-selector";
@@ -19,6 +20,8 @@ function categoryIdsFromValue(value) {
}
export default class CategoryControl extends Component {
+ @service siteSettings;
+
@tracked selectedCategories = [];
constructor() {
@@ -35,7 +38,10 @@ export default class CategoryControl extends Component {
}
get clearable() {
- return !this.args.schema?.required;
+ return (
+ !this.args.schema?.required &&
+ this.siteSettings.allow_uncategorized_topics
+ );
}
get categoryIds() {
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index ea37b5c655f..3d4dbc10ed4 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -254,6 +254,7 @@ en:
"trigger:error": "Error trigger"
"flow:wait": "Wait"
"action:modal": "Modal"
+ "action:topic_category": "Topic category"
node_descriptions:
"trigger:topic_closed": "Fires when a topic is closed by a user or auto-close timer"
"trigger:post_created": "Fires when a new post or reply is created"
@@ -304,6 +305,7 @@ en:
"trigger:error": "Fires when another workflow fails"
"flow:wait": "Pause workflow execution and resume after a delay or webhook callback"
"action:modal": "Shows a modal to a user and waits for them to choose an option"
+ "action:topic_category": "Set or clear the category of a topic"
topic_admin_button:
label: "Button label"
label_placeholder: "Run workflow"
@@ -358,6 +360,11 @@ en:
topic_id: "Topic ID"
tag_names: "Tags"
actor_username: "Performed by user"
+ topic_category:
+ topic_id: "Topic ID"
+ category_id: "Category"
+ category_id_description: "Leave empty to set no category."
+ actor_username: "Performed by user"
limit:
max_items: "Max items"
keep: "Keep"
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.yml
index 9efa957133c..b9b2d958f76 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -117,6 +117,10 @@ en:
topic_tags:
no_tag_names: "No tag names provided."
operation_failed: "Tag operation failed: %{errors}"
+ topic_category:
+ operation_failed: "Category change failed."
+ private_message: "Personal messages cannot have a category."
+ uncategorized_not_allowed: "This site does not allow topics without a category."
sort:
code_missing_return: "Code must contain a return statement."
split_out:
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb
index bb960ad3265..de65f16039c 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb
@@ -174,6 +174,16 @@ module DiscourseWorkflows
},
},
],
+ "action:topic_category" => [
+ {
+ name: "Move the trigger topic to another category",
+ parameters: {
+ topic_id: "={{ $json.topic.id }}",
+ category_id: 123,
+ actor_username: "system",
+ },
+ },
+ ],
}.freeze
QUERY_STOP_WORDS = %w[
@@ -200,6 +210,7 @@ module DiscourseWorkflows
"user profile bio title trust level lock groups fields lookup edit update",
"action:flag_post" =>
"flag spam moderation review queue reviewable hide delete silence triage report",
+ "action:topic_category" => "category recategorize move topic uncategorized",
}.freeze
def self.signature
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_category/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_category/v1.rb
new file mode 100644
index 00000000000..5836a7f4112
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_category/v1.rb
@@ -0,0 +1,118 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module TopicCategory
+ class V1 < NodeType
+ description(
+ name: "action:topic_category",
+ version: "1.0",
+ defaults: {
+ icon: "folder-open",
+ color: "deep-orange",
+ },
+ group: "discourse_actions",
+ capabilities: {
+ run_scope: "per_item",
+ },
+ output_contracts: [
+ {
+ schema: {
+ "$schema" => Schema::DRAFT_URI,
+ "type" => "object",
+ "properties" => {
+ "topic_id" => {
+ "type" => "integer",
+ },
+ "category_id" => {
+ "type" => "integer",
+ },
+ "old_category_id" => {
+ "type" => "integer",
+ },
+ },
+ },
+ },
+ ],
+ properties: {
+ topic_id: {
+ type: :string,
+ required: true,
+ },
+ category_id: {
+ type: :integer,
+ required: false,
+ ui: {
+ control: :category,
+ },
+ },
+ actor_username: {
+ type: :string,
+ required: false,
+ default: "system",
+ ui: {
+ control: :actor,
+ },
+ },
+ },
+ )
+
+ def execute(exec_ctx)
+ items =
+ exec_ctx.input_items.map.with_index do |_item, item_index|
+ config = {
+ "topic_id" => exec_ctx.get_node_parameter("topic_id", item_index),
+ "category_id" => exec_ctx.get_node_parameter("category_id", item_index),
+ }
+
+ wrap(process(exec_ctx, config, item_index))
+ end
+
+ [items]
+ end
+
+ private
+
+ def process(exec_ctx, config, item_index)
+ topic = ::Topic.find(config["topic_id"])
+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)
+ guardian = actor.guardian
+
+ category_id = target_category_id(config["category_id"])
+ old_category_id = topic.category_id
+
+ guardian.ensure_can_edit!(topic)
+ guardian.ensure_can_move_topic_to_category!(category_id)
+
+ if topic.private_message?
+ raise_node_error!(I18n.t("discourse_workflows.errors.topic_category.private_message"))
+ end
+
+ if category_id == SiteSetting.uncategorized_category_id &&
+ !SiteSetting.allow_uncategorized_topics
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.topic_category.uncategorized_not_allowed"),
+ )
+ end
+
+ if !topic.change_category_to_id(category_id) || topic.errors.present?
+ errors = topic.errors.full_messages.presence
+ raise_node_error!(
+ errors&.join(", ") ||
+ I18n.t("discourse_workflows.errors.topic_category.operation_failed"),
+ )
+ end
+
+ { topic_id: topic.id, category_id: topic.category_id, old_category_id: old_category_id }
+ end
+
+ def target_category_id(raw_value)
+ category_id = raw_value.presence.to_i
+ return SiteSetting.uncategorized_category_id if category_id.zero?
+
+ ::Category.find(category_id).id
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_category_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_category_spec.rb
new file mode 100644
index 00000000000..1b56350f6f5
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_category_spec.rb
@@ -0,0 +1,137 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::TopicCategory::V1 do
+ fab!(:category)
+ fab!(:other_category, :category)
+ fab!(:topic) { Fabricate(:topic, category: category) }
+
+ describe "#execute" do
+ let(:item) { { "json" => {} } }
+
+ it "moves the topic to the given category" do
+ config = { "topic_id" => topic.id.to_s, "category_id" => other_category.id.to_s }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["topic_id"]).to eq(topic.id)
+ expect(result["category_id"]).to eq(other_category.id)
+ expect(result["old_category_id"]).to eq(category.id)
+ expect(topic.reload.category_id).to eq(other_category.id)
+ end
+
+ it "accepts an integer category value" do
+ config = { "topic_id" => topic.id.to_s, "category_id" => other_category.id }
+
+ execute_node(configuration: config, item: item)
+
+ expect(topic.reload.category_id).to eq(other_category.id)
+ end
+
+ it "moves the topic to the uncategorized category when the category is blank" do
+ SiteSetting.allow_uncategorized_topics = true
+
+ config = { "topic_id" => topic.id.to_s, "category_id" => "" }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["category_id"]).to eq(SiteSetting.uncategorized_category_id)
+ expect(topic.reload.category_id).to eq(SiteSetting.uncategorized_category_id)
+ end
+
+ it "succeeds without changes when the topic is already in the category" do
+ config = { "topic_id" => topic.id.to_s, "category_id" => category.id.to_s }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["category_id"]).to eq(category.id)
+ expect(result["old_category_id"]).to eq(category.id)
+ expect(topic.reload.category_id).to eq(category.id)
+ end
+
+ it "raises when the topic does not exist" do
+ config = { "topic_id" => "-1", "category_id" => other_category.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ ActiveRecord::RecordNotFound,
+ )
+ end
+
+ it "raises when the category does not exist" do
+ config = { "topic_id" => topic.id.to_s, "category_id" => "-1" }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ ActiveRecord::RecordNotFound,
+ )
+ end
+
+ it "raises when clearing the category while uncategorized topics are disabled" do
+ SiteSetting.allow_uncategorized_topics = false
+
+ config = { "topic_id" => topic.id.to_s, "category_id" => "" }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "This site does not allow topics without a category.",
+ )
+ expect(topic.reload.category_id).to eq(category.id)
+ end
+
+ it "raises for private message topics" do
+ pm_topic = Fabricate(:private_message_topic)
+
+ config = { "topic_id" => pm_topic.id.to_s, "category_id" => other_category.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "Personal messages cannot have a category.",
+ )
+ expect(pm_topic.reload.category_id).to be_nil
+ end
+
+ context "with a non-staff actor" do
+ fab!(:author) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:author_topic) { Fabricate(:topic, user: author, category: category) }
+
+ it "allows moving own topic to an allowed category" do
+ config = {
+ "topic_id" => author_topic.id.to_s,
+ "category_id" => other_category.id.to_s,
+ "actor_username" => author.username,
+ }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["category_id"]).to eq(other_category.id)
+ expect(author_topic.reload.category_id).to eq(other_category.id)
+ end
+
+ it "prevents moving another user's topic" do
+ other_user = Fabricate(:user, refresh_auto_groups: true)
+ config = {
+ "topic_id" => author_topic.id.to_s,
+ "category_id" => other_category.id.to_s,
+ "actor_username" => other_user.username,
+ }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ Discourse::InvalidAccess,
+ )
+ expect(author_topic.reload.category_id).to eq(category.id)
+ end
+
+ it "prevents moving a topic to a restricted category" do
+ staff_category = Fabricate(:private_category, group: Group[:staff])
+ config = {
+ "topic_id" => author_topic.id.to_s,
+ "category_id" => staff_category.id.to_s,
+ "actor_username" => author.username,
+ }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ Discourse::InvalidAccess,
+ )
+ expect(author_topic.reload.category_id).to eq(category.id)
+ end
+ end
+ end
+end
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 f8e559eb42e..1210230aad1 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
@@ -413,6 +413,8 @@ module("Integration | Component | workflows property engine", function (hooks) {
});
test("can clear optional category controls", async function (assert) {
+ this.siteSettings.allow_uncategorized_topics = true;
+
this.setProperties({
configuration: {
category_id: 2,
@@ -463,6 +465,57 @@ module("Integration | Component | workflows property engine", function (hooks) {
assert.strictEqual(this.formApi.get("category_id"), "");
});
+ test("hides the clear button on category controls when uncategorized topics are disallowed", async function (assert) {
+ this.siteSettings.allow_uncategorized_topics = false;
+
+ 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())
+ .doesNotExist("clearing would move the topic to uncategorized");
+ });
+
test("can clear optional group controls", async function (assert) {
this.setProperties({
configuration: {