2014-05-27 16:14:37 -04:00
|
|
|
require 'csv'
|
|
|
|
require_dependency 'system_message'
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
|
|
|
|
class BulkInvite < Jobs::Base
|
|
|
|
sidekiq_options retry: false
|
2014-06-25 16:04:26 -04:00
|
|
|
attr_accessor :current_user
|
2014-05-27 16:14:37 -04:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@logs = []
|
|
|
|
@sent = 0
|
|
|
|
@failed = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(args)
|
|
|
|
filename = args[:filename]
|
|
|
|
identifier = args[:identifier]
|
|
|
|
chunks = args[:chunks].to_i
|
2014-06-25 16:04:26 -04:00
|
|
|
@current_user = User.find_by(id: args[:current_user_id])
|
2014-05-27 16:14:37 -04:00
|
|
|
|
|
|
|
raise Discourse::InvalidParameters.new(:filename) if filename.blank?
|
|
|
|
raise Discourse::InvalidParameters.new(:identifier) if identifier.blank?
|
|
|
|
raise Discourse::InvalidParameters.new(:chunks) if chunks <= 0
|
|
|
|
|
2014-06-25 14:35:11 -04:00
|
|
|
# merge chunks, and get csv path
|
|
|
|
csv_path = get_csv_path(filename, identifier, chunks)
|
|
|
|
|
|
|
|
# read csv file, and send out invitations
|
2014-06-25 16:04:26 -04:00
|
|
|
read_csv_file(csv_path)
|
2014-06-25 14:35:11 -04:00
|
|
|
|
|
|
|
# send notification to user regarding progress
|
2014-06-25 16:04:26 -04:00
|
|
|
notify_user
|
2014-06-25 14:35:11 -04:00
|
|
|
|
|
|
|
# since emails have already been sent out, delete the uploaded csv file
|
|
|
|
FileUtils.rm_rf(csv_path) rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_csv_path(filename, identifier, chunks)
|
2014-05-27 16:14:37 -04:00
|
|
|
csv_path = "#{Invite.base_directory}/#{filename}"
|
|
|
|
tmp_csv_path = "#{csv_path}.tmp"
|
|
|
|
# path to tmp directory
|
|
|
|
tmp_directory = File.dirname(Invite.chunk_path(identifier, filename, 0))
|
|
|
|
# merge all chunks
|
|
|
|
HandleChunkUpload.merge_chunks(chunks, upload_path: csv_path, tmp_upload_path: tmp_csv_path, model: Invite, identifier: identifier, filename: filename, tmp_directory: tmp_directory)
|
|
|
|
|
2014-06-25 14:35:11 -04:00
|
|
|
return csv_path
|
|
|
|
end
|
|
|
|
|
2014-06-25 16:04:26 -04:00
|
|
|
def read_csv_file(csv_path)
|
2014-05-27 16:14:37 -04:00
|
|
|
CSV.foreach(csv_path) 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
|
2014-06-26 11:16:53 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2014-06-26 11:16:53 -04:00
|
|
|
def get_group_ids(group_names, csv_line_number)
|
|
|
|
group_ids = []
|
2014-06-26 13:19:34 -04:00
|
|
|
if group_names
|
2014-06-26 11:16:53 -04:00
|
|
|
group_names = group_names.split(';')
|
|
|
|
group_names.each { |group_name|
|
|
|
|
group_detail = Group.find_by_name(group_name)
|
2014-06-26 13:19:34 -04:00
|
|
|
if group_detail
|
2014-06-26 11:16:53 -04:00
|
|
|
# valid group
|
|
|
|
group_ids.push(group_detail.id)
|
|
|
|
else
|
|
|
|
# invalid group
|
|
|
|
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
|
|
|
|
return group_ids
|
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
|
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?
|
|
|
|
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
|
|
|
|
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]
|
|
|
|
group_ids = get_group_ids(csv_info[1], csv_line_number)
|
|
|
|
topic = get_topic(csv_info[2], csv_line_number)
|
2014-10-09 10:44:15 -04:00
|
|
|
begin
|
|
|
|
Invite.invite_by_email(email, @current_user, topic, group_ids)
|
|
|
|
rescue => e
|
|
|
|
log "Error inviting '#{email}' -- #{e}"
|
|
|
|
@sent -= 1
|
|
|
|
@failed += 1
|
|
|
|
end
|
2014-06-25 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
2014-05-27 16:14:37 -04:00
|
|
|
def log(message)
|
|
|
|
save_log(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
2014-07-02 04:58:57 -04:00
|
|
|
SystemMessage.create_from_system_user(@current_user, :bulk_invite_succeeded, sent: @sent)
|
2014-05-27 16:14:37 -04:00
|
|
|
else
|
2014-07-02 04:58:57 -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
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|