forked from AkkomaGang/akkoma
Add pagination to users admin API
This commit is contained in:
parent
14bc6bd40f
commit
2883f75a3a
4 changed files with 51 additions and 19 deletions
|
@ -772,10 +772,25 @@ def search(query, resolve \\ false, for_user \\ nil) do
|
|||
Enum.uniq_by(fts_results ++ trigram_results, & &1.id)
|
||||
end
|
||||
|
||||
def all_except_one(user) do
|
||||
query = from(u in User, where: u.id != ^user.id)
|
||||
def all_except_one(user, page, page_size) do
|
||||
from(
|
||||
u in User,
|
||||
where: u.id != ^user.id,
|
||||
limit: ^page_size,
|
||||
offset: ^((page - 1) * page_size),
|
||||
order_by: u.id
|
||||
)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
Repo.all(query)
|
||||
def count_all_except_one(user) do
|
||||
query =
|
||||
from(
|
||||
u in User,
|
||||
where: u.id != ^user.id
|
||||
)
|
||||
|
||||
Repo.aggregate(query, :count, :id)
|
||||
end
|
||||
|
||||
defp do_search(subquery, for_user, options \\ []) do
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|
||||
@users_page_size 50
|
||||
|
||||
use Pleroma.Web, :controller
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.Relay
|
||||
|
@ -61,11 +63,19 @@ def untag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
|
|||
do: json_response(conn, :no_content, "")
|
||||
end
|
||||
|
||||
def list_users(%{assigns: %{user: admin}} = conn, _data) do
|
||||
users = User.all_except_one(admin)
|
||||
|
||||
def list_users(%{assigns: %{user: admin}} = conn, %{"page" => page_string}) do
|
||||
with {page, _} <- Integer.parse(page_string),
|
||||
users <- User.all_except_one(admin, page, @users_page_size),
|
||||
count <- User.count_all_except_one(admin),
|
||||
do:
|
||||
conn
|
||||
|> json(UserView.render("index_for_admin.json", %{users: users}))
|
||||
|> json(
|
||||
UserView.render("index_for_admin.json", %{
|
||||
users: users,
|
||||
count: count,
|
||||
page_size: @users_page_size
|
||||
})
|
||||
)
|
||||
end
|
||||
|
||||
def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname})
|
||||
|
|
|
@ -27,9 +27,12 @@ def render("user.json", %{user: user = %User{}} = assigns) do
|
|||
else: %{}
|
||||
end
|
||||
|
||||
def render("index_for_admin.json", %{users: users} = opts) do
|
||||
users
|
||||
|> render_many(UserView, "show_for_admin.json", opts)
|
||||
def render("index_for_admin.json", %{users: users, count: count, page_size: page_size} = opts) do
|
||||
%{
|
||||
users: render_many(users, UserView, "show_for_admin.json", opts),
|
||||
count: count,
|
||||
page_size: page_size
|
||||
}
|
||||
end
|
||||
|
||||
def render("show_for_admin.json", %{user: user}) do
|
||||
|
|
|
@ -338,15 +338,19 @@ test "GET /api/pleroma/admin/users" do
|
|||
conn =
|
||||
build_conn()
|
||||
|> assign(:user, admin)
|
||||
|> get("/api/pleroma/admin/users")
|
||||
|> get("/api/pleroma/admin/users?page=1")
|
||||
|
||||
assert json_response(conn, 200) == [
|
||||
assert json_response(conn, 200) == %{
|
||||
"count" => 1,
|
||||
"page_size" => 50,
|
||||
"users" => [
|
||||
%{
|
||||
"deactivated" => user.info.deactivated,
|
||||
"id" => user.id,
|
||||
"nickname" => user.nickname
|
||||
}
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation" do
|
||||
|
|
Loading…
Reference in a new issue