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