Return total from pagination + tests
authorMaxim Filippov <colixer@gmail.com>
Mon, 2 Sep 2019 19:48:52 +0000 (22:48 +0300)
committerMaxim Filippov <colixer@gmail.com>
Mon, 2 Sep 2019 19:48:52 +0000 (22:48 +0300)
CHANGELOG.md
lib/pleroma/activity/search.ex
lib/pleroma/conversation/participation.ex
lib/pleroma/notification.ex
lib/pleroma/pagination.ex
lib/pleroma/user/search.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex
lib/pleroma/web/mastodon_api/mastodon_api.ex
test/pagination_test.exs [new file with mode: 0644]

index 4acb749aca91f3bcbacd64c47a0d430ab50466a4..06ad303de1b442f842382a329753598e94ca195b 100644 (file)
@@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Mastodon API: Unsubscribe followers when they unfollow a user
 - AdminAPI: Add "godmode" while fetching user statuses (i.e. admin can see private statuses)
 - Improve digest email template
+– Pagination: return `total` alongside with `items` when paginating
 
 ### Fixed
 - Following from Osada
index f847ac2381faa0d25678aa79503a8cc1752ba24c..f7156c81cd2dfb03f9a75c956d94036877e360d8 100644 (file)
@@ -27,6 +27,7 @@ defmodule Pleroma.Activity.Search do
     |> maybe_restrict_local(user)
     |> maybe_restrict_author(author)
     |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => limit}, :offset)
+    |> Map.get(:items)
     |> maybe_fetch(user, search_query)
   end
 
index ea5b9fe17ba0526647d78caf03f2f167fa130720..5fd8d3d414272e5a14516867780a0b626035e396 100644 (file)
@@ -67,6 +67,7 @@ defmodule Pleroma.Conversation.Participation do
       preload: [conversation: [:users]]
     )
     |> Pleroma.Pagination.fetch_paginated(params)
+    |> Map.get(:items)
   end
 
   def for_user_and_conversation(user, conversation) do
index 5d29af8536a5b6d8409a87916b2aa58a1e9ab6c4..3e4ddd2ba052e746077488728d8d0b62753d98bf 100644 (file)
@@ -75,6 +75,7 @@ defmodule Pleroma.Notification do
     user
     |> for_user_query(opts)
     |> Pagination.fetch_paginated(opts)
