2016-11-15 10:56:29 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-26 10:42:38 -04:00
|
|
|
class FetchAtomService < BaseService
|
|
|
|
def call(url)
|
2017-02-12 11:28:15 -05:00
|
|
|
return if url.blank?
|
|
|
|
|
2017-08-13 20:29:36 -04:00
|
|
|
@url = url
|
2016-09-26 10:42:38 -04:00
|
|
|
|
2017-08-13 20:29:36 -04:00
|
|
|
perform_request
|
|
|
|
process_response
|
2016-10-05 07:26:44 -04:00
|
|
|
rescue OpenSSL::SSL::SSLError => e
|
|
|
|
Rails.logger.debug "SSL error: #{e}"
|
2017-06-29 07:04:07 -04:00
|
|
|
nil
|
|
|
|
rescue HTTP::ConnectionError => e
|
|
|
|
Rails.logger.debug "HTTP ConnectionError: #{e}"
|
|
|
|
nil
|
2016-09-26 10:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-08-13 20:29:36 -04:00
|
|
|
def perform_request
|
|
|
|
@response = Request.new(:get, @url)
|
|
|
|
.add_headers('Accept' => 'application/activity+json, application/ld+json, application/atom+xml, text/html')
|
|
|
|
.perform
|
|
|
|
end
|
2016-09-26 10:42:38 -04:00
|
|
|
|
2017-08-13 20:29:36 -04:00
|
|
|
def process_response(terminal = false)
|
|
|
|
return nil if @response.code != 200
|
2016-09-26 10:42:38 -04:00
|
|
|
|
2017-08-13 20:29:36 -04:00
|
|
|
if @response.mime_type == 'application/atom+xml'
|
|
|
|
[@url, @response.to_s, :ostatus]
|
|
|
|
elsif ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@response.mime_type)
|
|
|
|
[@url, @response.to_s, :activitypub]
|
|
|
|
elsif @response['Link'] && !terminal
|
|
|
|
process_headers
|
|
|
|
elsif @response.mime_type == 'text/html' && !terminal
|
|
|
|
process_html
|
|
|
|
end
|
2016-09-26 10:42:38 -04:00
|
|
|
end
|
|
|
|
|
2017-08-13 20:29:36 -04:00
|
|
|
def process_html
|
|
|
|
page = Nokogiri::HTML(@response.to_s)
|
2016-09-26 10:42:38 -04:00
|
|
|
|
2017-08-13 20:29:36 -04: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-26 10:42:38 -04:00
|
|
|
|
2017-08-13 20:29:36 -04:00
|
|
|
if !json_link.nil?
|
|
|
|
@url = json_link['href']
|
|
|
|
perform_request
|
|
|
|
process_response(true)
|
|
|
|
elsif !atom_link.nil?
|
|
|
|
@url = atom_link['href']
|
|
|
|
perform_request
|
|
|
|
process_response(true)
|
|
|
|
end
|
2016-09-26 10:42:38 -04:00
|
|
|
end
|
|
|
|
|
2017-08-13 20:29:36 -04:00
|
|
|
def process_headers
|
|
|
|
link_header = LinkHeader.parse(@response['Link'].is_a?(Array) ? @response['Link'].first : @response['Link'])
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
if !json_link.nil?
|
|
|
|
@url = json_link.href
|
|
|
|
perform_request
|
|
|
|
process_response(true)
|
|
|
|
elsif !atom_link.nil?
|
|
|
|
@url = atom_link.href
|
|
|
|
perform_request
|
|
|
|
process_response(true)
|
|
|
|
end
|
2016-09-26 10:42:38 -04:00
|
|
|
end
|
|
|
|
end
|