mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-16 02:42:34 +03:00
fix(playqueue): refuse queue index updates when tracks fail to load
RetrieveWithMediaFiles now returns ErrIncompleteQueue when it reads the queue but a track query fails. Before, it returned the partially loaded list with no error, and a current-only PUT /api/queue could validate and map an index against that list and store the wrong track. updateQueue already fails on any retrieval error, so it now returns 500 and writes nothing. The GET handlers (getPlayQueue, getPlayQueueByIndex and GET /api/queue) treat ErrIncompleteQueue like before and still return the tracks that loaded.
This commit is contained in:
1 parent
b5fe0b4221
commit
d4eecb2785
8 files changed
+74
-6
No files matched your search
@@ -14,4 +14,5 @@ var (
|
||||
ErrNotAvailable = errors.New("functionality not available")
|
||||
ErrValidation = errors.New("validation error")
|
||||
ErrPlaylistNotEditable = errors.New("playlist tracks are not editable")
|
||||
ErrIncompleteQueue = errors.New("could not load all play queue tracks")
|
||||
)
|
||||
@@ -3,6 +3,7 @@ package persistence
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -83,6 +84,9 @@ func (r *playQueueRepository) RetrieveWithMediaFiles(ctx context.Context, userId
|
||||
stored := q.Items
|
||||
var loadErr error
|
||||
q.Items, loadErr = r.loadTracks(ctx, stored)
|
||||
if loadErr != nil && err == nil {
|
||||
err = fmt.Errorf("%w: %w", model.ErrIncompleteQueue, loadErr)
|
||||
}
|
||||
if loadErr == nil && len(q.Items) < len(stored) {
|
||||
q.Current, q.Position = remapCurrent(stored, q.Items, q.Current, q.Position)
|
||||
}
|
||||
|
||||
@@ -309,6 +309,21 @@ var _ = Describe("PlayQueueRepository", func() {
|
||||
AssertPlayQueue(expected, actual)
|
||||
})
|
||||
|
||||
It("returns ErrIncompleteQueue with the queue as stored when the tracks fail to load", func() {
|
||||
Expect(repo.Store(ctx, aPlayQueue("userid", 1, 45, songComeTogether, songDayInALife))).To(Succeed())
|
||||
tx, err := GetDBXBuilder().Begin()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
DeferCleanup(func() { _ = tx.Rollback() })
|
||||
_, err = tx.NewQuery("ALTER TABLE media_file RENAME TO media_file_unavailable").Execute()
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
actual, err := NewPlayQueueRepository(tx).RetrieveWithMediaFiles(ctx, "userid")
|
||||
Expect(err).To(MatchError(model.ErrIncompleteQueue))
|
||||
Expect(actual.Items).To(BeEmpty())
|
||||
Expect(actual.Current).To(Equal(1))
|
||||
Expect(actual.Position).To(Equal(int64(45)))
|
||||
})
|
||||
|
||||
Context("when tracks are no longer in the library", func() {
|
||||
var mfRepo model.MediaFileRepository
|
||||
gone := songRadioactivity
|
||||
|
||||
@@ -86,7 +86,7 @@ func getQueue(ds model.DataStore) http.HandlerFunc {
|
||||
user, _ := request.UserFrom(ctx)
|
||||
repo := ds.PlayQueue()
|
||||
pq, err := repo.RetrieveWithMediaFiles(ctx, user.ID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) && !errors.Is(err, model.ErrIncompleteQueue) {
|
||||
log.Error(ctx, "Error retrieving queue", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
|
||||
@@ -3,6 +3,7 @@ package nativeapi
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
@@ -151,6 +152,20 @@ var _ = Describe("Queue Endpoints", func() {
|
||||
Expect(resp.Position).To(Equal(int64(0)))
|
||||
})
|
||||
|
||||
It("returns the loaded tracks when some of them fail to load", func() {
|
||||
repo.Queue = &model.PlayQueue{UserID: user.ID, Items: model.MediaFiles{{ID: "track1"}}}
|
||||
repo.WithMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
|
||||
req := httptest.NewRequest("GET", "/queue", nil)
|
||||
req = req.WithContext(request.WithUser(req.Context(), user))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
getQueue(ds)(w, req)
|
||||
Expect(w.Code).To(Equal(http.StatusOK))
|
||||
var resp model.PlayQueue
|
||||
Expect(json.Unmarshal(w.Body.Bytes(), &resp)).To(Succeed())
|
||||
Expect(resp.Items).To(HaveLen(1))
|
||||
})
|
||||
|
||||
It("returns internal server error when retrieve fails", func() {
|
||||
repo.Err = true
|
||||
req := httptest.NewRequest("GET", "/queue", nil)
|
||||
@@ -223,6 +238,13 @@ var _ = Describe("Queue Endpoints", func() {
|
||||
Expect(put(updateQueuePayload{Current: new(2)})).To(Equal(http.StatusBadRequest))
|
||||
})
|
||||
|
||||
It("refuses to update current when the tracks fail to load", func() {
|
||||
repo.WithMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
|
||||
Expect(put(updateQueuePayload{Current: new(0)})).To(Equal(http.StatusInternalServerError))
|
||||
Expect(repo.LastCols).To(BeNil())
|
||||
Expect(repo.Queue.Current).To(Equal(2))
|
||||
})
|
||||
|
||||
It("keeps the current track and position the client saw when only ids change", func() {
|
||||
Expect(put(updateQueuePayload{Ids: new([]string{"s1", "s2", "s3"})})).To(Equal(http.StatusNoContent))
|
||||
Expect(repo.Queue.Current).To(Equal(1))
|
||||
|
||||
@@ -82,7 +82,7 @@ func (api *Router) GetPlayQueue(r *http.Request) (*responses.Subsonic, error) {
|
||||
|
||||
repo := api.ds.PlayQueue()
|
||||
pq, err := repo.RetrieveWithMediaFiles(r.Context(), user.ID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) && !errors.Is(err, model.ErrIncompleteQueue) {
|
||||
return nil, err
|
||||
}
|
||||
if pq == nil || len(pq.Items) == 0 {
|
||||
@@ -153,7 +153,7 @@ func (api *Router) GetPlayQueueByIndex(r *http.Request) (*responses.Subsonic, er
|
||||
|
||||
repo := api.ds.PlayQueue()
|
||||
pq, err := repo.RetrieveWithMediaFiles(r.Context(), user.ID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) && !errors.Is(err, model.ErrIncompleteQueue) {
|
||||
return nil, err
|
||||
}
|
||||
if pq == nil || len(pq.Items) == 0 {
|
||||
|
||||
@@ -2,6 +2,7 @@ package subsonic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/model/request"
|
||||
@@ -45,4 +46,27 @@ var _ = Describe("Bookmarks", func() {
|
||||
Expect(mfRepo.BookmarksAdded).To(ConsistOf("visible"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("when some queued tracks fail to load", func() {
|
||||
BeforeEach(func() {
|
||||
ds.MockedPlayQueue = &tests.MockPlayQueueRepo{
|
||||
Queue: &model.PlayQueue{UserID: "u1", Items: model.MediaFiles{{ID: "visible"}}},
|
||||
WithMediaFilesErr: fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue),
|
||||
}
|
||||
})
|
||||
|
||||
It("getPlayQueue returns the loaded tracks", func() {
|
||||
resp, err := router.GetPlayQueue(newGetRequest().WithContext(ctx))
|
||||
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(resp.PlayQueue.Entry).To(HaveLen(1))
|
||||
})
|
||||
|
||||
It("getPlayQueueByIndex returns the loaded tracks", func() {
|
||||
resp, err := router.GetPlayQueueByIndex(newGetRequest().WithContext(ctx))
|
||||
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
Expect(resp.PlayQueueByIndex.Entry).To(HaveLen(1))
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -12,8 +12,10 @@ type MockPlayQueueRepo struct {
|
||||
Queue *model.PlayQueue
|
||||
// QueueWithMediaFiles, when set, is what RetrieveWithMediaFiles returns, to simulate tracks the user can't see
|
||||
QueueWithMediaFiles *model.PlayQueue
|
||||
Err bool
|
||||
LastCols []string
|
||||
// WithMediaFilesErr is returned by RetrieveWithMediaFiles together with the queue
|
||||
WithMediaFilesErr error
|
||||
Err bool
|
||||
LastCols []string
|
||||
}
|
||||
|
||||
func (m *MockPlayQueueRepo) Store(_ context.Context, q *model.PlayQueue, cols ...string) error {
|
||||
@@ -44,7 +46,7 @@ func (m *MockPlayQueueRepo) RetrieveWithMediaFiles(_ context.Context, userId str
|
||||
copy(copyItems, src.Items)
|
||||
qCopy := *src
|
||||
qCopy.Items = copyItems
|
||||
return &qCopy, nil
|
||||
return &qCopy, m.WithMediaFilesErr
|
||||
}
|
||||
|
||||
func (m *MockPlayQueueRepo) Retrieve(_ context.Context, userId string) (*model.PlayQueue, error) {
|
||||
|
||||
Reference in new issue
Block a user