From 5cf2c7422b006d477498af8d7618a0d6f7659290 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Mon, 11 Nov 2019 14:16:57 +0300 Subject: [PATCH] [#1335] Applied code review suggestions. --- lib/pleroma/user.ex | 9 ++++----- lib/pleroma/user/search.ex | 15 +++++---------- lib/pleroma/user_block.ex | 11 +++++------ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index d4fc1c093..9dd1ad8fc 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1090,11 +1090,10 @@ def blocked_users(user) do end def blocked_ap_ids(user) do - Repo.all( - from(u in assoc(user, :blocked_users), - select: u.ap_id - ) - ) + user + |> assoc(:blocked_users) + |> select([u], u.ap_id) + |> Repo.all() end @spec subscribers(User.t()) :: [User.t()] diff --git a/lib/pleroma/user/search.ex b/lib/pleroma/user/search.ex index 8fa1e2e97..049e1a634 100644 --- a/lib/pleroma/user/search.ex +++ b/lib/pleroma/user/search.ex @@ -99,17 +99,12 @@ defp base_query(_user, false), do: User defp base_query(user, true), do: User.get_followers_query(user) defp filter_blocked_user(query, %User{} = blocker) do - blocker_id = FlakeId.from_string(blocker.id) - - from( - q in query, - where: - fragment( - "? NOT IN (SELECT blockee_id FROM user_blocks WHERE user_blocks.blocker_id = ?)", - q.id, - ^blocker_id - ) + query + |> join(:left, [u], b in Pleroma.UserBlock, + as: :blocks, + on: b.blocker_id == ^blocker.id and u.id == b.blockee_id ) + |> where([blocks: b], is_nil(b.blockee_id)) end defp filter_blocked_user(query, _), do: query diff --git a/lib/pleroma/user_block.ex b/lib/pleroma/user_block.ex index 1cdb15886..bcf4b64d9 100644 --- a/lib/pleroma/user_block.ex +++ b/lib/pleroma/user_block.ex @@ -45,24 +45,23 @@ def create(%User{} = blocker, %User{} = blockee) do def delete(%User{} = blocker, %User{} = blockee) do attrs = %{blocker_id: blocker.id, blockee_id: blockee.id} - if is_nil(existing_record = Repo.get_by(UserBlock, attrs)) do - {:ok, nil} - else - Repo.delete(existing_record) + case Repo.get_by(UserBlock, attrs) do + %UserBlock{} = existing_record -> Repo.delete(existing_record) + nil -> {:ok, nil} end end defp validate_not_self_block(%Ecto.Changeset{} = changeset) do changeset |> validate_change(:blockee_id, fn _, blockee_id -> - if blockee_id == changeset.changes[:blocker_id] || changeset.data.blocker_id do + if blockee_id == get_field(changeset, :blocker_id) do [blockee_id: "can't be equal to blocker_id"] else [] end end) |> validate_change(:blocker_id, fn _, blocker_id -> - if blocker_id == changeset.changes[:blockee_id] || changeset.data.blockee_id do + if blocker_id == get_field(changeset, :blockee_id) do [blocker_id: "can't be equal to blockee_id"] else []