Filter out internal users by default
authorEgor Kislitsyn <egor@kislitsyn.com>
Tue, 22 Sep 2020 12:55:40 +0000 (16:55 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Tue, 22 Sep 2020 12:56:11 +0000 (16:56 +0400)
lib/pleroma/user/query.ex
test/user/query_test.exs [new file with mode: 0644]

index d618432fffcc70e8bb86ceb44baec07f26adb8dd..193b90d9d65e077d0b20d789afd843f62ff85758 100644 (file)
@@ -47,6 +47,7 @@ defmodule Pleroma.User.Query do
             is_moderator: boolean(),
             super_users: boolean(),
             invisible: boolean(),
+            internal: boolean(),
             followers: User.t(),
             friends: User.t(),
             recipients_from_activity: [String.t()],
@@ -80,7 +81,9 @@ defmodule Pleroma.User.Query do
   end
 
   defp prepare_query(query, criteria) do
-    Enum.reduce(criteria, query, &compose_query/2)
+    criteria
+    |> Map.put_new(:internal, false)
+    |> Enum.reduce(query, &compose_query/2)
   end
 
   defp compose_query({key, value}, query)
@@ -129,14 +132,12 @@ defmodule Pleroma.User.Query do
 
   defp compose_query({:active, _}, query) do
     User.restrict_deactivated(query)
-    |> where([u], not is_nil(u.nickname))
     |> where([u], u.approval_pending == false)
   end
 
   defp compose_query({:legacy_active, _}, query) do
     query
     |> where([u], fragment("not (?->'deactivated' @> 'true')", u.info))
-    |> where([u], not is_nil(u.nickname))
   end
 
   defp compose_query({:deactivated, false}, query) do
@@ -145,7 +146,6 @@ defmodule Pleroma.User.Query do
 
   defp compose_query({:deactivated, true}, query) do
     where(query, [u], u.deactivated == ^true)
-    |> where([u], not is_nil(u.nickname))
   end
 
   defp compose_query({:need_approval, _}, query) do
@@ -199,10 +199,15 @@ defmodule Pleroma.User.Query do
     limit(query, ^limit)
   end
 
+  defp compose_query({:internal, false}, query) do
+    query
+    |> where([u], not is_nil(u.nickname))
+    |> where([u], not like(u.nickname, "internal.%"))
+  end
+
   defp compose_query(_unsupported_param, query), do: query
 
   defp location_query(query, local) do
     where(query, [u], u.local == ^local)
-    |> where([u], not is_nil(u.nickname))
   end
 end
diff --git a/test/user/query_test.exs b/test/user/query_test.exs
new file mode 100644 (file)
index 0000000..e2f5c7d
--- /dev/null
@@ -0,0 +1,37 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.User.QueryTest do
+  use Pleroma.DataCase, async: true
+
+  alias Pleroma.Repo
+  alias Pleroma.User
+  alias Pleroma.User.Query
+  alias Pleroma.Web.ActivityPub.InternalFetchActor
+
+  import Pleroma.Factory
+
+  describe "internal users" do
+    test "it filters out internal users by default" do
+      %User{nickname: "internal.fetch"} = InternalFetchActor.get_actor()
+
+      assert [_user] = User |> Repo.all()
+      assert [] == %{} |> Query.build() |> Repo.all()
+    end
+
+    test "it filters out users without nickname by default" do
+      insert(:user, %{nickname: nil})
+
+      assert [_user] = User |> Repo.all()
+      assert [] == %{} |> Query.build() |> Repo.all()
+    end
+
+    test "it returns internal users when enabled" do
+      %User{nickname: "internal.fetch"} = InternalFetchActor.get_actor()
+      insert(:user, %{nickname: nil})
+
+      assert %{internal: true} |> Query.build() |> Repo.aggregate(:count) == 2
+    end
+  end
+end