You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. class BlockDomainService < BaseService
  3. attr_reader :domain_block
  4. def call(domain_block)
  5. @domain_block = domain_block
  6. process_domain_block
  7. end
  8. private
  9. def process_domain_block
  10. if domain_block.silence?
  11. silence_accounts!
  12. else
  13. suspend_accounts!
  14. end
  15. end
  16. def silence_accounts!
  17. blocked_domain_accounts.in_batches.update_all(silenced: true)
  18. clear_media! if domain_block.reject_media?
  19. end
  20. def clear_media!
  21. clear_account_images
  22. clear_account_attachments
  23. end
  24. def suspend_accounts!
  25. blocked_domain_accounts.where(suspended: false).find_each do |account|
  26. account.subscription(api_subscription_url(account.id)).unsubscribe if account.subscribed?
  27. SuspendAccountService.new.call(account)
  28. end
  29. end
  30. def clear_account_images
  31. blocked_domain_accounts.find_each do |account|
  32. account.avatar.destroy
  33. account.header.destroy
  34. account.save
  35. end
  36. end
  37. def clear_account_attachments
  38. media_from_blocked_domain.find_each do |attachment|
  39. attachment.file.destroy
  40. attachment.type = :unknown
  41. attachment.save
  42. end
  43. end
  44. def blocked_domain
  45. domain_block.domain
  46. end
  47. def blocked_domain_accounts
  48. Account.where(domain: blocked_domain)
  49. end
  50. def media_from_blocked_domain
  51. MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
  52. end
  53. end