auto_close_registrations_scheduler.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. # frozen_string_literal: true
  2. class Scheduler::AutoCloseRegistrationsScheduler
  3. include Sidekiq::Worker
  4. include Redisable
  5. sidekiq_options retry: 0
  6. # Automatically switch away from open registrations if no
  7. # moderator had any activity in that period of time
  8. OPEN_REGISTRATIONS_MODERATOR_THRESHOLD = 1.week + UserTrackingConcern::SIGN_IN_UPDATE_FREQUENCY
  9. def perform
  10. return if Rails.configuration.x.email_domains_allowlist.present? || ENV['DISABLE_AUTOMATIC_SWITCHING_TO_APPROVED_REGISTRATIONS'] == 'true'
  11. return unless Setting.registrations_mode == 'open'
  12. switch_to_approval_mode! unless active_moderators?
  13. end
  14. private
  15. def active_moderators?
  16. User.those_who_can(:manage_reports).exists?(current_sign_in_at: OPEN_REGISTRATIONS_MODERATOR_THRESHOLD.ago...)
  17. end
  18. def switch_to_approval_mode!
  19. Setting.registrations_mode = 'approved'
  20. User.those_who_can(:manage_settings).includes(:account).find_each do |user|
  21. AdminMailer.with(recipient: user.account).auto_close_registrations.deliver_later
  22. end
  23. end
  24. end