A platform for community discussion. Free, open, simple.
DEV: Share the topic type, tag and group inbox matchers between workflow nodes
Régis Hanolbbde73713361
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.rbindex 62481de92c8..640706b625a 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@@ -79,7 +79,8 @@ if defined?(DiscourseWorkflows)topic.category_id,category_ids_parameter(trigger_ctx),include_subcategories: trigger_ctx.get_node_parameter("include_subcategories", true),- ) && matches_tags?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))+ ) &&+ matches_tags?(topic, normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))endprivate@@ -91,10 +92,6 @@ if defined?(DiscourseWorkflows)def voter@voter ||= @vote&.userend-- def matches_tags?(tag_names)- tag_names.empty? || (topic.tags.pluck(:name) & tag_names).any?- endendendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rbindex ddfa7fbd589..ef634019b90 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/node_type.rb@@ -5,6 +5,8 @@ module DiscourseWorkflowsinclude NodeErrorHandlingextend NodeTypeDescriptor+ TOPIC_TYPE_OPTIONS = %w[all topics personal_messages].freeze+DESCRIPTION_DEFAULTS = {version: "1.0",defaults: {@@ -374,6 +376,35 @@ module DiscourseWorkflowsself.class.matches_category_ids?(topic_category_id, category_ids, include_subcategories:)end+ def matches_topic_type?(topic, topic_type)+ case topic_type.presence || "topics"+ when "all"+ true+ when "topics"+ !topic.private_message?+ when "personal_messages"+ topic.private_message?+ else+ false+ end+ end++ def matches_group_inbox?(topic, group_id)+ return true if group_id.blank?+ return false if !topic.private_message?++ topic.allowed_groups.exists?(id: group_id.to_i)+ end++ def matches_tags?(topic, tag_names)+ tag_names.empty? || (topic_tag_names(topic) & tag_names).any?+ end++ def topic_tag_names(topic)+ @topic_tag_names ||= {}+ @topic_tag_names[topic.id] ||= topic.tags.pluck(:name)+ end+def resolve_timezone(exec_ctx, item_index)timezone = exec_ctx.get_node_parameter("timezone", item_index, default: nil).to_s.presencereturn exec_ctx.get_timezone if timezone.nil?diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rbindex 343f39e3619..79462c15f15 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_created/v1.rb@@ -4,8 +4,6 @@ module DiscourseWorkflowsmodule Nodesmodule PostCreatedclass V1 < NodeType- TOPIC_TYPE_OPTIONS = %w[all topics personal_messages].freeze-description(name: "trigger:post_created",version: "1.0",@@ -132,34 +130,6 @@ module DiscourseWorkflowsdef user_data(user)serialize_user(user)end-- def matches_topic_type?(topic, topic_type)- case topic_type.presence || "topics"- when "all"- true- when "topics"- !topic.private_message?- when "personal_messages"- topic.private_message?- else- false- end- end-- def matches_group_inbox?(topic, group_id)- return true if group_id.blank?- return false if !topic.private_message?-- topic.allowed_groups.exists?(id: group_id.to_i)- end-- def matches_tags?(topic, tag_names)- tag_names.empty? || (topic_tag_names(topic) & tag_names).any?- end-- def topic_tag_names(topic)- @topic_tag_names ||= topic.tags.pluck(:name)- endendendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rbindex 3395d35f4d5..ea82baab94e 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rb@@ -94,8 +94,11 @@ module DiscourseWorkflows@post.topic.category_id,category_ids_parameter(trigger_ctx),include_subcategories: trigger_ctx.get_node_parameter("include_subcategories", true),- ) && matches_tags?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names"))) &&- matches_trust_level?(trigger_ctx.get_node_parameter("trust_levels"))+ ) &&+ matches_tags?(+ @post.topic,+ normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")),+ ) && matches_trust_level?(trigger_ctx.get_node_parameter("trust_levels"))endprivate@@ -115,14 +118,6 @@ module DiscourseWorkflowsendend- def matches_tags?(tag_names)- tag_names.empty? || (topic_tag_names & tag_names).any?- end-- def topic_tag_names- @topic_tag_names ||= @post.topic.tags.pluck(:name)- end-def matches_trust_level?(trust_levels)trust_levels =Array.wrap(trust_levels).filter_map { |trust_level| trust_level.presence&.to_i }diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_moved/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_moved/v1.rbindex e6215372740..b8ac4bac9e0 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_moved/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_moved/v1.rb@@ -84,7 +84,11 @@ module DiscourseWorkflowsdestination_topic.category_id,category_ids_parameter(trigger_ctx),include_subcategories: trigger_ctx.get_node_parameter("include_subcategories", true),- ) && matches_tags?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))+ ) &&+ matches_tags?(+ destination_topic,+ normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")),+ )endprivate@@ -100,14 +104,6 @@ module DiscourseWorkflowsdef original_topic@original_topic ||= ::Topic.find_by(id: @original_topic_id)end-- def matches_tags?(tag_names)- tag_names.empty? || (destination_topic_tag_names & tag_names).any?- end-- def destination_topic_tag_names- @destination_topic_tag_names ||= destination_topic.tags.pluck(:name)- endendendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rbindex c4b882eff23..c31452a3794 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_closed/v1.rb@@ -66,17 +66,8 @@ module DiscourseWorkflows@topic.category_id,category_ids_parameter(trigger_ctx),include_subcategories: trigger_ctx.get_node_parameter("include_subcategories", true),- ) && matches_tags?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))- end-- private-- def matches_tags?(tag_names)- tag_names.empty? || (topic_tag_names & tag_names).any?- end-- def topic_tag_names- @topic_tag_names ||= @topic.tags.pluck(:name)+ ) &&+ matches_tags?(@topic, normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))endendenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rbindex 6e3818b96b7..e307c47662d 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_created/v1.rb@@ -4,8 +4,6 @@ module DiscourseWorkflowsmodule Nodesmodule TopicCreatedclass V1 < NodeType- TOPIC_TYPE_OPTIONS = %w[all topics personal_messages].freeze-description(name: "trigger:topic_created",version: "1.0",@@ -103,13 +101,14 @@ module DiscourseWorkflowsenddef matches?(trigger_ctx)- matches_topic_type?(trigger_ctx.get_node_parameter("topic_type", "topics")) &&- matches_group_inbox?(trigger_ctx.get_node_parameter("group_inbox_id")) &&+ matches_topic_type?(@topic, trigger_ctx.get_node_parameter("topic_type", "topics")) &&+ matches_group_inbox?(@topic, trigger_ctx.get_node_parameter("group_inbox_id")) &&matches_category_ids?(@topic.category_id,category_ids_parameter(trigger_ctx),include_subcategories: trigger_ctx.get_node_parameter("include_subcategories", true),- ) && matches_tags?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))+ ) &&+ matches_tags?(@topic, normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))endprivate@@ -117,34 +116,6 @@ module DiscourseWorkflowsdef post_data(post)serialize_post(post)end-- def matches_topic_type?(topic_type)- case topic_type.presence || "topics"- when "all"- true- when "topics"- [email protected]_message?- when "personal_messages"- @topic.private_message?- else- false- end- end-- def matches_group_inbox?(group_id)- return true if group_id.blank?- return false if [email protected]_message?-- @topic.allowed_groups.exists?(id: group_id.to_i)- end-- def matches_tags?(tag_names)- tag_names.empty? || (topic_tag_names & tag_names).any?- end-- def topic_tag_names- @topic_tag_names ||= @topic.tags.pluck(:name)- endendendend