mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
FEATURE: Merge discourse-automation (#26432)
Automation (previously known as discourse-automation) is now a core plugin.
This commit is contained in:
@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::TOPIC) do
|
||||
field :restricted_topic, component: :text
|
||||
|
||||
on_update do |automation, metadata, previous_metadata|
|
||||
ActiveRecord::Base.transaction do
|
||||
previous_topic_id = previous_metadata.dig("restricted_topic", "value")
|
||||
topic_id = metadata.dig("restricted_topic", "value")
|
||||
|
||||
if previous_topic_id && previous_topic_id != topic_id
|
||||
previous_topic = Topic.find_by(id: previous_topic_id)
|
||||
|
||||
if previous_topic
|
||||
TopicCustomField.where(
|
||||
topic_id: previous_topic_id,
|
||||
name: DiscourseAutomation::CUSTOM_FIELD,
|
||||
value: automation.id,
|
||||
).delete_all
|
||||
end
|
||||
end
|
||||
|
||||
if topic_id
|
||||
topic = Topic.find_by(id: topic_id)
|
||||
topic&.upsert_custom_fields(DiscourseAutomation::CUSTOM_FIELD => automation.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user