Merge branch 'update-service-files-of-openrc-and-systemd-to-new-recommended-paths...
[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 # 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.moderator_user_query()
89 |> Repo.all()
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 mrf_transparency = Keyword.get(instance, :mrf_transparency)
97
98 federation_response =
99 if mrf_transparency do
100 %{
101 mrf_policies: mrf_policies,
102 mrf_simple: mrf_simple,
103 mrf_keyword: mrf_keyword,
104 mrf_user_allowlist: mrf_user_allowlist,
105 quarantined_instances: quarantined
106 }
107 else
108 %{}
109 end
110
111 features =
112 [
113 "pleroma_api",
114 "mastodon_api",
115 "mastodon_api_streaming",
116 if Keyword.get(media_proxy, :enabled) do
117 "media_proxy"
118 end,
119 if Keyword.get(gopher, :enabled) do
120 "gopher"
121 end,
122 if Keyword.get(chat, :enabled) do
123 "chat"
124 end,
125 if Keyword.get(suggestions, :enabled) do
126 "suggestions"
127 end,
128 if Keyword.get(instance, :allow_relay) do
129 "relay"
130 end
131 ]
132 |> Enum.filter(& &1)
133
134 %{
135 version: "2.0",
136 software: %{
137 name: Pleroma.Application.name() |> String.downcase(),
138 version: Pleroma.Application.version()
139 },
140 protocols: ["ostatus", "activitypub"],
141 services: %{
142 inbound: [],
143 outbound: []
144 },
145 openRegistrations: Keyword.get(instance, :registrations_open),
146 usage: %{
147 users: %{
148 total: stats.user_count || 0
149 },
150 localPosts: stats.status_count || 0
151 },
152 metadata: %{
153 nodeName: Keyword.get(instance, :name),
154 nodeDescription: Keyword.get(instance, :description),
155 private: !Keyword.get(instance, :public, true),
156 suggestions: %{
157 enabled: Keyword.get(suggestions, :enabled, false),
158 thirdPartyEngine: Keyword.get(suggestions, :third_party_engine, ""),
159 timeout: Keyword.get(suggestions, :timeout, 5000),
160 limit: Keyword.get(suggestions, :limit, 23),
161 web: Keyword.get(suggestions, :web, "")
162 },
163 staffAccounts: staff_accounts,
164 federation: federation_response,
165 postFormats: Keyword.get(instance, :allowed_post_formats),
166 uploadLimits: %{
167 general: Keyword.get(instance, :upload_limit),
168 avatar: Keyword.get(instance, :avatar_upload_limit),
169 banner: Keyword.get(instance, :banner_upload_limit),
170 background: Keyword.get(instance, :background_upload_limit)
171 },
172 accountActivationRequired: Keyword.get(instance, :account_activation_required, false),
173 invitesEnabled: Keyword.get(instance, :invites_enabled, false),
174 features: features,
175 restrictedNicknames: Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
176 }
177 }
178 end
179
180 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
181 # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
182 def nodeinfo(conn, %{"version" => "2.0"}) do
183 conn
184 |> put_resp_header(
185 "content-type",
186 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
187 )
188 |> json(raw_nodeinfo())
189 end
190
191 def nodeinfo(conn, %{"version" => "2.1"}) do
192 raw_response = raw_nodeinfo()
193
194 updated_software =
195 raw_response
196 |> Map.get(:software)
197 |> Map.put(:repository, Pleroma.Application.repository())
198
199 response =
200 raw_response
201 |> Map.put(:software, updated_software)
202 |> Map.put(:version, "2.1")
203
204 conn
205 |> put_resp_header(
206 "content-type",
207 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#; charset=utf-8"
208 )
209 |> json(response)
210 end
211
212 def nodeinfo(conn, _) do
213 conn
214 |> put_status(404)
215 |> json(%{error: "Nodeinfo schema version not handled"})
216 end
217 end