nodeinfo_controller.ex 2.37 KB
Newer Older
dashie's avatar
dashie committed
1
2
3
4
5
defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
  use Pleroma.Web, :controller

  alias Pleroma.Stats
  alias Pleroma.Web
6
  alias Pleroma.{User, Repo}
dashie's avatar
dashie committed
7
8
9
10
11
12

  def schemas(conn, _params) do
    response = %{
      links: [
        %{
          rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
13
          href: Web.base_url() <> "/nodeinfo/2.0.json"
dashie's avatar
dashie committed
14
15
16
17
18
19
20
21
22
        }
      ]
    }

    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
lain's avatar
lain committed
23
24
    instance = Application.get_env(:pleroma, :instance)
    media_proxy = Application.get_env(:pleroma, :media_proxy)
25
    suggestions = Application.get_env(:pleroma, :suggestions)
26
27
    chat = Application.get_env(:pleroma, :chat)
    gopher = Application.get_env(:pleroma, :gopher)
28
29
    stats = Stats.get_stats()

30
31
32
33
34
    staff_accounts =
      User.moderator_user_query()
      |> Repo.all()
      |> Enum.map(fn u -> u.ap_id end)

dashie's avatar
dashie committed
35
36
37
38
    response = %{
      version: "2.0",
      software: %{
        name: "pleroma",
lain's avatar
lain committed
39
        version: Keyword.get(instance, :version)
dashie's avatar
dashie committed
40
41
42
43
44
45
      },
      protocols: ["ostatus", "activitypub"],
      services: %{
        inbound: [],
        outbound: []
      },
lain's avatar
lain committed
46
      openRegistrations: Keyword.get(instance, :registrations_open),
dashie's avatar
dashie committed
47
48
      usage: %{
        users: %{
49
          total: stats.user_count || 0
50
        },
51
        localPosts: stats.status_count || 0
dashie's avatar
dashie committed
52
      },
dashie's avatar
dashie committed
53
      metadata: %{
lain's avatar
lain committed
54
        nodeName: Keyword.get(instance, :name),
55
        nodeDescription: Keyword.get(instance, :description),
56
        mediaProxy: Keyword.get(media_proxy, :enabled),
Hakaba Hitoyo's avatar
merge    
Hakaba Hitoyo committed
57
        private: !Keyword.get(instance, :public, true),
Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
58
59
60
        suggestions: %{
          enabled: Keyword.get(suggestions, :enabled, false),
          thirdPartyEngine: Keyword.get(suggestions, :third_party_engine, ""),
Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
61
62
          timeout: Keyword.get(suggestions, :timeout, 5000),
          web: Keyword.get(suggestions, :web, "")
63
        },
64
        staffAccounts: staff_accounts,
65
66
        chat: Keyword.get(chat, :enabled),
        gopher: Keyword.get(gopher, :enabled)
dashie's avatar
dashie committed
67
      }
dashie's avatar
dashie committed
68
69
    }

70
71
72
73
74
75
    conn
    |> put_resp_header(
      "content-type",
      "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
    )
    |> json(response)
dashie's avatar
dashie committed
76
77
78
79
80
  end

  def nodeinfo(conn, _) do
    conn
    |> put_status(404)
dashie's avatar
dashie committed
81
    |> json(%{error: "Nodeinfo schema version not handled"})
dashie's avatar
dashie committed
82
83
  end
end