block_domain_service.rb 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 invalidate_association_caches!
  18. # Normally, associated models of a status are immutable (except for accounts)
  19. # so they are aggressively cached. After updating the media attachments to no
  20. # longer point to a local file, we need to clear the cache to make those
  21. # changes appear in the API and UI
  22. @affected_status_ids.each { |id| Rails.cache.delete_matched("statuses/#{id}-*") }
  23. end
  24. def silence_accounts!
  25. blocked_domain_accounts.without_silenced.in_batches.update_all(silenced_at: @domain_block.created_at)
  26. end
  27. def clear_media!
  28. @affected_status_ids = []
  29. clear_account_images!
  30. clear_account_attachments!
  31. clear_emojos!
  32. invalidate_association_caches!
  33. end
  34. def suspend_accounts!
  35. blocked_domain_accounts.without_suspended.reorder(nil).find_each do |account|
  36. SuspendAccountService.new.call(account, suspended_at: @domain_block.created_at)
  37. end
  38. end
  39. def clear_account_images!
  40. blocked_domain_accounts.reorder(nil).find_each do |account|
  41. account.avatar.destroy if account.avatar.exists?
  42. account.header.destroy if account.header.exists?
  43. account.save
  44. end
  45. end
  46. def clear_account_attachments!
  47. media_from_blocked_domain.reorder(nil).find_each do |attachment|
  48. @affected_status_ids << attachment.status_id if attachment.status_id.present?
  49. attachment.file.destroy if attachment.file.exists?
  50. attachment.type = :unknown
  51. attachment.save
  52. end
  53. end
  54. def clear_emojos!
  55. emojis_from_blocked_domains.destroy_all
  56. end
  57. def blocked_domain
  58. domain_block.domain
  59. end
  60. def blocked_domain_accounts
  61. Account.by_domain_and_subdomains(blocked_domain)
  62. end
  63. def media_from_blocked_domain
  64. MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
  65. end
  66. def emojis_from_blocked_domains
  67. CustomEmoji.by_domain_and_subdomains(blocked_domain)
  68. end
  69. end