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 @descriptions Pleroma.Docs.JSON.compile()
14 plug(Pleroma.Web.ApiSpec.CastAndValidate)
15 plug(OAuthScopesPlug, %{scopes: ["write"], admin: true} when action == :update)
19 %{scopes: ["read"], admin: true}
20 when action in [:show, :descriptions]
23 action_fallback(Pleroma.Web.AdminAPI.FallbackController)
25 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.ConfigOperation
27 def descriptions(conn, _params) do
28 descriptions = Enum.filter(@descriptions, &whitelisted_config?/1)
30 json(conn, descriptions)
33 def show(conn, %{only_db: true}) do
34 with :ok <- configurable_from_database() do
35 configs = Pleroma.Repo.all(ConfigDB)
37 render(conn, "index.json", %{
39 need_reboot: Restarter.Pleroma.need_reboot?()
44 def show(conn, _params) do
45 with :ok <- configurable_from_database() do
46 configs = ConfigDB.get_all_as_keyword()
49 Config.Holder.default_config()
50 |> ConfigDB.merge(configs)
51 |> Enum.map(fn {group, values} ->
52 Enum.map(values, fn {key, value} ->
54 if configs[group][key] do
55 ConfigDB.get_db_keys(configs[group][key], key)
58 db_value = configs[group][key]
61 if not is_nil(db_value) and Keyword.keyword?(db_value) and
62 ConfigDB.sub_key_full_update?(group, key, Keyword.keys(db_value)) do
63 ConfigDB.merge_group(group, key, value, db_value)
73 |> Pleroma.Maps.put_if_present(:db, db)
78 render(conn, "index.json", %{
80 need_reboot: Restarter.Pleroma.need_reboot?()
85 def update(%{body_params: %{configs: configs}} = conn, _) do
86 with :ok <- configurable_from_database() do
89 |> Enum.filter(&whitelisted_config?/1)
91 %{group: group, key: key, delete: true} = params ->
92 ConfigDB.delete(%{group: group, key: key, subkeys: params[:subkeys]})
94 %{group: group, key: key, value: value} ->
95 ConfigDB.update_or_create(%{group: group, key: key, value: value})
97 |> Enum.reject(fn {result, _} -> result == :error end)
101 |> Enum.map(fn {:ok, %{key: key, value: value} = config} ->
102 Map.put(config, :db, ConfigDB.get_db_keys(value, key))
104 |> Enum.split_with(&(Ecto.get_meta(&1, :state) == :deleted))
106 Config.TransferTask.load_and_update_env(deleted, false)
108 if not Restarter.Pleroma.need_reboot?() do
109 changed_reboot_settings? =
111 |> Enum.any?(&Config.TransferTask.pleroma_need_restart?(&1.group, &1.key, &1.value))
113 if changed_reboot_settings?, do: Restarter.Pleroma.need_reboot()
116 render(conn, "index.json", %{
118 need_reboot: Restarter.Pleroma.need_reboot?()
123 defp configurable_from_database do
124 if Config.get(:configurable_from_database) do
127 {:error, "To use this endpoint you need to enable configuration from database."}
131 defp whitelisted_config?(group, key) do
132 if whitelisted_configs = Config.get(:database_config_whitelist) do
133 Enum.any?(whitelisted_configs, fn
134 {whitelisted_group} ->
135 group == inspect(whitelisted_group)
137 {whitelisted_group, whitelisted_key} ->
138 group == inspect(whitelisted_group) && key == inspect(whitelisted_key)
145 defp whitelisted_config?(%{group: group, key: key}) do
146 whitelisted_config?(group, key)
149 defp whitelisted_config?(%{group: group} = config) do
150 whitelisted_config?(group, config[:key])