From 21f486c8725ddf625e39c7592eff8bd7b23785bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nicole=20miko=C5=82ajczyk?= Date: Sun, 30 Nov 2025 00:05:24 +0100 Subject: [PATCH] Order favourites and reblogs list from newest to oldest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: nicole mikołajczyk --- changelog.d/order-favourites-reblogs.change | 1 + .../controllers/status_controller.ex | 2 ++ .../controllers/status_controller_test.exs | 21 ++++++++++++++----- 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 changelog.d/order-favourites-reblogs.change diff --git a/changelog.d/order-favourites-reblogs.change b/changelog.d/order-favourites-reblogs.change new file mode 100644 index 000000000..67c235d62 --- /dev/null +++ b/changelog.d/order-favourites-reblogs.change @@ -0,0 +1 @@ +Order favourites and reblogs list from newest to oldest diff --git a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex index 32874d464..e2b7af388 100644 --- a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex @@ -488,6 +488,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do users = User |> Ecto.Query.where([u], u.ap_id in ^likes) + |> Ecto.Query.order_by([u], fragment("array_position(?, ?)", ^likes, u.ap_id)) |> Repo.all() |> Enum.filter(&(not User.blocks?(user, &1))) @@ -523,6 +524,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do users = User |> Ecto.Query.where([u], u.ap_id in ^announces) + |> Ecto.Query.order_by([u], fragment("array_position(?, ?)", ^announces, u.ap_id)) |> Repo.all() |> Enum.filter(&(not User.blocks?(user, &1))) diff --git a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs index 25a17d5c1..f9b81e0de 100644 --- a/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/status_controller_test.exs @@ -1867,18 +1867,29 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do %{activity: activity} end - test "returns users who have favorited the status", %{conn: conn, activity: activity} do - other_user = insert(:user) - {:ok, _} = CommonAPI.favorite(activity.id, other_user) + test "returns users who have favorited the status ordered from newest to oldest", %{ + conn: conn, + activity: activity + } do + [other_user_1, other_user_2] = insert_pair(:user) + [other_user_3, other_user_4] = insert_pair(:user) + + {:ok, _} = CommonAPI.favorite(activity.id, other_user_1) + {:ok, _} = CommonAPI.favorite(activity.id, other_user_3) + {:ok, _} = CommonAPI.favorite(activity.id, other_user_2) + {:ok, _} = CommonAPI.favorite(activity.id, other_user_4) response = conn |> get("/api/v1/statuses/#{activity.id}/favourited_by") |> json_response_and_validate_schema(:ok) - [%{"id" => id}] = response + [%{"id" => id1}, %{"id" => id2}, %{"id" => id3}, %{"id" => id4}] = response - assert id == other_user.id + assert id1 == other_user_4.id + assert id2 == other_user_2.id + assert id3 == other_user_3.id + assert id4 == other_user_1.id end test "returns empty array when status has not been favorited yet", %{