+ def config_update(conn, %{"configs" => configs}) do
+ with :ok <- configurable_from_database(conn) do
+ {_errors, results} =
+ Enum.map(configs, fn
+ %{"group" => group, "key" => key, "delete" => true} = params ->
+ ConfigDB.delete(%{group: group, key: key, subkeys: params["subkeys"]})
+
+ %{"group" => group, "key" => key, "value" => value} ->
+ ConfigDB.update_or_create(%{group: group, key: key, value: value})
+ end)
+ |> Enum.split_with(fn result -> elem(result, 0) == :error end)
+
+ {deleted, updated} =
+ results
+ |> Enum.map(fn {:ok, config} ->
+ Map.put(config, :db, ConfigDB.get_db_keys(config))
+ end)
+ |> Enum.split_with(fn config ->
+ Ecto.get_meta(config, :state) == :deleted
+ end)
+
+ Config.TransferTask.load_and_update_env(deleted, false)
+
+ need_reboot? =
+ Restarter.Pleroma.need_reboot?() ||
+ Enum.any?(updated, fn config ->
+ group = ConfigDB.from_string(config.group)
+ key = ConfigDB.from_string(config.key)
+ value = ConfigDB.from_binary(config.value)
+ Config.TransferTask.pleroma_need_restart?(group, key, value)
+ end)
+
+ response = %{configs: updated}
+
+ response =
+ if need_reboot? do
+ Restarter.Pleroma.need_reboot()
+ Map.put(response, :need_reboot, need_reboot?)
+ else
+ response
+ end
+
+ conn
+ |> put_view(ConfigView)
+ |> render("index.json", response)
+ end
+ end
+
+ def restart(conn, _params) do
+ with :ok <- configurable_from_database(conn) do
+ Restarter.Pleroma.restart(Config.get(:env), 50)
+
+ json(conn, %{})
+ end
+ end
+
+ defp configurable_from_database(conn) do
+ if Config.get(:configurable_from_database) do
+ :ok
+ else
+ errors(
+ conn,
+ {:error, "To use this endpoint you need to enable configuration from database."}
+ )
+ end