Skip to content

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

FEATURE: Add `Post edited` workflow trigger (#40479)

Joffrey JAFFEUX65aa94fdc38d
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
index f00548fdb89..9cf46bf4c51 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
@@ -291,6 +291,7 @@ export function propertyOptionLabel(nodeDefinitionOrType, fieldName, option) {
return (
translatedOrNull(`${base}.${key}_${valueKey}`) ||
translatedOrNull(`${base}.${key}s.${valueKey}`) ||
+ (option.label_key ? translatedOrNull(option.label_key) : null) ||
option.label ||
option.name ||
option.value
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index ecc7608b38f..54f1b55b35c 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -131,6 +131,7 @@ en:
nodes:
"trigger:topic_closed": "Topic closed"
"trigger:post_created": "Post created"
+ "trigger:post_edited": "Post edited"
"trigger:topic_created": "Topic created"
"trigger:topic_category_changed": "Topic category changed"
"trigger:topic_tag_changed": "Topic tag changed"
@@ -166,6 +167,7 @@ en:
node_descriptions:
"trigger:topic_closed": "Fires when a topic is closed by a user or auto-close timer"
"trigger:post_created": "Fires when a new post or reply is created"
+ "trigger:post_edited": "Fires after the first post in a topic is edited and cooked"
"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"
@@ -486,6 +488,18 @@ en:
category_id_description: "Only trigger for posts in this category"
tag_names: "Tags"
tag_names_description: "Only trigger for posts in topics that have at least one of these tags"
+ post_edited:
+ category_id: "Category"
+ category_id_description: "Only trigger for posts in this category"
+ tag_names: "Tags"
+ tag_names_description: "Only trigger for posts in topics that have at least one of these tags"
+ post_scope: "Post scope"
+ post_scope_description: "Choose which posts should trigger the workflow"
+ post_scope_first_post: "First post"
+ post_scope_replies: "Replies"
+ post_scope_all_posts: "All posts"
+ trust_levels: "Trust levels"
+ trust_levels_description: "Only trigger for posts created by users with these trust levels"
topic_created:
category_id: "Category"
category_id_description: "Only trigger for topics in this category"
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rb
new file mode 100644
index 00000000000..271d7b7b717
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/post_edited/v1.rb
@@ -0,0 +1,118 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module PostEdited
+ class V1 < NodeType
+ POST_SCOPE_OPTIONS = %w[first_post replies all_posts].freeze
+ TRUST_LEVEL_OPTIONS = [
+ { value: "0", label_key: "trust_levels.names.newuser" },
+ { value: "1", label_key: "trust_levels.names.basic" },
+ { value: "2", label_key: "trust_levels.names.member" },
+ { value: "3", label_key: "trust_levels.names.regular" },
+ { value: "4", label_key: "trust_levels.names.leader" },
+ ].freeze
+
+ description(
+ name: "trigger:post_edited",
+ version: "1.0",
+ defaults: {
+ icon: "comment",
+ color: "violet",
+ },
+ group: "discourse_triggers",
+ properties: {
+ post_scope: {
+ type: :options,
+ required: true,
+ default: "first_post",
+ options: POST_SCOPE_OPTIONS,
+ },
+ category_id: {
+ type: :integer,
+ required: false,
+ ui: {
+ control: :category,
+ },
+ },
+ tag_names: {
+ type: :string,
+ required: false,
+ ui: {
+ control: :tags,
+ },
+ },
+ trust_levels: {
+ type: :multi_options,
+ required: false,
+ options: TRUST_LEVEL_OPTIONS,
+ },
+ },
+ )
+
+ def initialize(post, cooked, *)
+ super(parameters: {})
+ @post = post
+ @cooked = cooked
+ end
+
+ def valid?
+ @post.present? && @post.topic.present? && @post.post_type == ::Post.types[:regular]
+ end
+
+ def output
+ {
+ post: serialize_post(@post, include_cooked: true).merge(cooked: @cooked),
+ topic: topic_data(@post.topic),
+ }
+ end
+
+ def matches?(trigger_ctx)
+ matches_post_scope?(trigger_ctx.get_node_parameter("post_scope", "first_post")) &&
+ matches_category?(trigger_ctx.get_node_parameter("category_id")) &&
+ matches_tags?(normalize_tag_names(trigger_ctx.get_node_parameter("tag_names"))) &&
+ matches_trust_level?(trigger_ctx.get_node_parameter("trust_levels"))
+ end
+
+ private
+
+ def topic_data(topic)
+ serialize_record(topic, TopicListItemSerializer)
+ end
+
+ def matches_post_scope?(post_scope)
+ case post_scope
+ when "all_posts"
+ true
+ when "replies"
+ @post.post_number > 1
+ else
+ @post.post_number == 1
+ end
+ end
+
+ def matches_category?(category_id)
+ category_id.blank? || topic_category_ids.include?(category_id.to_i)
+ end
+
+ def topic_category_ids
+ [@post.topic.category_id, @post.topic.category&.parent_category_id].compact
+ end
+
+ 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 }
+ trust_levels.empty? || trust_levels.include?(@post.user.trust_level)
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/plugin.rb b/plugins/discourse-workflows/plugin.rb
index c94f875a4b6..e79db644c31 100644
--- a/plugins/discourse-workflows/plugin.rb
+++ b/plugins/discourse-workflows/plugin.rb
@@ -85,6 +85,16 @@ after_initialize do
self,
)
+ Plugin::Filter.register(:after_post_cook) do |post, cooked|
+ DiscourseWorkflows::EventListener.handle(
+ DiscourseWorkflows::Nodes::PostEdited::V1,
+ post,
+ cooked,
+ )
+
+ cooked
+ end
+
add_to_serializer :site,
:topic_admin_button_workflows,
include_condition: -> { scope.is_admin? } do
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb
index dede81be2ce..19f2b2fd41f 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb
@@ -134,6 +134,50 @@ RSpec.describe DiscourseWorkflows::EventListener do
expect(enqueued_trigger_node_ids).not_to include("category-mismatch", "tag-mismatch")
end
+ it "only enqueues post edited workflows matching topic category, tags, and trust levels" do
+ topic.tags << tag
+ create_published_workflow(
+ "matching-trigger",
+ "trigger:post_edited",
+ configuration: {
+ "category_id" => category.id.to_s,
+ "tag_names" => [tag.name],
+ "trust_levels" => ["1"],
+ },
+ )
+ create_published_workflow(
+ "trust-level-mismatch",
+ "trigger:post_edited",
+ configuration: {
+ "trust_levels" => ["2"],
+ },
+ )
+ create_published_workflow(
+ "tag-mismatch",
+ "trigger:post_edited",
+ configuration: {
+ "tag_names" => ["missing"],
+ },
+ )
+
+ post = create_post(user: Fabricate(:user, trust_level: TrustLevel[1]), category: category)
+ post.topic.tags << tag
+ described_class.handle(DiscourseWorkflows::Nodes::PostEdited::V1, post, "<p>Cooked</p>")
+
+ expect(enqueued_trigger_node_ids).to include("matching-trigger")
+ expect(enqueued_trigger_node_ids).not_to include("trust-level-mismatch", "tag-mismatch")
+ end
+
+ it "does not enqueue post edited workflows for replies by default" do
+ create_post(topic: topic)
+ create_published_workflow("first-post-only", "trigger:post_edited")
+
+ post = create_post(user: Fabricate(:user, trust_level: TrustLevel[1]), topic: topic)
+ described_class.handle(DiscourseWorkflows::Nodes::PostEdited::V1, post, "<p>Cooked</p>")
+
+ expect(enqueued_trigger_node_ids).not_to include("first-post-only")
+ end
+
def create_published_workflow(trigger_node_id, trigger_type, configuration: {})
graph =
build_workflow_graph do |g|
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v1_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v1_spec.rb
new file mode 100644
index 00000000000..edeb49ad3af
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/post_edited/v1_spec.rb
@@ -0,0 +1,111 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::PostEdited::V1 do
+ fab!(:user) { Fabricate(:user, trust_level: TrustLevel[2]) }
+ fab!(:reply_user, :user)
+ fab!(:parent_category, :category)
+ fab!(:category) { Fabricate(:category, parent_category: parent_category) }
+ fab!(:first_post) { create_post(user: user, category: category, raw: "First post") }
+ fab!(:topic) { first_post.topic }
+ fab!(:tag) { Fabricate(:tag, name: "test-tag") }
+ fab!(:reply) { create_post(user: reply_user, topic: topic, raw: "Reply") }
+ fab!(:small_action) do
+ Fabricate(:post, topic: topic, user: reply_user, post_type: Post.types[:small_action])
+ end
+
+ before do
+ SiteSetting.tagging_enabled = true
+ topic.tags << tag
+ end
+
+ describe "#valid?" do
+ it "returns true for regular posts" do
+ trigger = described_class.new(first_post, "<p>Cooked</p>")
+
+ expect(trigger).to be_valid
+
+ reply_trigger = described_class.new(reply, "<p>Cooked</p>")
+ expect(reply_trigger).to be_valid
+ end
+
+ it "returns false for non-regular posts" do
+ trigger = described_class.new(small_action, "<p>Cooked</p>")
+
+ expect(trigger).not_to be_valid
+ end
+ end
+
+ describe "#output" do
+ it "returns post and topic data" do
+ trigger = described_class.new(first_post, "<p>Cooked</p>")
+ output = trigger.output
+
+ expect(output[:post][:id]).to eq(first_post.id)
+ expect(output[:post][:raw]).to eq(first_post.raw)
+ expect(output[:post][:cooked]).to eq("<p>Cooked</p>")
+ expect(output[:topic][:id]).to eq(topic.id)
+ expect(output[:topic][:tags].map { |topic_tag| topic_tag[:name] }).to eq(["test-tag"])
+ expect(output).not_to have_key(:cooked)
+ end
+ end
+
+ describe "#matches?" do
+ it "returns true when category, tags, and trust levels are blank" do
+ trigger = described_class.new(first_post, "<p>Cooked</p>")
+
+ expect(trigger.matches?(trigger_context({}))).to eq(true)
+ end
+
+ it "defaults to first posts only" do
+ trigger = described_class.new(reply, "<p>Cooked</p>")
+
+ expect(trigger.matches?(trigger_context({}))).to eq(false)
+ end
+
+ it "matches replies when configured for all posts" do
+ trigger = described_class.new(reply, "<p>Cooked</p>")
+
+ expect(trigger.matches?(trigger_context("post_scope" => "all_posts"))).to eq(true)
+ end
+
+ it "matches only replies when configured for replies" do
+ first_post_trigger = described_class.new(first_post, "<p>Cooked</p>")
+ reply_trigger = described_class.new(reply, "<p>Cooked</p>")
+
+ expect(first_post_trigger.matches?(trigger_context("post_scope" => "replies"))).to eq(false)
+ expect(reply_trigger.matches?(trigger_context("post_scope" => "replies"))).to eq(true)
+ end
+
+ it "matches the configured category including parent categories" do
+ trigger = described_class.new(first_post, "<p>Cooked</p>")
+
+ expect(trigger.matches?(trigger_context("category_id" => parent_category.id.to_s))).to eq(
+ true,
+ )
+ expect(trigger.matches?(trigger_context("category_id" => category.id.to_s))).to eq(true)
+ end
+
+ it "matches the configured tags and trust levels" do
+ trigger = described_class.new(first_post, "<p>Cooked</p>")
+
+ expect(
+ trigger.matches?(trigger_context("tag_names" => [tag.name], "trust_levels" => ["2"])),
+ ).to eq(true)
+ end
+
+ it "returns false when category, tags, or trust levels do not match" do
+ other_category = Fabricate(:category)
+ trigger = described_class.new(first_post, "<p>Cooked</p>")
+
+ expect(trigger.matches?(trigger_context("category_id" => other_category.id.to_s))).to eq(
+ false,
+ )
+ expect(trigger.matches?(trigger_context("tag_names" => ["missing"]))).to eq(false)
+ expect(trigger.matches?(trigger_context("trust_levels" => ["1"]))).to eq(false)
+ end
+ end
+
+ def trigger_context(parameters)
+ DiscourseWorkflows::TriggerNodeContext.new({ "parameters" => parameters })
+ end
+end
diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
index 6f9f4ebeb0f..8af776f8beb 100644
--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
@@ -170,6 +170,13 @@ module("Unit | Utility | workflows property engine", function () {
}),
"Custom (Cron)"
);
+ assert.strictEqual(
+ propertyOptionLabel("trigger:post_edited", "trust_levels", {
+ value: "0",
+ label_key: "trust_levels.names.newuser",
+ }),
+ "new user"
+ );
});
test("normalizes camelCase values for locale key lookup", function (assert) {