From ab27c90c9fccd20cacad86a5c5f9a48314884d53 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 17 Feb 2018 18:38:58 +0100 Subject: [PATCH] ActivityPub: Handle attachments. --- .../web/activity_pub/transmogrifier.ex | 24 +++++++ .../mastodon-create-with-attachment.json | 63 +++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 test/fixtures/mastodon-create-with-attachment.json diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 76f04e8a3..6cf7cfe35 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -11,6 +11,18 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do def fix_object(object) do object |> Map.put("actor", object["attributedTo"]) + |> fix_attachments + end + + def fix_attachments(object) do + attachments = object["attachment"] || [] + |> Enum.map(fn (data) -> + url = [%{"type" => "Link", "mediaType" => data["mediaType"], "url" => data["url"]}] + Map.put(data, "url", url) + end) + + object + |> Map.put("attachment", attachments) end # TODO: validate those with a Ecto scheme @@ -60,6 +72,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do object = object |> add_mention_tags |> add_attributed_to + |> prepare_attachments data = data |> Map.put("object", object) @@ -93,4 +106,15 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do object |> Map.put("attributedTo", attributedTo) end + + def prepare_attachments(object) do + attachments = (object["attachment"] || []) + |> Enum.map(fn (data) -> + [%{"mediaType" => media_type, "href" => href} | _] = data["url"] + %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"} + end) + + object + |> Map.put("attachment", attachments) + end end diff --git a/test/fixtures/mastodon-create-with-attachment.json b/test/fixtures/mastodon-create-with-attachment.json new file mode 100644 index 000000000..fb270b13f --- /dev/null +++ b/test/fixtures/mastodon-create-with-attachment.json @@ -0,0 +1,63 @@ +{ + "type": "Create", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "signature": { + "type": "RsaSignature2017", + "signatureValue": "KnaBoP7C4XYgzTFbM+CpGlx4p59ahWvNNo4reRGDlb/DmxL3OF1/WugNl0xHCOA3aoIX2rrkHniw+z4Yb+wOBf9ZOxgM+IHTKj69AEcm/4NxGXxStRv603JZNyboY371w8g/mIKmLLtL6dgUI3n2Laam2rYh//8aelEWQ240TxiJi/WcKuOT2DNInWOpfArgxJ4MA11n4tb4xX65RkxInTCFa1kaJG8L+A+EoXtIhTa4rCQDv/BH3a8x7vOJxHfEosEnkk/yVEqG+ccgoTvc+5/kK+TKk3S3GuXch0ro9RKqxfPAHkyg8eetRhNhKWZ/rgPNfcF6bGJKFA0i8TzjHw==", + "creator": "http://mastodon.example.org/users/admin#main-key", + "created": "2018-02-17T17:14:26Z" + }, + "published": "2018-02-17T17:14:26Z", + "object": { + "url": "http://mastodon.example.org/@admin/99541822081679796", + "type": "Note", + "to": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "tag": [], + "summary": null, + "sensitive": false, + "published": "2018-02-17T17:14:26Z", + "inReplyToAtomUri": null, + "inReplyTo": null, + "id": "http://mastodon.example.org/users/admin/statuses/99541822081679796", + "conversation": "tag:mastodon.example.org,2018-02-17:objectId=10:objectType=Conversation", + "content": "

http://mastodon.example.org/media/hw4nrZmV5DPbW2z_hao

", + "cc": [ + "http://mastodon.example.org/users/admin/followers" + ], + "attributedTo": "http://mastodon.example.org/users/admin", + "attachment": [ + { + "url": "http://mastodon.example.org/system/media_attachments/files/000/000/002/original/334ce029e7bfb920.jpg", + "type": "Document", + "name": null, + "mediaType": "image/jpeg" + } + ], + "atomUri": "http://mastodon.example.org/users/admin/statuses/99541822081679796" + }, + "id": "http://mastodon.example.org/users/admin/statuses/99541822081679796/activity", + "cc": [ + "http://mastodon.example.org/users/admin/followers" + ], + "actor": "http://mastodon.example.org/users/admin", + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "toot": "http://joinmastodon.org/ns#", + "sensitive": "as:sensitive", + "ostatus": "http://ostatus.org#", + "movedTo": "as:movedTo", + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "atomUri": "ostatus:atomUri", + "Hashtag": "as:Hashtag", + "Emoji": "toot:Emoji" + } + ] +} \ No newline at end of file