2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-03-17 19:32:23 -06:00
|
|
|
defmodule Pleroma.Pagination do
|
|
|
|
@moduledoc """
|
|
|
|
Implements Mastodon-compatible pagination.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
2019-12-30 01:30:20 -07:00
|
|
|
@type type :: :keyset | :offset
|
|
|
|
|
2019-03-17 19:32:23 -06:00
|
|
|
@default_limit 20
|
2020-02-28 07:29:53 -07:00
|
|
|
@max_limit 40
|
2020-06-05 13:18:29 -06:00
|
|
|
@page_keys ["max_id", "min_id", "limit", "since_id", "order"]
|
|
|
|
|
|
|
|
def page_keys, do: @page_keys
|
2019-07-11 07:55:31 -06:00
|
|
|
|
2019-12-30 01:30:20 -07:00
|
|
|
@spec fetch_paginated(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
|
2019-11-18 06:56:25 -07:00
|
|
|
def fetch_paginated(query, params, type \\ :keyset, table_binding \\ nil)
|
2019-07-11 07:55:31 -06:00
|
|
|
|
2020-06-04 11:33:16 -06:00
|
|
|
def fetch_paginated(query, %{total: true} = params, :keyset, table_binding) do
|
2019-09-02 13:48:52 -06:00
|
|
|
total = Repo.aggregate(query, :count, :id)
|
2019-03-17 19:32:23 -06:00
|
|
|
|
2019-09-02 13:48:52 -06:00
|
|
|
%{
|
|
|
|
total: total,
|
2020-06-04 11:33:16 -06:00
|
|
|
items: fetch_paginated(query, Map.drop(params, [:total]), :keyset, table_binding)
|
2019-09-02 13:48:52 -06:00
|
|
|
}
|
2019-03-17 19:32:23 -06:00
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
def fetch_paginated(query, params, :keyset, table_binding) do
|
2019-07-11 07:55:31 -06:00
|
|
|
options = cast_params(params)
|
2019-09-03 04:58:27 -06:00
|
|
|
|
|
|
|
query
|
2019-11-18 06:56:25 -07:00
|
|
|
|> paginate(options, :keyset, table_binding)
|
2019-09-03 04:58:27 -06:00
|
|
|
|> Repo.all()
|
|
|
|
|> enforce_order(options)
|
|
|
|
end
|
|
|
|
|
2020-06-04 11:33:16 -06:00
|
|
|
def fetch_paginated(query, %{total: true} = params, :offset, table_binding) do
|
2019-12-06 01:17:24 -07:00
|
|
|
total =
|
|
|
|
query
|
|
|
|
|> Ecto.Query.exclude(:left_join)
|
|
|
|
|> Repo.aggregate(:count, :id)
|
2019-07-11 07:55:31 -06:00
|
|
|
|
2019-09-02 13:48:52 -06:00
|
|
|
%{
|
|
|
|
total: total,
|
2020-06-04 11:33:16 -06:00
|
|
|
items: fetch_paginated(query, Map.drop(params, [:total]), :offset, table_binding)
|
2019-09-02 13:48:52 -06:00
|
|
|
}
|
2019-07-11 07:55:31 -06:00
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
def fetch_paginated(query, params, :offset, table_binding) do
|
2019-09-03 04:58:27 -06:00
|
|
|
options = cast_params(params)
|
|
|
|
|
|
|
|
query
|
2019-11-18 06:56:25 -07:00
|
|
|
|> paginate(options, :offset, table_binding)
|
2019-09-03 04:58:27 -06:00
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2019-12-30 01:30:20 -07:00
|
|
|
@spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
|
2019-11-18 06:56:25 -07:00
|
|
|
def paginate(query, options, method \\ :keyset, table_binding \\ nil)
|
2019-07-11 07:55:31 -06:00
|
|
|
|
2020-06-19 07:14:06 -06:00
|
|
|
def paginate(list, options, _method, _table_binding) when is_list(list) do
|
|
|
|
offset = options[:offset] || 0
|
|
|
|
limit = options[:limit] || 0
|
|
|
|
Enum.slice(list, offset, limit)
|
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
def paginate(query, options, :keyset, table_binding) do
|
2019-03-17 19:32:23 -06:00
|
|
|
query
|
2019-11-18 06:56:25 -07:00
|
|
|
|> restrict(:min_id, options, table_binding)
|
|
|
|
|> restrict(:since_id, options, table_binding)
|
|
|
|
|> restrict(:max_id, options, table_binding)
|
|
|
|
|> restrict(:order, options, table_binding)
|
|
|
|
|> restrict(:limit, options, table_binding)
|
2019-03-17 19:32:23 -06:00
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
def paginate(query, options, :offset, table_binding) do
|
2019-07-11 07:55:31 -06:00
|
|
|
query
|
2019-11-18 06:56:25 -07:00
|
|
|
|> restrict(:order, options, table_binding)
|
|
|
|
|> restrict(:offset, options, table_binding)
|
|
|
|
|> restrict(:limit, options, table_binding)
|
2019-07-11 07:55:31 -06:00
|
|
|
end
|
|
|
|
|
2019-03-17 19:32:23 -06:00
|
|
|
defp cast_params(params) do
|
|
|
|
param_types = %{
|
|
|
|
min_id: :string,
|
|
|
|
since_id: :string,
|
|
|
|
max_id: :string,
|
2019-07-11 07:55:31 -06:00
|
|
|
offset: :integer,
|
2019-11-18 11:34:54 -07:00
|
|
|
limit: :integer,
|
2021-03-07 01:33:21 -07:00
|
|
|
skip_extra_order: :boolean,
|
2019-11-18 11:34:54 -07:00
|
|
|
skip_order: :boolean
|
2019-03-17 19:32:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
changeset = cast({%{}, param_types}, params, Map.keys(param_types))
|
|
|
|
changeset.changes
|
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
|
|
|
|
where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
|
2019-03-17 19:32:23 -06:00
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
|
|
|
|
where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
|
2019-03-17 19:32:23 -06:00
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
|
|
|
|
where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
|
2019-03-17 19:32:23 -06:00
|
|
|
end
|
|
|
|
|
2019-11-18 11:34:54 -07:00
|
|
|
defp restrict(query, :order, %{skip_order: true}, _), do: query
|
|
|
|
|
2021-03-07 01:33:21 -07:00
|
|
|
defp restrict(%{order_bys: [_ | _]} = query, :order, %{skip_extra_order: true}, _), do: query
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
defp restrict(query, :order, %{min_id: _}, table_binding) do
|
|
|
|
order_by(
|
|
|
|
query,
|
|
|
|
[{u, table_position(query, table_binding)}],
|
|
|
|
fragment("? asc nulls last", u.id)
|
|
|
|
)
|
2019-03-17 19:32:23 -06:00
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
defp restrict(query, :order, _options, table_binding) do
|
|
|
|
order_by(
|
|
|
|
query,
|
|
|
|
[{u, table_position(query, table_binding)}],
|
|
|
|
fragment("? desc nulls last", u.id)
|
|
|
|
)
|
2019-03-17 19:32:23 -06:00
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
defp restrict(query, :offset, %{offset: offset}, _table_binding) do
|
2019-07-11 07:55:31 -06:00
|
|
|
offset(query, ^offset)
|
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
defp restrict(query, :limit, options, _table_binding) do
|
2020-02-28 07:29:53 -07:00
|
|
|
limit =
|
|
|
|
case Map.get(options, :limit, @default_limit) do
|
|
|
|
limit when limit < @max_limit -> limit
|
|
|
|
_ -> @max_limit
|
|
|
|
end
|
2019-03-17 19:32:23 -06:00
|
|
|
|
|
|
|
query
|
|
|
|
|> limit(^limit)
|
|
|
|
end
|
|
|
|
|
2019-11-18 06:56:25 -07:00
|
|
|
defp restrict(query, _, _, _), do: query
|
2019-03-17 19:32:23 -06:00
|
|
|
|
|
|
|
defp enforce_order(result, %{min_id: _}) do
|
|
|
|
result
|
|
|
|
|> Enum.reverse()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp enforce_order(result, _), do: result
|
2019-11-18 06:56:25 -07:00
|
|
|
|
|
|
|
defp table_position(%Ecto.Query{} = query, binding_name) do
|
|
|
|
Map.get(query.aliases, binding_name, 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp table_position(_, _), do: 0
|
2019-03-17 19:32:23 -06:00
|
|
|
end
|