logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git
commit: e7af2addd85c66edc62262e5e799649fef6a9911
parent 4c5b45ed73e93e6e8bcfeb527b9b398ec64c0caf
Author: Alexander Tumin <iamtakingiteasy@eientei.org>
Date:   Tue, 12 Dec 2023 15:08:20 +0300

Add media proxy to opengraph rich media cards

Diffstat:

Achangelog.d/opengraph-rich-media-proxy.add1+
Mlib/pleroma/web/mastodon_api/views/status_view.ex27+++++++++++++++++----------
Mtest/pleroma/web/mastodon_api/views/status_view_test.exs30++++++++++++++++++++++++++++++
3 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/changelog.d/opengraph-rich-media-proxy.add 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 @@ -562,25 +562,24 @@ defmodule Pleroma.Web.MastodonAPI.StatusView 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 @@ defmodule Pleroma.Web.MastodonAPI.StatusView 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 @@ -778,6 +778,36 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest 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