Merge branch 'revert-d31bbb1c' 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
8 def schemas(conn, _params) do
9 response = %{
10 links: [
11 %{
12 rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
13 href: Web.base_url() <> "/nodeinfo/2.0.json"
14 }
15 ]
16 }
17
18 json(conn, response)
19 end
20
21 # Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
22 def nodeinfo(conn, %{"version" => "2.0"}) do
23 instance = Application.get_env(:pleroma, :instance)
24 media_proxy = Application.get_env(:pleroma, :media_proxy)
25 suggestions = Application.get_env(:pleroma, :suggestions)
26 chat = Application.get_env(:pleroma, :chat)
27 gopher = Application.get_env(:pleroma, :gopher)
28 stats = Stats.get_stats()
29
30 mrf_simple =
31 Application.get_env(:pleroma, :mrf_simple)
32 |> Enum.into(%{})
33
34 mrf_policies = Keyword.get(instance, :rewrite_policy)
35
36 mrf_policies =
37 if(is_list(mrf_policies)) do
38 mrf_policies
39 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
40 else
41 [to_string(mrf_policies) |> String.split(".") |> List.last()]
42 end
43
44 quarantined = Keyword.get(instance, :quarantined_instances)
45
46 quarantined =
47 if is_list(quarantined) do
48 quarantined
49 else
50 []
51 end
52
53 staff_accounts =
54 User.moderator_user_query()
55 |> Repo.all()
56 |> Enum.map(fn u -> u.ap_id end)
57
58 response = %{
59 version: "2.0",
60 software: %{
61 name: "pleroma",
62 version: Keyword.get(instance, :version)
63 },
64 protocols: ["ostatus", "activitypub"],
65 services: %{
66 inbound: [],
67 outbound: []
68 },
69 openRegistrations: Keyword.get(instance, :registrations_open),
70 usage: %{
71 users: %{
72 total: stats.user_count || 0
73 },
74 localPosts: stats.status_count || 0
75 },
76 metadata: %{
77 nodeName: Keyword.get(instance, :name),
78 nodeDescription: Keyword.get(instance, :description),
79 mediaProxy: Keyword.get(media_proxy, :enabled),
80 private: !Keyword.get(instance, :public, true),
81 suggestions: %{
82 enabled: Keyword.get(suggestions, :enabled, false),
83 thirdPartyEngine: Keyword.get(suggestions, :third_party_engine, ""),
84 timeout: Keyword.get(suggestions, :timeout, 5000),
85 limit: Keyword.get(suggestions, :limit, 23),
86 web: Keyword.get(suggestions, :web, "")
87 },
88 staffAccounts: staff_accounts,
89 chat: Keyword.get(chat, :enabled),
90 gopher: Keyword.get(gopher, :enabled),
91 federation: %{
92 mrf_policies: mrf_policies,
93 mrf_simple: mrf_simple,
94 quarantined_instances: quarantined
95 },
96 postFormats: Keyword.get(instance, :allowed_post_formats)
97 }
98 }
99
100 conn
101 |> put_resp_header(
102 "content-type",
103 "application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#; charset=utf-8"
104 )
105 |> json(response)
106 end
107
108 def nodeinfo(conn, _) do
109 conn
110 |> put_status(404)
111 |> json(%{error: "Nodeinfo schema version not handled"})
112 end
113 end