闭社主体 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.

35 lines
927 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. account = FollowRemoteAccountService.new.call("#{username}@#{domain}")
  18. UpdateRemoteProfileService.new.call(xml, account) unless account.nil?
  19. account
  20. rescue TypeError
  21. Rails.logger.debug "Unparseable URL given: #{url}"
  22. nil
  23. rescue Nokogiri::XML::XPath::SyntaxError
  24. Rails.logger.debug 'Invalid XML or missing namespace'
  25. nil
  26. end
  27. end