fetch_remote_account_service.rb 1006 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # frozen_string_literal: true
  2. class FetchRemoteAccountService < BaseService
  3. include AuthorExtractor
  4. def call(url, prefetched_body = nil, protocol = :ostatus)
  5. if prefetched_body.nil?
  6. resource_url, body, protocol = FetchAtomService.new.call(url)
  7. else
  8. resource_url = url
  9. body = prefetched_body
  10. end
  11. case protocol
  12. when :ostatus
  13. process_atom(resource_url, body)
  14. when :activitypub
  15. ActivityPub::FetchRemoteAccountService.new.call(resource_url, body)
  16. end
  17. end
  18. private
  19. def process_atom(url, body)
  20. xml = Nokogiri::XML(body)
  21. xml.encoding = 'utf-8'
  22. account = author_from_xml(xml.at_xpath('/xmlns:feed', xmlns: TagManager::XMLNS), false)
  23. UpdateRemoteProfileService.new.call(xml, account) unless account.nil?
  24. account
  25. rescue TypeError
  26. Rails.logger.debug "Unparseable URL given: #{url}"
  27. nil
  28. rescue Nokogiri::XML::XPath::SyntaxError
  29. Rails.logger.debug 'Invalid XML or missing namespace'
  30. nil
  31. end
  32. end