projects
/
akkoma
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
[#1094] Rate-limited follow & unfollow actions.
[akkoma]
/
lib
/
pleroma
/
pagination.ex
diff --git
a/lib/pleroma/pagination.ex
b/lib/pleroma/pagination.ex
index 7c864deef8feb05b79d8001bba03389c94e77294..2b869ccdcc494c2a9bb158772da59c1167aa7529 100644
(file)
--- a/
lib/pleroma/pagination.ex
+++ b/
lib/pleroma/pagination.ex
@@
-1,3
+1,7
@@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
defmodule Pleroma.Pagination do
@moduledoc """
Implements Mastodon-compatible pagination.
defmodule Pleroma.Pagination do
@moduledoc """
Implements Mastodon-compatible pagination.
@@
-10,16
+14,28
@@
defmodule Pleroma.Pagination do
@default_limit 20
@default_limit 20
- def fetch_paginated(query, params) do
+ def fetch_paginated(query, params, type \\ :keyset)
+
+ def fetch_paginated(query, params, :keyset) do
options = cast_params(params)
query
options = cast_params(params)
query
- |> paginate(options)
+ |> paginate(options
, :keyset
)
|> Repo.all()
|> enforce_order(options)
end
|> Repo.all()
|> enforce_order(options)
end
- def paginate(query, options) do
+ def fetch_paginated(query, params, :offset) do
+ options = cast_params(params)
+
+ query
+ |> paginate(options, :offset)
+ |> Repo.all()
+ end
+
+ def paginate(query, options, method \\ :keyset)
+
+ def paginate(query, options, :keyset) do
query
|> restrict(:min_id, options)
|> restrict(:since_id, options)
query
|> restrict(:min_id, options)
|> restrict(:since_id, options)
@@
-28,14
+44,27
@@
defmodule Pleroma.Pagination do
|> restrict(:limit, options)
end
|> restrict(:limit, options)
end
+ def paginate(query, options, :offset) do
+ query
+ |> restrict(:offset, options)
+ |> restrict(:limit, options)
+ end
+
defp cast_params(params) do
param_types = %{
min_id: :string,
since_id: :string,
max_id: :string,
defp cast_params(params) do
param_types = %{
min_id: :string,
since_id: :string,
max_id: :string,
+ offset: :integer,
limit: :integer
}
limit: :integer
}
+ params =
+ Enum.reduce(params, %{}, fn
+ {key, _value}, acc when is_atom(key) -> Map.drop(acc, [key])
+ {key, value}, acc -> Map.put(acc, key, value)
+ end)
+
changeset = cast({%{}, param_types}, params, Map.keys(param_types))
changeset.changes
end
changeset = cast({%{}, param_types}, params, Map.keys(param_types))
changeset.changes
end
@@
-60,6
+89,10
@@
defmodule Pleroma.Pagination do
order_by(query, [u], fragment("? desc nulls last", u.id))
end
order_by(query, [u], fragment("? desc nulls last", u.id))
end
+ defp restrict(query, :offset, %{offset: offset}) do
+ offset(query, ^offset)
+ end
+
defp restrict(query, :limit, options) do
limit = Map.get(options, :limit, @default_limit)
defp restrict(query, :limit, options) do
limit = Map.get(options, :limit, @default_limit)