From 69795b5ca2d8a1ad4b37a4cc334bc3a6ea7e8244 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Mon, 28 Oct 2019 08:58:32 +0100
Subject: [PATCH] Resolve "Pagination of results in genres in Subsonic API does
 not work"

---
 api/funkwhale_api/subsonic/views.py |  8 +++++++-
 api/tests/subsonic/test_views.py    | 17 +++++++++++++++++
 changes/changelog.d/954.bugfix      |  1 +
 3 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 changes/changelog.d/954.bugfix

diff --git a/api/funkwhale_api/subsonic/views.py b/api/funkwhale_api/subsonic/views.py
index 60209a92d..db2620100 100644
--- a/api/funkwhale_api/subsonic/views.py
+++ b/api/funkwhale_api/subsonic/views.py
@@ -351,6 +351,12 @@ class SubsonicViewSet(viewsets.GenericViewSet):
             )
         )
         queryset = queryset.playable_by(actor)
+        try:
+            offset = int(data.get("offset", 0))
+        except (TypeError, ValueError):
+
+            offset = 0
+
         try:
             size = int(
                 data["count"]
@@ -369,7 +375,7 @@ class SubsonicViewSet(viewsets.GenericViewSet):
             )
             .prefetch_related("uploads")
             .distinct()
-            .order_by("-creation_date")[:size]
+            .order_by("-creation_date")[offset : offset + size]
         )
         data = {
             "songsByGenre": {
diff --git a/api/tests/subsonic/test_views.py b/api/tests/subsonic/test_views.py
index d58cc3932..f56cfc1a9 100644
--- a/api/tests/subsonic/test_views.py
+++ b/api/tests/subsonic/test_views.py
@@ -520,6 +520,23 @@ def test_get_songs_by_genre(f, tags_field, db, logged_in_api_client, factories):
     assert response.data == expected
 
 
+def test_get_songs_by_genre_offset(logged_in_api_client, factories):
+    url = reverse("api:subsonic-get_songs_by_genre")
+    assert url.endswith("getSongsByGenre") is True
+    track1 = factories["music.Track"](playable=True, set_tags=["Rock"])
+    factories["music.Track"](playable=True, set_tags=["Rock"])
+    factories["music.Track"](playable=True, set_tags=["Pop"])
+    # the API order tracks by creation date DESC, so the second one
+    # returned by the API is the first one to be created in the test.
+    expected = {"songsByGenre": {"song": serializers.get_song_list_data([track1])}}
+
+    response = logged_in_api_client.get(
+        url, {"f": "json", "count": 1, "offset": 1, "genre": "rock"}
+    )
+    assert response.status_code == 200
+    assert response.data == expected
+
+
 @pytest.mark.parametrize("f", ["json"])
 def test_search3(f, db, logged_in_api_client, factories):
     url = reverse("api:subsonic-search3")
diff --git a/changes/changelog.d/954.bugfix b/changes/changelog.d/954.bugfix
new file mode 100644
index 000000000..eacb9603a
--- /dev/null
+++ b/changes/changelog.d/954.bugfix
@@ -0,0 +1 @@
+Fixed pagination in subsonic getSongsByGenre endpoint (#954)
-- 
GitLab