From 90e2d835e66460b03c48ba8a789361496e215f4a Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Mon, 30 Jan 2023 12:50:45 +0000 Subject: [PATCH] Add tests for follow request pagination --- lib/pleroma/following_relationship.ex | 7 +++--- lib/pleroma/user.ex | 2 ++ .../operations/follow_request_operation.ex | 5 ++--- .../controllers/follow_request_controller.ex | 7 +++--- .../follow_request_controller_test.exs | 22 +++++++++++++++++++ 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/pleroma/following_relationship.ex b/lib/pleroma/following_relationship.ex index d2515c240..9e75458e5 100644 --- a/lib/pleroma/following_relationship.ex +++ b/lib/pleroma/following_relationship.ex @@ -157,12 +157,11 @@ defmodule Pleroma.FollowingRelationship do def get_follow_requests_query(%User{id: id}) do __MODULE__ - |> join(:inner, [r], f in assoc(r, :follower)) + |> join(:inner, [r], f in assoc(r, :follower), as: :follower) |> where([r], r.state == ^:follow_pending) |> where([r], r.following_id == ^id) - |> where([r, f], f.is_active == true) - |> select([r, f], f) - |> order_by([r, f], r.inserted_at) + |> where([r, follower: f], f.is_active == true) + |> select([r, follower: f], f) end def following?(%User{id: follower_id}, %User{id: followed_id}) do diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index e5ec1a2c9..1572a895e 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -274,10 +274,12 @@ defmodule Pleroma.User do defdelegate following?(follower, followed), to: FollowingRelationship defdelegate following_ap_ids(user), to: FollowingRelationship defdelegate get_follow_requests_query(user), to: FollowingRelationship + def get_follow_requests(user) do get_follow_requests_query(user) |> Repo.all() end + defdelegate search(query, opts \\ []), to: User.Search @doc """ diff --git a/lib/pleroma/web/api_spec/operations/follow_request_operation.ex b/lib/pleroma/web/api_spec/operations/follow_request_operation.ex index ddaae0763..d6f59191b 100644 --- a/lib/pleroma/web/api_spec/operations/follow_request_operation.ex +++ b/lib/pleroma/web/api_spec/operations/follow_request_operation.ex @@ -66,11 +66,11 @@ defmodule Pleroma.Web.ApiSpec.FollowRequestOperation do defp pagination_params do [ - Operation.parameter(:max_id, :query, :integer, "Return items older than this ID"), + Operation.parameter(:max_id, :query, :string, "Return items older than this ID"), Operation.parameter( :since_id, :query, - :integer, + :string, "Return the oldest items newer than this ID" ), Operation.parameter( @@ -81,5 +81,4 @@ defmodule Pleroma.Web.ApiSpec.FollowRequestOperation do ) ] end - end diff --git a/lib/pleroma/web/mastodon_api/controllers/follow_request_controller.ex b/lib/pleroma/web/mastodon_api/controllers/follow_request_controller.ex index 25246c210..e534d0388 100644 --- a/lib/pleroma/web/mastodon_api/controllers/follow_request_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/follow_request_controller.ex @@ -4,8 +4,10 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestController do use Pleroma.Web, :controller + import Pleroma.Web.ControllerHelper, - only: [add_link_headers: 2] + only: [add_link_headers: 2] + alias Pleroma.User alias Pleroma.Web.CommonAPI alias Pleroma.Web.Plugs.OAuthScopesPlug @@ -27,11 +29,10 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestController do @doc "GET /api/v1/follow_requests" def index(%{assigns: %{user: followed}} = conn, params) do - params = Map.put(params, :id_type, :integer) follow_requests = followed |> User.get_follow_requests_query() - |> Pagination.fetch_paginated(params) + |> Pagination.fetch_paginated(params, :keyset, :follower) conn |> add_link_headers(follow_requests) diff --git a/test/pleroma/web/mastodon_api/controllers/follow_request_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/follow_request_controller_test.exs index 069ffb3d6..e3f59d886 100644 --- a/test/pleroma/web/mastodon_api/controllers/follow_request_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/follow_request_controller_test.exs @@ -10,6 +10,11 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do import Pleroma.Factory + defp extract_next_link_header(header) do + [_, next_link] = Regex.run(~r{<(?.*)>; rel="next"}, header) + next_link + end + describe "locked accounts" do setup do user = insert(:user, is_locked: true) @@ -31,6 +36,23 @@ defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do assert to_string(other_user.id) == relationship["id"] end + test "/api/v1/follow_requests paginates", %{user: user, conn: conn} do + for _ <- 1..21 do + other_user = insert(:user) + {:ok, _, _, _activity} = CommonAPI.follow(other_user, user) + {:ok, _, _} = User.follow(other_user, user, :follow_pending) + end + + conn = get(conn, "/api/v1/follow_requests") + assert length(json_response_and_validate_schema(conn, 200)) == 20 + assert [link_header] = get_resp_header(conn, "link") + assert link_header =~ "rel=\"next\"" + next_link = extract_next_link_header(link_header) + assert next_link =~ "/api/v1/follow_requests" + conn = get(conn, next_link) + assert length(json_response_and_validate_schema(conn, 200)) == 1 + end + test "/api/v1/follow_requests/:id/authorize works", %{user: user, conn: conn} do other_user = insert(:user)