ActivityPub: Handle attachments.
authorlain <lain@soykaf.club>
Sat, 17 Feb 2018 17:38:58 +0000 (18:38 +0100)
committerlain <lain@soykaf.club>
Sat, 17 Feb 2018 17:38:58 +0000 (18:38 +0100)
lib/pleroma/web/activity_pub/transmogrifier.ex
test/fixtures/mastodon-create-with-attachment.json [new file with mode: 0644]

index 76f04e8a3f7b2d61f00be0df2d81e676dfd33ea4..6cf7cfe3565902a10004d456489a0e49cc3128fd 100644 (file)
@@ -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 (file)
index 0000000..fb270b1
--- /dev/null
@@ -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": "<p><a href=\"http://mastodon.example.org/media/hw4nrZmV5DPbW2z_hao\" rel=\"nofollow noopener\" target=\"_blank\"><span class=\"invisible\">http://</span><span class=\"ellipsis\">mastodon.example.org/media/hw4</span><span class=\"invisible\">nrZmV5DPbW2z_hao</span></a></p>",
+    "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