A platform for community discussion. Free, open, simple.
FEATURE: adds a topic received vote trigger to workflows (#42086)
Joffrey JAFFEUX46baf1c080fa
diff --git a/plugins/discourse-topic-voting/app/services/discourse_topic_voting/votes/cast.rb b/plugins/discourse-topic-voting/app/services/discourse_topic_voting/votes/cast.rbindex 55288e3483e..e4f09287d9e 100644--- a/plugins/discourse-topic-voting/app/services/discourse_topic_voting/votes/cast.rb+++ b/plugins/discourse-topic-voting/app/services/discourse_topic_voting/votes/cast.rb@@ -19,12 +19,13 @@ module DiscourseTopicVotingpolicy :current_user_can_votetransaction do- step :create_vote+ model :vote, :create_votestep :refresh_vote_countendstep :enqueue_backfill_badgesstep :enqueue_topic_upvote_webhook+ step :publish_vote_created_eventprivate@@ -72,6 +73,10 @@ module DiscourseTopicVotingWebHook.enqueue_topic_voting_hooks(:topic_upvote, topic, payload.to_json)end++ def publish_vote_created_event(vote:)+ DiscourseEvent.trigger(:topic_voting_vote_created, vote)+ endendendenddiff --git a/plugins/discourse-topic-voting/config/locales/client.en.yml b/plugins/discourse-topic-voting/config/locales/client.en.ymlindex 5e035ce0bb3..ac82047de96 100755--- a/plugins/discourse-topic-voting/config/locales/client.en.yml+++ b/plugins/discourse-topic-voting/config/locales/client.en.yml@@ -44,3 +44,14 @@ en:group_name: "Topic Voting Event"topic_upvote: "When a user votes on a topic"topic_unvote: "When a user unvotes on a topic"+ discourse_workflows:+ node_unavailable:+ requires_topic_voting: "Requires topic voting to be enabled"+ nodes:+ "trigger:topic_received_vote": "Topic received vote"+ node_descriptions:+ "trigger:topic_received_vote": "Fires when a user votes on a topic"+ topic_received_vote:+ category_ids_description: "Only trigger for topics in these categories. Leave empty to match all categories."+ tag_names: "Tags"+ tag_names_description: "Only trigger for topics that have at least one of these tags"diff --git a/plugins/discourse-topic-voting/lib/discourse_topic_voting/workflows/schema.rb b/plugins/discourse-topic-voting/lib/discourse_topic_voting/workflows/schema.rbnew file mode 100644index 00000000000..6e86f4554f6--- /dev/null+++ b/plugins/discourse-topic-voting/lib/discourse_topic_voting/workflows/schema.rb@@ -0,0 +1,27 @@+# frozen_string_literal: true++module DiscourseTopicVoting+ module Workflows+ module Schema+ VOTE_SCHEMA =+ DiscourseWorkflows::Schema.entity(+ "vote",+ JSON.parse(<<~JSON),+ {+ "id": { "type": "integer" },+ "count": { "type": "integer" },+ "created_at": { "type": "string", "format": "date-time" }+ }+ JSON+ "Vote that was cast, with the topic vote count once it was cast",+ )++ TOPIC_RECEIVED_VOTE_OUTPUT_SCHEMA =+ DiscourseWorkflows::Schema.merge(+ DiscourseWorkflows::Schema::TOPIC_LIST_ITEM_SCHEMA,+ DiscourseWorkflows::Schema::USER_SCHEMA,+ VOTE_SCHEMA,+ ).freeze+ end+ end+enddiff --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.rbnew file mode 100644index 00000000000..c0c3f25dc68--- /dev/null+++ b/plugins/discourse-topic-voting/lib/discourse_workflows/nodes/topic_received_vote/v1.rb@@ -0,0 +1,102 @@+# frozen_string_literal: true++if defined?(DiscourseWorkflows)+ module DiscourseWorkflows+ module Nodes+ module TopicReceivedVote+ class V1 < DiscourseWorkflows::NodeType+ description(+ name: "trigger:topic_received_vote",+ version: "1.0",+ defaults: {+ icon: "vote-up",+ color: "purple",+ },+ group: "discourse_triggers",+ events: [:topic_voting_vote_created],+ available: -> { SiteSetting.topic_voting_enabled },+ unavailable_reason_key: "discourse_workflows.node_unavailable.requires_topic_voting",+ output_contracts: [+ {+ schema: DiscourseTopicVoting::Workflows::Schema::TOPIC_RECEIVED_VOTE_OUTPUT_SCHEMA,+ },+ ],+ properties: {+ category_ids: {+ type: :array,+ required: false,+ ui: {+ control: :category,+ multiple: true,+ },+ },+ include_subcategories: {+ type: :boolean,+ required: false,+ default: true,+ ui: {+ control: :checkbox,+ },+ display_options: {+ show: {+ category_ids: [{ condition: { exists: true } }],+ },+ },+ },+ tag_names: {+ type: :string,+ required: false,+ ui: {+ control: :tags,+ },+ },+ },+ )++ def initialize(vote, *)+ super(parameters: {})+ @vote = vote+ end++ def valid?+ @vote.present? && topic.present? && voter.present?+ end++ def output+ {+ topic: serialize_record(topic, TopicListItemSerializer),+ user: serialize_user(voter),+ vote: {+ id: @vote.id,+ count: topic.vote_count,+ created_at: @vote.created_at.iso8601,+ },+ }+ 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?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names")))+ end++ private++ def topic+ @topic ||= @vote&.topic+ end++ def voter+ @voter ||= @vote&.user+ end++ def matches_tags?(tag_names)+ tag_names.empty? || (topic.tags.pluck(:name) & tag_names).any?+ end+ end+ end+ end+ end+enddiff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rbindex 5dc1153a3f5..94a3c86eb25 100755--- a/plugins/discourse-topic-voting/plugin.rb+++ b/plugins/discourse-topic-voting/plugin.rb@@ -34,6 +34,20 @@ require_relative "lib/discourse_topic_voting/topic_votes_filter"after_initialize doSeedFu.fixture_paths << Rails.root.join("plugins/discourse-topic-voting/db/fixtures").to_s+ if respond_to?(:register_discourse_workflows_node)+ register_discourse_workflows_node do+ require_relative "lib/discourse_workflows/nodes/topic_received_vote/v1"+ DiscourseWorkflows::Nodes::TopicReceivedVote::V1+ end++ on(:topic_voting_vote_created) do |vote|+ DiscourseWorkflows::EventListener.handle(+ DiscourseWorkflows::Nodes::TopicReceivedVote::V1,+ vote,+ )+ end+ end+reloadable_patch doregister_category_type(DiscourseTopicVoting::Categories::Types::Ideas)CategoriesController.prepend(DiscourseTopicVoting::CategoriesControllerExtension)diff --git a/plugins/discourse-topic-voting/spec/integration/workflows_topic_received_vote_spec.rb b/plugins/discourse-topic-voting/spec/integration/workflows_topic_received_vote_spec.rbnew file mode 100644index 00000000000..9210b8f1958--- /dev/null+++ b/plugins/discourse-topic-voting/spec/integration/workflows_topic_received_vote_spec.rb@@ -0,0 +1,69 @@+# frozen_string_literal: true++RSpec.describe "Topic received vote workflow trigger" do+ fab!(:voter, :user)+ fab!(:category)+ fab!(:other_category, :category)+ fab!(:topic) { Fabricate(:topic, category: category) }++ before do+ SiteSetting.topic_voting_enabled = true+ SiteSetting.enable_discourse_workflows = true+ DiscourseTopicVoting::CategorySetting.create!(category: category)+ Category.reset_voting_cache+ end++ it "enqueues matching workflows when a topic is voted on" do+ all_categories_workflow =+ Fabricate(+ :discourse_workflows_workflow,+ created_by: voter,+ published: true,+ **build_workflow_graph { |graph| graph.node "trigger-all", "trigger:topic_received_vote" },+ )+ matching_category_workflow =+ Fabricate(+ :discourse_workflows_workflow,+ created_by: voter,+ published: true,+ **build_workflow_graph do |graph|+ graph.node "trigger-matching",+ "trigger:topic_received_vote",+ configuration: {+ "category_ids" => [category.id],+ }+ end,+ )+ Fabricate(+ :discourse_workflows_workflow,+ created_by: voter,+ published: true,+ **build_workflow_graph do |graph|+ graph.node "trigger-other",+ "trigger:topic_received_vote",+ configuration: {+ "category_ids" => [other_category.id],+ }+ end,+ )++ DiscourseTopicVoting::Votes::Cast.call(params: { topic_id: topic.id }, guardian: voter.guardian)++ jobs =+ Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.select do |job|+ job["args"].first["trigger_node_id"].in?(%w[trigger-all trigger-matching trigger-other])+ end++ expect(jobs.map { |job| job["args"].first["workflow_id"] }).to contain_exactly(+ all_categories_workflow.id,+ matching_category_workflow.id,+ )+ expect(jobs.map { |job| job["args"].first["trigger_data"] }).to all(+ include(+ "topic" => include("id" => topic.id),+ "user" => include("id" => voter.id),+ "vote" => include("count" => 1),+ ),+ )+ end+enddiff --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.rbnew file mode 100644index 00000000000..63d7052b7a3--- /dev/null+++ b/plugins/discourse-topic-voting/spec/lib/discourse_workflows/nodes/topic_received_vote/v1_spec.rb@@ -0,0 +1,86 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::TopicReceivedVote::V1 do+ fab!(:voter, :user)+ fab!(:category)+ fab!(:tag)+ fab!(:topic) { Fabricate(:topic, category: category, tags: [tag]) }+ fab!(:vote) { Fabricate(:topic_voting_votes, user: voter, topic: topic) }++ before do+ SiteSetting.topic_voting_enabled = true+ SiteSetting.enable_discourse_workflows = true+ DiscourseTopicVoting::CategorySetting.create!(category: category)+ Category.reset_voting_cache+ topic.update_vote_count+ end++ it "returns the correct identifier" do+ expect(described_class.identifier).to eq("trigger:topic_received_vote")+ end++ describe "#valid?" do+ it "returns true when a vote is present" do+ expect(described_class.new(vote)).to be_valid+ end++ it "returns false when the vote is nil" do+ expect(described_class.new(nil)).not_to be_valid+ end+ end++ describe "#output" do+ it "returns the topic, the voter and the vote", :aggregate_failures do+ output = described_class.new(vote).output++ expect(output[:topic][:id]).to eq(topic.id)+ expect(output[:user][:username]).to eq(voter.username)+ expect(output[:vote]).to include(id: vote.id, count: 1)+ expect(output).to match_node_output_schema(described_class)+ end+ end++ describe "#matches?" do+ it "matches every vote by default" do+ expect(described_class.new(vote).matches?(trigger_context({}))).to eq(true)+ end++ it "matches only votes on topics in the selected categories" do+ other_category = Fabricate(:category)++ expect(+ described_class.new(vote).matches?(trigger_context("category_ids" => [category.id])),+ ).to eq(true)+ expect(+ described_class.new(vote).matches?(trigger_context("category_ids" => [other_category.id])),+ ).to eq(false)+ end++ it "matches votes on topics in subcategories when subcategories are included" do+ parent_category = Fabricate(:category)+ category.update!(parent_category: parent_category)++ context = { "category_ids" => [parent_category.id] }++ expect(described_class.new(vote).matches?(trigger_context(context))).to eq(true)+ expect(+ described_class.new(vote).matches?(+ trigger_context(context.merge("include_subcategories" => false)),+ ),+ ).to eq(false)+ end++ it "matches only votes on topics carrying one of the selected tags" do+ expect(described_class.new(vote).matches?(trigger_context("tag_names" => [tag.name]))).to eq(+ true,+ )+ expect(described_class.new(vote).matches?(trigger_context("tag_names" => ["other"]))).to eq(+ false,+ )+ end+ end++ def trigger_context(parameters)+ DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters })+ end+enddiff --git a/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/votes/cast_spec.rb b/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/votes/cast_spec.rbindex 864e420ae0c..a23bf29035b 100644--- a/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/votes/cast_spec.rb+++ b/plugins/discourse-topic-voting/spec/services/discourse_topic_voting/votes/cast_spec.rb@@ -101,6 +101,15 @@ RSpec.describe DiscourseTopicVoting::Votes::Cast doexpect { result }.not_to change(Jobs::EmitWebHookEvent.jobs, :size)end+ it "triggers the vote created event" do+ events = DiscourseEvent.track_events(:topic_voting_vote_created) { result }++ expect(events.size).to eq(1)+ expect(events.first[:params].first).to eq(+ DiscourseTopicVoting::Vote.find_by(user: user, topic: topic),+ )+ end+it "enqueues the backfill badges job" doexpect { result }.to change(Jobs::DiscourseTopicVoting::BackfillBadges.jobs, :size).by(1)expect(Jobs::DiscourseTopicVoting::BackfillBadges.jobs.last["args"].first).to include(