A platform for community discussion. Free, open, simple.
FEATURE: adds bump operation to topic action (#42410)
Joffrey JAFFEUXa88666d33b09
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex bdced5d422a..6a7961247f2 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -593,8 +593,11 @@ en:list: "List topics"close: "Close topic"archive: "Archive topic"+ bump: "Bump topic"set_custom_fields: "Set custom fields"topic_id: "Topic ID"+ silent: "Bump silently"+ silent_description: "Move the topic to the top of the latest list without adding a visible entry to it"title: "Title"raw: "Body"raw_description: "Raw content for the first post"diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.ymlindex 21f2f955a29..c2cdbabaf84 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -83,6 +83,7 @@ en:inconsistent_format: "Every returned item must use the same format."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."diff --git a/plugins/discourse-workflows/config/templates/bump-topic.json b/plugins/discourse-workflows/config/templates/bump-topic.jsonnew file mode 100644index 00000000000..6b8f5a0ef90--- /dev/null+++ b/plugins/discourse-workflows/config/templates/bump-topic.json@@ -0,0 +1,53 @@+{+ "name": "Bump topic from the admin menu",+ "description": "Adds a button to the topic admin menu that immediately bumps the topic to the top of the latest list.",+ "nodes": [+ {+ "id": "bump-button",+ "type": "trigger:topic_admin_button",+ "typeVersion": "1.0",+ "name": "Bump button",+ "parameters": {+ "label": "Bump topic",+ "icon": "arrow-up"+ },+ "credentials": {},+ "webhookId": null,+ "position": {+ "x": 12,+ "y": 22+ }+ },+ {+ "id": "bump-topic",+ "type": "action:topic",+ "typeVersion": "1.0",+ "name": "Bump topic",+ "parameters": {+ "operation": "bump",+ "topic_id": "={{ $json.topic.id }}",+ "silent": false,+ "actor_username": "system"+ },+ "credentials": {},+ "webhookId": null,+ "position": {+ "x": 292,+ "y": 22+ }+ }+ ],+ "connections": {+ "Bump button": {+ "main": [+ [+ {+ "node": "Bump topic",+ "type": "main",+ "index": 0+ }+ ]+ ]+ }+ }+}diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rbindex 876656e7fec..e95b787f9e7 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic/v1.rb@@ -4,7 +4,7 @@ module DiscourseWorkflowsmodule Nodesmodule Topicclass V1 < NodeType- OPERATIONS = %w[create get list close archive set_custom_fields].freeze+ OPERATIONS = %w[create get list close archive bump set_custom_fields].freezeMAX_LIMIT = 100DEFAULT_LIMIT = 30CUSTOM_FIELD_OPTIONS_LIMIT = 100@@ -48,7 +48,21 @@ module DiscourseWorkflowsrequired: true,display_options: {show: {- operation: %w[get close archive set_custom_fields],+ operation: %w[get close archive bump set_custom_fields],+ },+ },+ },+ silent: {+ type: :boolean,+ required: false,+ default: false,+ ui: {+ control: :boolean,+ expression: true,+ },+ display_options: {+ show: {+ operation: ["bump"],},},},@@ -217,6 +231,7 @@ module DiscourseWorkflows"operation" =>exec_ctx.get_node_parameter("operation", item_index, default: "create"),"topic_id" => exec_ctx.get_node_parameter("topic_id", item_index),+ "silent" => exec_ctx.get_node_parameter("silent", item_index, default: false),"title" => exec_ctx.get_node_parameter("title", item_index),"raw" => exec_ctx.get_node_parameter("raw", item_index),"category_id" => exec_ctx.get_node_parameter("category_id", item_index),@@ -250,6 +265,8 @@ module DiscourseWorkflowswrap(close_topic(exec_ctx, config, item_index))when "archive"wrap(archive_topic(exec_ctx, config, item_index))+ when "bump"+ wrap(bump_topic(exec_ctx, config, item_index))when "set_custom_fields"wrap(set_custom_fields(exec_ctx, config, item_index))else@@ -379,6 +396,32 @@ module DiscourseWorkflows}end+ def bump_topic(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_see!(topic)+ guardian.ensure_can_update_bumped_at!++ if config["silent"].present?+ topic.update_column(:bumped_at, Time.zone.now)+ elsif topic.add_small_action(+ actor,+ "autobumped",+ nil,+ bump: true,+ skip_guardian: true,+ ).blank?+ raise_node_error!(I18n.t("discourse_workflows.errors.topic.bump_failed"))+ end++ topic.reload+ {+ topic: exec_ctx.serialize_topic(topic, guardian: guardian),+ post: post_data(topic.first_post),+ }+ end+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)diff --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 bc2de3ddd7f..aa88f9ed34e 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@@ -344,6 +344,69 @@ RSpec.describe DiscourseWorkflows::Nodes::Topic::V1 doendend+ context "with operation 'bump'" do+ fab!(:topic) { Fabricate(:topic, category: category, user: user) }+ fab!(:post) { Fabricate(:post, topic: topic, user: user) }++ before do+ freeze_time+ topic.update_columns(bumped_at: 1.month.ago)+ end++ it "bumps the topic and adds a small action explaining the move" do+ result =+ execute_node(+ configuration: {+ "operation" => "bump",+ "topic_id" => topic.id.to_s,+ "actor_username" => admin.username,+ },+ item: item,+ )++ expect(topic.reload.bumped_at).to eq_time(Time.zone.now)+ expect(topic.posts.last).to have_attributes(+ post_type: Post.types[:small_action],+ action_code: "autobumped",+ )+ expect(result["topic"]).to include(+ "id" => topic.id,+ "bumped_at" => Time.zone.now.utc.iso8601,+ )+ end++ it "bumps the topic without adding a post when silent is true" do+ expect do+ execute_node(+ configuration: {+ "operation" => "bump",+ "topic_id" => topic.id.to_s,+ "silent" => true,+ "actor_username" => admin.username,+ },+ item: item,+ )+ end.not_to change { topic.posts.count }++ expect(topic.reload.bumped_at).to eq_time(Time.zone.now)+ end++ it "raises when the actor cannot update the bump date" do+ expect do+ execute_node(+ configuration: {+ "operation" => "bump",+ "topic_id" => topic.id.to_s,+ "actor_username" => user.username,+ },+ item: item,+ )+ end.to raise_error(Discourse::InvalidAccess)++ expect(topic.reload.bumped_at).to eq_time(1.month.ago)+ end+ end+context "with operation 'list'" dofab!(:topic_1, :topic) doFabricate(:topic, category: category, user: user, title: "First topic about workflows")