From 2fb44f012df2fc41d8a8d0afb15dc513f968512a Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 18 Feb 2021 14:21:49 -0600 Subject: [PATCH] Add AdminAPI endpoint for retrieving a list of tabs to be rendered in AdminFE for ConfigDB Settings --- config/description.exs | 24 ++++++++++++++++++++++ lib/pleroma/docs/json.ex | 8 +++++++- .../web/admin_api/controllers/config_controller.ex | 11 ++++++++-- .../api_spec/operations/admin/config_operation.ex | 24 ++++++++++++++++++++++ lib/pleroma/web/router.ex | 1 + 5 files changed, 65 insertions(+), 3 deletions(-) diff --git a/config/description.exs b/config/description.exs index d9b15e684..7d07f3289 100644 --- a/config/description.exs +++ b/config/description.exs @@ -69,6 +69,30 @@ frontend_options = [ } ] +config :pleroma, :config_tablist, [ + %{label: "ActivityPub", path: "activity-pub"}, + %{label: "Authentication", path: "authentication"}, + %{label: "Captcha", path: "captcha"}, + %{label: "BBS / SSH access", path: "esshd"}, + %{label: "Emoji", path: "emoji"}, + %{label: "Frontend", path: "frontend"}, + %{label: "Gopher", path: "gopher"}, + %{label: "HTTP", path: "http"}, + %{label: "Instance", path: "instance"}, + %{label: "Job queue", path: "job-queue"}, + %{label: "Link Formatter", path: "link-formatter"}, + %{label: "Logger", path: "logger"}, + %{label: "Mailer", path: "mailer"}, + %{label: "Media Proxy", path: "media-proxy"}, + %{label: "Metadata", path: "metadata"}, + %{label: "MRF", path: "mrf"}, + %{label: "Rate limiters", path: "rate-limiters"}, + %{label: "Relays", path: "relays"}, + %{label: "Web push encryption", path: "web-push"}, + %{label: "Upload", path: "upload"}, + %{label: "Other", path: "other"} +] + config :pleroma, :config_description, [ %{ group: :pleroma, diff --git a/lib/pleroma/docs/json.ex b/lib/pleroma/docs/json.ex index f22432ea4..e34d7ada8 100644 --- a/lib/pleroma/docs/json.ex +++ b/lib/pleroma/docs/json.ex @@ -7,6 +7,7 @@ defmodule Pleroma.Docs.JSON do @external_resource "config/description.exs" @raw_config Pleroma.Config.Loader.read("config/description.exs") @raw_descriptions @raw_config[:pleroma][:config_description] + @raw_tablist @raw_config[:pleroma][:config_tablist] @term __MODULE__.Compiled @spec compile :: :ok @@ -15,7 +16,12 @@ defmodule Pleroma.Docs.JSON do Pleroma.Web.ActivityPub.MRF.config_descriptions() |> Enum.reduce(@raw_descriptions, fn description, acc -> [description | acc] end) - :persistent_term.put(@term, Pleroma.Docs.Generator.convert_to_strings(descriptions)) + data = %{ + tabs: @raw_tablist, + descriptions: Pleroma.Docs.Generator.convert_to_strings(descriptions) + } + + :persistent_term.put(@term, data) end @spec compiled_descriptions :: Map.t() diff --git a/lib/pleroma/web/admin_api/controllers/config_controller.ex b/lib/pleroma/web/admin_api/controllers/config_controller.ex index a718d7b8d..5a4a771cd 100644 --- a/lib/pleroma/web/admin_api/controllers/config_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/config_controller.ex @@ -15,7 +15,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigController do plug( OAuthScopesPlug, %{scopes: ["admin:read"]} - when action in [:show, :descriptions] + when action in [:show, :descriptions, :tabs] ) action_fallback(Pleroma.Web.AdminAPI.FallbackController) @@ -23,11 +23,18 @@ defmodule Pleroma.Web.AdminAPI.ConfigController do defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ConfigOperation def descriptions(conn, _params) do - descriptions = Enum.filter(Pleroma.Docs.JSON.compiled_descriptions(), &whitelisted_config?/1) + %{tabs: _, descriptions: descriptions} = Pleroma.Docs.JSON.compiled_descriptions() + descriptions = Enum.filter(descriptions, &whitelisted_config?/1) json(conn, descriptions) end + def tabs(conn, _params) do + %{tabs: tabs, descriptions: _} = Pleroma.Docs.JSON.compiled_descriptions() + + json(conn, tabs) + end + def show(conn, %{only_db: true}) do with :ok <- configurable_from_database() do configs = Pleroma.Repo.all(ConfigDB) diff --git a/lib/pleroma/web/api_spec/operations/admin/config_operation.ex b/lib/pleroma/web/api_spec/operations/admin/config_operation.ex index 30c3433b7..fc41dc8e2 100644 --- a/lib/pleroma/web/api_spec/operations/admin/config_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/config_operation.ex @@ -107,6 +107,30 @@ defmodule Pleroma.Web.ApiSpec.Admin.ConfigOperation do } end + def tabs_operation do + %Operation{ + tags: ["Instance configuration"], + summary: "Retrieve config tabs for AdminFE", + operationId: "AdminAPI.ConfigController.tabs", + security: [%{"oAuth" => ["admin:read"]}], + parameters: admin_api_params(), + responses: %{ + 200 => + Operation.response("Config Tab List", "application/json", %Schema{ + type: :array, + items: %Schema{ + type: :object, + properties: %{ + label: %Schema{type: :string}, + path: %Schema{type: :string}, + } + } + }), + 400 => Operation.response("Bad Request", "application/json", ApiError) + } + } + end + defp any do %Schema{ oneOf: [ diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index d71011033..5b118814f 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -232,6 +232,7 @@ defmodule Pleroma.Web.Router do get("/config", ConfigController, :show) post("/config", ConfigController, :update) get("/config/descriptions", ConfigController, :descriptions) + get("/config/tabs", ConfigController, :tabs) get("/need_reboot", AdminAPIController, :need_reboot) get("/restart", AdminAPIController, :restart)