X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fpagination.ex;h=1307ad10223d652a3caf86619492663e1b49796c;hb=32d1e048178a94017c9d8cca79b28272fa6da9f4;hp=243f1a3295c37b48bae595388734cc6090f84e89;hpb=0937895182f167966872a3347098f78efe23dde9;p=akkoma diff --git a/lib/pleroma/pagination.ex b/lib/pleroma/pagination.ex index 243f1a329..1307ad102 100644 --- a/lib/pleroma/pagination.ex +++ b/lib/pleroma/pagination.ex @@ -12,11 +12,15 @@ defmodule Pleroma.Pagination do alias Pleroma.Repo + @type type :: :keyset | :offset + @default_limit 20 + @max_limit 40 @page_keys ["max_id", "min_id", "limit", "since_id", "order"] def page_keys, do: @page_keys + @spec fetch_paginated(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()] def fetch_paginated(query, params, type \\ :keyset, table_binding \\ nil) def fetch_paginated(query, %{"total" => true} = params, :keyset, table_binding) do @@ -38,7 +42,10 @@ defmodule Pleroma.Pagination do end def fetch_paginated(query, %{"total" => true} = params, :offset, table_binding) do - total = Repo.aggregate(query, :count, :id) + total = + query + |> Ecto.Query.exclude(:left_join) + |> Repo.aggregate(:count, :id) %{ total: total, @@ -54,6 +61,7 @@ defmodule Pleroma.Pagination do |> Repo.all() end + @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()] def paginate(query, options, method \\ :keyset, table_binding \\ nil) def paginate(query, options, :keyset, table_binding) do @@ -78,7 +86,8 @@ defmodule Pleroma.Pagination do since_id: :string, max_id: :string, offset: :integer, - limit: :integer + limit: :integer, + skip_order: :boolean } params = @@ -103,6 +112,8 @@ defmodule Pleroma.Pagination do where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id) end + defp restrict(query, :order, %{skip_order: true}, _), do: query + defp restrict(query, :order, %{min_id: _}, table_binding) do order_by( query, @@ -124,7 +135,11 @@ defmodule Pleroma.Pagination do end defp restrict(query, :limit, options, _table_binding) do - limit = Map.get(options, :limit, @default_limit) + limit = + case Map.get(options, :limit, @default_limit) do + limit when limit < @max_limit -> limit + _ -> @max_limit + end query |> limit(^limit)