about summary refs log tree commit diff
path: root/app/lib/vacuum/statuses_vacuum.rb
blob: 28c087b1c2e8eb0616a5baa89e50547513583817 (plain) (blame)
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
# frozen_string_literal: true

class Vacuum::StatusesVacuum
  include Redisable

  def initialize(retention_period)
    @retention_period = retention_period
  end

  def perform
    vacuum_statuses! if @retention_period.present?
  end

  private

  def vacuum_statuses!
    statuses_scope.in_batches do |statuses|
      # Side-effects not covered by foreign keys, such
      # as the search index, must be handled first.
      statuses.direct_visibility
              .includes(mentions: :account)
              .find_each(&:unlink_from_conversations!)
      remove_from_search_index(statuses.ids) if Chewy.enabled?

      # Foreign keys take care of most associated records for us.
      # Media attachments will be orphaned.
      statuses.delete_all
    end
  end

  def statuses_scope
    Status.unscoped.kept
          .joins(:account).merge(Account.remote)
          .where('statuses.id < ?', retention_period_as_id)
  end

  def retention_period_as_id
    Mastodon::Snowflake.id_at(@retention_period.ago, with_random: false)
  end

  def remove_from_search_index(status_ids)
    with_redis { |redis| redis.sadd('chewy:queue:StatusesIndex', status_ids) }
  end
end