statuses_vacuum.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. remove_from_search_index(statuses.ids) if Chewy.enabled?
  19. # Foreign keys take care of most associated records for us.
  20. # Media attachments will be orphaned.
  21. statuses.delete_all
  22. end
  23. end
  24. def statuses_scope
  25. Status.unscoped.kept
  26. .joins(:account).merge(Account.remote)
  27. .where('statuses.id < ?', retention_period_as_id)
  28. end
  29. def retention_period_as_id
  30. Mastodon::Snowflake.id_at(@retention_period.ago, with_random: false)
  31. end
  32. def remove_from_search_index(status_ids)
  33. with_redis { |redis| redis.sadd('chewy:queue:StatusesIndex', status_ids) }
  34. end
  35. end