merge-upstream-5.0.1 #66

Merged
778a69cd merged 80 commits from merge-upstream-5.0.1 into main 2024-12-26 12:55:41 +01:00
3 changed files with 16 additions and 5 deletions
Showing only changes of commit 41aa81097d - Show all commits

View file

@ -602,7 +602,7 @@ defmodule Mobilizon.Events do
|> filter_local_or_from_followed_instances_events()
|> filter_public_visibility()
|> event_order(Map.get(args, :sort_by, :match_desc), search_string)
|> Page.build_page(page, limit, :begins_on)
|> Page.build_page(page, limit)
end
@doc """

View file

@ -73,7 +73,7 @@ defmodule Mobilizon.Instances do
query
end
%Page{elements: elements} = paged_instances = Page.build_page(query, page, limit, :domain)
%Page{elements: elements} = paged_instances = Page.build_page(query, page, limit)
%Page{
paged_instances

View file

@ -23,11 +23,22 @@ defmodule Mobilizon.Storage.Page do
`field` is use to define the field that will be used for the count aggregate, which should be the same as the field used for order_by
See https://stackoverflow.com/q/12693089/10204399
"""
@spec build_page(Ecto.Queryable.t(), integer | nil, integer | nil, atom()) :: t(any)
def build_page(query, page, limit, field \\ :id) do
@spec build_page(Ecto.Queryable.t(), integer | nil, integer) :: t(any)
def build_page(query, page, limit) do
count_query =
query
# Exclude select because we add a new one below
|> exclude(:select)
# Exclude order_by for perf
|> exclude(:order_by)
# Exclude preloads to avoid error "cannot preload associations in subquery"
|> exclude(:preload)
|> subquery()
|> select([r], count(fragment("*")))
[total, elements] =
[
fn -> Repo.aggregate(query, :count, field) end,
fn -> Repo.one(count_query) end,
fn -> Repo.all(paginate(query, page, limit)) end
]
|> Enum.map(&Task.async/1)