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.

97 lines
2.8 KiB

  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. clear_media! if domain_block.reject_media?
  18. if domain_block.silence?
  19. silence_accounts!
  20. elsif domain_block.suspend?
  21. suspend_accounts!
  22. end
  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