1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Pagination do
7 Implements Mastodon-compatible pagination.
15 @type type :: :keyset | :offset
20 @spec fetch_paginated(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
21 def fetch_paginated(query, params, type \\ :keyset, table_binding \\ nil)
23 def fetch_paginated(query, %{total: true} = params, :keyset, table_binding) do
24 total = Repo.aggregate(query, :count, :id)
28 items: fetch_paginated(query, Map.drop(params, [:total]), :keyset, table_binding)
32 def fetch_paginated(query, params, :keyset, table_binding) do
33 options = cast_params(params)
36 |> paginate(options, :keyset, table_binding)
38 |> enforce_order(options)
41 def fetch_paginated(query, %{total: true} = params, :offset, table_binding) do
44 |> Ecto.Query.exclude(:left_join)
45 |> Repo.aggregate(:count, :id)
49 items: fetch_paginated(query, Map.drop(params, [:total]), :offset, table_binding)
53 def fetch_paginated(query, params, :offset, table_binding) do
54 options = cast_params(params)
57 |> paginate(options, :offset, table_binding)
61 @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
62 def paginate(query, options, method \\ :keyset, table_binding \\ nil)
64 def paginate(query, options, :keyset, table_binding) do
66 |> restrict(:min_id, options, table_binding)
67 |> restrict(:since_id, options, table_binding)
68 |> restrict(:max_id, options, table_binding)
69 |> restrict(:order, options, table_binding)
70 |> restrict(:limit, options, table_binding)
73 def paginate(query, options, :offset, table_binding) do
75 |> restrict(:order, options, table_binding)
76 |> restrict(:offset, options, table_binding)
77 |> restrict(:limit, options, table_binding)
80 defp cast_params(params) do
90 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
94 defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
95 where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
98 defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
99 where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
102 defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
103 where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
106 defp restrict(query, :order, %{skip_order: true}, _), do: query
108 defp restrict(query, :order, %{min_id: _}, table_binding) do
111 [{u, table_position(query, table_binding)}],
112 fragment("? asc nulls last", u.id)
116 defp restrict(query, :order, _options, table_binding) do
119 [{u, table_position(query, table_binding)}],
120 fragment("? desc nulls last", u.id)
124 defp restrict(query, :offset, %{offset: offset}, _table_binding) do
125 offset(query, ^offset)
128 defp restrict(query, :limit, options, _table_binding) do
130 case Map.get(options, :limit, @default_limit) do
131 limit when limit < @max_limit -> limit
139 defp restrict(query, _, _, _), do: query
141 defp enforce_order(result, %{min_id: _}) do
146 defp enforce_order(result, _), do: result
148 defp table_position(%Ecto.Query{} = query, binding_name) do
149 Map.get(query.aliases, binding_name, 0)
152 defp table_position(_, _), do: 0