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