Applied relationships preloading to GET /api/v1/accounts/relationships. Refactoring...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Wed, 25 Mar 2020 14:01:45 +0000 (17:01 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Wed, 25 Mar 2020 14:01:45 +0000 (17:01 +0300)
lib/pleroma/conversation/participation.ex
lib/pleroma/following_relationship.ex
lib/pleroma/thread_mute.ex
lib/pleroma/user.ex
lib/pleroma/user_relationship.ex
lib/pleroma/web/mastodon_api/views/account_view.ex

index 693825cf5fbef554bffda87bc7bb20757afb7e12..215265fc90a201c8c24539e9c0cc632bbea62699 100644 (file)
@@ -129,21 +129,18 @@ defmodule Pleroma.Conversation.Participation do
   end
 
   def restrict_recipients(query, user, %{"recipients" => user_ids}) do
-    user_ids =
+    user_binary_ids =
       [user.id | user_ids]
       |> Enum.uniq()
-      |> Enum.reduce([], fn user_id, acc ->
-        {:ok, user_id} = FlakeId.Ecto.CompatType.dump(user_id)
-        [user_id | acc]
-      end)
+      |> User.binary_id()
 
     conversation_subquery =
       __MODULE__
       |> group_by([p], p.conversation_id)
       |> having(
         [p],
-        count(p.user_id) == ^length(user_ids) and
-          fragment("array_agg(?) @> ?", p.user_id, ^user_ids)
+        count(p.user_id) == ^length(user_binary_ids) and
+          fragment("array_agg(?) @> ?", p.user_id, ^user_binary_ids)
       )
       |> select([p], %{id: p.conversation_id})
 
index dd16961364ad20671b9b3477077eda5b35c990f7..624bddfe487231b836b7f845fcb3e85009695813 100644 (file)
@@ -135,12 +135,8 @@ defmodule Pleroma.FollowingRelationship do
         target_users
       )
       when is_list(source_users) and is_list(target_users) do
-    get_bin_ids = fn user ->
-      with {:ok, bin_id} <- CompatType.dump(user.id), do: bin_id
-    end
-
-    source_user_ids = Enum.map(source_users, &get_bin_ids.(&1))
-    target_user_ids = Enum.map(target_users, &get_bin_ids.(&1))
+    source_user_ids = User.binary_id(source_users)
+    target_user_ids = User.binary_id(target_users)
 
     __MODULE__
     |> where(
index cc815430a39f576530a3f23d9b6f0ff5cd1accd5..f657758aa7246321f294c631b081dce286d08e73 100644 (file)
@@ -24,10 +24,10 @@ defmodule Pleroma.ThreadMute do
   end
 
   def query(user_id, context) do
-    {:ok, user_id} = FlakeId.Ecto.CompatType.dump(user_id)
+    user_binary_id = User.binary_id(user_id)
 
     ThreadMute
-    |> Ecto.Query.where(user_id: ^user_id)
+    |> Ecto.Query.where(user_id: ^user_binary_id)
     |> Ecto.Query.where(context: ^context)
   end
 
index f74e43cce99c5febe720f32e0b24f2b27a10ac7a..699256a3b9aa81a54183b42457b489b54b3efcd0 100644 (file)
@@ -218,6 +218,21 @@ defmodule Pleroma.User do
     end
   end
 
+  @doc "Dumps id to SQL-compatible format"
+  def binary_id(source_id) when is_binary(source_id) do
+    with {:ok, dumped_id} <- FlakeId.Ecto.CompatType.dump(source_id) do
+      dumped_id
+    else
+      _ -> source_id
+    end
+  end
+
+  def binary_id(source_ids) when is_list(source_ids) do
+    Enum.map(source_ids, &binary_id/1)
+  end
+
+  def binary_id(%User{} = user), do: binary_id(user.id)
+
   @doc "Returns status account"
   @spec account_status(User.t()) :: account_status()
   def account_status(%User{deactivated: true}), do: :deactivated
index 9423e3a42c71d7c8b3333216742a9c10c51a29b9..519d2998d27bdc262ea755280f58965fbed380c7 100644 (file)
@@ -8,7 +8,6 @@ defmodule Pleroma.UserRelationship do
   import Ecto.Changeset
   import Ecto.Query
 
-  alias FlakeId.Ecto.CompatType
   alias Pleroma.Repo
   alias Pleroma.User
   alias Pleroma.UserRelationship
@@ -84,12 +83,8 @@ defmodule Pleroma.UserRelationship do
         target_to_source_rel_types \\ nil
       )
       when is_list(source_users) and is_list(target_users) do
-    get_bin_ids = fn user ->
-      with {:ok, bin_id} <- CompatType.dump(user.id), do: bin_id
-    end
-
-    source_user_ids = Enum.map(source_users, &get_bin_ids.(&1))
-    target_user_ids = Enum.map(target_users, &get_bin_ids.(&1))
+    source_user_ids = User.binary_id(source_users)
+    target_user_ids = User.binary_id(target_users)
 
     get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
 
index 702d9e6583a05105b92f1ccd34521828bd876333..6b2eca1f33bcc62581daaf8427fa4d4453092808 100644 (file)
@@ -9,6 +9,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
   alias Pleroma.UserRelationship
   alias Pleroma.Web.CommonAPI.Utils
   alias Pleroma.Web.MastodonAPI.AccountView
+  alias Pleroma.Web.MastodonAPI.StatusView
   alias Pleroma.Web.MediaProxy
 
   defp find_following_rel(following_relationships, follower, following) do
@@ -129,7 +130,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
   end
 
   def render("relationships.json", %{user: user, targets: targets}) do
-    render_many(targets, AccountView, "relationship.json", user: user, as: :target)
+    relationships_opts = StatusView.relationships_opts(user, targets)
+    opts = %{as: :target, user: user, relationships: relationships_opts}
+
+    render_many(targets, AccountView, "relationship.json", opts)
   end
 
   defp do_render("show.json", %{user: user} = opts) do