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