2014-05-28 04:14:37 +08:00
|
|
|
require 'csv'
|
|
|
|
require_dependency 'system_message'
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
|
|
|
|
class BulkInvite < Jobs::Base
|
|
|
|
sidekiq_options retry: false
|
2014-06-26 04:04:26 +08:00
|
|
|
attr_accessor :current_user
|
2014-05-28 04:14:37 +08:00
|
|
|
|
|
|
|
def initialize
|
2018-08-30 15:34:58 +08:00
|
|
|
super
|
2014-05-28 04:14:37 +08:00
|
|
|
@logs = []
|
|
|
|
@sent = 0
|
|
|
|
@failed = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(args)
|
2016-12-05 00:06:35 +08:00
|
|
|
filename = args[:filename]
|
2014-06-26 04:04:26 +08:00
|
|
|
@current_user = User.find_by(id: args[:current_user_id])
|
2016-12-05 00:06:35 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:filename) if filename.blank?
|
2018-03-28 16:20:08 +08:00
|
|
|
csv_path = "#{Invite.base_directory}/#{filename}"
|
2014-06-26 02:35:11 +08:00
|
|
|
|
|
|
|
# read csv file, and send out invitations
|
2018-03-28 16:20:08 +08:00
|
|
|
read_csv_file(csv_path)
|
2016-10-18 03:00:29 +08:00
|
|
|
ensure
|
2014-06-26 02:35:11 +08:00
|
|
|
# send notification to user regarding progress
|
2014-06-26 04:04:26 +08:00
|
|
|
notify_user
|
2014-06-26 02:35:11 +08:00
|
|
|
|
2018-03-28 16:20:08 +08:00
|
|
|
FileUtils.rm_rf(csv_path) if csv_path
|
2014-06-26 02:35:11 +08:00
|
|
|
end
|
|
|
|
|
2014-06-26 04:04:26 +08:00
|
|
|
def read_csv_file(csv_path)
|
2018-01-09 23:03:17 +08:00
|
|
|
file = File.open(csv_path, encoding: 'bom|utf-8')
|
|
|
|
CSV.new(file).each do |csv_info|
|
2014-06-27 01:19:34 +08:00
|
|
|
if csv_info[0]
|
|
|
|
if (EmailValidator.email_regex =~ csv_info[0])
|
2014-06-26 23:16:53 +08:00
|
|
|
# email is valid
|
|
|
|
send_invite(csv_info, $INPUT_LINE_NUMBER)
|
2014-05-28 04:14:37 +08:00
|
|
|
@sent += 1
|
|
|
|
else
|
2014-06-26 02:35:11 +08:00
|
|
|
# invalid email
|
2018-08-30 15:34:58 +08:00
|
|
|
save_log "Invalid Email '#{csv_info[0]}' at line number '#{$INPUT_LINE_NUMBER}'"
|
2014-05-28 04:14:37 +08:00
|
|
|
@failed += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-18 03:00:29 +08:00
|
|
|
rescue Exception => e
|
2018-08-30 15:34:58 +08:00
|
|
|
save_log "Bulk Invite Process Failed -- '#{e.message}'"
|
2016-10-18 03:00:29 +08:00
|
|
|
@failed += 1
|
2018-01-09 23:03:17 +08:00
|
|
|
ensure
|
|
|
|
file.close
|
2014-05-28 04:14:37 +08:00
|
|
|
end
|
|
|
|
|
2014-06-26 23:16:53 +08:00
|
|
|
def get_group_ids(group_names, csv_line_number)
|
|
|
|
group_ids = []
|
2014-06-27 01:19:34 +08:00
|
|
|
if group_names
|
2014-06-26 23:16:53 +08:00
|
|
|
group_names = group_names.split(';')
|
|
|
|
group_names.each { |group_name|
|
|
|
|
group_detail = Group.find_by_name(group_name)
|
2014-06-27 01:19:34 +08:00
|
|
|
if group_detail
|
2014-06-26 23:16:53 +08:00
|
|
|
# valid group
|
|
|
|
group_ids.push(group_detail.id)
|
|
|
|
else
|
|
|
|
# invalid group
|
2018-08-30 15:34:58 +08:00
|
|
|
save_log "Invalid Group '#{group_name}' at line number '#{csv_line_number}'"
|
2014-06-27 01:19:34 +08:00
|
|
|
@failed += 1
|
2014-06-26 23:16:53 +08:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
return group_ids
|
2014-05-28 04:14:37 +08:00
|
|
|
end
|
|
|
|
|
2014-06-26 23:16:53 +08:00
|
|
|
def get_topic(topic_id, csv_line_number)
|
|
|
|
topic = nil
|
2014-06-27 01:19:34 +08:00
|
|
|
if topic_id
|
2014-06-26 23:16:53 +08:00
|
|
|
topic = Topic.find_by_id(topic_id)
|
|
|
|
if topic.nil?
|
2018-08-30 15:34:58 +08:00
|
|
|
save_log "Invalid Topic ID '#{topic_id}' at line number '#{csv_line_number}'"
|
2014-06-27 01:19:34 +08:00
|
|
|
@failed += 1
|
2014-06-26 02:35:11 +08:00
|
|
|
end
|
2014-06-26 23:16:53 +08:00
|
|
|
end
|
|
|
|
return topic
|
2014-06-26 02:35:11 +08:00
|
|
|
end
|
|
|
|
|
2014-06-26 23:16:53 +08: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 22:44:15 +08:00
|
|
|
begin
|
|
|
|
Invite.invite_by_email(email, @current_user, topic, group_ids)
|
|
|
|
rescue => e
|
2018-08-30 15:34:58 +08:00
|
|
|
save_log "Error inviting '#{email}' -- #{Rails::Html::FullSanitizer.new.sanitize(e.message)}"
|
2014-10-09 22:44:15 +08:00
|
|
|
@sent -= 1
|
|
|
|
@failed += 1
|
|
|
|
end
|
2014-06-26 02:35:11 +08:00
|
|
|
end
|
|
|
|
|
2014-05-28 04:14:37 +08:00
|
|
|
def save_log(message)
|
|
|
|
@logs << "[#{Time.now}] #{message}"
|
|
|
|
end
|
|
|
|
|
2014-06-26 04:04:26 +08:00
|
|
|
def notify_user
|
|
|
|
if @current_user
|
2014-05-28 04:14:37 +08:00
|
|
|
if (@sent > 0 && @failed == 0)
|
2018-08-30 15:34:58 +08:00
|
|
|
SystemMessage.create_from_system_user(
|
|
|
|
@current_user,
|
|
|
|
:bulk_invite_succeeded,
|
|
|
|
sent: @sent
|
|
|
|
)
|
2014-05-28 04:14:37 +08:00
|
|
|
else
|
2018-08-30 15:34:58 +08:00
|
|
|
SystemMessage.create_from_system_user(
|
|
|
|
@current_user,
|
|
|
|
:bulk_invite_failed,
|
|
|
|
sent: @sent,
|
|
|
|
failed: @failed,
|
|
|
|
logs: @logs.join("\n")
|
|
|
|
)
|
2014-05-28 04:14:37 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|