block_domain_service.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.in_batches.update_all(silenced: true)
  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.where(suspended: false).reorder(nil).find_each do |account|
  36. UnsubscribeService.new.call(account) if account.subscribed?
  37. SuspendAccountService.new.call(account)
  38. end
  39. end
  40. def clear_account_images!
  41. blocked_domain_accounts.reorder(nil).find_each do |account|
  42. account.avatar.destroy if account.avatar.exists?
  43. account.header.destroy if account.header.exists?
  44. account.save
  45. end
  46. end
  47. def clear_account_attachments!
  48. media_from_blocked_domain.reorder(nil).find_each do |attachment|
  49. @affected_status_ids << attachment.status_id if attachment.status_id.present?
  50. attachment.file.destroy if attachment.file.exists?
  51. attachment.type = :unknown
  52. attachment.save
  53. end
  54. end
  55. def clear_emojos!
  56. emojis_from_blocked_domains.destroy_all
  57. end
  58. def blocked_domain
  59. domain_block.domain
  60. end
  61. def blocked_domain_accounts
  62. Account.where(domain: blocked_domain)
  63. end
  64. def media_from_blocked_domain
  65. MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
  66. end
  67. def emojis_from_blocked_domains
  68. CustomEmoji.where(domain: blocked_domain)
  69. end
  70. end