You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.3 KiB

  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. RejectFollowService.new.call(account, target_account) if target_account.requested?(account)
  8. block = account.block!(target_account)
  9. BlockWorker.perform_async(account.id, target_account.id)
  10. create_notification(block) unless target_account.local?
  11. block
  12. end
  13. private
  14. def create_notification(block)
  15. if block.target_account.ostatus?
  16. NotificationWorker.perform_async(build_xml(block), block.account_id, block.target_account_id)
  17. elsif block.target_account.activitypub?
  18. ActivityPub::DeliveryWorker.perform_async(build_json(block), block.account_id, block.target_account.inbox_url)
  19. end
  20. end
  21. def build_json(block)
  22. ActiveModelSerializers::SerializableResource.new(
  23. block,
  24. serializer: ActivityPub::BlockSerializer,
  25. adapter: ActivityPub::Adapter
  26. ).to_json
  27. end
  28. def build_xml(block)
  29. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.block_salmon(block))
  30. end
  31. end