1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Plugs.OAuthScopesPlug
12 plug(Pleroma.Web.ApiSpec.CastAndValidate)
13 plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action == :update)
17 %{scopes: ["read"], admin: true}
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 def descriptions(conn, _params) do
26 descriptions = Enum.filter(Pleroma.Docs.JSON.compiled_descriptions(), &whitelisted_config?/1)
28 json(conn, descriptions)
31 def show(conn, %{only_db: true}) do
32 with :ok <- configurable_from_database() do
33 configs = Pleroma.Repo.all(ConfigDB)
35 render(conn, "index.json", %{
37 need_reboot: Restarter.Pleroma.need_reboot?()
42 def show(conn, _params) do
43 with :ok <- configurable_from_database() do
44 configs = ConfigDB.get_all_as_keyword()
47 Config.Holder.default_config()
48 |> ConfigDB.merge(configs)
49 |> Enum.map(fn {group, values} ->
50 Enum.map(values, fn {key, value} ->
52 if configs[group][key] do
53 ConfigDB.get_db_keys(configs[group][key], key)
56 db_value = configs[group][key]
59 if not is_nil(db_value) and Keyword.keyword?(db_value) and
60 ConfigDB.sub_key_full_update?(group, key, Keyword.keys(db_value)) do
61 ConfigDB.merge_group(group, key, value, db_value)
71 |> Pleroma.Maps.put_if_present(:db, db)
76 render(conn, "index.json", %{
78 need_reboot: Restarter.Pleroma.need_reboot?()
83 def update(%{body_params: %{configs: configs}} = conn, _) do
84 with :ok <- configurable_from_database() do
87 |> Enum.filter(&whitelisted_config?/1)
89 %{group: group, key: key, delete: true} = params ->
90 ConfigDB.delete(%{group: group, key: key, subkeys: params[:subkeys]})
92 %{group: group, key: key, value: value} ->
93 ConfigDB.update_or_create(%{group: group, key: key, value: value})
95 |> Enum.reject(fn {result, _} -> result == :error end)
99 |> Enum.map(fn {:ok, %{key: key, value: value} = config} ->
100 Map.put(config, :db, ConfigDB.get_db_keys(value, key))
102 |> Enum.split_with(&(Ecto.get_meta(&1, :state) == :deleted))
104 Config.TransferTask.load_and_update_env(deleted, false)
106 if not Restarter.Pleroma.need_reboot?() do
107 changed_reboot_settings? =
109 |> Enum.any?(&Config.TransferTask.pleroma_need_restart?(&1.group, &1.key, &1.value))
111 if changed_reboot_settings?, do: Restarter.Pleroma.need_reboot()
114 render(conn, "index.json", %{
116 need_reboot: Restarter.Pleroma.need_reboot?()
121 defp configurable_from_database do
122 if Config.get(:configurable_from_database) do
125 {:error, "To use this endpoint you need to enable configuration from database."}
129 defp whitelisted_config?(group, key) do
130 if whitelisted_configs = Config.get(:database_config_whitelist) do
131 Enum.any?(whitelisted_configs, fn
132 {whitelisted_group} ->
133 group == inspect(whitelisted_group)
135 {whitelisted_group, whitelisted_key} ->
136 group == inspect(whitelisted_group) && key == inspect(whitelisted_key)
143 defp whitelisted_config?(%{group: group, key: key}) do
144 whitelisted_config?(group, key)
147 defp whitelisted_config?(%{group: group} = config) do
148 whitelisted_config?(group, config[:key])