From c5c09fc61b7b6e591e9de23028e5caea8f26b996 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Mon, 13 Apr 2020 06:53:45 +0300 Subject: [PATCH] fix mediaType of object --- .../web/activity_pub/transmogrifier.ex | 21 +++++++++++++++---- test/fixtures/tesla_mock/bittube-video.json | 1 + test/fixtures/tesla_mock/hanimated.json | 1 + test/support/http_request_mock.ex | 16 ++++++++++++++ test/web/activity_pub/transmogrifier_test.exs | 9 ++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/tesla_mock/bittube-video.json create mode 100644 test/fixtures/tesla_mock/hanimated.json diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 39feae285..17e3c203a 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -35,6 +35,7 @@ def fix_object(object, options \\ []) do |> fix_actor |> fix_url |> fix_attachments + |> fix_media_type |> fix_context |> fix_in_reply_to(options) |> fix_emoji @@ -357,6 +358,12 @@ def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options) def fix_type(object, _), do: object + defp fix_media_type(%{"mediaType" => _} = object) do + Map.put(object, "mediaType", "text/html") + end + + defp fix_media_type(object), do: object + defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do with true <- id =~ "follows", %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id), @@ -1207,18 +1214,24 @@ def add_attributed_to(object) do def prepare_attachments(object) do attachments = - (object["attachment"] || []) + object + |> Map.get("attachment", []) |> Enum.map(fn data -> [%{"mediaType" => media_type, "href" => href} | _] = data["url"] - %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"} + + %{ + "url" => href, + "mediaType" => media_type, + "name" => data["name"], + "type" => "Document" + } end) Map.put(object, "attachment", attachments) end def strip_internal_fields(object) do - object - |> Map.drop(Pleroma.Constants.object_internal_fields()) + Map.drop(object, Pleroma.Constants.object_internal_fields()) end defp strip_internal_tags(%{"tag" => tags} = object) do diff --git a/test/fixtures/tesla_mock/bittube-video.json b/test/fixtures/tesla_mock/bittube-video.json new file mode 100644 index 000000000..be839862f --- /dev/null +++ b/test/fixtures/tesla_mock/bittube-video.json @@ -0,0 +1 @@ +{"type":"Video","id":"https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b","name":"Implications of 5G Rollout Simply Explained","duration":"PT428S","uuid":"2aad7dfb-5c75-4ee6-a9ed-08436af0558b","tag":[{"type":"Hashtag","name":"5g"},{"type":"Hashtag","name":"big brother"},{"type":"Hashtag","name":"facial recognition"},{"type":"Hashtag","name":"smart device"}],"category":{"identifier":"15","name":"Science & Technology"},"language":{"identifier":"en","name":"English"},"views":5,"sensitive":false,"waitTranscoding":true,"state":1,"commentsEnabled":true,"downloadEnabled":true,"published":"2020-04-12T11:55:44.805Z","originallyPublishedAt":null,"updated":"2020-04-13T02:01:24.279Z","mediaType":"text/markdown","content":null,"support":null,"subtitleLanguage":[],"icon":{"type":"Image","url":"https://bittube.video/static/thumbnails/2aad7dfb-5c75-4ee6-a9ed-08436af0558b.jpg","mediaType":"image/jpeg","width":223,"height":122},"url":[{"type":"Link","mediaType":"text/html","href":"https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b"},{"type":"Link","mediaType":"video/mp4","href":"https://bittube.video/static/webseed/2aad7dfb-5c75-4ee6-a9ed-08436af0558b-240.mp4","height":240,"size":17158094,"fps":30},{"type":"Link","mediaType":"application/x-bittorrent","href":"https://bittube.video/static/torrents/2aad7dfb-5c75-4ee6-a9ed-08436af0558b-240.torrent","height":240},{"type":"Link","mediaType":"application/x-bittorrent;x-scheme-handler/magnet","href":"magnet:?xs=https%3A%2F%2Fbittube.video%2Fstatic%2Ftorrents%2F2aad7dfb-5c75-4ee6-a9ed-08436af0558b-240.torrent&xt=urn:btih:16c8f60d788a29e7ff195de44b4a1558b41dc6c3&dn=Implications+of+5G+Rollout+Simply+Explained&tr=wss%3A%2F%2Fbittube.video%3A443%2Ftracker%2Fsocket&tr=https%3A%2F%2Fbittube.video%2Ftracker%2Fannounce&ws=https%3A%2F%2Fbittube.video%2Fstatic%2Fwebseed%2F2aad7dfb-5c75-4ee6-a9ed-08436af0558b-240.mp4","height":240},{"type":"Link","mediaType":"video/mp4","href":"https://bittube.video/static/webseed/2aad7dfb-5c75-4ee6-a9ed-08436af0558b-0.mp4","height":0,"size":5215186,"fps":0},{"type":"Link","mediaType":"application/x-bittorrent","href":"https://bittube.video/static/torrents/2aad7dfb-5c75-4ee6-a9ed-08436af0558b-0.torrent","height":0},{"type":"Link","mediaType":"application/x-bittorrent;x-scheme-handler/magnet","href":"magnet:?xs=https%3A%2F%2Fbittube.video%2Fstatic%2Ftorrents%2F2aad7dfb-5c75-4ee6-a9ed-08436af0558b-0.torrent&xt=urn:btih:8a043b09291f2947423ce96d1cd0e977662d6de8&dn=Implications+of+5G+Rollout+Simply+Explained&tr=wss%3A%2F%2Fbittube.video%3A443%2Ftracker%2Fsocket&tr=https%3A%2F%2Fbittube.video%2Ftracker%2Fannounce&ws=https%3A%2F%2Fbittube.video%2Fstatic%2Fwebseed%2F2aad7dfb-5c75-4ee6-a9ed-08436af0558b-0.mp4","height":0},{"type":"Link","mediaType":"video/mp4","href":"https://bittube.video/static/webseed/2aad7dfb-5c75-4ee6-a9ed-08436af0558b-360.mp4","height":360,"size":22813140,"fps":30},{"type":"Link","mediaType":"application/x-bittorrent","href":"https://bittube.video/static/torrents/2aad7dfb-5c75-4ee6-a9ed-08436af0558b-360.torrent","height":360},{"type":"Link","mediaType":"application/x-bittorrent;x-scheme-handler/magnet","href":"magnet:?xs=https%3A%2F%2Fbittube.video%2Fstatic%2Ftorrents%2F2aad7dfb-5c75-4ee6-a9ed-08436af0558b-360.torrent&xt=urn:btih:d121f7493998d4204b3d33d00da7fea1c9a42484&dn=Implications+of+5G+Rollout+Simply+Explained&tr=wss%3A%2F%2Fbittube.video%3A443%2Ftracker%2Fsocket&tr=https%3A%2F%2Fbittube.video%2Ftracker%2Fannounce&ws=https%3A%2F%2Fbittube.video%2Fstatic%2Fwebseed%2F2aad7dfb-5c75-4ee6-a9ed-08436af0558b-360.mp4","height":360}],"likes":"https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b/likes","dislikes":"https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b/dislikes","shares":"https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b/announces","comments":"https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b/comments","attributedTo":[{"type":"Person","id":"https://bittube.video/accounts/hanimated.moh"},{"type":"Group","id":"https://bittube.video/video-channels/hanimated.moh_channel"}],"to":["https://www.w3.org/ns/activitystreams#Public"],"cc":["https://bittube.video/accounts/hanimated.moh/followers"],"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"RsaSignature2017":"https://w3id.org/security#RsaSignature2017","pt":"https://joinpeertube.org/ns#","sc":"http://schema.org#","Hashtag":"as:Hashtag","uuid":"sc:identifier","category":"sc:category","licence":"sc:license","subtitleLanguage":"sc:subtitleLanguage","sensitive":"as:sensitive","language":"sc:inLanguage","expires":"sc:expires","CacheFile":"pt:CacheFile","Infohash":"pt:Infohash","originallyPublishedAt":"sc:datePublished","views":{"@type":"sc:Number","@id":"pt:views"},"state":{"@type":"sc:Number","@id":"pt:state"},"size":{"@type":"sc:Number","@id":"pt:size"},"fps":{"@type":"sc:Number","@id":"pt:fps"},"startTimestamp":{"@type":"sc:Number","@id":"pt:startTimestamp"},"stopTimestamp":{"@type":"sc:Number","@id":"pt:stopTimestamp"},"position":{"@type":"sc:Number","@id":"pt:position"},"commentsEnabled":{"@type":"sc:Boolean","@id":"pt:commentsEnabled"},"downloadEnabled":{"@type":"sc:Boolean","@id":"pt:downloadEnabled"},"waitTranscoding":{"@type":"sc:Boolean","@id":"pt:waitTranscoding"},"support":{"@type":"sc:Text","@id":"pt:support"}},{"likes":{"@id":"as:likes","@type":"@id"},"dislikes":{"@id":"as:dislikes","@type":"@id"},"playlists":{"@id":"pt:playlists","@type":"@id"},"shares":{"@id":"as:shares","@type":"@id"},"comments":{"@id":"as:comments","@type":"@id"}}]} diff --git a/test/fixtures/tesla_mock/hanimated.json b/test/fixtures/tesla_mock/hanimated.json new file mode 100644 index 000000000..564deebd9 --- /dev/null +++ b/test/fixtures/tesla_mock/hanimated.json @@ -0,0 +1 @@ +{"type":"Person","id":"https://bittube.video/accounts/hanimated.moh","following":"https://bittube.video/accounts/hanimated.moh/following","followers":"https://bittube.video/accounts/hanimated.moh/followers","playlists":"https://bittube.video/accounts/hanimated.moh/playlists","inbox":"https://bittube.video/accounts/hanimated.moh/inbox","outbox":"https://bittube.video/accounts/hanimated.moh/outbox","preferredUsername":"hanimated.moh","url":"https://bittube.video/accounts/hanimated.moh","name":"Nosat","endpoints":{"sharedInbox":"https://bittube.video/inbox"},"publicKey":{"id":"https://bittube.video/accounts/hanimated.moh#main-key","owner":"https://bittube.video/accounts/hanimated.moh","publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwuoQT+4uyAboQcf/okCM\nFqUS/LuqFc2888OSKZFAz00Op/dyOB/pkr1+QLxbl8ZGiUWhmnmhNwmmd3tbhSsC\nvLv9Mz/YaWQPYLfRS/s/7iIxdniC4lo/YgicOrzcvetHmk1feOg5vb5/yc+bgUSm\nOk+L4azqXP9GmZyofzvufT65bUmzQRFXP19eL55YZWvZDaC81QAfRXsqtCqbehtF\nQNOjGhnl6a7Kfe8KprRDPV/3WvvFjftnNO2qenIIOFLLeznkQ0ELP6lyb9pvv/1C\n2/GRh2BwmgVlCTw1kTxLSdj80BFX5P8AudSiIx079lVkhamEhzsNLkMpQFqWAAlg\nrQIDAQAB\n-----END PUBLIC KEY-----"},"icon":{"type":"Image","mediaType":"image/jpeg","url":"https://bittube.video/lazy-static/avatars/84b8acc3-e48b-4642-a9f4-360a4499579b.jpg"},"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"RsaSignature2017":"https://w3id.org/security#RsaSignature2017","pt":"https://joinpeertube.org/ns#","sc":"http://schema.org#","Hashtag":"as:Hashtag","uuid":"sc:identifier","category":"sc:category","licence":"sc:license","subtitleLanguage":"sc:subtitleLanguage","sensitive":"as:sensitive","language":"sc:inLanguage","expires":"sc:expires","CacheFile":"pt:CacheFile","Infohash":"pt:Infohash","originallyPublishedAt":"sc:datePublished","views":{"@type":"sc:Number","@id":"pt:views"},"state":{"@type":"sc:Number","@id":"pt:state"},"size":{"@type":"sc:Number","@id":"pt:size"},"fps":{"@type":"sc:Number","@id":"pt:fps"},"startTimestamp":{"@type":"sc:Number","@id":"pt:startTimestamp"},"stopTimestamp":{"@type":"sc:Number","@id":"pt:stopTimestamp"},"position":{"@type":"sc:Number","@id":"pt:position"},"commentsEnabled":{"@type":"sc:Boolean","@id":"pt:commentsEnabled"},"downloadEnabled":{"@type":"sc:Boolean","@id":"pt:downloadEnabled"},"waitTranscoding":{"@type":"sc:Boolean","@id":"pt:waitTranscoding"},"support":{"@type":"sc:Text","@id":"pt:support"}},{"likes":{"@id":"as:likes","@type":"@id"},"dislikes":{"@id":"as:dislikes","@type":"@id"},"playlists":{"@id":"pt:playlists","@type":"@id"},"shares":{"@id":"as:shares","@type":"@id"},"comments":{"@id":"as:comments","@type":"@id"}}],"summary":null} diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex index 20cb2b3d1..54dde0432 100644 --- a/test/support/http_request_mock.ex +++ b/test/support/http_request_mock.ex @@ -308,6 +308,22 @@ def get("https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3" }} end + def get("https://bittube.video/accounts/hanimated.moh", _, _, _) do + {:ok, + %Tesla.Env{ + status: 200, + body: File.read!("test/fixtures/tesla_mock/hanimated.json") + }} + end + + def get("https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b", _, _, _) do + {:ok, + %Tesla.Env{ + status: 200, + body: File.read!("test/fixtures/tesla_mock/bittube-video.json") + }} + end + def get("https://mobilizon.org/events/252d5816-00a3-4a89-a66f-15bf65c33e39", _, _, [ {"accept", "application/activity+json"} ]) do diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 2332029e5..de9663fa9 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -1221,6 +1221,15 @@ test "it rejects activities without a valid ID" do :error = Transmogrifier.handle_incoming(data) end + test "it remaps mediaType of object" do + {:ok, object} = + Fetcher.fetch_object_from_id( + "https://bittube.video/videos/watch/2aad7dfb-5c75-4ee6-a9ed-08436af0558b" + ) + + assert object.data["mediaType"] == "text/html" + end + test "it remaps video URLs as attachments if necessary" do {:ok, object} = Fetcher.fetch_object_from_id(