fetch_remote_status_service.rb 1.3 KB

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