From: Francis Dinh <normandy@firemail.cc>
Date: Tue, 22 May 2018 08:27:40 +0000 (-0400)
Subject: Hook up unfollow and (un)block to MastoAPI + tests
X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=b2c6ae7d820fb9d6bf81c6912e3e9b10a6fa7dd2;p=akkoma

Hook up unfollow and (un)block to MastoAPI + tests
---

diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index 85f9c5b7b..b21f5de20 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -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} ->
diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs
index 35f0deffe..081c202b1 100644
--- a/test/web/activity_pub/activity_pub_test.exs
+++ b/test/web/activity_pub/activity_pub_test.exs
@@ -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)