User: search: exclude deactivated users from user search
authora1batross <a1ba.omarov@gmail.com>
Thu, 15 Sep 2022 20:38:35 +0000 (22:38 +0200)
committerFrancis Dinh <normandy@biribiri.dev>
Fri, 16 Sep 2022 01:21:06 +0000 (21:21 -0400)
This way we don't pollute search results with deactivated and deleted users

lib/pleroma/user/search.ex
test/pleroma/user_search_test.exs

index a4f6abca24b358a0764977de3152f5a40a2868a4..6b3f589993afee4b97d8d45ecdb730af4cafc7ce 100644 (file)
@@ -94,6 +94,7 @@ defmodule Pleroma.User.Search do
     |> subquery()
     |> order_by(desc: :search_rank)
     |> maybe_restrict_local(for_user)
+    |> filter_deactivated_users()
   end
 
   defp select_top_users(query, top_user_ids) do
@@ -166,6 +167,10 @@ defmodule Pleroma.User.Search do
     from(q in query, where: q.actor_type != "Application")
   end
 
+  defp filter_deactivated_users(query) do
+    from(q in query, where: q.is_active == true)
+  end
+
   defp filter_blocked_user(query, %User{} = blocker) do
     query
     |> join(:left, [u], b in Pleroma.UserRelationship,
index 69167bb0c84a0e3abbc11c5d4f4cc028070e49b6..8634a2e2b3ca5fa04bbc017e0970883dc1a3ddd3 100644 (file)
@@ -65,6 +65,14 @@ defmodule Pleroma.UserSearchTest do
       assert found_user.id == user.id
     end
 
+    test "excludes deactivated users from results" do
+      user = insert(:user, %{nickname: "john t1000"})
+      insert(:user, %{is_active: false, nickname: "john t800"})
+
+      [found_user] = User.search("john")
+      assert found_user.id == user.id
+    end
+
     # Note: as in Mastodon, `is_discoverable` doesn't anyhow relate to user searchability
     test "includes non-discoverable users in results" do
       insert(:user, %{nickname: "john 3000", is_discoverable: false})