Merge branch '1149-oban-job-queue' 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(:offset, options)
68 |> restrict(:limit, options)
69 end
70
71 defp cast_params(params) do
72 param_types = %{
73 min_id: :string,
74 since_id: :string,
75 max_id: :string,
76 offset: :integer,
77 limit: :integer
78 }
79
80 params =
81 Enum.reduce(params, %{}, fn
82 {key, _value}, acc when is_atom(key) -> Map.drop(acc, [key])
83 {key, value}, acc -> Map.put(acc, key, value)
84 end)
85
86 changeset = cast({%{}, param_types}, params, Map.keys(param_types))
87 changeset.changes
88 end
89
90 defp restrict(query, :min_id, %{min_id: min_id}) do
91 where(query, [q], q.id > ^min_id)
92 end
93
94 defp restrict(query, :since_id, %{since_id: since_id}) do
95 where(query, [q], q.id > ^since_id)
96 end
97
98 defp restrict(query, :max_id, %{max_id: max_id}) do
99 where(query, [q], q.id < ^max_id)
100 end
101
102 defp restrict(query, :order, %{min_id: _}) do
103 order_by(query, [u], fragment("? asc nulls last", u.id))
104 end
105
106 defp restrict(query, :order, _options) do
107 order_by(query, [u], fragment("? desc nulls last", u.id))
108 end
109
110 defp restrict(query, :offset, %{offset: offset}) do
111 offset(query, ^offset)
112 end
113
114 defp restrict(query, :limit, options) do
115 limit = Map.get(options, :limit, @default_limit)
116
117 query
118 |> limit(^limit)
119 end
120
121 defp restrict(query, _, _), do: query
122
123 defp enforce_order(result, %{min_id: _}) do
124 result
125 |> Enum.reverse()
126 end
127
128 defp enforce_order(result, _), do: result
129 end