The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

75 lines
4.6 KiB

  1. # frozen_string_literal: true
  2. require_relative '../../config/boot'
  3. require_relative '../../config/environment'
  4. require_relative 'cli_helper'
  5. module Mastodon
  6. class StatusesCLI < Thor
  7. include ActionView::Helpers::NumberHelper
  8. def self.exit_on_failure?
  9. true
  10. end
  11. option :days, type: :numeric, default: 90
  12. option :clean_followed, type: :boolean
  13. desc 'remove', 'Remove unreferenced statuses'
  14. long_desc <<~LONG_DESC
  15. Remove statuses that are not referenced by local user activity, such as
  16. ones that came from relays, or belonging to users that were once followed
  17. by someone locally but no longer are.
  18. This is a computationally heavy procedure that creates extra database
  19. indices before commencing, and removes them afterward.
  20. LONG_DESC
  21. def remove
  22. say('Creating temporary database indices...')
  23. ActiveRecord::Base.connection.add_index(:accounts, :id, name: :index_accounts_local, where: 'domain is null', algorithm: :concurrently) unless ActiveRecord::Base.connection.index_name_exists?(:accounts, :index_accounts_local)
  24. ActiveRecord::Base.connection.add_index(:status_pins, :status_id, name: :index_status_pins_status_id, algorithm: :concurrently) unless ActiveRecord::Base.connection.index_name_exists?(:status_pins, :index_status_pins_status_id)
  25. ActiveRecord::Base.connection.add_index(:media_attachments, :remote_url, name: :index_media_attachments_remote_url, where: 'remote_url is not null', algorithm: :concurrently) unless ActiveRecord::Base.connection.index_name_exists?(:media_attachments, :index_media_attachments_remote_url)
  26. max_id = Mastodon::Snowflake.id_at(options[:days].days.ago)
  27. start_at = Time.now.to_f
  28. say('Beginning removal... This might take a while...')
  29. scope = Status.remote.where('id < ?', max_id)
  30. # Skip reblogs of local statuses
  31. scope = scope.where('reblog_of_id NOT IN (SELECT statuses1.id FROM statuses AS statuses1 WHERE statuses1.id = statuses.reblog_of_id AND (statuses1.uri IS NULL OR statuses1.local))')
  32. # Skip statuses that are pinned on profiles
  33. scope = scope.where('id NOT IN (SELECT status_pins.status_id FROM status_pins WHERE statuses.id = status_id)')
  34. # Skip statuses that mention local accounts
  35. scope = scope.where('id NOT IN (SELECT mentions.status_id FROM mentions WHERE statuses.id = mentions.status_id AND mentions.account_id IN (SELECT accounts.id FROM accounts WHERE domain IS NULL))')
  36. # Skip statuses which have replies
  37. scope = scope.where('id NOT IN (SELECT statuses1.in_reply_to_id FROM statuses AS statuses1 WHERE statuses.id = statuses1.in_reply_to_id)')
  38. # Skip statuses reblogged by local accounts or with recent boosts
  39. scope = scope.where('id NOT IN (SELECT statuses1.reblog_of_id FROM statuses AS statuses1 WHERE statuses.id = statuses1.reblog_of_id AND (statuses1.uri IS NULL OR statuses1.local OR statuses1.id >= ?))', max_id)
  40. # Skip statuses favourited by local users
  41. scope = scope.where('id NOT IN (SELECT favourites.status_id FROM favourites WHERE statuses.id = favourites.status_id AND favourites.account_id IN (SELECT accounts.id FROM accounts WHERE domain IS NULL))')
  42. # Skip statuses bookmarked by local users
  43. scope = scope.where('id NOT IN (SELECT bookmarks.status_id FROM bookmarks WHERE statuses.id = bookmarks.status_id AND bookmarks.account_id IN (SELECT accounts.id FROM accounts WHERE domain IS NULL))')
  44. unless options[:clean_followed]
  45. # Skip accounts followed by local accounts
  46. scope = scope.where('account_id NOT IN (SELECT follows.target_account_id FROM follows WHERE statuses.account_id = follows.target_account_id)')
  47. end
  48. scope.in_batches.delete_all
  49. say('Beginning removal of now-orphaned media attachments to free up disk space...')
  50. Scheduler::MediaCleanupScheduler.new.perform
  51. say("Done after #{Time.now.to_f - start_at}s", :green)
  52. ensure
  53. say('Removing temporary database indices to restore write performance...')
  54. ActiveRecord::Base.connection.remove_index(:accounts, name: :index_accounts_local) if ActiveRecord::Base.connection.index_name_exists?(:accounts, :index_accounts_local)
  55. ActiveRecord::Base.connection.remove_index(:status_pins, name: :index_status_pins_status_id) if ActiveRecord::Base.connection.index_name_exists?(:status_pins, :index_status_pins_status_id)
  56. ActiveRecord::Base.connection.remove_index(:media_attachments, name: :index_media_attachments_remote_url) if ActiveRecord::Base.connection.index_name_exists?(:media_attachments, :index_media_attachments_remote_url)
  57. end
  58. end
  59. end