1
0

fetch_remote_status_service.rb 1013 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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