fetch_remote_status_service.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # frozen_string_literal: true
  2. class FetchRemoteStatusService < BaseService
  3. include AuthorExtractor
  4. def call(url, prefetched_body = nil)
  5. if prefetched_body.nil?
  6. atom_url, body = FetchAtomService.new.call(url)
  7. else
  8. atom_url = url
  9. body = prefetched_body
  10. end
  11. return nil if atom_url.nil?
  12. process_atom(atom_url, body)
  13. end
  14. private
  15. def process_atom(url, body)
  16. Rails.logger.debug "Processing Atom for remote status at #{url}"
  17. xml = Nokogiri::XML(body)
  18. xml.encoding = 'utf-8'
  19. account = author_from_xml(xml.at_xpath('/xmlns:entry', xmlns: TagManager::XMLNS))
  20. domain = Addressable::URI.parse(url).normalize.host
  21. return nil unless !account.nil? && confirmed_domain?(domain, account)
  22. statuses = ProcessFeedService.new.call(body, account)
  23. statuses.first
  24. rescue Nokogiri::XML::XPath::SyntaxError
  25. Rails.logger.debug 'Invalid XML or missing namespace'
  26. nil
  27. end
  28. def confirmed_domain?(domain, account)
  29. account.domain.nil? || domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url).normalize.host).zero?
  30. end
  31. end