1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.Config do
8 import Pleroma.Web.Gettext
12 @type t :: %__MODULE__{}
16 field(:group, :string)
17 field(:value, :binary)
22 @spec get_by_params(map()) :: Config.t() | nil
23 def get_by_params(params), do: Repo.get_by(Config, params)
25 @spec changeset(Config.t(), map()) :: Changeset.t()
26 def changeset(config, params \\ %{}) do
28 |> cast(params, [:key, :group, :value])
29 |> validate_required([:key, :group, :value])
30 |> unique_constraint(:key, name: :config_group_key_index)
33 @spec create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
36 |> changeset(Map.put(params, :value, transform(params[:value])))
40 @spec update(Config.t(), map()) :: {:ok, Config} | {:error, Changeset.t()}
41 def update(%Config{} = config, %{value: value}) do
43 |> change(value: transform(value))
47 @spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
48 def update_or_create(params) do
49 with %Config{} = config <- Config.get_by_params(Map.take(params, [:group, :key])) do
50 Config.update(config, params)
52 nil -> Config.create(params)
56 @spec delete(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
58 with %Config{} = config <- Config.get_by_params(Map.delete(params, :subkeys)) do
59 if params[:subkeys] do
62 :erlang.binary_to_term(config.value),
63 Enum.map(params[:subkeys], &do_transform_string(&1))
66 Config.update(config, %{value: updated_value})
74 dgettext("errors", "Config with params %{params} not found", params: inspect(params))
80 @spec from_binary(binary()) :: term()
81 def from_binary(binary), do: :erlang.binary_to_term(binary)
83 @spec from_binary_with_convert(binary()) :: any()
84 def from_binary_with_convert(binary) do
89 defp do_convert(entity) when is_list(entity) do
90 for v <- entity, into: [], do: do_convert(v)
93 defp do_convert(entity) when is_map(entity) do
94 for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
97 defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
98 defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
100 defp do_convert(entity) when is_tuple(entity),
101 do: %{"tuple" => do_convert(Tuple.to_list(entity))}
103 defp do_convert(entity) when is_boolean(entity) or is_number(entity) or is_nil(entity),
106 defp do_convert(entity) when is_atom(entity) do
107 string = to_string(entity)
109 if String.starts_with?(string, "Elixir."),
110 do: do_convert(string),
114 defp do_convert("Elixir." <> module_name), do: module_name
116 defp do_convert(entity) when is_binary(entity), do: entity
118 @spec transform(any()) :: binary()
119 def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity) do
120 :erlang.term_to_binary(do_transform(entity))
123 def transform(entity), do: :erlang.term_to_binary(entity)
125 defp do_transform(%Regex{} = entity) when is_map(entity), do: entity
127 defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
128 {dispatch_settings, []} = do_eval(entity)
129 {:dispatch, [dispatch_settings]}
132 defp do_transform(%{"tuple" => [":partial_chain", entity]}) do
133 {partial_chain, []} = do_eval(entity)
134 {:partial_chain, partial_chain}
137 defp do_transform(%{"tuple" => entity}) do
138 Enum.reduce(entity, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
141 defp do_transform(entity) when is_map(entity) do
142 for {k, v} <- entity, into: %{}, do: {do_transform(k), do_transform(v)}
145 defp do_transform(entity) when is_list(entity) do
146 for v <- entity, into: [], do: do_transform(v)
149 defp do_transform(entity) when is_binary(entity) do
151 |> do_transform_string()
154 defp do_transform(entity), do: entity
156 defp do_transform_string("~r/" <> pattern) do
157 pattern = String.trim_trailing(pattern, "/")
161 defp do_transform_string(":" <> atom), do: String.to_atom(atom)
163 defp do_transform_string(value) do
164 if String.starts_with?(value, "Pleroma") or String.starts_with?(value, "Phoenix"),
165 do: String.to_existing_atom("Elixir." <> value),
169 defp do_eval(entity) do
170 cleaned_string = String.replace(entity, ~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
171 Code.eval_string(cleaned_string, [], requires: [], macros: [])