A platform for community discussion. Free, open, simple.
FEATURE: Add a "Like post" action and a "Post liked" trigger to workflows
Régis Hanol7b52ff539c60
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex c792f640c29..c3b33fce1c0 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -236,6 +236,7 @@ en:"trigger:post_moved": "Post moved""trigger:post_destroyed": "Post deleted""trigger:post_recovered": "Post recovered"+ "trigger:post_like_changed": "Post liked""trigger:topic_created": "Topic created""trigger:topic_category_changed": "Topic category changed""trigger:topic_tag_changed": "Topic tag changed"@@ -266,6 +267,7 @@ en:"action:topic": "Topic""action:post": "Post""action:flag_post": "Flag post"+ "action:post_like": "Like post""action:send_personal_message": "Send personal message""action:set_fields": "Set fields""trigger:stale_topic": "Stale topic"@@ -320,6 +322,7 @@ en:"trigger:post_moved": "Fires when a post is moved to another topic""trigger:post_destroyed": "Fires when a post is deleted. The user is whoever deleted it, not the author.""trigger:post_recovered": "Fires when a deleted post is restored. The user is whoever restored it, not the author."+ "trigger:post_like_changed": "Fires when a post is liked or unliked. The user is whoever liked it, not the author.""trigger:topic_created": "Fires when a new topic is created""trigger:topic_category_changed": "Fires when a topic is moved to a different category""trigger:topic_tag_changed": "Fires when tags are added to or removed from a topic"@@ -350,6 +353,7 @@ en:"action:topic": "Create, retrieve, list, close, archive, or set custom fields on topics""action:post": "Create, edit, delete, recover, retrieve, or list posts""action:flag_post": "Flag a post as spam or add it to the review queue, optionally hiding or deleting the post and silencing its author"+ "action:post_like": "Like or unlike a post""action:send_personal_message": "Send a personal message to users or groups""action:set_fields": "Set or modify data fields in the workflow context""trigger:stale_topic": "Fires when a topic has had no activity for a period"@@ -672,6 +676,13 @@ en:spam_silence: "Flag as spam, hide post and silence user"reason: "Reason"actor_username: "Performed by user"+ post_like:+ operations:+ like: "Like post"+ unlike: "Unlike post"+ post_like_changed:+ changes_liked: "Liked"+ changes_unliked: "Unliked"flag_user:username: "User"reason: "Reason"@@ -814,6 +825,7 @@ en:array: "Array"object: "Object"fields:+ actor_username: "Performed by user"category_ids: "Categories"category_ids_description: "Only trigger for topics in these categories. Leave empty to match all categories."category_ids_placeholder: "All categories"@@ -829,6 +841,9 @@ en:changed_fields_location: "Location"changed_fields_title: "Title"changed_fields_profile_images: "Profile and card backgrounds"+ changes: "Changes"+ changes_description: "Leave empty to match any change."+ changes_placeholder: "Any change"combinator: "Match mode"combinator_and: "All conditions must pass"combinator_or: "Any condition can pass"@@ -838,9 +853,13 @@ en:group_ids_description: "Only trigger for users in at least one of these groups"group_ids_placeholder: "All groups"include_subcategories: "Include subcategories"+ operation: "Operation"+ post_id: "Post ID"reviewable_types: "Reviewable types"reviewable_types_description: "Only trigger for these reviewable types"reviewable_types_placeholder: "All types"+ tag_names: "Tags"+ tag_names_description: "Only trigger for topics that have at least one of these tags"dynamic_values:badge_id: "a badge ID"boolean: "true or false"diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rbindex 8851e7c049c..397d1d3bd6f 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb@@ -267,6 +267,8 @@ module DiscourseWorkflows"site setting settings configuration config admin toggle enable disable change update value","action:flag_user" =>"flag user report spammer spam suspect review queue moderation approve reject account signup",+ "action:post_like" => "like unlike heart favorite upvote",+ "trigger:post_like_changed" => "liked unliked like heart favorite upvote","trigger:user_added_to_group" => "joined added to group membership member","trigger:user_removed_from_group" => "left removed from group membership member","trigger:user_created" => "signup sign up register registration new account joined site",diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v1.rbnew file mode 100644index 00000000000..a7b298be055--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like/v1.rb@@ -0,0 +1,89 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module PostLike+ class V1 < NodeType+ OPERATIONS = %w[like unlike].freeze+ OUTPUT_SCHEMA = {+ "$schema" => Schema::DRAFT_URI,+ "type" => "object",+ "properties" => {+ "post_id" => {+ "type" => "integer",+ },+ "username" => {+ "type" => "string",+ },+ "changed" => {+ "type" => "boolean",+ },+ },+ }.freeze++ description(+ name: "action:post_like",+ version: "1.0",+ defaults: {+ icon: "heart",+ color: "red",+ },+ group: "discourse_actions",+ capabilities: {+ run_scope: "per_item",+ },+ output_contracts: [{ schema: OUTPUT_SCHEMA }],+ properties: {+ operation: {+ type: :options,+ required: true,+ options: OPERATIONS,+ default: "like",+ },+ post_id: {+ type: :string,+ required: true,+ },+ **actor_property(allow_anonymous: false),+ },+ )++ def execute(exec_ctx)+ items =+ exec_ctx.input_items.map.with_index do |_item, item_index|+ wrap(process(exec_ctx, item_index))+ end++ [items]+ end++ private++ def process(exec_ctx, item_index)+ like = exec_ctx.get_node_parameter("operation", item_index) != "unlike"+ post = ::Post.find(exec_ctx.get_node_parameter("post_id", item_index))+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)+ liked =+ ::PostAction.exists?(+ post:,+ user: actor,+ post_action_type_id: ::PostActionType::LIKE_POST_ACTION_ID,+ )+ changed = like != liked++ if changed+ result =+ if like+ ::PostActionCreator.like(actor, post)+ else+ ::PostActionDestroyer.destroy(actor, post, :like)+ end+ raise_node_error!(result.errors.full_messages.join(", "), item_index:) if result.failed?+ end++ { post_id: post.id, username: actor.username, changed: }+ end+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v1.rbnew file mode 100644index 00000000000..120a7775aea--- /dev/null+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_like_changed/v1.rb@@ -0,0 +1,73 @@+# frozen_string_literal: true++module DiscourseWorkflows+ module Nodes+ module PostLikeChanged+ class V1 < NodeType+ CHANGES = { like_created: "liked", like_destroyed: "unliked" }.freeze++ OUTPUT_SCHEMA =+ Schema.merge(+ Schema::POST_SCHEMA,+ Schema::TOPIC_LIST_ITEM_SCHEMA,+ Schema::USER_SCHEMA,+ Schema.document("change" => { "type" => "string", "enum" => CHANGES.values }),+ ).freeze++ description(+ name: "trigger:post_like_changed",+ version: "1.0",+ defaults: {+ icon: "heart",+ color: "red",+ },+ group: "discourse_triggers",+ event: CHANGES.keys,+ output_contracts: [{ schema: OUTPUT_SCHEMA }],+ properties: {+ changes: {+ type: :multi_options,+ required: false,+ default: [],+ options: CHANGES.values,+ },+ **CATEGORY_FILTER_PROPERTIES,+ **TAG_FILTER_PROPERTIES,+ },+ )++ def self.from_event(event_name, post_action, *)+ new(event_name, post_action)+ end++ def initialize(event_name, post_action)+ super(parameters: {})+ @change = CHANGES[event_name]+ @post = post_action.post+ @user = post_action.user+ end++ def valid?+ @change.present? && @post&.topic.present? && @user.present?+ end++ def user_id+ @user.id+ end++ def output+ {+ change: @change,+ post: serialize_post(@post),+ topic: topic_data(@post.topic),+ user: serialize_user(@user),+ }+ end++ def matches?(trigger_ctx)+ matches_changes?(trigger_ctx, @change) && matches_topic_filters?(@post.topic, trigger_ctx)+ end+ end+ end+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_changed_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_changed_spec.rbnew file mode 100644index 00000000000..55893786c98--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_changed_spec.rb@@ -0,0 +1,46 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::PostLikeChanged::V1 do+ fab!(:admin)+ fab!(:category)+ fab!(:other_category, :category)+ fab!(:topic) { Fabricate(:topic, category:) }+ fab!(:post) { Fabricate(:post, topic:) }+ fab!(:liker, :user)+ fab!(:tag)++ describe "#matches?" do+ it "filters changes, categories, and tags" do+ SiteSetting.tagging_enabled = true+ topic.tags << tag+ trigger = described_class.from_event(:like_created, PostAction.new(post:, user: liker))++ expect(trigger.matches?(trigger_context({}))).to eq(true)+ expect(trigger.matches?(trigger_context(changes: ["unliked"]))).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(changes: ["liked"], category_ids: [category.id], tag_names: [tag.name]),+ ),+ ).to eq(true)+ end+ end++ describe "event dispatch" do+ it "runs workflows as the liker when a post is liked and unliked" do+ graph = build_workflow_graph { |builder| builder.node "like", described_class.identifier }+ Fabricate(:discourse_workflows_workflow, created_by: admin, published: true, **graph)++ PostActionCreator.like(liker, post)+ PostActionDestroyer.destroy(liker, post, :like)++ jobs = Jobs::DiscourseWorkflows::ExecuteWorkflow.jobs.map { |job| job["args"].first }+ data = jobs.pluck("trigger_data")+ expect(data.pluck("change")).to eq(%w[liked unliked])+ expect(data.map { |payload| payload.dig("user", "id") }).to eq([liker.id, liker.id])+ expect(jobs.pluck("user_id")).to eq([liker.id, liker.id])+ expect(data).to all(match_node_output_schema(described_class))+ end+ end+enddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_spec.rbnew file mode 100644index 00000000000..5a4e32f2f23--- /dev/null+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_like_spec.rb@@ -0,0 +1,52 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::PostLike::V1 do+ fab!(:post)+ fab!(:actor, :user)++ describe "#execute" do+ let(:configuration) do+ { "operation" => operation, "post_id" => post.id.to_s, "actor_username" => actor.username }+ end++ context "with like operation" do+ let(:operation) { "like" }++ it "likes the post as the actor" do+ result = execute_node(configuration:)++ expect(result).to eq("post_id" => post.id, "username" => actor.username, "changed" => true)+ expect(result).to match_node_output_schema(described_class)+ expect(post.reload.like_count).to eq(1)+ end++ it "leaves an existing like untouched" do+ PostActionCreator.like(actor, post)++ expect(execute_node(configuration:)).to include("changed" => false)+ expect(post.reload.like_count).to eq(1)+ end++ it "fails when the actor cannot like the post" do+ configuration["actor_username"] = post.user.username++ expect { execute_node(configuration:) }.to raise_error(DiscourseWorkflows::NodeError)+ end+ end++ context "with unlike operation" do+ let(:operation) { "unlike" }++ it "removes the actor's like" do+ PostActionCreator.like(actor, post)++ expect(execute_node(configuration:)).to include("changed" => true)+ expect(post.reload.like_count).to eq(0)+ end++ it "does nothing when the actor has not liked the post" do+ expect(execute_node(configuration:)).to include("changed" => false)+ end+ end+ end+end