block_service.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # frozen_string_literal: true
  2. class BlockService < BaseService
  3. def call(account, target_account)
  4. return if account.id == target_account.id
  5. UnfollowService.new.call(account, target_account) if account.following?(target_account)
  6. UnfollowService.new.call(target_account, account) if target_account.following?(account)
  7. block = account.block!(target_account)
  8. BlockWorker.perform_async(account.id, target_account.id)
  9. create_notification(block) unless target_account.local?
  10. block
  11. end
  12. private
  13. def create_notification(block)
  14. if block.target_account.ostatus?
  15. NotificationWorker.perform_async(build_xml(block), block.account_id, block.target_account_id)
  16. elsif block.target_account.activitypub?
  17. ActivityPub::DeliveryWorker.perform_async(build_json(block), block.account_id, block.target_account.inbox_url)
  18. end
  19. end
  20. def build_json(block)
  21. ActiveModelSerializers::SerializableResource.new(
  22. block,
  23. serializer: ActivityPub::BlockSerializer,
  24. adapter: ActivityPub::Adapter
  25. ).to_json
  26. end
  27. def build_xml(block)
  28. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.block_salmon(block))
  29. end
  30. end