From 1ad0d718c8233fc3c3046aaf2512f15053caf7c7 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Fri, 13 Nov 2026 09:58:04 +0100 Subject: [PATCH] feat(mixes): a picture for each mix, a collage of album covers - The server says which albums stand for a mix (album_ids): up to 4 albums that have a cover, by different artists, with most of the songs the mix draws from. The list gives them for every mix and a mix gives its own. - The Mixes page shows the collage on each card (a 2 by 2 collage with four covers, the first one alone with fewer, the rings with none), and the page of a mix shows it in its header. - Nothing is fetched for it: a mix has a picture once the covers of its albums were looked up. --- README.md | 4 +- internal/api/http/handlers_mixes.go | 15 ++++-- internal/api/http/mixes_test.go | 50 ++++++++++++++++++ internal/api/openapi/openapi.yaml | 13 ++++- internal/mixes/mixes.go | 28 +++++++++- internal/mixes/mixes_test.go | 72 ++++++++++++++++++++++++++ internal/storage/mixes.go | 62 ++++++++++++++++++++-- internal/storage/mixes_test.go | 79 +++++++++++++++++++++++++++++ web/src/api/schema.d.ts | 8 +++ web/src/components/MixCover.tsx | 20 ++++++++ web/src/pages/MixPage.tsx | 64 ++++++++++++----------- web/src/pages/MixesPage.tsx | 10 ++-- web/src/pages/mixes.test.tsx | 42 +++++++++++++-- 13 files changed, 414 insertions(+), 53 deletions(-) create mode 100644 web/src/components/MixCover.tsx diff --git a/README.md b/README.md index 9e6e2e8..08e335a 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr - **Artist descriptions.** `GET /artists/{id}/description` shows what Wikipedia says about an artist: a short text, with the page it comes from, a link to it and the licence (CC BY-SA 4.0), which you must show next to it. With the `wikipedia` provider and the `musicbrainz` one switched on (the link to Wikidata comes from MusicBrainz), Sonda follows ids and never a name: the artist's MusicBrainz id, then the Wikidata item that MusicBrainz links to it, then the Wikipedia page that Wikidata names in the first of your languages (`[wikipedia] languages`, the code of that Wikipedia, for example `["it", "en"]`) that has one. An artist without a MusicBrainz id has no description. It is kept for 30 days and removed with the provider's data. To read the descriptions of the whole library in one go, an admin starts a background job like a scan: `POST /library/descriptions` (follow it with `GET /library/descriptions`, stop it with `DELETE /library/descriptions`; `?refresh=true` reads every artist again). It only goes through artists that have a MusicBrainz id, never replaces a description by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). - **Artist photos.** `GET /artists/{id}/image` serves a free picture of an artist from Wikimedia Commons, and `GET /artists/{id}/image/credit` says whose it is and under which licence, which you must show next to it. Only free licences are kept (public domain, CC0, CC BY, CC BY-SA): non-commercial, no-derivatives, fair-use and unknown ones are refused, because Sonda stores a copy (`?size=small` is 300 pixels, `large` 1000) in the data folder. With the `commons` provider and the `musicbrainz` one switched on, Sonda follows ids and never a name: the artist's MusicBrainz id, the Wikidata item MusicBrainz links to it, the picture Wikidata names for it, and Commons for its licence. Many artists will have none. Kept for 30 days (a week when there is none) and removed, files included, with the provider's data. To look for the pictures of the whole library in one go, an admin starts a background job like a scan: `POST /library/artist-images` (follow it with `GET /library/artist-images`, stop it with `DELETE /library/artist-images`; `?refresh=true` searches every artist again). It only goes through artists that have a MusicBrainz id, never replaces a picture by a failure, and ends at once, saying so, when a provider it needs is off. See [External providers](#external-providers). - **Album covers.** `GET /albums/{id}/cover` serves the cover of an album from your own files: the picture embedded in them, or else a `cover`, `folder`, `front`, `album` or `albumart` image in the album's folder. It is built the first time it is asked for (a few at a time), scaled (`?size=small` is 300 pixels, `large` 1000) and kept in the data folder (`storage.data_dir`, you can delete it: covers are built again). A picture you replace in a folder is noticed within a week, or at once after a full rescan. When your files have no picture at all and an admin switched on the `coverartarchive` provider, the cover is asked from the Cover Art Archive by the MusicBrainz release group id that the tags of the album's files carry (`MUSICBRAINZ_RELEASEGROUPID`; run a full rescan to read it for music you already indexed); your own files always come first, and only that id leaves the server. An album whose files have no id is looked up in MusicBrainz by its artist and title when the `musicbrainz` provider is on too (see the list of what leaves your server). For an album split in `CD1`/`Disc 2` folders, the picture in the folder above is used. Nothing is sent anywhere unless you switched on the `coverartarchive` provider (see [External providers](#external-providers)). It also takes `?access_token=`, so an `` element can use it. To build the covers of the whole library in one go instead of one album at a time, an admin starts a background job like a scan: `POST /library/covers` (follow it with `GET /library/covers`, stop it with `DELETE /library/covers`; `?refresh=true` searches every album again; it never replaces a cover by "no cover" because a disk was not mounted, and keeps the covers that came from the archive if you switched it off). -- **Mixes.** `GET /mixes` lists playlists that Sonda makes for you, like the mixes of a streaming service, from what you listen to and from the genres of your music: one mix for each genre of your library that has at least 30 songs by 4 artists (the genres you play most first), and, once you have some listening history, *Your favourites* and *Rediscover* (artists you like and have not played for a month); *New to you* is the songs you never played. `GET /mixes/{id}` gives a mix of up to 50 songs, at most 3 by one artist and never two of the same artist in a row. They are picked by weighted chance: the artists you play weigh more and songs you skipped lately or artists you avoid are left out. A mix stays the same for a day, and `?variant=2` gives another; `POST /mixes/{id}/playlist` keeps the one you are looking at as a playlist of your own. A mix is worked out when you ask for it, from your own history and the genre tags of your files: nothing is stored and nothing is sent to a provider. The id of a genre mix has the genre in it (`genre:hard rock`), so it is percent-encoded in the path. +- **Mixes.** `GET /mixes` lists playlists that Sonda makes for you, like the mixes of a streaming service, from what you listen to and from the genres of your music: one mix for each genre of your library that has at least 30 songs by 4 artists (the genres you play most first), and, once you have some listening history, *Your favourites* and *Rediscover* (artists you like and have not played for a month); *New to you* is the songs you never played. `GET /mixes/{id}` gives a mix of up to 50 songs, at most 3 by one artist and never two of the same artist in a row. They are picked by weighted chance: the artists you play weigh more and songs you skipped lately or artists you avoid are left out. A mix stays the same for a day, and `?variant=2` gives another; `POST /mixes/{id}/playlist` keeps the one you are looking at as a playlist of your own. A mix is worked out when you ask for it, from your own history and the genre tags of your files: nothing is stored and nothing is sent to a provider. The id of a genre mix has the genre in it (`genre:hard rock`), so it is percent-encoded in the path. Each mix says which albums stand for it (`album_ids`: up to 4 albums that have a cover, by different artists, with most of the songs the mix draws from), for a picture made of their covers; the covers are the ones the library already has, so a mix has a picture once the covers of its albums were looked up (browsing the albums, or the covers job), and none is fetched for it. - **Smart playlists.** A smart playlist is a set of rules instead of a list of songs, worked out again every time you open it, so new songs and your listening change it by themselves. `POST /smart-playlists` keeps a name, the rules, an order (`random`, `most_played`, `least_played`, `recently_added` or `title`) and a limit (50 by default, 500 at most); `GET /smart-playlists/{id}/tracks` gives the songs now. Every rule that is set must hold: genres, artists and albums (is, or is not), file formats and a minimum bitrate (about the same file), added in the last N days (by the date of the file), never played, played or not played in the last N days, played at least N times, skipped or not skipped lately, only artists you like or without the ones you avoid, and the length of the song. `POST /smart-playlists/preview` tries rules without keeping them, and `POST /smart-playlists/{id}/playlist` keeps what you see as a normal playlist (a copy). Only the rules are stored; the songs come from the files and from your own history, nothing is sent to a provider, and a smart playlist is yours alone, and `GET /genres` lists the genres to choose from. A random order stays the same for a day, and `?variant=2` gives another. The year of a song is not in the index yet, so there is no rule about it. A user can have 200. - **Playlists.** `POST /playlists` makes a playlist (a name and an optional description) and `GET /playlists` lists yours, the one changed last first. `POST /playlists/{id}/tracks` adds 1 to 500 tracks at the end or at a `position`, `GET /playlists/{id}/tracks` lists them in order, `PATCH /playlists/{id}/tracks/{item}` moves one and `DELETE` takes one out; `PATCH /playlists/{id}` renames and `DELETE /playlists/{id}` deletes (the tracks stay in the library). A track can be in a playlist more than once, which is why each place has its own item id. A playlist is yours alone: someone else's answers `playlist_not_found`, as one that does not exist. A track whose files were deleted or moved away stays in the playlist and shows `available: false`; it plays again when the files come back and a scan finds them. A user can have 1000 playlists of up to 10000 tracks. Nothing is shared and nothing leaves your server. - **Wishlist and "not interested".** `PUT /wishlist/{artist}` puts an artist on your wishlist (`GET /wishlist` lists it, `DELETE` takes it off), and `POST /discover/{artist}/dismiss` hides an artist from your Discover for good (`DELETE` brings it back, `GET /discover/dismissed` lists them). An artist is wished or dismissed, never both, and both lists are yours alone. Discover marks the wished artists it shows (`wishlisted`) and never suggests a dismissed one. The wishlist is a list for you and sends nothing anywhere; Sonda downloads nothing, but an admin can send an artist that Discover showed to their own Lidarr (see [External providers](#external-providers)), which works for an artist that has a MusicBrainz id and that Sonda knows of from a list or from your files. An artist that only Last.fm named is kept while you have it marked (your lists still show it, but other users cannot see that it exists), and goes with the rest of Last.fm's data if an admin removes Last.fm's data. Removing MusicBrainz's data does not touch it. @@ -346,7 +346,7 @@ npm install SONDA_URL=http://127.0.0.1:8080 npm run dev # open http://localhost:5173, /api is proxied to Sonda ``` -What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys, a volume slider with mute (remembered in the browser) and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Playlists have a page of their own: make one, put a song in it with the button next to any song (or make a new playlist right there), play it, rename it, delete it, move a song up or down and take one out; a song whose files are gone is shown as not available and skipped when the playlist plays. Mixes have a page too: a card for each mix (the ones from your listening first, then one for each genre), and on a mix its songs, Play, Shuffle again (another set of songs) and Save as playlist, which keeps what you see as a playlist of your own. Smart playlists have their own page too: make one with an editor of the rules (genres, format and bitrate, when it was added, your listening and taste, the length, the order and the limit) and a live preview of the songs it gives, play it, shuffle it again (for the random order), keep what you see as a playlist, edit it or delete it; rules by artist and album are in the API only, and an edit keeps them. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it, and to manage accounts: add one by email, give or take away the admin role, reset a password (which ends that person's sessions) and delete an account. The last admin cannot be changed, and your own account has no buttons there. Providers have a card each that says what the provider is sent: turning one on asks first, the API key is write-only (never shown, and gone from the page once saved), and there are a test of the key, the contact or address, removing what a provider gave, and, for Lidarr, the profiles and root folder to use when an artist is added. The bulk jobs (album covers, artist pictures, artist descriptions) each have a card that says what the job sends and to whom: starting one asks first and says it goes through the whole library, reading everything again is a separate choice, a job whose providers are off cannot be started, and the page follows its progress and can stop it. There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings. +What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys, a volume slider with mute (remembered in the browser) and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Playlists have a page of their own: make one, put a song in it with the button next to any song (or make a new playlist right there), play it, rename it, delete it, move a song up or down and take one out; a song whose files are gone is shown as not available and skipped when the playlist plays. Mixes have a page too: a card for each mix, with a collage of the covers of its albums (the ones from your listening first, then one for each genre), and on a mix its songs, Play, Shuffle again (another set of songs) and Save as playlist, which keeps what you see as a playlist of your own. Smart playlists have their own page too: make one with an editor of the rules (genres, format and bitrate, when it was added, your listening and taste, the length, the order and the limit) and a live preview of the songs it gives, play it, shuffle it again (for the random order), keep what you see as a playlist, edit it or delete it; rules by artist and album are in the API only, and an edit keeps them. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it, and to manage accounts: add one by email, give or take away the admin role, reset a password (which ends that person's sessions) and delete an account. The last admin cannot be changed, and your own account has no buttons there. Providers have a card each that says what the provider is sent: turning one on asks first, the API key is write-only (never shown, and gone from the page once saved), and there are a test of the key, the contact or address, removing what a provider gave, and, for Lidarr, the profiles and root folder to use when an artist is added. The bulk jobs (album covers, artist pictures, artist descriptions) each have a card that says what the job sends and to whom: starting one asks first and says it goes through the whole library, reading everything again is a separate choice, a job whose providers are off cannot be started, and the page follows its progress and can stop it. There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings. More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). `npm run build` writes to `internal/webui/dist` (git-ignored), not to `web/dist`. diff --git a/internal/api/http/handlers_mixes.go b/internal/api/http/handlers_mixes.go index 321cecf..268bf03 100644 --- a/internal/api/http/handlers_mixes.go +++ b/internal/api/http/handlers_mixes.go @@ -19,11 +19,12 @@ type mixHandlers struct { } type mixJSON struct { - ID string `json:"id"` - Kind string `json:"kind"` - Title string `json:"title"` - Genre *string `json:"genre"` - Artists []string `json:"artists"` + ID string `json:"id"` + Kind string `json:"kind"` + Title string `json:"title"` + Genre *string `json:"genre"` + Artists []string `json:"artists"` + AlbumIDs []int64 `json:"album_ids"` } type mixWithTracksJSON struct { @@ -37,6 +38,10 @@ func toMixJSON(m mixes.Mix) mixJSON { if out.Artists == nil { out.Artists = []string{} } + out.AlbumIDs = m.AlbumIDs + if out.AlbumIDs == nil { + out.AlbumIDs = []int64{} + } if m.Genre != "" { g := m.Genre out.Genre = &g diff --git a/internal/api/http/mixes_test.go b/internal/api/http/mixes_test.go index 39414ac..df31d3e 100644 --- a/internal/api/http/mixes_test.go +++ b/internal/api/http/mixes_test.go @@ -236,3 +236,53 @@ func TestAMixIsSavedAsAPlaylistWithTheSongsThatWereShown(t *testing.T) { t.Errorf("playlists = %v, want 2", list.Body["total"]) } } + +func TestMixesSayWhichAlbumsStandForThem(t *testing.T) { + m := newMixAPI(t) + r := do(t, m.h, "GET", "/api/v1/mixes", m.alice, nil) + for _, x := range r.Body["items"].([]any) { + if ids, ok := x.(map[string]any)["album_ids"].([]any); !ok || len(ids) != 0 { + t.Fatalf("with no cover a mix has an empty list: %v", x) + } + } + // Five of the eight rock albums (one for each artist) get a cover; a mix shows four. + rows, err := m.db.Query("SELECT DISTINCT t.album_id FROM tracks t WHERE t.title LIKE 'rock song%' ORDER BY t.album_id") + if err != nil { + t.Fatal(err) + } + var albums []int64 + for rows.Next() { + var id int64 + if err := rows.Scan(&id); err != nil { + t.Fatal(err) + } + albums = append(albums, id) + } + rows.Close() + if len(albums) != 8 { + t.Fatalf("albums = %v", albums) + } + for _, id := range albums[:5] { + if _, err := m.db.Exec("DELETE FROM images WHERE kind = 'album' AND owner_id = ?", id); err != nil { + t.Fatal(err) + } + if _, err := m.db.Exec("INSERT INTO images (kind, owner_id, status, checked_at) VALUES ('album', ?, 'ok', 1)", id); err != nil { + t.Fatal(err) + } + } + want := []any{float64(albums[0]), float64(albums[1]), float64(albums[2]), float64(albums[3])} + list := do(t, m.h, "GET", "/api/v1/mixes", m.alice, nil) + var got []any + for _, x := range list.Body["items"].([]any) { + if x.(map[string]any)["id"] == "genre:rock" { + got = x.(map[string]any)["album_ids"].([]any) + } + } + if !slices.Equal(got, want) { + t.Errorf("rock albums = %v, want %v", got, want) + } + one := m.get(t, "genre:rock", "") + if ids := one.Body["album_ids"].([]any); !slices.Equal(ids, want) { + t.Errorf("album_ids of the mix = %v", one.Body["album_ids"]) + } +} diff --git a/internal/api/openapi/openapi.yaml b/internal/api/openapi/openapi.yaml index 6999c5a..cdb292b 100644 --- a/internal/api/openapi/openapi.yaml +++ b/internal/api/openapi/openapi.yaml @@ -3364,13 +3364,21 @@ components: Mix: type: object additionalProperties: false - required: [id, kind, title, genre, artists] + required: [id, kind, title, genre, artists, album_ids] properties: id: {type: string, description: "`favourites`, `rediscover`, `unheard` or `genre:`."} kind: {type: string, enum: [genre, favourites, rediscover, unheard]} title: {type: string, description: In English.} genre: {type: string, nullable: true, description: "The genre, for a genre mix; null for the others."} artists: {type: array, items: {type: string}, description: "A few of the artists it has, up to 3."} + album_ids: + type: array + maxItems: 4 + items: {type: integer, format: int64} + description: | + Up to 4 albums that have a cover and stand for the mix, by different artists, the ones with most songs + among those the mix draws from: their covers (`GET /albums/{id}/cover`) make the picture of the mix. + Empty when none has a cover. MixList: type: object @@ -3384,13 +3392,14 @@ components: MixWithTracks: type: object additionalProperties: false - required: [id, kind, title, genre, artists, variant, tracks] + required: [id, kind, title, genre, artists, album_ids, variant, tracks] properties: id: {type: string} kind: {type: string, enum: [genre, favourites, rediscover, unheard]} title: {type: string} genre: {type: string, nullable: true} artists: {type: array, items: {type: string}} + album_ids: {type: array, maxItems: 4, items: {type: integer, format: int64}, description: "As in the list of mixes."} variant: {type: integer, minimum: 0} tracks: type: array diff --git a/internal/mixes/mixes.go b/internal/mixes/mixes.go index 6dc0b6a..b73e828 100644 --- a/internal/mixes/mixes.go +++ b/internal/mixes/mixes.go @@ -41,6 +41,7 @@ const ( unheardGenres = 3 // New to you takes songs from the best genres this many MaxVariant = 1_000_000 recentPlayFactor = 0.3 + CoverAlbums = 4 // albums that stand for a mix, to make its picture ) // Kind is what a mix is made of. @@ -67,6 +68,9 @@ type Mix struct { Title string // in English; a client may say it in its own words from the kind and the genre Genre string // the genre, for a genre mix Artists []string // a few of the artists it has, up to 3 + // AlbumIDs are up to CoverAlbums albums that have a cover and stand for the mix: by different artists, the + // ones with most songs among those the mix draws from. Empty when none of them has a cover. + AlbumIDs []int64 } // Genre is a genre as the files spell it, with how many songs have it and by how many artists. @@ -104,6 +108,9 @@ type Store interface { GenreArtists(ctx context.Context, genres []string, limit int) ([]string, error) // ArtistGenres returns, for each artist, how many of their songs have each genre. ArtistGenres(ctx context.Context, artistIDs []int64) (map[int64]map[string]int, error) + // Covers returns up to limit albums that have a cover, by different artists, with most playable songs + // of the filter first. + Covers(ctx context.Context, f Filter, limit int) ([]int64, error) // Pool returns the playable songs of the filter. Pool(ctx context.Context, f Filter) ([]TrackRef, error) // History returns what the user did with the songs they did anything with; minListen is how @@ -350,11 +357,24 @@ func (s *Service) List(ctx context.Context, userID int64) ([]Mix, error) { } out := make([]Mix, len(st.defs)) for i, d := range st.defs { - out[i] = d.Mix + if out[i], err = s.withCovers(ctx, d); err != nil { + return nil, err + } } return out, nil } +// withCovers gives the mix the albums that stand for it. +func (s *Service) withCovers(ctx context.Context, d definition) (Mix, error) { + ids, err := s.store.Covers(ctx, Filter{Genres: d.raw, ArtistIDs: d.artists}, CoverAlbums) + if err != nil { + return Mix{}, fmt.Errorf("read covers: %w", err) + } + m := d.Mix + m.AlbumIDs = append([]int64{}, ids...) + return m, nil +} + // Get makes one mix: its songs in order. variant 0 is the mix of the day; another number gives // another mix of the same kind. It gives ErrNotFound for a mix the listener does not have. func (s *Service) Get(ctx context.Context, userID int64, id string, variant int) (Mix, []catalog.Track, error) { @@ -386,7 +406,11 @@ func (s *Service) Get(ctx context.Context, userID int64, id string, variant int) if err != nil { return Mix{}, nil, fmt.Errorf("read songs: %w", err) } - return def.Mix, tracks, nil + mix, err := s.withCovers(ctx, *def) + if err != nil { + return Mix{}, nil, err + } + return mix, tracks, nil } // candidates weighs the songs a mix may take. A song skipped lately is out, and so is every song diff --git a/internal/mixes/mixes_test.go b/internal/mixes/mixes_test.go index a2ae5d2..5e73a35 100644 --- a/internal/mixes/mixes_test.go +++ b/internal/mixes/mixes_test.go @@ -50,6 +50,21 @@ type fakeStore struct { history map[int64]History names map[int64]string asked []Filter + covers map[string][]int64 // key: the first genre spelling or "artists"; the albums to give + coverAsk []Filter +} + +func (f *fakeStore) Covers(_ context.Context, fl Filter, limit int) ([]int64, error) { + f.coverAsk = append(f.coverAsk, fl) + key := "artists" + if len(fl.Genres) > 0 { + key = fl.Genres[0] + } + out := f.covers[key] + if len(out) > limit { + out = out[:limit] + } + return out, nil } func (f *fakeStore) Genres(context.Context) ([]Genre, error) { return f.genres, nil } @@ -407,3 +422,60 @@ func TestNewToYouNeverHasASongThatWasPlayedOrSkipped(t *testing.T) { } } } + +// ---- the albums that stand for a mix ---- + +func TestEveryMixSaysWhichAlbumsStandForIt(t *testing.T) { + st := library() + st.covers = map[string][]int64{"rock": {11, 12, 13, 14, 15}, "metal": {21}, "jazz+funk": {31, 32}, "artists": {41, 42}} + recent := day.Add(-2 * 24 * time.Hour) + old := day.Add(-60 * 24 * time.Hour) + many := learned(pref(1, 5, recent), pref(2, 4, recent), pref(3, 3, old), pref(4, 2, old), pref(5, 1.5, old)) + list, err := newService(st, many).List(context.Background(), 1) + if err != nil { + t.Fatal(err) + } + byID := map[string]Mix{} + for _, m := range list { + byID[m.ID] = m + } + if got := byID["genre:rock"].AlbumIDs; !slices.Equal(got, []int64{11, 12, 13, 14}) { + t.Errorf("rock = %v, want the first four", got) + } + if got := byID["genre:metal"].AlbumIDs; !slices.Equal(got, []int64{21}) { + t.Errorf("metal = %v: a mix with one album with a cover has one", got) + } + if got := byID["genre:jazz funk"].AlbumIDs; !slices.Equal(got, []int64{31, 32}) { + t.Errorf("jazz funk = %v: every spelling is asked", got) + } + if got := byID["favourites"].AlbumIDs; !slices.Equal(got, []int64{41, 42}) { + t.Errorf("favourites = %v", got) + } + if got := byID["genre:pop"].AlbumIDs; got == nil || len(got) != 0 { + t.Errorf("pop = %#v: a mix with no cover says so with an empty list, not nothing", got) + } + // What is asked is what the mix draws from. + var sawArtists, sawGenres bool + for _, f := range st.coverAsk { + sawArtists = sawArtists || len(f.ArtistIDs) > 0 + sawGenres = sawGenres || len(f.Genres) > 0 + } + if !sawArtists || !sawGenres { + t.Errorf("asked %+v", st.coverAsk) + } +} + +func TestOneMixAsksForTheAlbumsOfItself(t *testing.T) { + st := library() + st.covers = map[string][]int64{"rock": {11, 12}} + m, _, err := newService(st, taste.Learned{}).Get(context.Background(), 1, "genre:rock", 0) + if err != nil { + t.Fatal(err) + } + if !slices.Equal(m.AlbumIDs, []int64{11, 12}) { + t.Errorf("albums = %v", m.AlbumIDs) + } + if len(st.coverAsk) != 1 { + t.Errorf("%d questions about covers for one mix: %+v", len(st.coverAsk), st.coverAsk) + } +} diff --git a/internal/storage/mixes.go b/internal/storage/mixes.go index b8bc56d..a8781a9 100644 --- a/internal/storage/mixes.go +++ b/internal/storage/mixes.go @@ -114,11 +114,10 @@ func (r *MixRepo) ArtistGenres(ctx context.Context, artistIDs []int64) (map[int6 return out, rows.Err() } -// Pool returns the playable songs that have one of the genres or are by one of the artists, with -// their main artist (the one with the lowest id when there are several), in the order of their ids. -func (r *MixRepo) Pool(ctx context.Context, f mixes.Filter) ([]mixes.TrackRef, error) { +// filterCondition is the condition on a song (alias t) for a filter: it has one of the genres or is by +// one of the artists. ok is false when the filter asks for nothing. +func filterCondition(f mixes.Filter) (cond string, args []any, ok bool) { var or []string - var args []any if len(f.Genres) > 0 { or = append(or, "EXISTS (SELECT 1 FROM track_genres g WHERE g.track_id = t.id AND g.genre IN (?))") args = append(args, bun.In(f.Genres)) @@ -128,12 +127,22 @@ func (r *MixRepo) Pool(ctx context.Context, f mixes.Filter) ([]mixes.TrackRef, e args = append(args, bun.In(f.ArtistIDs)) } if len(or) == 0 { + return "", nil, false + } + return "(" + strings.Join(or, " OR ") + ")", args, true +} + +// Pool returns the playable songs that have one of the genres or are by one of the artists, with +// their main artist (the one with the lowest id when there are several), in the order of their ids. +func (r *MixRepo) Pool(ctx context.Context, f mixes.Filter) ([]mixes.TrackRef, error) { + cond, args, ok := filterCondition(f) + if !ok { return []mixes.TrackRef{}, nil } vis, _ := visible("t", 0) rows, err := r.db.QueryContext(ctx, `SELECT t.id, COALESCE((SELECT MIN(ta.artist_id) FROM track_artists ta WHERE ta.track_id = t.id AND ta.role = 'main'), 0) - FROM tracks t WHERE `+vis+` AND (`+strings.Join(or, " OR ")+`) ORDER BY t.id`, args...) + FROM tracks t WHERE `+vis+` AND `+cond+` ORDER BY t.id`, args...) if err != nil { return nil, fmt.Errorf("read the pool: %w", err) } @@ -149,6 +158,49 @@ func (r *MixRepo) Pool(ctx context.Context, f mixes.Filter) ([]mixes.TrackRef, e return out, rows.Err() } +// Covers returns up to limit albums that have a cover and playable songs of the filter, the album with +// most of them first, one for each artist (an album with no artist stands for itself). +func (r *MixRepo) Covers(ctx context.Context, f mixes.Filter, limit int) ([]int64, error) { + cond, args, ok := filterCondition(f) + if !ok || limit <= 0 { + return []int64{}, nil + } + vis, _ := visible("t", 0) + // More albums than wanted are read: the ones of an artist that already has one are dropped below. + rows, err := r.db.QueryContext(ctx, + `SELECT a.id, a.artist_id, COUNT(DISTINCT t.id) AS n + FROM albums a + JOIN images i ON i.kind = 'album' AND i.owner_id = a.id AND i.status = 'ok' + JOIN tracks t ON t.album_id = a.id + WHERE `+vis+` AND `+cond+` + GROUP BY a.id, a.artist_id ORDER BY n DESC, a.id LIMIT ?`, append(args, limit*10)...) + if err != nil { + return nil, fmt.Errorf("read covers: %w", err) + } + defer rows.Close() + out := []int64{} + seen := map[int64]bool{} + for rows.Next() { + var ( + id, n int64 + artist sql.NullInt64 + ) + if err := rows.Scan(&id, &artist, &n); err != nil { + return nil, err + } + if artist.Valid { + if seen[artist.Int64] { + continue + } + seen[artist.Int64] = true + } + if len(out) < limit { + out = append(out, id) + } + } + return out, rows.Err() +} + // History returns what the user did with each song they did anything with. A play counts as a // listen when it was completed or lasted minListen; a skip counts as a skip only when it came // before the listener had heard that much (as in the taste model). diff --git a/internal/storage/mixes_test.go b/internal/storage/mixes_test.go index 6803bef..29b70dd 100644 --- a/internal/storage/mixes_test.go +++ b/internal/storage/mixes_test.go @@ -268,3 +268,82 @@ func TestMixRepoTracksComeInTheOrderAskedWithoutTheOnesThatCannotBePlayed(t *tes } }) } + +// cover gives an album a cover record with the status. +func (e *mixEnv) cover(album int64, status string) { + e.t.Helper() + if _, err := e.db.Exec("DELETE FROM images WHERE kind = 'album' AND owner_id = ?", album); err != nil { // the scan may have made one + e.t.Fatal(err) + } + if _, err := e.db.Exec("INSERT INTO images (kind, owner_id, status, checked_at) VALUES ('album', ?, ?, 1)", album, status); err != nil { + e.t.Fatal(err) + } +} + +func (e *mixEnv) albumOf(title string) int64 { + e.t.Helper() + var id int64 + if err := e.db.QueryRow("SELECT album_id FROM tracks WHERE id = ?", e.tracks[title]).Scan(&id); err != nil { + e.t.Fatal(err) + } + return id +} + +func TestMixRepoCoversAreAlbumsWithACoverByDifferentArtistsWithMostSongsFirst(t *testing.T) { + songs := []mixSong{ + {"r1", "Queen", []string{"rock"}}, {"r2", "Queen", []string{"rock"}}, {"r3", "Queen", []string{"rock"}}, + {"k1", "Kiss", []string{"rock"}}, {"k2", "Kiss", []string{"rock"}}, + {"a1", "ABBA", []string{"rock"}}, + {"m1", "Slayer", []string{"metal"}}, {"m2", "Slayer", []string{"metal"}}, + {"p1", "Pooh", []string{"pop"}}, + } + mixBackends(t, songs, func(t *testing.T, e *mixEnv) { + ctx := context.Background() + queen, kiss, abba, slayer := e.albumOf("r1"), e.albumOf("k1"), e.albumOf("a1"), e.albumOf("m1") + // A second album of Queen, with one song, and a cover too: the artist is not shown twice. + var second int64 + if err := e.db.QueryRow("INSERT INTO albums (title, artist_id, title_key, search_key) SELECT 'Second', artist_id, 'second', 'second' FROM albums WHERE id = ? RETURNING id", queen).Scan(&second); err != nil { + t.Fatal(err) + } + if _, err := e.db.Exec("UPDATE tracks SET album_id = ? WHERE id = ?", second, e.tracks["r3"]); err != nil { + t.Fatal(err) + } + e.cover(queen, "ok") + e.cover(second, "ok") + e.cover(kiss, "ok") + e.cover(abba, "none") // no cover + e.cover(slayer, "ok") // another genre + + got, err := e.repo.Covers(ctx, mixes.Filter{Genres: []string{"rock"}}, 4) + if err != nil { + t.Fatal(err) + } + if want := []int64{queen, kiss}; !slices.Equal(got, want) { + t.Errorf("rock = %v, want %v: Queen once (the album with most songs), Kiss, and not the album with no cover", got, want) + } + if got, _ := e.repo.Covers(ctx, mixes.Filter{Genres: []string{"rock"}}, 1); !slices.Equal(got, []int64{queen}) { + t.Errorf("limit 1 = %v", got) + } + // By artist, and by genre or artist together. + got, _ = e.repo.Covers(ctx, mixes.Filter{ArtistIDs: []int64{e.artist["Slayer"]}}, 4) + if !slices.Equal(got, []int64{slayer}) { + t.Errorf("artist = %v", got) + } + got, _ = e.repo.Covers(ctx, mixes.Filter{Genres: []string{"metal"}, ArtistIDs: []int64{e.artist["Kiss"]}}, 4) + if len(got) != 2 { + t.Errorf("metal or Kiss = %v, want two albums", got) + } + // Nothing asked for, nothing given. + if got, _ := e.repo.Covers(ctx, mixes.Filter{}, 4); len(got) != 0 { + t.Errorf("no filter = %v", got) + } + // An album whose songs are all gone is not offered. + if _, err := e.db.Exec("UPDATE audio_files SET missing_at = 1 WHERE track_id IN (?, ?)", e.tracks["k1"], e.tracks["k2"]); err != nil { + t.Fatal(err) + } + got, _ = e.repo.Covers(ctx, mixes.Filter{Genres: []string{"rock"}}, 4) + if !slices.Equal(got, []int64{queen}) { + t.Errorf("after Kiss's files went = %v", got) + } + }) +} diff --git a/web/src/api/schema.d.ts b/web/src/api/schema.d.ts index ddc87b3..5344897 100644 --- a/web/src/api/schema.d.ts +++ b/web/src/api/schema.d.ts @@ -2240,6 +2240,12 @@ export interface components { genre: string | null; /** @description A few of the artists it has, up to 3. */ artists: string[]; + /** + * @description Up to 4 albums that have a cover and stand for the mix, by different artists, the ones with most songs + * among those the mix draws from: their covers (`GET /albums/{id}/cover`) make the picture of the mix. + * Empty when none has a cover. + */ + album_ids: number[]; }; MixList: { items: components["schemas"]["Mix"][]; @@ -2251,6 +2257,8 @@ export interface components { title: string; genre: string | null; artists: string[]; + /** @description As in the list of mixes. */ + album_ids: number[]; variant: number; /** @description In the order to play them. Fewer than 50 when the library has fewer to give. */ tracks: components["schemas"]["Track"][]; diff --git a/web/src/components/MixCover.tsx b/web/src/components/MixCover.tsx new file mode 100644 index 0000000..16f3f16 --- /dev/null +++ b/web/src/components/MixCover.tsx @@ -0,0 +1,20 @@ +import Cover from './Cover' + +/** + * The picture of a mix: the covers of the albums it draws from. Four make a 2 by 2 collage; with fewer the first one + * stands alone, and with none there are the rings of an album with no cover. With `fluid` it shrinks to fit a narrow + * place, `size` being the largest. + */ +export default function MixCover({ albumIds, size, fluid = false }: { albumIds: number[]; size: number; fluid?: boolean }) { + if (albumIds.length < 4) return + return ( +
+ {albumIds.slice(0, 4).map((id) => ( + + ))} +
+ ) +} diff --git a/web/src/pages/MixPage.tsx b/web/src/pages/MixPage.tsx index 62da825..d00be23 100644 --- a/web/src/pages/MixPage.tsx +++ b/web/src/pages/MixPage.tsx @@ -4,6 +4,7 @@ import { useTranslation } from 'react-i18next' import { Link, useParams } from 'react-router-dom' import { ApiError } from '@/api/client' import { useMix, useSaveMix } from '@/api/mixes' +import MixCover from '@/components/MixCover' import { EmptyMessage, ErrorMessage, Loading } from '@/components/Status' import TrackList from '@/components/TrackList' import { Button } from '@/components/ui/button' @@ -39,36 +40,39 @@ function MixView({ id }: { id: string }) { return (
-
-

{title}

- {mix.artists.length > 0 &&

{t('mixes.with', { artists: mix.artists.join(', ') })}

} -

{t('playlists.meta', { count: mix.tracks.length, duration: formatTime(duration) })}

-
- - - +
+ +
+

{title}

+ {mix.artists.length > 0 &&

{t('mixes.with', { artists: mix.artists.join(', ') })}

} +

{t('playlists.meta', { count: mix.tracks.length, duration: formatTime(duration) })}

+
+ + + +
diff --git a/web/src/pages/MixesPage.tsx b/web/src/pages/MixesPage.tsx index 3692cb0..6a54208 100644 --- a/web/src/pages/MixesPage.tsx +++ b/web/src/pages/MixesPage.tsx @@ -2,6 +2,7 @@ import { useTranslation } from 'react-i18next' import { Link } from 'react-router-dom' import { useMixes, type Mix } from '@/api/mixes' import { CARD_GRID } from '@/components/cards' +import MixCover from '@/components/MixCover' import Section from '@/components/Section' import { EmptyMessage, ErrorMessage, Loading } from '@/components/Status' import { mixTitle } from './mixTitle' @@ -9,9 +10,12 @@ import { mixTitle } from './mixTitle' function MixCard({ mix }: { mix: Mix }) { const { t } = useTranslation() return ( - - {mixTitle(t, mix)} - {mix.artists.join(', ')} + + + + {mixTitle(t, mix)} + {mix.artists.join(', ')} + ) } diff --git a/web/src/pages/mixes.test.tsx b/web/src/pages/mixes.test.tsx index e73c0ba..3801d8c 100644 --- a/web/src/pages/mixes.test.tsx +++ b/web/src/pages/mixes.test.tsx @@ -26,12 +26,12 @@ function track(n: number) { } } -function mix(id: string, kind: string, genre: string | null, artists: string[] = ['Radiohead']) { - return { id, kind, title: 'a title in English', genre, artists } +function mix(id: string, kind: string, genre: string | null, artists: string[] = ['Radiohead'], album_ids: number[] = []) { + return { id, kind, title: 'a title in English', genre, artists, album_ids } } -function withTracks(id: string, kind: string, genre: string | null, variant: number, tracks: object[]) { - return { ...mix(id, kind, genre), variant, tracks } +function withTracks(id: string, kind: string, genre: string | null, variant: number, tracks: object[], album_ids: number[] = []) { + return { ...mix(id, kind, genre, ['Radiohead'], album_ids), variant, tracks } } const HARD_ROCK = 'genre%3Ahard%20rock' @@ -218,3 +218,37 @@ test('the mixes made from listening have their own names', async () => { renderPage(, '/mixes/rediscover', '/mixes/:id') expect(await screen.findByRole('heading', { level: 1, name: 'Rediscover' })).toBeInTheDocument() }) + +// --- the picture of a mix --------------------------------------------------------------------- + +const coversOf = (el: HTMLElement) => Array.from(el.querySelectorAll('img')).map((img) => img.getAttribute('src')) + +test('a mix with four albums with a cover shows them as a collage on its card', async () => { + mockFetch({ 'GET /api/v1/mixes': () => ({ items: [mix('genre:rock', 'genre', 'rock', ['Queen'], [11, 12, 13, 14, 15])] }) }) + renderPage(, '/mixes', '/mixes') + const card = await screen.findByRole('link', { name: /Rock Mix/ }) + expect(coversOf(card)).toEqual(['/api/v1/albums/11/cover', '/api/v1/albums/12/cover', '/api/v1/albums/13/cover', '/api/v1/albums/14/cover']) +}) + +test('a mix with fewer than four albums shows the first one whole', async () => { + mockFetch({ 'GET /api/v1/mixes': () => ({ items: [mix('genre:rock', 'genre', 'rock', ['Queen'], [11, 12, 13])] }) }) + renderPage(, '/mixes', '/mixes') + const card = await screen.findByRole('link', { name: /Rock Mix/ }) + expect(coversOf(card)).toEqual(['/api/v1/albums/11/cover']) +}) + +test('a mix with no cover has the rings in place of a picture', async () => { + mockFetch({ 'GET /api/v1/mixes': () => ({ items: [mix('unheard', 'unheard', null)] }) }) + renderPage(, '/mixes', '/mixes') + const card = await screen.findByRole('link', { name: /New to you/ }) + expect(coversOf(card)).toEqual([]) + expect(card.querySelector('[data-placeholder]')).not.toBeNull() +}) + +test('the page of a mix shows the same collage', async () => { + mockFetch(oneMix({ [`GET /api/v1/mixes/${HARD_ROCK}`]: () => withTracks('genre:hard rock', 'genre', 'hard rock', 0, songs(1, 2), [5, 6, 7, 8]) })) + open() + const title = await screen.findByRole('heading', { level: 1, name: 'Hard Rock Mix' }) + const header = title.closest('header')! + expect(coversOf(header)).toEqual(['/api/v1/albums/5/cover', '/api/v1/albums/6/cover', '/api/v1/albums/7/cover', '/api/v1/albums/8/cover']) +})