1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.FrontendController do
6 use Pleroma.Web, :controller
9 alias Pleroma.Web.Plugs.OAuthScopesPlug
11 plug(Pleroma.Web.ApiSpec.CastAndValidate)
12 plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action == :install)
13 plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action == :index)
14 action_fallback(Pleroma.Web.AdminAPI.FallbackController)
16 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.FrontendOperation
18 def index(conn, _params) do
19 installed = installed()
21 # FIrst get frontends from config,
22 # then add frontends that are installed but not in the config
24 Config.get([:frontends, :available], [])
25 |> Enum.map(fn {name, desc} ->
27 |> Map.put("installed", name in installed)
28 |> Map.put("installed_refs", installed_refs(name))
34 |> Enum.filter(fn n -> not Enum.any?(frontends, fn f -> f["name"] == n end) end)
35 |> Enum.map(fn name ->
36 %{"name" => name, "installed" => true, "installed_refs" => installed_refs(name)}
39 render(conn, "index.json", frontends: frontends)
42 def install(%{body_params: params} = conn, _params) do
43 with :ok <- Pleroma.Frontend.install(params.name, Map.delete(params, :name)) do
49 frontend_directory = Pleroma.Frontend.dir()
51 if File.exists?(frontend_directory) do
52 File.ls!(frontend_directory)
58 def installed_refs(name) do
59 if name in installed() do
60 File.ls!(Path.join(Pleroma.Frontend.dir(), name))