fix(feeds): make sure posts for feeds are ordered by publication date desc

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2023-12-20 09:26:28 +01:00
parent e73fd9b370
commit 3c75856149
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
2 changed files with 14 additions and 3 deletions

View file

@ -22,11 +22,13 @@ defmodule Mobilizon.Posts do
:private :private
]) ])
@spec list_public_local_posts(integer | nil, integer | nil) :: Page.t(Post.t()) @spec list_public_local_posts(integer | nil, integer | nil, atom | nil, atom | nil) ::
def list_public_local_posts(page \\ nil, limit \\ nil) do Page.t(Post.t())
def list_public_local_posts(page \\ nil, limit \\ nil, sort \\ nil, direction \\ nil) do
Post Post
|> filter_public() |> filter_public()
|> filter_local() |> filter_local()
|> order_posts(sort, direction)
|> preload_post_associations() |> preload_post_associations()
|> Page.build_page(page, limit) |> Page.build_page(page, limit)
end end
@ -171,4 +173,13 @@ defmodule Mobilizon.Posts do
defp preload_post_associations(query, associations \\ @post_preloads) do defp preload_post_associations(query, associations \\ @post_preloads) do
preload(query, ^associations) preload(query, ^associations)
end end
@spec order_posts(Ecto.Queryable.t(), atom | nil, atom | nil) :: Ecto.Queryable.t()
def order_posts(query, nil, _direction), do: query
def order_posts(query, _sort, nil), do: query
def order_posts(query, sort, direction) do
order_by_param = Keyword.new([{direction, sort}])
order_by(query, ^order_by_param)
end
end end

View file

@ -97,7 +97,7 @@ defmodule Mobilizon.Service.Export.Common do
@spec fetch_instance_public_content(integer()) :: {:ok, list(Event.t()), list(Post.t())} @spec fetch_instance_public_content(integer()) :: {:ok, list(Event.t()), list(Post.t())}
def fetch_instance_public_content(limit) do def fetch_instance_public_content(limit) do
%Page{elements: events} = Events.list_public_local_events(1, limit, :begins_on, :desc) %Page{elements: events} = Events.list_public_local_events(1, limit, :begins_on, :desc)
%Page{elements: posts} = Posts.list_public_local_posts(1, limit) %Page{elements: posts} = Posts.list_public_local_posts(1, limit, :publish_at, :desc)
{:ok, events, posts} {:ok, events, posts}
end end