Web.Federator: Fix unused variable
[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, Repo, Stats, User, Web}
9 alias Pleroma.Web.ActivityPub.MRF
10
11 plug(Pleroma.Web.FederatingPlug)
12
13 def schemas(conn, _params) do
14 response = %{
15 links: [
16 %{
17 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
18 href: Web.base_url() <> "/nodeinfo/2.0.json"
19 },
20 %{
21 rel: "http://nodeinfo.diaspora.software/ns/schema/2.1",
22 href: Web.base_url() <> "/nodeinfo/2.1.json"
23 }
24 ]
25 }
26
27 json(conn, response)
28 end
29
30 # returns a nodeinfo 2.0 map, since 2.1 just adds a repository field
31 # under software.
32 def raw_nodeinfo do
33 instance = Application.get_env(:pleroma, :instance)
34 media_proxy = Application.get_env(:pleroma, :media_proxy)
35 suggestions = Application.get_env(:pleroma, :suggestions)
36 chat = Application.get_env(:pleroma, :chat)
37 gopher = Application.get_env(:pleroma, :gopher)
38 stats = Stats.get_stats()
39
40 mrf_simple =
41 Application.get_env(:pleroma, :mrf_simple)
42 |> Enum.into(%{})
43
44 mrf_policies =
45 MRF.get_policies()
46 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
47
48 quarantined = Keyword.get(instance, :quarantined_instances)
49
50 quarantined =
51 if is_list(quarantined) do
52 quarantined
53 else
54 []
55 end
56
57 staff_accounts =
58 User.moderator_user_query()
59 |> Repo.all()
60 |> Enum.map(fn u -> u.ap_id end)
61
62 mrf_user_allowlist =
63 Config.get([:mrf_user_allowlist], [])
64 |> Enum.into(%{}, fn {k, v} -> {k, length(v)} end)
65
66 federation_response =
67 if Keyword.get(instance, :mrf_transparency) do
68 %{
69 mrf_policies: mrf_policies,
70 mrf_simple: mrf_simple,
71 mrf_user_allowlist: mrf_user_allowlist,
72 quarantined_instances: quarantined
73 }
74 else
75 %{}
76 end
77
78 features =
79 [
80 "pleroma_api",
81 "mastodon_api",
82 "mastodon_api_streaming",
83 if Keyword.get(media_proxy, :enabled) do
84 "media_proxy"
85 end,
86 if Keyword.get(gopher, :enabled) do
87 "gopher"
88 end,
89 if Keyword.get(chat, :enabled) do
90 "chat"
91 end,
92 if Keyword.get(suggestions, :enabled) do
93 "suggestions"
94 end,
95 if Keyword.get(instance, :allow_relay) do
96 "relay"
97 end
98 ]
99 |> Enum.filter(& &1)
100
101 %{
102 version: "2.0",
103 software: %{
104 name: Pleroma.Application.name() |> String.downcase(),
105 version: Pleroma.Application.version()
106 },
107 protocols: ["ostatus", "activitypub"],
108 services: %{
109 inbound: [],
110 outbound: []
111 },
112 openRegistrations: Keyword.get(instance, :registrations_open),
113 usage: %{
114 users: %{
115 total: stats.user_count || 0
116 },
117 localPosts: stats.status_count || 0
118 },
119 metadata: %{
120 nodeName: Keyword.get(instance, :name),
121 nodeDescription: Keyword.get(instance, :description),
122 private: !Keyword.get(instance, :public, true),
123 suggestions: %{
124 enabled: Keyword.get(suggestions, :enabled, false),
125 thirdPartyEngine: Keyword.get(suggestions, :third_party_engine, ""),
126 timeout: Keyword.get(suggestions, :timeout, 5000),
127 limit: Keyword.get(suggestions, :limit, 23),
128 web: Keyword.get(suggestions, :web, "")
129 },
130 staffAccounts: staff_accounts,
131 federation: federation_response,
132 postFormats: Keyword.get(instance, :allowed_post_formats),
133 uploadLimits: %{
134 general: Keyword.get(instance, :upload_limit),
135 avatar: Keyword.get(instance, :avatar_upload_limit),
136 banner: Keyword.get(instance, :banner_upload_limit),
137 background: Keyword.get(instance, :background_upload_limit)
138 },
139 accountActivationRequired: Keyword.get(instance, :account_activation_required, false),
140 invitesEnabled: Keyword.get(instance, :invites_enabled, false),
141 features: features,
142 restrictedNicknames: Pleroma.Config.get([Pleroma.User, :restricted_nicknames])
143 }
144 }
145 end
146
147 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
148 # and https://github.com/jhass/nodeinfo/blob/master/schemas/2.1/schema.json
149 def nodeinfo(conn, %{"version" => "2.0"}) do
150 conn
151 |> put_resp_header(
152 "content-type",
153 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
154 )
155 |> json(raw_nodeinfo())
156 end
157
158 def nodeinfo(conn, %{"version" => "2.1"}) do
159 raw_response = raw_nodeinfo()
160
161 updated_software =
162 raw_response
163 |> Map.get(:software)
164 |> Map.put(:repository, Pleroma.Application.repository())
165
166 response =
167 raw_response
168 |> Map.put(:software, updated_software)
169 |> Map.put(:version, "2.1")
170
171 conn
172 |> put_resp_header(
173 "content-type",
174 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.1#; charset=utf-8"
175 )
176 |> json(response)
177 end
178
179 def nodeinfo(conn, _) do
180 conn
181 |> put_status(404)
182 |> json(%{error: "Nodeinfo schema version not handled"})
183 end
184 end