summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/admin_api/controllers/frontend_controller.ex
blob: 145c1fa4a528ad6dc1c3ea11b89e46384c54a420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.AdminAPI.FrontendController do
  use Pleroma.Web, :controller

  alias Pleroma.Config
  alias Pleroma.Web.Plugs.OAuthScopesPlug

  plug(Pleroma.Web.ApiSpec.CastAndValidate)
  plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action == :install)
  plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action == :index)
  action_fallback(Pleroma.Web.AdminAPI.FallbackController)

  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.FrontendOperation

  def index(conn, _params) do
    installed = installed()

    frontends =
      [:frontends, :available]
      |> Config.get([])
      |> Enum.map(fn {name, desc} ->
        desc
        |> Map.put("installed", name in installed)
        |> Map.put("installed_refs", installed_refs(name))
      end)

    render(conn, "index.json", frontends: frontends)
  end

  def install(%{body_params: params} = conn, _params) do
    with :ok <- Pleroma.Frontend.install(params.name, Map.delete(params, :name)) do
      index(conn, %{})
    end
  end

  defp installed do
    frontend_directory = Pleroma.Frontend.dir()

    if File.exists?(frontend_directory) do
      File.ls!(frontend_directory)
    else
      []
    end
  end

  def installed_refs(name) do
    if name in installed() do
      File.ls!(Path.join(Pleroma.Frontend.dir(), name))
    else
      []
    end
  end
end