mirror of
https://github.com/discourse/discourse.git
synced 2025-05-26 08:01:42 +08:00
DEV: Move array type custom fields to JSON type in automation (#26939)
The automation plugin has 4 custom field types that are array typed. However, array typed custom fields are deprecated and should be migrated to JSON type. This commit does a couple of things: 1. Migrate all four custom fields to JSON 2. Fix a couple of small bugs that have been discovered while migrating the custom fields to JSON (see the comments on this commit's PR for details https://github.com/discourse/discourse/pull/26939)
This commit is contained in:
@ -75,7 +75,9 @@ module DiscourseAutomation
|
||||
.find_each do |automation|
|
||||
once_per_user = automation.trigger_field("once_per_user")["value"]
|
||||
if once_per_user &&
|
||||
UserCustomField.exists?(name: DiscourseAutomation::CUSTOM_FIELD, user_id: user.id)
|
||||
user.custom_fields[
|
||||
DiscourseAutomation::AUTOMATION_IDS_CUSTOM_FIELD
|
||||
].presence&.include?(automation.id)
|
||||
next
|
||||
end
|
||||
|
||||
@ -135,7 +137,8 @@ module DiscourseAutomation
|
||||
user.save_custom_fields
|
||||
end
|
||||
|
||||
automation.attach_custom_field(user)
|
||||
automation.add_id_to_custom_field(user, DiscourseAutomation::AUTOMATION_IDS_CUSTOM_FIELD)
|
||||
|
||||
automation.trigger!("kind" => name, "user" => user, "user_data" => user_data)
|
||||
end
|
||||
end
|
||||
|
@ -10,9 +10,9 @@ module DiscourseAutomation
|
||||
return if !SiteSetting.discourse_automation_enabled
|
||||
return if self.post_type == Post.types[:small_action]
|
||||
return if !topic
|
||||
return if topic.custom_fields[DiscourseAutomation::CUSTOM_FIELD].blank?
|
||||
return if topic.custom_fields[DiscourseAutomation::AUTOMATION_IDS_CUSTOM_FIELD].blank?
|
||||
|
||||
topic.custom_fields[DiscourseAutomation::CUSTOM_FIELD].each do |automation_id|
|
||||
topic.custom_fields[DiscourseAutomation::AUTOMATION_IDS_CUSTOM_FIELD].each do |automation_id|
|
||||
automation = DiscourseAutomation::Automation.find_by(id: automation_id)
|
||||
next if automation&.script != DiscourseAutomation::Scripts::TOPIC_REQUIRED_WORDS
|
||||
|
||||
|
@ -70,9 +70,7 @@ DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::AUTO_RESPONDER
|
||||
end
|
||||
.join("\n\n")
|
||||
|
||||
value = (Array(post.topic.custom_fields[key]) << automation.id).compact.uniq
|
||||
post.topic.custom_fields[key] = value
|
||||
post.topic.save_custom_fields
|
||||
automation.add_id_to_custom_field(post.topic, key)
|
||||
|
||||
PostCreator.create!(
|
||||
answering_user,
|
||||
|
@ -12,17 +12,19 @@ DiscourseAutomation::Triggerable.add(DiscourseAutomation::Triggers::TOPIC) do
|
||||
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
|
||||
automation.remove_id_from_custom_field(
|
||||
previous_topic,
|
||||
DiscourseAutomation::AUTOMATION_IDS_CUSTOM_FIELD,
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
if topic_id
|
||||
topic = Topic.find_by(id: topic_id)
|
||||
topic&.upsert_custom_fields(DiscourseAutomation::CUSTOM_FIELD => automation.id)
|
||||
|
||||
next if !topic
|
||||
|
||||
automation.add_id_to_custom_field(topic, DiscourseAutomation::AUTOMATION_IDS_CUSTOM_FIELD)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user