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.

74 lines
1.5 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. clear_media! if domain_block.reject_media?
  11. if domain_block.silence?
  12. silence_accounts!
  13. elsif domain_block.suspend?
  14. suspend_accounts!
  15. end
  16. end
  17. def silence_accounts!
  18. blocked_domain_accounts.in_batches.update_all(silenced: true)
  19. end
  20. def clear_media!
  21. clear_account_images
  22. clear_account_attachments
  23. clear_emojos
  24. end
  25. def suspend_accounts!
  26. blocked_domain_accounts.where(suspended: false).find_each do |account|
  27. UnsubscribeService.new.call(account) if account.subscribed?
  28. SuspendAccountService.new.call(account)
  29. end
  30. end
  31. def clear_account_images
  32. blocked_domain_accounts.find_each do |account|
  33. account.avatar.destroy
  34. account.header.destroy
  35. account.save
  36. end
  37. end
  38. def clear_account_attachments
  39. media_from_blocked_domain.find_each do |attachment|
  40. attachment.file.destroy
  41. attachment.type = :unknown
  42. attachment.save
  43. end
  44. end
  45. def clear_emojos
  46. emojis_from_blocked_domains.destroy_all
  47. end
  48. def blocked_domain
  49. domain_block.domain
  50. end
  51. def blocked_domain_accounts
  52. Account.where(domain: blocked_domain)
  53. end
  54. def media_from_blocked_domain
  55. MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
  56. end
  57. def emojis_from_blocked_domains
  58. CustomEmoji.where(domain: blocked_domain)
  59. end
  60. end