Add pagination to users admin API

This commit is contained in:
Maxim Filippov 2019-02-28 01:11:56 +03:00
parent 14bc6bd40f
commit 2883f75a3a
4 changed files with 51 additions and 19 deletions

View File

@ -772,10 +772,25 @@ defmodule Pleroma.User do
Enum.uniq_by(fts_results ++ trigram_results, & &1.id) Enum.uniq_by(fts_results ++ trigram_results, & &1.id)
end end
def all_except_one(user) do def all_except_one(user, page, page_size) do
query = from(u in User, where: u.id != ^user.id) 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 end
defp do_search(subquery, for_user, options \\ []) do defp do_search(subquery, for_user, options \\ []) do

View File

@ -3,6 +3,8 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.AdminAPI.AdminAPIController do defmodule Pleroma.Web.AdminAPI.AdminAPIController do
@users_page_size 50
use Pleroma.Web, :controller use Pleroma.Web, :controller
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.Relay alias Pleroma.Web.ActivityPub.Relay
@ -61,11 +63,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
do: json_response(conn, :no_content, "") do: json_response(conn, :no_content, "")
end end
def list_users(%{assigns: %{user: admin}} = conn, _data) do def list_users(%{assigns: %{user: admin}} = conn, %{"page" => page_string}) do
users = User.all_except_one(admin) with {page, _} <- Integer.parse(page_string),
users <- User.all_except_one(admin, page, @users_page_size),
conn count <- User.count_all_except_one(admin),
|> json(UserView.render("index_for_admin.json", %{users: users})) do:
conn
|> json(
UserView.render("index_for_admin.json", %{
users: users,
count: count,
page_size: @users_page_size
})
)
end end
def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname}) def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname})

View File

@ -27,9 +27,12 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
else: %{} else: %{}
end end
def render("index_for_admin.json", %{users: users} = opts) do def render("index_for_admin.json", %{users: users, count: count, page_size: page_size} = opts) do
users %{
|> render_many(UserView, "show_for_admin.json", opts) users: render_many(users, UserView, "show_for_admin.json", opts),
count: count,
page_size: page_size
}
end end
def render("show_for_admin.json", %{user: user}) do def render("show_for_admin.json", %{user: user}) do

View File

@ -338,15 +338,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
conn = conn =
build_conn() build_conn()
|> assign(:user, admin) |> 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,
"deactivated" => user.info.deactivated, "page_size" => 50,
"id" => user.id, "users" => [
"nickname" => user.nickname %{
} "deactivated" => user.info.deactivated,
] "id" => user.id,
"nickname" => user.nickname
}
]
}
end end
test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation" do test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation" do