From bc2e98b20099be767a8262b734c6702edea663b4 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Fri, 7 Feb 2020 16:17:34 +0400 Subject: [PATCH] Add User.get_follow_state/2 --- lib/pleroma/following_relationship.ex | 21 +++------------------ lib/pleroma/user.ex | 22 ++++++++++++++++++++-- test/web/common_api/common_api_test.exs | 12 ++++-------- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/pleroma/following_relationship.ex b/lib/pleroma/following_relationship.ex index cc381af53..b8cb3bf03 100644 --- a/lib/pleroma/following_relationship.ex +++ b/lib/pleroma/following_relationship.ex @@ -30,24 +30,9 @@ def changeset(%__MODULE__{} = following_relationship, attrs) 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) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 398c91cf3..5ea36fea3 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -652,8 +652,8 @@ def unfollow(%User{ap_id: ap_id}, %User{ap_id: ap_id}) 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 @@ def unfollow(%User{} = follower, %User{} = followed) 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 diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 7eff24ce4..a7b362525 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -544,11 +544,9 @@ test "cancels a pending follow for a local user" 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 @@ test "cancels a pending follow for a remote user" 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)