闭社主体 forked from https://github.com/tootsuite/mastodon
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.

44 lines
1.2 KiB

  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).normalized_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. rescue Goldfinger::NotFoundError, Goldfinger::Error
  28. Rails.logger.debug 'Exceptions related to Goldfinger occurs'
  29. nil
  30. end
  31. def confirmed_domain?(domain, account)
  32. account.domain.nil? || domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url).normalized_host).zero?
  33. end
  34. end