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