Browse Source

Move more tasks to tootctl (#8675)

* Move more tasks to tootctl

- tootctl feeds build
- tootctl feeds clear
- tootctl accounts refresh

Clean up exit codes and help messages

* Move user modifying to tootctl

* Improve user modification through CLI, rename commands

add -> create
mod -> modify
del -> delete

To remove ambiguity

* Fix code style issues

* Fix not being able to unset admin/mod role
Eugen Rochko 3 months ago
parent
commit
6a3f9b7e53
No account linked to committer's email address
7 changed files with 205 additions and 387 deletions
  1. 5
    0
      .rubocop.yml
  2. 5
    0
      lib/cli.rb
  3. 109
    5
      lib/mastodon/accounts_cli.rb
  4. 0
    4
      lib/mastodon/emoji_cli.rb
  5. 81
    0
      lib/mastodon/feeds_cli.rb
  6. 5
    8
      lib/mastodon/media_cli.rb
  7. 0
    370
      lib/tasks/mastodon.rake

+ 5
- 0
.rubocop.yml View File

@@ -77,6 +77,11 @@ Rails/SkipsModelValidations:
77 77
 Rails/HttpStatus:
78 78
   Enabled: false
79 79
 
80
+Rails/Exit:
81
+  Exclude:
82
+    - 'lib/mastodon/*'
83
+    - 'lib/cli'
84
+
80 85
 Style/ClassAndModuleChildren:
81 86
   Enabled: false
82 87
 

+ 5
- 0
lib/cli.rb View File

@@ -4,6 +4,8 @@ require 'thor'
4 4
 require_relative 'mastodon/media_cli'
5 5
 require_relative 'mastodon/emoji_cli'
6 6
 require_relative 'mastodon/accounts_cli'
7
+require_relative 'mastodon/feeds_cli'
8
+
7 9
 module Mastodon
8 10
   class CLI < Thor
9 11
     desc 'media SUBCOMMAND ...ARGS', 'Manage media files'
@@ -14,5 +16,8 @@ module Mastodon
14 16
 
15 17
     desc 'accounts SUBCOMMAND ...ARGS', 'Manage accounts'
16 18
     subcommand 'accounts', Mastodon::AccountsCLI
19
+
20
+    desc 'feeds SUBCOMMAND ...ARGS', 'Manage feeds'
21
+    subcommand 'feeds', Mastodon::FeedsCLI
17 22
   end
18 23
 end

+ 109
- 5
lib/mastodon/accounts_cli.rb View File

@@ -40,6 +40,7 @@ module Mastodon
40 40
         say('OK', :green)
41 41
       else
42 42
         say('No account(s) given', :red)
43
+        exit(1)
43 44
       end
44 45
     end
45 46
 
@@ -48,7 +49,7 @@ module Mastodon
48 49
     option :role, default: 'user'
49 50
     option :reattach, type: :boolean
50 51
     option :force, type: :boolean
51
-    desc 'add USERNAME', 'Create a new user'
52
+    desc 'create USERNAME', 'Create a new user'
52 53
     long_desc <<-LONG_DESC
53 54
       Create a new user account with a given USERNAME and an
54 55
       e-mail address provided with --email.
@@ -65,7 +66,7 @@ module Mastodon
65 66
       the --force option to delete the old record and reattach the
66 67
       username to the new account anyway.
67 68
     LONG_DESC
68
-    def add(username)
69
+    def create(username)
69 70
       account  = Account.new(username: username)
70 71
       password = SecureRandom.hex
71 72
       user     = User.new(email: options[:email], password: password, admin: options[:role] == 'admin', moderator: options[:role] == 'moderator', confirmed_at: Time.now.utc)
@@ -98,19 +99,75 @@ module Mastodon
98 99
           say(key)
99 100
           say('    ' + error, :red)
100 101
         end
102
+
103
+        exit(1)
101 104
       end
102 105
     end
103 106
 
104
-    desc 'del USERNAME', 'Delete a user'
107
+    option :role
108
+    option :email
109
+    option :confirm, type: :boolean
110
+    option :enable, type: :boolean
111
+    option :disable, type: :boolean
112
+    option :disable_2fa, type: :boolean
113
+    desc 'modify USERNAME', 'Modify a user'
114
+    long_desc <<-LONG_DESC
115
+      Modify a user account.
116
+
117
+      With the --role option, update the user's role to one of "user",
118
+      "moderator" or "admin".
119
+
120
+      With the --email option, update the user's e-mail address. With
121
+      the --confirm option, mark the user's e-mail as confirmed.
122
+
123
+      With the --disable option, lock the user out of their account. The
124
+      --enable option is the opposite.
125
+
126
+      With the --disable-2fa option, the two-factor authentication
127
+      requirement for the user can be removed.
128
+    LONG_DESC
129
+    def modify(username)
130
+      user = Account.find_local(username)&.user
131
+
132
+      if user.nil?
133
+        say('No user with such username', :red)
134
+        exit(1)
135
+      end
136
+
137
+      if options[:role]
138
+        user.admin = options[:role] == 'admin'
139
+        user.moderator = options[:role] == 'moderator'
140
+      end
141
+
142
+      user.email = options[:email] if options[:email]
143
+      user.disabled = false if options[:enable]
144
+      user.disabled = true if options[:disable]
145
+      user.otp_required_for_login = false if options[:disable_2fa]
146
+      user.confirm if options[:confirm]
147
+
148
+      if user.save
149
+        say('OK', :green)
150
+      else
151
+        user.errors.to_h.each do |key, error|
152
+          say('Failure/Error: ', :red)
153
+          say(key)
154
+          say('    ' + error, :red)
155
+        end
156
+
157
+        exit(1)
158
+      end
159
+    end
160
+
161
+    desc 'delete USERNAME', 'Delete a user'
105 162
     long_desc <<-LONG_DESC
106 163
       Remove a user account with a given USERNAME.
107 164
     LONG_DESC
108
-    def del(username)
165
+    def delete(username)
109 166
       account = Account.find_local(username)
110 167
 
111 168
       if account.nil?
112 169
         say('No user with such username', :red)
113
-        return
170
+        exit(1)
114 171
       end
115 172
 
116 173
       say("Deleting user with #{account.statuses_count}, this might take a while...")
@@ -182,9 +239,56 @@ module Mastodon
182 239
       end
183 240
     end
184 241
 
242
+    option :all, type: :boolean
243
+    option :domain
244
+    desc 'refresh [USERNAME]', 'Fetch remote user data and files'
245
+    long_desc <<-LONG_DESC
246
+      Fetch remote user data and files for one or multiple accounts.
247
+
248
+      With the --all option, all remote accounts will be processed.
249
+      Through the --domain option, this can be narrowed down to a
250
+      specific domain only. Otherwise, a single remote account must
251
+      be specified with USERNAME.
252
+
253
+      All processing is done in the background through Sidekiq.
254
+    LONG_DESC
255
+    def refresh(username = nil)
256
+      if options[:domain] || options[:all]
257
+        queued = 0
258
+        scope  = Account.remote
259
+        scope  = scope.where(domain: options[:domain]) if options[:domain]
260
+
261
+        scope.select(:id).reorder(nil).find_in_batches do |accounts|
262
+          Maintenance::RedownloadAccountMediaWorker.push_bulk(accounts.map(&:id))
263
+          queued += accounts.size
264
+        end
265
+
266
+        say("Scheduled refreshment of #{queued} accounts", :green, true)
267
+      elsif username.present?
268
+        username, domain = username.split('@')
269
+        account = Account.find_remote(username, domain)
270
+
271
+        if account.nil?
272
+          say('No such account', :red)
273
+          exit(1)
274
+        end
275
+
276
+        Maintenance::RedownloadAccountMediaWorker.perform_async(account.id)
277
+        say('OK', :green)
278
+      else
279
+        say('No account(s) given', :red)
280
+        exit(1)
281
+      end
282
+    end
283
+
185 284
     private
186 285
 
187 286
     def rotate_keys_for_account(account, delay = 0)
287
+      if account.nil?
288
+        say('No such account', :red)
289
+        exit(1)
290
+      end
291
+
188 292
       old_key = account.private_key
189 293
       new_key = OpenSSL::PKey::RSA.new(2048).to_pem
190 294
       account.update(private_key: new_key)

+ 0
- 4
lib/mastodon/emoji_cli.rb View File

@@ -5,8 +5,6 @@ require_relative '../../config/boot'
5 5
 require_relative '../../config/environment'
6 6
 require_relative 'cli_helper'
7 7
 
8
-# rubocop:disable Rails/Output
9
-
10 8
 module Mastodon
11 9
   class EmojiCLI < Thor
12 10
     option :prefix
@@ -77,5 +75,3 @@ module Mastodon
77 75
     end
78 76
   end
79 77
 end
80
-
81
-# rubocop:enable Rails/Output

+ 81
- 0
lib/mastodon/feeds_cli.rb View File

@@ -0,0 +1,81 @@
1
+# frozen_string_literal: true
2
+
3
+require_relative '../../config/boot'
4
+require_relative '../../config/environment'
5
+require_relative 'cli_helper'
6
+
7
+module Mastodon
8
+  class FeedsCLI < Thor
9
+    option :all, type: :boolean, default: false
10
+    option :background, type: :boolean, default: false
11
+    option :dry_run, type: :boolean, default: false
12
+    option :verbose, type: :boolean, default: false
13
+    desc 'build [USERNAME]', 'Build home and list feeds for one or all users'
14
+    long_desc <<-LONG_DESC
15
+      Build home and list feeds that are stored in Redis from the database.
16
+
17
+      With the --all option, all active users will be processed.
18
+      Otherwise, a single user specified by USERNAME.
19
+
20
+      With the --background option, regeneration will be queued into Sidekiq,
21
+      and the command will exit as soon as possible.
22
+
23
+      With the --dry-run option, no work will be done.
24
+
25
+      With the --verbose option, when accounts are processed sequentially in the
26
+      foreground, the IDs of the accounts will be printed.
27
+    LONG_DESC
28
+    def build(username = nil)
29
+      dry_run = options[:dry_run] ? '(DRY RUN)' : ''
30
+
31
+      if options[:all] || username.nil?
32
+        processed = 0
33
+        queued    = 0
34
+
35
+        User.active.select(:id, :account_id).reorder(nil).find_in_batches do |users|
36
+          if options[:background]
37
+            RegenerationWorker.push_bulk(users.map(&:account_id)) unless options[:dry_run]
38
+            queued += users.size
39
+          else
40
+            users.each do |user|
41
+              RegenerationWorker.new.perform(user.account_id) unless options[:dry_run]
42
+              options[:verbose] ? say(user.account_id) : say('.', :green, false)
43
+              processed += 1
44
+            end
45
+          end
46
+        end
47
+
48
+        if options[:background]
49
+          say("Scheduled feed regeneration for #{queued} accounts #{dry_run}", :green, true)
50
+        else
51
+          say
52
+          say("Regenerated feeds for #{processed} accounts #{dry_run}", :green, true)
53
+        end
54
+      elsif username.present?
55
+        account = Account.find_local(username)
56
+
57
+        if options[:background]
58
+          RegenerationWorker.perform_async(account.id) unless options[:dry_run]
59
+        else
60
+          RegenerationWorker.new.perform(account.id) unless options[:dry_run]
61
+        end
62
+
63
+        say("OK #{dry_run}", :green, true)
64
+      else
65
+        say('No account(s) given', :red)
66
+        exit(1)
67
+      end
68
+    end
69
+
70
+    desc 'clear', 'Remove all home and list feeds from Redis'
71
+    def clear
72
+      keys = Redis.current.keys('feed:*')
73
+
74
+      Redis.current.pipelined do
75
+        keys.each { |key| Redis.current.del(key) }
76
+      end
77
+
78
+      say('OK', :green)
79
+    end
80
+  end
81
+end

+ 5
- 8
lib/mastodon/media_cli.rb View File

@@ -4,8 +4,6 @@ require_relative '../../config/boot'
4 4
 require_relative '../../config/environment'
5 5
 require_relative 'cli_helper'
6 6
 
7
-# rubocop:disable Rails/Output
8
-
9 7
 module Mastodon
10 8
   class MediaCLI < Thor
11 9
     option :days, type: :numeric, default: 7
@@ -25,9 +23,10 @@ module Mastodon
25 23
       it may impact other operations of the Mastodon server, and it may overload
26 24
       the underlying file storage.
27 25
 
28
-      With the --verbose option, output deleting file ID to console (only when --background false).
26
+      With the --dry-run option, no work will be done.
29 27
 
30
-      With the --dry-run option, output the number of files to delete without deleting.
28
+      With the --verbose option, when media attachments are processed sequentially in the
29
+      foreground, the IDs of the media attachments will be printed.
31 30
     DESC
32 31
     def remove
33 32
       time_ago  = options[:days].days.ago
@@ -53,12 +52,10 @@ module Mastodon
53 52
       say
54 53
 
55 54
       if options[:background]
56
-        say("Scheduled the deletion of #{queued} media attachments #{dry_run}.", :green)
55
+        say("Scheduled the deletion of #{queued} media attachments #{dry_run}", :green, true)
57 56
       else
58
-        say("Removed #{processed} media attachments #{dry_run}.", :green)
57
+        say("Removed #{processed} media attachments #{dry_run}", :green, true)
59 58
       end
60 59
     end
61 60
   end
62 61
 end
63
-
64
-# rubocop:enable Rails/Output

+ 0
- 370
lib/tasks/mastodon.rake View File

@@ -390,139 +390,6 @@ namespace :mastodon do
390 390
     end
391 391
   end
392 392
 
393
-  desc 'Turn a user into an admin, identified by the USERNAME environment variable'
394
-  task make_admin: :environment do
395
-    include RoutingHelper
396
-
397
-    account_username = ENV.fetch('USERNAME')
398
-    user             = User.joins(:account).where(accounts: { username: account_username })
399
-
400
-    if user.present?
401
-      user.update(admin: true)
402
-      puts "Congrats! #{account_username} is now an admin. \\o/\nNavigate to #{edit_admin_settings_url} to get started"
403
-    else
404
-      puts "User could not be found; please make sure an account with the `#{account_username}` username exists."
405
-    end
406
-  end
407
-
408
-  desc 'Turn a user into a moderator, identified by the USERNAME environment variable'
409
-  task make_mod: :environment do
410
-    account_username = ENV.fetch('USERNAME')
411
-    user             = User.joins(:account).where(accounts: { username: account_username })
412
-
413
-    if user.present?
414
-      user.update(moderator: true)
415
-      puts "Congrats! #{account_username} is now a moderator \\o/"
416
-    else
417
-      puts "User could not be found; please make sure an account with the `#{account_username}` username exists."
418
-    end
419
-  end
420
-
421
-  desc 'Remove admin and moderator privileges from user identified by the USERNAME environment variable'
422
-  task revoke_staff: :environment do
423
-    account_username = ENV.fetch('USERNAME')
424
-    user             = User.joins(:account).where(accounts: { username: account_username })
425
-
426
-    if user.present?
427
-      user.update(moderator: false, admin: false)
428
-      puts "#{account_username} is no longer admin or moderator."
429
-    else
430
-      puts "User could not be found; please make sure an account with the `#{account_username}` username exists."
431
-    end
432
-  end
433
-
434
-  desc 'Manually confirms a user with associated user email address stored in USER_EMAIL environment variable.'
435
-  task confirm_email: :environment do
436
-    email = ENV.fetch('USER_EMAIL')
437
-    user  = User.find_by(email: email)
438
-
439
-    if user
440
-      user.update(confirmed_at: Time.now.utc)
441
-      puts "#{email} confirmed"
442
-    else
443
-      abort "#{email} not found"
444
-    end
445
-  end
446
-
447
-  desc 'Add a user by providing their email, username and initial password.' \
448
-       'The user will receive a confirmation email, then they must reset their password before logging in.'
449
-  task add_user: :environment do
450
-    disable_log_stdout!
451
-
452
-    prompt = TTY::Prompt.new
453
-
454
-    begin
455
-      email = prompt.ask('E-mail:', required: true) do |q|
456
-        q.modify :strip
457
-      end
458
-
459
-      username = prompt.ask('Username:', required: true) do |q|
460
-        q.modify :strip
461
-      end
462
-
463
-      role = prompt.select('Role:', %w(user moderator admin))
464
-
465
-      if prompt.yes?('Proceed to create the user?')
466
-        user = User.new(email: email, password: SecureRandom.hex, admin: role == 'admin', moderator: role == 'moderator', account_attributes: { username: username })
467
-
468
-        if user.save
469
-          prompt.ok 'User created and confirmation mail sent to the user\'s email address.'
470
-          prompt.ok "Here is the random password generated for the user: #{user.password}"
471
-        else
472
-          prompt.warn 'User was not created because of the following errors:'
473
-
474
-          user.errors.each do |key, val|
475
-            prompt.error "#{key}: #{val}"
476
-          end
477
-        end
478
-      else
479
-        prompt.ok 'Aborting. Bye!'
480
-      end
481
-    rescue TTY::Reader::InputInterrupt
482
-      prompt.ok 'Aborting. Bye!'
483
-    end
484
-  end
485
-
486
-  namespace :media do
487
-    desc 'Remove media attachments attributed to silenced accounts'
488
-    task remove_silenced: :environment do
489
-      nb_media_attachments = 0
490
-      MediaAttachment.where(account: Account.silenced).select(:id).reorder(nil).find_in_batches do |media_attachments|
491
-        nb_media_attachments += media_attachments.length
492
-        Maintenance::DestroyMediaWorker.push_bulk(media_attachments.map(&:id))
493
-      end
494
-      puts "Scheduled the deletion of #{nb_media_attachments} media attachments"
495
-    end
496
-
497
-    desc 'Remove cached remote media attachments that are older than NUM_DAYS. By default 7 (week)'
498
-    task remove_remote: :environment do
499
-      puts 'Please use `./bin/tootctl media remove --help` directly'.colorize(:yellow)
500
-      require_relative '../mastodon/media_cli'
501
-      cli = Mastodon::MediaCLI.new([], days: (ENV['NUM_DAYS'] || 7).to_i)
502
-      cli.invoke(:remove)
503
-    end
504
-
505
-    desc 'Set unknown attachment type for remote-only attachments'
506
-    task set_unknown: :environment do
507
-      puts 'Setting unknown attachment type for remote-only attachments...'
508
-      MediaAttachment.where(file_file_name: nil).where.not(type: :unknown).in_batches.update_all(type: :unknown)
509
-      puts 'Done!'
510
-    end
511
-
512
-    desc 'Redownload avatars/headers of remote users. Optionally limit to a particular domain with DOMAIN'
513
-    task redownload_avatars: :environment do
514
-      accounts = Account.remote
515
-      accounts = accounts.where(domain: ENV['DOMAIN']) if ENV['DOMAIN'].present?
516
-      nb_accounts = 0
517
-
518
-      accounts.select(:id).reorder(nil).find_in_batches do |accounts_batch|
519
-        nb_accounts += accounts_batch.length
520
-        Maintenance::RedownloadAccountMediaWorker.push_bulk(accounts_batch.map(&:id))
521
-      end
522
-      puts "Scheduled the download of avatars/headers for #{nb_accounts} remote users"
523
-    end
524
-  end
525
-
526 393
   namespace :push do
527 394
     desc 'Unsubscribes from PuSH updates of feeds nobody follows locally'
528 395
     task clear: :environment do
@@ -530,28 +397,6 @@ namespace :mastodon do
530 397
     end
531 398
   end
532 399
 
533
-  namespace :feeds do
534
-    desc 'Clear all timelines without regenerating them'
535
-    task clear_all: :environment do
536
-      Redis.current.keys('feed:*').each { |key| Redis.current.del(key) }
537
-    end
538
-
539
-    desc 'Generates home timelines for users who logged in in the past two weeks'
540
-    task build: :environment do
541
-      User.active.select(:id, :account_id).reorder(nil).find_in_batches do |users|
542
-        RegenerationWorker.push_bulk(users.map(&:account_id))
543
-      end
544
-    end
545
-  end
546
-
547
-  namespace :users do
548
-    desc 'List e-mails of all admin users'
549
-    task admins: :environment do
550
-      puts 'Admin user emails:'
551
-      puts User.admins.map(&:email).join("\n")
552
-    end
553
-  end
554
-
555 400
   namespace :settings do
556 401
     desc 'Open registrations on this instance'
557 402
     task open_registrations: :environment do
@@ -572,221 +417,6 @@ namespace :mastodon do
572 417
       puts "VAPID_PUBLIC_KEY=#{vapid_key.public_key}"
573 418
     end
574 419
   end
575
-
576
-  namespace :maintenance do
577
-    desc 'Update counter caches'
578
-    task update_counter_caches: :environment do
579
-      puts 'Updating counter caches for accounts...'
580
-
581
-      Account.unscoped.where.not(protocol: :activitypub).select('id').find_in_batches do |batch|
582
-        Account.where(id: batch.map(&:id)).update_all('statuses_count = (select count(*) from statuses where account_id = accounts.id), followers_count = (select count(*) from follows where target_account_id = accounts.id), following_count = (select count(*) from follows where account_id = accounts.id)')
583
-      end
584
-
585
-      puts 'Updating counter caches for statuses...'
586
-
587
-      Status.unscoped.select('id').find_in_batches do |batch|
588
-        Status.where(id: batch.map(&:id)).update_all('favourites_count = (select count(*) from favourites where favourites.status_id = statuses.id), reblogs_count = (select count(*) from statuses as reblogs where reblogs.reblog_of_id = statuses.id)')
589
-      end
590
-
591
-      puts 'Done!'
592
-    end
593
-
594
-    desc 'Generate static versions of GIF avatars/headers'
595
-    task add_static_avatars: :environment do
596
-      puts 'Generating static avatars/headers for GIF ones...'
597
-
598
-      Account.unscoped.where(avatar_content_type: 'image/gif').or(Account.unscoped.where(header_content_type: 'image/gif')).find_each do |account|
599
-        begin
600
-          account.avatar.reprocess! if account.avatar_content_type == 'image/gif' && !account.avatar.exists?(:static)
601
-          account.header.reprocess! if account.header_content_type == 'image/gif' && !account.header.exists?(:static)
602
-        rescue StandardError => e
603
-          Rails.logger.error "Error while generating static avatars/headers for account #{account.id}: #{e}"
604
-          next
605
-        end
606
-      end
607
-
608
-      puts 'Done!'
609
-    end
610
-
611
-    desc 'Ensure referencial integrity'
612
-    task prepare_for_foreign_keys: :environment do
613
-      # All the deletes:
614
-      ActiveRecord::Base.connection.execute('DELETE FROM statuses USING statuses s LEFT JOIN accounts a ON a.id = s.account_id WHERE statuses.id = s.id AND a.id IS NULL')
615
-
616
-      if ActiveRecord::Base.connection.table_exists? :account_domain_blocks
617
-        ActiveRecord::Base.connection.execute('DELETE FROM account_domain_blocks USING account_domain_blocks adb LEFT JOIN accounts a ON a.id = adb.account_id WHERE account_domain_blocks.id = adb.id AND a.id IS NULL')
618
-      end
619
-
620
-      if ActiveRecord::Base.connection.table_exists? :conversation_mutes
621
-        ActiveRecord::Base.connection.execute('DELETE FROM conversation_mutes USING conversation_mutes cm LEFT JOIN accounts a ON a.id = cm.account_id WHERE conversation_mutes.id = cm.id AND a.id IS NULL')
622
-        ActiveRecord::Base.connection.execute('DELETE FROM conversation_mutes USING conversation_mutes cm LEFT JOIN conversations c ON c.id = cm.conversation_id WHERE conversation_mutes.id = cm.id AND c.id IS NULL')
623
-      end
624
-
625
-      ActiveRecord::Base.connection.execute('DELETE FROM favourites USING favourites f LEFT JOIN accounts a ON a.id = f.account_id WHERE favourites.id = f.id AND a.id IS NULL')
626
-      ActiveRecord::Base.connection.execute('DELETE FROM favourites USING favourites f LEFT JOIN statuses s ON s.id = f.status_id WHERE favourites.id = f.id AND s.id IS NULL')
627
-      ActiveRecord::Base.connection.execute('DELETE FROM blocks USING blocks b LEFT JOIN accounts a ON a.id = b.account_id WHERE blocks.id = b.id AND a.id IS NULL')
628
-      ActiveRecord::Base.connection.execute('DELETE FROM blocks USING blocks b LEFT JOIN accounts a ON a.id = b.target_account_id WHERE blocks.id = b.id AND a.id IS NULL')
629
-      ActiveRecord::Base.connection.execute('DELETE FROM follow_requests USING follow_requests fr LEFT JOIN accounts a ON a.id = fr.account_id WHERE follow_requests.id = fr.id AND a.id IS NULL')
630
-      ActiveRecord::Base.connection.execute('DELETE FROM follow_requests USING follow_requests fr LEFT JOIN accounts a ON a.id = fr.target_account_id WHERE follow_requests.id = fr.id AND a.id IS NULL')
631
-      ActiveRecord::Base.connection.execute('DELETE FROM follows USING follows f LEFT JOIN accounts a ON a.id = f.account_id WHERE follows.id = f.id AND a.id IS NULL')
632
-      ActiveRecord::Base.connection.execute('DELETE FROM follows USING follows f LEFT JOIN accounts a ON a.id = f.target_account_id WHERE follows.id = f.id AND a.id IS NULL')
633
-      ActiveRecord::Base.connection.execute('DELETE FROM mutes USING mutes m LEFT JOIN accounts a ON a.id = m.account_id WHERE mutes.id = m.id AND a.id IS NULL')
634
-      ActiveRecord::Base.connection.execute('DELETE FROM mutes USING mutes m LEFT JOIN accounts a ON a.id = m.target_account_id WHERE mutes.id = m.id AND a.id IS NULL')
635
-      ActiveRecord::Base.connection.execute('DELETE FROM imports USING imports i LEFT JOIN accounts a ON a.id = i.account_id WHERE imports.id = i.id AND a.id IS NULL')
636
-      ActiveRecord::Base.connection.execute('DELETE FROM mentions USING mentions m LEFT JOIN accounts a ON a.id = m.account_id WHERE mentions.id = m.id AND a.id IS NULL')
637
-      ActiveRecord::Base.connection.execute('DELETE FROM mentions USING mentions m LEFT JOIN statuses s ON s.id = m.status_id WHERE mentions.id = m.id AND s.id IS NULL')
638
-      ActiveRecord::Base.connection.execute('DELETE FROM notifications USING notifications n LEFT JOIN accounts a ON a.id = n.account_id WHERE notifications.id = n.id AND a.id IS NULL')
639
-      ActiveRecord::Base.connection.execute('DELETE FROM notifications USING notifications n LEFT JOIN accounts a ON a.id = n.from_account_id WHERE notifications.id = n.id AND a.id IS NULL')
640
-      ActiveRecord::Base.connection.execute('DELETE FROM preview_cards USING preview_cards pc LEFT JOIN statuses s ON s.id = pc.status_id WHERE preview_cards.id = pc.id AND s.id IS NULL')
641
-      ActiveRecord::Base.connection.execute('DELETE FROM reports USING reports r LEFT JOIN accounts a ON a.id = r.account_id WHERE reports.id = r.id AND a.id IS NULL')
642
-      ActiveRecord::Base.connection.execute('DELETE FROM reports USING reports r LEFT JOIN accounts a ON a.id = r.target_account_id WHERE reports.id = r.id AND a.id IS NULL')
643
-      ActiveRecord::Base.connection.execute('DELETE FROM statuses_tags USING statuses_tags st LEFT JOIN statuses s ON s.id = st.status_id WHERE statuses_tags.tag_id = st.tag_id AND statuses_tags.status_id = st.status_id AND s.id IS NULL')
644
-      ActiveRecord::Base.connection.execute('DELETE FROM statuses_tags USING statuses_tags st LEFT JOIN tags t ON t.id = st.tag_id WHERE statuses_tags.tag_id = st.tag_id AND statuses_tags.status_id = st.status_id AND t.id IS NULL')
645
-      ActiveRecord::Base.connection.execute('DELETE FROM stream_entries USING stream_entries se LEFT JOIN accounts a ON a.id = se.account_id WHERE stream_entries.id = se.id AND a.id IS NULL')
646
-      ActiveRecord::Base.connection.execute('DELETE FROM subscriptions USING subscriptions s LEFT JOIN accounts a ON a.id = s.account_id WHERE subscriptions.id = s.id AND a.id IS NULL')
647
-      ActiveRecord::Base.connection.execute('DELETE FROM users USING users u LEFT JOIN accounts a ON a.id = u.account_id WHERE users.id = u.id AND a.id IS NULL')
648
-      ActiveRecord::Base.connection.execute('DELETE FROM web_settings USING web_settings ws LEFT JOIN users u ON u.id = ws.user_id WHERE web_settings.id = ws.id AND u.id IS NULL')
649
-      ActiveRecord::Base.connection.execute('DELETE FROM oauth_access_grants USING oauth_access_grants oag LEFT JOIN users u ON u.id = oag.resource_owner_id WHERE oauth_access_grants.id = oag.id AND oag.resource_owner_id IS NOT NULL AND u.id IS NULL')
650
-      ActiveRecord::Base.connection.execute('DELETE FROM oauth_access_grants USING oauth_access_grants oag LEFT JOIN oauth_applications a ON a.id = oag.application_id WHERE oauth_access_grants.id = oag.id AND oag.application_id IS NOT NULL AND a.id IS NULL')
651
-      ActiveRecord::Base.connection.execute('DELETE FROM oauth_access_tokens USING oauth_access_tokens oat LEFT JOIN users u ON u.id = oat.resource_owner_id WHERE oauth_access_tokens.id = oat.id AND oat.resource_owner_id IS NOT NULL AND u.id IS NULL')
652
-      ActiveRecord::Base.connection.execute('DELETE FROM oauth_access_tokens USING oauth_access_tokens oat LEFT JOIN oauth_applications a ON a.id = oat.application_id WHERE oauth_access_tokens.id = oat.id AND oat.application_id IS NOT NULL AND a.id IS NULL')
653
-
654
-      # All the nullifies:
655
-      ActiveRecord::Base.connection.execute('UPDATE statuses SET in_reply_to_id = NULL FROM statuses s LEFT JOIN statuses rs ON rs.id = s.in_reply_to_id WHERE statuses.id = s.id AND s.in_reply_to_id IS NOT NULL AND rs.id IS NULL')
656
-      ActiveRecord::Base.connection.execute('UPDATE statuses SET in_reply_to_account_id = NULL FROM statuses s LEFT JOIN accounts a ON a.id = s.in_reply_to_account_id WHERE statuses.id = s.id AND s.in_reply_to_account_id IS NOT NULL AND a.id IS NULL')
657
-      ActiveRecord::Base.connection.execute('UPDATE media_attachments SET status_id = NULL FROM media_attachments ma LEFT JOIN statuses s ON s.id = ma.status_id WHERE media_attachments.id = ma.id AND ma.status_id IS NOT NULL AND s.id IS NULL')
658
-      ActiveRecord::Base.connection.execute('UPDATE media_attachments SET account_id = NULL FROM media_attachments ma LEFT JOIN accounts a ON a.id = ma.account_id WHERE media_attachments.id = ma.id AND ma.account_id IS NOT NULL AND a.id IS NULL')
659
-      ActiveRecord::Base.connection.execute('UPDATE reports SET action_taken_by_account_id = NULL FROM reports r LEFT JOIN accounts a ON a.id = r.action_taken_by_account_id WHERE reports.id = r.id AND r.action_taken_by_account_id IS NOT NULL AND a.id IS NULL')
660
-    end
661
-
662
-    desc 'Remove deprecated preview cards'
663
-    task remove_deprecated_preview_cards: :environment do
664
-      next unless ActiveRecord::Base.connection.table_exists? 'deprecated_preview_cards'
665
-
666
-      class DeprecatedPreviewCard < ActiveRecord::Base
667
-        self.inheritance_column = false
668
-
669
-        path = '/preview_cards/:attachment/:id_partition/:style/:filename'
670
-        if ENV['S3_ENABLED'] != 'true'
671
-          path = (ENV['PAPERCLIP_ROOT_PATH'] || ':rails_root/public/system') + path
672
-        end
673
-
674
-        has_attached_file :image, styles: { original: '280x120>' }, convert_options: { all: '-quality 80 -strip' }, path: path
675
-      end
676
-
677
-      puts 'Delete records and associated files from deprecated preview cards? [y/N]: '
678
-      confirm = STDIN.gets.chomp
679
-
680
-      if confirm.casecmp('y').zero?
681
-        DeprecatedPreviewCard.in_batches.destroy_all
682
-
683
-        puts 'Drop deprecated preview cards table? [y/N]: '
684
-        confirm = STDIN.gets.chomp
685
-
686
-        if confirm.casecmp('y').zero?
687
-          ActiveRecord::Migration.drop_table :deprecated_preview_cards
688
-        end
689
-      end
690
-    end
691
-
692
-    desc 'Migrate photo preview cards made before 2.1'
693
-    task migrate_photo_preview_cards: :environment do
694
-      status_ids = Status.joins(:preview_cards)
695
-                         .where(preview_cards: { embed_url: '', type: :photo })
696
-                         .reorder(nil)
697
-                         .group(:id)
698
-                         .pluck(:id)
699
-
700
-      PreviewCard.where(embed_url: '', type: :photo).delete_all
701
-      LinkCrawlWorker.push_bulk status_ids
702
-    end
703
-
704
-    desc 'Find case-insensitive username duplicates of local users'
705
-    task find_duplicate_usernames: :environment do
706
-      include RoutingHelper
707
-
708
-      disable_log_stdout!
709
-
710
-      duplicate_masters = Account.find_by_sql('SELECT * FROM accounts WHERE id IN (SELECT min(id) FROM accounts WHERE domain IS NULL GROUP BY lower(username) HAVING count(*) > 1)')
711
-      pastel = Pastel.new
712
-
713
-      duplicate_masters.each do |account|
714
-        puts pastel.yellow('First of their name: ') + pastel.bold(account.username) + " (#{admin_account_url(account.id)})"
715
-
716
-        Account.where('lower(username) = ?', account.username.downcase).where.not(id: account.id).each do |duplicate|
717
-          puts '  ' + pastel.red('Duplicate: ') + admin_account_url(duplicate.id)
718
-        end
719
-      end
720
-    end
721
-
722
-    desc 'Remove all home feed regeneration markers'
723
-    task remove_regeneration_markers: :environment do
724
-      keys = Redis.current.keys('account:*:regeneration')
725
-
726
-      Redis.current.pipelined do
727
-        keys.each { |key| Redis.current.del(key) }
728
-      end
729
-    end
730
-
731
-    desc 'Check every known remote account and delete those that no longer exist in origin'
732
-    task purge_removed_accounts: :environment do
733
-      prepare_for_options!
734
-
735
-      options = {}
736
-
737
-      OptionParser.new do |opts|
738
-        opts.banner = 'Usage: rails mastodon:maintenance:purge_removed_accounts [options]'
739
-
740
-        opts.on('-f', '--force', 'Remove all encountered accounts without asking for confirmation') do
741
-          options[:force] = true
742
-        end
743
-
744
-        opts.on('-h', '--help', 'Display this message') do
745
-          puts opts
746
-          exit
747
-        end
748
-      end.parse!
749
-
750
-      disable_log_stdout!
751
-
752
-      total        = Account.remote.where(protocol: :activitypub).count
753
-      progress_bar = ProgressBar.create(total: total, format: '%c/%C |%w>%i| %e')
754
-
755
-      Account.remote.where(protocol: :activitypub).partitioned.find_each do |account|
756
-        progress_bar.increment
757
-
758
-        begin
759
-          code = Request.new(:head, account.uri).perform(&:code)
760
-        rescue StandardError
761
-          # This could happen due to network timeout, DNS timeout, wrong SSL cert, etc,
762
-          # which should probably not lead to perceiving the account as deleted, so
763
-          # just skip till next time
764
-          next
765
-        end
766
-
767
-        if [404, 410].include?(code)
768
-          if options[:force]
769
-            SuspendAccountService.new.call(account)
770
-            account.destroy
771
-          else
772
-            progress_bar.pause
773
-            progress_bar.clear
774
-            print "\nIt seems like #{account.acct} no longer exists. Purge the account from the database? [Y/n]: ".colorize(:yellow)
775
-            confirm = STDIN.gets.chomp
776
-            puts ''
777
-            progress_bar.resume
778
-
779
-            if confirm.casecmp('n').zero?
780
-              next
781
-            else
782
-              SuspendAccountService.new.call(account)
783
-              account.destroy
784
-            end
785
-          end
786
-        end
787
-      end
788
-    end
789
-  end
790 420
 end
791 421
 
792 422
 def disable_log_stdout!