From 3aff8067e4b65962fe65812807cbb52bd971ddd8 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Sat, 17 Nov 2018 18:34:45 +0100 Subject: [PATCH 1/8] =?UTF-8?q?transmogrifier:=20When=20it=E2=80=99s=20a?= =?UTF-8?q?=20Video=20move=20"url"=20to=20"attachment"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pleroma/web/activity_pub/transmogrifier.ex | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index e6af4b211..d5cc82918 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -69,8 +69,8 @@ def contain_origin_from_id(id, %{"id" => other_id} = _params) do def fix_object(object) do object |> fix_actor - |> fix_attachments |> fix_url + |> fix_attachments |> fix_context |> fix_in_reply_to |> fix_emoji @@ -200,8 +200,14 @@ def fix_url(%{"url" => url} = object) when is_list(url) do true -> "" end - object - |> Map.put("url", url_string) + if Map.get(object, "type") == "Video" do + object + |> Map.delete("url") + |> Map.put("attachment", url_string) + else + object + |> Map.put("url", url_string) + end end def fix_url(object), do: object From 71f6d9f418087a16ff266ef380b3290088e0d301 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 23 Dec 2018 13:28:17 +0000 Subject: [PATCH 2/8] transmogrifier: significantly rework handling of peertube videos, add test --- .../web/activity_pub/transmogrifier.ex | 37 +++++++++++++------ test/web/activity_pub/transmogrifier_test.exs | 30 +++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index d5cc82918..4bc96b00d 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -170,8 +170,14 @@ def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachm attachments = attachment |> Enum.map(fn data -> - url = [%{"type" => "Link", "mediaType" => data["mediaType"], "href" => data["url"]}] - Map.put(data, "url", url) + media_type = data["mediaType"] || data["mimeType"] + href = data["url"] || data["href"] + + url = [%{"type" => "Link", "mediaType" => media_type, "href" => href}] + + data + |> Map.put("mediaType", media_type) + |> Map.put("url", url) end) object @@ -190,7 +196,22 @@ def fix_url(%{"url" => url} = object) when is_map(url) do |> Map.put("url", url["href"]) end - def fix_url(%{"url" => url} = object) when is_list(url) do + def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do + first_element = Enum.at(url, 0) + + link_element = + url + |> Enum.filter(fn x -> is_map(x) end) + |> Enum.filter(fn x -> x["mimeType"] == "text/html" end) + |> Enum.at(0) + + object + |> Map.put("attachment", [first_element]) + |> Map.put("url", link_element["href"]) + end + + def fix_url(%{"type" => object_type, "url" => url} = object) + when object_type != "Video" and is_list(url) do first_element = Enum.at(url, 0) url_string = @@ -200,14 +221,8 @@ def fix_url(%{"url" => url} = object) when is_list(url) do true -> "" end - if Map.get(object, "type") == "Video" do - object - |> Map.delete("url") - |> Map.put("attachment", url_string) - else - object - |> Map.put("url", url_string) - end + object + |> Map.put("url", url_string) end def fix_url(object), do: object diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 0428e052d..6778db390 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -684,6 +684,36 @@ test "it rejects activities without a valid ID" do :error = Transmogrifier.handle_incoming(data) end + + test "it remaps video URLs as attachments if necessary" do + {:ok, object} = + ActivityPub.fetch_object_from_id( + "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3" + ) + + attachment = %{ + "type" => "Link", + "mediaType" => "video/mp4", + "href" => + "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4", + "mimeType" => "video/mp4", + "size" => 5_015_880, + "url" => [ + %{ + "href" => + "https://peertube.moe/static/webseed/df5f464b-be8d-46fb-ad81-2d4c2d1630e3-480.mp4", + "mediaType" => "video/mp4", + "type" => "Link" + } + ], + "width" => 480 + } + + assert object.data["url"] == + "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3" + + assert object.data["attachment"] == [attachment] + end end describe "prepare outgoing" do From 32dfc1d12a29004ef3d90c8405d25e49e74c80ab Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 23 Dec 2018 13:40:33 +0000 Subject: [PATCH 3/8] mastodon api: status view: remove obsolete peertube hack --- lib/pleroma/web/mastodon_api/views/status_view.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 46c559e3a..d06da812c 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -106,7 +106,6 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity} favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || []) attachment_data = object["attachment"] || [] - attachment_data = attachment_data ++ if object["type"] == "Video", do: [object], else: [] attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment) created_at = Utils.to_masto_date(object["published"]) From 9f48485f64a99dbf29f3984e614b25aee32efdc4 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 23 Dec 2018 13:42:42 +0000 Subject: [PATCH 4/8] tests: mastodon api: add test verifying that peertube videos are correctly rendered --- test/web/mastodon_api/status_view_test.exs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index b7ac92760..0af7d8621 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -5,6 +5,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do alias Pleroma.User alias Pleroma.Web.OStatus alias Pleroma.Web.CommonAPI + alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Activity import Pleroma.Factory import Tesla.Mock @@ -157,6 +159,22 @@ test "a reblog" do assert represented[:emojis] == [] end + test "a peertube video" do + user = insert(:user) + + {:ok, object} = + ActivityPub.fetch_object_from_id( + "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3" + ) + + %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"]) + + represented = StatusView.render("status.json", %{for: user, activity: activity}) + + assert represented[:id] == to_string(activity.id) + assert length(represented[:media_attachments]) == 1 + end + describe "build_tags/1" do test "it returns a a dictionary tags" do object_tags = [ From 79b51a97fe9479948d429f095c5b07bc78ab5edf Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 23 Dec 2018 13:53:25 +0000 Subject: [PATCH 5/8] twitter api: activity representer: remove peertube hack --- .../web/twitter_api/representers/activity_representer.ex | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 2808192b0..d9dd352be 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -171,14 +171,7 @@ def to_map( HTML.filter_tags(content, User.html_filter_policy(opts[:for])) |> Formatter.emojify(object["emoji"]) - video = - if object["type"] == "Video" do - [object] - else - [] - end - - attachments = (object["attachment"] || []) ++ video + attachments = object["attachment"] || [] reply_parent = Activity.get_in_reply_to_activity(activity) From 873938d223949f647a196b7f2a4140d323fbab1c Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 23 Dec 2018 13:55:08 +0000 Subject: [PATCH 6/8] tests: twitter api: activity view test: enable tesla mock --- test/web/twitter_api/views/activity_view_test.exs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs index 8aa9e3130..09d7a18d4 100644 --- a/test/web/twitter_api/views/activity_view_test.exs +++ b/test/web/twitter_api/views/activity_view_test.exs @@ -12,6 +12,13 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do alias Pleroma.Web.ActivityPub.ActivityPub import Pleroma.Factory + import Tesla.Mock + + setup do + mock(fn env -> apply(HttpRequestMock, :request, [env]) end) + :ok + end + import Mock test "a create activity with a html status" do From 34a4ed22c4904270b1c5c2acab09e995d9a8d6dd Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 23 Dec 2018 13:58:45 +0000 Subject: [PATCH 7/8] twitter api: add "Video" to supported activity types list --- lib/pleroma/web/twitter_api/views/activity_view.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex index 91d086740..c37d5486a 100644 --- a/lib/pleroma/web/twitter_api/views/activity_view.ex +++ b/lib/pleroma/web/twitter_api/views/activity_view.ex @@ -301,7 +301,8 @@ def render_content(%{"type" => "Note"} = object) do {summary, content} end - def render_content(%{"type" => object_type} = object) when object_type in ["Article", "Page"] do + def render_content(%{"type" => object_type} = object) + when object_type in ["Article", "Page", "Video"] do summary = object["name"] || object["summary"] content = From a2bceaf688608f61151e298e6025ccbecc9de227 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 23 Dec 2018 13:59:06 +0000 Subject: [PATCH 8/8] tests: twitter api: add test proving that peertube videos are correctly handled --- test/web/twitter_api/views/activity_view_test.exs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs index 09d7a18d4..fd511b546 100644 --- a/test/web/twitter_api/views/activity_view_test.exs +++ b/test/web/twitter_api/views/activity_view_test.exs @@ -265,4 +265,18 @@ test "a delete activity" do assert result == expected end + + test "a peertube video" do + {:ok, object} = + ActivityPub.fetch_object_from_id( + "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3" + ) + + %Activity{} = activity = Activity.get_create_activity_by_object_ap_id(object.data["id"]) + + result = ActivityView.render("activity.json", activity: activity) + + assert length(result["attachments"]) == 1 + assert result["summary"] == "Friday Night" + end end