2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-05-27 16:14:37 -04:00
|
|
|
require 'csv'
|
|
|
|
require_dependency 'system_message'
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
|
|
|
|
class BulkInvite < Jobs::Base
|
|
|
|
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)
|
2016-12-04 11:06:35 -05:00
|
|
|
filename = args[:filename]
|
|
|
|
raise Discourse::InvalidParameters.new(:filename) if filename.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-04-15 09:13:53 -04:00
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
csv_path = "#{Invite.base_directory}/#{filename}"
|
2014-06-25 14:35:11 -04:00
|
|
|
|
|
|
|
# read csv file, and send out invitations
|
2018-03-28 04:20:08 -04:00
|
|
|
read_csv_file(csv_path)
|
2016-10-17 15:00:29 -04:00
|
|
|
ensure
|
2014-06-25 14:35:11 -04:00
|
|
|
# send notification to user regarding progress
|
2014-06-25 16:04:26 -04:00
|
|
|
notify_user
|
2019-04-15 21:59:35 -04:00
|
|
|
File.delete(csv_path) if csv_path
|
2014-06-25 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
2019-04-15 09:13:53 -04:00
|
|
|
private
|
|
|
|
|
2014-06-25 16:04:26 -04:00
|
|
|
def read_csv_file(csv_path)
|
2018-01-09 10:03:17 -05:00
|
|
|
file = File.open(csv_path, encoding: 'bom|utf-8')
|
|
|
|
CSV.new(file).each do |csv_info|
|
2014-06-26 13:19:34 -04:00
|
|
|
if csv_info[0]
|
|
|
|
if (EmailValidator.email_regex =~ csv_info[0])
|
2014-06-26 11:16:53 -04:00
|
|
|
# email is valid
|
|
|
|
send_invite(csv_info, $INPUT_LINE_NUMBER)
|
2014-05-27 16:14:37 -04:00
|
|
|
@sent += 1
|
|
|
|
else
|
2014-06-25 14:35:11 -04:00
|
|
|
# invalid email
|
2018-08-30 03:34:58 -04:00
|
|
|
save_log "Invalid Email '#{csv_info[0]}' at line number '#{$INPUT_LINE_NUMBER}'"
|
2014-05-27 16:14:37 -04:00
|
|
|
@failed += 1
|
|
|
|
end
|
|
|
|
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
|
2018-01-09 10:03:17 -05:00
|
|
|
ensure
|
2019-04-04 06:06:51 -04:00
|
|
|
file&.close
|
2014-05-27 16:14:37 -04:00
|
|
|
end
|
|
|
|
|
2019-04-15 09:26:03 -04:00
|
|
|
def get_groups(group_names, csv_line_number)
|
|
|
|
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
|
2018-08-30 03:34:58 -04:00
|
|
|
save_log "Invalid Group '#{group_name}' at line number '#{csv_line_number}'"
|
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
|
|
|
|
|
2014-06-26 11:16:53 -04:00
|
|
|
def get_topic(topic_id, csv_line_number)
|
|
|
|
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?
|
2018-08-30 03:34:58 -04:00
|
|
|
save_log "Invalid Topic ID '#{topic_id}' at line number '#{csv_line_number}'"
|
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
|
|
|
|
2014-06-26 11:16:53 -04:00
|
|
|
return topic
|
2014-06-25 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
2014-06-26 11:16:53 -04:00
|
|
|
def send_invite(csv_info, csv_line_number)
|
|
|
|
email = csv_info[0]
|
2019-04-15 09:26:03 -04:00
|
|
|
groups = get_groups(csv_info[1], csv_line_number)
|
2014-06-26 11:16:53 -04:00
|
|
|
topic = get_topic(csv_info[2], csv_line_number)
|
2019-04-15 09:26:03 -04:00
|
|
|
|
2014-10-09 10:44:15 -04:00
|
|
|
begin
|
2019-04-15 09:26:03 -04:00
|
|
|
if user = User.find_by_email(email)
|
|
|
|
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
|
|
|
|
Invite.invite_by_email(email, @current_user, topic, groups.map(&:id))
|
|
|
|
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
|