feat(playlists): changes a phone queues offline are safe to send twice (#8023)
release / go (push) Successful in 1m59s
release / web (push) Successful in 1m23s
release / govulncheck (push) Successful in 22s
release / integration (push) Successful in 4m38s
release / android (push) Successful in 4m45s
release / Build signed APK (push) Successful in 5m22s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 17s
release / Verify release artifacts (tag releases only) (push) Skipped

- A new playlist can carry the client's own id; sending the create
  again finds the same list.
- Removing a song can name the song expected at that position; if
  another song is there now, nothing is removed (409 list_changed).
- A reorder can give the songs in their new order (track_ids); sending
  it again changes nothing.
- The playlist service tells the owner's clients about every change
  itself, so a change made from a Subsonic app refreshes the web and
  Android apps too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 10:22:40 -05:00
co-authored by Claude Opus 5.5
parent 1df91678ba
commit a77f3ae6ff
9 changed files with 421 additions and 88 deletions
+43 -2
View File
@@ -57,20 +57,26 @@ func (q *Queries) AppendPlaylistTrack(ctx context.Context, arg AppendPlaylistTra
}
const createPlaylist = `-- name: CreatePlaylist :one
INSERT INTO playlists (user_id, name, description, is_public)
VALUES ($1, $2, $3, $4)
INSERT INTO playlists (id, user_id, name, description, is_public)
VALUES (COALESCE($1::uuid, gen_random_uuid()), $2, $3, $4, $5)
ON CONFLICT (id) DO NOTHING
RETURNING id, user_id, name, description, is_public, cover_path, track_count, duration_sec, created_at, updated_at, kind, system_variant, seed_artist_id, shelf_rank
`
type CreatePlaylistParams struct {
ID pgtype.UUID
UserID pgtype.UUID
Name string
Description string
IsPublic bool
}
// id is the client's own id for the list, or NULL for a new one. A client
// that made the list offline sends its id again on every retry, so an id
// already taken inserts nothing and returns no row.
func (q *Queries) CreatePlaylist(ctx context.Context, arg CreatePlaylistParams) (Playlist, error) {
row := q.db.QueryRow(ctx, createPlaylist,
arg.ID,
arg.UserID,
arg.Name,
arg.Description,
@@ -309,6 +315,41 @@ func (q *Queries) ListPlaylistCoverAlbums(ctx context.Context, playlistIds []pgt
return items, nil
}
const listPlaylistEntriesForUpdate = `-- name: ListPlaylistEntriesForUpdate :many
SELECT position, track_id
FROM playlist_tracks
WHERE playlist_id = $1
ORDER BY position
FOR UPDATE
`
type ListPlaylistEntriesForUpdateRow struct {
Position int32
TrackID pgtype.UUID
}
// The track at each position, locking the rows while a reorder rewrites
// them.
func (q *Queries) ListPlaylistEntriesForUpdate(ctx context.Context, playlistID pgtype.UUID) ([]ListPlaylistEntriesForUpdateRow, error) {
rows, err := q.db.Query(ctx, listPlaylistEntriesForUpdate, playlistID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListPlaylistEntriesForUpdateRow
for rows.Next() {
var i ListPlaylistEntriesForUpdateRow
if err := rows.Scan(&i.Position, &i.TrackID); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listPlaylistTracks = `-- name: ListPlaylistTracks :many
SELECT pt.playlist_id, pt.position, pt.track_id, pt.title, pt.artist_name, pt.album_title, pt.duration_sec, pt.added_at, pt.pick_kind,
t.id AS live_track_id,
+15 -2
View File
@@ -1,6 +1,10 @@
-- name: CreatePlaylist :one
INSERT INTO playlists (user_id, name, description, is_public)
VALUES ($1, $2, $3, $4)
-- id is the client's own id for the list, or NULL for a new one. A client
-- that made the list offline sends its id again on every retry, so an id
-- already taken inserts nothing and returns no row.
INSERT INTO playlists (id, user_id, name, description, is_public)
VALUES (COALESCE(sqlc.narg(id)::uuid, gen_random_uuid()), sqlc.arg(user_id), sqlc.arg(name), sqlc.arg(description), sqlc.arg(is_public))
ON CONFLICT (id) DO NOTHING
RETURNING *;
-- name: GetPlaylist :one
@@ -49,6 +53,15 @@ UPDATE playlists SET cover_path = $2, updated_at = now() WHERE id = $1;
DELETE FROM playlists WHERE id = $1
RETURNING id, cover_path;
-- name: ListPlaylistEntriesForUpdate :many
-- The track at each position, locking the rows while a reorder rewrites
-- them.
SELECT position, track_id
FROM playlist_tracks
WHERE playlist_id = $1
ORDER BY position
FOR UPDATE;
-- name: ListPlaylistTracks :many
-- Joined to tracks for the live track id (the service layer derives the
-- stream URL from it); LEFT JOIN preserves the row when track_id is NULL