diff --git a/lib/mobilizon/posts/posts.ex b/lib/mobilizon/posts/posts.ex index 5fe60b433..97387b709 100644 --- a/lib/mobilizon/posts/posts.ex +++ b/lib/mobilizon/posts/posts.ex @@ -22,11 +22,13 @@ defmodule Mobilizon.Posts do :private ]) - @spec list_public_local_posts(integer | nil, integer | nil) :: Page.t(Post.t()) - def list_public_local_posts(page \\ nil, limit \\ nil) do + @spec list_public_local_posts(integer | nil, integer | nil, atom | nil, atom | nil) :: + Page.t(Post.t()) + def list_public_local_posts(page \\ nil, limit \\ nil, sort \\ nil, direction \\ nil) do Post |> filter_public() |> filter_local() + |> order_posts(sort, direction) |> preload_post_associations() |> Page.build_page(page, limit) end @@ -171,4 +173,13 @@ defmodule Mobilizon.Posts do defp preload_post_associations(query, associations \\ @post_preloads) do preload(query, ^associations) 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 diff --git a/lib/service/export/common.ex b/lib/service/export/common.ex index 9f3175642..149055e31 100644 --- a/lib/service/export/common.ex +++ b/lib/service/export/common.ex @@ -97,7 +97,7 @@ defmodule Mobilizon.Service.Export.Common do @spec fetch_instance_public_content(integer()) :: {:ok, list(Event.t()), list(Post.t())} def fetch_instance_public_content(limit) do %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} end