Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.3KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Pagination do
  5. @moduledoc """
  6. Implements Mastodon-compatible pagination.
  7. """
  8. import Ecto.Query
  9. import Ecto.Changeset
  10. alias Pleroma.Repo
  11. @type type :: :keyset | :offset
  12. @default_limit 20
  13. @max_limit 40
  14. @page_keys ["max_id", "min_id", "limit", "since_id", "order"]
  15. def page_keys, do: @page_keys
  16. @spec fetch_paginated(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
  17. def fetch_paginated(query, params, type \\ :keyset, table_binding \\ nil)
  18. def fetch_paginated(query, %{total: true} = params, :keyset, table_binding) do
  19. total = Repo.aggregate(query, :count, :id)
  20. %{
  21. total: total,
  22. items: fetch_paginated(query, Map.drop(params, [:total]), :keyset, table_binding)
  23. }
  24. end
  25. def fetch_paginated(query, params, :keyset, table_binding) do
  26. options = cast_params(params)
  27. query
  28. |> paginate(options, :keyset, table_binding)
  29. |> Repo.all()
  30. |> enforce_order(options)
  31. end
  32. def fetch_paginated(query, %{total: true} = params, :offset, table_binding) do
  33. total =
  34. query
  35. |> Ecto.Query.exclude(:left_join)
  36. |> Repo.aggregate(:count, :id)
  37. %{
  38. total: total,
  39. items: fetch_paginated(query, Map.drop(params, [:total]), :offset, table_binding)
  40. }
  41. end
  42. def fetch_paginated(query, params, :offset, table_binding) do
  43. options = cast_params(params)
  44. query
  45. |> paginate(options, :offset, table_binding)
  46. |> Repo.all()
  47. end
  48. @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
  49. def paginate(query, options, method \\ :keyset, table_binding \\ nil)
  50. def paginate(list, options, _method, _table_binding) when is_list(list) do
  51. offset = options[:offset] || 0
  52. limit = options[:limit] || 0
  53. Enum.slice(list, offset, limit)
  54. end
  55. def paginate(query, options, :keyset, table_binding) do
  56. query
  57. |> restrict(:min_id, options, table_binding)
  58. |> restrict(:since_id, options, table_binding)
  59. |> restrict(:max_id, options, table_binding)
  60. |> restrict(:order, options, table_binding)
  61. |> restrict(:limit, options, table_binding)
  62. end
  63. def paginate(query, options, :offset, table_binding) do
  64. query
  65. |> restrict(:order, options, table_binding)
  66. |> restrict(:offset, options, table_binding)
  67. |> restrict(:limit, options, table_binding)
  68. end
  69. defp cast_params(params) do
  70. param_types = %{
  71. min_id: :string,
  72. since_id: :string,
  73. max_id: :string,
  74. offset: :integer,
  75. limit: :integer,
  76. skip_order: :boolean
  77. }
  78. changeset = cast({%{}, param_types}, params, Map.keys(param_types))
  79. changeset.changes
  80. end
  81. defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
  82. where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
  83. end
  84. defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
  85. where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
  86. end
  87. defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
  88. where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
  89. end
  90. defp restrict(query, :order, %{skip_order: true}, _), do: query
  91. defp restrict(query, :order, %{min_id: _}, table_binding) do
  92. order_by(
  93. query,
  94. [{u, table_position(query, table_binding)}],
  95. fragment("? asc nulls last", u.id)
  96. )
  97. end
  98. defp restrict(query, :order, _options, table_binding) do
  99. order_by(
  100. query,
  101. [{u, table_position(query, table_binding)}],
  102. fragment("? desc nulls last", u.id)
  103. )
  104. end
  105. defp restrict(query, :offset, %{offset: offset}, _table_binding) do
  106. offset(query, ^offset)
  107. end
  108. defp restrict(query, :limit, options, _table_binding) do
  109. limit =
  110. case Map.get(options, :limit, @default_limit) do
  111. limit when limit < @max_limit -> limit
  112. _ -> @max_limit
  113. end
  114. query
  115. |> limit(^limit)
  116. end
  117. defp restrict(query, _, _, _), do: query
  118. defp enforce_order(result, %{min_id: _}) do
  119. result
  120. |> Enum.reverse()
  121. end
  122. defp enforce_order(result, _), do: result
  123. defp table_position(%Ecto.Query{} = query, binding_name) do
  124. Map.get(query.aliases, binding_name, 0)
  125. end
  126. defp table_position(_, _), do: 0
  127. end