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.

37 lines
1.2 KiB

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