mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
fix(playqueue): keep the current track when earlier tracks are missing (#6299)
This commit is contained in:
parent
35948d566d
commit
644ecf88ee
@ -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"
|
||||
|
||||
@ -80,10 +81,37 @@ func (r *playQueueRepository) RetrieveWithMediaFiles(ctx context.Context, userId
|
||||
var res playQueue
|
||||
err := r.queryOne(ctx, sel, &res)
|
||||
q := r.toModel(&res)
|
||||
q.Items = r.loadTracks(ctx, q.Items)
|
||||
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)
|
||||
}
|
||||
return &q, err
|
||||
}
|
||||
|
||||
// remapCurrent relies on loaded being stored in the same order, minus the missing tracks. If the current track
|
||||
// is gone, it moves to the next remaining one (or the last) and restarts it.
|
||||
func remapCurrent(stored, loaded model.MediaFiles, current int, position int64) (int, int64) {
|
||||
kept := 0
|
||||
for i, t := range stored {
|
||||
if kept >= len(loaded) || loaded[kept].ID != t.ID {
|
||||
continue
|
||||
}
|
||||
if i >= current {
|
||||
if i > current {
|
||||
position = 0
|
||||
}
|
||||
return kept, position
|
||||
}
|
||||
kept++
|
||||
}
|
||||
return max(len(loaded)-1, 0), 0
|
||||
}
|
||||
|
||||
func (r *playQueueRepository) Retrieve(ctx context.Context, userId string) (*model.PlayQueue, error) {
|
||||
sel := r.newSelect(ctx).Columns("*").Where(Eq{"user_id": userId})
|
||||
var res playQueue
|
||||
@ -130,13 +158,14 @@ func (r *playQueueRepository) toModel(pq *playQueue) model.PlayQueue {
|
||||
|
||||
// loadTracks loads the tracks from the database. It receives a list of track IDs and returns a list of MediaFiles
|
||||
// in the same order as the input list.
|
||||
func (r *playQueueRepository) loadTracks(ctx context.Context, tracks model.MediaFiles) model.MediaFiles {
|
||||
func (r *playQueueRepository) loadTracks(ctx context.Context, tracks model.MediaFiles) (model.MediaFiles, error) {
|
||||
if len(tracks) == 0 {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
mfRepo := NewMediaFileRepository(r.db)
|
||||
trackMap := map[string]model.MediaFile{}
|
||||
var loadErr error
|
||||
|
||||
// Create an iterator to collect all track IDs
|
||||
ids := slice.SeqFunc(tracks, func(t model.MediaFile) string { return t.ID })
|
||||
@ -148,6 +177,7 @@ func (r *playQueueRepository) loadTracks(ctx context.Context, tracks model.Media
|
||||
if err != nil {
|
||||
u := loggedUser(ctx)
|
||||
log.Error(ctx, "Could not load playqueue/bookmark's tracks", "user", u.UserName, err)
|
||||
loadErr = err
|
||||
}
|
||||
for _, t := range tracks {
|
||||
trackMap[t.ID] = t
|
||||
@ -162,7 +192,7 @@ func (r *playQueueRepository) loadTracks(ctx context.Context, tracks model.Media
|
||||
newTracks = append(newTracks, track)
|
||||
}
|
||||
}
|
||||
return newTracks
|
||||
return newTracks, loadErr
|
||||
}
|
||||
|
||||
func (r *playQueueRepository) clearPlayQueue(ctx context.Context, userId string) error {
|
||||
|
||||
@ -309,36 +309,67 @@ var _ = Describe("PlayQueueRepository", func() {
|
||||
AssertPlayQueue(expected, actual)
|
||||
})
|
||||
|
||||
It("does not return tracks if they don't exist in the DB", func() {
|
||||
// Add a new song to the DB
|
||||
newSong := songRadioactivity
|
||||
newSong.ID = "temp-track"
|
||||
newSong.Path = "/new-path"
|
||||
mfRepo := NewMediaFileRepository(GetDBXBuilder())
|
||||
|
||||
Expect(mfRepo.Put(ctx, &newSong)).To(Succeed())
|
||||
|
||||
// Create a playqueue with the new song
|
||||
pq := aPlayQueue("userid", 0, 0, newSong, songAntenna)
|
||||
Expect(repo.Store(ctx, pq)).To(Succeed())
|
||||
|
||||
// Retrieve the playqueue
|
||||
actual, err := repo.RetrieveWithMediaFiles(ctx, "userid")
|
||||
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())
|
||||
|
||||
// The playqueue should contain both tracks
|
||||
AssertPlayQueue(pq, actual)
|
||||
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)))
|
||||
})
|
||||
|
||||
// Delete the new song
|
||||
Expect(mfRepo.Delete(ctx, "temp-track")).To(Succeed())
|
||||
Context("when tracks are no longer in the library", func() {
|
||||
var mfRepo model.MediaFileRepository
|
||||
gone := songRadioactivity
|
||||
gone.ID = "gone-track"
|
||||
gone.Path = "/gone-track"
|
||||
|
||||
// Retrieve the playqueue
|
||||
actual, err = repo.RetrieveWithMediaFiles(ctx, "userid")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
BeforeEach(func() {
|
||||
mfRepo = NewMediaFileRepository(GetDBXBuilder())
|
||||
Expect(mfRepo.Put(ctx, &gone)).To(Succeed())
|
||||
DeferCleanup(func() { _ = mfRepo.Delete(ctx, gone.ID) })
|
||||
})
|
||||
|
||||
// The playqueue should not contain the deleted track
|
||||
Expect(actual.Items).To(HaveLen(1))
|
||||
Expect(actual.Items[0].ID).To(Equal(songAntenna.ID))
|
||||
retrieveAfterRemoval := func(current int, position int64, items ...model.MediaFile) *model.PlayQueue {
|
||||
Expect(repo.Store(ctx, aPlayQueue("userid", current, position, items...))).To(Succeed())
|
||||
Expect(mfRepo.Delete(ctx, gone.ID)).To(Succeed())
|
||||
actual, err := repo.RetrieveWithMediaFiles(ctx, "userid")
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
return actual
|
||||
}
|
||||
|
||||
DescribeTable("points to the right current track",
|
||||
func(current int, items []model.MediaFile, expectedID string, expectedPosition int64) {
|
||||
actual := retrieveAfterRemoval(current, 45, items...)
|
||||
|
||||
Expect(actual.Items).To(HaveLen(len(items) - 1))
|
||||
Expect(actual.Items[actual.Current].ID).To(Equal(expectedID))
|
||||
Expect(actual.Position).To(Equal(expectedPosition))
|
||||
},
|
||||
Entry("when an earlier track is removed", 1, []model.MediaFile{gone, songComeTogether, songDayInALife}, songComeTogether.ID, int64(45)),
|
||||
Entry("when a later track is removed", 0, []model.MediaFile{songComeTogether, gone}, songComeTogether.ID, int64(45)),
|
||||
Entry("moving to the next track from the start when the current one is removed", 1, []model.MediaFile{songComeTogether, gone, songDayInALife}, songDayInALife.ID, int64(0)),
|
||||
Entry("moving to the last track when the removed current one was the last", 1, []model.MediaFile{songComeTogether, gone}, songComeTogether.ID, int64(0)),
|
||||
)
|
||||
|
||||
It("leaves the stored queue untouched", func() {
|
||||
retrieveAfterRemoval(1, 45, gone, songComeTogether, songDayInALife)
|
||||
|
||||
Consistently(func(g Gomega) {
|
||||
stored, err := repo.Retrieve(ctx, "userid")
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
g.Expect(stored.Items).To(HaveLen(3))
|
||||
g.Expect(stored.Items[0].ID).To(Equal(gone.ID))
|
||||
g.Expect(stored.Current).To(Equal(1))
|
||||
g.Expect(stored.Position).To(Equal(int64(45)))
|
||||
}).Should(Succeed())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
@ -29,11 +30,11 @@ func validateCurrentIndex(w http.ResponseWriter, current int, itemsLength int) b
|
||||
return true
|
||||
}
|
||||
|
||||
// retrieveExistingQueue retrieves an existing play queue for a user with proper error handling.
|
||||
// retrieveQueue retrieves a user's play queue with proper error handling.
|
||||
// Returns the queue (nil if not found) and false if an error occurred and response was sent.
|
||||
func retrieveExistingQueue(ctx context.Context, w http.ResponseWriter, ds model.DataStore, userID string) (*model.PlayQueue, bool) {
|
||||
existing, err := ds.PlayQueue().Retrieve(ctx, userID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
func retrieveQueue(ctx context.Context, w http.ResponseWriter, userID string, retrieve func(context.Context, string) (*model.PlayQueue, error), ignore ...error) (*model.PlayQueue, bool) {
|
||||
existing, err := retrieve(ctx, userID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) && !slices.ContainsFunc(ignore, func(e error) bool { return errors.Is(err, e) }) {
|
||||
log.Error(ctx, "Error retrieving queue", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return nil, false
|
||||
@ -41,6 +42,46 @@ func retrieveExistingQueue(ctx context.Context, w http.ResponseWriter, ds model.
|
||||
return existing, true
|
||||
}
|
||||
|
||||
// storedIndex maps an index in the queue as GET returns it to the stored queue, which keeps the same order.
|
||||
func storedIndex(stored, visible model.MediaFiles, i int) int {
|
||||
kept := 0
|
||||
for j, t := range stored {
|
||||
if kept < len(visible) && visible[kept].ID == t.ID {
|
||||
if kept == i {
|
||||
return j
|
||||
}
|
||||
kept++
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
// fallbackCurrent returns the stored index of the track GET shows in place of a stored current track the user can't see.
|
||||
func fallbackCurrent(ctx context.Context, w http.ResponseWriter, ds model.DataStore, userID string) (int, bool, bool) {
|
||||
repo := ds.PlayQueue()
|
||||
stored, ok := retrieveQueue(ctx, w, userID, repo.Retrieve)
|
||||
if !ok || stored == nil {
|
||||
return 0, false, ok
|
||||
}
|
||||
if stored.Current >= 0 && stored.Current < len(stored.Items) {
|
||||
visible, err := ds.MediaFile().Exists(ctx, stored.Items[stored.Current].ID)
|
||||
if err != nil {
|
||||
log.Error(ctx, "Error checking current queue track", err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return 0, false, false
|
||||
}
|
||||
if visible {
|
||||
return 0, false, true
|
||||
}
|
||||
}
|
||||
visible, ok := retrieveQueue(ctx, w, userID, repo.RetrieveWithMediaFiles)
|
||||
if !ok || visible == nil {
|
||||
return 0, false, ok
|
||||
}
|
||||
current := storedIndex(stored.Items, visible.Items, visible.Current)
|
||||
return current, current != stored.Current, true
|
||||
}
|
||||
|
||||
// decodeUpdatePayload decodes the JSON payload from the request body.
|
||||
// Returns false if decoding fails (and sends error response), true if successful.
|
||||
func decodeUpdatePayload(w http.ResponseWriter, r *http.Request) (*updateQueuePayload, bool) {
|
||||
@ -72,7 +113,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
|
||||
@ -137,21 +178,36 @@ func updateQueue(ds model.DataStore) http.HandlerFunc {
|
||||
// Initialize play queue with user ID and client info
|
||||
pq := &model.PlayQueue{UserID: user.ID, ChangedBy: client}
|
||||
var cols []string // Track which columns to update in the database
|
||||
repo := ds.PlayQueue()
|
||||
|
||||
// Handle queue items update
|
||||
// Clients index the queue as GET returns it, without the tracks the user can't see
|
||||
if payload.Ids != nil {
|
||||
pq.Items = createMediaFileItems(*payload.Ids)
|
||||
cols = append(cols, "items")
|
||||
|
||||
// If current index is not being updated, validate existing current index
|
||||
// against the new items list to ensure it remains valid
|
||||
if payload.Current == nil {
|
||||
existing, ok := retrieveExistingQueue(ctx, w, ds, user.ID)
|
||||
// On a load error GET shows the stored current and position, so only items get written
|
||||
visible, ok := retrieveQueue(ctx, w, user.ID, repo.RetrieveWithMediaFiles, model.ErrIncompleteQueue)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if existing != nil && !validateCurrentIndex(w, existing.Current, len(*payload.Ids)) {
|
||||
return
|
||||
if visible != nil {
|
||||
if !validateCurrentIndex(w, visible.Current, len(*payload.Ids)) {
|
||||
return
|
||||
}
|
||||
stored, ok := retrieveQueue(ctx, w, user.ID, repo.Retrieve)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// Write only what GET changed, so a concurrent position update is not overwritten
|
||||
if visible.Current != stored.Current {
|
||||
pq.Current = visible.Current
|
||||
cols = append(cols, "current")
|
||||
}
|
||||
if payload.Position == nil && visible.Position != stored.Position {
|
||||
pq.Position = visible.Position
|
||||
cols = append(cols, "position")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -167,13 +223,21 @@ func updateQueue(ds model.DataStore) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// If only current index is being updated, validate against existing items
|
||||
existing, ok := retrieveExistingQueue(ctx, w, ds, user.ID)
|
||||
visible, ok := retrieveQueue(ctx, w, user.ID, repo.RetrieveWithMediaFiles)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if existing != nil && !validateCurrentIndex(w, *payload.Current, len(existing.Items)) {
|
||||
return
|
||||
if visible != nil {
|
||||
if !validateCurrentIndex(w, *payload.Current, len(visible.Items)) {
|
||||
return
|
||||
}
|
||||
stored, ok := retrieveQueue(ctx, w, user.ID, repo.Retrieve)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if stored != nil {
|
||||
pq.Current = storedIndex(stored.Items, visible.Items, *payload.Current)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -182,6 +246,17 @@ func updateQueue(ds model.DataStore) http.HandlerFunc {
|
||||
if payload.Position != nil {
|
||||
pq.Position = max(*payload.Position, 0) // Ensure position is non-negative
|
||||
cols = append(cols, "position")
|
||||
|
||||
if payload.Ids == nil && payload.Current == nil {
|
||||
current, moved, ok := fallbackCurrent(ctx, w, ds, user.ID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if moved {
|
||||
pq.Current = current
|
||||
cols = append(cols, "current")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no fields were specified for update, return success without doing anything
|
||||
@ -191,7 +266,7 @@ func updateQueue(ds model.DataStore) http.HandlerFunc {
|
||||
}
|
||||
|
||||
// Perform partial update of the specified columns only
|
||||
if err := ds.PlayQueue().Store(ctx, pq, cols...); err != nil {
|
||||
if err := repo.Store(ctx, pq, cols...); err != nil {
|
||||
log.Error(ctx, "Error updating 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)
|
||||
@ -195,6 +210,95 @@ var _ = Describe("Queue Endpoints", func() {
|
||||
Expect(repo.LastCols).To(ConsistOf("items"))
|
||||
})
|
||||
|
||||
It("updates only ids when some tracks fail to load", func() {
|
||||
repo.Queue = &model.PlayQueue{UserID: user.ID, Current: 1, Position: 45,
|
||||
Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}, {ID: "s3"}}}
|
||||
repo.QueueWithMediaFiles = &model.PlayQueue{UserID: user.ID, Current: 1, Position: 45,
|
||||
Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}}}
|
||||
repo.WithMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
|
||||
body, _ := json.Marshal(updateQueuePayload{Ids: new([]string{"s1", "s2", "s3", "s4"})})
|
||||
req := httptest.NewRequest("PUT", "/queue", bytes.NewReader(body))
|
||||
req = req.WithContext(request.WithUser(req.Context(), user))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
updateQueue(ds)(w, req)
|
||||
Expect(w.Code).To(Equal(http.StatusNoContent))
|
||||
Expect(repo.Queue.Items).To(HaveLen(4))
|
||||
Expect(repo.LastCols).To(ConsistOf("items"))
|
||||
})
|
||||
|
||||
Context("when the stored queue has tracks the user can't see", func() {
|
||||
BeforeEach(func() {
|
||||
repo.Queue = &model.PlayQueue{UserID: user.ID, Current: 2, Position: 45,
|
||||
Items: model.MediaFiles{{ID: "gone1"}, {ID: "s1"}, {ID: "gone2"}, {ID: "s2"}}}
|
||||
repo.QueueWithMediaFiles = &model.PlayQueue{UserID: user.ID, Current: 1, Position: 0,
|
||||
Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}}}
|
||||
ds.MediaFile().(*tests.MockMediaFileRepo).SetData(repo.QueueWithMediaFiles.Items)
|
||||
})
|
||||
|
||||
put := func(payload updateQueuePayload) int {
|
||||
body, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest("PUT", "/queue", bytes.NewReader(body))
|
||||
req = req.WithContext(request.WithUser(req.Context(), user))
|
||||
w := httptest.NewRecorder()
|
||||
updateQueue(ds)(w, req)
|
||||
return w.Code
|
||||
}
|
||||
|
||||
It("stores current as the index of the same track in the stored queue", func() {
|
||||
Expect(put(updateQueuePayload{Current: new(1)})).To(Equal(http.StatusNoContent))
|
||||
Expect(repo.Queue.Current).To(Equal(3))
|
||||
Expect(repo.LastCols).To(ConsistOf("current"))
|
||||
})
|
||||
|
||||
It("rejects a current that is only valid for the stored queue", func() {
|
||||
Expect(put(updateQueuePayload{Current: new(2)})).To(Equal(http.StatusBadRequest))
|
||||
})
|
||||
|
||||
It("moves current to the track the client saw when only position changes", func() {
|
||||
Expect(put(updateQueuePayload{Position: new(int64(30))})).To(Equal(http.StatusNoContent))
|
||||
Expect(repo.Queue.Current).To(Equal(3))
|
||||
Expect(repo.Queue.Position).To(Equal(int64(30)))
|
||||
Expect(repo.LastCols).To(ConsistOf("current", "position"))
|
||||
})
|
||||
|
||||
It("writes only position when the stored current track is visible", func() {
|
||||
repo.Queue.Current = 1
|
||||
repo.QueueWithMediaFiles.Current = 0
|
||||
Expect(put(updateQueuePayload{Position: new(int64(30))})).To(Equal(http.StatusNoContent))
|
||||
Expect(repo.LastCols).To(ConsistOf("position"))
|
||||
})
|
||||
|
||||
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))
|
||||
Expect(repo.Queue.Position).To(BeZero())
|
||||
Expect(repo.LastCols).To(ConsistOf("items", "current", "position"))
|
||||
})
|
||||
|
||||
It("writes only the current index when the current track is visible and only ids change", func() {
|
||||
repo.Queue.Current = 1
|
||||
repo.QueueWithMediaFiles.Current = 0
|
||||
repo.QueueWithMediaFiles.Position = 45
|
||||
Expect(put(updateQueuePayload{Ids: new([]string{"s1", "s2"})})).To(Equal(http.StatusNoContent))
|
||||
Expect(repo.Queue.Current).To(BeZero())
|
||||
Expect(repo.LastCols).To(ConsistOf("items", "current"))
|
||||
})
|
||||
|
||||
It("uses the position from the request when ids and position change", func() {
|
||||
Expect(put(updateQueuePayload{Ids: new([]string{"s1", "s2"}), Position: new(int64(10))})).To(Equal(http.StatusNoContent))
|
||||
Expect(repo.Queue.Current).To(Equal(1))
|
||||
Expect(repo.Queue.Position).To(Equal(int64(10)))
|
||||
})
|
||||
})
|
||||
|
||||
It("updates ids and current", func() {
|
||||
repo.Queue = &model.PlayQueue{UserID: user.ID}
|
||||
payload := updateQueuePayload{Ids: new([]string{"s1", "s2"}), Current: new(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))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -9,9 +9,13 @@ import (
|
||||
|
||||
type MockPlayQueueRepo struct {
|
||||
model.PlayQueueRepository
|
||||
Queue *model.PlayQueue
|
||||
Err bool
|
||||
LastCols []string
|
||||
Queue *model.PlayQueue
|
||||
// QueueWithMediaFiles, when set, is what RetrieveWithMediaFiles returns, to simulate tracks the user can't see
|
||||
QueueWithMediaFiles *model.PlayQueue
|
||||
// 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 {
|
||||
@ -31,14 +35,18 @@ func (m *MockPlayQueueRepo) RetrieveWithMediaFiles(_ context.Context, userId str
|
||||
if m.Err {
|
||||
return nil, errors.New("error")
|
||||
}
|
||||
if m.Queue == nil || m.Queue.UserID != userId {
|
||||
src := m.Queue
|
||||
if m.QueueWithMediaFiles != nil {
|
||||
src = m.QueueWithMediaFiles
|
||||
}
|
||||
if src == nil || src.UserID != userId {
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
copyItems := make(model.MediaFiles, len(m.Queue.Items))
|
||||
copy(copyItems, m.Queue.Items)
|
||||
qCopy := *m.Queue
|
||||
copyItems := make(model.MediaFiles, len(src.Items))
|
||||
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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user