Skip to content

Commit eebd6ba

Browse files
ClearlyClairekmycode
authored andcommitted
Automatically switch from open to approved registrations in absence of moderators (mastodon#29318)
1 parent 2025ac1 commit eebd6ba

File tree

6 files changed

+109
-0
lines changed

6 files changed

+109
-0
lines changed

app/mailers/admin_mailer.rb

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ def new_critical_software_updates
6969
end
7070
end
7171

72+
def auto_close_registrations
73+
locale_for_account(@me) do
74+
mail subject: default_i18n_subject(instance: @instance)
75+
end
76+
end
77+
7278
private
7379

7480
def process_params
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<%= raw t('admin_mailer.auto_close_registrations.body', instance: @instance) %>
2+
3+
<%= raw t('application_mailer.view')%> <%= admin_settings_registrations_url %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
class Scheduler::AutoCloseRegistrationsScheduler
4+
include Sidekiq::Worker
5+
include Redisable
6+
7+
sidekiq_options retry: 0
8+
9+
# Automatically switch away from open registrations if no
10+
# moderator had any activity in that period of time
11+
OPEN_REGISTRATIONS_MODERATOR_THRESHOLD = 1.week + UserTrackingConcern::SIGN_IN_UPDATE_FREQUENCY
12+
13+
def perform
14+
return if Rails.configuration.x.email_domains_whitelist.present? || ENV['DISABLE_AUTOMATIC_SWITCHING_TO_APPROVED_REGISTRATIONS'] == 'true'
15+
return unless Setting.registrations_mode == 'open'
16+
17+
switch_to_approval_mode! unless active_moderators?
18+
end
19+
20+
private
21+
22+
def active_moderators?
23+
User.those_who_can(:manage_reports).exists?(current_sign_in_at: OPEN_REGISTRATIONS_MODERATOR_THRESHOLD.ago...)
24+
end
25+
26+
def switch_to_approval_mode!
27+
Setting.registrations_mode = 'approved'
28+
29+
User.those_who_can(:view_devops).includes(:account).find_each do |user|
30+
AdminMailer.with(recipient: user.account).auto_close_registrations.deliver_later
31+
end
32+
end
33+
end

config/locales/en.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,9 @@ en:
11121112
title: Webhooks
11131113
webhook: Webhook
11141114
admin_mailer:
1115+
auto_close_registrations:
1116+
body: Due to a lack of recent moderator activity, registrations on %{instance} have been automatically switched to requiring manual review, to prevent %{instance} from being used as a platform for potential bad actors. You can switch it back to open registrations at any time.
1117+
subject: Registrations for %{instance} have been automatically switched to requiring approval
11151118
new_appeal:
11161119
actions:
11171120
delete_statuses: to delete their posts

config/sidekiq.yml

+4
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@
6464
interval: 30 minutes
6565
class: Scheduler::SoftwareUpdateCheckScheduler
6666
queue: scheduler
67+
auto_close_registrations_scheduler:
68+
interval: 1 hour
69+
class: Scheduler::AutoCloseRegistrationsScheduler
70+
queue: scheduler
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
describe Scheduler::AutoCloseRegistrationsScheduler do
6+
subject { described_class.new }
7+
8+
describe '#perform' do
9+
let(:moderator_activity_date) { Time.now.utc }
10+
11+
before do
12+
Fabricate(:user, role: UserRole.find_by(name: 'Owner'), current_sign_in_at: 10.years.ago)
13+
Fabricate(:user, role: UserRole.find_by(name: 'Moderator'), current_sign_in_at: moderator_activity_date)
14+
end
15+
16+
context 'when registrations are open' do
17+
before do
18+
Setting.registrations_mode = 'open'
19+
end
20+
21+
context 'when a moderator has logged in recently' do
22+
let(:moderator_activity_date) { Time.now.utc }
23+
24+
it 'does not change registrations mode' do
25+
expect { subject.perform }.to_not change(Setting, :registrations_mode)
26+
end
27+
end
28+
29+
context 'when a moderator has not recently signed in' do
30+
let(:moderator_activity_date) { 1.year.ago }
31+
32+
it 'changes registrations mode from open to approved' do
33+
expect { subject.perform }.to change(Setting, :registrations_mode).from('open').to('approved')
34+
end
35+
end
36+
end
37+
38+
context 'when registrations are closed' do
39+
before do
40+
Setting.registrations_mode = 'none'
41+
end
42+
43+
context 'when a moderator has logged in recently' do
44+
let(:moderator_activity_date) { Time.now.utc }
45+
46+
it 'does not change registrations mode' do
47+
expect { subject.perform }.to_not change(Setting, :registrations_mode)
48+
end
49+
end
50+
51+
context 'when a moderator has not recently signed in' do
52+
let(:moderator_activity_date) { 1.year.ago }
53+
54+
it 'does not change registrations mode' do
55+
expect { subject.perform }.to_not change(Setting, :registrations_mode)
56+
end
57+
end
58+
end
59+
end
60+
end

0 commit comments

Comments
 (0)