2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-05-27 16:14:37 -04:00
|
|
|
module Jobs
|
2019-10-02 00:01:53 -04:00
|
|
|
class BulkInvite < ::Jobs::Base
|
2014-05-27 16:14:37 -04:00
|
|
|
sidekiq_options retry: false
|
|
|
|
|
|
|
|
def initialize
|
2018-08-30 03:34:58 -04:00
|
|
|
super
|
2014-05-27 16:14:37 -04:00
|
|
|
@logs = []
|
|
|
|
@sent = 0
|
|
|
|
@failed = 0
|
2019-04-15 21:31:51 -04:00
|
|
|
@groups = {}
|
|
|
|
@valid_groups = {}
|
2014-05-27 16:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute(args)
|
2019-06-04 10:49:46 -04:00
|
|
|
invites = args[:invites]
|
|
|
|
raise Discourse::InvalidParameters.new(:invites) if invites.blank?
|
2019-04-15 09:13:53 -04:00
|
|
|
|
|
|
|
@current_user = User.find_by(id: args[:current_user_id])
|
|
|
|
raise Discourse::InvalidParameters.new(:current_user_id) unless @current_user
|
2019-04-15 21:31:51 -04:00
|
|
|
@guardian = Guardian.new(@current_user)
|
2019-07-19 01:59:12 -04:00
|
|
|
@total_invites = invites.length
|
2019-04-15 09:13:53 -04:00
|
|
|
|
2019-06-04 10:49:46 -04:00
|
|
|
process_invites(invites)
|
2019-07-19 01:59:12 -04:00
|
|
|
|
|
|
|
if @total_invites > Invite::BULK_INVITE_EMAIL_LIMIT
|
2019-10-21 13:25:35 -04:00
|
|
|
::Jobs.enqueue(:process_bulk_invite_emails)
|
2019-07-19 01:59:12 -04:00
|
|
|
end
|
2016-10-17 15:00:29 -04:00
|
|
|
ensure
|
2014-06-25 16:04:26 -04:00
|
|
|
notify_user
|
2014-06-25 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
2019-04-15 09:13:53 -04:00
|
|
|
private
|
|
|
|
|
2019-06-04 10:49:46 -04:00
|
|
|
def process_invites(invites)
|
|
|
|
invites.each do |invite|
|
|
|
|
if (EmailValidator.email_regex =~ invite[:email])
|
|
|
|
# email is valid
|
|
|
|
send_invite(invite)
|
|
|
|
@sent += 1
|
|
|
|
else
|
|
|
|
# invalid email
|
|
|
|
save_log "Invalid Email '#{invite[:email]}"
|
|
|
|
@failed += 1
|
2014-05-27 16:14:37 -04:00
|
|
|
end
|
|
|
|
end
|
2016-10-17 15:00:29 -04:00
|
|
|
rescue Exception => e
|
2018-08-30 03:34:58 -04:00
|
|
|
save_log "Bulk Invite Process Failed -- '#{e.message}'"
|
2016-10-17 15:00:29 -04:00
|
|
|
@failed += 1
|
2014-05-27 16:14:37 -04:00
|
|
|
end
|
|
|
|
|
2019-06-04 10:49:46 -04:00
|
|
|
def get_groups(group_names)
|
2019-04-15 09:26:03 -04:00
|
|
|
groups = []
|
2019-04-15 09:13:53 -04:00
|
|
|
|
2014-06-26 13:19:34 -04:00
|
|
|
if group_names
|
2014-06-26 11:16:53 -04:00
|
|
|
group_names = group_names.split(';')
|
2019-04-15 09:13:53 -04:00
|
|
|
|
2014-06-26 11:16:53 -04:00
|
|
|
group_names.each { |group_name|
|
2019-04-15 21:31:51 -04:00
|
|
|
group = fetch_group(group_name)
|
2019-04-15 09:13:53 -04:00
|
|
|
|
2019-04-15 21:31:51 -04:00
|
|
|
if group && can_edit_group?(group)
|
2014-06-26 11:16:53 -04:00
|
|
|
# valid group
|
2019-04-15 21:31:51 -04:00
|
|
|
groups.push(group)
|
2014-06-26 11:16:53 -04:00
|
|
|
else
|
|
|
|
# invalid group
|
2019-06-04 10:49:46 -04:00
|
|
|
save_log "Invalid Group '#{group_name}'"
|
2014-06-26 13:19:34 -04:00
|
|
|
@failed += 1
|
2014-06-26 11:16:53 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2019-04-15 09:26:03 -04:00
|
|
|
|
|
|
|
groups
|
2014-05-27 16:14:37 -04:00
|
|
|
end
|
|
|
|
|
2019-06-04 10:49:46 -04:00
|
|
|
def get_topic(topic_id)
|
2014-06-26 11:16:53 -04:00
|
|
|
topic = nil
|
2019-04-15 09:26:03 -04:00
|
|
|
|
2014-06-26 13:19:34 -04:00
|
|
|
if topic_id
|
2014-06-26 11:16:53 -04:00
|
|
|
topic = Topic.find_by_id(topic_id)
|
|
|
|
if topic.nil?
|
2019-06-04 10:49:46 -04:00
|
|
|
save_log "Invalid Topic ID '#{topic_id}'"
|
2014-06-26 13:19:34 -04:00
|
|
|
@failed += 1
|
2014-06-25 14:35:11 -04:00
|
|
|
end
|
2014-06-26 11:16:53 -04:00
|
|
|
end
|
2019-04-15 09:26:03 -04:00
|
|
|
|
2019-11-14 15:10:51 -05:00
|
|
|
topic
|
2014-06-25 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
2019-06-04 10:49:46 -04:00
|
|
|
def send_invite(invite)
|
|
|
|
email = invite[:email]
|
|
|
|
groups = get_groups(invite[:groups])
|
|
|
|
topic = get_topic(invite[:topic_id])
|
2019-04-15 09:26:03 -04:00
|
|
|
|
2014-10-09 10:44:15 -04:00
|
|
|
begin
|
2019-10-30 02:08:47 -04:00
|
|
|
if user = Invite.find_user_by_email(email)
|
2019-04-15 09:26:03 -04:00
|
|
|
if groups.present?
|
|
|
|
Group.transaction do
|
|
|
|
groups.each do |group|
|
|
|
|
group.add(user)
|
|
|
|
|
|
|
|
GroupActionLogger
|
|
|
|
.new(@current_user, group)
|
|
|
|
.log_add_user_to_group(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2019-07-19 01:59:12 -04:00
|
|
|
if @total_invites > Invite::BULK_INVITE_EMAIL_LIMIT
|
|
|
|
invite = Invite.create_invite_by_email(email, @current_user,
|
|
|
|
topic: topic,
|
|
|
|
group_ids: groups.map(&:id),
|
|
|
|
emailed_status: Invite.emailed_status_types[:bulk_pending]
|
|
|
|
)
|
|
|
|
else
|
|
|
|
Invite.invite_by_email(email, @current_user, topic, groups.map(&:id))
|
|
|
|
end
|
2019-04-15 09:26:03 -04:00
|
|
|
end
|
2014-10-09 10:44:15 -04:00
|
|
|
rescue => e
|
2018-08-30 03:34:58 -04:00
|
|
|
save_log "Error inviting '#{email}' -- #{Rails::Html::FullSanitizer.new.sanitize(e.message)}"
|
2014-10-09 10:44:15 -04:00
|
|
|
@sent -= 1
|
|
|
|
@failed += 1
|
|
|
|
end
|
2014-06-25 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
2014-05-27 16:14:37 -04:00
|
|
|
def save_log(message)
|
|
|
|
@logs << "[#{Time.now}] #{message}"
|
|
|
|
end
|
|
|
|
|
2014-06-25 16:04:26 -04:00
|
|
|
def notify_user
|
|
|
|
if @current_user
|
2014-05-27 16:14:37 -04:00
|
|
|
if (@sent > 0 && @failed == 0)
|
2018-08-30 03:34:58 -04:00
|
|
|
SystemMessage.create_from_system_user(
|
|
|
|
@current_user,
|
|
|
|
:bulk_invite_succeeded,
|
|
|
|
sent: @sent
|
|
|
|
)
|
2014-05-27 16:14:37 -04:00
|
|
|
else
|
2018-08-30 03:34:58 -04:00
|
|
|
SystemMessage.create_from_system_user(
|
|
|
|
@current_user,
|
|
|
|
:bulk_invite_failed,
|
|
|
|
sent: @sent,
|
|
|
|
failed: @failed,
|
|
|
|
logs: @logs.join("\n")
|
|
|
|
)
|
2014-05-27 16:14:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-15 21:31:51 -04:00
|
|
|
def fetch_group(group_name)
|
|
|
|
group_name = group_name.downcase
|
|
|
|
group = @groups[group_name]
|
|
|
|
|
|
|
|
unless group
|
|
|
|
group = Group.find_by("lower(name) = ?", group_name)
|
|
|
|
@groups[group_name] = group
|
|
|
|
end
|
|
|
|
|
|
|
|
group
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit_group?(group)
|
|
|
|
group_name = group.name.downcase
|
|
|
|
result = @valid_groups[group_name]
|
|
|
|
|
|
|
|
unless result
|
|
|
|
result = @guardian.can_edit_group?(group)
|
|
|
|
@valid_groups[group_name] = result
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2014-05-27 16:14:37 -04:00
|
|
|
end
|
|
|
|
end
|