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