logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

frontend_controller.ex (1908B)


  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. defmodule Pleroma.Web.AdminAPI.FrontendController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.Config
  7. alias Pleroma.Web.Plugs.OAuthScopesPlug
  8. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  9. plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action == :install)
  10. plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action == :index)
  11. action_fallback(Pleroma.Web.AdminAPI.FallbackController)
  12. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.FrontendOperation
  13. def index(conn, _params) do
  14. installed = installed()
  15. # FIrst get frontends from config,
  16. # then add frontends that are installed but not in the config
  17. frontends =
  18. Config.get([:frontends, :available], [])
  19. |> Enum.map(fn {name, desc} ->
  20. desc
  21. |> Map.put("installed", name in installed)
  22. |> Map.put("installed_refs", installed_refs(name))
  23. end)
  24. frontends =
  25. frontends ++
  26. (installed
  27. |> Enum.filter(fn n -> not Enum.any?(frontends, fn f -> f["name"] == n end) end)
  28. |> Enum.map(fn name ->
  29. %{"name" => name, "installed" => true, "installed_refs" => installed_refs(name)}
  30. end))
  31. render(conn, "index.json", frontends: frontends)
  32. end
  33. def install(%{body_params: params} = conn, _params) do
  34. with :ok <- Pleroma.Frontend.install(params.name, Map.delete(params, :name)) do
  35. index(conn, %{})
  36. end
  37. end
  38. defp installed do
  39. frontend_directory = Pleroma.Frontend.dir()
  40. if File.exists?(frontend_directory) do
  41. File.ls!(frontend_directory)
  42. else
  43. []
  44. end
  45. end
  46. def installed_refs(name) do
  47. if name in installed() do
  48. File.ls!(Path.join(Pleroma.Frontend.dir(), name))
  49. else
  50. []
  51. end
  52. end
  53. end