Repository navigation
Expand file tree
/
Copy pathaccounts.rb
More file actions
628 lines (506 loc) · 21.9 KB
/
Copy pathaccounts.rb
File metadata and controls
628 lines (506 loc) · 21.9 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# frozen_string_literal: true
require_relative 'base'
module Mastodon::CLI
class Accounts < Base
option :all, type: :boolean
desc 'rotate [USERNAME]', 'Generate and broadcast new keys'
long_desc <<-LONG_DESC
Generate and broadcast new RSA keys as part of security
maintenance.
With the --all option, all local accounts will be subject
to the rotation. Otherwise, and by default, only a single
account specified by the USERNAME argument will be
processed.
LONG_DESC
def rotate(username = nil)
if options[:all]
delay = 0
scope = Account.local.without_suspended
progress = create_progress_bar(scope.count)
scope.find_in_batches do |accounts|
accounts.each do |account|
rotate_keys_for_account(account, delay)
progress.increment
end
delay += 5.minutes
end
progress.finish
say("OK, rotated keys for #{progress.progress} accounts", :green)
elsif username.present?
rotate_keys_for_account(Account.find_local(username))
say('OK', :green)
else
fail_with_message 'No account(s) given'
end
end
option :email, required: true
option :confirmed, type: :boolean
option :role
option :reattach, type: :boolean
option :force, type: :boolean
option :approve, type: :boolean
desc 'create USERNAME', 'Create a new user account'
long_desc <<-LONG_DESC
Create a new user account with a given USERNAME and an
e-mail address provided with --email.
With the --confirmed option, the confirmation e-mail will
be skipped and the account will be active straight away.
With the --role option, the role can be supplied.
With the --reattach option, the new user will be reattached
to a given existing username of an old account. If the old
account is still in use by someone else, you can supply
the --force option to delete the old record and reattach the
username to the new account anyway.
With the --approve option, the account will be approved.
LONG_DESC
def create(username)
role_id = nil
if options[:role]
role = UserRole.find_by(name: options[:role])
fail_with_message 'Cannot find user role with that name' if role.nil?
role_id = role.id
end
account = Account.new(username: username)
password = SecureRandom.hex
user = User.new(
email: options[:email],
password: password,
agreement: true,
role_id: role_id,
confirmed_at: options[:confirmed] ? Time.now.utc : nil,
bypass_registration_checks: true
)
if options[:reattach]
account = Account.find_local(username) || Account.new(username: username)
if account.user.present? && !options[:force]
say('The chosen username is currently in use', :red)
say('Use --force to reattach it anyway and delete the other user')
return
elsif account.user.present?
DeleteAccountService.new.call(account, reserve_email: false, reserve_username: false)
account = Account.new(username: username)
end
end
account.suspended_at = nil
account.requested_deletion_at = nil
user.account = account
if user.save
if options[:confirmed]
user.confirmed_at = nil
user.mark_email_as_confirmed!
end
user.approve! if options[:approve]
say('OK', :green)
say("New password: #{password}")
else
report_errors(user.errors)
end
end
option :role
option :remove_role, type: :boolean
option :email
option :confirm, type: :boolean
option :enable, type: :boolean
option :disable, type: :boolean
option :disable_2fa, type: :boolean
option :disable_sign_in_token, type: :boolean
option :approve, type: :boolean
option :reset_password, type: :boolean
desc 'modify USERNAME', 'Modify a user account'
long_desc <<-LONG_DESC
Modify a user account.
With the --role option, update the user's role. To remove the user's
role, i.e. demote to normal user, use --remove-role.
With the --email option, update the user's e-mail address. With
the --confirm option, mark the user's e-mail as confirmed.
With the --disable option, lock the user out of their account. The
--enable option is the opposite.
With the --approve option, the account will be approved, if it was
previously not due to not having open registrations.
With the --disable-2fa option, the two-factor authentication
requirement for the user can be removed.
With the --disable-sign-in-token option, an account that has been
inactive for a long time won't have to introduce a security code that
has been sent to their email when trying to log in. This option
only disables the security code check for a limited amount of time
(up to 92 hours)
With the --enable-sign-in-token option, ensure that the account
generates a security code challenge if it has been inactive for too
long.
With the --reset-password option, the user's password is replaced by
a randomly-generated one, printed in the output.
LONG_DESC
def modify(username)
user = Account.find_local(username)&.user
fail_with_message 'No user with such username' if user.nil?
if options[:role]
role = UserRole.find_by(name: options[:role])
fail_with_message 'Cannot find user role with that name' if role.nil?
user.role_id = role.id
elsif options[:remove_role]
user.role_id = nil
end
user.email = options[:email] if options[:email]
user.disabled = false if options[:enable]
user.disabled = true if options[:disable]
user.approved = true if options[:approve]
user.disable_two_factor! if options[:disable_2fa]
user.disable_sign_in_token! if options[:disable_sign_in_token]
user.enable_sign_in_token! if options[:enable_sign_in_token]
# Password changes are a little different, as we also need to ensure
# sessions, subscriptions, and access tokens are revoked after changing:
password = SecureRandom.hex if options[:reset_password]
user.change_password!(password) if options[:reset_password]
if user.save
user.confirm if options[:confirm]
say('OK', :green)
say("New password: #{password}") if options[:reset_password]
else
report_errors(user.errors)
end
end
option :email
option :dry_run, type: :boolean
desc 'delete [USERNAME]', 'Delete a user'
long_desc <<-LONG_DESC
Remove a user account with a given USERNAME.
With the --email option, the user is selected based on email
rather than username.
LONG_DESC
def delete(username = nil)
if username.present? && options[:email].present?
fail_with_message 'Use username or --email, not both'
elsif username.blank? && options[:email].blank?
fail_with_message 'No username provided'
end
account = nil
if username.present?
account = Account.find_local(username)
fail_with_message 'No user with such username' if account.nil?
else
account = Account.left_joins(:user).find_by(user: { email: options[:email] })
fail_with_message 'No user with such email' if account.nil?
end
say("Deleting user with #{account.statuses_count} statuses, this might take a while...#{dry_run_mode_suffix}")
DeleteAccountService.new.call(account, reserve_email: false) unless dry_run?
say("OK#{dry_run_mode_suffix}", :green)
end
option :force, type: :boolean, aliases: [:f], description: 'Override public key check'
desc 'merge FROM TO', 'Merge two remote accounts into one'
long_desc <<-LONG_DESC
Merge two remote accounts specified by their username@domain
into one, whereby the TO account is the one being merged into
and kept, while the FROM one is removed. It is primarily meant
to fix duplicates caused by other servers changing their domain.
The command by default only works if both accounts have the same
public key to prevent mistakes. To override this, use the --force.
LONG_DESC
def merge(from_acct, to_acct)
username, domain = from_acct.split('@')
from_account = Account.find_remote(username, domain)
fail_with_message "No such account (#{from_acct})" if from_account.nil? || from_account.local?
username, domain = to_acct.split('@')
to_account = Account.find_remote(username, domain)
fail_with_message "No such account (#{to_acct})" if to_account.nil? || to_account.local?
if from_account.public_key != to_account.public_key && !options[:force]
fail_with_message <<~ERROR
Accounts don't have the same public key, might not be duplicates!
Override with --force
ERROR
end
to_account.merge_with!(from_account)
from_account.destroy
say('OK', :green)
end
desc 'backup USERNAME', 'Request a backup for a user'
long_desc <<-LONG_DESC
Request a new backup for an account with a given USERNAME.
The backup will be created in Sidekiq asynchronously, and
the user will receive an e-mail with a link to it once
it's done.
LONG_DESC
def backup(username)
account = Account.find_local(username)
fail_with_message 'No user with such username' if account.nil?
backup = account.user.backups.create!
BackupWorker.perform_async(backup.id)
say('OK', :green)
end
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :dry_run, type: :boolean
desc 'cull [DOMAIN...]', 'Remove remote accounts that no longer exist'
long_desc <<-LONG_DESC
Query every single remote account in the database to determine
if it still exists on the origin server, and if it doesn't,
remove it from the database.
Accounts that have had confirmed activity within the last week
are excluded from the checks.
LONG_DESC
def cull(*domains)
skip_threshold = 7.days.ago
skip_domains = Concurrent::Set.new
query = Account.remote.activitypub
query = query.where(domain: domains) unless domains.empty?
processed, culled = parallelize_with_progress(query.partitioned) do |account|
next if account.updated_at >= skip_threshold || (account.last_webfingered_at.present? && account.last_webfingered_at >= skip_threshold) || skip_domains.include?(account.domain)
code = 0
begin
code = Request.new(:head, account.uri).perform(&:code)
rescue *Mastodon::HTTP_CONNECTION_ERRORS, Mastodon::PrivateNetworkAddressError
skip_domains << account.domain
end
if [404, 410].include?(code)
DeleteAccountService.new.call(account, reserve_username: false) unless dry_run?
1
else
# Touch account even during dry run to avoid getting the account into the window again
account.touch
end
end
say("Visited #{processed} accounts, removed #{culled}#{dry_run_mode_suffix}", :green)
unless skip_domains.empty?
say('The following domains were not available during the check:', :yellow)
shell.indent(2) do
skip_domains.each { |domain| say(domain) }
end
end
end
option :all, type: :boolean
option :domain
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :verbose, type: :boolean, aliases: [:v]
option :dry_run, type: :boolean
desc 'refresh [USERNAMES]', 'Fetch remote user data and files'
long_desc <<-LONG_DESC
Fetch remote user data and files for one or multiple accounts.
With the --all option, all remote accounts will be processed.
Through the --domain option, this can be narrowed down to a
specific domain only. Otherwise, remote accounts must be
specified with space-separated USERNAMES.
LONG_DESC
def refresh(*usernames)
if options[:domain] || options[:all]
scope = Account.remote
scope = scope.where(domain: options[:domain]) if options[:domain]
processed, = parallelize_with_progress(scope) do |account|
next if dry_run?
account.reset_avatar!
account.reset_header!
account.save
end
say("Refreshed #{processed} accounts#{dry_run_mode_suffix}", :green, true)
elsif !usernames.empty?
usernames.each do |user|
user, domain = user.split('@')
account = Account.find_remote(user, domain)
fail_with_message 'No such account' if account.nil?
next if dry_run?
begin
account.reset_avatar!
account.reset_header!
account.save
rescue Mastodon::UnexpectedResponseError
say("Account failed: #{user}@#{domain}", :red)
end
end
say("OK#{dry_run_mode_suffix}", :green)
else
fail_with_message 'No account(s) given'
end
end
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :verbose, type: :boolean, aliases: [:v]
desc 'follow USERNAME', 'Make all local accounts follow account specified by USERNAME'
def follow(username)
target_account = Account.find_local(username)
fail_with_message 'No such account' if target_account.nil?
processed, = parallelize_with_progress(Account.local.without_suspended) do |account|
FollowService.new.call(account, target_account, bypass_limit: true)
end
say("OK, followed target from #{processed} accounts", :green)
end
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :verbose, type: :boolean, aliases: [:v]
desc 'unfollow ACCT', 'Make all local accounts unfollow account specified by ACCT'
def unfollow(acct)
username, domain = acct.split('@')
target_account = Account.find_remote(username, domain)
fail_with_message 'No such account' if target_account.nil?
processed, = parallelize_with_progress(target_account.followers.local) do |account|
UnfollowService.new.call(account, target_account)
end
say("OK, unfollowed target from #{processed} accounts", :green)
end
option :follows, type: :boolean, default: false
option :followers, type: :boolean, default: false
desc 'reset-relationships USERNAME', 'Reset all follows and/or followers for a user'
long_desc <<-LONG_DESC
Reset all follows and/or followers for a user specified by USERNAME.
With the --follows option, the command unfollows everyone that the account follows,
and then re-follows the users that would be followed by a brand new account.
With the --followers option, the command removes all followers of the account.
LONG_DESC
def reset_relationships(username)
fail_with_message 'Please specify either --follows or --followers, or both' unless options[:follows] || options[:followers]
account = Account.find_local(username)
fail_with_message 'No such account' if account.nil?
total = 0
total += account.following.reorder(nil).count if options[:follows]
total += account.followers.reorder(nil).count if options[:followers]
progress = create_progress_bar(total)
if options[:follows]
account.following.reorder(nil).find_each do |target_account|
UnfollowService.new.call(account, target_account)
rescue => e
progress.log pastel.red("Error processing #{target_account.id}: #{e}")
ensure
progress.increment
end
BootstrapTimelineWorker.perform_async(account.id)
end
if options[:followers]
account.followers.reorder(nil).find_each do |target_account|
UnfollowService.new.call(target_account, account)
rescue => e
progress.log pastel.red("Error processing #{target_account.id}: #{e}")
ensure
progress.increment
end
end
progress.finish
say("Processed #{progress.progress} relationships", :green, true)
end
option :number, type: :numeric, aliases: [:n]
option :all, type: :boolean
desc 'approve [USERNAME]', 'Approve pending accounts'
long_desc <<~LONG_DESC
When registrations require review from staff, approve pending accounts,
either all of them with the --all option, or a specific number of them
specified with the --number (-n) option, or only a single specific
account identified by its username.
LONG_DESC
def approve(username = nil)
fail_with_message 'Number must be positive' if options[:number]&.negative?
if options[:all]
User.pending.find_each(&:approve!)
say('OK', :green)
elsif options[:number]&.positive?
User.pending.order(created_at: :asc).limit(options[:number]).each(&:approve!)
say('OK', :green)
elsif username.present?
account = Account.find_local(username)
fail_with_message 'No such account' if account.nil?
account.user&.approve!
say('OK', :green)
end
end
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :dry_run, type: :boolean
desc 'prune', 'Prune remote accounts that never interacted with local users'
long_desc <<-LONG_DESC
Prune remote account that
- follows no local accounts
- is not followed by any local accounts
- has no statuses on local
- has not been mentioned
- has not been favourited local posts
- not muted/blocked by us
LONG_DESC
def prune
_, deleted = parallelize_with_progress(prunable_accounts) do |account|
next if account.bot? || account.group?
next if account.suspended?
next if account.silenced?
account.destroy unless dry_run?
1
end
say("OK, pruned #{deleted} accounts#{dry_run_mode_suffix}", :green)
end
option :force, type: :boolean
option :replay, type: :boolean
option :target
desc 'migrate USERNAME', 'Migrate a local user to another account'
long_desc <<~LONG_DESC
With --replay, replay the last migration of the specified account, in
case some remote server may not have properly processed the associated
`Move` activity.
With --target, specify another account to migrate to.
With --force, perform the migration even if the selected account
redirects to a different account that the one specified.
LONG_DESC
def migrate(username)
fail_with_message 'Use --replay or --target, not both' if options[:replay].present? && options[:target].present?
fail_with_message 'Use either --replay or --target' if options[:replay].blank? && options[:target].blank?
account = Account.find_local(username)
fail_with_message "No such account: #{username}" if account.nil?
migration = nil
if options[:replay]
migration = account.migrations.last
fail_with_message 'The specified account has not performed any migration' if migration.nil?
fail_with_message 'The specified account is not redirecting to its last migration target. Use --force if you want to replay the migration anyway' unless options[:force] || migration.target_account_id == account.moved_to_account_id
end
if options[:target]
target_account = ResolveAccountService.new.call(options[:target])
fail_with_message "The specified target account could not be found: #{options[:target]}" if target_account.nil?
fail_with_message 'The specified account is redirecting to a different target account. Use --force if you want to change the migration target' unless options[:force] || account.moved_to_account_id.nil? || account.moved_to_account_id == target_account.id
begin
migration = account.migrations.create!(acct: target_account.acct)
rescue ActiveRecord::RecordInvalid => e
fail_with_message "Error: #{e.message}"
end
end
MoveService.new.call(migration)
say("OK, migrated #{account.acct} to #{migration.target_account.acct}", :green)
end
private
def prunable_accounts
Account
.remote
.non_automated
.where.not(referencing_account(Mention, :account_id))
.where.not(referencing_account(Favourite, :account_id))
.where.not(referencing_account(Status, :account_id))
.where.not(referencing_account(Follow, :account_id))
.where.not(referencing_account(Follow, :target_account_id))
.where.not(referencing_account(Block, :account_id))
.where.not(referencing_account(Block, :target_account_id))
.where.not(referencing_account(Mute, :target_account_id))
.where.not(referencing_account(Report, :target_account_id))
.where.not(referencing_account(FollowRequest, :account_id))
.where.not(referencing_account(FollowRequest, :target_account_id))
end
def referencing_account(model, attribute)
model
.where(model.arel_table[attribute].eq Account.arel_table[:id])
.select(1)
.arel
.exists
end
def report_errors(errors)
message = errors.map do |error|
<<~STRING
Failure/Error: #{error.attribute}
#{error.type}
STRING
end.join
fail_with_message message
end
def rotate_keys_for_account(account, delay = 0)
fail_with_message 'No such account' if account.nil?
old_key = account.keypair
new_key = OpenSSL::PKey::RSA.new(2048)
account.update(public_key: '', keypairs: [account.keypairs.build(local_fragment: '#main-key', type: :rsa, public_key: new_key.public_key.to_pem, private_key: new_key.to_pem)])
ActivityPub::UpdateDistributionWorker.perform_in(
delay,
account.id,
{
'sign_with' => {
'private_key' => old_key.private_key,
'local_fragment' => old_key.local_fragment,
'type' => old_key.type,
},
}
)
end
end
end