Return total from pagination + tests

This commit is contained in:
Maxim Filippov 2019-09-02 22:48:52 +03:00
commit a4c5f71e93
10 changed files with 109 additions and 7 deletions

View 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

View 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

View file

@ -75,6 +75,7 @@ defmodule Pleroma.Notification do
user
|> for_user_query(opts)
|> Pagination.fetch_paginated(opts)
|> Map.get(:items)
end
@doc """

View file

@ -18,19 +18,29 @@ defmodule Pleroma.Pagination do
def fetch_paginated(query, params, :keyset) do
options = cast_params(params)
total = Repo.aggregate(query, :count, :id)
query
|> paginate(options, :keyset)
|> Repo.all()
|> enforce_order(options)
%{
total: total,
items:
query
|> paginate(options, :keyset)
|> Repo.all()
|> enforce_order(options)
}
end
def fetch_paginated(query, params, :offset) do
options = cast_params(params)
total = Repo.aggregate(query, :count, :id)
query
|> paginate(options, :offset)
|> Repo.all()
%{
total: total,
items:
query
|> paginate(options, :offset)
|> Repo.all()
}
end
def paginate(query, options, method \\ :keyset)

View 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

View 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

View 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

View 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