mirror of https://github.com/mastodon/mastodon
Automatically switch from open to approved registrations in absence of moderators (#29318)
parent
08342ad40c
commit
2751acb6cd
@ -0,0 +1,3 @@
|
|||||||
|
<%= raw t('admin_mailer.auto_close_registrations.body', instance: @instance) %>
|
||||||
|
|
||||||
|
<%= raw t('application_mailer.view')%> <%= admin_settings_registrations_url %>
|
@ -0,0 +1,33 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class Scheduler::AutoCloseRegistrationsScheduler
|
||||||
|
include Sidekiq::Worker
|
||||||
|
include Redisable
|
||||||
|
|
||||||
|
sidekiq_options retry: 0
|
||||||
|
|
||||||
|
# Automatically switch away from open registrations if no
|
||||||
|
# moderator had any activity in that period of time
|
||||||
|
OPEN_REGISTRATIONS_MODERATOR_THRESHOLD = 1.week + UserTrackingConcern::SIGN_IN_UPDATE_FREQUENCY
|
||||||
|
|
||||||
|
def perform
|
||||||
|
return if Rails.configuration.x.email_domains_whitelist.present? || ENV['DISABLE_AUTOMATIC_SWITCHING_TO_APPROVED_REGISTRATIONS'] == 'true'
|
||||||
|
return unless Setting.registrations_mode == 'open'
|
||||||
|
|
||||||
|
switch_to_approval_mode! unless active_moderators?
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def active_moderators?
|
||||||
|
User.those_who_can(:manage_reports).exists?(current_sign_in_at: OPEN_REGISTRATIONS_MODERATOR_THRESHOLD.ago...)
|
||||||
|
end
|
||||||
|
|
||||||
|
def switch_to_approval_mode!
|
||||||
|
Setting.registrations_mode = 'approved'
|
||||||
|
|
||||||
|
User.those_who_can(:view_devops).includes(:account).find_each do |user|
|
||||||
|
AdminMailer.with(recipient: user.account).auto_close_registrations.deliver_later
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,60 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Scheduler::AutoCloseRegistrationsScheduler do
|
||||||
|
subject { described_class.new }
|
||||||
|
|
||||||
|
describe '#perform' do
|
||||||
|
let(:moderator_activity_date) { Time.now.utc }
|
||||||
|
|
||||||
|
before do
|
||||||
|
Fabricate(:user, role: UserRole.find_by(name: 'Owner'), current_sign_in_at: 10.years.ago)
|
||||||
|
Fabricate(:user, role: UserRole.find_by(name: 'Moderator'), current_sign_in_at: moderator_activity_date)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when registrations are open' do
|
||||||
|
before do
|
||||||
|
Setting.registrations_mode = 'open'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a moderator has logged in recently' do
|
||||||
|
let(:moderator_activity_date) { Time.now.utc }
|
||||||
|
|
||||||
|
it 'does not change registrations mode' do
|
||||||
|
expect { subject.perform }.to_not change(Setting, :registrations_mode)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a moderator has not recently signed in' do
|
||||||
|
let(:moderator_activity_date) { 1.year.ago }
|
||||||
|
|
||||||
|
it 'changes registrations mode from open to approved' do
|
||||||
|
expect { subject.perform }.to change(Setting, :registrations_mode).from('open').to('approved')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when registrations are closed' do
|
||||||
|
before do
|
||||||
|
Setting.registrations_mode = 'none'
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a moderator has logged in recently' do
|
||||||
|
let(:moderator_activity_date) { Time.now.utc }
|
||||||
|
|
||||||
|
it 'does not change registrations mode' do
|
||||||
|
expect { subject.perform }.to_not change(Setting, :registrations_mode)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when a moderator has not recently signed in' do
|
||||||
|
let(:moderator_activity_date) { 1.year.ago }
|
||||||
|
|
||||||
|
it 'does not change registrations mode' do
|
||||||
|
expect { subject.perform }.to_not change(Setting, :registrations_mode)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue