2019-06-14 15:45:05 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.AdminAPI.Config do
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2019-07-10 09:25:58 +00:00
|
|
|
import Pleroma.Web.Gettext
|
2019-06-14 15:45:05 +00:00
|
|
|
alias __MODULE__
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{}
|
|
|
|
|
|
|
|
schema "config" do
|
|
|
|
field(:key, :string)
|
2019-06-23 05:16:16 +00:00
|
|
|
field(:group, :string)
|
2019-06-14 15:45:05 +00:00
|
|
|
field(:value, :binary)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2019-06-23 05:16:16 +00:00
|
|
|
@spec get_by_params(map()) :: Config.t() | nil
|
|
|
|
def get_by_params(params), do: Repo.get_by(Config, params)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
|
|
|
@spec changeset(Config.t(), map()) :: Changeset.t()
|
|
|
|
def changeset(config, params \\ %{}) do
|
|
|
|
config
|
2019-06-23 05:16:16 +00:00
|
|
|
|> cast(params, [:key, :group, :value])
|
|
|
|
|> validate_required([:key, :group, :value])
|
|
|
|
|> unique_constraint(:key, name: :config_group_key_index)
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
|
2019-06-23 05:16:16 +00:00
|
|
|
def create(params) do
|
2019-06-14 15:45:05 +00:00
|
|
|
%Config{}
|
2019-06-23 05:16:16 +00:00
|
|
|
|> changeset(Map.put(params, :value, transform(params[:value])))
|
2019-06-14 15:45:05 +00:00
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec update(Config.t(), map()) :: {:ok, Config} | {:error, Changeset.t()}
|
|
|
|
def update(%Config{} = config, %{value: value}) do
|
|
|
|
config
|
|
|
|
|> change(value: transform(value))
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
|
2019-06-23 05:16:16 +00:00
|
|
|
def update_or_create(params) do
|
|
|
|
with %Config{} = config <- Config.get_by_params(Map.take(params, [:group, :key])) do
|
2019-06-14 15:45:05 +00:00
|
|
|
Config.update(config, params)
|
|
|
|
else
|
|
|
|
nil -> Config.create(params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-23 05:16:16 +00:00
|
|
|
@spec delete(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
|
|
|
|
def delete(params) do
|
2019-08-03 18:16:09 +00:00
|
|
|
with %Config{} = config <- Config.get_by_params(Map.delete(params, :subkeys)) do
|
|
|
|
if params[:subkeys] do
|
|
|
|
updated_value =
|
|
|
|
Keyword.drop(
|
|
|
|
:erlang.binary_to_term(config.value),
|
|
|
|
Enum.map(params[:subkeys], &do_transform_string(&1))
|
|
|
|
)
|
|
|
|
|
|
|
|
Config.update(config, %{value: updated_value})
|
|
|
|
else
|
|
|
|
Repo.delete(config)
|
|
|
|
{:ok, nil}
|
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
else
|
2019-07-10 09:25:58 +00:00
|
|
|
nil ->
|
|
|
|
err =
|
|
|
|
dgettext("errors", "Config with params %{params} not found", params: inspect(params))
|
|
|
|
|
|
|
|
{:error, err}
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec from_binary(binary()) :: term()
|
2019-07-11 13:02:13 +00:00
|
|
|
def from_binary(binary), do: :erlang.binary_to_term(binary)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
@spec from_binary_with_convert(binary()) :: any()
|
|
|
|
def from_binary_with_convert(binary) do
|
2019-06-14 15:45:05 +00:00
|
|
|
from_binary(binary)
|
|
|
|
|> do_convert()
|
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_list(entity) do
|
|
|
|
for v <- entity, into: [], do: do_convert(v)
|
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-08-30 11:04:21 +00:00
|
|
|
defp do_convert(%Regex{} = entity), do: inspect(entity)
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_map(entity) do
|
|
|
|
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
|
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
|
2019-07-23 19:17:00 +00:00
|
|
|
defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_tuple(entity),
|
|
|
|
do: %{"tuple" => do_convert(Tuple.to_list(entity))}
|
2019-06-22 14:30:53 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_boolean(entity) or is_number(entity) or is_nil(entity),
|
|
|
|
do: entity
|
2019-06-22 06:01:30 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_atom(entity) do
|
|
|
|
string = to_string(entity)
|
2019-06-22 06:01:30 +00:00
|
|
|
|
|
|
|
if String.starts_with?(string, "Elixir."),
|
2019-07-11 13:02:13 +00:00
|
|
|
do: do_convert(string),
|
|
|
|
else: ":" <> string
|
2019-06-22 06:01:30 +00:00
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert("Elixir." <> module_name), do: module_name
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_binary(entity), do: entity
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
@spec transform(any()) :: binary()
|
|
|
|
def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity) do
|
|
|
|
:erlang.term_to_binary(do_transform(entity))
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def transform(entity), do: :erlang.term_to_binary(entity)
|
|
|
|
|
2019-08-30 11:04:21 +00:00
|
|
|
defp do_transform(%Regex{} = entity), do: entity
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
|
2019-07-23 19:17:00 +00:00
|
|
|
{dispatch_settings, []} = do_eval(entity)
|
2019-07-11 13:02:13 +00:00
|
|
|
{:dispatch, [dispatch_settings]}
|
2019-06-22 14:30:53 +00:00
|
|
|
end
|
|
|
|
|
2019-07-23 19:17:00 +00:00
|
|
|
defp do_transform(%{"tuple" => [":partial_chain", entity]}) do
|
|
|
|
{partial_chain, []} = do_eval(entity)
|
|
|
|
{:partial_chain, partial_chain}
|
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(%{"tuple" => entity}) do
|
|
|
|
Enum.reduce(entity, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
|
2019-06-22 14:30:53 +00:00
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(entity) when is_map(entity) do
|
|
|
|
for {k, v} <- entity, into: %{}, do: {do_transform(k), do_transform(v)}
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(entity) when is_list(entity) do
|
|
|
|
for v <- entity, into: [], do: do_transform(v)
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(entity) when is_binary(entity) do
|
|
|
|
String.trim(entity)
|
2019-06-22 14:30:53 +00:00
|
|
|
|> do_transform_string()
|
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(entity), do: entity
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform_string("~r/" <> pattern) do
|
2019-08-30 11:04:21 +00:00
|
|
|
modificator = String.split(pattern, "/") |> List.last()
|
|
|
|
pattern = String.trim_trailing(pattern, "/" <> modificator)
|
|
|
|
|
|
|
|
case modificator do
|
|
|
|
"" -> ~r/#{pattern}/
|
|
|
|
"i" -> ~r/#{pattern}/i
|
|
|
|
"u" -> ~r/#{pattern}/u
|
|
|
|
"s" -> ~r/#{pattern}/s
|
|
|
|
end
|
2019-07-11 13:02:13 +00:00
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform_string(":" <> atom), do: String.to_atom(atom)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform_string(value) do
|
|
|
|
if String.starts_with?(value, "Pleroma") or String.starts_with?(value, "Phoenix"),
|
|
|
|
do: String.to_existing_atom("Elixir." <> value),
|
|
|
|
else: value
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
2019-07-23 19:17:00 +00:00
|
|
|
|
|
|
|
defp do_eval(entity) do
|
|
|
|
cleaned_string = String.replace(entity, ~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
|
|
|
|
Code.eval_string(cleaned_string, [], requires: [], macros: [])
|
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|