Merge branch 'develop' into stable
[akkoma] / lib / pleroma / helpers / uri_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Helpers.UriHelper do
6 def modify_uri_params(uri, overridden_params, deleted_params \\ []) do
7 uri = URI.parse(uri)
8
9 existing_params = URI.query_decoder(uri.query || "") |> Map.new()
10 overridden_params = Map.new(overridden_params, fn {k, v} -> {to_string(k), v} end)
11 deleted_params = Enum.map(deleted_params, &to_string/1)
12
13 updated_params =
14 existing_params
15 |> Map.merge(overridden_params)
16 |> Map.drop(deleted_params)
17
18 uri
19 |> Map.put(:query, URI.encode_query(updated_params))
20 |> URI.to_string()
21 |> String.replace_suffix("?", "")
22 end
23
24 def maybe_add_base("/" <> uri, base), do: Path.join([base, uri])
25 def maybe_add_base(uri, _base), do: uri
26 end