after_block_domain_from_account_service.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. clear_notifications!
  11. remove_follows!
  12. reject_existing_followers!
  13. reject_pending_follow_requests!
  14. end
  15. private
  16. def remove_follows!
  17. @account.active_relationships.where(account: Account.where(domain: @domain)).includes(:target_account).reorder(nil).find_each do |follow|
  18. UnfollowService.new.call(@account, follow.target_account)
  19. end
  20. end
  21. def clear_notifications!
  22. Notification.where(account: @account).where(from_account: Account.where(domain: @domain)).in_batches.delete_all
  23. end
  24. def reject_existing_followers!
  25. @account.passive_relationships.where(account: Account.where(domain: @domain)).includes(:account).reorder(nil).find_each do |follow|
  26. reject_follow!(follow)
  27. end
  28. end
  29. def reject_pending_follow_requests!
  30. FollowRequest.where(target_account: @account).where(account: Account.where(domain: @domain)).includes(:account).reorder(nil).find_each do |follow_request|
  31. reject_follow!(follow_request)
  32. end
  33. end
  34. def reject_follow!(follow)
  35. follow.destroy
  36. return unless follow.account.activitypub?
  37. ActivityPub::DeliveryWorker.perform_async(Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), @account.id, follow.account.inbox_url)
  38. end
  39. end