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.

45 lines
1.4 KiB

  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, resource_options, protocol = FetchAtomService.new.call(url)
  7. else
  8. resource_url = url
  9. resource_options = { prefetched_body: prefetched_body }
  10. end
  11. case protocol
  12. when :ostatus
  13. process_atom(resource_url, **resource_options)
  14. when :activitypub
  15. ActivityPub::FetchRemoteStatusService.new.call(resource_url, **resource_options)
  16. end
  17. end
  18. private
  19. def process_atom(url, prefetched_body:)
  20. Rails.logger.debug "Processing Atom for remote status at #{url}"
  21. xml = Nokogiri::XML(prefetched_body)
  22. xml.encoding = 'utf-8'
  23. account = author_from_xml(xml.at_xpath('/xmlns:entry', xmlns: OStatus::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(prefetched_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 || account.uri).normalized_host).zero?
  34. end
  35. end