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
|
|
|
|
with %Config{} = config <- Config.get_by_params(params) do
|
2019-06-14 15:45:05 +00:00
|
|
|
Repo.delete(config)
|
|
|
|
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()
|
|
|
|
def from_binary(value), do: :erlang.binary_to_term(value)
|
|
|
|
|
|
|
|
@spec from_binary_to_map(binary()) :: any()
|
|
|
|
def from_binary_to_map(binary) do
|
|
|
|
from_binary(binary)
|
|
|
|
|> do_convert()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_convert([{k, v}] = value) when is_list(value) and length(value) == 1,
|
|
|
|
do: %{k => do_convert(v)}
|
|
|
|
|
|
|
|
defp do_convert(values) when is_list(values), do: for(val <- values, do: do_convert(val))
|
|
|
|
|
|
|
|
defp do_convert({k, v} = value) when is_tuple(value),
|
|
|
|
do: %{k => do_convert(v)}
|
|
|
|
|
2019-06-22 14:30:53 +00:00
|
|
|
defp do_convert(value) when is_tuple(value), do: %{"tuple" => do_convert(Tuple.to_list(value))}
|
|
|
|
|
2019-06-22 06:01:30 +00:00
|
|
|
defp do_convert(value) when is_binary(value) or is_map(value) or is_number(value), do: value
|
|
|
|
|
|
|
|
defp do_convert(value) when is_atom(value) do
|
|
|
|
string = to_string(value)
|
|
|
|
|
|
|
|
if String.starts_with?(string, "Elixir."),
|
|
|
|
do: String.trim_leading(string, "Elixir."),
|
|
|
|
else: value
|
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
|
|
|
|
@spec transform(any()) :: binary()
|
2019-06-23 05:16:16 +00:00
|
|
|
def transform(%{"tuple" => _} = entity), do: :erlang.term_to_binary(do_transform(entity))
|
|
|
|
|
2019-06-14 15:45:05 +00:00
|
|
|
def transform(entity) when is_map(entity) do
|
|
|
|
tuples =
|
|
|
|
for {k, v} <- entity,
|
|
|
|
into: [],
|
|
|
|
do: {if(is_atom(k), do: k, else: String.to_atom(k)), do_transform(v)}
|
|
|
|
|
|
|
|
Enum.reject(tuples, fn {_k, v} -> is_nil(v) end)
|
|
|
|
|> Enum.sort()
|
|
|
|
|> :erlang.term_to_binary()
|
|
|
|
end
|
|
|
|
|
|
|
|
def transform(entity) when is_list(entity) do
|
|
|
|
list = Enum.map(entity, &do_transform(&1))
|
|
|
|
:erlang.term_to_binary(list)
|
|
|
|
end
|
|
|
|
|
|
|
|
def transform(entity), do: :erlang.term_to_binary(entity)
|
|
|
|
|
|
|
|
defp do_transform(%Regex{} = value) when is_map(value), do: value
|
|
|
|
|
2019-06-22 14:30:53 +00:00
|
|
|
defp do_transform(%{"tuple" => [k, values] = entity}) when length(entity) == 2 do
|
|
|
|
{do_transform(k), do_transform(values)}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_transform(%{"tuple" => values}) do
|
|
|
|
Enum.reduce(values, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
|
|
|
|
end
|
|
|
|
|
2019-06-14 15:45:05 +00:00
|
|
|
defp do_transform(value) when is_map(value) do
|
2019-06-22 14:30:53 +00:00
|
|
|
values = for {key, val} <- value, into: [], do: {String.to_atom(key), do_transform(val)}
|
2019-06-14 15:45:05 +00:00
|
|
|
|
|
|
|
Enum.sort(values)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_transform(value) when is_list(value) do
|
|
|
|
Enum.map(value, &do_transform(&1))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_transform(entity) when is_list(entity) and length(entity) == 1, do: hd(entity)
|
|
|
|
|
|
|
|
defp do_transform(value) when is_binary(value) do
|
2019-06-22 14:30:53 +00:00
|
|
|
String.trim(value)
|
|
|
|
|> do_transform_string()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_transform(value), do: value
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-06-22 14:30:53 +00:00
|
|
|
defp do_transform_string(value) when byte_size(value) == 0, do: nil
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-06-22 14:30:53 +00:00
|
|
|
defp do_transform_string(value) do
|
|
|
|
cond do
|
|
|
|
String.starts_with?(value, "Pleroma") or String.starts_with?(value, "Phoenix") ->
|
|
|
|
String.to_existing_atom("Elixir." <> value)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-06-22 14:30:53 +00:00
|
|
|
String.starts_with?(value, ":") ->
|
|
|
|
String.replace(value, ":", "") |> String.to_existing_atom()
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-06-22 14:30:53 +00:00
|
|
|
String.starts_with?(value, "i:") ->
|
|
|
|
String.replace(value, "i:", "") |> String.to_integer()
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-06-22 14:30:53 +00:00
|
|
|
true ->
|
|
|
|
value
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|