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.

42 lines
1013 B

  1. # frozen_string_literal: true
  2. class FetchRemoteStatusService < 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. Rails.logger.debug "Processing Atom for remote status at #{url}"
  11. xml = Nokogiri::XML(body)
  12. xml.encoding = 'utf-8'
  13. account = extract_author(url, xml)
  14. return nil if account.nil?
  15. statuses = ProcessFeedService.new.call(body, account)
  16. statuses.first
  17. end
  18. def extract_author(url, xml)
  19. url_parts = Addressable::URI.parse(url)
  20. username = xml.at_xpath('//xmlns:author/xmlns:name').try(:content)
  21. domain = url_parts.host
  22. return nil if username.nil?
  23. Rails.logger.debug "Going to webfinger #{username}@#{domain}"
  24. return FollowRemoteAccountService.new.call("#{username}@#{domain}")
  25. rescue Nokogiri::XML::XPath::SyntaxError
  26. Rails.logger.debug 'Invalid XML or missing namespace'
  27. nil
  28. end
  29. end