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