X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fpleroma%2Fpagination.ex;h=28e37933e2abfbaaaa2258c82b65f96f210cb0df;hb=78c44f31ca10c296dffc22e0b01a2a0cd9e25cfa;hp=d43a96cd2ee4bcfef30d9570722c59e7fd730b89;hpb=7605fe5065b62dcb8aaf7db07e5d168138b80d3e;p=akkoma
diff --git a/lib/pleroma/pagination.ex b/lib/pleroma/pagination.ex
index d43a96cd2..28e37933e 100644
--- a/lib/pleroma/pagination.ex
+++ b/lib/pleroma/pagination.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Pagination do
@@ -23,12 +23,12 @@ defmodule Pleroma.Pagination do
@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
+ def fetch_paginated(query, %{total: true} = params, :keyset, table_binding) do
total = Repo.aggregate(query, :count, :id)
%{
total: total,
- items: fetch_paginated(query, Map.drop(params, ["total"]), :keyset, table_binding)
+ items: fetch_paginated(query, Map.drop(params, [:total]), :keyset, table_binding)
}
end
@@ -41,7 +41,7 @@ defmodule Pleroma.Pagination do
|> enforce_order(options)
end
- def fetch_paginated(query, %{"total" => true} = params, :offset, table_binding) do
+ def fetch_paginated(query, %{total: true} = params, :offset, table_binding) do
total =
query
|> Ecto.Query.exclude(:left_join)
@@ -49,7 +49,7 @@ defmodule Pleroma.Pagination do
%{
total: total,
- items: fetch_paginated(query, Map.drop(params, ["total"]), :offset, table_binding)
+ items: fetch_paginated(query, Map.drop(params, [:total]), :offset, table_binding)
}
end
@@ -64,6 +64,12 @@ defmodule Pleroma.Pagination do
@spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
def paginate(query, options, method \\ :keyset, table_binding \\ nil)
+ def paginate(list, options, _method, _table_binding) when is_list(list) do
+ offset = options[:offset] || 0
+ limit = options[:limit] || 0
+ Enum.slice(list, offset, limit)
+ end
+
def paginate(query, options, :keyset, table_binding) do
query
|> restrict(:min_id, options, table_binding)
@@ -82,20 +88,15 @@ defmodule Pleroma.Pagination do
defp cast_params(params) do
param_types = %{
- min_id: :string,
- since_id: :string,
- max_id: :string,
+ min_id: params[:id_type] || :string,
+ since_id: params[:id_type] || :string,
+ max_id: params[:id_type] || :string,
offset: :integer,
limit: :integer,
+ skip_extra_order: :boolean,
skip_order: :boolean
}
- params =
- Enum.reduce(params, %{}, fn
- {key, _value}, acc when is_atom(key) -> Map.drop(acc, [key])
- {key, value}, acc -> Map.put(acc, key, value)
- end)
-
changeset = cast({%{}, param_types}, params, Map.keys(param_types))
changeset.changes
end
@@ -114,6 +115,8 @@ defmodule Pleroma.Pagination do
defp restrict(query, :order, %{skip_order: true}, _), do: query
+ defp restrict(%{order_bys: [_ | _]} = query, :order, %{skip_extra_order: true}, _), do: query
+
defp restrict(query, :order, %{min_id: _}, table_binding) do
order_by(
query,