From 67dadd954ee0bc0c42b95a9ca0e471051a75a7b6 Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Wed, 2 May 2018 21:31:42 +0200
Subject: [PATCH 1/9] Add first try at nodeinfo

---
 lib/pleroma/web/nodeinfo/nodeinfo.ex          |  2 +
 .../web/nodeinfo/nodeinfo_controller.ex       | 53 +++++++++++++++++++
 lib/pleroma/web/router.ex                     |  5 ++
 3 files changed, 60 insertions(+)
 create mode 100644 lib/pleroma/web/nodeinfo/nodeinfo.ex
 create mode 100644 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo.ex b/lib/pleroma/web/nodeinfo/nodeinfo.ex
new file mode 100644
index 000000000..139597f9c
--- /dev/null
+++ b/lib/pleroma/web/nodeinfo/nodeinfo.ex
@@ -0,0 +1,2 @@
+
+
diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
new file mode 100644
index 000000000..2c06875f2
--- /dev/null
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -0,0 +1,53 @@
+defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
+  use Pleroma.Web, :controller
+
+  alias Pleroma.Web.Nodeinfo
+  alias Pleroma.Stats
+  alias Pleroma.Web
+
+  @instance Application.get_env(:pleroma, :instance)
+
+  def schemas(conn, _params) do
+    response = %{
+      links: [
+        %{
+          rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
+          href: Web.base_url() <> "/nodeinfo/2.0"
+        }
+      ]
+    }
+
+    json(conn, response)
+  end
+
+  # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
+  def nodeinfo(conn, %{"version" => "2.0"}) do
+    response = %{
+      version: "2.0",
+      software: %{
+        name: "pleroma",
+        version: "#{Keyword.get(@instance, :version)})"
+      },
+      protocols: ["ostatus", "activitypub"],
+      services: %{
+        inbound: [],
+        outbound: []
+      },
+      openRegistrations: Keyword.get(@instance, :registrations_open),
+      usage: %{
+        users: %{
+          total: Stats.get_stats().user_count,
+        }
+      },
+      metadata: %{}
+    }
+
+    json(conn, response)
+  end
+
+  def nodeinfo(conn, _) do
+    conn
+    |> put_status(404)
+    |> json(%{error: "Nodeinfo schema not handled"})
+  end
+end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index cecf5527c..c025dea33 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -295,6 +295,11 @@ def user_fetcher(username) do
 
       get("/host-meta", WebFinger.WebFingerController, :host_meta)
       get("/webfinger", WebFinger.WebFingerController, :webfinger)
+      get("/nodeinfo", Nodeinfo.NodeinfoController, :schemas)
+    end
+
+    scope "/nodeinfo", Pleroma.Web do
+      get("/:version", Nodeinfo.NodeinfoController, :nodeinfo)
     end
   end
 

From 65168393d888ce071a1b17cb88aabcefb94b3c34 Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Wed, 2 May 2018 21:43:12 +0200
Subject: [PATCH 2/9] Format files

---
 lib/pleroma/web/nodeinfo/nodeinfo.ex            | 1 -
 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo.ex b/lib/pleroma/web/nodeinfo/nodeinfo.ex
index 139597f9c..8b1378917 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo.ex
@@ -1,2 +1 @@
 
-
diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 2c06875f2..7b152e315 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -36,7 +36,7 @@ def nodeinfo(conn, %{"version" => "2.0"}) do
       openRegistrations: Keyword.get(@instance, :registrations_open),
       usage: %{
         users: %{
-          total: Stats.get_stats().user_count,
+          total: Stats.get_stats().user_count
         }
       },
       metadata: %{}

From abeb009accaece7f05e8a16a4b5bc28cd6682bc0 Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Wed, 2 May 2018 22:16:19 +0200
Subject: [PATCH 3/9] Change path of nodeinfo to use .json format

---
 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 7b152e315..4f18a9c33 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -12,7 +12,7 @@ def schemas(conn, _params) do
       links: [
         %{
           rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
-          href: Web.base_url() <> "/nodeinfo/2.0"
+          href: Web.base_url() <> "/nodeinfo/2.0.json"
         }
       ]
     }

