after_block_domain_from_account_service.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # frozen_string_literal: true
  2. class AfterBlockDomainFromAccountService < BaseService
  3. include Payloadable
  4. # This service does not create an AccountDomainBlock record,
  5. # it's meant to be called after such a record has been created
  6. # synchronously, to "clean up"
  7. def call(account, domain)
  8. @account = account
  9. @domain = domain
  10. @domain_block_event = nil
  11. clear_notifications!
  12. clear_notification_permissions!
  13. remove_follows!
  14. reject_existing_followers!
  15. reject_pending_follow_requests!
  16. notify_of_severed_relationships!
  17. end
  18. private
  19. def remove_follows!
  20. @account.active_relationships.where(target_account: Account.where(domain: @domain)).includes(:target_account).reorder(nil).in_batches do |follows|
  21. domain_block_event.import_from_active_follows!(follows)
  22. follows.each { |follow| UnfollowService.new.call(@account, follow.target_account) }
  23. end
  24. end
  25. def clear_notifications!
  26. Notification.where(account: @account).where(from_account: Account.where(domain: @domain)).in_batches.delete_all
  27. end
  28. def clear_notification_permissions!
  29. NotificationPermission.where(account: @account, from_account: Account.where(domain: @domain)).in_batches.delete_all
  30. end
  31. def reject_existing_followers!
  32. @account.passive_relationships.where(account: Account.where(domain: @domain)).includes(:account).reorder(nil).in_batches do |follows|
  33. domain_block_event.import_from_passive_follows!(follows)
  34. follows.each { |follow| reject_follow!(follow) }
  35. end
  36. end
  37. def reject_pending_follow_requests!
  38. FollowRequest.where(target_account: @account).where(account: Account.where(domain: @domain)).includes(:account).reorder(nil).find_each do |follow_request|
  39. reject_follow!(follow_request)
  40. end
  41. end
  42. def reject_follow!(follow)
  43. follow.destroy
  44. return unless follow.account.activitypub?
  45. ActivityPub::DeliveryWorker.perform_async(Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), @account.id, follow.account.inbox_url)
  46. end
  47. def notify_of_severed_relationships!
  48. return if @domain_block_event.nil?
  49. event = AccountRelationshipSeveranceEvent.create!(account: @account, relationship_severance_event: @domain_block_event)
  50. LocalNotificationWorker.perform_async(@account.id, event.id, 'AccountRelationshipSeveranceEvent', 'severed_relationships')
  51. end
  52. def domain_block_event
  53. @domain_block_event ||= RelationshipSeveranceEvent.create!(type: :user_domain_block, target_name: @domain)
  54. end
  55. end