Hook up unfollow and (un)block to MastoAPI + tests
authorFrancis Dinh <normandy@firemail.cc>
Tue, 22 May 2018 08:27:40 +0000 (04:27 -0400)
committerFrancis Dinh <normandy@firemail.cc>
Tue, 22 May 2018 08:27:40 +0000 (04:27 -0400)
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
test/web/activity_pub/activity_pub_test.exs

index 85f9c5b7bcdeb6d9cf1a649ebccf764851a040de..b21f5de208c08f2acfa004fb8a01b099c794f626 100644 (file)
@@ -457,24 +457,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     end
   end
 
-  # TODO: Clean up and unify
   def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
     with %User{} = followed <- Repo.get(User, id),
-         {:ok, follower, follow_activity} <- User.unfollow(follower, followed),
-         {:ok, _activity} <-
-           ActivityPub.insert(%{
-             "type" => "Undo",
-             "actor" => follower.ap_id,
-             # get latest Follow for these users
-             "object" => follow_activity.data["id"]
-           }) do
+         {:ok, _activity} <- ActivityPub.unfollow(follower, followed),
+         {:ok, follower, _} <- User.unfollow(follower, followed) do
       render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
     end
   end
 
   def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
     with %User{} = blocked <- Repo.get(User, id),
-         {:ok, blocker} <- User.block(blocker, blocked) do
+         {:ok, blocker} <- User.block(blocker, blocked),
+         {:ok, _activity} <- ActivityPub.block(blocker, blocked) do
       render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
     else
       {:error, message} ->
@@ -486,7 +480,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
 
   def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
     with %User{} = blocked <- Repo.get(User, id),
-         {:ok, blocker} <- User.unblock(blocker, blocked) do
+         {:ok, blocker} <- User.unblock(blocker, blocked),
+         {:ok, _activity} <- ActivityPub.unblock(blocker, blocked) do
       render(conn, AccountView, "relationship.json", %{user: blocker, target: blocked})
     else
       {:error, message} ->
index 35f0deffebd65cb3cfec8b87bf3940e478396544..081c202b14d5616f8102f9fc3ea4d995e7e38585 100644 (file)
@@ -425,10 +425,43 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
 
       assert activity.data["type"] == "Undo"
       assert activity.data["actor"] == follower.ap_id
+
+      assert is_map(activity.data["object"])
+      assert activity.data["object"]["type"] == "Follow"
+      assert activity.data["object"]["object"] == followed.ap_id
       assert activity.data["object"]["id"] == follow_activity.data["id"]
     end
   end
 
+  describe "blocking / unblocking" do
+    test "creates a block activity" do
+      blocker = insert(:user)
+      blocked = insert(:user)
+
+      {:ok, activity} = ActivityPub.block(blocker, blocked)
+
+      assert activity.data["type"] == "Block"
+      assert activity.data["actor"] == blocker.ap_id
+      assert activity.data["object"] == blocked.ap_id
+    end
+
+    test "creates an undo activity for the last block" do
+      blocker = insert(:user)
+      blocked = insert(:user)
+
+      {:ok, block_activity} = ActivityPub.block(blocker, blocked)
+      {:ok, activity} = ActivityPub.unblock(blocker, blocked)
+
+      assert activity.data["type"] == "Undo"
+      assert activity.data["actor"] == blocker.ap_id
+
+      assert is_map(activity.data["object"])
+      assert activity.data["object"]["type"] == "Block"
+      assert activity.data["object"]["object"] == blocked.ap_id
+      assert activity.data["object"]["id"] == block_activity.data["id"]
+    end
+  end
+
   describe "deletion" do
     test "it creates a delete activity and deletes the original object" do
       note = insert(:note_activity)