Merge branch 'fix-tlsv1.3' into 'develop'
[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.Config
9 alias Pleroma.Stats
10 alias Pleroma.User
11 alias Pleroma.Web
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 # This horror is needed to convert regex sigils to strings
48 mrf_keyword =
49 Application.get_env(:pleroma, :mrf_keyword, [])
50 |> Enum.map(fn {key, value} ->
51 {key,
52 Enum.map(value, fn
53 {pattern, replacement} ->
54 %{
55 "pattern" =>
56 if not is_binary(pattern) do
57 inspect(pattern)
58 else
59 pattern
60 end,
61 "replacement" => replacement
62 }
63
64 pattern ->
65 if not is_binary(pattern) do
66 inspect(pattern)
67 else
68 pattern
69 end
70 end)}
71 end)
72 |> Enum.into(%{})
73
74 mrf_policies =
75 MRF.get_policies()
76 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
77
78 quarantined = Keyword.get(instance, :quarantined_instances)
79
80 quarantined =
81 if is_list(quarantined) do
82 quarantined
83 else
84 []
85 end
86
87 staff_accounts =
88 User.all_superusers()
89 |> Enum.map(fn u -> u.ap_id end)
90
91 mrf_user_allowlist =
92 Config.get([:mrf_user_allowlist], [])
93 |> Enum.into(%{}, fn {k, v} -> {k, length(v)} end)
94
95 federation_response =
96 if Keyword.get(instance, :mrf_transparency) do
97 %{
98 mrf_policies: mrf_policies,
99 mrf_simple: mrf_simple,
100 mrf_keyword: mrf_keyword,
101 mrf_user_allowlist: mrf_user_allowlist,
102 quarantined_instances: quarantined
103 }
104 else
105 %{}
106 end
107
108 features =
109 [
110 "pleroma_api",
111 "mastodon_api",
112 "mastodon_api_streaming",
113 if Keyword.get(media_proxy, :enabled) do
114 "media_proxy"
115 end,
116 if Keyword.get(gopher, :enabled) do
117 "gopher"
118 end,
119 if Keyword.get(chat, :enabled) do
120 "chat"
121 end,
122 if Keyword.get(suggestions, :enabled) do
123 "suggestions"
124 end,
125 if Keyword.get(instance, :allow_relay) do
126 "relay"
127 end
128 ]
129 |> Enum.filter(& &1)
130
131 %{
132 version: "2.0",
133 software: %{
134 name: Pleroma.Application.name() |> String.downcase(),
135 version: Pleroma.Application.version()
136 },
137 protocols: ["ostatus", "activitypub"],
138 services: %{
139 inbound: [],
140 outbound: []
141 },
142 openRegistrations: Keyword.get(instance, :registrations_open),
143 usage: %{
144 users: %{
145 total: stats.user_count || 0
146 },
147 localPosts: stats.status_count || 0
148 },
149 metadata: %{
150 nodeName: Keyword.get(instance, :name),
151 nodeDescription: Keyword.get(instance, :description),
152 private: !Keyword.get(instance, :public, true),
153 suggestions: %{
154 enabled: Keyword.get(suggestions, :enabled, false),
155 thirdPartyEngine: Keyword.get(suggestions, :third_party_engine, ""),
156 timeout: Keyword.get(suggestions, :timeout, 5000),
157 limit: Keyword.get(suggestions, :limit, 23),
158 web: Keyword.get(suggestions, :web, "")
159 },
160 staffAccounts: staff_accounts,
161 federation: federation_response,
162 postFormats: Keyword.get(instance, :allowed_post_formats),
163 uploadLimits: %{
164 general: Keyword.get(instance, :upload_limit),
165 avatar: Keyword.get(instance, :avatar_upload_limit),
166 banner: Keyword.get(instance, :banner_upload_limit),
167 background: Keyword.get(instance, :background_upload_limit)
168 },
169 accountActivationRequired: Keyword.get(instance, :account_activation_required, false),
170 invitesEnabled: Keyword.get(instance, :invites_enabled, false),
171 features: features,
172 restrictedNicknames: Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
173 }
174 }
175 end
176
177 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
178 # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
179 def nodeinfo(conn, %{"version" => "2.0"}) do
180 conn
181 |> put_resp_header(
182 "content-type",
183 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
184 )
185 |> json(raw_nodeinfo())
186 end
187
188 def nodeinfo(conn, %{"version" => "2.1"}) do
189 raw_response = raw_nodeinfo()
190
191 updated_software =
192 raw_response
193 |> Map.get(:software)
194 |> Map.put(:repository, Pleroma.Application.repository())
195
196 response =
197 raw_response
198 |> Map.put(:software, updated_software)
199 |> Map.put(:version, "2.1")
200
201 conn
202 |> put_resp_header(
203 "content-type",
204 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#; charset=utf-8"
205 )
206 |> json(response)
207 end
208
209 def nodeinfo(conn, _) do
210 conn
211 |> put_status(404)
212 |> json(%{error: "Nodeinfo schema version not handled"})
213 end
214 end