Merge branch '1151-list-librarie' into 'master'
Fix #1151: Updated the /api/v1/libraries endpoint to support listing public libraries of a pod See merge request funkwhale/funkwhale!1135
This commit is contained in:
commit
d43eed805b
|
@ -218,3 +218,12 @@ class AlbumFilter(
|
||||||
def filter_playable(self, queryset, name, value):
|
def filter_playable(self, queryset, name, value):
|
||||||
actor = utils.get_actor_from_request(self.request)
|
actor = utils.get_actor_from_request(self.request)
|
||||||
return queryset.playable_by(actor, value)
|
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"]
|
||||||
|
|
|
@ -273,6 +273,7 @@ class LibraryViewSet(
|
||||||
oauth_permissions.ScopePermission,
|
oauth_permissions.ScopePermission,
|
||||||
common_permissions.OwnerPermission,
|
common_permissions.OwnerPermission,
|
||||||
]
|
]
|
||||||
|
filterset_class = filters.LibraryFilter
|
||||||
required_scope = "libraries"
|
required_scope = "libraries"
|
||||||
anonymous_policy = "setting"
|
anonymous_policy = "setting"
|
||||||
owner_field = "actor.user"
|
owner_field = "actor.user"
|
||||||
|
@ -282,8 +283,12 @@ class LibraryViewSet(
|
||||||
qs = super().get_queryset()
|
qs = super().get_queryset()
|
||||||
# allow retrieving a single library by uuid if request.user isn't
|
# allow retrieving a single library by uuid if request.user isn't
|
||||||
# the owner. Any other get should be from the owner only
|
# 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)
|
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
|
return qs
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
|
|
|
@ -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):
|
def test_user_can_list_their_library(factories, logged_in_api_client):
|
||||||
actor = logged_in_api_client.user.create_actor()
|
actor = logged_in_api_client.user.create_actor()
|
||||||
library = factories["music.Library"](actor=actor)
|
library = factories["music.Library"](actor=actor)
|
||||||
factories["music.Library"]()
|
factories["music.Library"](privacy_level="everyone")
|
||||||
|
|
||||||
url = reverse("api:v1:libraries-list")
|
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.status_code == 200
|
||||||
assert response.data["count"] == 1
|
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)
|
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):
|
def test_library_list_excludes_channel_library(factories, logged_in_api_client):
|
||||||
actor = logged_in_api_client.user.create_actor()
|
actor = logged_in_api_client.user.create_actor()
|
||||||
factories["audio.Channel"](attributed_to=actor)
|
factories["audio.Channel"](attributed_to=actor)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Updated the /api/v1/libraries endpoint to support listing public libraries from other users/pods (#1151)
|
|
@ -5,3 +5,16 @@ Next release notes
|
||||||
|
|
||||||
Those release notes refer to the current development branch and are reset
|
Those release notes refer to the current development branch and are reset
|
||||||
after each release.
|
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``
|
||||||
|
|
|
@ -53,7 +53,7 @@ export default {
|
||||||
fetch() {
|
fetch() {
|
||||||
this.isLoading = true
|
this.isLoading = true
|
||||||
let self = this
|
let self = this
|
||||||
axios.get("libraries/").then(response => {
|
axios.get("libraries/", {params: {scope: 'me'}}).then(response => {
|
||||||
self.isLoading = false
|
self.isLoading = false
|
||||||
self.libraries = response.data.results
|
self.libraries = response.data.results
|
||||||
if (self.libraries.length === 0) {
|
if (self.libraries.length === 0) {
|
||||||
|
|
Loading…
Reference in New Issue