about summary refs log tree commit diff
path: root/app/workers/mark_expired_statuses_worker.rb
blob: ef39d205dbced1c2c166c5bf552326fa5e1731ae (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
45
46
47
48
# frozen_string_literal: true

class MarkExpiredStatusesWorker
  include Sidekiq::Worker

  sidekiq_options queue: 'bulk'

  def perform(account_id)
    @account = Account.find(account_id)
    return if @account&.user.nil?

    @roar_defederate = @account.user.roar_defederate
    @roar_lifespan = @account.user.roar_lifespan

    defederate = @account.user.roar_defederate_old && @roar_defederate != 0
    lifespan = @account.user.roar_lifespan_old && @roar_lifespan != 0

    return unless defederate || lifespan

    offset = 30.minutes

    @account.statuses.find_each do |status|
      modified = false

      if defederate && !status.local_only? && status.updated_at < @roar_defederate.days.ago
        status.defederate_after = offset
        modified = true
      end

      if lifespan && status.updated_at < @roar_lifespan.days.ago
        status.delete_after = offset + 30.minutes
        modified = true
      end

      if modified
        Rails.cache.delete("statuses/#{status.id}")
        offset += 1.second
      end
    end

    UserSettingsDecorator.new(@account.user).update({
      'setting_roar_defederate_old' => false,
      'setting_roar_lifespan_old' => false,
    })
  rescue ActiveRecord::RecordNotFound
    true
  end
end