1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
19 @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
21 def page_keys, do: @page_keys
23 @spec fetch_paginated(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
24 def fetch_paginated(query, params, type \\ :keyset, table_binding \\ nil)
26 def fetch_paginated(query, %{total: true} = params, :keyset, table_binding) do
27 total = Repo.aggregate(query, :count, :id)
31 items: fetch_paginated(query, Map.drop(params, [:total]), :keyset, table_binding)
35 def fetch_paginated(query, params, :keyset, table_binding) do
36 options = cast_params(params)
39 |> paginate(options, :keyset, table_binding)
41 |> enforce_order(options)
44 def fetch_paginated(query, %{total: true} = params, :offset, table_binding) do
47 |> Ecto.Query.exclude(:left_join)
48 |> Repo.aggregate(:count, :id)
52 items: fetch_paginated(query, Map.drop(params, [:total]), :offset, table_binding)
56 def fetch_paginated(query, params, :offset, table_binding) do
57 options = cast_params(params)
60 |> paginate(options, :offset, table_binding)
64 @spec paginate_list(list(), keyword()) :: list()
65 def paginate_list(list, options) do
66 offset = options[:offset] || 0
67 limit = options[:limit] || 0
68 Enum.slice(list, offset, limit)
71 @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
72 def paginate(query, options, method \\ :keyset, table_binding \\ nil)
74 def paginate(query, options, :keyset, table_binding) do
76 |> restrict(:min_id, options, table_binding)
77 |> restrict(:since_id, options, table_binding)
78 |> restrict(:max_id, options, table_binding)
79 |> restrict(:order, options, table_binding)
80 |> restrict(:limit, options, table_binding)
83 def paginate(query, options, :offset, table_binding) do
85 |> restrict(:order, options, table_binding)
86 |> restrict(:offset, options, table_binding)
87 |> restrict(:limit, options, table_binding)
90 defp cast_params(params) do
97 skip_extra_order: :boolean,
101 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
105 defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
106 where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
109 defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
110 where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
113 defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
114 where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
117 defp restrict(query, :order, %{skip_order: true}, _), do: query
119 defp restrict(%{order_bys: [_ | _]} = query, :order, %{skip_extra_order: true}, _), do: query
121 defp restrict(query, :order, %{min_id: _}, table_binding) do
124 [{u, table_position(query, table_binding)}],
125 fragment("? asc nulls last", u.id)
129 defp restrict(query, :order, _options, table_binding) do
132 [{u, table_position(query, table_binding)}],
133 fragment("? desc nulls last", u.id)
137 defp restrict(query, :offset, %{offset: offset}, _table_binding) do
138 offset(query, ^offset)
141 defp restrict(query, :limit, options, _table_binding) do
143 case Map.get(options, :limit, @default_limit) do
144 limit when limit < @max_limit -> limit
152 defp restrict(query, _, _, _), do: query
154 defp enforce_order(result, %{min_id: _}) do
159 defp enforce_order(result, _), do: result
161 defp table_position(%Ecto.Query{} = query, binding_name) do
162 Map.get(query.aliases, binding_name, 0)
165 defp table_position(_, _), do: 0