Cancellation of a follow request for a remote user
authorEgor Kislitsyn <egor@kislitsyn.com>
Thu, 6 Feb 2020 12:47:15 +0000 (16:47 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Thu, 6 Feb 2020 14:02:33 +0000 (18:02 +0400)
lib/pleroma/following_relationship.ex
test/web/common_api/common_api_test.exs

index 0b0219b82c3b240aa77a486e748eb943a5ba7df4..cc381af53f1e47986d70bdfbe0b272044829edb4 100644 (file)
@@ -30,9 +30,24 @@ defmodule Pleroma.FollowingRelationship do
   end
 
   def get(%User{} = follower, %User{} = following) do
-    __MODULE__
-    |> where(follower_id: ^follower.id, following_id: ^following.id)
-    |> Repo.one()
+    following_relationship =
+      __MODULE__
+      |> where(follower_id: ^follower.id, following_id: ^following.id)
+      |> Repo.one()
+
+    case {following_relationship, following.local} do
+      {nil, false} ->
+        case Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, following) do
+          %{data: %{"state" => state}} when state in ["pending", "accept"] ->
+            %{state: state}
+
+          _ ->
+            nil
+        end
+
+      {following_relationship, _} ->
+        following_relationship
+    end
   end
 
   def update(follower, following, "reject"), do: unfollow(follower, following)
@@ -58,8 +73,8 @@ defmodule Pleroma.FollowingRelationship do
 
   def unfollow(%User{} = follower, %User{} = following) do
     case get(follower, following) do
-      nil -> {:ok, nil}
       %__MODULE__{} = following_relationship -> Repo.delete(following_relationship)
+      _ -> {:ok, nil}
     end
   end
 
index 2bbe6c923c629adf825e8fc7050bef8e5a4c5071..7eff24ce4e6b834705cfd17bc11c37e1140b9a0b 100644 (file)
@@ -537,7 +537,7 @@ defmodule Pleroma.Web.CommonAPITest do
       refute User.subscribed_to?(follower, followed)
     end
 
-    test "cancels a pending follow" do
+    test "cancels a pending follow for a local user" do
       follower = insert(:user)
       followed = insert(:user, locked: true)
 
@@ -560,6 +560,30 @@ defmodule Pleroma.Web.CommonAPITest do
                }
              } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
     end
+
+    test "cancels a pending follow for a remote user" do
+      follower = insert(:user)
+      followed = insert(:user, locked: true, local: false, ap_enabled: true)
+
+      assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
+               CommonAPI.follow(follower, followed)
+
+      assert %{state: "pending"} = Pleroma.FollowingRelationship.get(follower, followed)
+
+      assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
+
+      assert Pleroma.FollowingRelationship.get(follower, followed) == nil
+
+      assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
+               Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
+
+      assert %{
+               data: %{
+                 "type" => "Undo",
+                 "object" => %{"type" => "Follow", "state" => "cancelled"}
+               }
+             } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower)
+    end
   end
 
   describe "accept_follow_request/2" do