diff --git a/funkwhale_network/routes.py b/funkwhale_network/routes.py index 5f84b009b97987b3f9c7a701a0d246a824b60b5f..1a0a530858930b15fcdc938e49c45068897a8106 100644 --- a/funkwhale_network/routes.py +++ b/funkwhale_network/routes.py @@ -21,6 +21,7 @@ domain_filters = { "open_registrations": fields.Bool(), "anonymous_can_listen": fields.Bool(), "federation_enabled": fields.Bool(), + "limit": fields.Integer(), } @@ -48,11 +49,14 @@ async def index(request): async def domains(request): if request.method == "GET": - filters = await parser.parse(domain_filters, request) + limit = filters.pop("limit", 0) rows = await db.get_domains(request["conn"], private=False, **filters) + total = len(rows) + if limit: + rows = rows[:limit] payload = { - "count": len(rows), + "count": total, "previous": None, "next": None, "results": [ diff --git a/tests/test_routes.py b/tests/test_routes.py index 35973ad9157d41d1739ae70e560bd15af9c7ac05..fbe969819a44b9f1fbcf5d2af7465b427423f1a2 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -29,6 +29,29 @@ async def test_domains_get(db_conn, client, factories): } +async def test_domains_get_page_size(db_conn, client, factories): + + checks = sorted( + [ + await factories["Check"].c(private=False), + await factories["Check"].c(private=False), + ], + key=lambda o: o["domain"], + ) + for check in checks: + domain = await db.get_domain(db_conn, check["domain"]) + check["first_seen"] = domain["first_seen"] + check["node_name"] = domain["node_name"] + resp = await client.get("/api/domains", params={"limit": 1}) + assert resp.status == 200 + assert await resp.json() == { + "count": 2, + "next": None, + "previous": None, + "results": [serializers.serialize_domain_from_check(checks[0])], + } + + @pytest.mark.parametrize( "field", ["up", "open_registrations", "anonymous_can_listen", "federation_enabled"] )