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.ConfigController do
6 use Pleroma.Web, :controller
10 alias Pleroma.Web.Plugs.OAuthScopesPlug
12 plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
13 plug(OAuthScopesPlug, %{scopes: ["admin:write"]} when action == :update)
17 %{scopes: ["admin:read"]}
18 when action in [:show, :descriptions]
21 action_fallback(Pleroma.Web.AdminAPI.FallbackController)
23 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ConfigOperation
25 defp translate_descriptions(descriptions, path \\ []) do
26 Enum.map(descriptions, fn desc -> translate_item(desc, path) end)
29 defp translate_string(str, path, type) do
32 "config_descriptions",
33 Pleroma.Docs.Translator.Compiler.msgctxt_for(path, type),
38 defp maybe_put_translated(item, key, path) do
45 path ++ [Pleroma.Docs.Translator.Compiler.key_for(item)],
54 defp translate_item(item, path) do
56 |> maybe_put_translated(:label, path)
57 |> maybe_put_translated(:description, path)
58 |> translate_children(path)
61 defp translate_children(%{children: children} = item, path) when is_list(children) do
65 translate_descriptions(children, path ++ [Pleroma.Docs.Translator.Compiler.key_for(item)])
69 defp translate_children(item, _path) do
73 def descriptions(conn, _params) do
74 descriptions = Enum.filter(Pleroma.Docs.JSON.compiled_descriptions(), &whitelisted_config?/1)
76 json(conn, translate_descriptions(descriptions))
79 def show(%{private: %{open_api_spex: %{params: %{only_db: true}}}} = conn, _) do
80 with :ok <- configurable_from_database() do
81 configs = Pleroma.Repo.all(ConfigDB)
83 render(conn, "index.json", %{
85 need_reboot: Restarter.Pleroma.need_reboot?()
90 def show(conn, _params) do
91 with :ok <- configurable_from_database() do
92 configs = ConfigDB.get_all_as_keyword()
95 Config.Holder.default_config()
96 |> ConfigDB.merge(configs)
97 |> Enum.map(fn {group, values} ->
98 Enum.map(values, fn {key, value} ->
100 if configs[group][key] do
101 ConfigDB.get_db_keys(configs[group][key], key)
104 db_value = configs[group][key]
107 if not is_nil(db_value) and Keyword.keyword?(db_value) and
108 ConfigDB.sub_key_full_update?(group, key, Keyword.keys(db_value)) do
109 ConfigDB.merge_group(group, key, value, db_value)
119 |> Pleroma.Maps.put_if_present(:db, db)
124 render(conn, "index.json", %{
126 need_reboot: Restarter.Pleroma.need_reboot?()
131 def update(%{private: %{open_api_spex: %{body_params: %{configs: configs}}}} = conn, _) do
132 with :ok <- configurable_from_database() do
135 |> Enum.filter(&whitelisted_config?/1)
137 %{group: group, key: key, delete: true} = params ->
138 ConfigDB.delete(%{group: group, key: key, subkeys: params[:subkeys]})
140 %{group: group, key: key, value: value} ->
141 ConfigDB.update_or_create(%{group: group, key: key, value: value})
143 |> Enum.reject(fn {result, _} -> result == :error end)
147 |> Enum.map(fn {:ok, %{key: key, value: value} = config} ->
148 Map.put(config, :db, ConfigDB.get_db_keys(value, key))
150 |> Enum.split_with(&(Ecto.get_meta(&1, :state) == :deleted))
152 Config.TransferTask.load_and_update_env(deleted, false)
154 if not Restarter.Pleroma.need_reboot?() do
155 changed_reboot_settings? =
157 |> Enum.any?(&Config.TransferTask.pleroma_need_restart?(&1.group, &1.key, &1.value))
159 if changed_reboot_settings?, do: Restarter.Pleroma.need_reboot()
162 render(conn, "index.json", %{
164 need_reboot: Restarter.Pleroma.need_reboot?()
169 defp configurable_from_database do
170 if Config.get(:configurable_from_database) do
173 {:error, "You must enable configurable_from_database in your config file."}
177 defp whitelisted_config?(group, key) do
178 if whitelisted_configs = Config.get(:database_config_whitelist) do
179 Enum.any?(whitelisted_configs, fn
180 {whitelisted_group} ->
181 group == inspect(whitelisted_group)
183 {whitelisted_group, whitelisted_key} ->
184 group == inspect(whitelisted_group) && key == inspect(whitelisted_key)
191 defp whitelisted_config?(%{group: group, key: key}) do
192 whitelisted_config?(group, key)
195 defp whitelisted_config?(%{group: group} = config) do
196 whitelisted_config?(group, config[:key])