discourse/plugins/automation/lib/discourse_automation/scripts/gift_exchange.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

73 lines
2.5 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::GIFT_EXCHANGE) do
placeholder :year
placeholder :giftee_username
placeholder :gifter_username
field :giftee_assignment_messages, component: :pms, accepts_placeholders: true, required: true
field :gift_exchangers_group, component: :group, required: true
version 17
triggerables %i[point_in_time]
script do |_, fields, automation|
now = Time.zone.now
group_id = fields.dig("gift_exchangers_group", "value")
unless group = Group.find_by(id: group_id)
Rails.logger.warn "[discourse-automation] Couldnt find group with id #{group_id}"
next
end
cf_name = "#{group.name}-gifts-were-exchanged-#{automation.id}-#{version}-#{now.year}"
if group.custom_fields[cf_name].present?
Rails.logger.warn "[discourse-automation] Gift exchange script has already been run on #{cf_name} this year #{now.year} for this script version #{version}"
next
end
usernames = group.users.pluck(:username)
if usernames.size < 3
Rails.logger.warn "[discourse-automation] Gift exchange needs at least 3 users in a group"
next
end
usernames.shuffle!
usernames << usernames[0]
# shuffle the pairs to prevent prying eyes to identify matches by looking at the timestamps of the topics
pairs = usernames.each_cons(2).to_a.shuffle
pairs.each do |gifter, giftee|
placeholders = { year: now.year.to_s, gifter_username: gifter, giftee_username: giftee }
Array(fields.dig("giftee_assignment_messages", "value")).each do |giftee_assignment_message|
if giftee_assignment_message["title"].blank?
Rails.logger.warn "[discourse-automation] Gift exchange requires a title for the PM"
next
end
if giftee_assignment_message["raw"].blank?
Rails.logger.warn "[discourse-automation] Gift exchange requires a raw for the PM"
next
end
raw = utils.apply_placeholders(giftee_assignment_message["raw"], placeholders)
title = utils.apply_placeholders(giftee_assignment_message["title"], placeholders)
utils.send_pm(
{ target_usernames: Array(gifter), title: title, raw: raw },
delay: giftee_assignment_message["delay"],
prefers_encrypt: giftee_assignment_message["prefers_encrypt"],
automation_id: automation.id,
)
end
end
group.custom_fields[cf_name] = true
group.save_custom_fields
end
end