Merge remote-tracking branch 'origin/develop' into conversations_three
[akkoma] / test / web / activity_pub / activity_pub_test.exs
index 46b4cf7b6d18d232bbf73eb7a5591f452080ae0a..15276ba7b193f0c02010b52f76c3842d06ba16e0 100644 (file)
@@ -84,17 +84,21 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
       {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
 
-      fetch_one = ActivityPub.fetch_activities([], %{"tag" => "test"})
-      fetch_two = ActivityPub.fetch_activities([], %{"tag" => ["test", "essais"]})
+      fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
+
+      fetch_two =
+        ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
 
       fetch_three =
         ActivityPub.fetch_activities([], %{
+          "type" => "Create",
           "tag" => ["test", "essais"],
           "tag_reject" => ["reject"]
         })
 
       fetch_four =
         ActivityPub.fetch_activities([], %{
+          "type" => "Create",
           "tag" => ["test"],
           "tag_all" => ["test", "reject"]
         })
@@ -126,9 +130,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
 
     test "doesn't drop activities with content being null" do
+      user = insert(:user)
+
       data = %{
-        "ok" => true,
+        "actor" => user.ap_id,
+        "to" => [],
         "object" => %{
+          "actor" => user.ap_id,
+          "to" => [],
+          "type" => "Note",
           "content" => nil
         }
       }
@@ -144,8 +154,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
 
     test "inserts a given map into the activity database, giving it an id if it has none." do
+      user = insert(:user)
+
       data = %{
-        "ok" => true
+        "actor" => user.ap_id,
+        "to" => [],
+        "object" => %{
+          "actor" => user.ap_id,
+          "to" => [],
+          "type" => "Note",
+          "content" => "hey"
+        }
       }
 
       {:ok, %Activity{} = activity} = ActivityPub.insert(data)
@@ -155,9 +174,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       given_id = "bla"
 
       data = %{
-        "ok" => true,
         "id" => given_id,
-        "context" => "blabla"
+        "actor" => user.ap_id,
+        "to" => [],
+        "context" => "blabla",
+        "object" => %{
+          "actor" => user.ap_id,
+          "to" => [],
+          "type" => "Note",
+          "content" => "hey"
+        }
       }
 
       {:ok, %Activity{} = activity} = ActivityPub.insert(data)
@@ -168,10 +194,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
 
     test "adds a context when none is there" do
+      user = insert(:user)
+
       data = %{
-        "id" => "some_id",
+        "actor" => user.ap_id,
+        "to" => [],
         "object" => %{
-          "id" => "object_id"
+          "actor" => user.ap_id,
+          "to" => [],
+          "type" => "Note",
+          "content" => "hey"
         }
       }
 
@@ -184,16 +216,24 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
 
     test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
+      user = insert(:user)
+
       data = %{
+        "actor" => user.ap_id,
+        "to" => [],
         "object" => %{
+          "actor" => user.ap_id,
+          "to" => [],
           "type" => "Note",
-          "ok" => true
+          "content" => "hey"
         }
       }
 
       {:ok, %Activity{} = activity} = ActivityPub.insert(data)
-      assert is_binary(activity.data["object"]["id"])
-      assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"])
+      object = Object.normalize(activity.data["object"])
+
+      assert is_binary(object.data["id"])
+      assert %Object{} = Object.get_by_ap_id(activity.data["object"])
     end
   end
 
@@ -206,7 +246,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
           to: ["user1", "user1", "user2"],
           actor: user,
           context: "",
-          object: %{}
+          object: %{
+            "to" => ["user1", "user1", "user2"],
+            "type" => "Note",
+            "content" => "testing"
+          }
         })
 
       assert activity.data["to"] == ["user1", "user2"]
@@ -218,18 +262,30 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       user = insert(:user)
 
       {:ok, _} =
-        CommonAPI.post(User.get_by_id(user.id), %{"status" => "1", "visibility" => "public"})
+        CommonAPI.post(User.get_cached_by_id(user.id), %{
+          "status" => "1",
+          "visibility" => "public"
+        })
 
       {:ok, _} =
-        CommonAPI.post(User.get_by_id(user.id), %{"status" => "2", "visibility" => "unlisted"})
+        CommonAPI.post(User.get_cached_by_id(user.id), %{
+          "status" => "2",
+          "visibility" => "unlisted"
+        })
 
       {:ok, _} =
-        CommonAPI.post(User.get_by_id(user.id), %{"status" => "2", "visibility" => "private"})
+        CommonAPI.post(User.get_cached_by_id(user.id), %{
+          "status" => "2",
+          "visibility" => "private"
+        })
 
       {:ok, _} =
-        CommonAPI.post(User.get_by_id(user.id), %{"status" => "3", "visibility" => "direct"})
+        CommonAPI.post(User.get_cached_by_id(user.id), %{
+          "status" => "3",
+          "visibility" => "direct"
+        })
 
-      user = User.get_by_id(user.id)
+      user = User.get_cached_by_id(user.id)
       assert user.info.note_count == 2
     end
 
