clear_domain_media_service.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # frozen_string_literal: true
  2. class ClearDomainMediaService < BaseService
  3. attr_reader :domain_block
  4. def call(domain_block)
  5. @domain_block = domain_block
  6. clear_media! if domain_block.reject_media?
  7. end
  8. private
  9. def invalidate_association_caches!
  10. # Normally, associated models of a status are immutable (except for accounts)
  11. # so they are aggressively cached. After updating the media attachments to no
  12. # longer point to a local file, we need to clear the cache to make those
  13. # changes appear in the API and UI
  14. @affected_status_ids.each { |id| Rails.cache.delete_matched("statuses/#{id}-*") }
  15. end
  16. def clear_media!
  17. @affected_status_ids = []
  18. begin
  19. clear_account_images!
  20. clear_account_attachments!
  21. clear_emojos!
  22. ensure
  23. invalidate_association_caches!
  24. end
  25. end
  26. def clear_account_images!
  27. blocked_domain_accounts.reorder(nil).find_each do |account|
  28. account.avatar.destroy if account.avatar&.exists?
  29. account.header.destroy if account.header&.exists?
  30. account.save
  31. end
  32. end
  33. def clear_account_attachments!
  34. media_from_blocked_domain.reorder(nil).find_each do |attachment|
  35. @affected_status_ids << attachment.status_id if attachment.status_id.present?
  36. attachment.file.destroy if attachment.file&.exists?
  37. attachment.type = :unknown
  38. attachment.save
  39. end
  40. end
  41. def clear_emojos!
  42. emojis_from_blocked_domains.destroy_all
  43. end
  44. def blocked_domain
  45. domain_block.domain
  46. end
  47. def blocked_domain_accounts
  48. Account.by_domain_and_subdomains(blocked_domain)
  49. end
  50. def media_from_blocked_domain
  51. MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
  52. end
  53. def emojis_from_blocked_domains
  54. CustomEmoji.by_domain_and_subdomains(blocked_domain)
  55. end
  56. end