authorize_follow_service.rb 1.3 KB

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