4518bed5a446c08839c08374967640175799a9c7
[akkoma] / lib / pleroma / web / admin_api / controllers / frontend_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.FrontendController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Config
9 alias Pleroma.Web.Plugs.OAuthScopesPlug
10
11 plug(Pleroma.Web.ApiSpec.CastAndValidate)
12 plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action == :install)
13 plug(OAuthScopesPlug, %{scopes: ["read"], admin: true} when action == :index)
14 action_fallback(Pleroma.Web.AdminAPI.FallbackController)
15
16 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.FrontendOperation
17
18 def index(conn, _params) do
19 installed = installed()
20
21 frontends =
22 [:frontends, :available]
23 |> Config.get([])
24 |> Enum.map(fn {name, desc} ->
25 Map.put(desc, "installed", name in installed)
26 end)
27
28 render(conn, "index.json", frontends: frontends)
29 end
30
31 def install(%{body_params: params} = conn, _params) do
32 Pleroma.Frontend.install(params.name, Map.delete(params, :name))
33
34 index(conn, %{})
35 end
36
37 defp installed do
38 File.ls!(Pleroma.Frontend.dir())
39 end
40 end