闭社主体 forked from https://github.com/tootsuite/mastodon
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
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, @response.to_s, :ostatus]
  34. elsif ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@response.mime_type)
  35. if supported_activity?(@response.to_s)
  36. [@url, @response.to_s, :activitypub]
  37. else
  38. @unsupported_activity = true
  39. nil
  40. end
  41. elsif @response['Link'] && !terminal
  42. process_headers
  43. elsif @response.mime_type == 'text/html' && !terminal
  44. process_html
  45. end
  46. end
  47. def process_html
  48. page = Nokogiri::HTML(@response.to_s)
  49. 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']) }
  50. atom_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
  51. result ||= process(json_link['href'], terminal: true) unless json_link.nil? || @unsupported_activity
  52. result ||= process(atom_link['href'], terminal: true) unless atom_link.nil?
  53. result
  54. end
  55. def process_headers
  56. link_header = LinkHeader.parse(@response['Link'].is_a?(Array) ? @response['Link'].first : @response['Link'])
  57. 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"'])
  58. atom_link = link_header.find_link(%w(rel alternate), %w(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 supported_activity?(body)
  64. json = body_to_json(body)
  65. return false unless supported_context?(json)
  66. json['type'] == 'Person' ? json['inbox'].present? : true
  67. end
  68. end