Skip to content

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

FEATURE: allows to assign a post (#42623)

Joffrey JAFFEUX40cf4a11b4a8
diff --git a/plugins/discourse-assign/config/locales/client.en.yml b/plugins/discourse-assign/config/locales/client.en.yml
index f70a7698b42..02e0d917e31 100644
--- a/plugins/discourse-assign/config/locales/client.en.yml
+++ b/plugins/discourse-assign/config/locales/client.en.yml
@@ -203,19 +203,24 @@ en:
node_unavailable:
requires_assign: "Requires the Assign plugin to be enabled"
nodes:
- "action:assign_topic": "Assign topic"
+ "action:assign_topic": "Assign topic or post"
"action:check_assignment": "Check assignment"
"trigger:assigned": "Assigned"
node_descriptions:
- "action:assign_topic": "Assign or unassign a topic"
+ "action:assign_topic": "Assign or unassign a topic or post"
"action:check_assignment": "Check whether a user or group is assigned to a topic or post"
"trigger:assigned": "Fires when a topic or post is assigned"
assign_topic:
+ resource: "Resource"
+ resources:
+ topic: "Topic"
+ post: "Post"
operation: "Operation"
operations:
assign: "Assign"
unassign: "Unassign"
topic_id: "Topic ID"
+ post_id: "Post ID"
assignee: "Assignee"
replace_existing: "Replace existing assignment"
check_assignment:
diff --git a/plugins/discourse-assign/config/locales/server.en.yml b/plugins/discourse-assign/config/locales/server.en.yml
index a7dd1f8de62..1b4c7f0a568 100644
--- a/plugins/discourse-assign/config/locales/server.en.yml
+++ b/plugins/discourse-assign/config/locales/server.en.yml
@@ -61,7 +61,7 @@ en:
quarterly: "quarterly"
discourse_workflows:
assign_topic:
- error: "Failed to assign topic: %{reason}"
+ error: "Failed to assign: %{reason}"
check_assignment:
unknown_target_type: "Unknown assignment target type: %{target_type}"
assign_mailer:
diff --git a/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb b/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
index 1d10d2b6b4b..5aaf23a7f8b 100644
--- a/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
+++ b/plugins/discourse-assign/lib/discourse_workflows/nodes/assign_topic/v1.rb
@@ -5,6 +5,7 @@ if defined?(DiscourseWorkflows)
module Nodes
module AssignTopic
class V1 < DiscourseWorkflows::NodeType
+ RESOURCES = %w[topic post].freeze
OPERATIONS = %w[assign unassign].freeze
description(
@@ -23,6 +24,13 @@ if defined?(DiscourseWorkflows)
run_scope: "per_item",
},
properties: {
+ resource: {
+ type: :options,
+ required: true,
+ options: RESOURCES,
+ default: "topic",
+ no_data_expression: true,
+ },
operation: {
type: :options,
required: true,
@@ -35,6 +43,20 @@ if defined?(DiscourseWorkflows)
topic_id: {
type: :string,
required: true,
+ display_options: {
+ show: {
+ resource: ["topic"],
+ },
+ },
+ },
+ post_id: {
+ type: :string,
+ required: true,
+ display_options: {
+ show: {
+ resource: ["post"],
+ },
+ },
},
assignee: {
type: :string,
@@ -64,10 +86,13 @@ if defined?(DiscourseWorkflows)
actor = exec_ctx.user || Discourse.system_user
items =
exec_ctx.input_items.map.with_index do |_item, item_index|
+ resource =
+ exec_ctx.get_node_parameter("resource", item_index, default: "topic").to_s
config = {
"operation" =>
exec_ctx.get_node_parameter("operation", item_index, default: "assign"),
- "topic_id" => exec_ctx.get_node_parameter("topic_id", item_index),
+ "resource" => resource,
+ "target_id" => exec_ctx.get_node_parameter("#{resource}_id", item_index),
"assignee" => exec_ctx.get_node_parameter("assignee", item_index),
"replace_existing" =>
exec_ctx.get_node_parameter("replace_existing", item_index, default: true),
@@ -81,9 +106,9 @@ if defined?(DiscourseWorkflows)
private
def process(actor, config)
- topic = ::Topic.find(config["topic_id"])
- assigner = ::Assigner.new(topic, actor)
- previously_assigned = topic.assignment&.assigned_to
+ target = find_target(config["resource"], config["target_id"])
+ assigner = ::Assigner.new(target, actor)
+ previously_assigned = target.assignment&.assigned_to
case config["operation"]
when "unassign"
@@ -92,9 +117,9 @@ if defined?(DiscourseWorkflows)
{ previously_assigned: assignee_data(previously_assigned, actor.guardian) }
else
assignee = find_assignee(config["assignee"])
- if config["replace_existing"] != false && topic.assignment
+ if config["replace_existing"] != false && target.assignment
assigner.unassign
- topic.association(:assignment).reset
+ target.association(:assignment).reset
end
result = assigner.assign(assignee)
@@ -115,6 +140,10 @@ if defined?(DiscourseWorkflows)
end
end
+ def find_target(resource, target_id)
+ resource == "post" ? ::Post.find(target_id) : ::Topic.find(target_id)
+ end
+
def find_assignee(identifier)
::User.find_by(username: identifier) || ::Group.find_by!(name: identifier)
end
diff --git a/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assign_topic/v1_spec.rb b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assign_topic/v1_spec.rb
index 6933edbd702..bbea14cdc76 100644
--- a/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assign_topic/v1_spec.rb
+++ b/plugins/discourse-assign/spec/lib/discourse_workflows/nodes/assign_topic/v1_spec.rb
@@ -15,6 +15,34 @@ RSpec.describe DiscourseWorkflows::Nodes::AssignTopic::V1 do
SiteSetting.assign_enabled = true
end
+ describe ".property_schema" do
+ it "selects a topic by default and conditionally requires the target ID" do
+ schema = described_class.property_schema
+
+ expect(schema[:resource]).to include(
+ default: "topic",
+ options: %w[topic post],
+ no_data_expression: true,
+ )
+ expect(schema[:topic_id]).to include(
+ required: true,
+ display_options: {
+ show: {
+ resource: ["topic"],
+ },
+ },
+ )
+ expect(schema[:post_id]).to include(
+ required: true,
+ display_options: {
+ show: {
+ resource: ["post"],
+ },
+ },
+ )
+ end
+ end
+
def execute_node(configuration:, item:, run_as_user: Discourse.system_user)
action = described_class.new(parameters: configuration)
input_items = [item]
@@ -174,6 +202,81 @@ RSpec.describe DiscourseWorkflows::Nodes::AssignTopic::V1 do
expect(topic.assignment.assigned_by_user_id).to eq(Discourse.system_user.id)
end
+
+ it "assigns a post to a user when the post resource is selected", :aggregate_failures do
+ config = {
+ "resource" => "post",
+ "operation" => "assign",
+ "post_id" => post.id.to_s,
+ "assignee" => user.username,
+ }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["assignee"]["type"]).to eq("user")
+ expect(result["assignee"]["user"]["id"]).to eq(user.id)
+ expect(Assignment.exists?(target: post, assigned_to: user)).to eq(true)
+ expect(result).to match_node_output_schema(described_class)
+ end
+
+ it "assigns a post to a group when the post resource is selected", :aggregate_failures do
+ SiteSetting.assign_allowed_on_groups = "#{assign_allowed_group.id}|#{group.id}"
+ group.update!(assignable_level: Group::ALIAS_LEVELS[:everyone])
+ config = {
+ "resource" => "post",
+ "operation" => "assign",
+ "post_id" => post.id.to_s,
+ "assignee" => group.name,
+ }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["assignee"]["type"]).to eq("group")
+ expect(result["assignee"]["group"]["id"]).to eq(group.id)
+ expect(Assignment.exists?(target: post, assigned_to: group)).to eq(true)
+ end
+
+ it "replaces an existing post assignment", :aggregate_failures do
+ other_user = Fabricate(:moderator)
+ ::Assigner.new(post, Discourse.system_user).assign(other_user)
+ config = {
+ "resource" => "post",
+ "operation" => "assign",
+ "post_id" => post.id.to_s,
+ "assignee" => user.username,
+ }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["previously_assigned"]["user"]["username"]).to eq(other_user.username)
+ expect(Assignment.find_by(target: post).assigned_to).to eq(user)
+ end
+
+ it "raises when the selected post does not exist" do
+ config = {
+ "resource" => "post",
+ "operation" => "assign",
+ "post_id" => "-1",
+ "assignee" => user.username,
+ }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ ActiveRecord::RecordNotFound,
+ )
+ end
+
+ it "raises a node error when the actor cannot assign the selected post" do
+ config = {
+ "resource" => "post",
+ "operation" => "assign",
+ "post_id" => post.id.to_s,
+ "assignee" => user.username,
+ }
+
+ expect do
+ execute_node(configuration: config, item: item, run_as_user: Fabricate(:user))
+ end.to raise_error(DiscourseWorkflows::NodeError, /Failed to assign:/)
+ end
end
context "with unassign operation" do
@@ -196,6 +299,16 @@ RSpec.describe DiscourseWorkflows::Nodes::AssignTopic::V1 do
expect { execute_node(configuration: config, item: item) }.not_to raise_error
end
+
+ it "unassigns a post when the post resource is selected" do
+ ::Assigner.new(post, Discourse.system_user).assign(user)
+ config = { "resource" => "post", "operation" => "unassign", "post_id" => post.id.to_s }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["previously_assigned"]["user"]["username"]).to eq(user.username)
+ expect(Assignment.exists?(target: post)).to eq(false)
+ end
end
end
end