aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Felder <feld@feld.me>2021-03-11 09:20:29 -0600
committerMark Felder <feld@feld.me>2021-03-11 09:20:29 -0600
commit029ff6538972b59c6259dd7345ad9c4465fb3f73 (patch)
tree9be5c843393d8d37a01d26e19cd980dd0636d91d
parent8246db2a968943a0cab615b8b5c1439aa4cb2547 (diff)
downloadpleroma-029ff6538972b59c6259dd7345ad9c4465fb3f73.tar.gz
Leverage function pattern matching instead
-rw-r--r--lib/pleroma/web/mastodon_api/views/status_view.ex36
1 files changed, 26 insertions, 10 deletions
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index cf8037abb..581b4e952 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -379,18 +379,15 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
page_url = page_url_data |> to_string
- image_url =
- cond do
- !is_binary(rich_media["image"]) ->
- nil
-
- String.starts_with?(rich_media["image"], "http") ->
- rich_media["image"]
-
- true ->
- URI.merge(page_url_data, URI.parse(rich_media["image"])) |> to_string
+ image_url_data =
+ if is_binary(rich_media["image"]) do
+ URI.parse(rich_media["image"])
+ else
+ nil
end
+ image_url = get_image_url(image_url_data, page_url_data)
+
%{
type: "link",
provider_name: page_url_data.host,
@@ -546,4 +543,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
do: %{name: name, website: url}
defp build_application(_), do: nil
+
+ # Workaround for Elixir issue #10771
+ # Avoid applying URI.merge unless necessary
+ # TODO: revert to always attempting URI.merge(image_url_data, page_url_data)
+ # when Elixir 1.12 is the minimum supported version
+ @spec get_image_url(struct() | nil, struct()) :: String.t() | nil
+ defp get_image_url(
+ %URI{scheme: image_scheme, host: image_host} = image_url_data,
+ %URI{} = _page_url_data
+ )
+ when not is_nil(image_scheme) and not is_nil(image_host) do
+ image_url_data |> to_string
+ end
+
+ defp get_image_url(%URI{} = image_url_data, %URI{} = page_url_data) do
+ URI.merge(page_url_data, image_url_data) |> to_string
+ end
+
+ defp get_image_url(_, _), do: nil
end