+    |> Map.get(:items)
   end
 
   @doc """
index 2b869ccdcc494c2a9bb158772da59c1167aa7529..d21ecf628400bb83913d6f1577655b0b775834ea 100644 (file)
@@ -18,19 +18,29 @@ defmodule Pleroma.Pagination do
 
   def fetch_paginated(query, params, :keyset) do
     options = cast_params(params)
-
-    query
-    |> paginate(options, :keyset)
-    |> Repo.all()
-    |> enforce_order(options)
+    total = Repo.aggregate(query, :count, :id)
+
+    %{
+      total: total,
+      items:
+        query
+        |> paginate(options, :keyset)
+        |> Repo.all()
+        |> enforce_order(options)
+    }
   end
 
   def fetch_paginated(query, params, :offset) do
     options = cast_params(params)
-
-    query
-    |> paginate(options, :offset)
-    |> Repo.all()
+    total = Repo.aggregate(query, :count, :id)
+
+    %{
+      total: total,
+      items:
+        query
+        |> paginate(options, :offset)
+        |> Repo.all()
+    }
   end
 
   def paginate(query, options, method \\ :keyset)
index 6fb2c2352f8aa9655f635ffc687ae40fb26f95ad..bc05639b53b5fb7ffac420fb4bdd8f727fd7852a 100644 (file)
@@ -34,6 +34,7 @@ defmodule Pleroma.User.Search do
         query_string
         |> search_query(for_user, following)
         |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
+        |> Map.get(:items)
       end)
 
     results
index eeb826814423c620a3dc0e341093d00bccc7ab8e..8f07638cd00f18fe249729030765a40eaf2a149c 100644 (file)
@@ -556,6 +556,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     q
     |> restrict_unlisted()
     |> Pagination.fetch_paginated(opts)
+    |> Map.get(:items)
     |> Enum.reverse()
   end
 
@@ -953,6 +954,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
     fetch_activities_query(recipients ++ list_memberships, opts)
     |> Pagination.fetch_paginated(opts)
+    |> Map.get(:items)
     |> Enum.reverse()
     |> maybe_update_cc(list_memberships, opts["user"])
   end
@@ -987,6 +989,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     fetch_activities_query([], opts)
     |> fetch_activities_bounded_query(recipients, recipients_with_public)
     |> Pagination.fetch_paginated(opts)
+    |> Map.get(:items)
     |> Enum.reverse()
   end
 
index 83e877c0e79e352022b6df5df528b59618e3d58f..d532ba685985d2ea94cf996b9c0e746f0e6ad66f 100644 (file)
@@ -420,6 +420,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
       [user.ap_id]
       |> ActivityPub.fetch_activities_query(params)
       |> Pagination.fetch_paginated(params)
+      |> Map.get(:items)
 
     conn
     |> add_link_headers(:dm_timeline, activities)
@@ -1194,6 +1195,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     bookmarks =
       Bookmark.for_user_query(user.id)
       |> Pagination.fetch_paginated(params)
+      |> Map.get(:items)
 
     activities =
       bookmarks
index ac01d1ff39a42639f4b457b780b5893e0429c3e5..cf396294409b77e2b610983d3c0a597b4840312c 100644 (file)
@@ -45,12 +45,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
     user
     |> User.get_followers_query()
     |> Pagination.fetch_paginated(params)
+    |> Map.get(:items)
   end
 
   def get_friends(user, params \\ %{}) do
     user
     |> User.get_friends_query()
     |> Pagination.fetch_paginated(params)
+    |> Map.get(:items)
   end
 
   def get_notifications(user, params \\ %{}) do
@@ -60,12 +62,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
     |> Notification.for_user_query(options)
     |> restrict(:exclude_types, options)
     |> Pagination.fetch_paginated(params)
+    |> Map.get(:items)
   end
 
   def get_scheduled_activities(user, params \\ %{}) do
     user
     |> ScheduledActivity.for_user_query()
     |> Pagination.fetch_paginated(params)
+    |> Map.get(:items)
   end
 
   defp cast_params(params) do
diff --git a/test/pagination_test.exs b/test/pagination_test.exs
new file mode 100644 (file)
index 0000000..048ab6f
--- /dev/null
@@ -0,0 +1,78 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.PaginationTest do
+  use Pleroma.DataCase
+
+  import Pleroma.Factory
+
+  alias Pleroma.Object
+  alias Pleroma.Pagination
+
+  describe "keyset" do
+    setup do
+      notes = insert_list(5, :note)
+
+      %{notes: notes}
+    end
+
+    test "paginates by min_id", %{notes: notes} do
+      id = Enum.at(notes, 2).id |> Integer.to_string()
+      %{total: total, items: paginated} = Pagination.fetch_paginated(Object, %{"min_id" => id})
+
+      assert length(paginated) == 2
+      assert total == 5
+    end
+
+    test "paginates by since_id", %{notes: notes} do
+      id = Enum.at(notes, 2).id |> Integer.to_string()
+      %{total: total, items: paginated} = Pagination.fetch_paginated(Object, %{"since_id" => id})
+
+      assert length(paginated) == 2
+      assert total == 5
+    end
+
+    test "paginates by max_id", %{notes: notes} do
+      id = Enum.at(notes, 1).id |> Integer.to_string()
+      %{total: total, items: paginated} = Pagination.fetch_paginated(Object, %{"max_id" => id})
+
+      assert length(paginated) == 1
+      assert total == 5
+    end
+
+    test "paginates by min_id & limit", %{notes: notes} do
+      id = Enum.at(notes, 2).id |> Integer.to_string()
+
+      %{total: total, items: paginated} =
+        Pagination.fetch_paginated(Object, %{"min_id" => id, "limit" => 1})
+
+      assert length(paginated) == 1
+      assert total == 5
+    end
+  end
+
+  describe "offset" do
+    setup do
+      notes = insert_list(5, :note)
+
+      %{notes: notes}
+    end
+
+    test "paginates by limit" do
+      %{total: total, items: paginated} =
+        Pagination.fetch_paginated(Object, %{"limit" => 2}, :offset)
+
+      assert length(paginated) == 2
+      assert total == 5
+    end
+
+    test "paginates by limit & offset" do
+      %{total: total, items: paginated} =
+        Pagination.fetch_paginated(Object, %{"limit" => 2, "offset" => 4}, :offset)
+
+      assert length(paginated) == 1
+      assert total == 5
+    end
+  end
+end