Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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 if Config.get([:media_proxy, :enabled]) do
62 "media_proxy"
63 end,
64 if Config.get([:gopher, :enabled]) do
65 "gopher"
66 end,
67 if Config.get([:chat, :enabled]) do
68 "chat"
69 end,
70 if Config.get([:suggestions, :enabled]) do
71 "suggestions"
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: stats.user_count || 0
97 },
98 localPosts: 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: Config.get([:suggestions, :enabled], false),
106 thirdPartyEngine: Config.get([:suggestions, :third_party_engine], ""),
107 timeout: Config.get([:suggestions, :timeout], 5000),
108 limit: Config.get([:suggestions, :limit], 23),
109 web: Config.get([:suggestions, :web], "")
110 },
111 staffAccounts: staff_accounts,
112 federation: federation_response,
113 pollLimits: Config.get([:instance, :poll_limits]),
114 postFormats: Config.get([:instance, :allowed_post_formats]),
115 uploadLimits: %{
116 general: Config.get([:instance, :upload_limit]),
117 avatar: Config.get([:instance, :avatar_upload_limit]),
118 banner: Config.get([:instance, :banner_upload_limit]),
119 background: Config.get([:instance, :background_upload_limit])
120 },
121 accountActivationRequired: Config.get([:instance, :account_activation_required], false),
122 invitesEnabled: Config.get([:instance, :invites_enabled], false),
123 mailerEnabled: Config.get([Pleroma.Emails.Mailer, :enabled], false),
124 features: features,
125 restrictedNicknames: Config.get([Pleroma.User, :restricted_nicknames]),
126 skipThreadContainment: Config.get([:instance, :skip_thread_containment], false)
127 }
128 }
129 end
130
131 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
132 # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
133 def nodeinfo(conn, %{"version" => "2.0"}) do
134 conn
135 |> put_resp_header(
136 "content-type",
137 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
138 )
139 |> json(raw_nodeinfo())
140 end
141
142 def nodeinfo(conn, %{"version" => "2.1"}) do
143 raw_response = raw_nodeinfo()
144
145 updated_software =
146 raw_response
147 |> Map.get(:software)
148 |> Map.put(:repository, Pleroma.Application.repository())
149
150 response =
151 raw_response
152 |> Map.put(:software, updated_software)
153 |> Map.put(:version, "2.1")
154
155 conn
156 |> put_resp_header(
157 "content-type",
158 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#; charset=utf-8"
159 )
160 |> json(response)
161 end
162
163 def nodeinfo(conn, _) do
164 render_error(conn, :not_found, "Nodeinfo schema version not handled")
165 end
166 end