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
27 params = Map.put(params, :value, transform(params[:value]))
30 |> cast(params, [:key, :group, :value])
31 |> validate_required([:key, :group, :value])
32 |> unique_constraint(:key, name: :config_group_key_index)
35 @spec create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
42 @spec update(Config.t(), map()) :: {:ok, Config} | {:error, Changeset.t()}
43 def update(%Config{} = config, %{value: value}) do
45 |> changeset(%{value: value})
49 @spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
50 def update_or_create(params) do
51 search_opts = Map.take(params, [:group, :key])
53 with %Config{} = config <- Config.get_by_params(search_opts) do
54 Config.update(config, params)
56 nil -> Config.create(params)
60 @spec delete(map()) :: {:ok, Config.t()} | {:error, Changeset.t()} | {:ok, nil}
62 search_opts = Map.delete(params, :subkeys)
64 with %Config{} = config <- Config.get_by_params(search_opts),
65 {config, sub_keys} when is_list(sub_keys) <- {config, params[:subkeys]},
66 old_value <- :erlang.binary_to_term(config.value),
67 keys <- Enum.map(sub_keys, &do_transform_string(&1)),
68 new_value <- Keyword.drop(old_value, keys) do
69 Config.update(config, %{value: new_value})
77 dgettext("errors", "Config with params %{params} not found", params: inspect(params))
83 @spec from_binary(binary()) :: term()
84 def from_binary(binary), do: :erlang.binary_to_term(binary)
86 @spec from_binary_with_convert(binary()) :: any()
87 def from_binary_with_convert(binary) do
93 @spec from_string(String.t()) :: atom() | no_return()
94 def from_string(":" <> entity), do: String.to_existing_atom(entity)
96 def from_string(entity) when is_binary(entity) do
97 if is_module_name?(entity) do
98 String.to_existing_atom("Elixir.#{entity}")
104 defp do_convert(entity) when is_list(entity) do
105 for v <- entity, into: [], do: do_convert(v)
108 defp do_convert(%Regex{} = entity), do: inspect(entity)
110 defp do_convert(entity) when is_map(entity) do
111 for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
114 defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
115 # TODO: will become useless after removing hackney
116 defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
118 defp do_convert(entity) when is_tuple(entity),
119 do: %{"tuple" => do_convert(Tuple.to_list(entity))}
121 defp do_convert(entity) when is_boolean(entity) or is_number(entity) or is_nil(entity),
124 defp do_convert(entity) when is_atom(entity), do: inspect(entity)
126 defp do_convert(entity) when is_binary(entity), do: entity
128 @spec transform(any()) :: binary() | no_return()
129 def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity) do
132 |> :erlang.term_to_binary()
135 def transform(entity), do: :erlang.term_to_binary(entity)
137 defp do_transform(%Regex{} = entity), do: entity
139 defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
140 {dispatch_settings, []} = do_eval(entity)
141 {:dispatch, [dispatch_settings]}
144 # TODO: will become useless after removing hackney
145 defp do_transform(%{"tuple" => [":partial_chain", entity]}) do
146 {partial_chain, []} = do_eval(entity)
147 {:partial_chain, partial_chain}
150 defp do_transform(%{"tuple" => entity}) do
151 Enum.reduce(entity, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
154 defp do_transform(entity) when is_map(entity) do
155 for {k, v} <- entity, into: %{}, do: {do_transform(k), do_transform(v)}
158 defp do_transform(entity) when is_list(entity) do
159 for v <- entity, into: [], do: do_transform(v)
162 defp do_transform(entity) when is_binary(entity) do
165 |> do_transform_string()
168 defp do_transform(entity), do: entity
170 @delimiters ["/", "|", "\"", "'", {"(", ")"}, {"[", "]"}, {"{", "}"}, {"<", ">"}]
172 defp find_valid_delimiter([], _string, _),
173 do: raise(ArgumentError, message: "valid delimiter for Regex expression not found")
175 defp find_valid_delimiter([{leading, closing} = delimiter | others], pattern, regex_delimiter)
176 when is_tuple(delimiter) do
177 if String.contains?(pattern, closing) do
178 find_valid_delimiter(others, pattern, regex_delimiter)
180 {:ok, {leading, closing}}
184 defp find_valid_delimiter([delimiter | others], pattern, regex_delimiter) do
185 if String.contains?(pattern, delimiter) do
186 find_valid_delimiter(others, pattern, regex_delimiter)
188 {:ok, {delimiter, delimiter}}
192 @regex_parts ~r/^~r(?'delimiter'[\/|"'([{<]{1})(?'pattern'.+)[\/|"')\]}>]{1}(?'modifier'[uismxfU]*)/u
194 defp do_transform_string("~r" <> _pattern = regex) do
195 with %{"modifier" => modifier, "pattern" => pattern, "delimiter" => regex_delimiter} <-
196 Regex.named_captures(@regex_parts, regex),
197 {:ok, {leading, closing}} <- find_valid_delimiter(@delimiters, pattern, regex_delimiter),
198 {result, _} <- Code.eval_string("~r#{leading}#{pattern}#{closing}#{modifier}") do
203 defp do_transform_string(":" <> atom), do: String.to_atom(atom)
205 defp do_transform_string(value) do
206 if is_module_name?(value) do
207 String.to_existing_atom("Elixir." <> value)
213 @spec is_module_name?(String.t()) :: boolean()
214 def is_module_name?(string) do
215 Regex.match?(~r/^(Pleroma|Phoenix|Tesla)\./, string) or string in ["Oban", "Ueberauth"]
218 defp do_eval(entity) do
219 cleaned_string = String.replace(entity, ~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
220 Code.eval_string(cleaned_string)