[#1335] Applied code review suggestions.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Mon, 11 Nov 2019 11:16:57 +0000 (14:16 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Mon, 11 Nov 2019 11:16:57 +0000 (14:16 +0300)
lib/pleroma/user.ex
lib/pleroma/user/search.ex
lib/pleroma/user_block.ex

index d4fc1c093d2197b56335e58e582f2a5913522dfc..9dd1ad8fc3b6edaec78c9121d00f5050f8af32f0 100644 (file)
@@ -1090,11 +1090,10 @@ defmodule Pleroma.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()]
index 8fa1e2e979f88f0e42ce62ada245bacb2320bf9e..049e1a634d722dc333b22a8376da66fc191d53f0 100644 (file)
@@ -99,17 +99,12 @@ defmodule Pleroma.User.Search do
   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
index 1cdb1588672c9591f2c2c12d11ef7f73975e0951..bcf4b64d990b135ddf455ca46307fa9f8bf073b8 100644 (file)
@@ -45,24 +45,23 @@ defmodule Pleroma.UserBlock 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
         []