From b8789cc8b68c64803dc2243768eda6c74837ba3c Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Wed, 2 May 2018 22:36:19 +0200
Subject: [PATCH 4/9] Add localPosts count, also set to 0 if null

---
 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 4f18a9c33..6db0d19e3 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -36,8 +36,9 @@ def nodeinfo(conn, %{"version" => "2.0"}) do
       openRegistrations: Keyword.get(@instance, :registrations_open),
       usage: %{
         users: %{
-          total: Stats.get_stats().user_count
-        }
+          total: Stats.get_stats().user_count || 0
+        },
+        localPosts: Stats.get_stats().status_count || 0
       },
       metadata: %{}
     }

From fd0bf3aa71a67afab3cfe55d17d7dce9e2f2b305 Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Wed, 2 May 2018 22:44:13 +0200
Subject: [PATCH 5/9] Simplify version and fix typo

---
 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 6db0d19e3..722518763 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -26,7 +26,7 @@ def nodeinfo(conn, %{"version" => "2.0"}) do
       version: "2.0",
       software: %{
         name: "pleroma",
-        version: "#{Keyword.get(@instance, :version)})"
+        version: Keyword.get(@instance, :version)
       },
       protocols: ["ostatus", "activitypub"],
       services: %{

From e448734952088fb15b5d2f3be6c27041a6ba7efc Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Wed, 2 May 2018 22:45:20 +0200
Subject: [PATCH 6/9] Explicit error message

---
 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 722518763..bf845a442 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -49,6 +49,6 @@ def nodeinfo(conn, %{"version" => "2.0"}) do
   def nodeinfo(conn, _) do
     conn
     |> put_status(404)
-    |> json(%{error: "Nodeinfo schema not handled"})
+    |> json(%{error: "Nodeinfo schema version not handled"})
   end
 end

From 804497542d8a0488b697bca1547e3f564e308a2e Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Thu, 3 May 2018 10:50:02 +0200
Subject: [PATCH 7/9] Return the right content-type for the schema call

---
 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index bf845a442..534a858ae 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -43,7 +43,12 @@ def nodeinfo(conn, %{"version" => "2.0"}) do
       metadata: %{}
     }
 
-    json(conn, response)
+    conn
+    |> put_resp_header(
+      "content-type",
+      "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
+    )
+    |> json(response)
   end
 
   def nodeinfo(conn, _) do

From 724f096ac76f9f4c124721e3f0c2675655d7a8d0 Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Thu, 3 May 2018 10:52:20 +0200
Subject: [PATCH 8/9] Add metadata.nodeName

---
 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 534a858ae..83ad4f284 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -40,7 +40,9 @@ def nodeinfo(conn, %{"version" => "2.0"}) do
         },
         localPosts: Stats.get_stats().status_count || 0
       },
-      metadata: %{}
+      metadata: %{
+        nodeName: Keyword.get(@instance, :name)
+      }
     }
 
     conn

From c3f9e24c70722366a31c20eae24fdb81790df2f8 Mon Sep 17 00:00:00 2001
From: Dashie <dashie@sigpipe.me>
Date: Thu, 3 May 2018 15:31:39 +0200
Subject: [PATCH 9/9] Use a variable for stats to avoid double call

---
 lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
index 83ad4f284..36a7802ed 100644
--- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
+++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex
@@ -22,6 +22,8 @@ def schemas(conn, _params) do
 
   # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
   def nodeinfo(conn, %{"version" => "2.0"}) do
+    stats = Stats.get_stats()
+
     response = %{
       version: "2.0",
       software: %{
@@ -36,9 +38,9 @@ def nodeinfo(conn, %{"version" => "2.0"}) do
       openRegistrations: Keyword.get(@instance, :registrations_open),
       usage: %{
         users: %{
-          total: Stats.get_stats().user_count || 0
+          total: stats.user_count || 0
         },
-        localPosts: Stats.get_stats().status_count || 0
+        localPosts: stats.status_count || 0
       },
       metadata: %{
         nodeName: Keyword.get(@instance, :name)