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