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.

95 lines
3.5 KiB

  1. # frozen_string_literal: true
  2. class FetchAtomService < BaseService
  3. include JsonLdHelper
  4. def call(url)
  5. return if url.blank?
  6. result = process(url)
  7. # retry without ActivityPub
  8. result ||= process(url) if @unsupported_activity
  9. result
  10. rescue OpenSSL::SSL::SSLError => e
  11. Rails.logger.debug "SSL error: #{e}"
  12. nil
  13. rescue HTTP::ConnectionError => e
  14. Rails.logger.debug "HTTP ConnectionError: #{e}"
  15. nil
  16. end
  17. private
  18. def process(url, terminal = false)
  19. @url = url
  20. perform_request { |response| process_response(response, terminal) }
  21. end
  22. def perform_request(&block)
  23. accept = 'text/html'
  24. accept = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/atom+xml, ' + accept unless @unsupported_activity
  25. Request.new(:get, @url).add_headers('Accept' => accept).perform(&block)
  26. end
  27. def process_response(response, terminal = false)
  28. return nil if response.code != 200
  29. response_type = response.headers['Content-type']
  30. if response_type == 'application/atom+xml'
  31. [@url, { prefetched_body: response.body_with_limit }, :ostatus]
  32. elsif ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(response_type)
  33. body = response.body_with_limit
  34. json = body_to_json(body)
  35. if supported_context?(json) && equals_or_includes_any?(json['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES) && json['inbox'].present?
  36. [json['id'], { prefetched_body: body, id: true }, :activitypub]
  37. elsif supported_context?(json) && expected_type?(json)
  38. [json['id'], { prefetched_body: body, id: true }, :activitypub]
  39. else
  40. @unsupported_activity = true
  41. nil
  42. end
  43. elsif !terminal
  44. link_header = response['Link'] && parse_link_header(response)
  45. if link_header&.find_link(%w(rel alternate))
  46. process_link_headers(link_header)
  47. elsif response_type == 'text/html'
  48. process_html(response)
  49. end
  50. end
  51. end
  52. def expected_type?(json)
  53. equals_or_includes_any?(json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
  54. end
  55. def process_html(response)
  56. page = Nokogiri::HTML(response.body_with_limit)
  57. json_link = page.xpath('//link[@rel="alternate"]').find { |link| ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(link['type']) }
  58. atom_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
  59. result ||= process(json_link['href'], terminal: true) unless json_link.nil? || @unsupported_activity
  60. result ||= process(atom_link['href'], terminal: true) unless atom_link.nil?
  61. result
  62. end
  63. def process_link_headers(link_header)
  64. json_link = link_header.find_link(%w(rel alternate), %w(type application/activity+json)) || link_header.find_link(%w(rel alternate), ['type', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'])
  65. atom_link = link_header.find_link(%w(rel alternate), %w(type application/atom+xml))
  66. result ||= process(json_link.href, terminal: true) unless json_link.nil? || @unsupported_activity
  67. result ||= process(atom_link.href, terminal: true) unless atom_link.nil?
  68. result
  69. end
  70. def parse_link_header(response)
  71. LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
  72. end
  73. end