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