闭社主体 forked from https://github.com/tootsuite/mastodon
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.

40 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. class FetchRemoteAccountService < BaseService
  3. def call(url, prefetched_body = nil)
  4. if prefetched_body.nil?
  5. atom_url, body = FetchAtomService.new.call(url)
  6. else
  7. atom_url = url
  8. body = prefetched_body
  9. end
  10. return nil if atom_url.nil?
  11. process_atom(atom_url, body)
  12. end
  13. private
  14. def process_atom(url, body)
  15. xml = Nokogiri::XML(body)
  16. xml.encoding = 'utf-8'
  17. url_parts = Addressable::URI.parse(url)
  18. username = xml.at_xpath('//xmlns:author/xmlns:name').try(:content)
  19. domain = url_parts.host
  20. return nil if username.nil?
  21. Rails.logger.debug "Going to webfinger #{username}@#{domain}"
  22. account = FollowRemoteAccountService.new.call("#{username}@#{domain}")
  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