Skip to content

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

DEV: add automation trigger on-close-topic (#34705)

chapoi3e01922cb8f2
diff --git a/plugins/automation/config/locales/server.en.yml b/plugins/automation/config/locales/server.en.yml
index e6ac188ea8a..228cd33acb1 100644
--- a/plugins/automation/config/locales/server.en.yml
+++ b/plugins/automation/config/locales/server.en.yml
@@ -62,6 +62,9 @@ en:
topic:
title: Topic
description: The associated script will only be used on the specified topic, this is usefull for scripts doing validation for example
+ topic_closed:
+ title: Topic closed
+ description: Triggers a script when a topic is closed.
after_post_cook:
title: After post cook
description: When a post content is cooked the automation will be triggered
@@ -105,7 +108,7 @@ en:
doc: Allows to send multiple pms to a user. Each PM accepts a delay.
suspend_user_by_email:
title: Suspend user by email
- description: Automatically suspend an account based on email address
+ description: Automatically suspend an account based on email address
user_global_notice:
title: User global notice
description: Allows to display a global notice for a user
@@ -149,6 +152,6 @@ en:
send_chat_message:
title: "Send Chat Message"
description: "Send a custom chat message to a channel"
- random_assign:
+ random_assign:
title: "Random Assign"
description: "Randomly assign topics to a group"
diff --git a/plugins/automation/lib/discourse_automation/event_handlers.rb b/plugins/automation/lib/discourse_automation/event_handlers.rb
index 55cedefbe8c..3fb8dd14d32 100644
--- a/plugins/automation/lib/discourse_automation/event_handlers.rb
+++ b/plugins/automation/lib/discourse_automation/event_handlers.rb
@@ -306,6 +306,23 @@ module DiscourseAutomation
end
end
+ def self.handle_topic_closed(topic)
+ name = DiscourseAutomation::Triggers::TOPIC_CLOSED
+
+ DiscourseAutomation::Automation
+ .where(trigger: name, enabled: true)
+ .find_each do |automation|
+ automation.trigger!(
+ "kind" => name,
+ "topic" => topic,
+ "placeholders" => {
+ "topic_url" => topic.relative_url,
+ "topic_title" => topic.title,
+ },
+ )
+ end
+ end
+
def self.handle_after_post_cook(post, cooked)
return cooked if post.post_type != Post.types[:regular] || post.post_number > 1
diff --git a/plugins/automation/lib/discourse_automation/triggers.rb b/plugins/automation/lib/discourse_automation/triggers.rb
index d1a0a1348fd..08a3a7fbb67 100644
--- a/plugins/automation/lib/discourse_automation/triggers.rb
+++ b/plugins/automation/lib/discourse_automation/triggers.rb
@@ -13,6 +13,7 @@ module DiscourseAutomation
STALLED_TOPIC = "stalled_topic"
STALLED_WIKI = "stalled_wiki"
TOPIC = "topic"
+ TOPIC_CLOSED = "topic_closed"
USER_ADDED_TO_GROUP = "user_added_to_group"
USER_BADGE_GRANTED = "user_badge_granted"
USER_FIRST_LOGGED_IN = "user_first_logged_in"
diff --git a/plugins/automation/lib/discourse_automation/triggers/topic_closed.rb b/plugins/automation/lib/discourse_automation/triggers/topic_closed.rb
new file mode 100644
index 00000000000..d075d6ece25
--- /dev/null
+++ b/plugins/automation/lib/discourse_automation/triggers/topic_closed.rb
@@ -0,0 +1,3 @@
+# frozen_string_literal: true
+
+DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::TOPIC_CLOSED)
diff --git a/plugins/automation/plugin.rb b/plugins/automation/plugin.rb
index f5ae007b615..088ae8737a3 100644
--- a/plugins/automation/plugin.rb
+++ b/plugins/automation/plugin.rb
@@ -74,6 +74,7 @@ after_initialize do
lib/discourse_automation/triggers/stalled_wiki
lib/discourse_automation/triggers/topic_tags_changed
lib/discourse_automation/triggers/topic
+ lib/discourse_automation/triggers/topic_closed
lib/discourse_automation/triggers/user_added_to_group
lib/discourse_automation/triggers/user_badge_granted
lib/discourse_automation/triggers/user_promoted
@@ -193,6 +194,8 @@ after_initialize do
)
end
+ on(:topic_closed) { |topic| ::DiscourseAutomation::EventHandlers.handle_topic_closed(topic) }
+
on(:post_created) do |post|
DiscourseAutomation::EventHandlers.handle_post_created_edited(post, :create)
end
diff --git a/plugins/automation/spec/lib/discourse_automation/event_handlers_spec.rb b/plugins/automation/spec/lib/discourse_automation/event_handlers_spec.rb
index 71d7771e0de..0c00fb789d8 100644
--- a/plugins/automation/spec/lib/discourse_automation/event_handlers_spec.rb
+++ b/plugins/automation/spec/lib/discourse_automation/event_handlers_spec.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true
describe DiscourseAutomation::EventHandlers do
+ before { SiteSetting.discourse_automation_enabled = true }
+
describe "#handle_stalled_topic" do
context "when tags are empty" do
fab!(:automation) do
@@ -21,4 +23,24 @@ describe DiscourseAutomation::EventHandlers do
end
end
end
+
+ describe "#handle_topic_closed" do
+ fab!(:automation) do
+ Fabricate(:automation, trigger: DiscourseAutomation::Triggers::TOPIC_CLOSED, enabled: true)
+ end
+ fab!(:topic) { Fabricate(:post).topic }
+
+ it "triggers automation when a topic is closed" do
+ list =
+ capture_contexts do
+ TopicStatusUpdater.new(topic, Fabricate(:admin)).update!("closed", true)
+ end
+
+ expect(list.size).to eq(1)
+ expect(list.first["kind"]).to eq("topic_closed")
+ expect(list.first["topic"].id).to eq(topic.id)
+ expect(list.first["placeholders"]["topic_url"]).to eq(topic.relative_url)
+ expect(list.first["placeholders"]["topic_title"]).to eq(topic.title)
+ end
+ end
end