activitypub transmogrifier: cleanups and tests for incoming accepts/rejects
authorWilliam Pitcock <nenolod@dereferenced.org>
Fri, 25 May 2018 12:51:04 +0000 (12:51 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Fri, 25 May 2018 12:53:12 +0000 (12:53 +0000)
lib/pleroma/web/activity_pub/transmogrifier.ex
test/fixtures/mastodon-reject-activity.json [new file with mode: 0644]
test/web/activity_pub/transmogrifier_test.exs

index 519548788feada97bfc8f789471db295462a91ef..41198d4e60cfdd57b95c0b0991d4eab502c32c94 100644 (file)
@@ -152,10 +152,10 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
         {:ok, follow_object}
 
       is_binary(follow_object) ->
-        object = get_obj_helper(follow_object) || ActivityPub.fetch_object_from_id(follow_object)
+        object = Activity.get_by_ap_id(follow_object)
 
         if object do
-          {:ok, object}
+          {:ok, object.data}
         else
           {:error, nil}
         end
@@ -170,12 +170,13 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       ) do
     with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
          {:ok, follow_activity} <- get_follow_activity(follow_object),
-         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]) do
+         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]),
+         {:ok, activity} <- ActivityPub.insert(data, true) do
       if not User.following?(follower, followed) do
-        User.follow(follower, followed)
+        {:ok, follower} = User.follow(follower, followed)
       end
 
-      {:ok, data}
+      {:ok, activity}
     end
   end
 
@@ -184,10 +185,11 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       ) do
     with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
          {:ok, follow_activity} <- get_follow_activity(follow_object),
-         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]) do
+         %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity["actor"]),
+         {:ok, activity} <- ActivityPub.insert(data, true) do
       User.unfollow(follower, followed)
 
-      {:ok, data}
+      {:ok, activity}
     end
   end
 
diff --git a/test/fixtures/mastodon-reject-activity.json b/test/fixtures/mastodon-reject-activity.json
new file mode 100644 (file)
index 0000000..9559d6c
--- /dev/null
@@ -0,0 +1,34 @@
+{
+  "type": "Reject",
+  "signature": {
+    "type": "RsaSignature2017",
+    "signatureValue": "rBzK4Kqhd4g7HDS8WE5oRbWQb2R+HF/6awbUuMWhgru/xCODT0SJWSri0qWqEO4fPcpoUyz2d25cw6o+iy9wiozQb3hQNnu69AR+H5Mytc06+g10KCHexbGhbAEAw/7IzmeXELHUbaqeduaDIbdt1zw4RkwLXdqgQcGXTJ6ND1wM3WMHXQCK1m0flasIXFoBxpliPAGiElV8s0+Ltuh562GvflG3kB3WO+j+NaR0ZfG5G9N88xMj9UQlCKit5gpAE5p6syUsCU2WGBHywTumv73i3OVTIFfq+P9AdMsRuzw1r7zoKEsthW4aOzLQDi01ZjvdBz8zH6JnjDU7SMN/Ig==",
+    "creator": "http://mastodon.example.org/users/admin#main-key",
+    "created": "2018-02-17T14:36:41Z"
+  },
+  "object": {
+    "type": "Follow",
+    "object": "http://mastodon.example.org/users/admin",
+    "id": "http://localtesting.pleroma.lol/users/lain#follows/4",
+    "actor": "http://localtesting.pleroma.lol/users/lain"
+  },
+  "nickname": "lain",
+  "id": "http://mastodon.example.org/users/admin#rejects/follows/4",
+  "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"
+    }
+  ]
+}
index cf6b1d0b59b642ecf64187c0ce9cedd8b56281a3..b51e02b08ba18c285db31d29ec8e1e3f71342bf8 100644 (file)
@@ -2,6 +2,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
   use Pleroma.DataCase
   alias Pleroma.Web.ActivityPub.Transmogrifier
   alias Pleroma.Web.ActivityPub.Utils
+  alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.OStatus
   alias Pleroma.Activity
   alias Pleroma.User
@@ -385,6 +386,114 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
 
       refute User.blocks?(blocker, user)
     end
+
+    test "it works for incoming accepts which were pre-accepted" do
+      follower = insert(:user)
+      followed = insert(:user)
+
+      {:ok, follower} = User.follow(follower, followed)
+      assert User.following?(follower, followed) == true
+
+      accept_data =
+        File.read!("test/fixtures/mastodon-accept-activity.json")
+        |> Poison.decode!()
+        |> Map.put("actor", followed.ap_id)
+
+      accept_data =
+        Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
+
+      {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(accept_data)
+
+      follower = Repo.get(User, follower.id)
+
+      assert User.following?(follower, followed) == true
+    end
+
+    test "it works for incoming accepts which were orphaned" do
+      follower = insert(:user)
+      followed = insert(:user, %{info: %{"locked" => true}})
+
+      {:ok, follow_activity} = ActivityPub.follow(follower, followed)
+
+      accept_data =
+        File.read!("test/fixtures/mastodon-accept-activity.json")
+        |> Poison.decode!()
+        |> Map.put("actor", followed.ap_id)
+
+      accept_data =
+        Map.put(accept_data, "object", Map.put(accept_data["object"], "actor", follower.ap_id))
+
+      {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(accept_data)
+
+      follower = Repo.get(User, follower.id)
+
+      assert User.following?(follower, followed) == true
+    end
+
+    test "it works for incoming accepts which are referenced by IRI only" do
+      follower = insert(:user)
+      followed = insert(:user, %{info: %{"locked" => true}})
+
+      {:ok, follow_activity} = ActivityPub.follow(follower, followed)
+
+      accept_data =
+        File.read!("test/fixtures/mastodon-accept-activity.json")
+        |> Poison.decode!()
+        |> Map.put("actor", followed.ap_id)
+        |> Map.put("object", follow_activity.data["id"])
+
+      {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(accept_data)
+
+      follower = Repo.get(User, follower.id)
+
+      assert User.following?(follower, followed) == true
+    end
+
+    test "it works for incoming rejects which are orphaned" do
+      follower = insert(:user)
+      followed = insert(:user, %{info: %{"locked" => true}})
+
+      {:ok, follower} = User.follow(follower, followed)
+      {:ok, follow_activity} = ActivityPub.follow(follower, followed)
+
+      assert User.following?(follower, followed) == true
+
+      reject_data =
+        File.read!("test/fixtures/mastodon-reject-activity.json")
+        |> Poison.decode!()
+        |> Map.put("actor", followed.ap_id)
+
+      reject_data =
+        Map.put(reject_data, "object", Map.put(reject_data["object"], "actor", follower.ap_id))
+
+      {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
+
+      follower = Repo.get(User, follower.id)
+
+      assert User.following?(follower, followed) == false
+    end
+
+    test "it works for incoming rejects which are referenced by IRI only" do
+      follower = insert(:user)
+      followed = insert(:user, %{info: %{"locked" => true}})
+
+      {:ok, follower} = User.follow(follower, followed)
+      {:ok, follow_activity} = ActivityPub.follow(follower, followed)
+
+      assert User.following?(follower, followed) == true
+
+      reject_data =
+        File.read!("test/fixtures/mastodon-reject-activity.json")
+        |> Poison.decode!()
+        |> Map.put("actor", followed.ap_id)
+        |> Map.put("object", follow_activity.data["id"])
+
+      {:ok, %Activity{data: _}} = Transmogrifier.handle_incoming(reject_data)
+
+      follower = Repo.get(User, follower.id)
+
+      assert User.following?(follower, followed) == false
+    end
   end
 
   describe "prepare outgoing" do