X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fpagination.ex;h=1b99e44f9304210b811ecd9668097847be2c395e;hb=efdfc85c2d8e5118c1aa18e4f04026ec90cd11d2;hp=3d7dd9e6aa32851aa616894b8decce8e6125c4dd;hpb=182f7bbb1170c44eac4ab4a9efa4ff0bff991c98;p=akkoma
diff --git a/lib/pleroma/pagination.ex b/lib/pleroma/pagination.ex
index 3d7dd9e6a..1b99e44f9 100644
--- a/lib/pleroma/pagination.ex
+++ b/lib/pleroma/pagination.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Pagination do
@@ -12,24 +12,72 @@ 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
+ total = Repo.aggregate(query, :count, :id)
+
+ %{
+ total: total,
+ items: fetch_paginated(query, Map.drop(params, [:total]), :keyset, table_binding)
+ }
+ end
- def fetch_paginated(query, params) do
+ def fetch_paginated(query, params, :keyset, table_binding) do
options = cast_params(params)
query
- |> paginate(options)
+ |> paginate(options, :keyset, table_binding)
|> Repo.all()
|> enforce_order(options)
end
- def paginate(query, options) do
+ def fetch_paginated(query, %{total: true} = params, :offset, table_binding) do
+ total =
+ query
+ |> Ecto.Query.exclude(:left_join)
+ |> Repo.aggregate(:count, :id)
+
+ %{
+ total: total,
+ items: fetch_paginated(query, Map.drop(params, [:total]), :offset, table_binding)
+ }
+ end
+
+ def fetch_paginated(query, params, :offset, table_binding) do
+ options = cast_params(params)
+
+ query
+ |> paginate(options, :offset, table_binding)
+ |> 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
+ query
+ |> restrict(:min_id, options, table_binding)
+ |> restrict(:since_id, options, table_binding)
+ |> restrict(:max_id, options, table_binding)
+ |> restrict(:order, options, table_binding)
+ |> restrict(:limit, options, table_binding)
+ end
+
+ def paginate(query, options, :offset, table_binding) do
query
- |> restrict(:min_id, options)
- |> restrict(:since_id, options)
- |> restrict(:max_id, options)
- |> restrict(:order, options)
- |> restrict(:limit, options)
+ |> restrict(:order, options, table_binding)
+ |> restrict(:offset, options, table_binding)
+ |> restrict(:limit, options, table_binding)
end
defp cast_params(params) do
@@ -37,47 +85,61 @@ defmodule Pleroma.Pagination do
min_id: :string,
since_id: :string,
max_id: :string,
- limit: :integer
+ offset: :integer,
+ limit: :integer,
+ 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
- defp restrict(query, :min_id, %{min_id: min_id}) do
- where(query, [q], q.id > ^min_id)
+ defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
+ where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
+ end
+
+ defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
+ where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
end
- defp restrict(query, :since_id, %{since_id: since_id}) do
- where(query, [q], q.id > ^since_id)
+ defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
+ where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
end
- defp restrict(query, :max_id, %{max_id: max_id}) do
- where(query, [q], q.id < ^max_id)
+ defp restrict(query, :order, %{skip_order: true}, _), do: query
+
+ defp restrict(query, :order, %{min_id: _}, table_binding) do
+ order_by(
+ query,
+ [{u, table_position(query, table_binding)}],
+ fragment("? asc nulls last", u.id)
+ )
end
- defp restrict(query, :order, %{min_id: _}) do
- order_by(query, [u], fragment("? asc nulls last", u.id))
+ defp restrict(query, :order, _options, table_binding) do
+ order_by(
+ query,
+ [{u, table_position(query, table_binding)}],
+ fragment("? desc nulls last", u.id)
+ )
end
- defp restrict(query, :order, _options) do
- order_by(query, [u], fragment("? desc nulls last", u.id))
+ defp restrict(query, :offset, %{offset: offset}, _table_binding) do
+ offset(query, ^offset)
end
- defp restrict(query, :limit, options) do
- limit = Map.get(options, :limit, @default_limit)
+ defp restrict(query, :limit, options, _table_binding) do
+ limit =
+ case Map.get(options, :limit, @default_limit) do
+ limit when limit < @max_limit -> limit
+ _ -> @max_limit
+ end
query
|> limit(^limit)
end
- defp restrict(query, _, _), do: query
+ defp restrict(query, _, _, _), do: query
defp enforce_order(result, %{min_id: _}) do
result
@@ -85,4 +147,10 @@ defmodule Pleroma.Pagination do
end
defp enforce_order(result, _), do: result
+
+ defp table_position(%Ecto.Query{} = query, binding_name) do
+ Map.get(query.aliases, binding_name, 0)
+ end
+
+ defp table_position(_, _), do: 0
end