Merge branch 'develop' into feature/reports-groups-and-multiple-state-update
[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 else
50 %{}
51 end
52
53 features =
54 [
55 "pleroma_api",
56 "mastodon_api",
57 "mastodon_api_streaming",
58 "polls",
59 "pleroma_explicit_addressing",
60 "shareable_emoji_packs",
61 "multifetch",
62 if Config.get([:media_proxy, :enabled]) do
63 "media_proxy"
64 end,
65 if Config.get([:gopher, :enabled]) do
66 "gopher"
67 end,
68 if Config.get([:chat, :enabled]) do
69 "chat"
70 end,
71 if Config.get([:suggestions, :enabled]) do
72 "suggestions"
73 end,
74 if Config.get([:instance, :allow_relay]) do
75 "relay"
76 end,
77 if Config.get([:instance, :safe_dm_mentions]) do
78 "safe_dm_mentions"
79 end
80 ]
81 |> Enum.filter(& &1)
82
83 %{
84 version: "2.0",
85 software: %{
86 name: Pleroma.Application.name() |> String.downcase(),
87 version: Pleroma.Application.version()
88 },
89 protocols: Publisher.gather_nodeinfo_protocol_names(),
90 services: %{
91 inbound: [],
92 outbound: []
93 },
94 openRegistrations: Config.get([:instance, :registrations_open]),
95 usage: %{
96 users: %{
97 total: stats.user_count || 0
98 },
99 localPosts: stats.status_count || 0
100 },
101 metadata: %{
102 nodeName: Config.get([:instance, :name]),
103 nodeDescription: Config.get([:instance, :description]),
104 private: !Config.get([:instance, :public], true),
105 suggestions: %{
106 enabled: Config.get([:suggestions, :enabled], false),
107 thirdPartyEngine: Config.get([:suggestions, :third_party_engine], ""),
108 timeout: Config.get([:suggestions, :timeout], 5000),
109 limit: Config.get([:suggestions, :limit], 23),
110 web: Config.get([:suggestions, :web], "")
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 accountActivationRequired: Config.get([:instance, :account_activation_required], false),
123 invitesEnabled: Config.get([:instance, :invites_enabled], false),
124 mailerEnabled: Config.get([Pleroma.Emails.Mailer, :enabled], false),
125 features: features,
126 restrictedNicknames: Config.get([Pleroma.User, :restricted_nicknames]),
127 skipThreadContainment: Config.get([:instance, :skip_thread_containment], false)
128 }
129 }
130 end
131
132 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
133 # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
134 def nodeinfo(conn, %{"version" => "2.0"}) do
135 conn
136 |> put_resp_header(
137 "content-type",
138 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
139 )
140 |> json(raw_nodeinfo())
141 end
142
143 def nodeinfo(conn, %{"version" => "2.1"}) do
144 raw_response = raw_nodeinfo()
145
146 updated_software =
147 raw_response
148 |> Map.get(:software)
149 |> Map.put(:repository, Pleroma.Application.repository())
150
151 response =
152 raw_response
153 |> Map.put(:software, updated_software)
154 |> Map.put(:version, "2.1")
155
156 conn
157 |> put_resp_header(
158 "content-type",
159 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#; charset=utf-8"
160 )
161 |> json(response)
162 end
163
164 def nodeinfo(conn, _) do
165 render_error(conn, :not_found, "Nodeinfo schema version not handled")
166 end
167 end