fix(playlists): a replayed append whose reply was lost adds nothing more (#8122)

An append that reached the server but whose reply never reached the
phone stayed in the offline queue and was sent again, and since an
append is an action, the song landed twice. Android now names each
append with a request id, carried by the live send and its queued row
alike; POST /api/playlists/{id}/tracks takes it as request_id, and the
server records it per playlist in the append's own transaction
(playlist_append_requests, pruned after 30 days). A send whose id is
already recorded changes nothing; a failed append rolls its id back
with it. Without an id an append behaves as before, so queued rows
from older builds and Subsonic clients are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-28 11:52:12 -05:00
co-authored by Claude Opus 5.5
parent 6996561cb1
commit ad17849205
15 changed files with 207 additions and 20 deletions
+47
View File
@@ -160,3 +160,50 @@ func trackTitles(d *playlists.PlaylistDetail) []string {
}
return out
}
func TestAppendTracksOnce_ASecondSendWithTheSameRequestAddsNothing(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
a := seedTrack(t, pool, "A", "Artist")
b := seedTrack(t, pool, "B", "Artist")
svc := playlists.NewService(pool, nil, t.TempDir())
ctx := context.Background()
pl, _ := svc.Create(ctx, user.ID, "Test", "", false)
sent := randomUUID()
for range 2 {
if err := svc.AppendTracksOnce(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID}, sent); err != nil {
t.Fatalf("append A: %v", err)
}
}
// A new request is a new append, even of the same song.
if err := svc.AppendTracksOnce(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID, b.ID}, randomUUID()); err != nil {
t.Fatalf("append A, B: %v", err)
}
got, _ := svc.Get(ctx, user.ID, pl.ID)
if titles := trackTitles(got); !slices.Equal(titles, []string{"A", "A", "B"}) {
t.Errorf("tracks = %v, want A, A, B", titles)
}
}
func TestAppendTracksOnce_AFailedAppendDoesNotUseUpItsRequest(t *testing.T) {
pool := dbtest.Pool(t)
user := seedUser(t, pool, "alice")
a := seedTrack(t, pool, "A", "Artist")
svc := playlists.NewService(pool, nil, t.TempDir())
ctx := context.Background()
pl, _ := svc.Create(ctx, user.ID, "Test", "", false)
sent := randomUUID()
err := svc.AppendTracksOnce(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID, randomUUID()}, sent)
if !errors.Is(err, playlists.ErrTrackNotFound) {
t.Fatalf("append with a missing track: %v, want ErrTrackNotFound", err)
}
if err := svc.AppendTracksOnce(ctx, user.ID, pl.ID, []pgtype.UUID{a.ID}, sent); err != nil {
t.Fatalf("append A: %v", err)
}
got, _ := svc.Get(ctx, user.ID, pl.ID)
if titles := trackTitles(got); !slices.Equal(titles, []string{"A"}) {
t.Errorf("tracks = %v, want A: the failed send rolled back with its request id", titles)
}
}
+34
View File
@@ -390,14 +390,48 @@ func editable(ctx context.Context, q *dbq.Queries, callerID, playlistID pgtype.U
// are populated from the tracks/albums/artists join at insert time.
// Triggers cover regeneration after.
func (s *Service) AppendTracks(ctx context.Context, callerID, playlistID pgtype.UUID, trackIDs []pgtype.UUID) error {
return s.AppendTracksOnce(ctx, callerID, playlistID, trackIDs, pgtype.UUID{})
}
// AppendTracksOnce is AppendTracks for a client that may send the same
// append twice: a phone replaying a queued append whose first send reached
// the server but whose reply was lost. A valid requestID is recorded with
// the append, in the same transaction, and a later send with the same id
// for the same playlist changes nothing. Without a requestID it appends
// every time.
func (s *Service) AppendTracksOnce(ctx context.Context, callerID, playlistID pgtype.UUID, trackIDs []pgtype.UUID, requestID pgtype.UUID) error {
if len(trackIDs) == 0 {
return fmt.Errorf("%w: at least one track id required", ErrInvalidInput)
}
return s.changeTracks(ctx, callerID, playlistID, func(tx pgx.Tx) error {
if requestID.Valid {
fresh, err := claimAppendRequest(ctx, tx, playlistID, requestID)
if err != nil || !fresh {
return err
}
}
return appendTracks(ctx, tx, playlistID, trackIDs)
})
}
// claimAppendRequest records requestID as applied to the playlist, and
// reports whether it was new. Old ids are forgotten here too, so the table
// stays as small as the playlist's last month of appends.
func claimAppendRequest(ctx context.Context, tx pgx.Tx, playlistID, requestID pgtype.UUID) (bool, error) {
q := dbq.New(tx)
if err := q.PrunePlaylistAppendRequests(ctx, playlistID); err != nil {
return false, fmt.Errorf("prune append requests: %w", err)
}
n, err := q.RecordPlaylistAppendRequest(ctx, dbq.RecordPlaylistAppendRequestParams{
PlaylistID: playlistID,
RequestID: requestID,
})
if err != nil {
return false, fmt.Errorf("record append request: %w", err)
}
return n == 1, nil
}
// ReplaceTracks makes trackIDs the playlist's whole track list, in order;
// an empty list empties it. The old list goes only if every new track
// exists.