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.

33 lines
838 B

  1. # frozen_string_literal: true
  2. class FetchRemoteAccountService < BaseService
  3. def call(url)
  4. atom_url, body = FetchAtomService.new.call(url)
  5. return nil if atom_url.nil?
  6. process_atom(atom_url, body)
  7. end
  8. private
  9. def process_atom(url, body)
  10. xml = Nokogiri::XML(body)
  11. xml.encoding = 'utf-8'
  12. url_parts = Addressable::URI.parse(url)
  13. username = xml.at_xpath('//xmlns:author/xmlns:name').try(:content)
  14. domain = url_parts.host
  15. return nil if username.nil?
  16. Rails.logger.debug "Going to webfinger #{username}@#{domain}"
  17. return FollowRemoteAccountService.new.call("#{username}@#{domain}")
  18. rescue TypeError
  19. Rails.logger.debug "Unparseable URL given: #{url}"
  20. nil
  21. rescue Nokogiri::XML::XPath::SyntaxError
  22. Rails.logger.debug 'Invalid XML or missing namespace'
  23. nil
  24. end
  25. end