A platform for community discussion. Free, open, simple.
FEATURE: topic_moderation node (#42993)
Joffrey JAFFEUX00cda38b0258
diff --git a/plugins/discourse-workflows/app/serializers/discourse_workflows/topic_serializer.rb b/plugins/discourse-workflows/app/serializers/discourse_workflows/topic_serializer.rbindex e55aa49e55c..e2d037fb900 100644--- a/plugins/discourse-workflows/app/serializers/discourse_workflows/topic_serializer.rb+++ b/plugins/discourse-workflows/app/serializers/discourse_workflows/topic_serializer.rb@@ -17,6 +17,7 @@ module DiscourseWorkflows:bumped_at,:archetype,:visible,+ :visibility_reason_id,:closed,:archived,:has_summary,@@ -95,6 +96,10 @@ module DiscourseWorkflowsobject.private_message?end+ def include_visibility_reason_id?+ object.visibility_reason_id.present?+ end+def custom_fieldsif object.custom_fields_preloaded?return object.preloaded_custom_fields.slice(*@custom_field_names)diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex 76e7a258cb2..97e40769350 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -270,6 +270,7 @@ en:"action:modal": "Modal""action:topic_category": "Topic category""action:topic_pin": "Topic pinning"+ "action:topic_moderation": "Topic moderation""action:flag_user": "Flag user"node_descriptions:"trigger:topic_closed": "Fires when a topic is closed by a user or auto-close timer"@@ -328,6 +329,7 @@ en:"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""action:topic_pin": "Pin or unpin a topic in its category, globally, or as a banner"+ "action:topic_moderation": "Perform moderation actions on a topic""action:flag_user": "Add a user to the review queue for a moderator to approve or reject. Best aimed at unapproved accounts, since approved ones can only be rejected by deletion"topic_admin_button:label: "Button label"@@ -441,6 +443,12 @@ en:timezone: "Timezone"timezone_none: "Workflow default"actor_username: "Performed by user"+ topic_moderation:+ operation: "Operation"+ operations:+ unlist_topic: "Unlist topic"+ topic_id: "Topic ID"+ 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.ymlindex c2cdbabaf84..be8f3451e79 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -76,6 +76,7 @@ en:node_does_not_accept_inputs: "%{node} does not accept inputs."duplicate_node_names: "Node names must be unique within a workflow. Duplicates: %{names}"no_rows_matched_filter: "No rows matched the provided filter."+ unknown_operation: "Unknown operation: %{operation}."actor:blank: "The user for '%{field}' resolved to a blank value."item:@@ -84,11 +85,9 @@ en:topic:create_failed: "Failed to create topic."bump_failed: "Failed to bump topic."- unknown_operation: "Unknown operation: %{operation}."post:topic_closed_or_archived: "Cannot create a post in a closed or archived topic."edit_failed: "Failed to edit post."- unknown_operation: "Unknown operation: %{operation}."invalid_filter: "Invalid post filter fragment: %{fragments}."flag_post:unknown_flag_type: "Unknown flag type: %{flag_type}."@@ -99,7 +98,6 @@ en:actor_not_staff: "The acting user must be a staff member to flag users."cannot_flag_staff: "'%{username}' cannot be flagged. Staff members and automated accounts cannot be added to the review queue."user:- unknown_operation: "Unknown operation: %{operation}."unknown_extensions: "Unknown user extensions: %{extensions}."invalid_updates: "User updates must be an object."invalid_trust_level: "Invalid trust level: %{level}."@@ -131,7 +129,6 @@ en:private_message: "Personal messages cannot have a category."uncategorized_not_allowed: "This site does not allow topics without a category."topic_pin:- unknown_operation: "Unknown operation: %{operation}."unknown_pin_type: "Unknown pin type: %{pin_type}."invalid_timestamp: "'%{field}' is not a valid date and time: '%{value}'."pin_not_allowed: "'%{username}' cannot pin or unpin topic %{topic_id}. Pinning requires staff, trust level 4, or moderator access to the topic's category."@@ -165,7 +162,6 @@ en:header_name_invalid: "Header name contains invalid characters."data_table:unknown_column: "Unknown column name '%{column}'."- unknown_operation: "Unknown operation: %{operation}."invalid_query: "Invalid query: %{errors}"invalid_row: "Invalid row: %{errors}"filter_required: "Filter must not be empty"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.rbindex cee5dae348b..e33c6f0e7ef 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@@ -194,6 +194,16 @@ module DiscourseWorkflows},},],+ "action:topic_moderation" => [+ {+ name: "Unlist the trigger topic",+ parameters: {+ operation: "unlist_topic",+ topic_id: "={{ $json.topic.id }}",+ actor_username: "system",+ },+ },+ ],}.freezeQUERY_STOP_WORDS = %w[@@ -229,6 +239,8 @@ module DiscourseWorkflows"action:flag_post" =>"flag spam moderation review queue reviewable hide delete silence triage report","action:topic_category" => "category recategorize move topic uncategorized",+ "action:topic_moderation" =>+ "moderate moderation unlist unlisted topic visibility invisible",}.freezedef self.signaturediff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/data_table/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/data_table/v1.rbindex d671f0a5d99..3397e0b1b68 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/data_table/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/data_table/v1.rb@@ -250,10 +250,7 @@ module DiscourseWorkflowsrow_existence_items(config, item, operation_name, node_proxy)elseraise_node_error!(- I18n.t(- "discourse_workflows.errors.data_table.unknown_operation",- operation: operation_name,- ),+ I18n.t("discourse_workflows.errors.unknown_operation", operation: operation_name),)endenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rbindex 0e233638097..d859cb86357 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post/v1.rb@@ -389,7 +389,7 @@ module DiscourseWorkflowselseraise_node_error!(I18n.t(- "discourse_workflows.errors.post.unknown_operation",+ "discourse_workflows.errors.unknown_operation",operation: config["operation"],),)diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rbindex e95b787f9e7..6dec3561809 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb@@ -272,7 +272,7 @@ module DiscourseWorkflowselseraise_node_error!(I18n.t(- "discourse_workflows.errors.topic.unknown_operation",+ "discourse_workflows.errors.unknown_operation",operation: config["operation"],),)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.rbnew file mode 100644index 00000000000..efe665916bd--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_moderation/v1.rb@@ -0,0 +1,84 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module TopicModeration+ class V1 < NodeType+ OPERATIONS = %w[unlist_topic].freeze++ description(+ name: "action:topic_moderation",+ version: "1.0",+ defaults: {+ icon: "shield-halved",+ color: "red",+ },+ group: "discourse_actions",+ capabilities: {+ run_scope: "per_item",+ },+ output_contracts: [{ schema: Schema::TOPIC_LIST_ITEM_SCHEMA }],+ properties: {+ operation: {+ type: :options,+ required: true,+ options: OPERATIONS,+ default: "unlist_topic",+ ui: {+ expression: false,+ },+ },+ topic_id: {+ type: :string,+ required: true,+ },+ 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|+ operation =+ exec_ctx.get_node_parameter("operation", item_index, default: "unlist_topic")+ topic_id = exec_ctx.get_node_parameter("topic_id", item_index)++ wrap(process(exec_ctx, operation, topic_id, item_index))+ end++ [items]+ end++ private++ def process(exec_ctx, operation, topic_id, item_index)+ if !OPERATIONS.include?(operation)+ raise_node_error!(+ I18n.t("discourse_workflows.errors.unknown_operation", operation: operation),+ )+ end++ topic = ::Topic.find(topic_id)+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)+ actor.guardian.ensure_can_toggle_topic_visibility!(topic)++ TopicStatusUpdater.new(topic, actor).update!(+ "visible",+ false,+ visibility_reason_id: ::Topic.visibility_reasons[:manually_unlisted],+ )++ topic.reload+ { topic: exec_ctx.serialize_topic(topic, guardian: actor.guardian) }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pin/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pin/v1.rbindex d1eb4bd31a3..683c90e6ebf 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pin/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pin/v1.rb@@ -166,8 +166,20 @@ module DiscourseWorkflowsprivatedef process(exec_ctx, config, item_index)- operation = validated(config["operation"], OPERATIONS, "unknown_operation", :operation)- pin_type = validated(config["pin_type"], PIN_TYPES, "unknown_pin_type", :pin_type)+ operation =+ validated(+ config["operation"],+ OPERATIONS,+ "discourse_workflows.errors.unknown_operation",+ :operation,+ )+ pin_type =+ validated(+ config["pin_type"],+ PIN_TYPES,+ "discourse_workflows.errors.topic_pin.unknown_pin_type",+ :pin_type,+ )topic = ::Topic.find(config["topic_id"])actor = exec_ctx.actor_from_parameter("actor_username", item_index)@@ -275,9 +287,7 @@ module DiscourseWorkflowsvalue = value.to_sreturn value if allowed.include?(value)- raise_node_error!(- I18n.t("discourse_workflows.errors.topic_pin.#{error_key}", param => value),- )+ raise_node_error!(I18n.t(error_key, param => value))endendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rbindex 2eb6e0b84f7..8ad4c72513e 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/user/v1.rb@@ -134,7 +134,7 @@ module DiscourseWorkflowsedit_user(exec_ctx, user, actor, item_index, extensions:)elseraise_node_error!(- I18n.t("discourse_workflows.errors.user.unknown_operation", operation: operation),+ I18n.t("discourse_workflows.errors.unknown_operation", operation: operation),)endenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/schema.rb b/plugins/discourse-workflows/lib/discourse_workflows/schema.rbindex c85b1208eaa..46c7b1da62b 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/schema.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/schema.rb@@ -61,6 +61,8 @@ module DiscourseWorkflows}},"first_post_id": { "type": "integer" },+ "visible": { "type": "boolean" },+ "visibility_reason_id": { "type": "integer" },"closed": { "type": "boolean" },"archived": { "type": "boolean" },"created_at": { "type": "string", "format": "date-time" },diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rbindex ebab2376b94..20703b1c6c2 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb@@ -61,6 +61,28 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog do)end+ it "finds the moderation action by unlist keywords", :aggregate_failures do+ result = invoke_tool(query: "unlist topic", include_examples: true)+ node = result[:nodes].find { |candidate| candidate[:type] == "action:topic_moderation" }++ expect(node[:examples]).to contain_exactly(+ include(+ name: "Unlist the trigger topic",+ parameters:+ include(+ operation: "unlist_topic",+ topic_id: "={{ $json.topic.id }}",+ actor_username: "system",+ ),+ ),+ )+ expect(node.dig(:output_contracts, 0, :fields)).to include(+ "topic.id" => "integer",+ "topic.visible" => "boolean",+ "topic.visibility_reason_id" => "integer",+ )+ end+it "exposes post author, topic link, and action output fields", :aggregate_failures doexpect(result[:status]).to eq("success")expect(output_fields_by_type.fetch("trigger:topic_created")).to include(diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_moderation_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_moderation_spec.rbnew file mode 100644index 00000000000..ea662bb42d8--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_moderation_spec.rb@@ -0,0 +1,132 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::TopicModeration::V1 do+ fab!(:category)+ fab!(:topic) { Fabricate(:topic, category: category) }+ fab!(:user)++ describe ".property_schema" do+ it "declares a fixed unlist topic operation" do+ expect(described_class.property_schema[:operation]).to include(+ type: :options,+ options: ["unlist_topic"],+ default: "unlist_topic",+ ui: {+ expression: false,+ },+ )+ end+ end++ describe "#execute" do+ it "unlists a topic as the system user", :aggregate_failures do+ result = nil++ expect do+ result =+ execute_node(+ configuration: {+ "operation" => "unlist_topic",+ "topic_id" => topic.id.to_s,+ },+ )+ end.to change { topic.reload.posts.count }.by(1)++ topic.reload+ expect(topic.visible).to eq(false)+ expect(topic.visibility_reason_id).to eq(Topic.visibility_reasons[:manually_unlisted])+ expect(topic.posts.last).to have_attributes(+ action_code: "visible.disabled",+ user_id: Discourse.system_user.id,+ )+ expect(result["topic"]).to include(+ "id" => topic.id,+ "visible" => false,+ "visibility_reason_id" => Topic.visibility_reasons[:manually_unlisted],+ )+ expect(result).to match_node_output_schema(described_class)+ end++ it "allows a category group moderator to unlist a topic" do+ group = Fabricate(:group)+ group_moderator = Fabricate(:user)+ group.add(group_moderator)+ Fabricate(:category_moderation_group, category: category, group: group)+ SiteSetting.enable_category_group_moderation = true++ execute_node(+ configuration: {+ "operation" => "unlist_topic",+ "topic_id" => topic.id.to_s,+ "actor_username" => group_moderator.username,+ },+ )++ expect(topic.reload.visible).to eq(false)+ expect(topic.posts.last.user_id).to eq(group_moderator.id)+ end++ it "rejects an actor without permission" do+ expect do+ execute_node(+ configuration: {+ "operation" => "unlist_topic",+ "topic_id" => topic.id.to_s,+ "actor_username" => user.username,+ },+ )+ end.to raise_error(Discourse::InvalidAccess)++ expect(topic.reload.visible).to eq(true)+ end++ it "does not create another action post when the topic is already unlisted" do+ topic.update!(+ visible: false,+ visibility_reason_id: Topic.visibility_reasons[:manually_unlisted],+ )+ result = nil++ expect do+ result =+ execute_node(+ configuration: {+ "operation" => "unlist_topic",+ "topic_id" => topic.id.to_s,+ },+ )+ end.not_to change { topic.reload.posts.count }++ expect(result.dig("topic", "visible")).to eq(false)+ end++ it "unlists each input item's topic separately" do+ second_topic = Fabricate(:topic)++ output =+ execute_node_output(+ configuration: {+ "operation" => "unlist_topic",+ "topic_id" => "={{ $json.topic_id }}",+ },+ input_items: [+ { "json" => { "topic_id" => topic.id } },+ { "json" => { "topic_id" => second_topic.id } },+ ],+ ).first++ expect(output.map { |item| item.dig("json", "topic", "id") }).to eq(+ [topic.id, second_topic.id],+ )+ expect([topic.reload.visible, second_topic.reload.visible]).to eq([false, false])+ end++ it "raises on an unknown operation" do+ expect do+ execute_node(configuration: { "operation" => "delete_topic", "topic_id" => topic.id.to_s })+ end.to raise_error(DiscourseWorkflows::NodeError, "Unknown operation: delete_topic.")++ expect(topic.reload.visible).to eq(true)+ end+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/schema_serializer_sync_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/schema_serializer_sync_spec.rbindex bbf91621ba1..723db0e7dd9 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/schema_serializer_sync_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/schema_serializer_sync_spec.rb@@ -32,7 +32,6 @@ RSpec.describe DiscourseWorkflows::Schema dounpinnedunseenviews- visible]end@@ -79,6 +78,7 @@ RSpec.describe DiscourseWorkflows::Schema doexpect_ledger(described_class::TOPIC_PROPERTIES,serialized_keys(DiscourseWorkflows::TopicSerializer, post.topic),+ unemitted: %w[visibility_reason_id],undeclared: topic_undeclared,)end@@ -87,7 +87,7 @@ RSpec.describe DiscourseWorkflows::Schema doexpect_ledger(described_class::TOPIC_PROPERTIES,serialized_keys(DiscourseWorkflows::TopicListItemSerializer, post.topic),- unemitted: %w[first_post_id],+ unemitted: %w[first_post_id visibility_reason_id],undeclared: topic_undeclared,)end