|> Repo.update()
end
+ @full_key_update [
+ {:pleroma, :ecto_repos},
+ {:pleroma, :assets},
+ {:quack, :meta},
+ {:mime, :types},
+ {:cors_plug, [:max_age, :methods, :expose, :headers]},
+ {:auto_linker, :opts},
+ {:swarm, :node_blacklist},
+ {:logger, :backends}
+ ]
+
+ defp only_full_update?(%Config{} = config) do
+ config_group = Config.from_string(config.group)
+ config_key = Config.from_string(config.key)
+
+ Enum.any?(@full_key_update, fn
+ {group, key} when is_list(key) ->
+ config_group == group and config_key in key
+
+ {group, key} ->
+ config_group == group and config_key == key
+ end)
+ end
+
+ defp can_be_partially_updated?(%Config{} = config), do: not only_full_update?(config)
+
@spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
def update_or_create(params) do
search_opts = Map.take(params, [:group, :key])
- with %Config{} = config <- Config.get_by_params(search_opts) do
- Config.update(config, params)
+ with %Config{} = config <- Config.get_by_params(search_opts),
+ {:partial_update, true, config} <-
+ {:partial_update, can_be_partially_updated?(config), config},
+ old_value <- from_binary(config.value),
+ transformed_value <- do_transform(params[:value]),
+ {:can_be_merged, true, config} <- {:can_be_merged, is_list(transformed_value), config},
+ new_value <- DeepMerge.deep_merge(old_value, transformed_value) do
+ Config.update(config, %{value: new_value, transformed?: true})
else
- nil -> Config.create(params)
+ {reason, false, config} when reason in [:partial_update, :can_be_merged] ->
+ Config.update(config, params)
+
+ nil ->
+ Config.create(params)
end
end
with %Config{} = config <- Config.get_by_params(search_opts),
{config, sub_keys} when is_list(sub_keys) <- {config, params[:subkeys]},
- old_value <- :erlang.binary_to_term(config.value),
+ old_value <- from_binary(config.value),
keys <- Enum.map(sub_keys, &do_transform_string(&1)),
new_value <- Keyword.drop(old_value, keys) do
Config.update(config, %{value: new_value})
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
end
+ defp do_convert({:proxy_url, {type, :localhost, port}}) do
+ %{"tuple" => [":proxy_url", %{"tuple" => [do_convert(type), "localhost", port]}]}
+ end
+
+ defp do_convert({:proxy_url, {type, host, port}}) when is_tuple(host) do
+ ip =
+ host
+ |> :inet_parse.ntoa()
+ |> to_string()
+
+ %{
+ "tuple" => [
+ ":proxy_url",
+ %{"tuple" => [do_convert(type), ip, port]}
+ ]
+ }
+ end
+
+ defp do_convert({:proxy_url, {type, host, port}}) do
+ %{
+ "tuple" => [
+ ":proxy_url",
+ %{"tuple" => [do_convert(type), to_string(host), port]}
+ ]
+ }
+ end
+
defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
# TODO: will become useless after removing hackney
defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
defp do_convert(entity) when is_boolean(entity) or is_number(entity) or is_nil(entity),
do: entity
+ defp do_convert(entity)
+ when is_atom(entity) and entity in [:"tlsv1.1", :"tlsv1.2", :"tlsv1.3"] do
+ ":#{to_string(entity)}"
+ end
+
defp do_convert(entity) when is_atom(entity), do: inspect(entity)
defp do_convert(entity) when is_binary(entity), do: entity
def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity) do
entity
|> do_transform()
- |> :erlang.term_to_binary()
+ |> to_binary()
end
- def transform(entity), do: :erlang.term_to_binary(entity)
+ def transform(entity), do: to_binary(entity)
+
+ @spec to_binary(any()) :: binary()
+ def to_binary(entity), do: :erlang.term_to_binary(entity)
defp do_transform(%Regex{} = entity), do: entity
+ defp do_transform(%{"tuple" => [":proxy_url", %{"tuple" => [type, host, port]}]}) do
+ {:proxy_url, {do_transform_string(type), parse_host(host), port}}
+ end
+
defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
{dispatch_settings, []} = do_eval(entity)
{:dispatch, [dispatch_settings]}
defp do_transform(entity), do: entity
+ defp parse_host("localhost"), do: :localhost
+
+ defp parse_host(host) do
+ charlist = to_charlist(host)
+
+ case :inet.parse_address(charlist) do
+ {:error, :einval} ->
+ charlist
+
+ {:ok, ip} ->
+ ip
+ end
+ end
+
@delimiters ["/", "|", "\"", "'", {"(", ")"}, {"[", "]"}, {"{", "}"}, {"<", ">"}]
defp find_valid_delimiter([], _string, _),
@spec is_module_name?(String.t()) :: boolean()
def is_module_name?(string) do
- Regex.match?(~r/^(Pleroma|Phoenix|Tesla)\./, string) or string in ["Oban", "Ueberauth"]
+ Regex.match?(~r/^(Pleroma|Phoenix|Tesla|Quack)\./, string) or
+ string in ["Oban", "Ueberauth", "ExSyslogger"]
end
defp do_eval(entity) do