30838b1eb76d467845144f1437c49a42eb38a6ea
[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
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 else
50 %{}
51 end
52 |> Map.put(:enabled, Config.get([:instance, :federating]))
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 "pleroma:api/v1/notifications:include_types_filter",
64 if Config.get([:media_proxy, :enabled]) do
65 "media_proxy"
66 end,
67 if Config.get([:gopher, :enabled]) do
68 "gopher"
69 end,
70 if Config.get([:chat, :enabled]) do
71 "chat"
72 end,
73 if Config.get([:instance, :allow_relay]) do
74 "relay"
75 end,
76 if Config.get([:instance, :safe_dm_mentions]) do
77 "safe_dm_mentions"
78 end
79 ]
80 |> Enum.filter(& &1)
81
82 %{
83 version: "2.0",
84 software: %{
85 name: Pleroma.Application.name() |> String.downcase(),
86 version: Pleroma.Application.version()
87 },
88 protocols: Publisher.gather_nodeinfo_protocol_names(),
89 services: %{
90 inbound: [],
91 outbound: []
92 },
93 openRegistrations: Config.get([:instance, :registrations_open]),
94 usage: %{
95 users: %{
96 total: Map.get(stats, :user_count, 0)
97 },
98 localPosts: Map.get(stats, :status_count, 0)
99 },
100 metadata: %{
101 nodeName: Config.get([:instance, :name]),
102 nodeDescription: Config.get([:instance, :description]),
103 private: !Config.get([:instance, :public], true),
104 suggestions: %{
105 enabled: false
106 },
107 staffAccounts: staff_accounts,
108 federation: federation_response,
109 pollLimits: Config.get([:instance, :poll_limits]),
110 postFormats: Config.get([:instance, :allowed_post_formats]),
111 uploadLimits: %{
112 general: Config.get([:instance, :upload_limit]),
113 avatar: Config.get([:instance, :avatar_upload_limit]),
114 banner: Config.get([:instance, :banner_upload_limit]),
115 background: Config.get([:instance, :background_upload_limit])
116 },
117 fieldsLimits: %{
118 maxFields: Config.get([:instance, :max_account_fields]),
119 maxRemoteFields: Config.get([:instance, :max_remote_account_fields]),
120 nameLength: Config.get([:instance, :account_field_name_length]),
121 valueLength: Config.get([:instance, :account_field_value_length])
122 },
123 accountActivationRequired: Config.get([:instance, :account_activation_required], false),
124 invitesEnabled: Config.get([:instance, :invites_enabled], false),
125 mailerEnabled: Config.get([Pleroma.Emails.Mailer, :enabled], false),
126 features: features,
127 restrictedNicknames: Config.get([Pleroma.User, :restricted_nicknames]),
128 skipThreadContainment: Config.get([:instance, :skip_thread_containment], false)
129 }
130 }
131 end
132
133 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
134 # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
135 def nodeinfo(conn, %{"version" => "2.0"}) do
136 conn
137 |> put_resp_header(
138 "content-type",
139 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
140 )
141 |> json(raw_nodeinfo())
142 end
143
144 def nodeinfo(conn, %{"version" => "2.1"}) do
145 raw_response = raw_nodeinfo()
146
147 updated_software =
148 raw_response
149 |> Map.get(:software)
150 |> Map.put(:repository, Pleroma.Application.repository())
151
152 response =
153 raw_response
154 |> Map.put(:software, updated_software)
155 |> Map.put(:version, "2.1")
156
157 conn
158 |> put_resp_header(
159 "content-type",
160 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#; charset=utf-8"
161 )
162 |> json(response)
163 end
164
165 def nodeinfo(conn, _) do
166 render_error(conn, :not_found, "Nodeinfo schema version not handled")
167 end
168 end