keep compatibility with nodeinfo 2.0
authorLuna <git@l4.pm>
Fri, 1 Feb 2019 17:23:40 +0000 (14:23 -0300)
committerLuna <git@l4.pm>
Fri, 1 Feb 2019 17:23:40 +0000 (14:23 -0300)
splits actual nodeinfo generation into raw_nodeinfo, the 2.0 handler
gives the same result, while the 2.1 handler inserts the
software.repository field. requested by @href

lib/pleroma/web/nodeinfo/nodeinfo_controller.ex

index f5974fbb99998d2e8a351d7be8b630b0545eac23..72b5d97acf13f4994dbbb5195ec714a68728d4c6 100644 (file)
@@ -16,6 +16,10 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
   def schemas(conn, _params) do
     response = %{
       links: [
+        %{
+          rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
+          href: Web.base_url() <> "/nodeinfo/2.0.json"
+        },
         %{
           rel: "http://nodeinfo.diaspora.software/ns/schema/2.1",
           href: Web.base_url() <> "/nodeinfo/2.1.json"
@@ -26,8 +30,9 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
     json(conn, response)
   end
 
-  # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
-  def nodeinfo(conn, %{"version" => "2.1"}) do
+  # returns a nodeinfo 2.0 map, since 2.1 just adds a repository field
+  # under software.
+  def raw_nodeinfo() do
     instance = Application.get_env(:pleroma, :instance)
     media_proxy = Application.get_env(:pleroma, :media_proxy)
     suggestions = Application.get_env(:pleroma, :suggestions)
@@ -98,12 +103,11 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
       ]
       |> Enum.filter(& &1)
 
-    response = %{
-      version: "2.1",
+    %{
+      version: "2.0",
       software: %{
         name: Pleroma.Application.name(),
         version: Pleroma.Application.version(),
-        repository: Pleroma.Application.repository()
       },
       protocols: ["ostatus", "activitypub"],
       services: %{
@@ -143,6 +147,28 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
         restrictedNicknames: Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
       }
     }
+  end
+
+  # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
+  # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
+  def nodeinfo(conn, %{"version" => "2.0"}) do
+    conn
+    |> put_resp_header(
+      "content-type",
+      "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
+    )
+    |> json(raw_nodeinfo())
+  end
+
+  def nodeinfo(conn, %{"version" => "2.1"}) do
+    raw_response = raw_nodeinfo()
+
+    updated_software =
+      raw_response
+      |> Map.get(:software)
+      |> Map.put(:repository, Pleroma.Application.repository())
+
+    response = raw_response |> Map.put(:software, updated_software)
 
     conn
     |> put_resp_header(