e4396da319c3ba3d1c938a6b0b164b5efe69302d
[akkoma] / lib / pleroma / web / nodeinfo / nodeinfo_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 exclusions = Config.get([:instance, :mrf_transparency_exclusions], [])
37
38 quarantined =
39 Config.get([:instance, :quarantined_instances], [])
40 |> Enum.filter(fn x -> !Enum.member?(exclusions, x) end)
41
42 staff_accounts =
43 User.all_superusers()
44 |> Enum.map(fn u -> u.ap_id end)
45 |> Enum.filter(fn u -> not Enum.member?(Config.get([:instance, :staff_transparency]), u) end)
46
47 federation_response =
48 if Config.get([:instance, :mrf_transparency]) do
49 {:ok, data} = MRF.describe()
50
51 data
52 |> Map.merge(%{quarantined_instances: quarantined})
53 else
54 %{}
55 end
56 |> Map.put(:enabled, Config.get([:instance, :federating]))
57
58 features =
59 [
60 "pleroma_api",
61 "mastodon_api",
62 "mastodon_api_streaming",
63 "polls",
64 "pleroma_explicit_addressing",
65 "shareable_emoji_packs",
66 "multifetch",
67 "pleroma:api/v1/notifications:include_types_filter",
68 if Config.get([:media_proxy, :enabled]) do
69 "media_proxy"
70 end,
71 if Config.get([:gopher, :enabled]) do
72 "gopher"
73 end,
74 if Config.get([:chat, :enabled]) do
75 "chat"
76 end,
77 if Config.get([:instance, :allow_relay]) do
78 "relay"
79 end,
80 if Config.get([:instance, :safe_dm_mentions]) do
81 "safe_dm_mentions"
82 end,
83 "pleroma_emoji_reactions"
84 ]
85 |> Enum.filter(& &1)
86
87 %{
88 version: "2.0",
89 software: %{
90 name: Pleroma.Application.name() |> String.downcase(),
91 version: Pleroma.Application.version()
92 },
93 protocols: Publisher.gather_nodeinfo_protocol_names(),
94 services: %{
95 inbound: [],
96 outbound: []
97 },
98 openRegistrations: Config.get([:instance, :registrations_open]),
99 usage: %{
100 users: %{
101 total: Map.get(stats, :user_count, 0)
102 },
103 localPosts: Map.get(stats, :status_count, 0)
104 },
105 metadata: %{
106 nodeName: Config.get([:instance, :name]),
107 nodeDescription: Config.get([:instance, :description]),
108 private: !Config.get([:instance, :public], true),
109 suggestions: %{
110 enabled: false
111 },
112 staffAccounts: staff_accounts,
113 federation: federation_response,
114 pollLimits: Config.get([:instance, :poll_limits]),
115 postFormats: Config.get([:instance, :allowed_post_formats]),
116 uploadLimits: %{
117 general: Config.get([:instance, :upload_limit]),
118 avatar: Config.get([:instance, :avatar_upload_limit]),
119 banner: Config.get([:instance, :banner_upload_limit]),
120 background: Config.get([:instance, :background_upload_limit])
121 },
122 fieldsLimits: %{
123 maxFields: Config.get([:instance, :max_account_fields]),
124 maxRemoteFields: Config.get([:instance, :max_remote_account_fields]),
125 nameLength: Config.get([:instance, :account_field_name_length]),
126 valueLength: Config.get([:instance, :account_field_value_length])
127 },
128 accountActivationRequired: Config.get([:instance, :account_activation_required], false),
129 invitesEnabled: Config.get([:instance, :invites_enabled], false),
130 mailerEnabled: Config.get([Pleroma.Emails.Mailer, :enabled], false),
131 features: features,
132 restrictedNicknames: Config.get([Pleroma.User, :restricted_nicknames]),
133 skipThreadContainment: Config.get([:instance, :skip_thread_containment], false)
134 }
135 }
136 end
137
138 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
139 # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
140 def nodeinfo(conn, %{"version" => "2.0"}) do
141 conn
142 |> put_resp_header(
143 "content-type",
144 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
145 )
146 |> json(raw_nodeinfo())
147 end
148
149 def nodeinfo(conn, %{"version" => "2.1"}) do
150 raw_response = raw_nodeinfo()
151
152 updated_software =
153 raw_response
154 |> Map.get(:software)
155 |> Map.put(:repository, Pleroma.Application.repository())
156
157 response =
158 raw_response
159 |> Map.put(:software, updated_software)
160 |> Map.put(:version, "2.1")
161
162 conn
163 |> put_resp_header(
164 "content-type",
165 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#; charset=utf-8"
166 )
167 |> json(response)
168 end
169
170 def nodeinfo(conn, _) do
171 render_error(conn, :not_found, "Nodeinfo schema version not handled")
172 end
173 end