block_domain_service.rb 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. @domain_block_event = nil
  7. process_domain_block!
  8. process_retroactive_updates! if update
  9. notify_of_severed_relationships!
  10. end
  11. private
  12. def process_retroactive_updates!
  13. # If the domain block severity has been changed, undo the appropriate limitations
  14. scope = Account.by_domain_and_subdomains(domain_block.domain)
  15. scope.where(silenced_at: domain_block.created_at).in_batches.update_all(silenced_at: nil) unless domain_block.silence?
  16. scope.where(suspended_at: domain_block.created_at).in_batches.update_all(suspended_at: nil, suspension_origin: nil) unless domain_block.suspend?
  17. end
  18. def process_domain_block!
  19. if domain_block.silence?
  20. silence_accounts!
  21. elsif domain_block.suspend?
  22. suspend_accounts!
  23. end
  24. DomainClearMediaWorker.perform_async(domain_block.id) if domain_block.reject_media?
  25. end
  26. def silence_accounts!
  27. blocked_domain_accounts.without_silenced.in_batches.update_all(silenced_at: @domain_block.created_at)
  28. end
  29. def suspend_accounts!
  30. blocked_domain_accounts.without_suspended.in_batches.update_all(suspended_at: @domain_block.created_at, suspension_origin: :local)
  31. blocked_domain_accounts.where(suspended_at: @domain_block.created_at).reorder(nil).find_each do |account|
  32. DeleteAccountService.new.call(account, reserve_username: true, suspended_at: @domain_block.created_at, relationship_severance_event: domain_block_event)
  33. end
  34. end
  35. def notify_of_severed_relationships!
  36. return if @domain_block_event.nil?
  37. # TODO: check how efficient that query is, also check `push_bulk`/`perform_bulk`
  38. @domain_block_event.affected_local_accounts.reorder(nil).find_each do |account|
  39. event = AccountRelationshipSeveranceEvent.create!(account: account, relationship_severance_event: @domain_block_event)
  40. LocalNotificationWorker.perform_async(account.id, event.id, 'AccountRelationshipSeveranceEvent', 'severed_relationships')
  41. end
  42. end
  43. def blocked_domain
  44. domain_block.domain
  45. end
  46. def domain_block_event
  47. @domain_block_event ||= RelationshipSeveranceEvent.create!(type: :domain_block, target_name: blocked_domain)
  48. end
  49. def blocked_domain_accounts
  50. Account.by_domain_and_subdomains(blocked_domain)
  51. end
  52. end