Merge branch 'develop' into global-status-expiration
[akkoma] / lib / pleroma / following_relationship.ex
index cc381af53f1e47986d70bdfbe0b272044829edb4..a9538ea4e4d613b8020b835781268890ca229b5a 100644 (file)
@@ -1,5 +1,5 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.FollowingRelationship do
@@ -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)
@@ -144,4 +129,32 @@ defmodule Pleroma.FollowingRelationship do
         move_following(origin, target)
     end
   end
+
+  def all_between_user_sets(
+        source_users,
+        target_users
+      )
+      when is_list(source_users) and is_list(target_users) do
+    source_user_ids = User.binary_id(source_users)
+    target_user_ids = User.binary_id(target_users)
+
+    __MODULE__
+    |> where(
+      fragment(
+        "(follower_id = ANY(?) AND following_id = ANY(?)) OR \
+        (follower_id = ANY(?) AND following_id = ANY(?))",
+        ^source_user_ids,
+        ^target_user_ids,
+        ^target_user_ids,
+        ^source_user_ids
+      )
+    )
+    |> Repo.all()
+  end
+
+  def find(following_relationships, follower, following) do
+    Enum.find(following_relationships, fn
+      fr -> fr.follower_id == follower.id and fr.following_id == following.id
+    end)
+  end
 end