2019-06-12 20:30:06 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-06-12 20:30:06 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Helpers.UriHelper do
|
2020-09-16 21:30:42 +02:00
|
|
|
def modify_uri_params(uri, overridden_params, deleted_params \\ []) do
|
2019-06-12 20:30:06 +02:00
|
|
|
uri = URI.parse(uri)
|
2020-09-16 21:30:42 +02:00
|
|
|
|
|
|
|
existing_params = URI.query_decoder(uri.query || "") |> Map.new()
|
|
|
|
overridden_params = Map.new(overridden_params, fn {k, v} -> {to_string(k), v} end)
|
|
|
|
deleted_params = Enum.map(deleted_params, &to_string/1)
|
2019-06-12 20:30:06 +02:00
|
|
|
|
|
|
|
updated_params =
|
2020-09-16 21:30:42 +02:00
|
|
|
existing_params
|
|
|
|
|> Map.merge(overridden_params)
|
|
|
|
|> Map.drop(deleted_params)
|
2019-06-12 20:30:06 +02:00
|
|
|
|
|
|
|
uri
|
|
|
|
|> Map.put(:query, URI.encode_query(updated_params))
|
|
|
|
|> URI.to_string()
|
2020-09-03 19:13:29 +02:00
|
|
|
|> String.replace_suffix("?", "")
|
2019-06-12 20:30:06 +02:00
|
|
|
end
|
|
|
|
|
2020-04-12 21:54:43 +02:00
|
|
|
def maybe_add_base("/" <> uri, base), do: Path.join([base, uri])
|
|
|
|
def maybe_add_base(uri, _base), do: uri
|
2019-06-12 20:30:06 +02:00
|
|
|
end
|