Merge branch 'user-query/filter-out-internal' into 'develop'

Filter out internal users by default

Closes #2147

See merge request pleroma/pleroma!3024
This commit is contained in:
lain 2020-09-23 11:51:04 +00:00
commit 4dadb7c433
2 changed files with 47 additions and 5 deletions

View 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 @@ defp base_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 @@ defp compose_query({:external, _}, query), do: location_query(query, false)
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 @@ 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
@ -199,10 +199,15 @@ defp compose_query({:limit, limit}, 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

37
test/user/query_test.exs Normal file
View File

@ -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