おそらく、調べるのに最も良い場所は、Data Explorer プラグインに追加された自動化スクリプトでしょう: discourse-data-explorer/plugin.rb at main · discourse/discourse-data-explorer · GitHub プラグインの既存のスクリプトとトリガーを見る価値もあります: https://github.com/discourse/discourse-automation/tree/main/lib/discourse_automation
Meta にカスタム自動化の追加に関する情報はあまりないので、ユーザーのアクティビティ概要メール設定を更新するスクリプトを追加する plugin.rb ファイルの例を以下に示します。このスクリプトは、Automation プラグインの ‘user_added_to_group’ または ‘user_removed_from_group’ トリガーによってトリガーできます。
# frozen_string_literal: true
# name: automation-script-example
# about: An example of how to add a script to an automation
# version: 0.0.1
# authors: scossar
enabled_site_setting :automation_script_example_enabled
after_initialize do
reloadable_patch do
if defined?(DiscourseAutomation)
DiscourseAutomation::Scriptable::USER_UPDATE_SUMMARY_EMAIL_OPTIONS =
"user_update_summary_email_options"
add_automation_scriptable(
DiscourseAutomation::Scriptable::USER_UPDATE_SUMMARY_EMAIL_OPTIONS
) do
field :email_digests, component: :boolean
version 1
triggerables [:user_added_to_group, :user_removed_from_group]
script do |context, fields, automation|
if automation.script == "user_update_summary_email_options" && (context["kind"] == "user_added_to_group" || context["kind"] == "user_removed_from_group")
user_id = context["user"].id
digest_option = fields.dig("email_digests", "value")
user_option = UserOption.find_by(user_id: user_id)
if (user_option)
user_option.update(email_digests: digest_option)
end
end
end
end
end
end
end
プラグインの全コードはこちらです: GitHub - scossar/automation-script-example: An example of how to add a custom script to the Discourse Automation plugin..
このコードを本番サイトでそのまま使用しないでください。今晩初めて Automation コードを見ました。コードに関する潜在的な問題についてフィードバックがあれば、この投稿と GitHub リポジトリを更新します。
編集: 私の懸念は、‘user_added_to_group’ または ‘user_removed_from_group’ トリガーによって複数の自動化スクリプトがトリガーされるケースをどのように最も適切に処理するかということでした。プラグインの最初のバージョンでは次をチェックしていました。
fields.has_key?("email_digests")
しかし、それはかなり不安定に感じられました。email_digests キーを持つ別のスクリプトが追加された場合はどうなるでしょうか?
更新されたコードは、コードブロックに automation パラメータを渡し、次をチェックします。
automation.script == "user_update_summary_email_options"
これにより、スクリプトが誤った自動化に対して実行されないことが保証されるはずです。
…さらに考えてみると、スクリプトが設定されていない自動化によってトリガーされる可能性は低いでしょう ![]()