Return total from pagination + tests
This commit is contained in:
parent
6d33c89c4d
commit
a4c5f71e93
10 changed files with 109 additions and 7 deletions
|
@ -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
|
- Mastodon API: Unsubscribe followers when they unfollow a user
|
||||||
- AdminAPI: Add "godmode" while fetching user statuses (i.e. admin can see private statuses)
|
- AdminAPI: Add "godmode" while fetching user statuses (i.e. admin can see private statuses)
|
||||||
- Improve digest email template
|
- Improve digest email template
|
||||||
|
– Pagination: return `total` alongside with `items` when paginating
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- Following from Osada
|
- Following from Osada
|
||||||
|
|
|
@ -27,6 +27,7 @@ defmodule Pleroma.Activity.Search do
|
||||||
|> maybe_restrict_local(user)
|
|> maybe_restrict_local(user)
|
||||||
|> maybe_restrict_author(author)
|
|> maybe_restrict_author(author)
|
||||||
|> Pagination.fetch_paginated(%{"offset" => offset, "limit" => limit}, :offset)
|
|> Pagination.fetch_paginated(%{"offset" => offset, "limit" => limit}, :offset)
|
||||||
|
|> Map.get(:items)
|
||||||
|> maybe_fetch(user, search_query)
|
|> maybe_fetch(user, search_query)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,7 @@ defmodule Pleroma.Conversation.Participation do
|
||||||
preload: [conversation: [:users]]
|
preload: [conversation: [:users]]
|
||||||
)
|
)
|
||||||
|> Pleroma.Pagination.fetch_paginated(params)
|
|> Pleroma.Pagination.fetch_paginated(params)
|
||||||
|
|> Map.get(:items)
|
||||||
end
|
end
|
||||||
|
|
||||||
def for_user_and_conversation(user, conversation) do
|
def for_user_and_conversation(user, conversation) do
|
||||||
|
|
|
@ -75,6 +75,7 @@ defmodule Pleroma.Notification do
|
||||||
user
|
user
|
||||||
|> for_user_query(opts)
|
|> for_user_query(opts)
|
||||||
|> Pagination.fetch_paginated(opts)
|
|> Pagination.fetch_paginated(opts)
|
||||||
|
|> Map.get(:items)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -18,19 +18,29 @@ defmodule Pleroma.Pagination do
|
||||||
|
|
||||||
def fetch_paginated(query, params, :keyset) do
|
def fetch_paginated(query, params, :keyset) do
|
||||||
options = cast_params(params)
|
options = cast_params(params)
|
||||||
|
total = Repo.aggregate(query, :count, :id)
|
||||||
|
|
||||||
|
%{
|
||||||
|
total: total,
|
||||||
|
items:
|
||||||
query
|
query
|
||||||
|> paginate(options, :keyset)
|
|> paginate(options, :keyset)
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|> enforce_order(options)
|
|> enforce_order(options)
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_paginated(query, params, :offset) do
|
def fetch_paginated(query, params, :offset) do
|
||||||
options = cast_params(params)
|
options = cast_params(params)
|
||||||
|
total = Repo.aggregate(query, :count, :id)
|
||||||
|
|
||||||
|
%{
|
||||||
|
total: total,
|
||||||
|
items:
|
||||||
query
|
query
|
||||||
|> paginate(options, :offset)
|
|> paginate(options, :offset)
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def paginate(query, options, method \\ :keyset)
|
def paginate(query, options, method \\ :keyset)
|
||||||
|
|
|
@ -34,6 +34,7 @@ defmodule Pleroma.User.Search do
|
||||||
query_string
|
query_string
|
||||||
|> search_query(for_user, following)
|
|> search_query(for_user, following)
|
||||||
|> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
|
|> Pagination.fetch_paginated(%{"offset" => offset, "limit" => result_limit}, :offset)
|
||||||
|
|> Map.get(:items)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
results
|
results
|
||||||
|
|
|
@ -556,6 +556,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
q
|
q
|
||||||
|> restrict_unlisted()
|
|> restrict_unlisted()
|
||||||
|> Pagination.fetch_paginated(opts)
|
|> Pagination.fetch_paginated(opts)
|
||||||
|
|> Map.get(:items)
|
||||||
|> Enum.reverse()
|
|> Enum.reverse()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -953,6 +954,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
|
|
||||||
fetch_activities_query(recipients ++ list_memberships, opts)
|
fetch_activities_query(recipients ++ list_memberships, opts)
|
||||||
|> Pagination.fetch_paginated(opts)
|
|> Pagination.fetch_paginated(opts)
|
||||||
|
|> Map.get(:items)
|
||||||
|> Enum.reverse()
|
|> Enum.reverse()
|
||||||
|> maybe_update_cc(list_memberships, opts["user"])
|
|> maybe_update_cc(list_memberships, opts["user"])
|
||||||
end
|
end
|
||||||
|
@ -987,6 +989,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||||
fetch_activities_query([], opts)
|
fetch_activities_query([], opts)
|
||||||
|> fetch_activities_bounded_query(recipients, recipients_with_public)
|
|> fetch_activities_bounded_query(recipients, recipients_with_public)
|
||||||
|> Pagination.fetch_paginated(opts)
|
|> Pagination.fetch_paginated(opts)
|
||||||
|
|> Map.get(:items)
|
||||||
|> Enum.reverse()
|
|> Enum.reverse()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -420,6 +420,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
[user.ap_id]
|
[user.ap_id]
|
||||||
|> ActivityPub.fetch_activities_query(params)
|
|> ActivityPub.fetch_activities_query(params)
|
||||||
|> Pagination.fetch_paginated(params)
|
|> Pagination.fetch_paginated(params)
|
||||||
|
|> Map.get(:items)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> add_link_headers(:dm_timeline, activities)
|
|> add_link_headers(:dm_timeline, activities)
|
||||||
|
@ -1194,6 +1195,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|
||||||
bookmarks =
|
bookmarks =
|
||||||
Bookmark.for_user_query(user.id)
|
Bookmark.for_user_query(user.id)
|
||||||
|> Pagination.fetch_paginated(params)
|
|> Pagination.fetch_paginated(params)
|
||||||
|
|> Map.get(:items)
|
||||||
|
|
||||||
activities =
|
activities =
|
||||||
bookmarks
|
bookmarks
|
||||||
|
|
|
@ -45,12 +45,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
|
||||||
user
|
user
|
||||||
|> User.get_followers_query()
|
|> User.get_followers_query()
|
||||||
|> Pagination.fetch_paginated(params)
|
|> Pagination.fetch_paginated(params)
|
||||||
|
|> Map.get(:items)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_friends(user, params \\ %{}) do
|
def get_friends(user, params \\ %{}) do
|
||||||
user
|
user
|
||||||
|> User.get_friends_query()
|
|> User.get_friends_query()
|
||||||
|> Pagination.fetch_paginated(params)
|
|> Pagination.fetch_paginated(params)
|
||||||
|
|> Map.get(:items)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_notifications(user, params \\ %{}) do
|
def get_notifications(user, params \\ %{}) do
|
||||||
|
@ -60,12 +62,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPI do
|
||||||
|> Notification.for_user_query(options)
|
|> Notification.for_user_query(options)
|
||||||
|> restrict(:exclude_types, options)
|
|> restrict(:exclude_types, options)
|
||||||
|> Pagination.fetch_paginated(params)
|
|> Pagination.fetch_paginated(params)
|
||||||
|
|> Map.get(:items)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_scheduled_activities(user, params \\ %{}) do
|
def get_scheduled_activities(user, params \\ %{}) do
|
||||||
user
|
user
|
||||||
|> ScheduledActivity.for_user_query()
|
|> ScheduledActivity.for_user_query()
|
||||||
|> Pagination.fetch_paginated(params)
|
|> Pagination.fetch_paginated(params)
|
||||||
|
|> Map.get(:items)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp cast_params(params) do
|
defp cast_params(params) do
|
||||||
|
|
78
test/pagination_test.exs
Normal file
78
test/pagination_test.exs
Normal file
|
@ -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
|
Loading…
Reference in a new issue