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.

166 lines
4.4KB

  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_extra_order: :boolean,
  77. skip_order: :boolean
  78. }
  79. changeset = cast({%{}, param_types}, params, Map.keys(param_types))
  80. changeset.changes
  81. end
  82. defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
  83. where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
  84. end
  85. defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
  86. where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
  87. end
  88. defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
  89. where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
  90. end
  91. defp restrict(query, :order, %{skip_order: true}, _), do: query
  92. defp restrict(%{order_bys: [_ | _]} = query, :order, %{skip_extra_order: true}, _), do: query
  93. defp restrict(query, :order, %{min_id: _}, table_binding) do
  94. order_by(
  95. query,
  96. [{u, table_position(query, table_binding)}],
  97. fragment("? asc nulls last", u.id)
  98. )
  99. end
  100. defp restrict(query, :order, _options, table_binding) do
  101. order_by(
  102. query,
  103. [{u, table_position(query, table_binding)}],
  104. fragment("? desc nulls last", u.id)
  105. )
  106. end
  107. defp restrict(query, :offset, %{offset: offset}, _table_binding) do
  108. offset(query, ^offset)
  109. end
  110. defp restrict(query, :limit, options, _table_binding) do
  111. limit =
  112. case Map.get(options, :limit, @default_limit) do
  113. limit when limit < @max_limit -> limit
  114. _ -> @max_limit
  115. end
  116. query
  117. |> limit(^limit)
  118. end
  119. defp restrict(query, _, _, _), do: query
  120. defp enforce_order(result, %{min_id: _}) do
  121. result
  122. |> Enum.reverse()
  123. end
  124. defp enforce_order(result, _), do: result
  125. defp table_position(%Ecto.Query{} = query, binding_name) do
  126. Map.get(query.aliases, binding_name, 0)
  127. end
  128. defp table_position(_, _), do: 0
  129. end