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.

83 lines
2.8 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
  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. else
  39. @unsupported_activity = true
  40. nil
  41. end
  42. elsif @response['Link'] && !terminal
  43. process_headers
  44. elsif @response.mime_type == 'text/html' && !terminal
  45. process_html
  46. end
  47. end
  48. def process_html
  49. page = Nokogiri::HTML(@response.to_s)
  50. 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']) }
  51. atom_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
  52. result ||= process(json_link['href'], terminal: true) unless json_link.nil? || @unsupported_activity
  53. result ||= process(atom_link['href'], terminal: true) unless atom_link.nil?
  54. result
  55. end
  56. def process_headers
  57. link_header = LinkHeader.parse(@response['Link'].is_a?(Array) ? @response['Link'].first : @response['Link'])
  58. 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"'])
  59. atom_link = link_header.find_link(%w(rel alternate), %w(type application/atom+xml))
  60. result ||= process(json_link.href, terminal: true) unless json_link.nil? || @unsupported_activity
  61. result ||= process(atom_link.href, terminal: true) unless atom_link.nil?
  62. result
  63. end
  64. end