move to 2.5.5
[anni] / lib / pleroma / web / preload / providers / instance.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Preload.Providers.Instance do
6   alias Pleroma.Web.MastodonAPI.InstanceView
7   alias Pleroma.Web.Nodeinfo.Nodeinfo
8   alias Pleroma.Web.Plugs.InstanceStatic
9   alias Pleroma.Web.Preload.Providers.Provider
10   alias Pleroma.Web.TwitterAPI.UtilView
11
12   @behaviour Provider
13   @instance_url "/api/v1/instance"
14   @panel_url "/instance/panel.html"
15   @nodeinfo_url "/nodeinfo/2.0.json"
16   @fe_config_url "/api/pleroma/frontend_configurations"
17
18   @impl Provider
19   def generate_terms(_params) do
20     %{}
21     |> build_info_tag()
22     |> build_panel_tag()
23     |> build_nodeinfo_tag()
24     |> build_fe_config_tag()
25   end
26
27   defp build_info_tag(acc) do
28     info_data = InstanceView.render("show.json", %{})
29
30     Map.put(acc, @instance_url, info_data)
31   end
32
33   defp build_panel_tag(acc) do
34     instance_path = InstanceStatic.file_path(@panel_url |> to_string())
35
36     if File.exists?(instance_path) do
37       panel_data = File.read!(instance_path)
38       Map.put(acc, @panel_url, panel_data)
39     else
40       acc
41     end
42   end
43
44   defp build_nodeinfo_tag(acc) do
45     case Nodeinfo.get_nodeinfo("2.0") do
46       {:error, _} ->
47         acc
48
49       nodeinfo_data ->
50         Map.put(acc, @nodeinfo_url, nodeinfo_data)
51     end
52   end
53
54   defp build_fe_config_tag(acc) do
55     fe_data = UtilView.render("frontend_configurations.json", %{})
56
57     Map.put(acc, @fe_config_url, fe_data)
58   end
59 end