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
+6
View File
@@ -467,6 +467,12 @@ type Playlist struct {
ShelfRank int32
}
type PlaylistAppendRequest struct {
PlaylistID pgtype.UUID
RequestID pgtype.UUID
CreatedAt pgtype.Timestamptz
}
type PlaylistTrack struct {
PlaylistID pgtype.UUID
Position int32
+33
View File
@@ -513,6 +513,39 @@ func (q *Queries) ListUserPlaylistIDsForOwner(ctx context.Context, userID pgtype
return items, nil
}
const prunePlaylistAppendRequests = `-- name: PrunePlaylistAppendRequests :exec
DELETE FROM playlist_append_requests
WHERE playlist_id = $1 AND created_at < now() - interval '30 days'
`
// Forgets the playlist's append request ids older than 30 days, by which
// time any queued replay of them has long since been sent.
func (q *Queries) PrunePlaylistAppendRequests(ctx context.Context, playlistID pgtype.UUID) error {
_, err := q.db.Exec(ctx, prunePlaylistAppendRequests, playlistID)
return err
}
const recordPlaylistAppendRequest = `-- name: RecordPlaylistAppendRequest :execrows
INSERT INTO playlist_append_requests (playlist_id, request_id)
VALUES ($1::uuid, $2::uuid)
ON CONFLICT DO NOTHING
`
type RecordPlaylistAppendRequestParams struct {
PlaylistID pgtype.UUID
RequestID pgtype.UUID
}
// Claims a client's append request id for the playlist. No row affected
// means the id was already applied: the append is a replay.
func (q *Queries) RecordPlaylistAppendRequest(ctx context.Context, arg RecordPlaylistAppendRequestParams) (int64, error) {
result, err := q.db.Exec(ctx, recordPlaylistAppendRequest, arg.PlaylistID, arg.RequestID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const renumberPlaylistTracksAfter = `-- name: RenumberPlaylistTracksAfter :exec
UPDATE playlist_tracks
SET position = position - 1
@@ -0,0 +1 @@
DROP TABLE IF EXISTS playlist_append_requests;
@@ -0,0 +1,13 @@
-- An append the phone sent but never heard back about (a timeout, the app
-- killed mid-request) stays in its offline queue and is sent again. An
-- append is an action, not a state, so the second send would add the songs
-- a second time. The phone now names each append with a request id, and
-- the server keeps the ids it has applied here: a send whose id is already
-- recorded for the playlist changes nothing. Rows older than 30 days are
-- pruned as new appends arrive; a queued write is replayed long before that.
CREATE TABLE playlist_append_requests (
playlist_id uuid NOT NULL REFERENCES playlists(id) ON DELETE CASCADE,
request_id uuid NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (playlist_id, request_id)
);
+13
View File
@@ -113,6 +113,19 @@ JOIN artists ON artists.id = t.artist_id
WHERE t.id = sqlc.arg(track_id)::uuid
RETURNING *;
-- name: RecordPlaylistAppendRequest :execrows
-- Claims a client's append request id for the playlist. No row affected
-- means the id was already applied: the append is a replay.
INSERT INTO playlist_append_requests (playlist_id, request_id)
VALUES (sqlc.arg(playlist_id)::uuid, sqlc.arg(request_id)::uuid)
ON CONFLICT DO NOTHING;
-- name: PrunePlaylistAppendRequests :exec
-- Forgets the playlist's append request ids older than 30 days, by which
-- time any queued replay of them has long since been sent.
DELETE FROM playlist_append_requests
WHERE playlist_id = $1 AND created_at < now() - interval '30 days';
-- name: DeletePlaylistTrack :exec
-- Two-step: delete the row at `position`, then renumber subsequent rows
-- to close the gap. The renumber is a single UPDATE; the service layer