block_domain_service.rb 1.3 KB
Newer Older
1
2
# frozen_string_literal: true

Eugen Rochko's avatar
Eugen Rochko committed
3
class BlockDomainService < BaseService
4
5
  attr_reader :domain_block

6
  def call(domain_block)
7
8
9
10
11
12
13
    @domain_block = domain_block
    process_domain_block
  end

  private

  def process_domain_block
14
    if domain_block.silence?
15
      silence_accounts!
16
    else
17
      suspend_accounts!
18
19
20
    end
  end

21
22
23
24
  def silence_accounts!
    blocked_domain_accounts.update_all(silenced: true)
    clear_media! if domain_block.reject_media?
  end
25

26
27
28
29
30
31
32
33
34
35
  def clear_media!
    clear_account_images
    clear_account_attachments
  end

  def suspend_accounts!
    blocked_domain_accounts.where(suspended: false).find_each do |account|
      account.subscription(api_subscription_url(account.id)).unsubscribe if account.subscribed?
      SuspendAccountService.new.call(account)
    end
36
37
  end

38
39
  def clear_account_images
    blocked_domain_accounts.find_each do |account|
40
41
42
      account.avatar.destroy
      account.header.destroy
    end
43
  end
44

45
46
  def clear_account_attachments
    media_from_blocked_domain.find_each do |attachment|
47
48
49
50
      attachment.file.destroy
    end
  end

51
52
53
54
55
56
57
58
59
60
  def blocked_domain
    domain_block.domain
  end

  def blocked_domain_accounts
    Account.where(domain: blocked_domain)
  end

  def media_from_blocked_domain
    MediaAttachment.where(account: blocked_domain_accounts).reorder(nil)
Eugen Rochko's avatar
Eugen Rochko committed
61
62
  end
end