闭社主体 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.

57 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class FetchRemoteStatusService < BaseService
  3. def call(url, prefetched_body = nil)
  4. if prefetched_body.nil?
  5. atom_url, body = FetchAtomService.new.call(url)
  6. else
  7. atom_url = url
  8. body = prefetched_body
  9. end
  10. return nil if atom_url.nil?
  11. process_atom(atom_url, body)
  12. end
  13. private
  14. def process_atom(url, body)
  15. Rails.logger.debug "Processing Atom for remote status at #{url}"
  16. xml = Nokogiri::XML(body)
  17. xml.encoding = 'utf-8'
  18. account = extract_author(url, xml)
  19. return nil if account.nil?
  20. statuses = ProcessFeedService.new.call(body, account)
  21. statuses.first
  22. end
  23. def extract_author(url, xml)
  24. url_parts = Addressable::URI.parse(url).normalize
  25. username = xml.at_xpath('//xmlns:author/xmlns:name').try(:content)
  26. domain = url_parts.host
  27. return nil if username.nil?
  28. Rails.logger.debug "Going to webfinger #{username}@#{domain}"
  29. account = FollowRemoteAccountService.new.call("#{username}@#{domain}")
  30. # If the author's confirmed URLs do not match the domain of the URL
  31. # we are reading this from, abort
  32. return nil unless confirmed_domain?(domain, account)
  33. account
  34. rescue Nokogiri::XML::XPath::SyntaxError
  35. Rails.logger.debug 'Invalid XML or missing namespace'
  36. nil
  37. end
  38. def confirmed_domain?(domain, account)
  39. domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url).normalize.host).zero?
  40. end
  41. end