FEATURE: adds support for a random assign automation (#162)

FEATURE: adds support for a random assign automation (#162)

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

GitHub sha: 954e908e246b29ffbca1ab7a851044beafabd110

This commit appears in #162 which was approved by eviltrout. It was merged by jjaffeux.