From 661b31d822eb827d5207cbdc385676c52f847c86 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 8 Jul 2019 17:26:47 +0200 Subject: [PATCH] Add NodeInfo 2.0 support (in addition to 2.1) and fix validation Signed-off-by: Thomas Citharel --- .../controllers/node_info_controller.ex | 41 ++++++++++++------- .../controllers/nodeinfo_controller_test.exs | 16 +++++++- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/lib/mobilizon_web/controllers/node_info_controller.ex b/lib/mobilizon_web/controllers/node_info_controller.ex index b044d0e38..31bf6bc18 100644 --- a/lib/mobilizon_web/controllers/node_info_controller.ex +++ b/lib/mobilizon_web/controllers/node_info_controller.ex @@ -7,37 +7,42 @@ defmodule MobilizonWeb.NodeInfoController do use MobilizonWeb, :controller alias Mobilizon.{Events, Users} + alias Mobilizon.CommonConfig @instance Application.get_env(:mobilizon, :instance) + @node_info_supported_versions ["2.0", "2.1"] + @node_info_schema_uri "http://nodeinfo.diaspora.software/ns/schema/" def schemas(conn, _params) do - response = %{ - links: [ + links = + @node_info_supported_versions + |> Enum.map(fn version -> %{ - rel: "http://nodeinfo.diaspora.software/ns/schema/2.1", - href: MobilizonWeb.Router.Helpers.node_info_url(MobilizonWeb.Endpoint, :nodeinfo, "2.1") + rel: @node_info_schema_uri <> version, + href: + MobilizonWeb.Router.Helpers.node_info_url(MobilizonWeb.Endpoint, :nodeinfo, version) } - ] - } + end) - json(conn, response) + json(conn, %{ + links: links + }) end # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json - def nodeinfo(conn, %{"version" => "2.1"}) do + def nodeinfo(conn, %{"version" => version}) when version in @node_info_supported_versions do response = %{ - version: "2.1", + version: version, software: %{ name: "mobilizon", - version: Keyword.get(@instance, :version), - repository: Keyword.get(@instance, :repository) + version: Keyword.get(@instance, :version) }, protocols: ["activitypub"], services: %{ inbound: [], - outbound: [] + outbound: ["atom1.0"] }, - openRegistrations: Keyword.get(@instance, :registrations_open), + openRegistrations: CommonConfig.registrations_open?(), usage: %{ users: %{ total: Users.count_users() @@ -46,10 +51,18 @@ defmodule MobilizonWeb.NodeInfoController do localComments: Events.count_local_comments() }, metadata: %{ - nodeName: Keyword.get(@instance, :name) + nodeName: CommonConfig.instance_name(), + nodeDescription: CommonConfig.instance_description() } } + response = + if version == "2.1" do + put_in(response, [:software, :repository], Keyword.get(@instance, :repository)) + else + response + end + conn |> put_resp_header( "content-type", diff --git a/test/mobilizon_web/controllers/nodeinfo_controller_test.exs b/test/mobilizon_web/controllers/nodeinfo_controller_test.exs index 832a9578b..44592c05c 100644 --- a/test/mobilizon_web/controllers/nodeinfo_controller_test.exs +++ b/test/mobilizon_web/controllers/nodeinfo_controller_test.exs @@ -8,6 +8,15 @@ defmodule MobilizonWeb.NodeInfoControllerTest do assert json_response(conn, 200) == %{ "links" => [ + %{ + "href" => + MobilizonWeb.Router.Helpers.node_info_url( + MobilizonWeb.Endpoint, + :nodeinfo, + "2.0" + ), + "rel" => "http://nodeinfo.diaspora.software/ns/schema/2.0" + }, %{ "href" => MobilizonWeb.Router.Helpers.node_info_url( @@ -27,10 +36,13 @@ defmodule MobilizonWeb.NodeInfoControllerTest do resp = json_response(conn, 200) assert resp == %{ - "metadata" => %{"nodeName" => Keyword.get(@instance, :name)}, + "metadata" => %{ + "nodeName" => Mobilizon.CommonConfig.instance_name(), + "nodeDescription" => Mobilizon.CommonConfig.instance_description() + }, "openRegistrations" => Keyword.get(@instance, :registrations_open), "protocols" => ["activitypub"], - "services" => %{"inbound" => [], "outbound" => []}, + "services" => %{"inbound" => [], "outbound" => ["atom1.0"]}, "software" => %{ "name" => "mobilizon", "version" => Keyword.get(@instance, :version),