diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2022-09-27 03:08:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-27 03:08:19 +0200 |
commit | 5c9abdeff1d0cf3e14d84c5ae298e6a5beccaf18 (patch) | |
tree | b4bad153eec9f2a39d96a9da342e1618ac43740b /app/lib/vacuum/statuses_vacuum.rb | |
parent | 3e0999cd1139d638332d62129dbf0b37263802fd (diff) |
Add retention policy for cached content and media (#19232)
Diffstat (limited to 'app/lib/vacuum/statuses_vacuum.rb')
-rw-r--r-- | app/lib/vacuum/statuses_vacuum.rb | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/app/lib/vacuum/statuses_vacuum.rb b/app/lib/vacuum/statuses_vacuum.rb new file mode 100644 index 000000000..41d6ba270 --- /dev/null +++ b/app/lib/vacuum/statuses_vacuum.rb @@ -0,0 +1,54 @@ +# 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? + end + + private + + def vacuum_statuses! + statuses_scope.find_in_batches do |statuses| + # Side-effects not covered by foreign keys, such + # as the search index, must be handled first. + + remove_from_account_conversations(statuses) + remove_from_search_index(statuses) + + # Foreign keys take care of most associated records + # for us. Media attachments will be orphaned. + + Status.where(id: statuses.map(&:id)).delete_all + end + end + + def statuses_scope + Status.unscoped.kept.where(account: Account.remote).where(Status.arel_table[:id].lt(retention_period_as_id)).select(:id, :visibility) + end + + def retention_period_as_id + Mastodon::Snowflake.id_at(@retention_period.ago, with_random: false) + end + + def analyze_statuses! + ActiveRecord::Base.connection.execute('ANALYZE statuses') + end + + def remove_from_account_conversations(statuses) + Status.where(id: statuses.select(&:direct_visibility?).map(&:id)).includes(:account, mentions: :account).each(&:unlink_from_conversations) + end + + def remove_from_search_index(statuses) + with_redis { |redis| redis.sadd('chewy:queue:StatusesIndex', statuses.map(&:id)) } if Chewy.enabled? + end + + def retention_period? + @retention_period.present? + end +end |