nodeinfo_controller.ex 2.53 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
    stats = Stats.get_stats()
29
    mrf_simple = Application.get_env(:pleroma, :mrf_simple)
30

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

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

73
74
75
76
77
78
    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
79
80
81
82
83
  end

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