Applied relationships preloading to GET /api/v1/accounts/relationships. Refactoring (User.binary_id/1).

This commit is contained in:
Ivan Tashkinov 2020-03-25 17:01:45 +03:00
parent 8f1d622b8d
commit be5e2c4dbb
6 changed files with 30 additions and 23 deletions

View File

@ -129,21 +129,18 @@ def for_user(user, params \\ %{}) 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})

View File

@ -135,12 +135,8 @@ def all_between_user_sets(
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(

View File

@ -24,10 +24,10 @@ def changeset(mute, params \\ %{}) 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

View File

@ -218,6 +218,21 @@ def unquote(:"#{outgoing_relation_target}_ap_ids")(user, restrict_deactivated? \
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

View 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 @@ def dictionary(
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

View 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 @@ def render(
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