From 2883f75a3a25599c6217460133578cddcd34ebb4 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Thu, 28 Feb 2019 01:11:56 +0300 Subject: [PATCH] Add pagination to users admin API --- lib/pleroma/user.ex | 21 ++++++++++++++++--- .../web/admin_api/admin_api_controller.ex | 20 +++++++++++++----- .../web/twitter_api/views/user_view.ex | 9 +++++--- .../admin_api/admin_api_controller_test.exs | 20 +++++++++++------- 4 files changed, 51 insertions(+), 19 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 01d532ab3..80e4ae296 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -772,10 +772,25 @@ defmodule Pleroma.User 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 diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex index ef72509fe..d75b7e7e7 100644 --- a/lib/pleroma/web/admin_api/admin_api_controller.ex +++ b/lib/pleroma/web/admin_api/admin_api_controller.ex @@ -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 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do do: json_response(conn, :no_content, "") end - def list_users(%{assigns: %{user: admin}} = conn, _data) do - users = User.all_except_one(admin) - - conn - |> json(UserView.render("index_for_admin.json", %{users: users})) + 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, + count: count, + page_size: @users_page_size + }) + ) end def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname}) diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index c5034cf36..e8514d3ba 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -27,9 +27,12 @@ defmodule Pleroma.Web.TwitterAPI.UserView 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 diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index f6ae16844..1b0a2f5be 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -338,15 +338,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do conn = build_conn() |> assign(:user, admin) - |> get("/api/pleroma/admin/users") + |> get("/api/pleroma/admin/users?page=1") - assert json_response(conn, 200) == [ - %{ - "deactivated" => user.info.deactivated, - "id" => user.id, - "nickname" => user.nickname - } - ] + 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