diff --git a/lib/federation/activity_pub/activity_pub.ex b/lib/federation/activity_pub/activity_pub.ex index 12ab02376..6a23293db 100644 --- a/lib/federation/activity_pub/activity_pub.ex +++ b/lib/federation/activity_pub/activity_pub.ex @@ -135,7 +135,7 @@ defmodule Mobilizon.Federation.ActivityPub do %Page{total: total_events, elements: events} = if actor_id == relay_actor_id do - Events.list_public_local_events(page, limit) + Events.list_public_local_events(page, limit, :publish_at, :desc) else Events.list_public_events_for_actor(actor, page, limit) end diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 8796ae442..3fa4cbaf9 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -387,13 +387,15 @@ defmodule Mobilizon.Events do |> Repo.stream() end - @spec list_public_local_events(integer | nil, integer | nil) :: Page.t(Event.t()) - def list_public_local_events(page \\ nil, limit \\ nil) do + @spec list_public_local_events(integer | nil, integer | nil, atom | nil, atom | nil) :: + Page.t(Event.t()) + def list_public_local_events(page \\ nil, limit \\ nil, sort \\ nil, direction \\ nil) do Event |> filter_public_visibility() |> filter_draft() |> filter_local() |> preload_for_event() + |> event_order_by(sort, direction) |> Page.build_page(page, limit) end 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 14da864de..149055e31 100644 --- a/lib/service/export/common.ex +++ b/lib/service/export/common.ex @@ -96,8 +96,8 @@ 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) - %Page{elements: posts} = Posts.list_public_local_posts(1, limit) + %Page{elements: events} = Events.list_public_local_events(1, limit, :begins_on, :desc) + %Page{elements: posts} = Posts.list_public_local_posts(1, limit, :publish_at, :desc) {:ok, events, posts} end diff --git a/lib/service/export/feed.ex b/lib/service/export/feed.ex index 80ec7dcb4..cc1a0294a 100644 --- a/lib/service/export/feed.ex +++ b/lib/service/export/feed.ex @@ -21,7 +21,7 @@ defmodule Mobilizon.Service.Export.Feed do @behaviour Cachable - @item_limit 500 + @item_limit 5000 @spec version :: String.t() defp version, do: Config.instance_version()