]>
cat aescling's git repositories - mastodon.git/blob - app/services/fetch_atom_service.rb
1 # frozen_string_literal: true
3 class FetchAtomService
< BaseService
11 # retry without ActivityPub
12 result
||= process(url
) if @unsupported_activity
15 rescue OpenSSL
::SSL::SSLError => e
16 Rails
.logger
.debug
"SSL error: #{e}"
18 rescue HTTP
::ConnectionError => e
19 Rails
.logger
.debug
"HTTP ConnectionError: #{e}"
25 def process(url
, terminal
= false)
27 perform_request
{ |response
| process_response(response
, terminal
) }
30 def perform_request(&block
)
32 accept
= 'application/activity+json
, application
/ld+json
; profile
="https://www.w3.org/ns/activitystreams", application
/atom+xml
, ' + accept
unless @unsupported_activity
34 Request
.new(:get, @url).add_headers('Accept' => accept
).perform(&block
)
37 def process_response(response
, terminal
= false)
38 return nil if response
.code !
= 200
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]
50 @unsupported_activity = true
54 link_header
= response
['Link'] && parse_link_header(response
)
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
)
64 def expected_type
?(json
)
65 equals_or_includes_any
?(json
['type'], ActivityPub
::Activity::Create::SUPPORTED_TYPES + ActivityPub
::Activity::Create::CONVERTED_TYPES)
68 def process_html(response
)
69 page
= Nokogiri
::HTML(response
.body_with_limit
)
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
' }
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?
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
))
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?
90 def parse_link_header(response
)
91 LinkHeader
.parse(response
['Link'].is_a
?(Array
) ? response
['Link'].first
: response
['Link'])
This page took 0.113933 seconds and 5 git commands to generate.