From d9349bc52f23c7b57fa5b677df186af6f66fb00d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 7 Dec 2021 01:10:47 -0500 Subject: [PATCH 1/5] Transmogrifier: test fix_attachments/1 --- .../web/activity_pub/transmogrifier_test.exs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index 5a3b57acb..4616f8090 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -524,4 +524,44 @@ test "returns {:ok, %Object{}} for success case" do ) end end + + describe "fix_attachments/1" do + test "transforms dimensions into a url" do + object = %{ + "attachment" => [ + %{ + "type" => "Document", + "name" => "Hello world", + "url" => "https://media.example.tld/1.jpg", + "width" => 880, + "height" => 960, + "mediaType" => "image/jpeg", + "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of" + } + ] + } + + expected = %{ + "attachment" => [ + %{ + "type" => "Document", + "name" => "Hello world", + "url" => [ + %{ + "type" => "Link", + "mediaType" => "image/jpeg", + "href" => "https://media.example.tld/1.jpg", + "width" => 880, + "height" => 960 + } + ], + "mediaType" => "image/jpeg", + "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of" + } + ] + } + + assert Transmogrifier.fix_attachments(object) == expected + end + end end From 3f03d71ea62fe63c953a850473217f9b94f2e1b9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 7 Dec 2021 12:59:03 -0500 Subject: [PATCH 2/5] AttachmentValidator: ingest width and height --- .../object_validators/attachment_validator.ex | 10 +++--- .../attachment_validator_test.exs | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex b/lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex index 837787b9f..59fef42d6 100644 --- a/lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/attachment_validator.ex @@ -68,12 +68,14 @@ def fix_media_type(data) do end end - defp handle_href(href, mediaType) do + defp handle_href(href, mediaType, data) do [ %{ "href" => href, "type" => "Link", - "mediaType" => mediaType + "mediaType" => mediaType, + "width" => data["width"], + "height" => data["height"] } ] end @@ -81,10 +83,10 @@ defp handle_href(href, mediaType) do defp fix_url(data) do cond do is_binary(data["url"]) -> - Map.put(data, "url", handle_href(data["url"], data["mediaType"])) + Map.put(data, "url", handle_href(data["url"], data["mediaType"], data)) is_binary(data["href"]) and data["url"] == nil -> - Map.put(data, "url", handle_href(data["href"], data["mediaType"])) + Map.put(data, "url", handle_href(data["href"], data["mediaType"], data)) true -> data diff --git a/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs index 0e49fda99..9150b8d41 100644 --- a/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/attachment_validator_test.exs @@ -105,5 +105,37 @@ test "it handles image dimensions" do assert attachment.mediaType == "image/jpeg" end + + test "it transforms image dimentions to our internal format" do + attachment = %{ + "type" => "Document", + "name" => "Hello world", + "url" => "https://media.example.tld/1.jpg", + "width" => 880, + "height" => 960, + "mediaType" => "image/jpeg", + "blurhash" => "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of" + } + + expected = %AttachmentValidator{ + type: "Document", + name: "Hello world", + mediaType: "image/jpeg", + blurhash: "eTKL26+HDjcEIBVl;ds+K6t301W.t7nit7y1E,R:v}ai4nXSt7V@of", + url: [ + %AttachmentValidator.UrlObjectValidator{ + type: "Link", + mediaType: "image/jpeg", + href: "https://media.example.tld/1.jpg", + width: 880, + height: 960 + } + ] + } + + {:ok, ^expected} = + AttachmentValidator.cast_and_validate(attachment) + |> Ecto.Changeset.apply_action(:insert) + end end end From 335684182a094c10fb9f72e3865fd1b9606484a4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 7 Dec 2021 13:35:34 -0500 Subject: [PATCH 3/5] Fix VideoHandlingTest --- .../web/activity_pub/transmogrifier/video_handling_test.exs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs index 62b4a2cb3..93b139a77 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs @@ -61,7 +61,7 @@ test "it remaps video URLs as attachments if necessary" do "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4", "mediaType" => "video/mp4", "type" => "Link", - "width" => nil, + "width" => 480, "height" => nil } ] @@ -87,7 +87,7 @@ test "it remaps video URLs as attachments if necessary" do "mediaType" => "video/mp4", "type" => "Link", "width" => nil, - "height" => nil + "height" => 1080 } ] } @@ -119,7 +119,7 @@ test "it works for peertube videos with only their mpegURL map" do "mediaType" => "video/mp4", "type" => "Link", "width" => nil, - "height" => nil + "height" => 1080 } ] } From 992d9287d06d6b000a34a0a73124c516b1504ce5 Mon Sep 17 00:00:00 2001 From: Haelwenn Date: Tue, 7 Dec 2021 22:53:36 +0000 Subject: [PATCH 4/5] Apply alexgleason's suggestion(s) to 1 file(s) --- test/pleroma/web/activity_pub/transmogrifier_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index 4616f8090..06daf6a9f 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -526,7 +526,7 @@ test "returns {:ok, %Object{}} for success case" do end describe "fix_attachments/1" do - test "transforms dimensions into a url" do + test "puts dimensions into attachment url field" do object = %{ "attachment" => [ %{ From 01cc099c8ef40efe72b611bc0925a62e5dfd057d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 7 Dec 2021 21:55:54 -0500 Subject: [PATCH 5/5] VideoHandlingTest: remove nil values --- .../web/activity_pub/transmogrifier/video_handling_test.exs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs index 29a75701b..87c53ebf4 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/video_handling_test.exs @@ -59,8 +59,7 @@ test "it remaps video URLs as attachments if necessary" do "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4", "mediaType" => "video/mp4", "type" => "Link", - "width" => 480, - "height" => nil + "width" => 480 } ] } @@ -82,7 +81,6 @@ test "it remaps video URLs as attachments if necessary" do "https://framatube.org/static/webseed/6050732a-8a7a-43d4-a6cd-809525a1d206-1080.mp4", "mediaType" => "video/mp4", "type" => "Link", - "width" => nil, "height" => 1080 } ] @@ -112,7 +110,6 @@ test "it works for peertube videos with only their mpegURL map" do "https://peertube.stream/static/streaming-playlists/hls/abece3c3-b9c6-47f4-8040-f3eed8c602e6/abece3c3-b9c6-47f4-8040-f3eed8c602e6-1080-fragmented.mp4", "mediaType" => "video/mp4", "type" => "Link", - "width" => nil, "height" => 1080 } ]