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