feat(subsonic): apps can make, change and delete playlists (#8021)
release / web (push) Successful in 1m43s
release / go (push) Successful in 2m21s
release / govulncheck (push) Successful in 20s
release / integration (push) Failing after 4m31s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 2m44s
release / android (push) Canceled after 3m43s
release / web (push) Successful in 1m43s
release / go (push) Successful in 2m21s
release / govulncheck (push) Successful in 20s
release / integration (push) Failing after 4m31s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 2m44s
release / android (push) Canceled after 3m43s
createPlaylist makes a playlist, or with playlistId replaces its songs; updatePlaylist renames it, sets its comment and visibility, removes songs by the index the app was shown and adds songs; deletePlaylist deletes it. A song index counts only the entries getPlaylist lists, so a hidden entry ahead of it doesn't shift which song goes. The playlist service gains ReplaceTracks and RemovePositions; the owner and system-mix checks that each change repeated are one function. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+134
-151
@@ -19,6 +19,7 @@ import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
@@ -279,18 +280,8 @@ type UpdateInput struct {
|
||||
// change something.
|
||||
func (s *Service) Update(ctx context.Context, callerID, playlistID pgtype.UUID, in UpdateInput) (*PlaylistRow, error) {
|
||||
q := dbq.New(s.pool)
|
||||
existing, err := q.GetPlaylist(ctx, playlistID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("get-before-update: %w", err)
|
||||
}
|
||||
if !pgtypeUUIDEqual(existing.UserID, callerID) {
|
||||
return nil, ErrForbidden
|
||||
}
|
||||
if existing.Kind == "system" {
|
||||
return nil, ErrSystemPlaylistReadonly
|
||||
if _, err := editable(ctx, q, callerID, playlistID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
params := dbq.UpdatePlaylistParams{
|
||||
@@ -334,18 +325,8 @@ func (s *Service) Update(ctx context.Context, callerID, playlistID pgtype.UUID,
|
||||
// confuse callers about whether the delete took effect).
|
||||
func (s *Service) Delete(ctx context.Context, callerID, playlistID pgtype.UUID) error {
|
||||
q := dbq.New(s.pool)
|
||||
existing, err := q.GetPlaylist(ctx, playlistID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("get-before-delete: %w", err)
|
||||
}
|
||||
if !pgtypeUUIDEqual(existing.UserID, callerID) {
|
||||
return ErrForbidden
|
||||
}
|
||||
if existing.Kind == "system" {
|
||||
return ErrSystemPlaylistReadonly
|
||||
if _, err := editable(ctx, q, callerID, playlistID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deleted, err := q.DeletePlaylist(ctx, playlistID)
|
||||
@@ -366,30 +347,25 @@ func (s *Service) Delete(ctx context.Context, callerID, playlistID pgtype.UUID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AssertEditable returns nil if the playlist exists, the caller owns it,
|
||||
// AND the playlist is user-created (kind='user'). Returns:
|
||||
// - ErrNotFound — playlist doesn't exist
|
||||
// - ErrForbidden — caller is not the owner
|
||||
// - ErrSystemPlaylistReadonly — playlist exists but kind='system'
|
||||
//
|
||||
// Edit handlers call this first; on error, the API layer maps the typed
|
||||
// error to the appropriate status code.
|
||||
func (s *Service) AssertEditable(ctx context.Context, callerID, playlistID pgtype.UUID) error {
|
||||
q := dbq.New(s.pool)
|
||||
// editable loads playlistID for a change by callerID. It answers
|
||||
// ErrNotFound when the playlist doesn't exist, ErrForbidden when the caller
|
||||
// doesn't own it, and ErrSystemPlaylistReadonly for a system mix, which only
|
||||
// Minstrel rebuilds.
|
||||
func editable(ctx context.Context, q *dbq.Queries, callerID, playlistID pgtype.UUID) (dbq.GetPlaylistRow, error) {
|
||||
p, err := q.GetPlaylist(ctx, playlistID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
return p, ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("playlists: load for editability check: %w", err)
|
||||
return p, fmt.Errorf("get playlist: %w", err)
|
||||
}
|
||||
if !pgtypeUUIDEqual(p.UserID, callerID) {
|
||||
return ErrForbidden
|
||||
return p, ErrForbidden
|
||||
}
|
||||
if p.Kind == "system" {
|
||||
return ErrSystemPlaylistReadonly
|
||||
return p, ErrSystemPlaylistReadonly
|
||||
}
|
||||
return nil
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// AppendTracks adds the given track ids at the end of the playlist,
|
||||
@@ -400,128 +376,145 @@ func (s *Service) AppendTracks(ctx context.Context, callerID, playlistID pgtype.
|
||||
if len(trackIDs) == 0 {
|
||||
return fmt.Errorf("%w: at least one track id required", ErrInvalidInput)
|
||||
}
|
||||
q := dbq.New(s.pool)
|
||||
pl, err := q.GetPlaylist(ctx, playlistID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("get playlist: %w", err)
|
||||
}
|
||||
if !pgtypeUUIDEqual(pl.UserID, callerID) {
|
||||
return ErrForbidden
|
||||
}
|
||||
if pl.Kind == "system" {
|
||||
return ErrSystemPlaylistReadonly
|
||||
}
|
||||
return s.changeTracks(ctx, callerID, playlistID, func(tx pgx.Tx) error {
|
||||
return appendTracks(ctx, tx, playlistID, trackIDs)
|
||||
})
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (s *Service) ReplaceTracks(ctx context.Context, callerID, playlistID pgtype.UUID, trackIDs []pgtype.UUID) error {
|
||||
return s.changeTracks(ctx, callerID, playlistID, func(tx pgx.Tx) error {
|
||||
rows, err := tx.Query(ctx,
|
||||
`DELETE FROM playlist_tracks WHERE playlist_id = $1 RETURNING track_id`, playlistID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("clear tracks: %w", err)
|
||||
}
|
||||
removed, err := pgx.CollectRows(rows, pgx.RowTo[pgtype.UUID])
|
||||
if err != nil {
|
||||
return fmt.Errorf("clear tracks: %w", err)
|
||||
}
|
||||
for _, tid := range removed {
|
||||
if err := logTrackChange(ctx, tx, playlistID, tid, syncpkg.OpDelete); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return appendTracks(ctx, tx, playlistID, trackIDs)
|
||||
})
|
||||
}
|
||||
|
||||
// RemoveTrack deletes the row at `position` and renumbers subsequent
|
||||
// rows to close the gap. Triggers cover regeneration after.
|
||||
func (s *Service) RemoveTrack(ctx context.Context, callerID, playlistID pgtype.UUID, position int32) error {
|
||||
return s.RemovePositions(ctx, callerID, playlistID, []int32{position})
|
||||
}
|
||||
|
||||
// RemovePositions deletes the rows at the given positions, all counted
|
||||
// in the list as it stands before the call, and closes the gaps. A
|
||||
// position repeated is removed once; one with no row is ErrNotFound and
|
||||
// removes nothing.
|
||||
func (s *Service) RemovePositions(ctx context.Context, callerID, playlistID pgtype.UUID, positions []int32) error {
|
||||
if len(positions) == 0 {
|
||||
return fmt.Errorf("%w: at least one position required", ErrInvalidInput)
|
||||
}
|
||||
// Highest first, so closing one gap never moves a row still to go.
|
||||
order := slices.Clone(positions)
|
||||
slices.Sort(order)
|
||||
order = slices.Compact(order)
|
||||
slices.Reverse(order)
|
||||
return s.changeTracks(ctx, callerID, playlistID, func(tx pgx.Tx) error {
|
||||
tq := dbq.New(tx)
|
||||
for _, position := range order {
|
||||
// The track id names the change for clients that sync.
|
||||
var deletedTrackID pgtype.UUID
|
||||
if err := tx.QueryRow(ctx,
|
||||
`SELECT track_id FROM playlist_tracks WHERE playlist_id = $1 AND position = $2`,
|
||||
playlistID, position,
|
||||
).Scan(&deletedTrackID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("lookup track at position: %w", err)
|
||||
}
|
||||
if err := tq.DeletePlaylistTrack(ctx, dbq.DeletePlaylistTrackParams{
|
||||
PlaylistID: playlistID,
|
||||
Position: position,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("delete track row: %w", err)
|
||||
}
|
||||
if err := logTrackChange(ctx, tx, playlistID, deletedTrackID, syncpkg.OpDelete); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tq.RenumberPlaylistTracksAfter(ctx, dbq.RenumberPlaylistTracksAfterParams{
|
||||
PlaylistID: playlistID,
|
||||
Position: position,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("renumber: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// changeTracks runs change in one transaction on a playlist callerID may
|
||||
// edit, then updates the playlist's song count and length and redraws its
|
||||
// cover. A cover that fails to draw is logged, not returned: the change
|
||||
// has already committed.
|
||||
func (s *Service) changeTracks(ctx context.Context, callerID, playlistID pgtype.UUID, change func(pgx.Tx) error) error {
|
||||
if _, err := editable(ctx, dbq.New(s.pool), callerID, playlistID); err != nil {
|
||||
return err
|
||||
}
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
tq := dbq.New(tx)
|
||||
|
||||
for _, tid := range trackIDs {
|
||||
_, ierr := tq.AppendPlaylistTrack(ctx, dbq.AppendPlaylistTrackParams{
|
||||
PlaylistID: playlistID,
|
||||
TrackID: tid,
|
||||
})
|
||||
if ierr != nil {
|
||||
// AppendPlaylistTrack uses INSERT ... SELECT FROM tracks WHERE id = X.
|
||||
// If X doesn't exist, no row inserts and the :one query errors with
|
||||
// pgx.ErrNoRows.
|
||||
if errors.Is(ierr, pgx.ErrNoRows) {
|
||||
return ErrTrackNotFound
|
||||
}
|
||||
return fmt.Errorf("append track: %w", ierr)
|
||||
}
|
||||
// Tx-bound: change row commits with the playlist_track row.
|
||||
if lerr := syncpkg.LogChange(ctx, tx, syncpkg.EntityPlaylistTrack,
|
||||
syncpkg.EncodePlaylistTrackID(syncpkg.FormatUUID(playlistID), syncpkg.FormatUUID(tid)),
|
||||
syncpkg.OpUpsert); lerr != nil {
|
||||
return fmt.Errorf("log change: %w", lerr)
|
||||
}
|
||||
if err := change(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tq.UpdatePlaylistRollups(ctx, playlistID); err != nil {
|
||||
if err := dbq.New(tx).UpdatePlaylistRollups(ctx, playlistID); err != nil {
|
||||
return fmt.Errorf("update rollups: %w", err)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
|
||||
// Regenerate cover (synchronous). Failure is logged but not returned —
|
||||
// the playlist mutation already committed.
|
||||
if _, cerr := GenerateCollage(ctx, s.pool, playlistID, s.dataDir); cerr != nil {
|
||||
s.logger.Warn("collage regeneration failed", "playlist_id", playlistID, "err", cerr)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveTrack deletes the row at `position` and renumbers subsequent
|
||||
// rows to close the gap. Triggers cover regeneration after.
|
||||
func (s *Service) RemoveTrack(ctx context.Context, callerID, playlistID pgtype.UUID, position int32) error {
|
||||
q := dbq.New(s.pool)
|
||||
pl, err := q.GetPlaylist(ctx, playlistID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("get playlist: %w", err)
|
||||
}
|
||||
if !pgtypeUUIDEqual(pl.UserID, callerID) {
|
||||
return ErrForbidden
|
||||
}
|
||||
if pl.Kind == "system" {
|
||||
return ErrSystemPlaylistReadonly
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
// appendTracks adds trackIDs at the end of the playlist inside tx. A track
|
||||
// id that matches no track is ErrTrackNotFound.
|
||||
func appendTracks(ctx context.Context, tx pgx.Tx, playlistID pgtype.UUID, trackIDs []pgtype.UUID) error {
|
||||
tq := dbq.New(tx)
|
||||
|
||||
// Capture the track_id before we delete the row — we need it for the
|
||||
// LogChange composite key after the delete commits.
|
||||
var deletedTrackID pgtype.UUID
|
||||
if err := tx.QueryRow(ctx,
|
||||
`SELECT track_id FROM playlist_tracks WHERE playlist_id = $1 AND position = $2`,
|
||||
playlistID, position,
|
||||
).Scan(&deletedTrackID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
for _, tid := range trackIDs {
|
||||
// The insert selects from tracks, so a missing track inserts
|
||||
// nothing and the :one query answers pgx.ErrNoRows.
|
||||
if _, err := tq.AppendPlaylistTrack(ctx, dbq.AppendPlaylistTrackParams{
|
||||
PlaylistID: playlistID,
|
||||
TrackID: tid,
|
||||
}); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrTrackNotFound
|
||||
}
|
||||
return fmt.Errorf("append track: %w", err)
|
||||
}
|
||||
if err := logTrackChange(ctx, tx, playlistID, tid, syncpkg.OpUpsert); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("lookup track at position: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := tq.DeletePlaylistTrack(ctx, dbq.DeletePlaylistTrackParams{
|
||||
PlaylistID: playlistID,
|
||||
Position: position,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("delete track row: %w", err)
|
||||
}
|
||||
if lerr := syncpkg.LogChange(ctx, tx, syncpkg.EntityPlaylistTrack,
|
||||
syncpkg.EncodePlaylistTrackID(syncpkg.FormatUUID(playlistID), syncpkg.FormatUUID(deletedTrackID)),
|
||||
syncpkg.OpDelete); lerr != nil {
|
||||
return fmt.Errorf("log change: %w", lerr)
|
||||
}
|
||||
if err := tq.RenumberPlaylistTracksAfter(ctx, dbq.RenumberPlaylistTracksAfterParams{
|
||||
PlaylistID: playlistID,
|
||||
Position: position,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("renumber: %w", err)
|
||||
}
|
||||
if err := tq.UpdatePlaylistRollups(ctx, playlistID); err != nil {
|
||||
return fmt.Errorf("update rollups: %w", err)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit: %w", err)
|
||||
}
|
||||
|
||||
if _, cerr := GenerateCollage(ctx, s.pool, playlistID, s.dataDir); cerr != nil {
|
||||
s.logger.Warn("collage regeneration failed", "playlist_id", playlistID, "err", cerr)
|
||||
// logTrackChange records a playlist entry's change for clients that sync,
|
||||
// in the transaction that makes it.
|
||||
func logTrackChange(ctx context.Context, tx pgx.Tx, playlistID, trackID pgtype.UUID, op syncpkg.Op) error {
|
||||
if err := syncpkg.LogChange(ctx, tx, syncpkg.EntityPlaylistTrack,
|
||||
syncpkg.EncodePlaylistTrackID(syncpkg.FormatUUID(playlistID), syncpkg.FormatUUID(trackID)),
|
||||
op); err != nil {
|
||||
return fmt.Errorf("log change: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -531,19 +524,9 @@ func (s *Service) RemoveTrack(ctx context.Context, callerID, playlistID pgtype.U
|
||||
// tracks at positions [0, 1, 2, 3] and the caller wants the order
|
||||
// (old[3], old[0], old[1], old[2]), they send [3, 0, 1, 2].
|
||||
func (s *Service) Reorder(ctx context.Context, callerID, playlistID pgtype.UUID, orderedPositions []int32) error {
|
||||
q := dbq.New(s.pool)
|
||||
pl, err := q.GetPlaylist(ctx, playlistID)
|
||||
pl, err := editable(ctx, dbq.New(s.pool), callerID, playlistID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("get playlist: %w", err)
|
||||
}
|
||||
if !pgtypeUUIDEqual(pl.UserID, callerID) {
|
||||
return ErrForbidden
|
||||
}
|
||||
if pl.Kind == "system" {
|
||||
return ErrSystemPlaylistReadonly
|
||||
return err
|
||||
}
|
||||
|
||||
if int32(len(orderedPositions)) != pl.TrackCount {
|
||||
|
||||
Reference in New Issue
Block a user