about summary refs log tree commit diff
path: root/app/workers/registration_janitor_worker.rb
blob: c3250f993f0020cdb34c5ba827ed5fac9871ebf0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

class RegistrationJanitorWorker
  include Sidekiq::Worker
  include LogHelper

  def perform(user_id)
    user = User.find(user_id)
    janitor = janitor_account || Account.representative

    spam_triggers = ENV.fetch('REGISTRATION_SPAM_TRIGGERS', '').split('|').map { |phrase| Regexp.escape(phrase.strip) }
    spam_triggers = spam_triggers.empty? ? /(?!)/ : /\b#{Regexp.union(spam_triggers)}\b/i

    intro = user.invite_request&.text
    # normalize it
    intro = intro.gsub(/[\u200b-\u200d\ufeff\u200e\u200f]/, '').strip.downcase unless intro.nil?

    return user.notify_staff_about_pending_account! unless intro.blank? || intro.split.count < 5 || spam_triggers.match?(intro)

    user_friendly_action_log(janitor, :reject_registration, user.account.username, "Registration was spam filtered.")
    Form::AccountBatch.new(current_account: janitor, account_ids: user.account.id, action: 'reject').save
  rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
    true
  end

  private

  def janitor_account
    account_id = ENV.fetch('JANITOR_USER', '').to_i
    return if account_id == 0
    Account.find_by(id: account_id)
  end
end