about summary refs log tree commit diff
path: root/app/services/unblock_domain_service.rb
blob: c9130d90e95e95d909f5ed516d25b9deb40e95b3 (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
49
50
# frozen_string_literal: true

class UnblockDomainService < BaseService
  include Redisable

  attr_accessor :domain_block

  def call(domain_block, destroy_domain_block = true)
    @domain_block = domain_block
    process_retroactive_updates
    clear_filtered_status_cache
    domain_block.destroy if destroy_domain_block
  end

  def process_retroactive_updates
    blocked_accounts.in_batches.update_all(update_options) unless domain_block.noop?
    if @domain_block.force_sensitive?
      blocked_accounts.where(force_sensitive: true).in_batches.update_all(force_sensitive: false)
    end
  end

  def clear_filtered_status_cache
    keys = redis.keys("filtered_statuses:*")
    redis.del(*keys) unless keys.empty?
  end

  def blocked_accounts
    scope = Account.where(domain: domain_block.domain)
    if domain_block.silence?
      scope.where(silenced_at: @domain_block.created_at)
    else
      scope.where(suspended_at: @domain_block.created_at)
    end
  end

  def update_options
    { domain_block_impact => nil }
  end

  def domain_block_impact
    case @domain_block.severity
    when 'force_unlisted'
      :force_unlisted
    when 'silence'
      :silenced_at
    when 'suspend'
      :suspended_at
    end
  end
end