21694a5ee4983b0dc383c19705d8302c1f676d45
[akkoma] / lib / pleroma / web / nodeinfo / nodeinfo_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Nodeinfo.NodeinfoController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Stats
9 alias Pleroma.Web
10 alias Pleroma.{User, Repo}
11 alias Pleroma.Config
12 alias Pleroma.Web.ActivityPub.MRF
13
14 plug(Pleroma.Web.FederatingPlug)
15
16 def schemas(conn, _params) do
17 response = %{
18 links: [
19 %{
20 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
21 href: Web.base_url() <> "/nodeinfo/2.0.json"
22 },
23 %{
24 rel: "http://nodeinfo.diaspora.software/ns/schema/2.1",
25 href: Web.base_url() <> "/nodeinfo/2.1.json"
26 }
27 ]
28 }
29
30 json(conn, response)
31 end
32
33 # returns a nodeinfo 2.0 map, since 2.1 just adds a repository field
34 # under software.
35 def raw_nodeinfo() do
36 instance = Application.get_env(:pleroma, :instance)
37 media_proxy = Application.get_env(:pleroma, :media_proxy)
38 suggestions = Application.get_env(:pleroma, :suggestions)
39 chat = Application.get_env(:pleroma, :chat)
40 gopher = Application.get_env(:pleroma, :gopher)
41 stats = Stats.get_stats()
42
43 mrf_simple =
44 Application.get_env(:pleroma, :mrf_simple)
45 |> Enum.into(%{})
46
47 mrf_policies =
48 MRF.get_policies()
49 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
50
51 quarantined = Keyword.get(instance, :quarantined_instances)
52
53 quarantined =
54 if is_list(quarantined) do
55 quarantined
56 else
57 []
58 end
59
60 staff_accounts =
61 User.moderator_user_query()
62 |> Repo.all()
63 |> Enum.map(fn u -> u.ap_id end)
64
65 mrf_user_allowlist =
66 Config.get([:mrf_user_allowlist], [])
67 |> Enum.into(%{}, fn {k, v} -> {k, length(v)} end)
68
69 mrf_transparency = Keyword.get(instance, :mrf_transparency)
70
71 federation_response =
72 if mrf_transparency do
73 %{
74 mrf_policies: mrf_policies,
75 mrf_simple: mrf_simple,
76 mrf_user_allowlist: mrf_user_allowlist,
77 quarantined_instances: quarantined
78 }
79 else
80 %{}
81 end
82
83 features =
84 [
85 "pleroma_api",
86 "mastodon_api",
87 "mastodon_api_streaming",
88 if Keyword.get(media_proxy, :enabled) do
89 "media_proxy"
90 end,
91 if Keyword.get(gopher, :enabled) do
92 "gopher"
93 end,
94 if Keyword.get(chat, :enabled) do
95 "chat"
96 end,
97 if Keyword.get(suggestions, :enabled) do
98 "suggestions"
99 end,
100 if Keyword.get(instance, :allow_relay) do
101 "relay"
102 end
103 ]
104 |> Enum.filter(& &1)
105
106 %{
107 version: "2.0",
108 software: %{
109 name: Pleroma.Application.name() |> String.downcase(),
110 version: Pleroma.Application.version()
111 },
112 protocols: ["ostatus", "activitypub"],
113 services: %{
114 inbound: [],
115 outbound: []
116 },
117 openRegistrations: Keyword.get(instance, :registrations_open),
118 usage: %{
119 users: %{
120 total: stats.user_count || 0
121 },
122 localPosts: stats.status_count || 0
123 },
124 metadata: %{
125 nodeName: Keyword.get(instance, :name),
126 nodeDescription: Keyword.get(instance, :description),
127 private: !Keyword.get(instance, :public, true),
128 suggestions: %{
129 enabled: Keyword.get(suggestions, :enabled, false),
130 thirdPartyEngine: Keyword.get(suggestions, :third_party_engine, ""),
131 timeout: Keyword.get(suggestions, :timeout, 5000),
132 limit: Keyword.get(suggestions, :limit, 23),
133 web: Keyword.get(suggestions, :web, "")
134 },
135 staffAccounts: staff_accounts,
136 federation: federation_response,
137 postFormats: Keyword.get(instance, :allowed_post_formats),
138 uploadLimits: %{
139 general: Keyword.get(instance, :upload_limit),
140 avatar: Keyword.get(instance, :avatar_upload_limit),
141 banner: Keyword.get(instance, :banner_upload_limit),
142 background: Keyword.get(instance, :background_upload_limit)
143 },
144 accountActivationRequired: Keyword.get(instance, :account_activation_required, false),
145 invitesEnabled: Keyword.get(instance, :invites_enabled, false),
146 features: features,
147 restrictedNicknames: Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
148 }
149 }
150 end
151
152 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
153 # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
154 def nodeinfo(conn, %{"version" => "2.0"}) do
155 conn
156 |> put_resp_header(
157 "content-type",
158 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
159 )
160 |> json(raw_nodeinfo())
161 end
162
163 def nodeinfo(conn, %{"version" => "2.1"}) do
164 raw_response = raw_nodeinfo()
165
166 updated_software =
167 raw_response
168 |> Map.get(:software)
169 |> Map.put(:repository, Pleroma.Application.repository())
170
171 response =
172 raw_response
173 |> Map.put(:software, updated_software)
174 |> Map.put(:version, "2.1")
175
176 conn
177 |> put_resp_header(
178 "content-type",
179 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#; charset=utf-8"
180 )
181 |> json(response)
182 end
183
184 def nodeinfo(conn, _) do
185 conn
186 |> put_status(404)
187 |> json(%{error: "Nodeinfo schema version not handled"})
188 end
189 end