feat(playlists): playlists list their cover albums, and system mixes say what they hold
release / go (push) Successful in 2m2s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 4m11s
release / android (push) Successful in 4m10s
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 2m48s

Each playlist on the wire (list and get) carries cover_album_ids: up to
six distinct albums with art, in the order they first appear. A shelf of
playlist tiles takes each tile's first album that no tile before it
shows, so no cover repeats along the shelf.

System mixes are stored with a one-line description of what they are
built from, written in one place so the web and Android apps show the
same words. Songs like and genre mixes have none; their names say it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 15:59:44 -05:00
co-authored by Claude Opus 5.5
parent 0e2538e888
commit ec017838a2
8 changed files with 256 additions and 8 deletions
+111
View File
@@ -0,0 +1,111 @@
package playlists_test
import (
"context"
"path/filepath"
"slices"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/playlists"
)
// A playlist's cover albums are its distinct albums that have art, in the
// order they first appear, at most six: a shelf of tiles picks from them so
// no two tiles show the same cover.
func TestCoverAlbums_DistinctCoveredInTrackOrderCappedAtSix(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
q := dbq.New(pool)
user := seedUser(t, pool, "covers")
svc := playlists.NewService(pool, nil, t.TempDir())
// Eight albums, one track each; album B has no art. A second track on
// album A comes third, so A must be counted once.
tracks := map[string]dbq.Track{}
for _, name := range []string{"A", "B", "C", "D", "E", "F", "G", "H"} {
tracks[name] = seedTrack(t, pool, "song "+name, "artist "+name)
if name != "B" {
if _, err := pool.Exec(ctx, `UPDATE albums SET cover_art_path = $2 WHERE id = $1`,
tracks[name].AlbumID, name+".jpg"); err != nil {
t.Fatalf("set cover %s: %v", name, err)
}
}
}
againOnA, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "song A2", AlbumID: tracks["A"].AlbumID, ArtistID: tracks["A"].ArtistID,
DurationMs: 1000, FilePath: filepath.Join(t.TempDir(), "a2.mp3"),
FileSize: 100, FileFormat: "mp3",
})
if err != nil {
t.Fatalf("seed second track on A: %v", err)
}
pl, err := svc.Create(ctx, user.ID, "Shelf test", "", false)
if err != nil {
t.Fatalf("Create: %v", err)
}
order := []pgtype.UUID{tracks["A"].ID, tracks["B"].ID, againOnA.ID}
for _, name := range []string{"C", "D", "E", "F", "G", "H"} {
order = append(order, tracks[name].ID)
}
if err := svc.AppendTracks(ctx, user.ID, pl.ID, order); err != nil {
t.Fatalf("AppendTracks: %v", err)
}
var want []string
for _, name := range []string{"A", "C", "D", "E", "F", "G"} {
want = append(want, uuidString(tracks[name].AlbumID))
}
ids := func(in []pgtype.UUID) []string {
out := make([]string, 0, len(in))
for _, id := range in {
out = append(out, uuidString(id))
}
return out
}
detail, err := svc.Get(ctx, user.ID, pl.ID)
if err != nil {
t.Fatalf("Get: %v", err)
}
if got := ids(detail.CoverAlbumIDs); !slices.Equal(got, want) {
t.Errorf("Get cover albums = %v, want %v", got, want)
}
rows, err := svc.List(ctx, user.ID)
if err != nil {
t.Fatalf("List: %v", err)
}
i := slices.IndexFunc(rows, func(r playlists.PlaylistRow) bool { return r.ID == pl.ID })
if i < 0 {
t.Fatal("List did not return the playlist")
}
if got := ids(rows[i].CoverAlbumIDs); !slices.Equal(got, want) {
t.Errorf("List cover albums = %v, want %v", got, want)
}
}
// A playlist with no covered albums lists none, rather than failing the
// List call that carries every other playlist.
func TestCoverAlbums_EmptyPlaylistHasNone(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
user := seedUser(t, pool, "nocovers")
svc := playlists.NewService(pool, nil, t.TempDir())
pl, err := svc.Create(ctx, user.ID, "Empty", "", false)
if err != nil {
t.Fatalf("Create: %v", err)
}
rows, err := svc.List(ctx, user.ID)
if err != nil {
t.Fatalf("List: %v", err)
}
for _, r := range rows {
if r.ID == pl.ID && len(r.CoverAlbumIDs) != 0 {
t.Errorf("cover albums = %v, want none", r.CoverAlbumIDs)
}
}
}
+39 -2
View File
@@ -75,6 +75,11 @@ type PlaylistRow struct {
SystemVariant *string // 'for_you' | 'songs_like_artist' | nil
SeedArtistID pgtype.UUID // Valid=true only for 'songs_like_artist'
CoverPath *string
// CoverAlbumIDs are up to six distinct albums with cover art, in the
// order they first appear in the playlist. Filled by List and Get;
// empty for a playlist with no covers, and on the rows Create and
// Update return.
CoverAlbumIDs []pgtype.UUID
TrackCount int32
DurationSec int32
CreatedAt pgtype.Timestamptz
@@ -200,10 +205,16 @@ func (s *Service) Get(ctx context.Context, callerID, playlistID pgtype.UUID) (*P
tracks = append(tracks, pt)
}
return &PlaylistDetail{
detail := &PlaylistDetail{
PlaylistRow: *playlistFromGetRow(row),
Tracks: tracks,
}, nil
}
covers, err := coverAlbums(ctx, q, []pgtype.UUID{playlistID})
if err != nil {
return nil, err
}
detail.CoverAlbumIDs = covers[playlistID]
return detail, nil
}
// List returns all playlists visible to callerID: every playlist owned
@@ -216,8 +227,34 @@ func (s *Service) List(ctx context.Context, callerID pgtype.UUID) ([]PlaylistRow
return nil, fmt.Errorf("list playlists: %w", err)
}
out := make([]PlaylistRow, 0, len(rows))
ids := make([]pgtype.UUID, 0, len(rows))
for _, r := range rows {
out = append(out, *playlistFromListRow(r))
ids = append(ids, r.ID)
}
covers, err := coverAlbums(ctx, q, ids)
if err != nil {
return nil, err
}
for i := range out {
out[i].CoverAlbumIDs = covers[out[i].ID]
}
return out, nil
}
// coverAlbums looks up the cover albums of several playlists in one query,
// keyed by playlist id. A playlist with no covered albums is absent.
func coverAlbums(ctx context.Context, q *dbq.Queries, ids []pgtype.UUID) (map[pgtype.UUID][]pgtype.UUID, error) {
out := make(map[pgtype.UUID][]pgtype.UUID, len(ids))
if len(ids) == 0 {
return out, nil
}
rows, err := q.ListPlaylistCoverAlbums(ctx, ids)
if err != nil {
return nil, fmt.Errorf("list cover albums: %w", err)
}
for _, r := range rows {
out[r.PlaylistID] = r.AlbumIds
}
return out, nil
}
+25
View File
@@ -1028,6 +1028,30 @@ func pickHeadAndTail(
return out
}
// systemMixDescription is the one line a client shows under a system mix's
// name, saying what the mix is built from. Kept here so the web and Android
// apps show the same words. Songs like and genre mixes have none: their names
// already say what they hold.
func systemMixDescription(variant string) string {
switch variant {
case "for_you":
return "Built around the songs you play most"
case "discover":
return "Songs you haven’t heard that fit your taste"
case "deep_cuts":
return "Lesser-played tracks by artists you love"
case "rediscover":
return "Albums you loved and haven’t played lately"
case "new_for_you":
return "Recent additions picked for you"
case "on_this_day":
return "What you were playing around this date"
case "first_listens":
return "Songs you’ve never played"
}
return ""
}
// insertSystemPlaylist inserts a kind='system' playlists row plus its
// playlist_tracks via AppendPlaylistTrack (which
// does the snapshot lookup from tracks/albums/artists). Then recomputes
@@ -1047,6 +1071,7 @@ func insertSystemPlaylist(ctx context.Context, qtx *dbq.Queries, userID pgtype.U
p, err := qtx.CreateSystemPlaylist(ctx, dbq.CreateSystemPlaylistParams{
UserID: userID,
Name: name,
Description: systemMixDescription(variant),
SystemVariant: &variantStr,
SeedArtistID: seedArtistID,
CoverPath: nil,