Merge branch 'fix/minor-doc-fix' into 'develop'
[akkoma] / lib / pleroma / pagination.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Pagination do
6 @moduledoc """
7 Implements Mastodon-compatible pagination.
8 """
9
10 import Ecto.Query
11 import Ecto.Changeset
12
13 alias Pleroma.Repo
14
15 @type type :: :keyset | :offset
16
17 @default_limit 20
18 @max_limit 40
19 @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
20
21 def page_keys, do: @page_keys
22
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)
25
26 def fetch_paginated(query, %{total: true} = params, :keyset, table_binding) do
27 total = Repo.aggregate(query, :count, :id)
28
29 %{
30 total: total,
31 items: fetch_paginated(query, Map.drop(params, [:total]), :keyset, table_binding)
32 }
33 end
34
35 def fetch_paginated(query, params, :keyset, table_binding) do
36 options = cast_params(params)
37
38 query
39 |> paginate(options, :keyset, table_binding)
40 |> Repo.all()
41 |> enforce_order(options)
42 end
43
44 def fetch_paginated(query, %{total: true} = params, :offset, table_binding) do
45 total =
46 query
47 |> Ecto.Query.exclude(:left_join)
48 |> Repo.aggregate(:count, :id)
49
50 %{
51 total: total,
52 items: fetch_paginated(query, Map.drop(params, [:total]), :offset, table_binding)
53 }
54 end
55
56 def fetch_paginated(query, params, :offset, table_binding) do
57 options = cast_params(params)
58
59 query
60 |> paginate(options, :offset, table_binding)
61 |> Repo.all()
62 end
63
64 @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
65 def paginate(query, options, method \\ :keyset, table_binding \\ nil)
66
67 def paginate(list, options, _method, _table_binding) when is_list(list) do
68 offset = options[:offset] || 0
69 limit = options[:limit] || 0
70 Enum.slice(list, offset, limit)
71 end
72
73 def paginate(query, options, :keyset, table_binding) do
74 query
75 |> restrict(:min_id, options, table_binding)
76 |> restrict(:since_id, options, table_binding)
77 |> restrict(:max_id, options, table_binding)
78 |> restrict(:order, options, table_binding)
79 |> restrict(:limit, options, table_binding)
80 end
81
82 def paginate(query, options, :offset, table_binding) do
83 query
84 |> restrict(:order, options, table_binding)
85 |> restrict(:offset, options, table_binding)
86 |> restrict(:limit, options, table_binding)
87 end
88
89 defp cast_params(params) do
90 param_types = %{
91 min_id: :string,
92 since_id: :string,
93 max_id: :string,
94 offset: :integer,
95 limit: :integer,
96 skip_order: :boolean
97 }
98
99 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
100 changeset.changes
101 end
102
103 defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
104 where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
105 end
106
107 defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
108 where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
109 end
110
111 defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
112 where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
113 end
114
115 defp restrict(query, :order, %{skip_order: true}, _), do: query
116
117 defp restrict(query, :order, %{min_id: _}, table_binding) do
118 order_by(
119 query,
120 [{u, table_position(query, table_binding)}],
121 fragment("? asc nulls last", u.id)
122 )
123 end
124
125 defp restrict(query, :order, _options, table_binding) do
126 order_by(
127 query,
128 [{u, table_position(query, table_binding)}],
129 fragment("? desc nulls last", u.id)
130 )
131 end
132
133 defp restrict(query, :offset, %{offset: offset}, _table_binding) do
134 offset(query, ^offset)
135 end
136
137 defp restrict(query, :limit, options, _table_binding) do
138 limit =
139 case Map.get(options, :limit, @default_limit) do
140 limit when limit < @max_limit -> limit
141 _ -> @max_limit
142 end
143
144 query
145 |> limit(^limit)
146 end
147
148 defp restrict(query, _, _, _), do: query
149
150 defp enforce_order(result, %{min_id: _}) do
151 result
152 |> Enum.reverse()
153 end
154
155 defp enforce_order(result, _), do: result
156
157 defp table_position(%Ecto.Query{} = query, binding_name) do
158 Map.get(query.aliases, binding_name, 0)
159 end
160
161 defp table_position(_, _), do: 0
162 end