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 FetchRemoteAccountService < 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::FetchRemoteAccountService.new.call(resource_url, **resource_options)
  16. end
  17. end
  18. private
  19. def process_atom(url, prefetched_body:)
  20. xml = Nokogiri::XML(prefetched_body)
  21. xml.encoding = 'utf-8'
  22. account = author_from_xml(xml.at_xpath('/xmlns:feed', xmlns: OStatus::TagManager::XMLNS), false)
  23. UpdateRemoteProfileService.new.call(xml, account) if account.present? && trusted_domain?(url, account)
  24. account
  25. rescue TypeError
  26. Rails.logger.debug "Unparseable URL given: #{url}"
  27. nil
  28. rescue Nokogiri::XML::XPath::SyntaxError
  29. Rails.logger.debug 'Invalid XML or missing namespace'
  30. nil
  31. end
  32. def trusted_domain?(url, account)
  33. domain = Addressable::URI.parse(url).normalized_host
  34. domain.casecmp(account.domain).zero? || domain.casecmp(Addressable::URI.parse(account.remote_url.presence || account.uri).normalized_host).zero?
  35. end
  36. end