Skip to content

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

FEATURE: Let reaction, solution and vote workflow triggers filter by topic

Régis Hanolf375bf87c9e8
diff --git a/plugins/discourse-reactions/lib/discourse_workflows/nodes/post_reaction_changed/v1.rb b/plugins/discourse-reactions/lib/discourse_workflows/nodes/post_reaction_changed/v1.rb
index 065f0d9d37c..e9b93d57433 100644
--- a/plugins/discourse-reactions/lib/discourse_workflows/nodes/post_reaction_changed/v1.rb
+++ b/plugins/discourse-reactions/lib/discourse_workflows/nodes/post_reaction_changed/v1.rb
@@ -50,8 +50,7 @@ if defined?(DiscourseWorkflows)
required: false,
no_data_expression: true,
},
- **CATEGORY_FILTER_PROPERTIES,
- **TAG_FILTER_PROPERTIES,
+ **TOPIC_SCOPE_FILTER_PROPERTIES,
},
)
@@ -84,9 +83,10 @@ if defined?(DiscourseWorkflows)
def matches?(trigger_ctx)
filter = trigger_ctx.get_node_parameter("reaction").to_s.delete(":").presence
- matches_changes?(trigger_ctx, change) &&
- (filter.nil? || [reaction, @previous_reaction].include?(filter)) &&
- matches_topic_filters?(@post.topic, trigger_ctx)
+ return false if !matches_changes?(trigger_ctx, change)
+ return false if filter && [reaction, @previous_reaction].exclude?(filter)
+
+ matches_topic_filters?(@post.topic, trigger_ctx)
end
private
diff --git a/plugins/discourse-reactions/spec/lib/discourse_workflows/nodes/post_reaction_changed_spec.rb b/plugins/discourse-reactions/spec/lib/discourse_workflows/nodes/post_reaction_changed_spec.rb
index 46bb5002155..801cd950125 100644
--- a/plugins/discourse-reactions/spec/lib/discourse_workflows/nodes/post_reaction_changed_spec.rb
+++ b/plugins/discourse-reactions/spec/lib/discourse_workflows/nodes/post_reaction_changed_spec.rb
@@ -19,7 +19,7 @@ RSpec.describe DiscourseWorkflows::Nodes::PostReactionChanged::V1, discourse_wor
end
describe "#matches?" do
- it "filters changes, reactions, categories, and tags" do
+ it "filters changes, reactions, categories, tags, and topics" do
SiteSetting.tagging_enabled = true
topic.tags << tag
react("hugs")
@@ -30,10 +30,12 @@ RSpec.describe DiscourseWorkflows::Nodes::PostReactionChanged::V1, discourse_wor
expect(trigger.matches?(trigger_context(reaction: "clap"))).to eq(false)
expect(trigger.matches?(trigger_context(category_ids: [other_category.id]))).to eq(false)
expect(trigger.matches?(trigger_context(tag_names: ["missing"]))).to eq(false)
+ expect(trigger.matches?(trigger_context(topic_ids: [topic.id + 1]))).to eq(false)
expect(
trigger.matches?(
trigger_context(
changes: ["replaced"],
+ topic_ids: [topic.id],
reaction: ":laughing:",
category_ids: [category.id],
tag_names: [tag.name],
diff --git a/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rb b/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rb
index 92f73063f07..7a8bd0684d0 100644
--- a/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rb
+++ b/plugins/discourse-solved/lib/discourse_workflows/nodes/solution_changed/v1.rb
@@ -38,8 +38,7 @@ if defined?(DiscourseWorkflows)
default: [],
options: CHANGES.values,
},
- **CATEGORY_FILTER_PROPERTIES,
- **TAG_FILTER_PROPERTIES,
+ **TOPIC_SCOPE_FILTER_PROPERTIES,
},
)
diff --git a/plugins/discourse-solved/spec/lib/discourse_workflows/nodes/solution_changed_spec.rb b/plugins/discourse-solved/spec/lib/discourse_workflows/nodes/solution_changed_spec.rb
index 8bc604a3198..e6f5a33634f 100644
--- a/plugins/discourse-solved/spec/lib/discourse_workflows/nodes/solution_changed_spec.rb
+++ b/plugins/discourse-solved/spec/lib/discourse_workflows/nodes/solution_changed_spec.rb
@@ -15,7 +15,7 @@ RSpec.describe DiscourseWorkflows::Nodes::SolutionChanged::V1, discourse_workflo
end
describe "#matches?" do
- it "filters changes, categories, and tags" do
+ it "filters changes, categories, tags, and topics" do
post.topic.tags << tag
trigger = described_class.from_event(:accepted_solution, post)
@@ -23,10 +23,12 @@ RSpec.describe DiscourseWorkflows::Nodes::SolutionChanged::V1, discourse_workflo
expect(trigger.matches?(trigger_context(changes: ["removed"]))).to eq(false)
expect(trigger.matches?(trigger_context(category_ids: [other_category.id]))).to eq(false)
expect(trigger.matches?(trigger_context(tag_names: ["missing"]))).to eq(false)
+ expect(trigger.matches?(trigger_context(topic_ids: [topic.id + 1]))).to eq(false)
expect(
trigger.matches?(
trigger_context(
changes: ["accepted"],
+ topic_ids: [topic.id],
category_ids: [category.id],
tag_names: [tag.name],
),
diff --git a/plugins/discourse-topic-voting/lib/discourse_workflows/nodes/topic_received_vote/v1.rb b/plugins/discourse-topic-voting/lib/discourse_workflows/nodes/topic_received_vote/v1.rb
index 27da3b8c4bd..ac92748eb39 100644
--- a/plugins/discourse-topic-voting/lib/discourse_workflows/nodes/topic_received_vote/v1.rb
+++ b/plugins/discourse-topic-voting/lib/discourse_workflows/nodes/topic_received_vote/v1.rb
@@ -22,8 +22,7 @@ if defined?(DiscourseWorkflows)
},
],
properties: {
- **CATEGORY_FILTER_PROPERTIES,
- **TAG_FILTER_PROPERTIES,
+ **TOPIC_SCOPE_FILTER_PROPERTIES,
},
)
@@ -49,12 +48,7 @@ if defined?(DiscourseWorkflows)
end
def matches?(trigger_ctx)
- matches_category_ids?(
- topic.category_id,
- category_ids_parameter(trigger_ctx),
- include_subcategories: trigger_ctx.get_node_parameter("include_subcategories", true),
- ) &&
- matches_tags?(topic, normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))
+ matches_topic_filters?(topic, trigger_ctx)
end
private
diff --git a/plugins/discourse-topic-voting/spec/lib/discourse_workflows/nodes/topic_received_vote/v1_spec.rb b/plugins/discourse-topic-voting/spec/lib/discourse_workflows/nodes/topic_received_vote/v1_spec.rb
index 63d7052b7a3..a67d643e6dc 100644
--- a/plugins/discourse-topic-voting/spec/lib/discourse_workflows/nodes/topic_received_vote/v1_spec.rb
+++ b/plugins/discourse-topic-voting/spec/lib/discourse_workflows/nodes/topic_received_vote/v1_spec.rb
@@ -45,6 +45,12 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicReceivedVote::V1 do
expect(described_class.new(vote).matches?(trigger_context({}))).to eq(true)
end
+ it "matches only votes on the selected topics" do
+ expect(
+ described_class.new(vote).matches?(trigger_context("topic_ids" => [topic.id + 1])),
+ ).to eq(false)
+ end
+
it "matches only votes on topics in the selected categories" do
other_category = Fabricate(:category)
@@ -79,8 +85,4 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicReceivedVote::V1 do
)
end
end
-
- def trigger_context(parameters)
- DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters })
- end
end