mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-06 05:19:56 +02:00
Fix #1036: Favor local uploads when playing a track with multiple uploads
This commit is contained in:
parent
cc453edfec
commit
882e245a09
4 changed files with 46 additions and 3 deletions
|
@ -281,6 +281,22 @@ def serialize_upload(upload):
|
|||
}
|
||||
|
||||
|
||||
def sort_uploads_for_listen(uploads):
|
||||
"""
|
||||
Given a list of uploads, return a sorted list of uploads, with local or locally
|
||||
cached ones first, and older first
|
||||
"""
|
||||
score = {upload: 0 for upload in uploads}
|
||||
for upload in uploads:
|
||||
if upload.is_local:
|
||||
score[upload] = 3
|
||||
elif upload.audio_file:
|
||||
score[upload] = 2
|
||||
|
||||
sorted_tuples = sorted(score.items(), key=lambda t: (t[1], -t[0].pk), reverse=True)
|
||||
return [t[0] for t in sorted_tuples]
|
||||
|
||||
|
||||
class TrackSerializer(OptionalDescriptionMixin, serializers.Serializer):
|
||||
artist = serializers.SerializerMethodField()
|
||||
album = TrackAlbumSerializer(read_only=True)
|
||||
|
@ -310,7 +326,8 @@ class TrackSerializer(OptionalDescriptionMixin, serializers.Serializer):
|
|||
return obj.listen_url
|
||||
|
||||
def get_uploads(self, obj):
|
||||
return [serialize_upload(u) for u in getattr(obj, "playable_uploads", [])]
|
||||
uploads = getattr(obj, "playable_uploads", [])
|
||||
return [serialize_upload(u) for u in sort_uploads_for_listen(uploads)]
|
||||
|
||||
def get_tags(self, obj):
|
||||
tagged_items = getattr(obj, "_prefetched_tagged_items", [])
|
||||
|
|
|
@ -25,6 +25,7 @@ from funkwhale_api.common import (
|
|||
from funkwhale_api.favorites.models import TrackFavorite
|
||||
from funkwhale_api.moderation import filters as moderation_filters
|
||||
from funkwhale_api.music import models as music_models
|
||||
from funkwhale_api.music import serializers as music_serializers
|
||||
from funkwhale_api.music import utils
|
||||
from funkwhale_api.music import views as music_views
|
||||
from funkwhale_api.playlists import models as playlists_models
|
||||
|
@ -255,10 +256,13 @@ class SubsonicViewSet(viewsets.GenericViewSet):
|
|||
data = request.GET or request.POST
|
||||
track = kwargs.pop("obj")
|
||||
queryset = track.uploads.select_related("track__album__artist", "track__artist")
|
||||
upload = queryset.first()
|
||||
if not upload:
|
||||
sorted_uploads = music_serializers.sort_uploads_for_listen(queryset)
|
||||
|
||||
if not sorted_uploads:
|
||||
return response.Response(status=404)
|
||||
|
||||
upload = sorted_uploads[0]
|
||||
|
||||
max_bitrate = data.get("maxBitRate")
|
||||
try:
|
||||
max_bitrate = min(max(int(max_bitrate), 0), 320) or None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue