Add pagination to users admin API
authorMaxim Filippov <colixer@gmail.com>
Wed, 27 Feb 2019 22:11:56 +0000 (01:11 +0300)
committerMaxim Filippov <colixer@gmail.com>
Wed, 27 Feb 2019 22:11:56 +0000 (01:11 +0300)
lib/pleroma/user.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/twitter_api/views/user_view.ex
test/web/admin_api/admin_api_controller_test.exs

index 01d532ab3158b3060e26a3fb0d3d69f6c37c5bcc..80e4ae296f84acbcea43270f698323e59a7e4af4 100644 (file)
@@ -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
index ef72509fe4f0ceb6b5facb2c4b33b4a55e9c57f6..d75b7e7e79e21016804e9f6b6319cbb53d9fbce8 100644 (file)
@@ -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})
index c5034cf3674b9236fb240b3d0044c9dd9c11f215..e8514d3ba2f69e3e7c9d960e979ec6403f9c224c 100644 (file)
@@ -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
index f6ae16844dd3101819e9b5eee8b72a9c572ef628..1b0a2f5be70cb03d2356b01afdbab0868cd0a138 100644 (file)
@@ -338,15 +338,19 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
     conn =
       build_conn()
       |> assign(:user, admin)
-      |> get("/api/pleroma/admin/users")
-
-    assert json_response(conn, 200) == [
-             %{
-               "deactivated" => user.info.deactivated,
-               "id" => user.id,
-               "nickname" => user.nickname
-             }
-           ]
+      |> get("/api/pleroma/admin/users?page=1")
+
+    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