discourse/plugins/automation/lib/discourse_automation/triggers/topic.rb
Osama Sayegh 3d4faf3272
FEATURE: Merge discourse-automation (#26432)
Automation (previously known as discourse-automation) is now a core plugin.
2024-04-03 18:20:43 +03:00

30 lines
921 B
Ruby

# 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