statuses_vacuum.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # frozen_string_literal: true
  2. class Vacuum::StatusesVacuum
  3. include Redisable
  4. def initialize(retention_period)
  5. @retention_period = retention_period
  6. end
  7. def perform
  8. vacuum_statuses! if @retention_period.present?
  9. end
  10. private
  11. def vacuum_statuses!
  12. statuses_scope.in_batches do |statuses|
  13. # Side-effects not covered by foreign keys, such
  14. # as the search index, must be handled first.
  15. statuses.direct_visibility
  16. .includes(mentions: :account)
  17. .find_each(&:unlink_from_conversations!)
  18. if Chewy.enabled?
  19. remove_from_index(statuses.ids, 'chewy:queue:StatusesIndex')
  20. remove_from_index(statuses.ids, 'chewy:queue:PublicStatusesIndex')
  21. end
  22. # Foreign keys take care of most associated records for us.
  23. # Media attachments will be orphaned.
  24. statuses.delete_all
  25. end
  26. end
  27. def statuses_scope
  28. Status.unscoped.kept
  29. .joins(:account).merge(Account.remote)
  30. .where(statuses: { id: ...retention_period_as_id })
  31. end
  32. def retention_period_as_id
  33. Mastodon::Snowflake.id_at(@retention_period.ago, with_random: false)
  34. end
  35. def remove_from_index(status_ids, index)
  36. with_redis { |redis| redis.sadd(index, status_ids) }
  37. end
  38. end