]> cat aescling's git repositories - mastodon.git/blob - app/services/fetch_atom_service.rb
Ignore JSON-LD profile in mime type comparison (#9179)
[mastodon.git] / app / services / fetch_atom_service.rb
1 # frozen_string_literal: true
2
3 class FetchAtomService < BaseService
4 include JsonLdHelper
5
6 def call(url)
7 return if url.blank?
8
9 result = process(url)
10
11 # retry without ActivityPub
12 result ||= process(url) if @unsupported_activity
13
14 result
15 rescue OpenSSL::SSL::SSLError => e
16 Rails.logger.debug "SSL error: #{e}"
17 nil
18 rescue HTTP::ConnectionError => e
19 Rails.logger.debug "HTTP ConnectionError: #{e}"
20 nil
21 end
22
23 private
24
25 def process(url, terminal = false)
26 @url = url
27 perform_request { |response| process_response(response, terminal) }
28 end
29
30 def perform_request(&block)
31 accept = 'text/html'
32 accept = 'application/activity+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams", application/atom+xml, ' + accept unless @unsupported_activity
33
34 Request.new(:get, @url).add_headers('Accept' => accept).perform(&block)
35 end
36
37 def process_response(response, terminal = false)
38 return nil if response.code != 200
39
40 if response.mime_type == 'application/atom+xml'
41 [@url, { prefetched_body: response.body_with_limit }, :ostatus]
42 elsif ['application/activity+json', 'application/ld+json'].include?(response.mime_type)
43 body = response.body_with_limit
44 json = body_to_json(body)
45 if supported_context?(json) && equals_or_includes_any?(json['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES) && json['inbox'].present?
46 [json['id'], { prefetched_body: body, id: true }, :activitypub]
47 elsif supported_context?(json) && expected_type?(json)
48 [json['id'], { prefetched_body: body, id: true }, :activitypub]
49 else
50 @unsupported_activity = true
51 nil
52 end
53 elsif !terminal
54 link_header = response['Link'] && parse_link_header(response)
55
56 if link_header&.find_link(%w(rel alternate))
57 process_link_headers(link_header)
58 elsif response.mime_type == 'text/html'
59 process_html(response)
60 end
61 end
62 end
63
64 def expected_type?(json)
65 equals_or_includes_any?(json['type'], ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
66 end
67
68 def process_html(response)
69 page = Nokogiri::HTML(response.body_with_limit)
70
71 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']) }
72 atom_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
73
74 result ||= process(json_link['href'], terminal: true) unless json_link.nil? || @unsupported_activity
75 result ||= process(atom_link['href'], terminal: true) unless atom_link.nil?
76
77 result
78 end
79
80 def process_link_headers(link_header)
81 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"'])
82 atom_link = link_header.find_link(%w(rel alternate), %w(type application/atom+xml))
83
84 result ||= process(json_link.href, terminal: true) unless json_link.nil? || @unsupported_activity
85 result ||= process(atom_link.href, terminal: true) unless atom_link.nil?
86
87 result
88 end
89
90 def parse_link_header(response)
91 LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
92 end
93 end
This page took 0.113933 seconds and 5 git commands to generate.