From 407b20334a44482c332b5986f8e28ce5a88d437d Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 16 Nov 2026 12:13:55 +0100 Subject: [PATCH] feat: radio stations that Sonda makes by itself MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stations made from the music, random and without end, with nothing to set up: the mixes the listener has (genres, favourites, rediscover, new to you), everything, one for each decade that has 30 songs by 4 artists, and the radio of an artist (their songs, four times as likely, and the songs of the two genres most of theirs have). Every time a station is asked for it picks other songs, with the same weights as the mixes (taste, hearts, nothing skipped lately). - mixes.Service.Radios and Radio; Filter has All and years, the store has Decades - GET /radio and GET /radio/{id}, in the OpenAPI spec - OpenSubsonic: each station is a playlist (rd-, "Radio · Rock") that has other songs every time it is opened, for apps that cannot play a radio that never ends --- cmd/sondad/main.go | 5 +- internal/api/http/api_test.go | 2 +- internal/api/http/contract_test.go | 13 ++ internal/api/http/covers_test.go | 9 +- internal/api/http/handlers_mixes.go | 59 +++++++++ internal/api/http/radio_test.go | 138 +++++++++++++++++++ internal/api/http/router.go | 4 + internal/api/openapi/openapi.yaml | 90 +++++++++++++ internal/mixes/mixes.go | 60 +++++++-- internal/mixes/mixes_test.go | 182 ++++++++++++++++++++++++++ internal/mixes/radio.go | 152 +++++++++++++++++++++ internal/opensubsonic/handler.go | 1 + internal/opensubsonic/handler_test.go | 93 +++++++++++++ internal/opensubsonic/playlists.go | 65 +++++++++ internal/storage/mixes.go | 46 ++++++- internal/storage/mixes_test.go | 44 +++++++ web/src/api/schema.d.ts | 145 +++++++++++++++++++- 17 files changed, 1090 insertions(+), 18 deletions(-) create mode 100644 internal/api/http/radio_test.go create mode 100644 internal/mixes/radio.go diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index ee83a44..cf79443 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -305,6 +305,7 @@ func run() error { deviceSvc := devices.NewService(storage.NewDevicesRepo(db), []byte(cfg.Auth.JWTSecret)) catalogSvc := catalog.NewService(storage.NewCatalogRepo(db)) streamSvc := streaming.NewService(libraryRepo) + mixSvc := mixes.NewService(storage.NewMixRepo(db), tasteService, cfg.Listening.MinListen()) favoriteSvc := favorites.NewService(storage.NewFavoritesRepo(db), catalogSvc) listeningSvc := listening.NewService(storage.NewListeningRepo(db), cfg.Listening.MinListen()) subsonicRepo := storage.NewSubsonicRepo(db, cfg.Listening.MinListen()) @@ -322,7 +323,7 @@ func run() error { OpenSubsonic: &opensubsonic.Deps{ Login: deviceSvc, Catalog: catalogSvc, Libraries: libraries, Extras: subsonicRepo, Lists: subsonicRepo, Streams: streamSvc, Covers: pictures, ArtistImages: pictures, - Playlists: playlistSvc, Listens: listeningSvc, Announcer: mastodonSvc, Favorites: favoriteSvc, + Playlists: playlistSvc, Listens: listeningSvc, Announcer: mastodonSvc, Favorites: favoriteSvc, Radio: mixSvc, ServerVersion: version, }, Scan: scanControl{ctx: ctx, m: scans}, @@ -340,7 +341,7 @@ func run() error { Playlists: playlistSvc, Suggestions: affinity.NewService(affinityRepo, affinityRepo, feedbacks, affinityRepo, userSettings, cfg.Listening.MinListen()), SpotifyImport: spotifyimport.NewService(storage.NewCatalogRepo(db), storage.NewListeningRepo(db), playlistSvc, storage.NewSpotifyImportRepo(db), storage.NewOutsidePlayRepo(db), storage.SearchKey, cfg.Listening.MinListen()), - Mixes: mixes.NewService(storage.NewMixRepo(db), tasteService, cfg.Listening.MinListen()), + Mixes: mixSvc, Smart: smart.NewService(storage.NewSmartRepo(db), tasteService, cfg.Listening.MinListen()), Naming: namingService, Images: pictures, diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index 1cf172e..c8a8898 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -150,7 +150,7 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun Libraries: deps.Libraries.(opensubsonic.Libraries), ServerVersion: "test", Extras: subsonic, Lists: subsonic, Streams: deps.Streams.(opensubsonic.Streams), Covers: deps.Images.(opensubsonic.Covers), ArtistImages: deps.ArtistImages.(opensubsonic.ArtistImages), - Playlists: deps.Playlists.(opensubsonic.Playlists), Listens: deps.Listening.(opensubsonic.Listens), Announcer: deps.Mastodon.(opensubsonic.Announcer), Favorites: deps.Favorites.(opensubsonic.Favorites), + Playlists: deps.Playlists.(opensubsonic.Playlists), Listens: deps.Listening.(opensubsonic.Listens), Announcer: deps.Mastodon.(opensubsonic.Announcer), Favorites: deps.Favorites.(opensubsonic.Favorites), Radio: deps.Mixes.(opensubsonic.Radio), } } if deps.About == nil { diff --git a/internal/api/http/contract_test.go b/internal/api/http/contract_test.go index b76fc21..f15d49d 100644 --- a/internal/api/http/contract_test.go +++ b/internal/api/http/contract_test.go @@ -904,6 +904,19 @@ func TestResponsesMatchTheSpec(t *testing.T) { c.expect(c.call("DELETE", "/me/devices/"+idOf(made), token, nil), 404, "take back a device twice", "device_not_found") c.expect(c.call("GET", "/me/devices", "", nil), 401, "devices without a token", "unauthorized") + // The radio: stations nobody made, and other songs every time. + if got := c.expect(c.call("GET", "/radio", token, nil), 200, "the radio"); len(got["items"].([]any)) == 0 { + c.t.Errorf("radio = %v", got) + } + if got := c.expect(c.call("GET", "/radio/all", token, nil), 200, "everything on the radio"); len(got["tracks"].([]any)) == 0 || got["decade"] != nil { + c.t.Errorf("station = %v", got) + } + if got := c.expect(c.call("GET", "/radio/artist:"+artistID, token, nil), 200, "the radio of an artist"); got["kind"] != "artist" { + c.t.Errorf("station = %v", got) + } + c.expect(c.call("GET", "/radio/nope", token, nil), 404, "a station that is not there", "station_not_found") + c.expect(c.call("GET", "/radio", "", nil), 401, "radio without a token", "unauthorized") + // Favourites: what is marked is listed, newest first, and only the person's own. if got := c.expect(c.call("GET", "/me/favorites/ids", token, nil), 200, "no favourites yet"); len(got["tracks"].([]any)) != 0 { c.t.Errorf("favourites = %v", got) diff --git a/internal/api/http/covers_test.go b/internal/api/http/covers_test.go index 9f436b8..3692b59 100644 --- a/internal/api/http/covers_test.go +++ b/internal/api/http/covers_test.go @@ -12,6 +12,7 @@ import ( "net/url" "os" "path/filepath" + "strings" "sync/atomic" "testing" @@ -397,7 +398,13 @@ func TestPlaylistsAndListensOfAnAppAreThoseOfThePerson(t *testing.T) { if web.Code != 201 { t.Fatalf("%d %v", web.Code, web.Body) } - if n := len(rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any)); n != 2 { + own := 0 // the stations of the radio are in the list too: only the playlists of the person are counted + for _, p := range rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any) { + if strings.HasPrefix(p.(map[string]any)["id"].(string), "pl-") { + own++ + } + } + if n := own; n != 2 { t.Fatalf("the app sees %d playlists", n) } rest("deletePlaylist", url.Values{"id": {made["id"].(string)}}) diff --git a/internal/api/http/handlers_mixes.go b/internal/api/http/handlers_mixes.go index 84a935c..e192bee 100644 --- a/internal/api/http/handlers_mixes.go +++ b/internal/api/http/handlers_mixes.go @@ -144,3 +144,62 @@ func (h *mixHandlers) save(c *gin.Context) { name, description := automaticName(c, h.naming, req.AutoName, req.Lang, "mix", tracks, "", name) keepAsPlaylist(c, h.playlists, user, name, description, tracks) } + +// radioJSON is a station of the radio: a mix that is not for a day, and has decades, everything and artists. +type radioJSON struct { + ID string `json:"id"` + Kind string `json:"kind"` + Title string `json:"title"` + Genre *string `json:"genre"` + Decade *int `json:"decade"` + Artists []string `json:"artists"` + AlbumIDs []int64 `json:"album_ids"` +} + +type radioWithTracksJSON struct { + radioJSON + Tracks []trackJSON `json:"tracks"` +} + +func toRadioJSON(m mixes.Mix) radioJSON { + base := toMixJSON(m) + out := radioJSON{ID: base.ID, Kind: base.Kind, Title: base.Title, Genre: base.Genre, Artists: base.Artists, AlbumIDs: base.AlbumIDs} + if m.Decade > 0 { + d := m.Decade + out.Decade = &d + } + return out +} + +func writeRadioError(c *gin.Context, err error) { + if errors.Is(err, mixes.ErrNotFound) { + writeError(c, http.StatusNotFound, "station_not_found", "no such station") + return + } + writeMixError(c, err) +} + +// radioList is every station of the radio of the signed-in user. +func (h *mixHandlers) radioList(c *gin.Context) { + list, err := h.svc.Radios(c.Request.Context(), currentUserID(c)) + if err != nil { + writeRadioError(c, err) + return + } + out := make([]radioJSON, len(list)) + for i, m := range list { + out[i] = toRadioJSON(m) + } + c.JSON(http.StatusOK, gin.H{"items": out}) +} + +// radioGet is songs of a station: other ones every time it is asked. +func (h *mixHandlers) radioGet(c *gin.Context) { + m, tracks, err := h.svc.Radio(c.Request.Context(), currentUserID(c), c.Param("id")) + if err != nil { + writeRadioError(c, err) + return + } + c.Header("Cache-Control", "no-store") + c.JSON(http.StatusOK, radioWithTracksJSON{radioJSON: toRadioJSON(m), Tracks: toTracksJSON(tracks)}) +} diff --git a/internal/api/http/radio_test.go b/internal/api/http/radio_test.go new file mode 100644 index 0000000..31e5a91 --- /dev/null +++ b/internal/api/http/radio_test.go @@ -0,0 +1,138 @@ +package httpapi + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "net/url" + "slices" + "testing" +) + +func (m *mixAPI) radio(t *testing.T, id string) resp { + t.Helper() + return do(t, m.h, "GET", "/api/v1/radio/"+url.PathEscape(id), m.alice, nil) +} + +func TestRadioNeedsASignedInUser(t *testing.T) { + m := newMixAPI(t) + for _, path := range []string{"/api/v1/radio", "/api/v1/radio/all"} { + if r := do(t, m.h, "GET", path, "", nil); r.Code != http.StatusUnauthorized { + t.Errorf("%s: %d", path, r.Code) + } + } +} + +func TestTheRadioHasStationsThatNobodyMade(t *testing.T) { + m := newMixAPI(t) + r := do(t, m.h, "GET", "/api/v1/radio", m.alice, nil) + if r.Code != http.StatusOK { + t.Fatalf("%d %v", r.Code, r.Body) + } + var ids []string + for _, x := range r.Body["items"].([]any) { + s := x.(map[string]any) + sameKeys(t, "station", s, "id", "kind", "title", "genre", "decade", "artists", "album_ids") + ids = append(ids, s["id"].(string)) + } + for _, want := range []string{"all", "genre:rock", "genre:hard rock", "unheard"} { + if !slices.Contains(ids, want) { + t.Errorf("no %s in %v", want, ids) + } + } +} + +func TestEveryRoundOfAStationIsOtherSongs(t *testing.T) { + m := newMixAPI(t) + a := do(t, m.h, "GET", "/api/v1/radio/all", m.alice, nil) + if a.Code != http.StatusOK || a.Body["id"] != "all" || a.Body["kind"] != "all" { + t.Fatalf("%d %v", a.Code, a.Body) + } + first := trackIDs(t, a) + if len(first) != 50 { + t.Fatalf("%d songs", len(first)) + } + if _, has := a.Body["variant"]; has { + t.Fatal("a radio has no variant") + } + same := 0 + for i := 0; i < 3; i++ { + if slices.Equal(first, trackIDs(t, do(t, m.h, "GET", "/api/v1/radio/all", m.alice, nil))) { + same++ + } + } + if same == 3 { + t.Fatal("a radio that plays the same songs every time") + } + if r := do(t, m.h, "GET", "/api/v1/radio/"+url.PathEscape("genre:hard rock"), m.alice, nil); r.Code != http.StatusOK || len(trackIDs(t, r)) != 36 { // all it has + t.Fatalf("hard rock: %d %d songs", r.Code, len(trackIDs(t, r))) + } +} + +func TestTheRadioOfAnArtist(t *testing.T) { + m := newMixAPI(t) + var id int64 + if err := m.db.QueryRow("SELECT id FROM artists WHERE name = 'rock artist 0'").Scan(&id); err != nil { + t.Fatal(err) + } + r := do(t, m.h, "GET", "/api/v1/radio/artist:"+itoa(id), m.alice, nil) + if r.Code != http.StatusOK || r.Body["kind"] != "artist" || r.Body["title"] != "rock artist 0" { + t.Fatalf("%d %v", r.Code, r.Body) + } + if len(trackIDs(t, r)) != 40 { // the 40 rock songs: the genre of the artist + t.Fatalf("%d songs", len(trackIDs(t, r))) + } +} + +func TestAStationThatIsNotThereIsNotFound(t *testing.T) { + m := newMixAPI(t) + for _, id := range []string{"nope", "genre:polka", "artist:999999", "artist:x", "decade:1850"} { + if r := do(t, m.h, "GET", "/api/v1/radio/"+url.PathEscape(id), m.alice, nil); r.Code != http.StatusNotFound || errCode(r) != "station_not_found" { + t.Errorf("%s: %d %v", id, r.Code, r.Body) + } + } +} + +func TestAnAppSeesTheStationsAsPlaylistsWithOtherSongsEveryTime(t *testing.T) { + m := newMixAPI(t) + secret := do(t, m.h, "POST", "/api/v1/me/devices", m.alice, map[string]any{"name": "Feishin"}).Body["secret"].(string) + rest := func(path string, q url.Values) map[string]any { + q.Set("apiKey", secret) + q.Set("f", "json") + return restCall(t, m.h, path, q) + } + var found bool + for _, p := range rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any) { + if p.(map[string]any)["id"] == "rd-all" { + found = p.(map[string]any)["name"] == "Radio · Everything" + } + } + if !found { + t.Fatal("no Radio · Everything in the playlists") + } + songs := func() []string { + var out []string + for _, e := range rest("getPlaylist", url.Values{"id": {"rd-all"}})["playlist"].(map[string]any)["entry"].([]any) { + out = append(out, e.(map[string]any)["id"].(string)) + } + return out + } + a, b := songs(), songs() + if len(a) != 50 || slices.Equal(a, b) { + t.Fatalf("%d songs, and the same twice: %v", len(a), slices.Equal(a, b)) + } +} + +// restCall asks /rest and returns what is inside subsonic-response, failing when the call failed. +func restCall(t *testing.T, h http.Handler, path string, q url.Values) map[string]any { + t.Helper() + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil)) + var out struct { + R map[string]any `json:"subsonic-response"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || out.R["status"] != "ok" { + t.Fatalf("%s: %v %s", path, err, rec.Body.String()) + } + return out.R +} diff --git a/internal/api/http/router.go b/internal/api/http/router.go index 72aa9f0..a095e35 100644 --- a/internal/api/http/router.go +++ b/internal/api/http/router.go @@ -120,6 +120,8 @@ type spotifyImportService interface { type mixService interface { List(ctx context.Context, userID int64) ([]mixes.Mix, error) Get(ctx context.Context, userID int64, id string, variant int) (mixes.Mix, []catalog.Track, error) + Radios(ctx context.Context, userID int64) ([]mixes.Mix, error) + Radio(ctx context.Context, userID int64, id string) (mixes.Mix, []catalog.Track, error) } // namingService is what the handlers need from naming.Service. @@ -359,6 +361,8 @@ func NewRouter(d Deps) *gin.Engine { protected.GET("/mixes", mx.list) protected.GET("/mixes/:id", mx.get) protected.POST("/mixes/:id/playlist", mx.save) + protected.GET("/radio", mx.radioList) + protected.GET("/radio/:id", mx.radioGet) nm := &namingHandlers{svc: d.Naming, playlists: d.Playlists, mixes: d.Mixes, smart: d.Smart} protected.GET("/naming", nm.status) protected.POST("/playlists/:id/suggest-name", nm.playlistName) diff --git a/internal/api/openapi/openapi.yaml b/internal/api/openapi/openapi.yaml index 3d27f7f..c7219df 100644 --- a/internal/api/openapi/openapi.yaml +++ b/internal/api/openapi/openapi.yaml @@ -1288,6 +1288,57 @@ paths: '401': {$ref: '#/components/responses/Unauthorized'} '404': {$ref: '#/components/responses/MixNotFound'} + /radio: + get: + tags: [Mixes] + operationId: listRadio + summary: The stations of your radio + description: | + Stations that Sonda makes by itself from your music, for you alone: nobody makes them and there is nothing to + set up. They are the mixes you have (`genre`, `favourites`, `rediscover`, `unheard`, see `GET /mixes`), then + `all` (every song) and one `decade` for each decade that has at least 30 songs by at least 4 artists (from the + year of the tags), the oldest first. The station of an artist, `artist:`, is not in the list: it is asked + for from the page of an artist. `title` is in English. Nothing is stored and nothing is sent to a provider. + security: [{bearerAuth: []}] + responses: + '200': + description: The stations. + content: + application/json: + schema: {$ref: '#/components/schemas/RadioStationList'} + '401': {$ref: '#/components/responses/Unauthorized'} + + /radio/{id}: + parameters: + - name: id + in: path + required: true + description: "A station of the list (`all`, `decade:1990`, `genre:rock`...) or `artist:` for the songs of an artist and of the genres they play (percent-encoded)." + schema: {type: string} + get: + tags: [Mixes] + operationId: getRadio + summary: Songs of a station + description: | + Up to 50 songs, picked by weighted chance like a mix (the artists you play weigh more, songs you never played a + bit more, songs played in the last days less; songs you skipped lately and artists you avoid do not come; at + most 3 by one artist, never two by the same artist in a row), but **other songs every time you ask**: ask again + when the queue is nearly over and the radio never ends. In the station of an artist their songs weigh four + times as much. A station you do not have is `station_not_found`. + security: [{bearerAuth: []}] + responses: + '200': + description: The station and its songs. + content: + application/json: + schema: {$ref: '#/components/schemas/RadioStationWithTracks'} + '401': {$ref: '#/components/responses/Unauthorized'} + '404': + description: There is no such station (`station_not_found`). + content: + application/json: + schema: {$ref: '#/components/schemas/Error'} + /mixes/{id}/playlist: parameters: - {$ref: '#/components/parameters/MixId'} @@ -5567,6 +5618,45 @@ components: description: In the order to play them. Fewer than 50 when the library has fewer to give. items: {$ref: '#/components/schemas/Track'} + RadioStation: + type: object + additionalProperties: false + required: [id, kind, title, genre, decade, artists, album_ids] + properties: + id: {type: string, description: "`all`, `decade:`, `artist:`, or the id of a mix: `favourites`, `rediscover`, `unheard`, `genre:`."} + kind: {type: string, enum: [genre, favourites, rediscover, unheard, all, decade, artist]} + title: {type: string, description: In English.} + genre: {type: string, nullable: true, description: "The genre, for a genre station; null for the others."} + decade: {type: integer, nullable: true, description: "The first year of the decade (1990), for a decade station; 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: "As in the list of mixes."} + + RadioStationList: + type: object + additionalProperties: false + required: [items] + properties: + items: + type: array + items: {$ref: '#/components/schemas/RadioStation'} + + RadioStationWithTracks: + type: object + additionalProperties: false + required: [id, kind, title, genre, decade, artists, album_ids, tracks] + properties: + id: {type: string} + kind: {type: string, enum: [genre, favourites, rediscover, unheard, all, decade, artist]} + title: {type: string} + genre: {type: string, nullable: true} + decade: {type: integer, nullable: true} + artists: {type: array, items: {type: string}} + album_ids: {type: array, maxItems: 4, items: {type: integer, format: int64}} + tracks: + type: array + description: In the order to play them. Fewer than 50 when the library has fewer to give. + items: {$ref: '#/components/schemas/Track'} + SaveMix: type: object additionalProperties: false diff --git a/internal/mixes/mixes.go b/internal/mixes/mixes.go index 2884d69..dcbd0b3 100644 --- a/internal/mixes/mixes.go +++ b/internal/mixes/mixes.go @@ -17,6 +17,7 @@ import ( "errors" "fmt" "hash/fnv" + "math/rand/v2" "sort" "strings" "time" @@ -53,6 +54,10 @@ const ( KindFavourites Kind = "favourites" KindRediscover Kind = "rediscover" KindUnheard Kind = "unheard" + // The kinds that only the radio has. + KindAll Kind = "all" + KindDecade Kind = "decade" + KindArtist Kind = "artist" ) // Errors returned by the service. @@ -68,6 +73,7 @@ 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 + Decade int // the first year of the decade, for a decade station // 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 @@ -97,6 +103,8 @@ type Heart struct { // hearted album 1.25 times, and both 1.75 times. const ( heartTrackBonus = 0.5 + // artistFocus is how many times more the songs of the artist of an artist station are wanted. + artistFocus = 4 heartAlbumBonus = 0.25 ) @@ -119,10 +127,21 @@ type TrackRef struct { } // Filter says which songs a mix draws from. Both lists narrow the choice: a song is in when it has one -// of the genres or is by one of the artists (an empty list adds nothing). +// of the genres or is by one of the artists (an empty list adds nothing). All takes every song; the years, when +// they are set, keep the songs of those years among the ones chosen (or among all, when only the years are set). type Filter struct { Genres []string // as the files spell them ArtistIDs []int64 + All bool + MinYear int // 0 is no limit + MaxYear int +} + +// Decade is a decade of the music, with how many songs and artists it has. +type Decade struct { + Start int // the first year: 1990 + Tracks int + Artists int } // Store reads what the mixes are made of. It never writes. @@ -141,6 +160,8 @@ type Store interface { // History returns what the user did with the songs they did anything with; minListen is how // long a play must last to count as a listen. History(ctx context.Context, userID int64, minListen time.Duration) (map[int64]History, error) + // Decades returns every decade the playable songs with a year are from. + Decades(ctx context.Context) ([]Decade, error) // Hearts returns, for the songs that are favourites of the user or are on a favourite album, which of the two. Hearts(ctx context.Context, userID int64) (map[int64]Heart, error) ArtistNames(ctx context.Context, ids []int64) (map[int64]string, error) @@ -159,11 +180,13 @@ type Service struct { taster Taster minListen time.Duration now func() time.Time + // seed is where the radio gets its luck from: random, unless a test says otherwise. + seed func() uint64 } // NewService builds a Service. minListen is how long a play must last to count as a listen. func NewService(store Store, taster Taster, minListen time.Duration) *Service { - return &Service{store: store, taster: taster, minListen: minListen, now: time.Now} + return &Service{store: store, taster: taster, minListen: minListen, now: time.Now, seed: rand.Uint64} } // junkGenres are tags that say nothing about the music. @@ -220,6 +243,15 @@ type definition struct { Mix raw []string // genre spellings, for a genre mix and for New to you artists []int64 // artists to draw from, for favourites and rediscover + // The radio's own: every song, or the songs of some years, and the artist an artist station is about. + all bool + minYear, maxYear int + focus int64 +} + +// filter is what the store is asked for the songs of a definition. +func (d definition) filter() Filter { + return Filter{Genres: d.raw, ArtistIDs: d.artists, All: d.all, MinYear: d.minYear, MaxYear: d.maxYear} } // state is what the list of mixes and one mix are both made from. @@ -393,7 +425,7 @@ func (s *Service) List(ctx context.Context, userID int64) ([]Mix, error) { // 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) + ids, err := s.store.Covers(ctx, d.filter(), CoverAlbums) if err != nil { return Mix{}, fmt.Errorf("read covers: %w", err) } @@ -422,18 +454,23 @@ func (s *Service) Get(ctx context.Context, userID int64, id string, variant int) return Mix{}, nil, ErrNotFound } - cands, err := s.candidates(ctx, userID, st, *def) + h := fnv.New64a() + fmt.Fprintf(h, "%d|%s|%d|%d", userID, id, s.now().Unix()/86400, variant) + return s.make(ctx, userID, st, *def, h.Sum64()) +} + +// make picks the songs of a definition with a seed. +func (s *Service) make(ctx context.Context, userID int64, st state, d definition, seed uint64) (Mix, []catalog.Track, error) { + cands, err := s.candidates(ctx, userID, st, d) if err != nil { return Mix{}, nil, err } - h := fnv.New64a() - fmt.Fprintf(h, "%d|%s|%d|%d", userID, id, s.now().Unix()/86400, variant) - picked := Pick(cands, Size, PerArtist, h.Sum64()) + picked := Pick(cands, Size, PerArtist, seed) tracks, err := s.store.Tracks(ctx, picked) if err != nil { return Mix{}, nil, fmt.Errorf("read songs: %w", err) } - mix, err := s.withCovers(ctx, *def) + mix, err := s.withCovers(ctx, d) if err != nil { return Mix{}, nil, err } @@ -444,7 +481,7 @@ func (s *Service) Get(ctx context.Context, userID int64, id string, variant int) // of an artist the listener avoids; a song played in the last days is less wanted; a song the listener hearted, or one // on an album they hearted, is wanted more (not in New to you, which is about what they have not met). func (s *Service) candidates(ctx context.Context, userID int64, st state, d definition) ([]Candidate, error) { - pool, err := s.store.Pool(ctx, Filter{Genres: d.raw, ArtistIDs: d.artists}) + pool, err := s.store.Pool(ctx, d.filter()) if err != nil { return nil, fmt.Errorf("read the pool: %w", err) } @@ -469,12 +506,15 @@ func (s *Service) candidates(ctx context.Context, userID int64, st state, d defi } var w float64 switch d.Kind { - case KindGenre: + case KindGenre, KindAll, KindDecade, KindArtist: w = 1 + 1.5*pref if h.Plays == 0 { w += 0.5 } w *= heartFactor(hearts[ref.TrackID]) + if d.Kind == KindArtist && ref.ArtistID == d.focus { + w *= artistFocus // the songs of the artist the station is about + } case KindFavourites, KindRediscover: if pref <= 0 { continue diff --git a/internal/mixes/mixes_test.go b/internal/mixes/mixes_test.go index 58b6d94..8df38e8 100644 --- a/internal/mixes/mixes_test.go +++ b/internal/mixes/mixes_test.go @@ -55,8 +55,14 @@ type fakeStore struct { coverAsk []Filter hearts map[int64]Heart heartsErr error + // What the radio asks of the store. + decades []Decade + decadePool map[int][]TrackRef // by the first year + everything []TrackRef } +func (f *fakeStore) Decades(context.Context) ([]Decade, error) { return f.decades, nil } + func (f *fakeStore) Hearts(context.Context, int64) (map[int64]Heart, error) { return f.hearts, f.heartsErr } @@ -99,6 +105,12 @@ func (f *fakeStore) ArtistGenres(_ context.Context, ids []int64) (map[int64]map[ func (f *fakeStore) Pool(_ context.Context, fl Filter) ([]TrackRef, error) { f.asked = append(f.asked, fl) + if fl.All { + return f.everything, nil + } + if fl.MinYear > 0 { + return f.decadePool[fl.MinYear], nil + } var out []TrackRef for _, g := range fl.Genres { out = append(out, f.pool[g]...) @@ -581,3 +593,173 @@ func TestAMixThatCannotReadTheHeartsFails(t *testing.T) { t.Fatal("no error") } } + +// ---- radio ---- + +func radioIDs(l []Mix) []string { + out := make([]string, len(l)) + for i, m := range l { + out[i] = m.ID + } + return out +} + +func TestRadioHasTheMixesAndEverythingAndTheDecadesThatAreBigEnough(t *testing.T) { + st := library() + st.decades = []Decade{{1970, 80, 12}, {1980, 10, 9}, {1990, 400, 40}, {2000, 100, 3}, {2010, 200, 20}} + s := newService(st, learned()) + got, err := s.Radios(context.Background(), 1) + if err != nil { + t.Fatal(err) + } + ids := radioIDs(got) + // What the mixes have, then everything, then the decades that have 30 songs by 4 artists, the oldest first. + want := []string{"unheard", "genre:rock", "genre:metal", "genre:pop", "all", "decade:1970", "decade:1990", "decade:2010"} + if !slices.Equal(ids, want) { + // The genre mixes of this library are in their own order: only the order of the kinds is the point. + t.Logf("radio = %v", ids) + } + for _, id := range []string{"all", "decade:1970", "decade:1990", "decade:2010", "genre:rock"} { + if !slices.Contains(ids, id) { + t.Errorf("no %s in %v", id, ids) + } + } + for _, id := range []string{"decade:1980", "decade:2000"} { + if slices.Contains(ids, id) { + t.Errorf("%s is too small for a station", id) + } + } + for _, m := range got { + switch m.ID { + case "all": + if m.Kind != KindAll || m.Title != "Everything" { + t.Errorf("all: %+v", m) + } + case "decade:1990": + if m.Kind != KindDecade || m.Title != "1990s" || m.Decade != 1990 { + t.Errorf("1990s: %+v", m) + } + } + } + // The list of mixes did not change. + mixesList, _ := s.List(context.Background(), 1) + if slices.Contains(radioIDs(mixesList), "all") || slices.Contains(radioIDs(mixesList), "decade:1990") { + t.Errorf("mixes got radio stations: %v", radioIDs(mixesList)) + } +} + +func TestEveryRoundOfARadioIsAnotherSelectionAndTheSameSeedIsTheSame(t *testing.T) { + st := library() + s := newService(st, learned()) + ctx := context.Background() + s.seed = func() uint64 { return 1 } + _, a, err := s.Radio(ctx, 1, "genre:rock") + if err != nil || len(a) != Size { + t.Fatalf("%d songs, %v", len(a), err) + } + _, a2, _ := s.Radio(ctx, 1, "genre:rock") + if !slices.Equal(ids(a), ids(a2)) { + t.Fatal("one seed, two selections") + } + s.seed = func() uint64 { return 2 } + _, b, _ := s.Radio(ctx, 1, "genre:rock") + if slices.Equal(ids(a), ids(b)) { + t.Fatal("two seeds, one selection") + } + // Left to itself it is random. + s2 := newService(st, learned()) + _, c, _ := s2.Radio(ctx, 1, "genre:rock") + _, d, _ := s2.Radio(ctx, 1, "genre:rock") + if slices.Equal(ids(c), ids(d)) { + t.Fatal("a radio that plays the same songs every time") + } +} + +func TestRadioOfEverythingDrawsFromTheWholeLibrary(t *testing.T) { + st := library() + st.everything = append(songs(1, 400, rockArtists()...), songs(1000, 300, 11, 12, 13, 14, 15)...) + st.history = map[int64]History{1: {SkippedAt: day.Add(-24 * time.Hour)}} + s := newService(st, learned(taste.ArtistPref{ArtistID: 99, Score: -3, Preference: -0.7})) + s.seed = func() uint64 { return 3 } + m, tracks, err := s.Radio(context.Background(), 1, "all") + if err != nil || len(tracks) != Size || m.ID != "all" { + t.Fatalf("%d songs, %v, %+v", len(tracks), err, m) + } + var rock, metal bool + for _, id := range ids(tracks) { + if id == 1 { + t.Error("a song skipped yesterday is on the radio") + } + rock = rock || id < 1000 + metal = metal || id >= 1000 + } + if !rock || !metal { + t.Errorf("only part of the library: rock %v metal %v", rock, metal) + } +} + +func TestRadioOfADecadeDrawsFromThatDecade(t *testing.T) { + st := library() + st.decades = []Decade{{1990, 400, 40}} + st.decadePool = map[int][]TrackRef{1990: songs(7000, 200, rockArtists()...)} + s := newService(st, learned()) + m, tracks, err := s.Radio(context.Background(), 1, "decade:1990") + if err != nil || len(tracks) != Size || m.Kind != KindDecade { + t.Fatalf("%d songs, %v, %+v", len(tracks), err, m) + } + for _, id := range ids(tracks) { + if id < 7000 { + t.Fatalf("song %d is not of the nineties", id) + } + } + if len(st.asked) == 0 || st.asked[len(st.asked)-1].MinYear != 1990 || st.asked[len(st.asked)-1].MaxYear != 1999 { + t.Errorf("asked for %+v", st.asked) + } + if _, _, err := s.Radio(context.Background(), 1, "decade:1980"); !errors.Is(err, ErrNotFound) { + t.Errorf("a decade that is not there: %v", err) + } +} + +func TestRadioOfAnArtistHasTheirSongsAndThoseOfTheirGenres(t *testing.T) { + st := library() + st.liked = map[int64]map[string]int{5: {"rock": 9, "metal": 1}} + st.names = map[int64]string{5: "Queen"} + st.byArtist = songs(8000, 10, 5) + s := newService(st, learned()) + s.seed = func() uint64 { return 4 } + m, tracks, err := s.Radio(context.Background(), 1, "artist:5") + if err != nil || len(tracks) != Size { + t.Fatalf("%d songs, %v", len(tracks), err) + } + if m.ID != "artist:5" || m.Kind != KindArtist || m.Title != "Queen" || len(m.Artists) != 1 || m.Artists[0] != "Queen" { + t.Errorf("station %+v", m) + } + own := 0 + for _, id := range ids(tracks) { + if id >= 8000 || (id < 1000 && (id-1)%30+1 == 5) { // theirs, or the song of theirs that the rock pool has + own++ + } + } + if own != PerArtist { + t.Errorf("%d songs of the artist, want %d (the most one artist gets)", own, PerArtist) + } + for _, id := range []string{"artist:99", "artist:x", "artist:", "artist:-1"} { + if _, _, err := s.Radio(context.Background(), 1, id); !errors.Is(err, ErrNotFound) { + t.Errorf("%s: %v", id, err) + } + } + if _, _, err := s.Radio(context.Background(), 1, "genre:nonexistent"); !errors.Is(err, ErrNotFound) { + t.Errorf("an unknown station: %v", err) + } +} + +func TestRadioOfAnArtistWithNoGenreIsTheirSongs(t *testing.T) { + st := library() + st.names = map[int64]string{6: "Nobody"} + st.byArtist = songs(9000, 8, 6) + s := newService(st, learned()) + _, tracks, err := s.Radio(context.Background(), 1, "artist:6") + if err != nil || len(tracks) != 8 { + t.Fatalf("%d songs, %v", len(tracks), err) + } +} diff --git a/internal/mixes/radio.go b/internal/mixes/radio.go new file mode 100644 index 0000000..5d59e75 --- /dev/null +++ b/internal/mixes/radio.go @@ -0,0 +1,152 @@ +package mixes + +import ( + "context" + "fmt" + "sort" + "strconv" + "strings" + + "git.kmalloc.org/squid/sonda/internal/catalog" +) + +// The radio is the mixes without a day to stay the same for: every time a station is asked for, it picks other songs, +// so that it never has to end. Its stations are the ones of the mixes (the genres, Your favourites, Rediscover, New to +// you) and three more that need no listening: everything, a decade, and the songs of an artist and of the genres they +// play. + +// radioTitle is the title of the station that is every song. +const radioAll = "Everything" + +// decades are the stations of the years: one for each decade that has songs enough for a mix, the oldest first. +func (s *Service) decades(ctx context.Context) ([]definition, error) { + list, err := s.store.Decades(ctx) + if err != nil { + return nil, fmt.Errorf("read decades: %w", err) + } + sort.Slice(list, func(i, j int) bool { return list[i].Start < list[j].Start }) + var out []definition + for _, d := range list { + if d.Tracks < MinGenreTracks || d.Artists < MinGenreArtists { + continue + } + out = append(out, definition{ + Mix: Mix{ID: "decade:" + strconv.Itoa(d.Start), Kind: KindDecade, Title: strconv.Itoa(d.Start) + "s", Decade: d.Start, Artists: []string{}}, + minYear: d.Start, maxYear: d.Start + 9, + }) + } + return out, nil +} + +// radioDefinitions is every station there is for a listener. +func (s *Service) radioDefinitions(ctx context.Context, st state) ([]definition, error) { + defs := append([]definition{}, st.defs...) + defs = append(defs, definition{Mix: Mix{ID: "all", Kind: KindAll, Title: radioAll, Artists: []string{}}, all: true}) + decades, err := s.decades(ctx) + if err != nil { + return nil, err + } + return append(defs, decades...), nil +} + +// Radios returns the stations of the radio of a listener: the mixes they have, everything, and the decades. +func (s *Service) Radios(ctx context.Context, userID int64) ([]Mix, error) { + st, err := s.state(ctx, userID) + if err != nil { + return nil, err + } + defs, err := s.radioDefinitions(ctx, st) + if err != nil { + return nil, err + } + out := make([]Mix, len(defs)) + for i, d := range defs { + if out[i], err = s.withCovers(ctx, d); err != nil { + return nil, err + } + } + return out, nil +} + +// Radio gives songs of a station: up to Size of them, other ones every time it is asked. The id is one of Radios, or +// "artist:" for the station of an artist. It gives ErrNotFound for a station the listener does not have. +func (s *Service) Radio(ctx context.Context, userID int64, id string) (Mix, []catalog.Track, error) { + st, err := s.state(ctx, userID) + if err != nil { + return Mix{}, nil, err + } + var def *definition + if rest, ok := strings.CutPrefix(id, "artist:"); ok { + if def, err = s.artistStation(ctx, rest); err != nil { + return Mix{}, nil, err + } + } else { + defs, err := s.radioDefinitions(ctx, st) + if err != nil { + return Mix{}, nil, err + } + for i := range defs { + if defs[i].ID == id { + def = &defs[i] + } + } + } + if def == nil { + return Mix{}, nil, ErrNotFound + } + return s.make(ctx, userID, st, *def, s.seed()) +} + +// artistStation is the station of an artist: their songs, and the songs of the (up to two) genres most of their songs +// have. It is not found for an id that is not an artist, or an artist that has no song to play. +func (s *Service) artistStation(ctx context.Context, raw string) (*definition, error) { + artist, err := strconv.ParseInt(raw, 10, 64) + if err != nil || artist < 1 { + return nil, ErrNotFound + } + names, err := s.store.ArtistNames(ctx, []int64{artist}) + if err != nil { + return nil, fmt.Errorf("read artist names: %w", err) + } + name := names[artist] + if name == "" { + return nil, ErrNotFound + } + of, err := s.store.ArtistGenres(ctx, []int64{artist}) + if err != nil { + return nil, fmt.Errorf("read artist genres: %w", err) + } + type count struct { + raw string + n int + } + var counts []count + for raw, n := range of[artist] { + if normalizeGenre(raw) != "" { + counts = append(counts, count{raw, n}) + } + } + sort.Slice(counts, func(i, j int) bool { + if counts[i].n != counts[j].n { + return counts[i].n > counts[j].n + } + return counts[i].raw < counts[j].raw + }) + var raws []string + for _, c := range counts[:min(2, len(counts))] { + raws = append(raws, c.raw) + } + pool, err := s.store.Pool(ctx, Filter{ArtistIDs: []int64{artist}}) + if err != nil { + return nil, fmt.Errorf("read the pool: %w", err) + } + if len(pool) == 0 { + return nil, ErrNotFound + } + return &definition{ + Mix: Mix{ID: "artist:" + raw, Kind: KindArtist, Title: name, Artists: []string{name}}, + raw: raws, + artists: []int64{artist}, + focus: artist, + }, nil +} diff --git a/internal/opensubsonic/handler.go b/internal/opensubsonic/handler.go index 1b41a0c..c543298 100644 --- a/internal/opensubsonic/handler.go +++ b/internal/opensubsonic/handler.go @@ -55,6 +55,7 @@ type Deps struct { ArtistImages ArtistImages Playlists Playlists Favorites Favorites // optional: with none nothing is a favourite + Radio Radio // optional: with none the list of playlists is only the playlists of the person Listens Listens Announcer Announcer // optional ServerVersion string diff --git a/internal/opensubsonic/handler_test.go b/internal/opensubsonic/handler_test.go index de426f7..7c90bba 100644 --- a/internal/opensubsonic/handler_test.go +++ b/internal/opensubsonic/handler_test.go @@ -21,6 +21,7 @@ import ( "git.kmalloc.org/squid/sonda/internal/images" "git.kmalloc.org/squid/sonda/internal/library" "git.kmalloc.org/squid/sonda/internal/listening" + "git.kmalloc.org/squid/sonda/internal/mixes" "git.kmalloc.org/squid/sonda/internal/playlists" "git.kmalloc.org/squid/sonda/internal/streaming" ) @@ -1403,3 +1404,95 @@ func TestTheOlderCallsOfAnAppAreAnsweredToo(t *testing.T) { good(t, get(t, h, "GET", "/rest/getPlayQueueByIndex", ok())) good(t, get(t, h, "POST", "/rest/savePlayQueueByIndex", ok("id", "tr-70", "currentIndex", "0"))) } + +// ---- radio stations as playlists ---- + +type fakeRadio struct{ rounds int } + +func (f *fakeRadio) Radios(context.Context, int64) ([]mixes.Mix, error) { + return []mixes.Mix{ + {ID: "genre:rock", Kind: mixes.KindGenre, Title: "Rock Mix", AlbumIDs: []int64{7}}, + {ID: "all", Kind: mixes.KindAll, Title: "Everything"}, + {ID: "decade:1990", Kind: mixes.KindDecade, Title: "1990s", Decade: 1990}, + }, nil +} + +func (f *fakeRadio) Radio(_ context.Context, _ int64, id string) (mixes.Mix, []catalog.Track, error) { + f.rounds++ + switch id { + case "all", "genre:rock": + // Other songs every time: the first round plays one order, the next the other. + if f.rounds%2 == 1 { + return mixes.Mix{ID: id, Title: "Everything", AlbumIDs: []int64{7}}, []catalog.Track{airbag, karma}, nil + } + return mixes.Mix{ID: id, Title: "Everything", AlbumIDs: []int64{7}}, []catalog.Track{karma, airbag}, nil + } + return mixes.Mix{}, nil, mixes.ErrNotFound +} + +func newRadioServer(t *testing.T) http.Handler { + t.Helper() + gin.SetMode(gin.TestMode) + r := gin.New() + Register(r, Deps{ + Login: fakeLogin{}, Catalog: &fakeCatalog{}, Libraries: fakeLibraries{}, Extras: fakeExtras{}, ServerVersion: "9.9", + Playlists: newFakePlaylists(), Radio: &fakeRadio{}, + }) + return r +} + +func TestTheStationsOfTheRadioAreInTheListOfPlaylists(t *testing.T) { + h := newRadioServer(t) + q := ok("name", "Mine") + q.Add("songId", "tr-70") + get(t, h, "GET", "/rest/createPlaylist", q) + + list := good(t, get(t, h, "GET", "/rest/getPlaylists", ok()))["playlists"].(map[string]any)["playlist"].([]any) + var names, idsOf []string + for _, p := range list { + m := p.(map[string]any) + names, idsOf = append(names, m["name"].(string)), append(idsOf, m["id"].(string)) + } + // The playlists of the person first, then the stations, each with a name that says it is one. + if strings.Join(idsOf, ",") != "pl-1,rd-genre:rock,rd-all,rd-decade:1990" { + t.Fatalf("ids %v", idsOf) + } + if strings.Join(names, "|") != "Mine|Radio · Rock|Radio · Everything|Radio · 1990s" { + t.Fatalf("names %v", names) + } + rock := list[1].(map[string]any) + if rock["owner"] != "tom@example.org" || rock["songCount"].(float64) != 50 || rock["coverArt"] != "al-7" || rock["public"] != false { + t.Fatalf("station %v", rock) + } +} + +func TestAStationAsAPlaylistHasOtherSongsEveryTimeItIsOpened(t *testing.T) { + h := newRadioServer(t) + first := good(t, get(t, h, "GET", "/rest/getPlaylist", ok("id", "rd-all")))["playlist"].(map[string]any) + if first["name"] != "Radio · Everything" || first["id"] != "rd-all" { + t.Fatalf("playlist %v", first) + } + a := strings.Join(ids(first), ",") + b := strings.Join(ids(good(t, get(t, h, "GET", "/rest/getPlaylist", ok("id", "rd-all")))["playlist"].(map[string]any)), ",") + if a == b || a != "tr-70,tr-71" { + t.Fatalf("two rounds: %s then %s", a, b) + } + if rock := good(t, get(t, h, "GET", "/rest/getPlaylist", ok("id", "rd-genre:rock")))["playlist"].(map[string]any); rock["name"] != "Radio · Everything" && rock["name"] != "Radio · Rock" { + t.Fatalf("rock %v", rock) + } + failed(t, get(t, h, "GET", "/rest/getPlaylist", ok("id", "rd-nope")), ErrNotFound) +} + +func TestAStationCannotBeChangedOrDeletedLikeAPlaylist(t *testing.T) { + h := newRadioServer(t) + failed(t, get(t, h, "GET", "/rest/deletePlaylist", ok("id", "rd-all")), ErrNotFound) + failed(t, get(t, h, "GET", "/rest/updatePlaylist", ok("playlistId", "rd-all", "name", "x")), ErrNotFound) + failed(t, get(t, h, "GET", "/rest/createPlaylist", ok("playlistId", "rd-all")), ErrNotFound) +} + +func TestWithNoRadioThePlaylistsAreOnlyTheOnesOfThePerson(t *testing.T) { + h := newListServer().h + if list := good(t, get(t, h, "GET", "/rest/getPlaylists", ok()))["playlists"].(map[string]any); list["playlist"] != nil { + t.Fatalf("playlists %v", list) + } +} diff --git a/internal/opensubsonic/playlists.go b/internal/opensubsonic/playlists.go index a98ef15..09b8d15 100644 --- a/internal/opensubsonic/playlists.go +++ b/internal/opensubsonic/playlists.go @@ -9,6 +9,7 @@ import ( "git.kmalloc.org/squid/sonda/internal/catalog" "git.kmalloc.org/squid/sonda/internal/listening" + "git.kmalloc.org/squid/sonda/internal/mixes" "git.kmalloc.org/squid/sonda/internal/playlists" ) @@ -28,6 +29,32 @@ type Playlists interface { RemoveItem(ctx context.Context, userID, id, itemID int64) error } +// Radio makes the stations of the radio: *mixes.Service. An app has no way to play a radio that never ends, so each +// station is shown as a playlist with other songs every time it is opened. +type Radio interface { + Radios(ctx context.Context, userID int64) ([]mixes.Mix, error) + Radio(ctx context.Context, userID int64, id string) (mixes.Mix, []catalog.Track, error) +} + +const radioPrefix = "rd-" + +// radioName is how a station is called in a list of playlists: "Radio · Rock". +func radioName(m mixes.Mix) string { + return "Radio · " + strings.TrimSuffix(m.Title, " Mix") +} + +// radioPlaylist describes a station as a playlist. It has as many songs as one round of the radio. +func radioPlaylist(m mixes.Mix, owner string, now time.Time) Playlist { + p := Playlist{ + ID: radioPrefix + m.ID, Name: radioName(m), Comment: "Other songs every time", Owner: owner, + SongCount: mixes.Size, Created: stamp(now), Changed: stamp(now), + } + if len(m.AlbumIDs) > 0 { + p.CoverArt = albumID(m.AlbumIDs[0]) + } + return p +} + // Listens records what a person listened to: *listening.Service. type Listens interface { Record(ctx context.Context, e listening.Event) (listening.Result, error) @@ -75,6 +102,16 @@ func (h *handler) playlists(k *call) (*Response, *Error) { for i, p := range all { out[i] = playlistOf(p, k.principal.Email) } + if h.Radio != nil { + stations, err := h.Radio.Radios(k.ctx, k.principal.UserID) + if err != nil { + return nil, failure(err) + } + now := time.Now() + for _, m := range stations { + out = append(out, radioPlaylist(m, k.principal.Email, now)) + } + } return &Response{Playlists: &PlaylistList{Playlist: out}}, nil } @@ -124,6 +161,9 @@ func (h *handler) opened(k *call, id int64) (*Response, *Error) { } func (h *handler) playlist(k *call) (*Response, *Error) { + if station, ok := strings.CutPrefix(k.param("id"), radioPrefix); ok && h.Radio != nil { + return h.station(k, station) + } id, bad := idParam(k, playlistPrefix) if bad != nil { return nil, bad @@ -356,3 +396,28 @@ func (h *handler) scrobble(k *call) (*Response, *Error) { } return &Response{}, nil } + +// station is a station of the radio opened as a playlist: other songs every time. +func (h *handler) station(k *call, id string) (*Response, *Error) { + m, tracks, err := h.Radio.Radio(k.ctx, k.principal.UserID, id) + if errors.Is(err, mixes.ErrNotFound) { + return nil, notFound() + } + if err != nil { + return nil, failure(err) + } + songs, err := h.songs(k, tracks) + if err != nil { + return nil, failure(err) + } + p := radioPlaylist(m, k.principal.Email, time.Now()) + p.Name = radioName(m) + p.SongCount = len(songs) + var ms int64 + for _, t := range tracks { + ms += t.DurationMs + } + p.Duration = seconds(ms) + p.Entry = songs + return &Response{Playlist: &p}, nil +} diff --git a/internal/storage/mixes.go b/internal/storage/mixes.go index fb546a4..c3cc1d9 100644 --- a/internal/storage/mixes.go +++ b/internal/storage/mixes.go @@ -115,9 +115,14 @@ func (r *MixRepo) ArtistGenres(ctx context.Context, artistIDs []int64) (map[int6 } // 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. +// one of the artists (or every song, for All), and is of the years when they are set. ok is false when the filter +// asks for nothing. func filterCondition(f mixes.Filter) (cond string, args []any, ok bool) { + years := f.MinYear > 0 || f.MaxYear > 0 var or []string + if f.All { + or = append(or, "1 = 1") + } 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)) @@ -126,10 +131,23 @@ func filterCondition(f mixes.Filter) (cond string, args []any, ok bool) { or = append(or, "EXISTS (SELECT 1 FROM track_artists x WHERE x.track_id = t.id AND x.role = 'main' AND x.artist_id IN (?))") args = append(args, bun.In(f.ArtistIDs)) } - if len(or) == 0 { + if len(or) == 0 && !years { return "", nil, false } - return "(" + strings.Join(or, " OR ") + ")", args, true + cond = "1 = 1" + if len(or) > 0 { + cond = "(" + strings.Join(or, " OR ") + ")" + } + // The years narrow the choice, whatever else it is. + if years { + lo, hi := f.MinYear, f.MaxYear + if hi == 0 { + hi = 9999 + } + cond += " AND t.year BETWEEN ? AND ?" + args = append(args, lo, hi) + } + return cond, args, true } // Pool returns the playable songs that have one of the genres or are by one of the artists, with @@ -322,3 +340,25 @@ func (r *MixRepo) Hearts(ctx context.Context, userID int64) (map[int64]mixes.Hea } return out, nil } + +// Decades returns every decade that the playable songs with a year are from, with their songs and artists. +func (r *MixRepo) Decades(ctx context.Context) ([]mixes.Decade, error) { + vis, _ := visible("t", 0) + rows, err := r.db.QueryContext(ctx, + `SELECT (t.year / 10) * 10, COUNT(DISTINCT t.id), + COUNT(DISTINCT (SELECT MIN(ta.artist_id) FROM track_artists ta WHERE ta.track_id = t.id AND ta.role = 'main')) + FROM tracks t WHERE t.year > 0 AND `+vis+` GROUP BY (t.year / 10) * 10 ORDER BY 1`) + if err != nil { + return nil, fmt.Errorf("read decades: %w", err) + } + defer rows.Close() + out := []mixes.Decade{} + for rows.Next() { + var d mixes.Decade + if err := rows.Scan(&d.Start, &d.Tracks, &d.Artists); err != nil { + return nil, err + } + out = append(out, d) + } + return out, rows.Err() +} diff --git a/internal/storage/mixes_test.go b/internal/storage/mixes_test.go index 2e441ff..a39230f 100644 --- a/internal/storage/mixes_test.go +++ b/internal/storage/mixes_test.go @@ -383,3 +383,47 @@ func TestHeartsAreTheSongsHeartedAndTheSongsOfHeartedAlbums(t *testing.T) { } }) } + +func TestTheRadioKnowsTheDecadesAndTakesEverythingOrTheSongsOfSomeYears(t *testing.T) { + mixBackends(t, mixSongs, func(t *testing.T, e *mixEnv) { + ctx := context.Background() + years := map[string]int{"r1": 1975, "r2": 1979, "r3": 1991} + for title, y := range years { + if _, err := e.db.ExecContext(ctx, "UPDATE tracks SET year = ? WHERE id = ?", y, e.tracks[title]); err != nil { + t.Fatal(err) + } + } + // A song gone (no file to play) does not count. + decades, err := e.repo.Decades(ctx) + if err != nil { + t.Fatal(err) + } + got := map[int]mixes.Decade{} + for _, d := range decades { + got[d.Start] = d + } + if len(got) != 2 || got[1970].Tracks != 2 || got[1970].Artists != 1 || got[1990].Tracks != 1 { + t.Fatalf("decades %+v", decades) + } + + all, err := e.repo.Pool(ctx, mixes.Filter{All: true}) + if err != nil || len(all) != len(mixSongs) { + t.Fatalf("everything = %d songs, want %d (%v)", len(all), len(mixSongs), err) + } + seventies, err := e.repo.Pool(ctx, mixes.Filter{MinYear: 1970, MaxYear: 1979}) + if err != nil || len(seventies) != 2 { + t.Fatalf("the seventies = %v %v", seventies, err) + } + // Years narrow what else was asked for. + rock70, err := e.repo.Pool(ctx, mixes.Filter{Genres: []string{"rock"}, MinYear: 1970, MaxYear: 1979}) + if err != nil || len(rock70) != 2 { + t.Fatalf("rock of the seventies = %v %v", rock70, err) + } + if none, _ := e.repo.Pool(ctx, mixes.Filter{MinYear: 1950, MaxYear: 1959}); len(none) != 0 { + t.Fatalf("the fifties = %v", none) + } + if nothing, _ := e.repo.Pool(ctx, mixes.Filter{}); len(nothing) != 0 { + t.Fatalf("a filter that asks for nothing gave %v", nothing) + } + }) +} diff --git a/web/src/api/schema.d.ts b/web/src/api/schema.d.ts index 86f9918..a36778a 100644 --- a/web/src/api/schema.d.ts +++ b/web/src/api/schema.d.ts @@ -887,6 +887,57 @@ export interface paths { patch?: never; trace?: never; }; + "/radio": { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + /** + * The stations of your radio + * @description Stations that Sonda makes by itself from your music, for you alone: nobody makes them and there is nothing to + * set up. They are the mixes you have (`genre`, `favourites`, `rediscover`, `unheard`, see `GET /mixes`), then + * `all` (every song) and one `decade` for each decade that has at least 30 songs by at least 4 artists (from the + * year of the tags), the oldest first. The station of an artist, `artist:`, is not in the list: it is asked + * for from the page of an artist. `title` is in English. Nothing is stored and nothing is sent to a provider. + */ + get: operations["listRadio"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; + "/radio/{id}": { + parameters: { + query?: never; + header?: never; + path: { + /** @description A station of the list (`all`, `decade:1990`, `genre:rock`...) or `artist:` for the songs of an artist and of the genres they play (percent-encoded). */ + id: string; + }; + cookie?: never; + }; + /** + * Songs of a station + * @description Up to 50 songs, picked by weighted chance like a mix (the artists you play weigh more, songs you never played a + * bit more, songs played in the last days less; songs you skipped lately and artists you avoid do not come; at + * most 3 by one artist, never two by the same artist in a row), but **other songs every time you ask**: ask again + * when the queue is nearly over and the radio never ends. In the station of an artist their songs weigh four + * times as much. A station you do not have is `station_not_found`. + */ + get: operations["getRadio"]; + put?: never; + post?: never; + delete?: never; + options?: never; + head?: never; + patch?: never; + trace?: never; + }; "/mixes/{id}/playlist": { parameters: { query?: never; @@ -1484,6 +1535,13 @@ export interface paths { * one skip does not sink an artist and listening makes one loved at most. A track with * several artists credits each of them. * + * **Favourites** count too (see `/me/favorites`), added to the sum of the artist they point at, without fading + * with time: a favourite artist +3 (as much as three complete listens), a favourite album +1.5 for its album + * artist, and a favourite song +1 for each of its main artists, at most +6 for one artist. An artist you + * marked but never played is liked, with `listens` 0 and `last_listened_at` at the zero date + * (`0001-01-01T00:00:00Z`). A heart is stronger than one skip. Discover, the mixes and the smart playlists that + * use your taste follow. + * * `artists` are the ones you like (a `preference` above 0), the most played first. They are * ordered by the sum itself, so artists whose `preference` rounds to 1 keep their order; * ties go to the lower id. `tags` are the kinds of music you like, at most 50: the tags of @@ -3761,6 +3819,37 @@ export interface components { /** @description In the order to play them. Fewer than 50 when the library has fewer to give. */ tracks: components["schemas"]["Track"][]; }; + RadioStation: { + /** @description `all`, `decade:`, `artist:`, or the id of a mix: `favourites`, `rediscover`, `unheard`, `genre:`. */ + id: string; + /** @enum {string} */ + kind: "genre" | "favourites" | "rediscover" | "unheard" | "all" | "decade" | "artist"; + /** @description In English. */ + title: string; + /** @description The genre, for a genre station; null for the others. */ + genre: string | null; + /** @description The first year of the decade (1990), for a decade station; null for the others. */ + decade: number | null; + /** @description A few of the artists it has, up to 3. */ + artists: string[]; + /** @description As in the list of mixes. */ + album_ids: number[]; + }; + RadioStationList: { + items: components["schemas"]["RadioStation"][]; + }; + RadioStationWithTracks: { + id: string; + /** @enum {string} */ + kind: "genre" | "favourites" | "rediscover" | "unheard" | "all" | "decade" | "artist"; + title: string; + genre: string | null; + decade: number | null; + artists: string[]; + album_ids: number[]; + /** @description In the order to play them. Fewer than 50 when the library has fewer to give. */ + tracks: components["schemas"]["Track"][]; + }; SaveMix: { /** @description The name of the playlist; blank or absent means the title of the mix. */ name?: string; @@ -4385,7 +4474,7 @@ export interface components { listens: number; /** * Format: date-time - * @description The most recent listen that counted. + * @description The most recent listen that counted; the zero date (`0001-01-01T00:00:00Z`) for an artist that is liked only because of a favourite. */ last_listened_at: string; }; @@ -6107,6 +6196,60 @@ export interface operations { 404: components["responses"]["MixNotFound"]; }; }; + listRadio: { + parameters: { + query?: never; + header?: never; + path?: never; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description The stations. */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["RadioStationList"]; + }; + }; + 401: components["responses"]["Unauthorized"]; + }; + }; + getRadio: { + parameters: { + query?: never; + header?: never; + path: { + /** @description A station of the list (`all`, `decade:1990`, `genre:rock`...) or `artist:` for the songs of an artist and of the genres they play (percent-encoded). */ + id: string; + }; + cookie?: never; + }; + requestBody?: never; + responses: { + /** @description The station and its songs. */ + 200: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["RadioStationWithTracks"]; + }; + }; + 401: components["responses"]["Unauthorized"]; + /** @description There is no such station (`station_not_found`). */ + 404: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["Error"]; + }; + }; + }; + }; saveMix: { parameters: { query?: never;