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

45 lines
1.2 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. email = xml.at_xpath('//xmlns:author/xmlns:email').try(:content)
  18. if email.nil?
  19. url_parts = Addressable::URI.parse(url)
  20. username = xml.at_xpath('//xmlns:author/xmlns:name').try(:content)
  21. domain = url_parts.host
  22. else
  23. username, domain = email.split('@')
  24. end
  25. return nil if username.nil? || domain.nil?
  26. Rails.logger.debug "Going to webfinger #{username}@#{domain}"
  27. account = FollowRemoteAccountService.new.call("#{username}@#{domain}")
  28. UpdateRemoteProfileService.new.call(xml, account) unless account.nil?
  29. account
  30. rescue TypeError
  31. Rails.logger.debug "Unparseable URL given: #{url}"
  32. nil
  33. rescue Nokogiri::XML::XPath::SyntaxError
  34. Rails.logger.debug 'Invalid XML or missing namespace'
  35. nil
  36. end
  37. end