Add User.get_follow_state/2
authorEgor Kislitsyn <egor@kislitsyn.com>
Fri, 7 Feb 2020 12:17:34 +0000 (16:17 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Fri, 7 Feb 2020 12:17:34 +0000 (16:17 +0400)
lib/pleroma/following_relationship.ex
lib/pleroma/user.ex
test/web/common_api/common_api_test.exs

index cc381af53f1e47986d70bdfbe0b272044829edb4..b8cb3bf0372a3960fcbbc484e77956553fcd86ca 100644 (file)
@@ -30,24 +30,9 @@ defmodule Pleroma.FollowingRelationship do
   end
 
   def get(%User{} = follower, %User{} = following) do
-    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
+    __MODULE__
+    |> where(follower_id: ^follower.id, following_id: ^following.id)
+    |> Repo.one()
   end
 
   def update(follower, following, "reject"), do: unfollow(follower, following)
index 398c91cf39451cda733e0ada761f1ff96c61a8de..5ea36fea32ed1a770307db6814a8681c38df8d23 100644 (file)
@@ -652,8 +652,8 @@ defmodule Pleroma.User do
   end
 
   def unfollow(%User{} = follower, %User{} = followed) do
-    case FollowingRelationship.get(follower, followed) do
-      %{state: state} when state in ["accept", "pending"] ->
+    case get_follow_state(follower, followed) do
+      state when state in ["accept", "pending"] ->
         FollowingRelationship.unfollow(follower, followed)
         {:ok, followed} = update_follower_count(followed)
 
@@ -671,6 +671,24 @@ defmodule Pleroma.User do
 
   defdelegate following?(follower, followed), to: FollowingRelationship
 
+  def get_follow_state(%User{} = follower, %User{} = following) do
+    following_relationship = FollowingRelationship.get(follower, following)
+
+    case {following_relationship, following.local} do
+      {nil, false} ->
+        case Utils.fetch_latest_follow(follower, following) do
+          %{data: %{"state" => state}} when state in ["pending", "accept"] -> state
+          _ -> nil
+        end
+
+      {%{state: state}, _} ->
+        state
+
+      {nil, _} ->
+        nil
+    end
+  end
+
   def locked?(%User{} = user) do
     user.locked || false
   end
index 7eff24ce4e6b834705cfd17bc11c37e1140b9a0b..a7b3625255bc21912af298e58207825f3e4d43be 100644 (file)
@@ -544,11 +544,9 @@ defmodule Pleroma.Web.CommonAPITest do
       assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
                CommonAPI.follow(follower, followed)
 
-      assert %{state: "pending"} = Pleroma.FollowingRelationship.get(follower, followed)
-
+      assert User.get_follow_state(follower, followed) == "pending"
       assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
-
-      assert Pleroma.FollowingRelationship.get(follower, followed) == nil
+      assert User.get_follow_state(follower, followed) == nil
 
       assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
                Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)
@@ -568,11 +566,9 @@ defmodule Pleroma.Web.CommonAPITest do
       assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} =
                CommonAPI.follow(follower, followed)
 
-      assert %{state: "pending"} = Pleroma.FollowingRelationship.get(follower, followed)
-
+      assert User.get_follow_state(follower, followed) == "pending"
       assert {:ok, follower} = CommonAPI.unfollow(follower, followed)
-
-      assert Pleroma.FollowingRelationship.get(follower, followed) == nil
+      assert User.get_follow_state(follower, followed) == nil
 
       assert %{id: ^activity_id, data: %{"state" => "cancelled"}} =
                Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed)