Use atoms as keys in `ActivityPub.fetch_*` functions options
[akkoma] / lib / pleroma / pagination.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
20 @spec fetch_paginated(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
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 @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
62 def paginate(query, options, method \\ :keyset, table_binding \\ nil)
63
64 def paginate(query, options, :keyset, table_binding) do
65 query
66 |> restrict(:min_id, options, table_binding)
67 |> restrict(:since_id, options, table_binding)
68 |> restrict(:max_id, options, table_binding)
69 |> restrict(:order, options, table_binding)
70 |> restrict(:limit, options, table_binding)
71 end
72
73 def paginate(query, options, :offset, table_binding) do
74 query
75 |> restrict(:order, options, table_binding)
76 |> restrict(:offset, options, table_binding)
77 |> restrict(:limit, options, table_binding)
78 end
79
80 defp cast_params(params) do
81 param_types = %{
82 min_id: :string,
83 since_id: :string,
84 max_id: :string,
85 offset: :integer,
86 limit: :integer,
87 skip_order: :boolean
88 }
89
90 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
91 changeset.changes
92 end
93
94 defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
95 where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
96 end
97
98 defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
99 where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
100 end
101
102 defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
103 where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
104 end
105
106 defp restrict(query, :order, %{skip_order: true}, _), do: query
107
108 defp restrict(query, :order, %{min_id: _}, table_binding) do
109 order_by(
110 query,
111 [{u, table_position(query, table_binding)}],
112 fragment("? asc nulls last", u.id)
113 )
114 end
115
116 defp restrict(query, :order, _options, table_binding) do
117 order_by(
118 query,
119 [{u, table_position(query, table_binding)}],
120 fragment("? desc nulls last", u.id)
121 )
122 end
123
124 defp restrict(query, :offset, %{offset: offset}, _table_binding) do
125 offset(query, ^offset)
126 end
127
128 defp restrict(query, :limit, options, _table_binding) do
129 limit =
130 case Map.get(options, :limit, @default_limit) do
131 limit when limit < @max_limit -> limit
132 _ -> @max_limit
133 end
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