logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

fetch_atom_service.rb (3414B)


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