Commit 846ad9a4 authored by Alexander Strizhakov's avatar Alexander Strizhakov Committed by kaniini

admin api configure changes

parent e4e3fd7e
...@@ -25,10 +25,15 @@ Configuration: `federation_incoming_replies_max_depth` option ...@@ -25,10 +25,15 @@ Configuration: `federation_incoming_replies_max_depth` option
- Admin API: Return users' tags when querying reports - Admin API: Return users' tags when querying reports
- Admin API: Return avatar and display name when querying users - Admin API: Return avatar and display name when querying users
- Admin API: Allow querying user by ID - Admin API: Allow querying user by ID
- Admin API: Added support for `tuples`.
- Added synchronization of following/followers counters for external users - Added synchronization of following/followers counters for external users
- Configuration: `enabled` option for `Pleroma.Emails.Mailer`, defaulting to `false`. - Configuration: `enabled` option for `Pleroma.Emails.Mailer`, defaulting to `false`.
- Mastodon API: Add support for categories for custom emojis by reusing the group feature. <https://github.com/tootsuite/mastodon/pull/11196> - Mastodon API: Add support for categories for custom emojis by reusing the group feature. <https://github.com/tootsuite/mastodon/pull/11196>
### Changed
- Configuration: Filter.AnonymizeFilename added ability to retain file extension with custom text
- Admin API: changed json structure for saving config settings.
## [1.0.0] - 2019-06-29 ## [1.0.0] - 2019-06-29
### Security ### Security
- Mastodon API: Fix display names not being sanitized - Mastodon API: Fix display names not being sanitized
......
...@@ -573,7 +573,7 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret ...@@ -573,7 +573,7 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
configs: [ configs: [
{ {
"group": string, "group": string,
"key": string, "key": string or string with leading `:` for atoms,
"value": string or {} or [] or {"tuple": []} "value": string or {} or [] or {"tuple": []}
} }
] ]
...@@ -583,10 +583,11 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret ...@@ -583,10 +583,11 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
## `/api/pleroma/admin/config` ## `/api/pleroma/admin/config`
### Update config settings ### Update config settings
Module name can be passed as string, which starts with `Pleroma`, e.g. `"Pleroma.Upload"`. Module name can be passed as string, which starts with `Pleroma`, e.g. `"Pleroma.Upload"`.
Atom or boolean value can be passed with `:` in the beginning, e.g. `":true"`, `":upload"`. For keys it is not needed. Atom keys and values can be passed with `:` in the beginning, e.g. `":upload"`.
Integer with `i:`, e.g. `"i:150"`. Tuples can be passed as `{"tuple": ["first_val", Pleroma.Module, []]}`.
Tuple with more than 2 values with `{"tuple": ["first_val", Pleroma.Module, []]}`.
`{"tuple": ["some_string", "Pleroma.Some.Module", []]}` will be converted to `{"some_string", Pleroma.Some.Module, []}`. `{"tuple": ["some_string", "Pleroma.Some.Module", []]}` will be converted to `{"some_string", Pleroma.Some.Module, []}`.
Keywords can be passed as lists with 2 child tuples, e.g.
`[{"tuple": ["first_val", Pleroma.Module]}, {"tuple": ["second_val", true]}]`.
Compile time settings (need instance reboot): Compile time settings (need instance reboot):
- all settings by this keys: - all settings by this keys:
...@@ -603,7 +604,7 @@ Compile time settings (need instance reboot): ...@@ -603,7 +604,7 @@ Compile time settings (need instance reboot):
- Params: - Params:
- `configs` => [ - `configs` => [
- `group` (string) - `group` (string)
- `key` (string) - `key` (string or string with leading `:` for atoms)
- `value` (string, [], {} or {"tuple": []}) - `value` (string, [], {} or {"tuple": []})
- `delete` = true (optional, if parameter must be deleted) - `delete` = true (optional, if parameter must be deleted)
] ]
...@@ -616,24 +617,25 @@ Compile time settings (need instance reboot): ...@@ -616,24 +617,25 @@ Compile time settings (need instance reboot):
{ {
"group": "pleroma", "group": "pleroma",
"key": "Pleroma.Upload", "key": "Pleroma.Upload",
"value": { "value": [
"uploader": "Pleroma.Uploaders.Local", {"tuple": [":uploader", "Pleroma.Uploaders.Local"]},
"filters": ["Pleroma.Upload.Filter.Dedupe"], {"tuple": [":filters", ["Pleroma.Upload.Filter.Dedupe"]]},
"link_name": ":true", {"tuple": [":link_name", true]},
"proxy_remote": ":false", {"tuple": [":proxy_remote", false]},
"proxy_opts": { {"tuple": [":proxy_opts", [
"redirect_on_failure": ":false", {"tuple": [":redirect_on_failure", false]},
"max_body_length": "i:1048576", {"tuple": [":max_body_length", 1048576]},
"http": { {"tuple": [":http": [
"follow_redirect": ":true", {"tuple": [":follow_redirect", true]},
"pool": ":upload" {"tuple": [":pool", ":upload"]},
} ]]}
}, ]
"dispatch": { ]},
{"tuple": [":dispatch", {
"tuple": ["/api/v1/streaming", "Pleroma.Web.MastodonAPI.WebsocketHandler", []] "tuple": ["/api/v1/streaming", "Pleroma.Web.MastodonAPI.WebsocketHandler", []]
} }]}
} ]
} }
] ]
} }
...@@ -644,7 +646,7 @@ Compile time settings (need instance reboot): ...@@ -644,7 +646,7 @@ Compile time settings (need instance reboot):
configs: [ configs: [
{ {
"group": string, "group": string,
"key": string, "key": string or string with leading `:` for atoms,
"value": string or {} or [] or {"tuple": []} "value": string or {} or [] or {"tuple": []}
} }
] ]
......
...@@ -371,13 +371,13 @@ def config_update(conn, %{"configs" => configs}) do ...@@ -371,13 +371,13 @@ def config_update(conn, %{"configs" => configs}) do
if Pleroma.Config.get([:instance, :dynamic_configuration]) do if Pleroma.Config.get([:instance, :dynamic_configuration]) do
updated = updated =
Enum.map(configs, fn Enum.map(configs, fn
%{"group" => group, "key" => key, "value" => value} ->
{:ok, config} = Config.update_or_create(%{group: group, key: key, value: value})
config
%{"group" => group, "key" => key, "delete" => "true"} -> %{"group" => group, "key" => key, "delete" => "true"} ->
{:ok, _} = Config.delete(%{group: group, key: key}) {:ok, _} = Config.delete(%{group: group, key: key})
nil nil
%{"group" => group, "key" => key, "value" => value} ->
{:ok, config} = Config.update_or_create(%{group: group, key: key, value: value})
config
end) end)
|> Enum.reject(&is_nil(&1)) |> Enum.reject(&is_nil(&1))
......
...@@ -67,99 +67,86 @@ def delete(params) do ...@@ -67,99 +67,86 @@ def delete(params) do
end end
@spec from_binary(binary()) :: term() @spec from_binary(binary()) :: term()
def from_binary(value), do: :erlang.binary_to_term(value) def from_binary(binary), do: :erlang.binary_to_term(binary)
@spec from_binary_to_map(binary()) :: any() @spec from_binary_with_convert(binary()) :: any()
def from_binary_to_map(binary) do def from_binary_with_convert(binary) do
from_binary(binary) from_binary(binary)
|> do_convert() |> do_convert()
end end
defp do_convert([{k, v}] = value) when is_list(value) and length(value) == 1, defp do_convert(entity) when is_list(entity) do
do: %{k => do_convert(v)} for v <- entity, into: [], do: do_convert(v)
end
defp do_convert(values) when is_list(values), do: for(val <- values, do: do_convert(val)) defp do_convert(entity) when is_map(entity) do
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
end
defp do_convert({k, v} = value) when is_tuple(value), defp do_convert({:dispatch, [entity]}), do: %{"tuple" => [":dispatch", [inspect(entity)]]}
do: %{k => do_convert(v)}
defp do_convert(value) when is_tuple(value), do: %{"tuple" => do_convert(Tuple.to_list(value))} defp do_convert(entity) when is_tuple(entity),
do: %{"tuple" => do_convert(Tuple.to_list(entity))}
defp do_convert(value) when is_binary(value) or is_map(value) or is_number(value), do: value defp do_convert(entity) when is_boolean(entity) or is_number(entity) or is_nil(entity),
do: entity
defp do_convert(value) when is_atom(value) do defp do_convert(entity) when is_atom(entity) do
string = to_string(value) string = to_string(entity)
if String.starts_with?(string, "Elixir."), if String.starts_with?(string, "Elixir."),
do: String.trim_leading(string, "Elixir."), do: do_convert(string),
else: value else: ":" <> string
end end
@spec transform(any()) :: binary() defp do_convert("Elixir." <> module_name), do: module_name
def transform(%{"tuple" => _} = entity), do: :erlang.term_to_binary(do_transform(entity))
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) defp do_convert(entity) when is_binary(entity), do: entity
|> Enum.sort()
|> :erlang.term_to_binary()
end
def transform(entity) when is_list(entity) do @spec transform(any()) :: binary()
list = Enum.map(entity, &do_transform(&1)) def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity) do
:erlang.term_to_binary(list) :erlang.term_to_binary(do_transform(entity))
end end
def transform(entity), do: :erlang.term_to_binary(entity) def transform(entity), do: :erlang.term_to_binary(entity)
defp do_transform(%Regex{} = value) when is_map(value), do: value defp do_transform(%Regex{} = entity) when is_map(entity), do: entity
defp do_transform(%{"tuple" => [k, values] = entity}) when length(entity) == 2 do defp do_transform(%{"tuple" => [":dispatch", [entity]]}) do
{do_transform(k), do_transform(values)} cleaned_string = String.replace(entity, ~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
{dispatch_settings, []} = Code.eval_string(cleaned_string, [], requires: [], macros: [])
{:dispatch, [dispatch_settings]}
end end
defp do_transform(%{"tuple" => values}) do defp do_transform(%{"tuple" => entity}) do
Enum.reduce(values, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end) Enum.reduce(entity, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
end end
defp do_transform(value) when is_map(value) do defp do_transform(entity) when is_map(entity) do
values = for {key, val} <- value, into: [], do: {String.to_atom(key), do_transform(val)} for {k, v} <- entity, into: %{}, do: {do_transform(k), do_transform(v)}
Enum.sort(values)
end end
defp do_transform(value) when is_list(value) do defp do_transform(entity) when is_list(entity) do
Enum.map(value, &do_transform(&1)) for v <- entity, into: [], do: do_transform(v)
end end
defp do_transform(entity) when is_list(entity) and length(entity) == 1, do: hd(entity) defp do_transform(entity) when is_binary(entity) do
String.trim(entity)
defp do_transform(value) when is_binary(value) do
String.trim(value)
|> do_transform_string() |> do_transform_string()
end end
defp do_transform(value), do: value defp do_transform(entity), do: entity
defp do_transform_string(value) when byte_size(value) == 0, do: nil
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)
String.starts_with?(value, ":") -> defp do_transform_string("~r/" <> pattern) do
String.replace(value, ":", "") |> String.to_existing_atom() pattern = String.trim_trailing(pattern, "/")
~r/#{pattern}/
end
String.starts_with?(value, "i:") -> defp do_transform_string(":" <> atom), do: String.to_atom(atom)
String.replace(value, "i:", "") |> String.to_integer()
true -> defp do_transform_string(value) do
value if String.starts_with?(value, "Pleroma") or String.starts_with?(value, "Phoenix"),
end do: String.to_existing_atom("Elixir." <> value),
else: value
end end
end end
...@@ -15,7 +15,7 @@ def render("show.json", %{config: config}) do ...@@ -15,7 +15,7 @@ def render("show.json", %{config: config}) do
%{ %{
key: config.key, key: config.key,
group: config.group, group: config.group,
value: Pleroma.Web.AdminAPI.Config.from_binary_to_map(config.value) value: Pleroma.Web.AdminAPI.Config.from_binary_with_convert(config.value)
} }
end end
end end
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment