Skip to content

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

FEATURE: limit a post menu trigger to a post number (#42486)

Joffrey JAFFEUX8b4543a194ef
diff --git a/frontend/discourse/app/form-kit/lib/validator.js b/frontend/discourse/app/form-kit/lib/validator.js
index 281cb028496..b75251ad69d 100644
--- a/frontend/discourse/app/form-kit/lib/validator.js
+++ b/frontend/discourse/app/form-kit/lib/validator.js
@@ -78,6 +78,10 @@ export default class Validator {
}
betweenValidator(value, rule) {
+ if (isBlank(value)) {
+ return;
+ }
+
if (rule.max) {
if (value > rule.max) {
return i18n("form_kit.errors.too_high", {
diff --git a/frontend/discourse/tests/unit/lib/form-kit/validator-test.js b/frontend/discourse/tests/unit/lib/form-kit/validator-test.js
index 463558bc4bb..5abd97fd810 100644
--- a/frontend/discourse/tests/unit/lib/form-kit/validator-test.js
+++ b/frontend/discourse/tests/unit/lib/form-kit/validator-test.js
@@ -99,6 +99,20 @@ module("Unit | Lib | FormKit | Validator", function (hooks) {
[],
"it returns no errors when the value is valid"
);
+
+ errors = await new Validator("", {
+ between: { min: 1, max: 5 },
+ }).validate();
+ assert.deepEqual(
+ errors,
+ [],
+ "it returns no errors when the value is an empty string"
+ );
+
+ errors = await new Validator(null, {
+ between: { min: 1, max: 5 },
+ }).validate();
+ assert.deepEqual(errors, [], "it returns no errors when the value is null");
});
test("integer", async function (assert) {
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
index c19986a20dd..5b471f33d6d 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/field.gjs
@@ -10,6 +10,7 @@ import {
fieldShowDescription,
fieldShowLabel,
fieldSupportsExpression,
+ fieldType,
findNodeType,
isExpression,
propertyDescription,
@@ -119,6 +120,9 @@ export default class Field extends Component {
if (schema.required) {
rules.push("required");
}
+ if (fieldType(schema) === "integer" && !this.supportsExpression) {
+ rules.push("integer");
+ }
if (schema.min != null || schema.max != null) {
const min = schema.min ?? Number.MIN_SAFE_INTEGER;
const max = schema.max ?? Number.MAX_SAFE_INTEGER;
diff --git a/plugins/discourse-workflows/app/controllers/discourse_workflows/post_button_controller.rb b/plugins/discourse-workflows/app/controllers/discourse_workflows/post_button_controller.rb
index 955f0e7c9fb..d9aa06cd1fc 100644
--- a/plugins/discourse-workflows/app/controllers/discourse_workflows/post_button_controller.rb
+++ b/plugins/discourse-workflows/app/controllers/discourse_workflows/post_button_controller.rb
@@ -20,6 +20,7 @@ module DiscourseWorkflows
on_model_not_found(:post) { raise Discourse::NotFound }
on_failed_policy(:can_use_post_button) { raise Discourse::InvalidAccess }
on_failed_policy(:can_see_post) { raise Discourse::NotFound }
+ on_failed_policy(:can_trigger_for_post) { raise Discourse::InvalidAccess }
end
end
end
diff --git a/plugins/discourse-workflows/app/models/discourse_workflows/workflow_dependency.rb b/plugins/discourse-workflows/app/models/discourse_workflows/workflow_dependency.rb
index 0b5f723b695..da059ba82c9 100644
--- a/plugins/discourse-workflows/app/models/discourse_workflows/workflow_dependency.rb
+++ b/plugins/discourse-workflows/app/models/discourse_workflows/workflow_dependency.rb
@@ -53,6 +53,7 @@ module DiscourseWorkflows
workflow_id: published_trigger.workflow_id,
label: parameters["label"],
icon: parameters["icon"],
+ post_number: DiscourseWorkflows::Nodes::PostButton::V1.resolved_post_number(parameters),
position: DiscourseWorkflows::Nodes::PostButton::V1.resolved_position(parameters),
confirmation: parameters["confirmation"] == true,
confirmation_message: parameters["confirmation_message"].presence,
diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/workflow/trigger_post_button.rb b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/trigger_post_button.rb
index 0d05d3c062f..532efbc8d00 100644
--- a/plugins/discourse-workflows/app/services/discourse_workflows/workflow/trigger_post_button.rb
+++ b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/trigger_post_button.rb
@@ -5,6 +5,7 @@ module DiscourseWorkflows
include Service::Base
params do
+ attribute :workflow_id, :integer
attribute :trigger_node_id, :string
attribute :post_id, :integer
@@ -16,17 +17,27 @@ module DiscourseWorkflows
policy :can_use_post_button
model :post
policy :can_see_post
+ policy :can_trigger_for_post
step :enqueue_workflow
private
def fetch_published_trigger(params:)
- DiscourseWorkflows::Workflow::Action::FindPublishedTriggers.call(
- trigger_type: "trigger:post_button",
- filter: ->(published_trigger) do
- published_trigger.trigger_node_id == params.trigger_node_id
- end,
- ).first
+ matches =
+ DiscourseWorkflows::Workflow::Action::FindPublishedTriggers.call(
+ trigger_type: "trigger:post_button",
+ filter: ->(published_trigger) do
+ published_trigger.trigger_node_id == params.trigger_node_id
+ end,
+ )
+
+ if params.workflow_id
+ return(
+ matches.find { |published_trigger| published_trigger.workflow_id == params.workflow_id }
+ )
+ end
+
+ matches.one? ? matches.first : nil
end
def can_use_post_button(published_trigger:, guardian:)
@@ -44,6 +55,13 @@ module DiscourseWorkflows
guardian.can_see?(post)
end
+ def can_trigger_for_post(published_trigger:, post:)
+ DiscourseWorkflows::Nodes::PostButton::V1.matches_post_number?(
+ post,
+ NodeData.parameters(published_trigger.trigger_node),
+ )
+ end
+
def enqueue_workflow(published_trigger:, post:, guardian:)
trigger = DiscourseWorkflows::Nodes::PostButton::V1.new(post)
DiscourseWorkflows::TriggerDispatcher.enqueue(
diff --git a/plugins/discourse-workflows/assets/javascripts/discourse/initializers/post-button-workflows.gjs b/plugins/discourse-workflows/assets/javascripts/discourse/initializers/post-button-workflows.gjs
index 7bfdf00c5d7..4bde983826a 100644
--- a/plugins/discourse-workflows/assets/javascripts/discourse/initializers/post-button-workflows.gjs
+++ b/plugins/discourse-workflows/assets/javascripts/discourse/initializers/post-button-workflows.gjs
@@ -94,6 +94,7 @@ function buildButtonComponent(workflow) {
await ajax("/discourse-workflows/trigger-post-button", {
type: "POST",
data: {
+ workflow_id: workflow.workflow_id,
trigger_node_id: workflow.trigger_node_id,
post_id: this.args.post.id,
},
@@ -152,6 +153,13 @@ export default {
"post-menu-buttons",
({ value: dag, context }) => {
buttons.forEach(({ key, workflow, component }) => {
+ if (
+ workflow.post_number != null &&
+ workflow.post_number !== context.post.post_number
+ ) {
+ return;
+ }
+
dag.add(
key,
component,
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 6a7961247f2..13f0cb25261 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -337,6 +337,9 @@ en:
label_placeholder: "Run workflow"
icon: "Button icon"
group_ids_description: "Only members of at least one of these groups can see and use the button"
+ post_number: "Post number"
+ post_number_description: "Show the button only on this post number in each topic. Leave empty to show it on every post."
+ post_number_placeholder: "Any post"
position: "Position"
position_first: "First in the row"
position_last: "Last in the row"
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_button/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_button/v1.rb
index 23088a7f62d..b573422da34 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_button/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_button/v1.rb
@@ -4,6 +4,7 @@ module DiscourseWorkflows
module Nodes
module PostButton
class V1 < NodeType
+ INVALID_POST_NUMBER = 0
POSITION_OPTIONS = %w[first last more_menu relative].freeze
POSITION_DIRECTION_OPTIONS = %w[before after].freeze
POSITION_ANCHOR_OPTIONS = %w[
@@ -52,6 +53,12 @@ module DiscourseWorkflows
filterable: true,
},
},
+ post_number: {
+ type: :integer,
+ required: false,
+ min: 1,
+ no_data_expression: true,
+ },
position: {
type: :options,
required: false,
@@ -141,6 +148,25 @@ module DiscourseWorkflows
group_ids.present? && user.in_any_groups?(group_ids)
end
+ def self.normalized_post_number(parameters)
+ value = parameters["post_number"].to_s
+ value.to_i if value.match?(/\A[1-9]\d*\z/)
+ end
+
+ def self.resolved_post_number(parameters)
+ value = parameters["post_number"]
+ return if value.to_s.strip.empty?
+
+ normalized_post_number(parameters) || INVALID_POST_NUMBER
+ end
+
+ def self.matches_post_number?(post, parameters)
+ post_number = resolved_post_number(parameters)
+ return true if post_number.nil?
+
+ post.present? && post.post_number == post_number
+ end
+
def self.resolved_position(parameters)
position = parameters["position"].presence || "last"
return position if position != "relative"
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_button_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_button_spec.rb
index e6840f50458..e53d0d59e55 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_button_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_button_spec.rb
@@ -66,6 +66,83 @@ RSpec.describe DiscourseWorkflows::Nodes::PostButton::V1 do
end
end
+ describe ".normalized_post_number" do
+ it "normalizes positive integer values" do
+ expect(described_class.normalized_post_number("post_number" => post.post_number)).to eq(
+ post.post_number,
+ )
+ expect(described_class.normalized_post_number("post_number" => post.post_number.to_s)).to eq(
+ post.post_number,
+ )
+ end
+
+ it "returns nil for missing, blank, malformed, and nonpositive values" do
+ [
+ {},
+ { "post_number" => "" },
+ { "post_number" => "invalid" },
+ { "post_number" => 0 },
+ { "post_number" => -1 },
+ ].each { |parameters| expect(described_class.normalized_post_number(parameters)).to be_nil }
+ end
+ end
+
+ describe ".resolved_post_number" do
+ it "returns nil when the parameter is missing or blank" do
+ [
+ {},
+ { "post_number" => nil },
+ { "post_number" => "" },
+ { "post_number" => " " },
+ ].each { |parameters| expect(described_class.resolved_post_number(parameters)).to be_nil }
+ end
+
+ it "returns an integer for numeric values" do
+ expect(described_class.resolved_post_number("post_number" => post.post_number)).to eq(
+ post.post_number,
+ )
+ expect(described_class.resolved_post_number("post_number" => post.post_number.to_s)).to eq(
+ post.post_number,
+ )
+ end
+
+ it "returns the invalid sentinel for malformed and nonpositive values" do
+ ["invalid", "1.5", 0, -1].each do |post_number|
+ expect(described_class.resolved_post_number("post_number" => post_number)).to eq(
+ described_class::INVALID_POST_NUMBER,
+ )
+ end
+ end
+ end
+
+ describe ".matches_post_number?" do
+ it "matches any post when the parameter is missing or blank" do
+ expect(described_class.matches_post_number?(post, {})).to eq(true)
+ expect(described_class.matches_post_number?(post, "post_number" => "")).to eq(true)
+ expect(described_class.matches_post_number?(post, "post_number" => " ")).to eq(true)
+ end
+
+ it "matches positive integer and numeric string values" do
+ expect(described_class.matches_post_number?(post, "post_number" => post.post_number)).to eq(
+ true,
+ )
+ expect(
+ described_class.matches_post_number?(post, "post_number" => post.post_number.to_s),
+ ).to eq(true)
+ expect(
+ described_class.matches_post_number?(post, "post_number" => post.post_number + 1),
+ ).to eq(false)
+ end
+
+ it "rejects malformed and nonpositive nonblank values" do
+ ["invalid", "1.5", 0, -1].each do |post_number|
+ expect(described_class.matches_post_number?(post, "post_number" => post_number)).to eq(
+ false,
+ )
+ end
+ end
+ end
+
describe ".resolved_position" do
it "defaults to last" do
expect(described_class.resolved_position({})).to eq("last")
diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/post_button_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/post_button_controller_spec.rb
index 16e4a330045..6852fdfb32d 100644
--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/post_button_controller_spec.rb
+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/post_button_controller_spec.rb
@@ -24,6 +24,7 @@ RSpec.describe DiscourseWorkflows::PostButtonController do
it "requires authentication" do
post "/discourse-workflows/trigger-post-button.json",
params: {
+ workflow_id: workflow.id,
trigger_node_id: "trigger-1",
post_id: post_record.id,
}
@@ -36,6 +37,7 @@ RSpec.describe DiscourseWorkflows::PostButtonController do
post "/discourse-workflows/trigger-post-button.json",
params: {
+ workflow_id: workflow.id,
trigger_node_id: "trigger-1",
post_id: post_record.id,
}
@@ -49,6 +51,7 @@ RSpec.describe DiscourseWorkflows::PostButtonController do
it "returns 204 on success" do
post "/discourse-workflows/trigger-post-button.json",
params: {
+ workflow_id: workflow.id,
trigger_node_id: "trigger-1",
post_id: post_record.id,
}
@@ -59,6 +62,7 @@ RSpec.describe DiscourseWorkflows::PostButtonController do
it "enqueues an ExecuteWorkflow job" do
post "/discourse-workflows/trigger-post-button.json",
params: {
+ workflow_id: workflow.id,
trigger_node_id: "trigger-1",
post_id: post_record.id,
}
@@ -80,6 +84,7 @@ RSpec.describe DiscourseWorkflows::PostButtonController do
it "returns 404 when trigger node does not exist" do
post "/discourse-workflows/trigger-post-button.json",
params: {
+ workflow_id: workflow.id,
trigger_node_id: "nonexistent",
post_id: post_record.id,
}
@@ -92,6 +97,7 @@ RSpec.describe DiscourseWorkflows::PostButtonController do
post "/discourse-workflows/trigger-post-button.json",
params: {
+ workflow_id: workflow.id,
trigger_node_id: "trigger-1",
post_id: post_record.id,
}
@@ -105,12 +111,37 @@ RSpec.describe DiscourseWorkflows::PostButtonController do
post "/discourse-workflows/trigger-post-button.json",
params: {
+ workflow_id: workflow.id,
trigger_node_id: "trigger-1",
post_id: private_post.id,
}
expect(response).to have_http_status(:not_found)
end
+
+ it "returns 403 without enqueuing when the configured post number does not match" do
+ update_workflow_node(workflow, "trigger-1") do |node|
+ node.merge(
+ "parameters" => {
+ "label" => "Run workflow",
+ "icon" => "bolt",
+ "group_ids" => [group.id],
+ "post_number" => post_record.post_number + 1,
+ },
+ )
+ end
+ publish_workflow!(workflow)
+
+ expect do
+ post "/discourse-workflows/trigger-post-button.json",
+ params: {
+ workflow_id: workflow.id,
+ trigger_node_id: "trigger-1",
+ post_id: post_record.id,
+ }
+ end.not_to change { Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.size }
+ expect(response).to have_http_status(:forbidden)
+ end
end
end
end
diff --git a/plugins/discourse-workflows/spec/serializers/site_serializer_spec.rb b/plugins/discourse-workflows/spec/serializers/site_serializer_spec.rb
index 66f777f279b..58321a56a3f 100644
--- a/plugins/discourse-workflows/spec/serializers/site_serializer_spec.rb
+++ b/plugins/discourse-workflows/spec/serializers/site_serializer_spec.rb
@@ -101,6 +101,7 @@ RSpec.describe SiteSerializer do
workflow_id: post_button_workflow.id,
label: "Run workflow",
icon: "bolt",
+ post_number: nil,
position: "last",
confirmation: false,
confirmation_message: nil,
@@ -152,6 +153,7 @@ RSpec.describe SiteSerializer do
"parameters" => {
"label" => "Run workflow",
"group_ids" => [Group::AUTO_GROUPS[:logged_in_users]],
+ "post_number" => "2",
"position" => "more_menu",
"confirmation" => true,
"confirmation_message" => "Really run this?",
@@ -169,11 +171,78 @@ RSpec.describe SiteSerializer do
workflow_id: post_button_workflow.id,
label: "Run workflow",
icon: nil,
+ post_number: 2,
position: "more_menu",
confirmation: true,
confirmation_message: "Really run this?",
},
)
end
+
+ it "serializes numeric post numbers as integers without leaking group ids" do
+ update_workflow_node(post_button_workflow, "trigger-1") do |node|
+ node.merge(
+ "parameters" => {
+ "label" => "Run workflow",
+ "group_ids" => [group.id],
+ "post_number" => 3,
+ },
+ )
+ end
+ publish_workflow!(post_button_workflow)
+
+ member_guardian = Guardian.new(member)
+ data =
+ described_class.new(Site.new(member_guardian), scope: member_guardian, root: false).as_json
+
+ expect(data[:post_button_workflows]).to contain_exactly(
+ {
+ trigger_node_id: "trigger-1",
+ workflow_id: post_button_workflow.id,
+ label: "Run workflow",
+ icon: nil,
+ post_number: 3,
+ position: "last",
+ confirmation: false,
+ confirmation_message: nil,
+ },
+ )
+ end
+
+ it "serializes malformed and nonpositive post numbers to the invalid sentinel" do
+ ["invalid", 0].each do |post_number|
+ update_workflow_node(post_button_workflow, "trigger-1") do |node|
+ node.merge(
+ "parameters" => {
+ "label" => "Run workflow",
+ "group_ids" => [group.id],
+ "post_number" => post_number,
+ },
+ )
+ end
+ publish_workflow!(post_button_workflow)
+
+ member_guardian = Guardian.new(member)
+ data =
+ described_class.new(
+ Site.new(member_guardian),
+ scope: member_guardian,
+ root: false,
+ ).as_json
+
+ expect(data[:post_button_workflows]).to contain_exactly(
+ {
+ trigger_node_id: "trigger-1",
+ workflow_id: post_button_workflow.id,
+ label: "Run workflow",
+ icon: nil,
+ post_number: DiscourseWorkflows::Nodes::PostButton::V1::INVALID_POST_NUMBER,
+ position: "last",
+ confirmation: false,
+ confirmation_message: nil,
+ },
+ )
+ end
+ end
end
end
diff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/trigger_post_button_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/trigger_post_button_spec.rb
index 3fdc804df49..4c2df35680b 100644
--- a/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/trigger_post_button_spec.rb
+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/trigger_post_button_spec.rb
@@ -27,7 +27,9 @@ RSpec.describe DiscourseWorkflows::Workflow::TriggerPostButton do
Fabricate(:discourse_workflows_workflow, created_by: admin, published: true, **graph)
end
- let(:params) { { trigger_node_id: "trigger-1", post_id: post_record.id } }
+ let(:params) do
+ { workflow_id: workflow.id, trigger_node_id: "trigger-1", post_id: post_record.id }
+ end
let(:dependencies) { { guardian: Guardian.new(member) } }
before { DiscourseWorkflows::WorkflowDependencyIndexer.call(workflow) }
@@ -39,7 +41,9 @@ RSpec.describe DiscourseWorkflows::Workflow::TriggerPostButton do
end
context "when workflow is not found" do
- let(:params) { { trigger_node_id: "nonexistent", post_id: post_record.id } }
+ let(:params) do
+ { workflow_id: workflow.id, trigger_node_id: "nonexistent", post_id: post_record.id }
+ end
it { is_expected.to fail_to_find_a_model(:published_trigger) }
end
@@ -50,6 +54,54 @@ RSpec.describe DiscourseWorkflows::Workflow::TriggerPostButton do
it { is_expected.to fail_to_find_a_model(:published_trigger) }
end
+ context "when published workflows share a trigger node id" do
+ fab!(:other_workflow) do
+ graph =
+ build_workflow_graph do |g|
+ g.node "trigger-1",
+ "trigger:post_button",
+ configuration: {
+ "label" => "Run other workflow",
+ "group_ids" => [group.id],
+ }
+ end
+ Fabricate(:discourse_workflows_workflow, created_by: admin, published: true, **graph)
+ end
+
+ before { DiscourseWorkflows::WorkflowDependencyIndexer.call(other_workflow) }
+
+ it "enqueues each specifically requested workflow" do
+ service_results = []
+
+ expect do
+ [workflow, other_workflow].each do |requested_workflow|
+ service_results << described_class.call(
+ params: params.merge(workflow_id: requested_workflow.id),
+ **dependencies,
+ )
+ end
+ end.to change { Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.size }.by(2)
+
+ service_results.each { |service_result| expect(service_result).to run_successfully }
+ expect(
+ Jobs::DiscourseWorkflows::ExecuteWorkflow
+ .jobs
+ .last(2)
+ .map { |job| job["args"].first["workflow_id"] },
+ ).to eq([workflow.id, other_workflow.id])
+ end
+
+ it "fails to find a trigger when a legacy request is ambiguous" do
+ legacy_params = params.except(:workflow_id)
+ legacy_result = nil
+
+ expect do
+ legacy_result = described_class.call(params: legacy_params, **dependencies)
+ end.not_to change { Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.size }
+ expect(legacy_result).to fail_to_find_a_model(:published_trigger)
+ end
+ end
+
context "when user is in none of the configured groups" do
fab!(:acting_user, :user)
let(:dependencies) { { guardian: Guardian.new(acting_user) } }
@@ -69,7 +121,7 @@ RSpec.describe DiscourseWorkflows::Workflow::TriggerPostButton do
end
context "when post does not exist" do
- let(:params) { { trigger_node_id: "trigger-1", post_id: -1 } }
+ let(:params) { { workflow_id: workflow.id, trigger_node_id: "trigger-1", post_id: -1 } }
it { is_expected.to fail_to_find_a_model(:post) }
end
@@ -80,12 +132,35 @@ RSpec.describe DiscourseWorkflows::Workflow::TriggerPostButton do
Fabricate(:post, topic: Fabricate(:topic, category: category))
end
- let(:params) { { trigger_node_id: "trigger-1", post_id: private_post.id } }
+ let(:params) do
+ { workflow_id: workflow.id, trigger_node_id: "trigger-1", post_id: private_post.id }
+ end
it { is_expected.to fail_a_policy(:can_see_post) }
end
- context "when everything is valid" do
+ context "when the configured post number does not match" do
+ before do
+ update_workflow_node(workflow, "trigger-1") do |node|
+ node.merge(
+ "parameters" => {
+ "label" => "Run workflow",
+ "icon" => "bolt",
+ "group_ids" => [group.id],
+ "post_number" => post_record.post_number + 1,
+ },
+ )
+ end
+ publish_workflow!(workflow)
+ end
+
+ it "fails the post-number policy without enqueuing a workflow" do
+ expect { result }.not_to change { Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.size }
+ expect(result).to fail_a_policy(:can_trigger_for_post)
+ end
+ end
+
+ context "when the post number is omitted" do
it { is_expected.to run_successfully }
it "enqueues an ExecuteWorkflow job acting as the clicking user" do
@@ -104,5 +179,29 @@ RSpec.describe DiscourseWorkflows::Workflow::TriggerPostButton do
expect(job["args"].first["trigger_data"]["topic"]).to include("id" => post_record.topic_id)
end
end
+
+ context "when a legacy request omits the workflow id" do
+ let(:params) { { trigger_node_id: "trigger-1", post_id: post_record.id } }
+
+ it { is_expected.to run_successfully }
+ end
+
+ context "when the configured post number matches" do
+ before do
+ update_workflow_node(workflow, "trigger-1") do |node|
+ node.merge(
+ "parameters" => {
+ "label" => "Run workflow",
+ "icon" => "bolt",
+ "group_ids" => [group.id],
+ "post_number" => post_record.post_number.to_s,
+ },
+ )
+ end
+ publish_workflow!(workflow)
+ end
+
+ it { is_expected.to run_successfully }
+ end
end
end
diff --git a/plugins/discourse-workflows/test/javascripts/acceptance/post-button-workflows-test.js b/plugins/discourse-workflows/test/javascripts/acceptance/post-button-workflows-test.js
index 7c2fc8240ef..154b42be001 100644
--- a/plugins/discourse-workflows/test/javascripts/acceptance/post-button-workflows-test.js
+++ b/plugins/discourse-workflows/test/javascripts/acceptance/post-button-workflows-test.js
@@ -51,6 +51,26 @@ acceptance("Discourse Workflows | Post button", function (needs) {
confirmation: false,
confirmation_message: null,
},
+ {
+ trigger_node_id: "trigger-1",
+ workflow_id: 6,
+ label: "Only post two",
+ icon: null,
+ post_number: 2,
+ position: "last",
+ confirmation: false,
+ confirmation_message: null,
+ },
+ {
+ trigger_node_id: "trigger-1",
+ workflow_id: 7,
+ label: "Any post",
+ icon: null,
+ post_number: null,
+ position: "last",
+ confirmation: false,
+ confirmation_message: null,
+ },
],
});
@@ -78,6 +98,11 @@ acceptance("Discourse Workflows | Post button", function (needs) {
await click("#post_1 .post-controls .workflow-post-button-1");
const params = new URLSearchParams(lastRequestBody);
+ assert.strictEqual(
+ params.get("workflow_id"),
+ "1",
+ "submits the workflow id"
+ );
assert.strictEqual(
params.get("trigger_node_id"),
"trigger-1",
@@ -120,6 +145,30 @@ acceptance("Discourse Workflows | Post button", function (needs) {
.exists("the button falls back gracefully when its anchor is absent");
});
+ test("filters workflow buttons by post number", async function (assert) {
+ await visit("/t/internationalization-localization/280");
+
+ assert
+ .dom("#post_1 .post-controls .workflow-post-button-6")
+ .doesNotExist("the restricted workflow is absent from post one");
+ assert
+ .dom("#post_2 .post-controls .workflow-post-button-6")
+ .exists("the restricted workflow renders on post two");
+
+ assert
+ .dom("#post_1 .post-controls .workflow-post-button-1")
+ .exists("a workflow without a post number renders on post one");
+ assert
+ .dom("#post_2 .post-controls .workflow-post-button-1")
+ .exists("a workflow without a post number renders on post two");
+ assert
+ .dom("#post_1 .post-controls .workflow-post-button-7")
+ .exists("a workflow with a null post number renders on post one");
+ assert
+ .dom("#post_2 .post-controls .workflow-post-button-7")
+ .exists("a workflow with a null post number renders on post two");
+ });
+
test("show-more buttons render in the show-more menu", async function (assert) {
await visit("/t/internationalization-localization/280");
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 f0bea376f35..006f74440f1 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
@@ -73,6 +73,111 @@ module("Integration | Component | workflows property engine", function (hooks) {
assert.dom("input").isFocused();
});
+ test("validates optional integer fields that do not support expressions", async function (assert) {
+ this.setProperties({
+ configuration: { post_number: "" },
+ formApi: null,
+ nodeType: "trigger:post_button",
+ schema: {
+ post_number: {
+ type: "integer",
+ required: false,
+ min: 1,
+ no_data_expression: true,
+ },
+ },
+ 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>
+ );
+
+ await this.formApi.submit();
+ assert
+ .form()
+ .field("post_number")
+ .hasNoErrors("the optional field is valid while blank");
+
+ await fillIn("input[name='post_number']", "1.5");
+ await this.formApi.submit();
+ assert
+ .form()
+ .field("post_number")
+ .hasError(
+ i18n("form_kit.errors.not_an_integer"),
+ "a fractional value shows the integer validation error"
+ );
+
+ await fillIn("input[name='post_number']", "2");
+ await this.formApi.submit();
+ assert
+ .form()
+ .field("post_number")
+ .hasNoErrors("an integer value clears the validation error");
+ });
+
+ test("expression-enabled integer fields accept expressions", async function (assert) {
+ this.setProperties({
+ configuration: { post_number: "={{ $json.post_number }}" },
+ formApi: null,
+ nodeType: "trigger:post_button",
+ schema: {
+ post_number: {
+ type: "integer",
+ min: 1,
+ },
+ },
+ 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>
+ );
+
+ assert.dom(".workflows-variable-input").includesText("post_number");
+
+ await this.formApi.submit();
+ assert
+ .form()
+ .field("post_number")
+ .hasNoErrors("the expression is not treated as a literal integer");
+ });
+
test("renders an inline description and a tooltip independently", async function (assert) {
I18n.translations[I18n.locale].js.discourse_workflows.post.raw_tooltip =
"Only visible to the agent";