Merge branch 'next-minor' into develop

This commit is contained in:
Benjamin Neff
2022-07-23 17:01:02 +02:00
10 changed files with 315 additions and 28 deletions

View File

@@ -3,7 +3,6 @@
class ConnectionTester
include Diaspora::Logging
NODEINFO_SCHEMA = "http://nodeinfo.diaspora.software/ns/schema/1.0"
NODEINFO_FRAGMENT = "/.well-known/nodeinfo"
class << self
@@ -124,8 +123,11 @@ class ConnectionTester
def nodeinfo
with_http_connection do |http|
ni_resp = http.get(NODEINFO_FRAGMENT)
nd_resp = http.get(find_nodeinfo_url(ni_resp.body))
find_software_version(nd_resp.body)
ni_urls = find_nodeinfo_urls(ni_resp.body)
raise NodeInfoFailure, "No supported NodeInfo version found" if ni_urls.empty?
version, url = ni_urls.max
find_software_version(version, http.get(url).body)
end
rescue Faraday::ClientError => e
raise HTTPFailure, "#{e.class}: #{e.message}"
@@ -180,20 +182,23 @@ class ConnectionTester
@result.ssl = (response.env.url.scheme == "https")
end
# walk the JSON document, get the actual document location
def find_nodeinfo_url(body)
# walk the JSON document, get the actual document locations
def find_nodeinfo_urls(body)
jrd = JSON.parse(body)
links = jrd.fetch("links")
raise NodeInfoFailure, "invalid JRD: '#/links' is not an array!" unless links.is_a?(Array)
links.find { |entry|
entry.fetch("rel") == NODEINFO_SCHEMA
}.fetch("href")
supported_rel_map = NodeInfo::VERSIONS.index_by {|v| "http://nodeinfo.diaspora.software/ns/schema/#{v}" }
links.map {|entry|
version = supported_rel_map[entry.fetch("rel")]
[version, entry.fetch("href")] if version
}.compact.to_h
end
# walk the JSON document, find the version string
def find_software_version(body)
def find_software_version(version, body)
info = JSON.parse(body)
JSON::Validator.validate!(NodeInfo.schema("1.0"), info)
JSON::Validator.validate!(NodeInfo.schema(version), info)
sw = info.fetch("software")
@result.software_version = "#{sw.fetch('name')} #{sw.fetch('version')}"
end

View File

@@ -4,13 +4,13 @@ require "pathname"
require "json-schema"
module NodeInfo
VERSIONS = %w(1.0 2.0).freeze
SCHEMAS = {}
private_constant :VERSIONS, :SCHEMAS
VERSIONS = %w[1.0 2.0 2.1].freeze
SCHEMAS = {} # rubocop:disable Style/MutableConstant
private_constant :SCHEMAS
# rubocop:disable Metrics/BlockLength
Document = Struct.new(:version, :software, :protocols, :services, :open_registrations, :usage, :metadata) do
Software = Struct.new(:name, :version) do
# rubocop:disable Lint/ConstantDefinitionInBlock
Software = Struct.new(:name, :version, :repository, :homepage) do
def initialize(name=nil, version=nil)
super(name, version)
end
@@ -21,6 +21,13 @@ module NodeInfo
"version" => version
}
end
def version_21_hash
version_10_hash.merge(
"repository" => repository,
"homepage" => homepage
)
end
end
Protocols = Struct.new(:protocols) do
@@ -80,6 +87,7 @@ module NodeInfo
}
end
end
# rubocop:enable Lint/ConstantDefinitionInBlock
def self.build
new.tap do |doc|
@@ -98,6 +106,8 @@ module NodeInfo
version_10_hash
when "2.0"
version_20_hash
when "2.1"
version_21_hash
end
end
@@ -144,6 +154,18 @@ module NodeInfo
)
end
def version_21_hash
deep_compact(
"version" => "2.1",
"software" => software.version_21_hash,
"protocols" => protocols.version_20_array,
"services" => services.version_10_hash,
"openRegistrations" => open_registrations,
"usage" => usage.version_10_hash,
"metadata" => metadata
)
end
def deep_compact(hash)
hash.tap do |hash|
hash.reject! {|_, value|
@@ -153,7 +175,6 @@ module NodeInfo
end
end
end
# rubocop:enable Metrics/BlockLength
def self.schema(version)
SCHEMAS[version] ||= JSON.parse(