@@ -244,25 +300,21 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       # public
       {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
       assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
-      assert data["object"]["repliesCount"] == 1
       assert object.data["repliesCount"] == 1
 
       # unlisted
       {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
       assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
-      assert data["object"]["repliesCount"] == 2
       assert object.data["repliesCount"] == 2
 
       # private
       {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
       assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
-      assert data["object"]["repliesCount"] == 2
       assert object.data["repliesCount"] == 2
 
       # direct
       {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
       assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
-      assert data["object"]["repliesCount"] == 2
       assert object.data["repliesCount"] == 2
     end
   end
@@ -341,6 +393,51 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     assert Enum.member?(activities, activity_one)
   end
 
+  test "doesn't return transitive interactions concerning blocked users" do
+    blocker = insert(:user)
+    blockee = insert(:user)
+    friend = insert(:user)
+
+    {:ok, blocker} = User.block(blocker, blockee)
+
+    {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
+
+    {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
+
+    {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
+
+    {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
+
+    activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
+
+    assert Enum.member?(activities, activity_one)
+    refute Enum.member?(activities, activity_two)
+    refute Enum.member?(activities, activity_three)
+    refute Enum.member?(activities, activity_four)
+  end
+
+  test "doesn't return announce activities concerning blocked users" do
+    blocker = insert(:user)
+    blockee = insert(:user)
+    friend = insert(:user)
+
+    {:ok, blocker} = User.block(blocker, blockee)
+
+    {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
+
+    {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
+
+    {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
+
+    activities =
+      ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
+      |> Enum.map(fn act -> act.id end)
+
+    assert Enum.member?(activities, activity_one.id)
+    refute Enum.member?(activities, activity_two.id)
+    refute Enum.member?(activities, activity_three.id)
+  end
+
   test "doesn't return muted activities" do
     activity_one = insert(:note_activity)
     activity_two = insert(:note_activity)
@@ -638,50 +735,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
   describe "fetch the latest Follow" do
     test "fetches the latest Follow activity" do
       %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
-      follower = User.get_by_ap_id(activity.data["actor"])
-      followed = User.get_by_ap_id(activity.data["object"])
+      follower = Repo.get_by(User, ap_id: activity.data["actor"])
+      followed = Repo.get_by(User, ap_id: activity.data["object"])
 
       assert activity == Utils.fetch_latest_follow(follower, followed)
     end
   end
 
-  describe "fetching an object" do
-    test "it fetches an object" do
-      {:ok, object} =
-        ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
-
-      assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
-      assert activity.data["id"]
-
-      {:ok, object_again} =
-        ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
-
-      assert [attachment] = object.data["attachment"]
-      assert is_list(attachment["url"])
-
-      assert object == object_again
-    end
-
-    test "it works with objects only available via Ostatus" do
-      {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
-      assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
-      assert activity.data["id"]
-
-      {:ok, object_again} =
-        ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
-
-      assert object == object_again
-    end
-
-    test "it correctly stitches up conversations between ostatus and ap" do
-      last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
-      {:ok, object} = ActivityPub.fetch_object_from_id(last)
-
-      object = Object.get_by_ap_id(object.data["inReplyTo"])
-      assert object
-    end
-  end
-
   describe "following / unfollowing" do
     test "creates a follow activity" do
       follower = insert(:user)
@@ -758,23 +818,35 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       user = insert(:user, info: %{note_count: 10})
 
       {:ok, a1} =
-        CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "public"})
+        CommonAPI.post(User.get_cached_by_id(user.id), %{
+          "status" => "yeah",
+          "visibility" => "public"
+        })
 
       {:ok, a2} =
-        CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "unlisted"})
+        CommonAPI.post(User.get_cached_by_id(user.id), %{
+          "status" => "yeah",
+          "visibility" => "unlisted"
+        })
 
       {:ok, a3} =
-        CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "private"})
+        CommonAPI.post(User.get_cached_by_id(user.id), %{
+          "status" => "yeah",
+          "visibility" => "private"
+        })
 
       {:ok, a4} =
-        CommonAPI.post(User.get_by_id(user.id), %{"status" => "yeah", "visibility" => "direct"})
+        CommonAPI.post(User.get_cached_by_id(user.id), %{
+          "status" => "yeah",
+          "visibility" => "direct"
+        })
 
-      {:ok, _} = a1.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
-      {:ok, _} = a2.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
-      {:ok, _} = a3.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
-      {:ok, _} = a4.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
+      {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
+      {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
+      {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
+      {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
 
-      user = User.get_by_id(user.id)
+      user = User.get_cached_by_id(user.id)
       assert user.info.note_count == 10
     end
 
@@ -814,22 +886,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
 
       _ = CommonAPI.delete(direct_reply.id, user2)
       assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
-      assert data["object"]["repliesCount"] == 2
       assert object.data["repliesCount"] == 2
 
       _ = CommonAPI.delete(private_reply.id, user2)
       assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
-      assert data["object"]["repliesCount"] == 2
       assert object.data["repliesCount"] == 2
 
       _ = CommonAPI.delete(public_reply.id, user2)
       assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
-      assert data["object"]["repliesCount"] == 1
       assert object.data["repliesCount"] == 1
 
       _ = CommonAPI.delete(unlisted_reply.id, user2)
       assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
-      assert data["object"]["repliesCount"] == 0
       assert object.data["repliesCount"] == 0
     end
   end
@@ -871,7 +939,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
 
       private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
-      assert [public_activity, private_activity_1, private_activity_3] == activities
+
+      assert [public_activity, private_activity_1, private_activity_3] ==
+               activities
+
       assert length(activities) == 3
 
       activities = ActivityPub.contain_timeline(activities, user1)
@@ -881,15 +952,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
-  test "it can fetch plume articles" do
-    {:ok, object} =
-      ActivityPub.fetch_object_from_id(
-        "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
-      )
-
-    assert object
-  end
-
   describe "update" do
     test "it creates an update activity with the new user data" do
       user = insert(:user)
@@ -911,15 +973,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     end
   end
 
-  test "it can fetch peertube videos" do
-    {:ok, object} =
-      ActivityPub.fetch_object_from_id(
-        "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
-      )
-
-    assert object
-  end
-
   test "returned pinned statuses" do
     Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
     user = insert(:user)