Skip to content

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

FEATURE: adds pin topic node action to workflows (#42300)

Joffrey JAFFEUXfb7467a27364
diff --git a/app/assets/stylesheets/common/base/modal.scss b/app/assets/stylesheets/common/base/modal.scss
index 2bc3647f0b3..f17cf9042d2 100644
--- a/app/assets/stylesheets/common/base/modal.scss
+++ b/app/assets/stylesheets/common/base/modal.scss
@@ -166,7 +166,7 @@ html:not(.keyboard-visible).mobile-device {
display: none;
}
- input {
+ input:where(:not(.form-kit__control-input)) {
width: auto;
&[type="text"],
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 783e182e63e..6d406694976 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
@@ -79,6 +79,11 @@ const FIELD_CONTROL_REGISTRY = {
checkbox: { kind: "field", type: "checkbox", renderer: DefaultInputControl },
textarea: { kind: "field", type: "textarea", renderer: DefaultInputControl },
time: { kind: "field", type: "input-time", renderer: DefaultInputControl },
+ date_time: {
+ kind: "field",
+ type: "input-datetime-local",
+ renderer: DefaultInputControl,
+ },
timezone: { kind: "field", type: "custom", renderer: TimezoneControl },
default: {
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index b5cb4d6733f..315f88e5b1d 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -156,7 +156,7 @@ en:
error_workflow_none: "None"
no_error_workflows_available: "No workflows with an Error trigger node are available"
timezone: "Timezone"
- timezone_description: "Schedule triggers and time conditions use this timezone. If unset, they use the site timezone."
+ timezone_description: "Schedule triggers, time conditions, and date values without a timezone offset use this timezone. If unset, they use the site timezone."
danger_zone: "Danger zone"
delete_description: "Permanently delete this workflow and all its executions. This action cannot be undone."
versions:
@@ -265,6 +265,7 @@ en:
"flow:wait": "Wait"
"action:modal": "Modal"
"action:topic_category": "Topic category"
+ "action:topic_pin": "Topic pinning"
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"
@@ -318,6 +319,7 @@ en:
"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"
+ "action:topic_pin": "Pin or unpin a topic in its category, globally, or as a banner"
topic_admin_button:
label: "Button label"
label_placeholder: "Run workflow"
@@ -407,6 +409,27 @@ en:
category_id: "Category"
category_id_description: "Leave empty to set no category."
actor_username: "Performed by user"
+ topic_pin:
+ operation: "Operation"
+ operations:
+ add: "Pin"
+ remove: "Unpin"
+ pin_type: "Pin type"
+ pin_types:
+ pin: "Pinned topic"
+ banner: "Banner topic"
+ topic_id: "Topic ID"
+ pinned_globally: "Pin globally"
+ pinned_globally_description: "Show the topic at the top of all topic lists instead of only its category."
+ pinned_until: "Pinned until"
+ pinned_until_description: "Automatically unpin at this time. Leave empty to pin indefinitely."
+ pinned_until_dynamic_hint: "Must resolve to a date and time, for example 2026-08-01 12:00."
+ bannered_until: "Bannered until"
+ bannered_until_description: "Automatically remove the banner at this time. Leave empty to keep the banner indefinitely."
+ bannered_until_dynamic_hint: "Must resolve to a date and time, for example 2026-08-01 12:00."
+ timezone: "Timezone"
+ timezone_none: "Workflow default"
+ 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 c5fa86d6afe..83232b31cd2 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -52,6 +52,7 @@ en:
webhook_timed_out: "Webhook timed out"
schedule_rules_required: "At least one schedule rule is required."
invalid_schedule_rule: "One or more schedule rules are invalid."
+ invalid_timezone: "Invalid timezone: %{timezone}."
not_enabled: "The discourse-workflows plugin is not enabled."
no_permission_to_manage: "You don't have permission to manage workflows."
node_preview_failed: "Something went wrong while previewing this step."
@@ -114,7 +115,6 @@ en:
invalid_time_range: "Start and end times must use the HH:MM format."
start_end_equal: "Start and end times cannot be the same."
invalid_time: "'%{field}' is not a valid HH:MM time: '%{value}'."
- invalid_timezone: "Invalid timezone: %{timezone}."
modal:
no_target_user: "No target user is available for the modal."
user_not_found: "User '%{username}' not found."
@@ -125,6 +125,15 @@ en:
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."
+ 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."
+ pin_globally_not_allowed: "'%{username}' cannot pin topic %{topic_id} globally. Pinning globally requires staff or trust level 4."
+ banner_not_allowed: "'%{username}' cannot make topic %{topic_id} a banner. Banner topics can only be managed by staff."
+ banner_private_message: "Personal messages cannot be made into banner topics."
+ banner_restricted_category: "Topics in a restricted category cannot be made into banner topics."
sort:
code_missing_return: "Code must contain a return statement."
summarize:
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
index b07a5651f37..2c6055c3017 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb
@@ -251,6 +251,17 @@ module DiscourseWorkflows
end
end
+ def self.expression_value?(value)
+ value.is_a?(String) && value.start_with?("=")
+ end
+
+ def self.validate_timezone_configuration(configuration, errors)
+ timezone = (configuration || {}).deep_stringify_keys["timezone"].presence
+ return if timezone.nil? || expression_value?(timezone) || WorkflowTimezone.valid?(timezone)
+
+ errors.add(:base, I18n.t("discourse_workflows.errors.invalid_timezone", timezone: timezone))
+ end
+
def initialize(**)
end
@@ -292,6 +303,20 @@ module DiscourseWorkflows
self.class.matches_category_ids?(topic_category_id, category_ids, include_subcategories:)
end
+ def resolve_timezone(exec_ctx, item_index)
+ timezone = exec_ctx.get_node_parameter("timezone", item_index, default: nil).to_s.presence
+ return exec_ctx.get_timezone if timezone.nil?
+
+ if !WorkflowTimezone.valid?(timezone)
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.invalid_timezone", timezone: timezone),
+ item_index: item_index,
+ )
+ end
+
+ timezone
+ end
+
def wrap(data, paired_item: nil)
Item.wrap(data, paired_item:)
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/time_window/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/time_window/v1.rb
index 6dc639ef6f0..2f6b1e77bd3 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/time_window/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/time_window/v1.rb
@@ -110,7 +110,7 @@ module DiscourseWorkflows
if ActiveModel::Type::Boolean.new.cast(config["use_time_range"])
start_value = config["start_time"]
end_value = config["end_time"]
- static_values = [start_value, end_value].reject { |value| expression?(value) }
+ static_values = [start_value, end_value].reject { |value| expression_value?(value) }
if static_values.any? { |value| parse_minutes(value).nil? }
errors.add(:base, I18n.t("discourse_workflows.errors.time_window.invalid_time_range"))
@@ -119,17 +119,7 @@ module DiscourseWorkflows
end
end
- timezone = config["timezone"].presence
- if timezone && !expression?(timezone) && !WorkflowTimezone.valid?(timezone)
- errors.add(
- :base,
- I18n.t("discourse_workflows.errors.time_window.invalid_timezone", timezone: timezone),
- )
- end
- end
-
- def self.expression?(value)
- value.is_a?(String) && value.start_with?("=")
+ validate_timezone_configuration(configuration, errors)
end
def self.valid_wday?(value)
@@ -224,20 +214,6 @@ module DiscourseWorkflows
minutes
end
-
- def resolve_timezone(exec_ctx, item_index)
- timezone = exec_ctx.get_node_parameter("timezone", item_index, default: nil).to_s.presence
- return exec_ctx.get_timezone if timezone.nil?
-
- if !WorkflowTimezone.valid?(timezone)
- raise_node_error!(
- I18n.t("discourse_workflows.errors.time_window.invalid_timezone", timezone: timezone),
- item_index: item_index,
- )
- end
-
- timezone
- end
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pin/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pin/v1.rb
new file mode 100644
index 00000000000..d1eb4bd31a3
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_pin/v1.rb
@@ -0,0 +1,285 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module TopicPin
+ class V1 < NodeType
+ OPERATIONS = %w[add remove].freeze
+ PIN_TYPES = %w[pin banner].freeze
+
+ OUTPUT_SCHEMA = {
+ "$schema" => Schema::DRAFT_URI,
+ "type" => "object",
+ "properties" => {
+ "topic_id" => {
+ "type" => "integer",
+ },
+ "pinned" => {
+ "type" => "boolean",
+ },
+ "pinned_globally" => {
+ "type" => "boolean",
+ },
+ "pinned_at" => {
+ "type" => %w[string null],
+ "format" => "date-time",
+ },
+ "pinned_until" => {
+ "type" => %w[string null],
+ "format" => "date-time",
+ },
+ "banner" => {
+ "type" => "boolean",
+ },
+ "bannered_until" => {
+ "type" => %w[string null],
+ "format" => "date-time",
+ },
+ },
+ }.freeze
+
+ description(
+ name: "action:topic_pin",
+ version: "1.0",
+ defaults: {
+ icon: "thumbtack",
+ color: "pink",
+ },
+ group: "discourse_actions",
+ capabilities: {
+ run_scope: "per_item",
+ },
+ output_contracts: [{ schema: OUTPUT_SCHEMA }],
+ properties: {
+ operation: {
+ type: :options,
+ required: true,
+ options: OPERATIONS,
+ default: "add",
+ ui: {
+ expression: true,
+ },
+ },
+ pin_type: {
+ type: :options,
+ required: true,
+ options: PIN_TYPES,
+ default: "pin",
+ ui: {
+ expression: true,
+ },
+ },
+ topic_id: {
+ type: :string,
+ required: true,
+ },
+ pinned_globally: {
+ type: :boolean,
+ required: false,
+ default: false,
+ ui: {
+ control: :boolean,
+ expression: true,
+ },
+ display_options: {
+ show: {
+ operation: ["add"],
+ pin_type: ["pin"],
+ },
+ },
+ },
+ pinned_until: {
+ type: :string,
+ required: false,
+ ui: {
+ control: :date_time,
+ },
+ display_options: {
+ show: {
+ operation: ["add"],
+ pin_type: ["pin"],
+ },
+ },
+ },
+ bannered_until: {
+ type: :string,
+ required: false,
+ ui: {
+ control: :date_time,
+ },
+ display_options: {
+ show: {
+ operation: ["add"],
+ pin_type: ["banner"],
+ },
+ },
+ },
+ timezone: {
+ type: :string,
+ required: false,
+ ui: {
+ control: :timezone,
+ },
+ control_options: {
+ none: "discourse_workflows.topic_pin.timezone_none",
+ },
+ display_options: {
+ show: {
+ operation: ["add"],
+ },
+ },
+ },
+ actor_username: {
+ type: :string,
+ required: false,
+ default: "system",
+ ui: {
+ control: :actor,
+ },
+ },
+ },
+ )
+
+ def self.validate_configuration(configuration, errors)
+ validate_timezone_configuration(configuration, errors)
+ end
+
+ def execute(exec_ctx)
+ items =
+ exec_ctx.input_items.map.with_index do |_item, item_index|
+ config = {
+ "operation" => exec_ctx.get_node_parameter("operation", item_index, default: "add"),
+ "pin_type" => exec_ctx.get_node_parameter("pin_type", item_index, default: "pin"),
+ "topic_id" => exec_ctx.get_node_parameter("topic_id", item_index),
+ "pinned_globally" =>
+ exec_ctx.get_node_parameter("pinned_globally", item_index, default: false),
+ "pinned_until" => exec_ctx.get_node_parameter("pinned_until", item_index),
+ "bannered_until" => exec_ctx.get_node_parameter("bannered_until", item_index),
+ }
+
+ wrap(process(exec_ctx, config, item_index))
+ end
+
+ [items]
+ end
+
+ private
+
+ def 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)
+
+ topic = ::Topic.find(config["topic_id"])
+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)
+
+ if pin_type == "banner"
+ change_banner(exec_ctx, topic, actor, operation, config, item_index)
+ else
+ change_pin(exec_ctx, topic, actor, operation, config, item_index)
+ end
+
+ {
+ topic_id: topic.id,
+ pinned: topic.pinned_at.present?,
+ pinned_globally: topic.pinned_globally,
+ pinned_at: topic.pinned_at&.utc&.iso8601,
+ pinned_until: topic.pinned_until&.utc&.iso8601,
+ banner: topic.archetype == ::Archetype.banner,
+ bannered_until: topic.bannered_until&.utc&.iso8601,
+ }
+ end
+
+ def change_pin(exec_ctx, topic, actor, operation, config, item_index)
+ globally = operation == "add" && config["pinned_globally"].present?
+ ensure_can_pin!(topic, actor, globally: globally)
+
+ if operation == "remove"
+ return if topic.pinned_at.blank?
+ topic.update_status("pinned", false, actor)
+ else
+ status = globally ? "pinned_globally" : "pinned"
+ until_value = timestamp(exec_ctx, config, "pinned", item_index)
+ topic.update_status(status, true, actor, until: until_value)
+ end
+ end
+
+ def change_banner(exec_ctx, topic, actor, operation, config, item_index)
+ ensure_can_banner!(topic, actor)
+
+ if operation == "remove"
+ return if topic.archetype != ::Archetype.banner
+ topic.remove_banner!(actor)
+ else
+ topic.make_banner!(actor, timestamp(exec_ctx, config, "bannered", item_index))
+ end
+ end
+
+ def ensure_can_pin!(topic, actor, globally:)
+ guardian = actor.guardian
+ allowed = globally ? guardian.can_moderate?(topic) : guardian.can_pin_unpin_topic?(topic)
+ return if allowed
+
+ denied!(globally ? "pin_globally_not_allowed" : "pin_not_allowed", actor, topic)
+ end
+
+ def ensure_can_banner!(topic, actor)
+ return if actor.guardian.can_banner_topic?(topic)
+
+ if topic.private_message?
+ raise_node_error!(I18n.t("discourse_workflows.errors.topic_pin.banner_private_message"))
+ elsif topic.category&.read_restricted?
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.topic_pin.banner_restricted_category"),
+ )
+ else
+ denied!("banner_not_allowed", actor, topic)
+ end
+ end
+
+ def denied!(key, actor, topic)
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.topic_pin.#{key}",
+ username: actor.username,
+ topic_id: topic.id,
+ ),
+ )
+ end
+
+ def timestamp(exec_ctx, config, prefix, item_index)
+ value = config["#{prefix}_until"].to_s.presence
+ return if value.nil?
+
+ zone = ActiveSupport::TimeZone[resolve_timezone(exec_ctx, item_index)]
+ parsed =
+ begin
+ zone&.parse(value)
+ rescue ArgumentError
+ nil
+ end
+
+ if parsed.nil?
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.topic_pin.invalid_timestamp",
+ field: "#{prefix}_until",
+ value: value,
+ ),
+ )
+ end
+
+ parsed.iso8601
+ end
+
+ def validated(value, allowed, error_key, param)
+ value = value.to_s
+ return value if allowed.include?(value)
+
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.topic_pin.#{error_key}", param => value),
+ )
+ end
+ end
+ end
+ end
+end
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 30899af7742..6a72e3e5359 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/property_schema_validator.rb
@@ -100,6 +100,7 @@ module DiscourseWorkflows
data_table_columns
data_table_condition_builder
data_table_select
+ date_time
field_path
filter_query
group_select
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/time_window_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/time_window_spec.rb
index 85f5605f8f9..60bae6ac70b 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/time_window_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/time_window_spec.rb
@@ -382,7 +382,7 @@ RSpec.describe DiscourseWorkflows::Nodes::TimeWindow::V1 do
errors = validate("timezone" => "Mars/Olympus")
expect(errors[:base]).to include(
- I18n.t("discourse_workflows.errors.time_window.invalid_timezone", timezone: "Mars/Olympus"),
+ I18n.t("discourse_workflows.errors.invalid_timezone", timezone: "Mars/Olympus"),
)
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_pin_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_pin_spec.rb
new file mode 100644
index 00000000000..022e3329ee5
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_pin_spec.rb
@@ -0,0 +1,399 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::TopicPin::V1 do
+ fab!(:topic)
+ fab!(:user)
+ fab!(:trust_level_4)
+
+ let(:item) { { "json" => {} } }
+
+ describe "#execute" do
+ context "with the pin type" do
+ context "with the add operation" do
+ it "pins the topic in its category" do
+ config = { "operation" => "add", "topic_id" => topic.id.to_s }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["pinned"]).to eq(true)
+ expect(result["pinned_globally"]).to eq(false)
+ expect(result).to match_node_output_schema(described_class, configuration: config)
+
+ topic.reload
+ expect(topic.pinned_at).to be_present
+ expect(topic.pinned_globally).to eq(false)
+ end
+
+ it "pins the topic globally" do
+ config = { "operation" => "add", "topic_id" => topic.id.to_s, "pinned_globally" => true }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["pinned"]).to eq(true)
+ expect(result["pinned_globally"]).to eq(true)
+
+ topic.reload
+ expect(topic.pinned_at).to be_present
+ expect(topic.pinned_globally).to eq(true)
+ end
+
+ it "sets pinned_until" do
+ pinned_until = 3.days.from_now.utc.strftime("%Y-%m-%d %H:%M%z")
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "pinned_until" => pinned_until,
+ }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["pinned_until"]).to eq(Time.parse(pinned_until).utc.iso8601)
+ expect(topic.reload.pinned_until).to eq_time(Time.parse(pinned_until))
+ end
+
+ it "pins indefinitely when pinned_until is blank" do
+ config = { "operation" => "add", "topic_id" => topic.id.to_s, "pinned_until" => "" }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["pinned"]).to eq(true)
+ expect(result["pinned_until"]).to be_nil
+ expect(topic.reload.pinned_until).to be_nil
+ end
+
+ context "when pinned_until carries no timezone offset" do
+ fab!(:workflow) do
+ Fabricate(:discourse_workflows_workflow, settings: { "timezone" => "America/New_York" })
+ end
+
+ it "anchors the value to the workflow timezone" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "pinned_until" => "2026-08-01 12:00",
+ }
+
+ execute_node(configuration: config, item: item, workflow: workflow)
+
+ # noon in New York on that date is 16:00 UTC, regardless of the server clock
+ expect(topic.reload.pinned_until.utc).to eq_time(Time.utc(2026, 8, 1, 16, 0))
+ end
+
+ it "prefers the node timezone over the workflow timezone" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "pinned_until" => "2026-08-01 12:00",
+ "timezone" => "Europe/Paris",
+ }
+
+ execute_node(configuration: config, item: item, workflow: workflow)
+
+ expect(topic.reload.pinned_until.utc).to eq_time(Time.utc(2026, 8, 1, 10, 0))
+ end
+
+ it "applies the node timezone to bannered_until too" do
+ config = {
+ "operation" => "add",
+ "pin_type" => "banner",
+ "topic_id" => topic.id.to_s,
+ "bannered_until" => "2026-08-01 12:00",
+ "timezone" => "Europe/Paris",
+ }
+
+ execute_node(configuration: config, item: item, workflow: workflow)
+
+ expect(topic.reload.bannered_until.utc).to eq_time(Time.utc(2026, 8, 1, 10, 0))
+ end
+
+ it "raises a readable error for an unknown node timezone" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "pinned_until" => "2026-08-01 12:00",
+ "timezone" => "Mars/Olympus_Mons",
+ }
+
+ expect {
+ execute_node(configuration: config, item: item, workflow: workflow)
+ }.to raise_error(DiscourseWorkflows::NodeError, %r{Invalid timezone: Mars/Olympus_Mons})
+ end
+
+ it "keeps an explicit offset instead of reinterpreting it" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "pinned_until" => "2026-08-01 12:00+02:00",
+ }
+
+ execute_node(configuration: config, item: item, workflow: workflow)
+
+ expect(topic.reload.pinned_until.utc).to eq_time(Time.utc(2026, 8, 1, 10, 0))
+ end
+
+ it "raises a readable error for an unparseable value" do
+ config = { "operation" => "add", "topic_id" => topic.id.to_s, "pinned_until" => "soon" }
+
+ expect {
+ execute_node(configuration: config, item: item, workflow: workflow)
+ }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "'pinned_until' is not a valid date and time: 'soon'.",
+ )
+ end
+
+ it "raises a readable error for an out-of-range date" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "pinned_until" => "2026-13-45",
+ }
+
+ expect {
+ execute_node(configuration: config, item: item, workflow: workflow)
+ }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "'pinned_until' is not a valid date and time: '2026-13-45'.",
+ )
+ end
+ end
+
+ it "adds a small action post" do
+ config = { "operation" => "add", "topic_id" => topic.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.to change {
+ topic.posts.where(action_code: "pinned.enabled").count
+ }.by(1)
+ end
+ end
+
+ context "with the remove operation" do
+ it "unpins a topic pinned in its category" do
+ topic.update_pinned(true)
+
+ config = { "operation" => "remove", "topic_id" => topic.id.to_s }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["pinned"]).to eq(false)
+ expect(topic.reload.pinned_at).to be_nil
+ end
+
+ it "clears pinned_globally when unpinning a globally pinned topic" do
+ topic.update_pinned(true, true)
+
+ config = { "operation" => "remove", "topic_id" => topic.id.to_s }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["pinned"]).to eq(false)
+ expect(result["pinned_globally"]).to eq(false)
+
+ topic.reload
+ expect(topic.pinned_at).to be_nil
+ expect(topic.pinned_globally).to eq(false)
+ end
+
+ it "does nothing when the topic is not pinned" do
+ config = { "operation" => "remove", "topic_id" => topic.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.not_to change {
+ topic.posts.count
+ }
+ end
+ end
+ end
+
+ context "with the banner pin type" do
+ it "makes the topic a banner" do
+ config = { "operation" => "add", "pin_type" => "banner", "topic_id" => topic.id.to_s }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["banner"]).to eq(true)
+ expect(result).to match_node_output_schema(described_class, configuration: config)
+ expect(topic.reload.archetype).to eq(Archetype.banner)
+ end
+
+ it "replaces an existing banner topic" do
+ previous_banner = Fabricate(:topic, archetype: Archetype.banner)
+
+ config = { "operation" => "add", "pin_type" => "banner", "topic_id" => topic.id.to_s }
+
+ execute_node(configuration: config, item: item)
+
+ expect(topic.reload.archetype).to eq(Archetype.banner)
+ expect(previous_banner.reload.archetype).to eq(Archetype.default)
+ end
+
+ it "sets bannered_until" do
+ bannered_until = 3.days.from_now.utc.strftime("%Y-%m-%d %H:%M%z")
+ config = {
+ "operation" => "add",
+ "pin_type" => "banner",
+ "topic_id" => topic.id.to_s,
+ "bannered_until" => bannered_until,
+ }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["bannered_until"]).to eq(Time.parse(bannered_until).utc.iso8601)
+ expect(topic.reload.bannered_until).to eq_time(Time.parse(bannered_until))
+ end
+
+ it "banners indefinitely when bannered_until is blank" do
+ config = {
+ "operation" => "add",
+ "pin_type" => "banner",
+ "topic_id" => topic.id.to_s,
+ "bannered_until" => "",
+ }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["banner"]).to eq(true)
+ expect(result["bannered_until"]).to be_nil
+ end
+
+ it "removes the banner" do
+ topic.make_banner!(Discourse.system_user)
+
+ config = { "operation" => "remove", "pin_type" => "banner", "topic_id" => topic.id.to_s }
+
+ result = execute_node(configuration: config, item: item)
+
+ expect(result["banner"]).to eq(false)
+ expect(topic.reload.archetype).to eq(Archetype.default)
+ end
+
+ it "does nothing when the topic is not a banner" do
+ config = { "operation" => "remove", "pin_type" => "banner", "topic_id" => topic.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.not_to change {
+ topic.posts.count
+ }
+ end
+ end
+
+ context "with permissions" do
+ it "raises when the actor cannot pin the topic" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "actor_username" => user.username,
+ }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /'#{user.username}' cannot pin or unpin topic #{topic.id}/,
+ )
+ expect(topic.reload.pinned_at).to be_nil
+ end
+
+ it "raises when the actor cannot pin the topic globally" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "pinned_globally" => true,
+ "actor_username" => user.username,
+ }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /cannot pin topic #{topic.id} globally/,
+ )
+ expect(topic.reload.pinned_globally).to eq(false)
+ end
+
+ it "allows a TL4 actor to pin globally" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "pinned_globally" => true,
+ "actor_username" => trust_level_4.username,
+ }
+
+ execute_node(configuration: config, item: item)
+
+ expect(topic.reload.pinned_globally).to eq(true)
+ end
+
+ it "raises when a non-staff actor banners the topic" do
+ config = {
+ "operation" => "add",
+ "pin_type" => "banner",
+ "topic_id" => topic.id.to_s,
+ "actor_username" => trust_level_4.username,
+ }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /can only be managed by staff/,
+ )
+ expect(topic.reload.archetype).to eq(Archetype.default)
+ end
+
+ it "reports the personal message reason when bannering a PM as staff" do
+ pm = Fabricate(:private_message_topic)
+
+ config = { "operation" => "add", "pin_type" => "banner", "topic_id" => pm.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "Personal messages cannot be made into banner topics.",
+ )
+ end
+
+ it "reports the restricted category reason when bannering as staff" do
+ private_category = Fabricate(:private_category, group: Fabricate(:group))
+ restricted = Fabricate(:topic, category: private_category)
+
+ config = { "operation" => "add", "pin_type" => "banner", "topic_id" => restricted.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "Topics in a restricted category cannot be made into banner topics.",
+ )
+ end
+
+ it "raises when pinning as the anonymous actor" do
+ config = {
+ "operation" => "add",
+ "topic_id" => topic.id.to_s,
+ "actor_username" => DiscourseWorkflows::AnonymousActor::USERNAME,
+ }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /'anonymous' cannot pin or unpin topic #{topic.id}/,
+ )
+ end
+ end
+
+ it "raises when the topic does not exist" do
+ config = { "operation" => "add", "topic_id" => "-1" }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ ActiveRecord::RecordNotFound,
+ )
+ end
+
+ it "raises on an unknown operation" do
+ config = { "operation" => "toggle", "topic_id" => topic.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "Unknown operation: toggle.",
+ )
+ end
+
+ it "raises on an unknown pin type" do
+ config = { "operation" => "add", "pin_type" => "sticky", "topic_id" => topic.id.to_s }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "Unknown pin type: sticky.",
+ )
+ end
+ end
+end