fetch_resource_service.rb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. class FetchResourceService < BaseService
  3. include JsonLdHelper
  4. ACCEPT_HEADER = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams", text/html'
  5. def call(url)
  6. return if url.blank?
  7. process(url)
  8. rescue HTTP::Error, OpenSSL::SSL::SSLError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError => e
  9. Rails.logger.debug "Error fetching resource #{@url}: #{e}"
  10. nil
  11. end
  12. private
  13. def process(url, terminal = false)
  14. @url = url
  15. perform_request { |response| process_response(response, terminal) }
  16. end
  17. def perform_request(&block)
  18. Request.new(:get, @url).add_headers('Accept' => ACCEPT_HEADER).on_behalf_of(Account.representative).perform(&block)
  19. end
  20. def process_response(response, terminal = false)
  21. return nil if response.code != 200
  22. if ['application/activity+json', 'application/ld+json'].include?(response.mime_type)
  23. body = response.body_with_limit
  24. json = body_to_json(body)
  25. [json['id'], { prefetched_body: body, id: true }, :activitypub] if supported_context?(json) && (equals_or_includes_any?(json['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES) || expected_type?(json))
  26. elsif !terminal
  27. link_header = response['Link'] && parse_link_header(response)
  28. if link_header&.find_link(%w(rel alternate))
  29. process_link_headers(link_header)
  30. elsif response.mime_type == 'text/html'
  31. process_html(response)
  32. end
  33. end
  34. end
  35. def expected_type?(json)
  36. equals_or_includes_any?(json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
  37. end
  38. def process_html(response)
  39. page = Nokogiri::HTML(response.body_with_limit)
  40. 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']) }
  41. process(json_link['href'], terminal: true) unless json_link.nil?
  42. end
  43. def process_link_headers(link_header)
  44. 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"'])
  45. process(json_link.href, terminal: true) unless json_link.nil?
  46. end
  47. def parse_link_header(response)
  48. LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
  49. end
  50. end