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