fix(subsonic): updatePlaylist checks the songs to add before changing anything (#8123)
The name, comment, removals and additions are separate writes, and the songs to add were only found missing when the last of them ran, so an unknown songIdToAdd answered error 70 over a playlist already renamed and trimmed. Every song to add is now looked up first, alongside the index check, and an unknown one answers 70 with nothing changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package subsonic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -106,7 +108,19 @@ func (h *pickHandlers) updatePlaylist(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
ctx := r.Context()
|
||||
|
||||
// Every index is checked before anything changes.
|
||||
// Every song to add and every index is checked before anything
|
||||
// changes: the name, comment, removals and additions are separate
|
||||
// writes, so a bad one found partway would answer an error over a
|
||||
// playlist already half changed.
|
||||
missing, err := firstMissingSong(ctx, dbq.New(h.pool), add)
|
||||
if err != nil {
|
||||
h.failPlaylistChange(w, r, err)
|
||||
return
|
||||
}
|
||||
if missing >= 0 {
|
||||
WriteFail(w, r, ErrDataNotFound, "Song not found: "+params["songIdToAdd"][missing])
|
||||
return
|
||||
}
|
||||
var remove []int32
|
||||
if indexes := params["songIndexToRemove"]; len(indexes) > 0 {
|
||||
_, positions, err := shownEntries(ctx, dbq.New(h.pool), detail)
|
||||
@@ -245,3 +259,25 @@ func songIDs(w http.ResponseWriter, r *http.Request, raw []string) ([]pgtype.UUI
|
||||
}
|
||||
return ids, true
|
||||
}
|
||||
|
||||
// firstMissingSong returns the index of the first of ids that names no
|
||||
// track, or -1 when every one exists.
|
||||
func firstMissingSong(ctx context.Context, q *dbq.Queries, ids []pgtype.UUID) (int, error) {
|
||||
if len(ids) == 0 {
|
||||
return -1, nil
|
||||
}
|
||||
found, err := q.GetTracksByIDs(ctx, ids)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("look up songs: %w", err)
|
||||
}
|
||||
have := make(map[pgtype.UUID]struct{}, len(found))
|
||||
for _, t := range found {
|
||||
have[t.ID] = struct{}{}
|
||||
}
|
||||
for i, id := range ids {
|
||||
if _, ok := have[id]; !ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
@@ -149,6 +149,18 @@ func TestUpdatePlaylist_IndexesCountTheSongsTheAppWasShown(t *testing.T) {
|
||||
if d := stored(t, h.playlists, f.user, id); d.Name != "Night Drive" || len(d.Tracks) != 2 {
|
||||
t.Errorf("after a refused update: name %q, %d songs; want it unchanged", d.Name, len(d.Tracks))
|
||||
}
|
||||
|
||||
// So does a song to add that isn't in the library, though it comes
|
||||
// after a rename and a removal that would each have worked.
|
||||
unknown := uuidToID(pgtype.UUID{Bytes: [16]byte{0xee}, Valid: true})
|
||||
q = "playlistId=" + pid + "&name=Changed&songIndexToRemove=0&songIdToAdd=" + unknown
|
||||
if code := call(t, h.updatePlaylist, f.user, q, nil); code != ErrDataNotFound {
|
||||
t.Errorf("unknown song to add: error %d, want %d", code, ErrDataNotFound)
|
||||
}
|
||||
d = stored(t, h.playlists, f.user, id)
|
||||
if got, want := storedTitles(d), []string{"Jetty", "Gale"}; d.Name != "Night Drive" || !slices.Equal(got, want) {
|
||||
t.Errorf("after an unknown song: name %q, songs %v; want Night Drive, %v", d.Name, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaylistWrites_OnlyTheOwnersOwnLists(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user