discourse/app/jobs/regular/bulk_invite.rb

Failed to ignore revisions in .git-blame-ignore-revs.

175 lines
4.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2014-05-28 04:14:37 +08:00
module Jobs
class BulkInvite < ::Jobs::Base
2014-05-28 04:14:37 +08:00
sidekiq_options retry: false
def initialize
2018-08-30 15:34:58 +08:00
super
2014-05-28 04:14:37 +08:00
@logs = []
@sent = 0
@failed = 0
@groups = {}
@valid_groups = {}
2014-05-28 04:14:37 +08:00
end
def execute(args)
invites = args[:invites]
raise Discourse::InvalidParameters.new(:invites) if invites.blank?
@current_user = User.find_by(id: args[:current_user_id])
raise Discourse::InvalidParameters.new(:current_user_id) unless @current_user
@guardian = Guardian.new(@current_user)
@total_invites = invites.length
process_invites(invites)
if @total_invites > Invite::BULK_INVITE_EMAIL_LIMIT
::Jobs.enqueue(:process_bulk_invite_emails)
end
ensure
2014-06-26 04:04:26 +08:00
notify_user
end
private
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-28 04:14:37 +08:00
end
end
rescue Exception => e
2018-08-30 15:34:58 +08:00
save_log "Bulk Invite Process Failed -- '#{e.message}'"
@failed += 1
2014-05-28 04:14:37 +08:00
end
def get_groups(group_names)
groups = []
2014-06-27 01:19:34 +08:00
if group_names
group_names = group_names.split(';')
group_names.each { |group_name|
group = fetch_group(group_name)
if group && can_edit_group?(group)
# valid group
groups.push(group)
else
# invalid group
save_log "Invalid Group '#{group_name}'"
2014-06-27 01:19:34 +08:00
@failed += 1
end
}
end
groups
2014-05-28 04:14:37 +08:00
end
def get_topic(topic_id)
topic = nil
2014-06-27 01:19:34 +08:00
if topic_id
topic = Topic.find_by_id(topic_id)
if topic.nil?
save_log "Invalid Topic ID '#{topic_id}'"
2014-06-27 01:19:34 +08:00
@failed += 1
end
end
topic
end
def send_invite(invite)
email = invite[:email]
groups = get_groups(invite[:groups])
topic = get_topic(invite[:topic_id])
begin
if user = Invite.find_user_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
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
end
rescue => e
2018-08-30 15:34:58 +08:00
save_log "Error inviting '#{email}' -- #{Rails::Html::FullSanitizer.new.sanitize(e.message)}"
@sent -= 1
@failed += 1
end
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
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-28 04:14:37 +08:00
end
end