block_domain_service.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. class BlockDomainService < BaseService
  3. attr_reader :domain_block
  4. def call(domain_block, update = false)
  5. @domain_block = domain_block
  6. process_domain_block!
  7. process_retroactive_updates! if update
  8. end
  9. private
  10. def process_retroactive_updates!
  11. # If the domain block severity has been changed, undo the appropriate limitations
  12. scope = Account.by_domain_and_subdomains(domain_block.domain)
  13. scope.where(silenced_at: domain_block.created_at).in_batches.update_all(silenced_at: nil) unless domain_block.silence?
  14. scope.where(suspended_at: domain_block.created_at).in_batches.update_all(suspended_at: nil) unless domain_block.suspend?
  15. end
  16. def process_domain_block!
  17. if domain_block.silence?
  18. silence_accounts!
  19. elsif domain_block.suspend?
  20. suspend_accounts!
  21. end
  22. clear_media! if domain_block.reject_media?
  23. end
  24. def invalidate_association_caches!
  25. # Normally, associated models of a status are immutable (except for accounts)
  26. # so they are aggressively cached. After updating the media attachments to no
  27. # longer point to a local file, we need to clear the cache to make those
  28. # changes appear in the API and UI
  29. @affected_status_ids.each { |id| Rails.cache.delete_matched("statuses/#{id}-*") }
  30. end
  31. def silence_accounts!
  32. blocked_domain_accounts.without_silenced.in_batches.update_all(silenced_at: @domain_block.created_at)
  33. end
  34. def clear_media!
  35. @affected_status_ids = []
  36. clear_account_images!
  37. clear_account_attachments!
  38. clear_emojos!
  39. invalidate_association_caches!
  40. end
  41. def suspend_accounts!
  42. blocked_domain_accounts.without_suspended.reorder(nil).find_each do |account|
  43. SuspendAccountService.new.call(account, reserve_username: true, suspended_at: @domain_block.created_at)
  44. end
  45. end
  46. def clear_account_images!
  47. blocked_domain_accounts.reorder(nil).find_each do |account|
  48. account.avatar.destroy if account.avatar.exists?
  49. account.header.destroy if account.header.exists?
  50. account.save
  51. end
  52. end
  53. def clear_account_attachments!
  54. media_from_blocked_domain.reorder(nil).find_each do |attachment|
  55. @affected_status_ids << attachment.status_id if attachment.status_id.present?
  56. attachment.file.destroy if attachment.file.exists?
  57. attachment.type = :unknown
  58. attachment.save
  59. end
  60. end
  61. def clear_emojos!
  62. emojis_from_blocked_domains.destroy_all
  63. end
  64. def blocked_domain
  65. domain_block.domain
  66. end
  67. def blocked_domain_accounts
  68. Account.by_domain_and_subdomains(blocked_domain)
  69. end
  70. def media_from_blocked_domain
  71. MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
  72. end
  73. def emojis_from_blocked_domains
  74. CustomEmoji.by_domain_and_subdomains(blocked_domain)
  75. end
  76. end