ActivityPub: Handle incoming likes.
authorlain <lain@soykaf.club>
Sat, 17 Feb 2018 19:13:12 +0000 (20:13 +0100)
committerlain <lain@soykaf.club>
Sat, 17 Feb 2018 19:13:12 +0000 (20:13 +0100)
lib/pleroma/web/activity_pub/transmogrifier.ex
test/fixtures/mastodon-like.json [new file with mode: 0644]
test/web/activity_pub/transmogrifier_test.exs

index 6cf7cfe3565902a10004d456489a0e49cc3128fd..82fcf0898af660e64b8fa75e36dea14b98bc14c4 100644 (file)
@@ -3,6 +3,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   A module to handle coding from internal to wire ActivityPub and back.
   """
   alias Pleroma.User
+  alias Pleroma.Object
   alias Pleroma.Web.ActivityPub.ActivityPub
 
   @doc """
@@ -62,6 +63,20 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     end
   end
 
+  def handle_incoming(%{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data) do
+    with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
+         %Object{} = object <- Object.get_by_ap_id(object_id),
+         {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do
+      {:ok, activity}
+    else
+      _e -> :error
+    end
+  end
+
+  # TODO
+  # Accept
+  # Undo
+
   def handle_incoming(_), do: :error
 
   @doc
diff --git a/test/fixtures/mastodon-like.json b/test/fixtures/mastodon-like.json
new file mode 100644 (file)
index 0000000..39fb44c
--- /dev/null
@@ -0,0 +1,29 @@
+{
+  "type": "Like",
+  "signature": {
+    "type": "RsaSignature2017",
+    "signatureValue": "fdxMfQSMwbC6wP6sh6neS/vM5879K67yQkHTbiT5Npr5wAac0y6+o3Ij+41tN3rL6wfuGTosSBTHOtta6R4GCOOhCaCSLMZKypnp1VltCzLDoyrZELnYQIC8gpUXVmIycZbREk22qWUe/w7DAFaKK4UscBlHDzeDVcA0K3Se5Sluqi9/Zh+ldAnEzj/rSEPDjrtvf5wGNf3fHxbKSRKFt90JvKK6hS+vxKUhlRFDf6/SMETw+EhwJSNW4d10yMUakqUWsFv4Acq5LW7l+HpYMvlYY1FZhNde1+uonnCyuQDyvzkff8zwtEJmAXC4RivO/VVLa17SmqheJZfI8oluVg==",
+    "creator": "http://mastodon.example.org/users/admin#main-key",
+    "created": "2018-02-17T18:57:49Z"
+  },
+  "object": "http://localtesting.pleroma.lol/objects/eb92579d-3417-42a8-8652-2492c2d4f454",
+  "nickname": "lain",
+  "id": "http://mastodon.example.org/users/admin#likes/2",
+  "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
index 57598d020f8e2d653155deafdb92f96e80f6fc66..2e7586227f1cec73af2909b97e965af3e4cedc2d 100644 (file)
@@ -3,6 +3,9 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
   alias Pleroma.Web.ActivityPub.Transmogrifier
   alias Pleroma.Activity
   alias Pleroma.User
+  alias Pleroma.Repo
+  import Ecto.Query
+
   import Pleroma.Factory
   alias Pleroma.Web.CommonAPI
 
@@ -43,7 +46,21 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       assert data["type"] == "Follow"
       assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
       assert User.following?(User.get_by_ap_id(data["actor"]), user)
+    end
+
+    test "it works for incoming likes" do
+      user = insert(:user)
+      {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
 
+      data = File.read!("test/fixtures/mastodon-like.json") |> Poison.decode!
+      |> Map.put("object", activity.data["object"]["id"])
+
+      {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
+
+      assert data["actor"] == "http://mastodon.example.org/users/admin"
+      assert data["type"] == "Like"
+      assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
+      assert data["object"] == activity.data["object"]["id"]
     end
   end