Allow activities pagination via limit/offset

This commit is contained in:
Maxim Filippov 2019-09-25 01:25:42 +03:00
parent 29dd8ab9c0
commit 60cbea5bb2
4 changed files with 32 additions and 7 deletions

View file

@ -64,6 +64,7 @@ def paginate(query, options, :keyset) do
def paginate(query, options, :offset) do def paginate(query, options, :offset) do
query query
|> restrict(:order, options)
|> restrict(:offset, options) |> restrict(:offset, options)
|> restrict(:limit, options) |> restrict(:limit, options)
end end

View file

@ -519,13 +519,13 @@ def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
|> Repo.one() |> Repo.one()
end end
def fetch_public_activities(opts \\ %{}) do def fetch_public_activities(opts \\ %{}, pagination \\ :keyset) do
opts = Map.drop(opts, ["user"]) opts = Map.drop(opts, ["user"])
[Pleroma.Constants.as_public()] [Pleroma.Constants.as_public()]
|> fetch_activities_query(opts) |> fetch_activities_query(opts)
|> restrict_unlisted() |> restrict_unlisted()
|> Pagination.fetch_paginated(opts) |> Pagination.fetch_paginated(opts, pagination)
|> Enum.reverse() |> Enum.reverse()
end end
@ -918,11 +918,11 @@ def fetch_activities_query(recipients, opts \\ %{}) do
|> exclude_poll_votes(opts) |> exclude_poll_votes(opts)
end end
def fetch_activities(recipients, opts \\ %{}) do def fetch_activities(recipients, opts \\ %{}, pagination \\ :keyset) do
list_memberships = Pleroma.List.memberships(opts["user"]) list_memberships = Pleroma.List.memberships(opts["user"])
fetch_activities_query(recipients ++ list_memberships, opts) fetch_activities_query(recipients ++ list_memberships, opts)
|> Pagination.fetch_paginated(opts) |> Pagination.fetch_paginated(opts, pagination)
|> Enum.reverse() |> Enum.reverse()
|> maybe_update_cc(list_memberships, opts["user"]) |> maybe_update_cc(list_memberships, opts["user"])
end end
@ -953,10 +953,15 @@ def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
) )
end end
def fetch_activities_bounded(recipients, recipients_with_public, opts \\ %{}) do def fetch_activities_bounded(
recipients,
recipients_with_public,
opts \\ %{},
pagination \\ :keyset
) 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, pagination)
|> Enum.reverse() |> Enum.reverse()
end end

View file

@ -463,13 +463,17 @@ def force_password_reset(conn, %{"nickname" => nickname}) do
end end
def list_reports(conn, params) do def list_reports(conn, params) do
{page, page_size} = page_params(params)
params = params =
params params
|> Map.put("type", "Flag") |> Map.put("type", "Flag")
|> Map.put("skip_preload", true) |> Map.put("skip_preload", true)
|> Map.put("total", true) |> Map.put("total", true)
|> Map.put("limit", page_size)
|> Map.put("offset", (page - 1) * page_size)
reports = ActivityPub.fetch_activities([], params) reports = ActivityPub.fetch_activities([], params, :offset)
conn conn
|> put_view(ReportView) |> put_view(ReportView)

View file

@ -647,6 +647,21 @@ test "retrieves ids up to max_id" do
assert last == last_expected assert last == last_expected
end end
test "paginates via offset/limit" do
_first_activities = ActivityBuilder.insert_list(10)
activities = ActivityBuilder.insert_list(10)
_later_activities = ActivityBuilder.insert_list(10)
first_expected = List.first(activities)
activities =
ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
first = List.first(activities)
assert length(activities) == 20
assert first == first_expected
end
test "doesn't return reblogs for users for whom reblogs have been muted" do test "doesn't return reblogs for users for whom reblogs have been muted" do
activity = insert(:note_activity) activity = insert(:note_activity)
user = insert(:user) user = insert(:user)