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.

35 lines
1.2 KiB

  1. # frozen_string_literal: true
  2. class AuthorizeFollowService < BaseService
  3. include Payloadable
  4. def call(source_account, target_account, **options)
  5. if options[:skip_follow_request]
  6. follow_request = FollowRequest.new(account: source_account, target_account: target_account, uri: options[:follow_request_uri])
  7. else
  8. follow_request = FollowRequest.find_by!(account: source_account, target_account: target_account)
  9. follow_request.authorize!
  10. end
  11. create_notification(follow_request) unless source_account.local?
  12. follow_request
  13. end
  14. private
  15. def create_notification(follow_request)
  16. if follow_request.account.ostatus?
  17. NotificationWorker.perform_async(build_xml(follow_request), follow_request.target_account_id, follow_request.account_id)
  18. elsif follow_request.account.activitypub?
  19. ActivityPub::DeliveryWorker.perform_async(build_json(follow_request), follow_request.target_account_id, follow_request.account.inbox_url)
  20. end
  21. end
  22. def build_json(follow_request)
  23. Oj.dump(serialize_payload(follow_request, ActivityPub::AcceptFollowSerializer))
  24. end
  25. def build_xml(follow_request)
  26. OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.authorize_follow_request_salmon(follow_request))
  27. end
  28. end