clear_domain_media_service.rb 1.9 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!(status_ids)
  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. Rails.cache.delete_multi(status_ids.map { |id| "statuses/#{id}" })
  15. end
  16. def clear_media!
  17. clear_account_images!
  18. clear_account_attachments!
  19. clear_emojos!
  20. end
  21. def clear_account_images!
  22. blocked_domain_accounts.reorder(nil).find_each do |account|
  23. account.avatar.destroy if account.avatar&.exists?
  24. account.header.destroy if account.header&.exists?
  25. account.save
  26. end
  27. end
  28. def clear_account_attachments!
  29. media_from_blocked_domain.reorder(nil).find_in_batches do |attachments|
  30. affected_status_ids = []
  31. attachments.each do |attachment|
  32. affected_status_ids << attachment.status_id if attachment.status_id.present?
  33. attachment.file.destroy if attachment.file&.exists?
  34. attachment.type = :unknown
  35. attachment.save
  36. end
  37. invalidate_association_caches!(affected_status_ids) unless affected_status_ids.empty?
  38. end
  39. end
  40. def clear_emojos!
  41. emojis_from_blocked_domains.destroy_all
  42. end
  43. def blocked_domain
  44. domain_block.domain
  45. end
  46. def blocked_domain_accounts
  47. Account.by_domain_and_subdomains(blocked_domain)
  48. end
  49. def media_from_blocked_domain
  50. MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
  51. end
  52. def emojis_from_blocked_domains
  53. CustomEmoji.by_domain_and_subdomains(blocked_domain)
  54. end
  55. end