From e7af2addd85c66edc62262e5e799649fef6a9911 Mon Sep 17 00:00:00 2001 From: Alexander Tumin Date: Tue, 12 Dec 2023 15:08:20 +0300 Subject: [PATCH] Add media proxy to opengraph rich media cards --- changelog.d/opengraph-rich-media-proxy.add | 1 + .../web/mastodon_api/views/status_view.ex | 27 ++++++++++------- .../mastodon_api/views/status_view_test.exs | 30 +++++++++++++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 changelog.d/opengraph-rich-media-proxy.add diff --git a/changelog.d/opengraph-rich-media-proxy.add b/changelog.d/opengraph-rich-media-proxy.add new file mode 100644 index 000000000..2b2fc657d --- /dev/null +++ b/changelog.d/opengraph-rich-media-proxy.add @@ -0,0 +1 @@ +Add media proxy to opengraph rich media cards diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index d070262cc..8825f3715 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -562,25 +562,24 @@ def render("card.json", %{rich_media: rich_media, page_url: page_url}) do page_url = page_url_data |> to_string - image_url_data = - if is_binary(rich_media["image"]) do - URI.parse(rich_media["image"]) - else - nil - end - - image_url = build_image_url(image_url_data, page_url_data) + image_url = proxied_url(rich_media["image"], page_url_data) + audio_url = proxied_url(rich_media["audio"], page_url_data) + video_url = proxied_url(rich_media["video"], page_url_data) %{ type: "link", provider_name: page_url_data.host, provider_url: page_url_data.scheme <> "://" <> page_url_data.host, url: page_url, - image: image_url |> MediaProxy.url(), + image: image_url, title: rich_media["title"] || "", description: rich_media["description"] || "", pleroma: %{ - opengraph: rich_media + opengraph: + rich_media + |> Maps.put_if_present("image", image_url) + |> Maps.put_if_present("audio", audio_url) + |> Maps.put_if_present("video", video_url) } } end @@ -817,4 +816,12 @@ defp get_source_content_type(%{"mediaType" => type} = _source) do defp get_source_content_type(_source) do Utils.get_content_type(nil) end + + defp proxied_url(url, page_url_data) do + if is_binary(url) do + build_image_url(URI.parse(url), page_url_data) |> MediaProxy.url() + else + nil + end + end end diff --git a/test/pleroma/web/mastodon_api/views/status_view_test.exs b/test/pleroma/web/mastodon_api/views/status_view_test.exs index baa9b32f5..0bc232556 100644 --- a/test/pleroma/web/mastodon_api/views/status_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/status_view_test.exs @@ -778,6 +778,36 @@ test "a rich media card with all relevant data renders correctly" do %{provider_name: "example.com"} = StatusView.render("card.json", %{page_url: page_url, rich_media: card}) end + + test "a rich media card has all media proxied" do + clear_config([:media_proxy, :enabled], true) + clear_config([:media_preview_proxy, :enabled]) + + page_url = "http://example.com" + + card = %{ + url: page_url, + site_name: "Example site name", + title: "Example website", + image: page_url <> "/example.jpg", + audio: page_url <> "/example.ogg", + video: page_url <> "/example.mp4", + description: "Example description" + } + + strcard = for {k, v} <- card, into: %{}, do: {to_string(k), v} + + %{ + provider_name: "example.com", + image: image, + pleroma: %{opengraph: og} + } = StatusView.render("card.json", %{page_url: page_url, rich_media: strcard}) + + assert String.match?(image, ~r/\/proxy\//) + assert String.match?(og["image"], ~r/\/proxy\//) + assert String.match?(og["audio"], ~r/\/proxy\//) + assert String.match?(og["video"], ~r/\/proxy\//) + end end test "does not embed a relationship in the account" do