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.

47 lines
1.1 KiB

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