defmodule Pleroma.Web.AdminAPI.AdminAPIController do
use Pleroma.Web, :controller
+
+ import Pleroma.Web.ControllerHelper, only: [json_response: 3]
+
alias Pleroma.Activity
alias Pleroma.ModerationLog
alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Web.MastodonAPI.StatusView
alias Pleroma.Web.Router
- import Pleroma.Web.ControllerHelper, only: [json_response: 3]
-
require Logger
+ @descriptions_json Pleroma.Docs.JSON.compile()
+ @users_page_size 50
+
plug(
OAuthScopesPlug,
%{scopes: ["read:accounts"], admin: true}
plug(
OAuthScopesPlug,
%{scopes: ["read"], admin: true}
- when action in [:config_show, :migrate_to_db, :migrate_from_db, :list_log]
+ when action in [:config_show, :migrate_from_db, :list_log]
)
plug(
when action in [:relay_follow, :relay_unfollow, :config_update]
)
- @users_page_size 50
-
action_fallback(:errors)
def user_delete(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
|> render("index.json", %{log: log})
end
- def migrate_to_db(conn, _params) do
- Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
- json(conn, %{})
+ def config_descriptions(conn, _params) do
+ conn
+ |> Plug.Conn.put_resp_content_type("application/json")
+ |> Plug.Conn.send_resp(200, @descriptions_json)
end
def migrate_from_db(conn, _params) do
- Mix.Tasks.Pleroma.Config.run(["migrate_from_db", Pleroma.Config.get(:env), "true"])
- json(conn, %{})
+ with :ok <- configurable_from_database(conn) do
+ Mix.Tasks.Pleroma.Config.run([
+ "migrate_from_db",
+ "--env",
+ to_string(Pleroma.Config.get(:env)),
+ "-d"
+ ])
+
+ json(conn, %{})
+ end
end
def config_show(conn, _params) do
- configs = Pleroma.Repo.all(Config)
-
- conn
- |> put_view(ConfigView)
- |> render("index.json", %{configs: configs})
+ with :ok <- configurable_from_database(conn) do
+ configs = Pleroma.Repo.all(Config)
+
+ if configs == [] do
+ errors(
+ conn,
+ {:error, "To use configuration from database migrate your settings to database."}
+ )
+ else
+ conn
+ |> put_view(ConfigView)
+ |> render("index.json", %{configs: configs})
+ end
+ end
end
def config_update(conn, %{"configs" => configs}) do
- updated =
- if Pleroma.Config.get([:instance, :dynamic_configuration]) do
- updated =
- Enum.map(configs, fn
- %{"group" => group, "key" => key, "delete" => "true"} = params ->
- {:ok, config} = Config.delete(%{group: group, key: key, subkeys: params["subkeys"]})
+ with :ok <- configurable_from_database(conn) do
+ updated =
+ Enum.map(configs, fn
+ %{"group" => group, "key" => key, "delete" => true} = params ->
+ with {:ok, config} <-
+ Config.delete(%{group: group, key: key, subkeys: params["subkeys"]}) do
config
+ end
- %{"group" => group, "key" => key, "value" => value} ->
- {:ok, config} = Config.update_or_create(%{group: group, key: key, value: value})
+ %{"group" => group, "key" => key, "value" => value} ->
+ with {:ok, config} <-
+ Config.update_or_create(%{group: group, key: key, value: value}) do
config
- end)
- |> Enum.reject(&is_nil(&1))
+ end
+ end)
+ |> Enum.reject(&is_nil(&1))
- Pleroma.Config.TransferTask.load_and_update_env()
- Mix.Tasks.Pleroma.Config.run(["migrate_from_db", Pleroma.Config.get(:env), "false"])
- updated
- else
- []
- end
+ Pleroma.Config.TransferTask.load_and_update_env()
- conn
- |> put_view(ConfigView)
- |> render("index.json", %{configs: updated})
+ Mix.Tasks.Pleroma.Config.run([
+ "migrate_from_db",
+ "--env",
+ to_string(Pleroma.Config.get(:env))
+ ])
+
+ conn
+ |> put_view(ConfigView)
+ |> render("index.json", %{configs: updated})
+ end
+ end
+
+ defp configurable_from_database(conn) do
+ if Pleroma.Config.get(:configurable_from_database) do
+ :ok
+ else
+ errors(
+ conn,
+ {:error, "To use this endpoint you need to enable configuration from database."}
+ )
+ end
end
def reload_emoji(conn, _params) do