From 954e908e246b29ffbca1ab7a851044beafabd110 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 24 Jun 2021 16:51:49 +0200 Subject: [PATCH] FEATURE: adds support for a random assign automation (#162) --- config/locales/client.en.yml | 8 ++++++++ config/locales/server.en.yml | 4 ++++ plugin.rb | 24 ++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 9da95b8..260bab0 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -69,3 +69,11 @@ en: bulk: unassign: "Unassign Topics" assign: "Assign Topics" + discourse_automation: + scriptables: + random_assign: + fields: + assignees_group: + label: Assignees Group + assigned_topic: + label: Assigned Topic ID diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index e37c6a2..603d0c1 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -62,3 +62,7 @@ en: %{topic_0} %{topic_1} %{topic_2} + discourse_automation: + scriptables: + random_assign: + title: Random assign diff --git a/plugin.rb b/plugin.rb index 178b241..05d36a8 100644 --- a/plugin.rb +++ b/plugin.rb @@ -575,4 +575,28 @@ after_initialize do end end end + + if defined?(DiscourseAutomation) + add_automation_scriptable('random_assign') do + field :assignees_group, component: :group + field :assigned_topic, component: :text + + version 1 + + triggerables %i[point_in_time recurring] + + script do |context, fields| + next unless SiteSetting.assign_enabled? + + next unless group_id = fields.dig('assignees_group', 'group_id') + next unless group = Group.find_by(id: group_id) + assign_to = group.group_users.order(Arel.sql('RANDOM()')).first.user + + next unless topic_id = fields.dig('assigned_topic', 'text') + next unless topic = Topic.find_by(id: topic_id) + + TopicAssigner.new(topic, Discourse.system_user).assign(assign_to) + end + end + end end