is_moderator: boolean(),
super_users: boolean(),
invisible: boolean(),
+ internal: boolean(),
followers: User.t(),
friends: User.t(),
recipients_from_activity: [String.t()],
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)
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
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
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
--- /dev/null
+# 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