2020-10-27 15:20:04 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-10-27 15:20:04 +00:00
|
|
|
# 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)
|
2021-02-17 18:37:23 +00:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action == :install)
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action == :index)
|
2020-10-27 15:20:04 +00:00
|
|
|
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
|
|
|
|
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.FrontendOperation
|
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
installed = installed()
|
|
|
|
|
2023-03-30 08:16:40 +00:00
|
|
|
# FIrst get frontends from config,
|
|
|
|
# then add frontends that are installed but not in the config
|
2020-10-27 15:20:04 +00:00
|
|
|
frontends =
|
2023-03-30 08:16:40 +00:00
|
|
|
Config.get([:frontends, :available], [])
|
2020-10-27 15:20:04 +00:00
|
|
|
|> Enum.map(fn {name, desc} ->
|
2023-03-29 20:18:44 +00:00
|
|
|
desc
|
|
|
|
|> Map.put("installed", name in installed)
|
|
|
|
|> Map.put("installed_refs", installed_refs(name))
|
2020-10-27 15:20:04 +00:00
|
|
|
end)
|
|
|
|
|
2023-03-30 08:16:40 +00:00
|
|
|
frontends =
|
|
|
|
frontends ++
|
|
|
|
(installed
|
|
|
|
|> Enum.filter(fn n -> not Enum.any?(frontends, fn f -> f["name"] == n end) end)
|
|
|
|
|> Enum.map(fn name ->
|
|
|
|
%{"name" => name, "installed" => true, "installed_refs" => installed_refs(name)}
|
|
|
|
end))
|
|
|
|
|
2020-10-27 15:20:04 +00:00
|
|
|
render(conn, "index.json", frontends: frontends)
|
|
|
|
end
|
|
|
|
|
|
|
|
def install(%{body_params: params} = conn, _params) do
|
2020-11-17 15:43:07 +00:00
|
|
|
with :ok <- Pleroma.Frontend.install(params.name, Map.delete(params, :name)) do
|
|
|
|
index(conn, %{})
|
|
|
|
end
|
2020-10-27 15:20:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp installed do
|
2021-08-14 18:42:12 +00:00
|
|
|
frontend_directory = Pleroma.Frontend.dir()
|
|
|
|
|
|
|
|
if File.exists?(frontend_directory) do
|
|
|
|
File.ls!(frontend_directory)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2020-10-27 15:20:04 +00:00
|
|
|
end
|
2023-03-29 20:18:44 +00:00
|
|
|
|
|
|
|
def installed_refs(name) do
|
|
|
|
if name in installed() do
|
|
|
|
File.ls!(Path.join(Pleroma.Frontend.dir(), name))
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
2020-10-27 15:20:04 +00:00
|
|
|
end
|