fetch_atom_service.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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
  21. process_response(terminal)
  22. end
  23. def perform_request
  24. accept = 'text/html'
  25. accept = 'application/activity+json, application/ld+json, application/atom+xml, ' + accept unless @unsupported_activity
  26. @response = Request.new(:get, @url)
  27. .add_headers('Accept' => accept)
  28. .perform
  29. end
  30. def process_response(terminal = false)
  31. return nil if @response.code != 200
  32. if @response.mime_type == 'application/atom+xml'
  33. [@url, { prefetched_body: @response.to_s }, :ostatus]
  34. elsif ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@response.mime_type)
  35. json = body_to_json(@response.to_s)
  36. if supported_context?(json) && json['type'] == 'Person' && json['inbox'].present?
  37. [json['id'], { prefetched_body: @response.to_s, id: true }, :activitypub]
  38. elsif supported_context?(json) && json['type'] == 'Note'
  39. [json['id'], { prefetched_body: @response.to_s, id: true }, :activitypub]
  40. else
  41. @unsupported_activity = true
  42. nil
  43. end
  44. elsif @response['Link'] && !terminal && link_header.find_link(%w(rel alternate))
  45. process_headers
  46. elsif @response.mime_type == 'text/html' && !terminal
  47. process_html
  48. end
  49. end
  50. def process_html
  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_headers
  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 link_header
  66. @link_header ||= LinkHeader.parse(@response['Link'].is_a?(Array) ? @response['Link'].first : @response['Link'])
  67. end
  68. end