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.

88 lines
3.1 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, 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. if response.mime_type == 'application/atom+xml'
  30. [@url, { prefetched_body: response.to_s }, :ostatus]
  31. elsif ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(response.mime_type)
  32. json = body_to_json(response.to_s)
  33. if supported_context?(json) && json['type'] == 'Person' && json['inbox'].present?
  34. [json['id'], { prefetched_body: response.to_s, id: true }, :activitypub]
  35. elsif supported_context?(json) && json['type'] == 'Note'
  36. [json['id'], { prefetched_body: response.to_s, id: true }, :activitypub]
  37. else
  38. @unsupported_activity = true
  39. nil
  40. end
  41. elsif !terminal
  42. link_header = response['Link'] && parse_link_header(response)
  43. if link_header&.find_link(%w(rel alternate))
  44. process_link_headers(link_header)
  45. elsif response.mime_type == 'text/html'
  46. process_html(response)
  47. end
  48. end
  49. end
  50. def process_html(response)
  51. page = Nokogiri::HTML(response.to_s)
  52. 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']) }
  53. atom_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
  54. result ||= process(json_link['href'], terminal: true) unless json_link.nil? || @unsupported_activity
  55. result ||= process(atom_link['href'], terminal: true) unless atom_link.nil?
  56. result
  57. end
  58. def process_link_headers(link_header)
  59. 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"'])
  60. atom_link = link_header.find_link(%w(rel alternate), %w(type application/atom+xml))
  61. result ||= process(json_link.href, terminal: true) unless json_link.nil? || @unsupported_activity
  62. result ||= process(atom_link.href, terminal: true) unless atom_link.nil?
  63. result
  64. end
  65. def parse_link_header(response)
  66. LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
  67. end
  68. end