diff --git a/api/funkwhale_api/music/filters.py b/api/funkwhale_api/music/filters.py index 79b2d2c63872f2816316b2e711cb20037f0f630a..d69dd13a3d3b095cabbc5730b78a970c35c88adf 100644 --- a/api/funkwhale_api/music/filters.py +++ b/api/funkwhale_api/music/filters.py @@ -218,3 +218,12 @@ class AlbumFilter( def filter_playable(self, queryset, name, value): actor = utils.get_actor_from_request(self.request) return queryset.playable_by(actor, value) + + +class LibraryFilter(filters.FilterSet): + q = fields.SearchFilter(search_fields=["name"],) + scope = common_filters.ActorScopeFilter(actor_field="actor", distinct=True) + + class Meta: + model = models.Library + fields = ["privacy_level", "q", "scope"] diff --git a/api/funkwhale_api/music/views.py b/api/funkwhale_api/music/views.py index 93635c11b55be1b10e996ad62f281e43f41e6c2f..6c9f7e41c4754b8c2a40819ab02529965e6583bb 100644 --- a/api/funkwhale_api/music/views.py +++ b/api/funkwhale_api/music/views.py @@ -273,6 +273,7 @@ class LibraryViewSet( oauth_permissions.ScopePermission, common_permissions.OwnerPermission, ] + filterset_class = filters.LibraryFilter required_scope = "libraries" anonymous_policy = "setting" owner_field = "actor.user" @@ -282,8 +283,12 @@ class LibraryViewSet( qs = super().get_queryset() # allow retrieving a single library by uuid if request.user isn't # the owner. Any other get should be from the owner only - if self.action != "retrieve": + if self.action not in ["retrieve", "list"]: qs = qs.filter(actor=self.request.user.actor) + if self.action == "list": + actor = utils.get_actor_from_request(self.request) + qs = qs.viewable_by(actor) + return qs def perform_create(self, serializer): diff --git a/api/tests/music/test_views.py b/api/tests/music/test_views.py index 2a362a536c447081bd05ab0b91105c611556b9ff..66f80d8fe155ea25485d28d498e4a0acd1a41e37 100644 --- a/api/tests/music/test_views.py +++ b/api/tests/music/test_views.py @@ -631,10 +631,10 @@ def test_user_can_create_library(factories, logged_in_api_client): def test_user_can_list_their_library(factories, logged_in_api_client): actor = logged_in_api_client.user.create_actor() library = factories["music.Library"](actor=actor) - factories["music.Library"]() + factories["music.Library"](privacy_level="everyone") url = reverse("api:v1:libraries-list") - response = logged_in_api_client.get(url) + response = logged_in_api_client.get(url, {"scope": "me"}) assert response.status_code == 200 assert response.data["count"] == 1 @@ -651,6 +651,19 @@ def test_user_can_retrieve_another_user_library(factories, logged_in_api_client) assert response.data["uuid"] == str(library.uuid) +def test_user_can_list_public_libraries(factories, api_client, preferences): + preferences["common__api_authentication_required"] = False + library = factories["music.Library"](privacy_level="everyone") + factories["music.Library"](privacy_level="me") + + url = reverse("api:v1:libraries-list") + response = api_client.get(url) + + assert response.status_code == 200 + assert response.data["count"] == 1 + assert response.data["results"][0]["uuid"] == str(library.uuid) + + def test_library_list_excludes_channel_library(factories, logged_in_api_client): actor = logged_in_api_client.user.create_actor() factories["audio.Channel"](attributed_to=actor) diff --git a/changes/changelog.d/1151.enhancement b/changes/changelog.d/1151.enhancement new file mode 100644 index 0000000000000000000000000000000000000000..c9b867a30208c3669a9d4b93571add7fc588e04b --- /dev/null +++ b/changes/changelog.d/1151.enhancement @@ -0,0 +1 @@ +Updated the /api/v1/libraries endpoint to support listing public libraries from other users/pods (#1151) diff --git a/changes/notes.rst b/changes/notes.rst index 96ac3d7651f92166072a2fb200c0dd57606851e3..3dc81fda814d04c32b056625df1bf7345a2d119f 100644 --- a/changes/notes.rst +++ b/changes/notes.rst @@ -5,3 +5,16 @@ Next release notes Those release notes refer to the current development branch and are reset after each release. + +Small API breaking change in ``/api/v1/libraries`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +To allow easier crawling of public libraries on a pod,we had to make a slight breaking change +to the behaviour of ``GET /api/v1/libraries``. + +Before, it returned only libraries owned by the current user. + +Now, it returns all the accessible libraries (including ones from other users and pods). + +If you are consuming the API via a third-party client and need to retrieve your libraries, +use the ``scope`` parameter, like this: ``GET /api/v1/libraries?scope=me`` diff --git a/front/src/views/content/libraries/Home.vue b/front/src/views/content/libraries/Home.vue index 2e5e394988bc5be8c117544ca513750662e93dc4..e3e71995ed733b57743606b37aa9c0b91524f537 100644 --- a/front/src/views/content/libraries/Home.vue +++ b/front/src/views/content/libraries/Home.vue @@ -53,7 +53,7 @@ export default { fetch() { this.isLoading = true let self = this - axios.get("libraries/").then(response => { + axios.get("libraries/", {params: {scope: 'me'}}).then(response => { self.isLoading = false self.libraries = response.data.results if (self.libraries.length === 0) {