logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

pagination.ex (4559B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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_list(list(), keyword()) :: list()
  49. def paginate_list(list, options) do
  50. offset = options[:offset] || 0
  51. limit = options[:limit] || 0
  52. Enum.slice(list, offset, limit)
  53. end
  54. @spec paginate(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
  55. def paginate(query, options, method \\ :keyset, table_binding \\ nil)
  56. def paginate(query, options, :keyset, table_binding) do
  57. query
  58. |> restrict(:min_id, options, table_binding)
  59. |> restrict(:since_id, options, table_binding)
  60. |> restrict(:max_id, options, table_binding)
  61. |> restrict(:order, options, table_binding)
  62. |> restrict(:limit, options, table_binding)
  63. end
  64. def paginate(query, options, :offset, table_binding) do
  65. query
  66. |> restrict(:order, options, table_binding)
  67. |> restrict(:offset, options, table_binding)
  68. |> restrict(:limit, options, table_binding)
  69. end
  70. defp cast_params(params) do
  71. param_types = %{
  72. min_id: :string,
  73. since_id: :string,
  74. max_id: :string,
  75. offset: :integer,
  76. limit: :integer,
  77. skip_extra_order: :boolean,
  78. skip_order: :boolean
  79. }
  80. changeset = cast({%{}, param_types}, params, Map.keys(param_types))
  81. changeset.changes
  82. end
  83. defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
  84. where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
  85. end
  86. defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
  87. where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
  88. end
  89. defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
  90. where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
  91. end
  92. defp restrict(query, :order, %{skip_order: true}, _), do: query
  93. defp restrict(%{order_bys: [_ | _]} = query, :order, %{skip_extra_order: true}, _), do: query
  94. defp restrict(query, :order, %{min_id: _}, table_binding) do
  95. order_by(
  96. query,
  97. [{u, table_position(query, table_binding)}],
  98. fragment("? asc nulls last", u.id)
  99. )
  100. end
  101. defp restrict(query, :order, _options, table_binding) do
  102. order_by(
  103. query,
  104. [{u, table_position(query, table_binding)}],
  105. fragment("? desc nulls last", u.id)
  106. )
  107. end
  108. defp restrict(query, :offset, %{offset: offset}, _table_binding) do
  109. offset(query, ^offset)
  110. end
  111. defp restrict(query, :limit, options, _table_binding) do
  112. limit =
  113. case Map.get(options, :limit, @default_limit) do
  114. limit when limit < @max_limit -> limit
  115. _ -> @max_limit
  116. end
  117. query
  118. |> limit(^limit)
  119. end
  120. defp restrict(query, _, _, _), do: query
  121. defp enforce_order(result, %{min_id: _}) do
  122. result
  123. |> Enum.reverse()
  124. end
  125. defp enforce_order(result, _), do: result
  126. defp table_position(%Ecto.Query{} = query, binding_name) do
  127. Map.get(query.aliases, binding_name, 0)
  128. end
  129. defp table_position(_, _), do: 0
  130. end