2016-11-16 02:56:29 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-27 00:42:38 +10:00
|
|
|
class FetchAtomService < BaseService
|
2017-08-23 02:30:15 +10:00
|
|
|
include JsonLdHelper
|
|
|
|
|
2016-09-27 00:42:38 +10:00
|
|
|
def call(url)
|
2017-02-13 03:28:15 +11:00
|
|
|
return if url.blank?
|
|
|
|
|
2017-08-23 02:30:15 +10:00
|
|
|
result = process(url)
|
2016-09-27 00:42:38 +10:00
|
|
|
|
2017-08-23 02:30:15 +10:00
|
|
|
# retry without ActivityPub
|
|
|
|
result ||= process(url) if @unsupported_activity
|
|
|
|
|
|
|
|
result
|
2016-10-05 22:26:44 +11:00
|
|
|
rescue OpenSSL::SSL::SSLError => e
|
|
|
|
Rails.logger.debug "SSL error: #{e}"
|
2017-06-29 21:04:07 +10:00
|
|
|
nil
|
|
|
|
rescue HTTP::ConnectionError => e
|
|
|
|
Rails.logger.debug "HTTP ConnectionError: #{e}"
|
|
|
|
nil
|
2016-09-27 00:42:38 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-23 02:30:15 +10:00
|
|
|
def process(url, terminal = false)
|
|
|
|
@url = url
|
2018-03-24 22:49:54 +11:00
|
|
|
perform_request { |response| process_response(response, terminal) }
|
2017-08-23 02:30:15 +10:00
|
|
|
end
|
|
|
|
|
2018-03-24 22:49:54 +11:00
|
|
|
def perform_request(&block)
|
2017-08-23 02:30:15 +10:00
|
|
|
accept = 'text/html'
|
|
|
|
accept = 'application/activity+json, application/ld+json, application/atom+xml, ' + accept unless @unsupported_activity
|
|
|
|
|
2018-03-24 22:49:54 +11:00
|
|
|
Request.new(:get, @url).add_headers('Accept' => accept).perform(&block)
|
2017-08-14 10:29:36 +10:00
|
|
|
end
|
2016-09-27 00:42:38 +10:00
|
|
|
|
2018-03-24 22:49:54 +11:00
|
|
|
def process_response(response, terminal = false)
|
|
|
|
return nil if response.code != 200
|
2016-09-27 00:42:38 +10:00
|
|
|
|
2018-03-24 22:49:54 +11:00
|
|
|
if response.mime_type == 'application/atom+xml'
|
2018-03-26 23:02:10 +11:00
|
|
|
[@url, { prefetched_body: response.body_with_limit }, :ostatus]
|
2018-03-24 22:49:54 +11:00
|
|
|
elsif ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(response.mime_type)
|
2018-03-26 23:02:10 +11:00
|
|
|
body = response.body_with_limit
|
|
|
|
json = body_to_json(body)
|
2017-10-04 10:13:48 +11:00
|
|
|
if supported_context?(json) && json['type'] == 'Person' && json['inbox'].present?
|
2018-03-26 23:02:10 +11:00
|
|
|
[json['id'], { prefetched_body: body, id: true }, :activitypub]
|
2018-01-06 08:42:50 +11:00
|
|
|
elsif supported_context?(json) && json['type'] == 'Note'
|
2018-03-26 23:02:10 +11:00
|
|
|
[json['id'], { prefetched_body: body, id: true }, :activitypub]
|
2017-08-23 02:30:15 +10:00
|
|
|
else
|
|
|
|
@unsupported_activity = true
|
|
|
|
nil
|
|
|
|
end
|
2018-03-24 22:49:54 +11:00
|
|
|
elsif !terminal
|
|
|
|
link_header = response['Link'] && parse_link_header(response)
|
|
|
|
|
|
|
|
if link_header&.find_link(%w(rel alternate))
|
|
|
|
process_link_headers(link_header)
|
|
|
|
elsif response.mime_type == 'text/html'
|
|
|
|
process_html(response)
|
|
|
|
end
|
2017-08-14 10:29:36 +10:00
|
|
|
end
|
2016-09-27 00:42:38 +10:00
|
|
|
end
|
|
|
|
|
2018-03-24 22:49:54 +11:00
|
|
|
def process_html(response)
|
2018-03-26 23:02:10 +11:00
|
|
|
page = Nokogiri::HTML(response.body_with_limit)
|
2016-09-27 00:42:38 +10:00
|
|
|
|
2017-08-14 10:29:36 +10:00
|
|
|
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']) }
|
|
|
|
atom_link = page.xpath('//link[@rel="alternate"]').find { |link| link['type'] == 'application/atom+xml' }
|
2016-09-27 00:42:38 +10:00
|
|
|
|
2017-08-23 20:25:57 +10:00
|
|
|
result ||= process(json_link['href'], terminal: true) unless json_link.nil? || @unsupported_activity
|
|
|
|
result ||= process(atom_link['href'], terminal: true) unless atom_link.nil?
|
2017-08-23 02:30:15 +10:00
|
|
|
|
|
|
|
result
|
2016-09-27 00:42:38 +10:00
|
|
|
end
|
|
|
|
|
2018-03-24 22:49:54 +11:00
|
|
|
def process_link_headers(link_header)
|
2017-08-14 10:29:36 +10:00
|
|
|
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"'])
|
|
|
|
atom_link = link_header.find_link(%w(rel alternate), %w(type application/atom+xml))
|
|
|
|
|
2017-08-23 02:30:15 +10:00
|
|
|
result ||= process(json_link.href, terminal: true) unless json_link.nil? || @unsupported_activity
|
|
|
|
result ||= process(atom_link.href, terminal: true) unless atom_link.nil?
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2018-01-04 14:56:04 +11:00
|
|
|
|
2018-03-24 22:49:54 +11:00
|
|
|
def parse_link_header(response)
|
|
|
|
LinkHeader.parse(response['Link'].is_a?(Array) ? response['Link'].first : response['Link'])
|
2018-01-04 14:56:04 +11:00
|
|
|
end
|
2016-09-27 00:42:38 +10:00
|
|
|
end
|