feat(smart): smart playlists, playlists made of rules
A smart playlist is a set of rules, an order and a limit, kept under a name and worked out again every time it is opened.
- Rules (all that are set must hold): genres, artists and albums (is, or is not), file formats and a minimum bitrate of the same file, added in the last N days, never played, played or not played in the last N days, played at least N times, skipped or not skipped lately, only liked artists or without the avoided ones, and the length of the song.
- Orders: random (the same all day, a variant gives another), most played, least played, recently added, by title. A limit of 50 by default, 500 at most.
- GET/POST /smart-playlists, GET/PATCH/DELETE /smart-playlists/{id}, GET /smart-playlists/{id}/tracks, POST /smart-playlists/preview to try rules without keeping them, and POST /smart-playlists/{id}/playlist to keep what is shown as a normal playlist.
- Only the rules are stored (migration 0020); the songs come from the files and from the owner's own history and taste. Nothing is sent to a provider, and a smart playlist is its owner's alone. A user can have 200.
- Mixes and smart playlists share the way a list of songs becomes a playlist.
This commit is contained in:
parent
25f41dd5fc
commit
104e41d10a
18 changed files with 2620 additions and 18 deletions
|
|
@ -22,6 +22,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr
|
|||
- **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 `<img>` 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.
|
||||
- **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. 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.
|
||||
- **Your own settings.** `GET` and `PATCH /me/settings` hold choices that belong to you, not to the server. Everyone starts private: `discover_from_listening` is off, so this setting never sends your listening to a provider. When you turn it on, Discover may start from the artists you listen to most, and their names are sent to Last.fm (only if an admin switched it on). What Last.fm answers is kept in the shared database like any other lookup, so another local user can see, from `retrieved_at` of `GET /artists/{id}/similar`, that someone looked up an artist, but not who.
|
||||
|
|
@ -294,6 +295,8 @@ Everything lives under `/api/v1`. Bodies are JSON. Errors always look like `{"er
|
|||
| signed in | `GET /discover` | Artists you may like, with a score and its parts. Query: `seed` (an artist id, up to 5 times), `limit` (default 20, max 30), `in_library=true\|false`. |
|
||||
| signed in | `GET`, `HEAD /albums/{id}/cover` | The cover of an album, a JPEG made from your own files. Query: `size=small\|large`. Also accepts `?access_token=`. |
|
||||
| signed in | `GET /mixes`, `GET /mixes/{id}`, `POST /mixes/{id}/playlist` | Your mixes: list them, read one (query `variant`), keep one as a playlist. Made for you alone, from your history and your genres; nothing is stored. |
|
||||
| signed in | `GET /smart-playlists`, `POST /smart-playlists`, `GET`, `PATCH`, `DELETE /smart-playlists/{id}` | Your smart playlists: list them (query `limit`, `offset`), make one from rules, read, change or delete one. Never anybody else's. |
|
||||
| signed in | `GET /smart-playlists/{id}/tracks`, `POST /smart-playlists/preview`, `POST /smart-playlists/{id}/playlist` | The songs the rules give now (query `variant`), a preview of rules that are not kept, and a smart playlist kept as a normal playlist. |
|
||||
| signed in | `GET /playlists`, `POST /playlists`, `GET`, `PATCH`, `DELETE /playlists/{id}` | Your playlists: list them (query `limit`, `offset`), make one, read, rename or delete one. Never anybody else's. |
|
||||
| signed in | `GET /playlists/{id}/tracks`, `POST /playlists/{id}/tracks`, `PATCH`, `DELETE /playlists/{id}/tracks/{item}` | The tracks of a playlist: list them in order, add some (at the end or at a `position`), move or remove an item. |
|
||||
| signed in | `GET /wishlist`, `PUT /wishlist/{id}`, `DELETE /wishlist/{id}` | Your wishlist: list it (query `limit`, `offset`), add an artist, take one off. |
|
||||
|
|
@ -381,6 +384,7 @@ internal/discover/ the score of an artist you may like, and the suggestions
|
|||
internal/feedback/ the wishlist and the artists a user is not interested in
|
||||
internal/playlists/ the playlists of each user
|
||||
internal/mixes/ the mixes made for a listener, by genre and by listening
|
||||
internal/smart/ smart playlists: rules that are worked out into songs
|
||||
internal/images/ covers made from the pictures in the user's own files
|
||||
internal/usersettings/ the settings that belong to one user
|
||||
internal/storage/ SQLite and PostgreSQL through bun, the repositories
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/providers/musicbrainz"
|
||||
"codeberg.org/squidd/sonda/internal/providers/outbound"
|
||||
"codeberg.org/squidd/sonda/internal/providers/wikipedia"
|
||||
"codeberg.org/squidd/sonda/internal/smart"
|
||||
"codeberg.org/squidd/sonda/internal/storage"
|
||||
"codeberg.org/squidd/sonda/internal/streaming"
|
||||
"codeberg.org/squidd/sonda/internal/taste"
|
||||
|
|
@ -294,6 +295,7 @@ func run() error {
|
|||
Feedback: feedbacks,
|
||||
Playlists: playlists.NewService(storage.NewPlaylistRepo(db)),
|
||||
Mixes: mixes.NewService(storage.NewMixRepo(db), tasteService, cfg.Listening.MinListen()),
|
||||
Smart: smart.NewService(storage.NewSmartRepo(db), tasteService, cfg.Listening.MinListen()),
|
||||
Images: pictures,
|
||||
CoversJob: jobs.Runner{Ctx: ctx, Job: images.NewCoversJob(pictures, imageRepo)},
|
||||
ArtistImagesJob: jobs.Runner{Ctx: ctx, Job: images.NewArtistImagesJob(pictures, imageRepo)},
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/mixes"
|
||||
"codeberg.org/squidd/sonda/internal/playlists"
|
||||
"codeberg.org/squidd/sonda/internal/providers"
|
||||
"codeberg.org/squidd/sonda/internal/smart"
|
||||
"codeberg.org/squidd/sonda/internal/storage"
|
||||
"codeberg.org/squidd/sonda/internal/streaming"
|
||||
"codeberg.org/squidd/sonda/internal/taste"
|
||||
|
|
@ -108,6 +109,7 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun
|
|||
deps.Feedback = feedbackSvc
|
||||
deps.Playlists = playlists.NewService(storage.NewPlaylistRepo(db))
|
||||
deps.Mixes = mixes.NewService(storage.NewMixRepo(db), deps.Taste.(mixes.Taster), 20*time.Second)
|
||||
deps.Smart = smart.NewService(storage.NewSmartRepo(db), deps.Taste.(smart.Taster), 20*time.Second)
|
||||
imageRepo := storage.NewImageRepo(db)
|
||||
imageSvc := images.NewService(filepath.Join(t.TempDir(), "data"), imageRepo, imageRepo)
|
||||
deps.Images, deps.ArtistImages = imageSvc, imageSvc
|
||||
|
|
|
|||
|
|
@ -824,6 +824,7 @@ func TestResponsesMatchTheSpec(t *testing.T) {
|
|||
|
||||
// ---- mixes ----
|
||||
c.mixes(t, token, libID)
|
||||
c.smartPlaylists(t, token)
|
||||
|
||||
// ---- every operation in the spec was exercised ----
|
||||
var missing []string
|
||||
|
|
@ -1026,3 +1027,41 @@ func (c *contract) mixes(t *testing.T, token, libID string) {
|
|||
c.expect(c.call("POST", "/mixes/genre%3Apolka/playlist", token, nil), 404, "save an unknown mix", "mix_not_found")
|
||||
c.expect(c.call("POST", "/mixes/genre%3Asynthwave/playlist", token, map[string]any{"variant": -1}), 400, "save with a negative variant", "invalid_input")
|
||||
}
|
||||
|
||||
// smartPlaylists makes, reads, previews, changes, keeps and deletes smart playlists, over the synthwave
|
||||
// songs that c.mixes added.
|
||||
func (c *contract) smartPlaylists(t *testing.T, token string) {
|
||||
t.Helper()
|
||||
rules := map[string]any{"genres": []string{"synthwave"}, "formats": []string{"flac"}, "never_played": true}
|
||||
c.expect(c.call("GET", "/smart-playlists", "", nil), 401, "smart playlists without a token", "unauthorized")
|
||||
c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "", "rules": rules}), 400, "a smart playlist with no name", "invalid_input")
|
||||
c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "x", "sort": "loudest"}), 400, "an unknown order", "invalid_input")
|
||||
made := c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "Fresh synth", "description": "new", "rules": rules, "sort": "title", "limit": 12}), 201, "make a smart playlist")
|
||||
id := strconv.Itoa(int(made["id"].(float64)))
|
||||
list := c.expect(c.call("GET", "/smart-playlists?limit=5&offset=0", token, nil), 200, "list smart playlists")
|
||||
if list["total"] != float64(1) {
|
||||
t.Fatalf("smart playlists = %v", list)
|
||||
}
|
||||
c.expect(c.call("GET", "/smart-playlists/"+id, token, nil), 200, "get a smart playlist")
|
||||
c.expect(c.call("GET", "/smart-playlists/999999", token, nil), 404, "an unknown smart playlist", "smart_playlist_not_found")
|
||||
c.expect(c.call("GET", "/smart-playlists/0", token, nil), 400, "a bad smart playlist id", "invalid_input")
|
||||
got := c.expect(c.call("GET", "/smart-playlists/"+id+"/tracks", token, nil), 200, "the songs of a smart playlist")
|
||||
if len(got["tracks"].([]any)) != 12 {
|
||||
t.Fatalf("smart playlist songs = %v", got)
|
||||
}
|
||||
c.expect(c.call("GET", "/smart-playlists/"+id+"/tracks?variant=2", token, nil), 200, "the songs, another variant")
|
||||
c.expect(c.call("GET", "/smart-playlists/"+id+"/tracks?variant=-1", token, nil), 400, "a negative variant", "invalid_input")
|
||||
c.expect(c.call("GET", "/smart-playlists/999999/tracks", token, nil), 404, "the songs of an unknown smart playlist", "smart_playlist_not_found")
|
||||
c.expect(c.call("POST", "/smart-playlists/preview", token, map[string]any{"rules": map[string]any{"min_duration_sec": 30, "liked_artists_only": false}, "sort": "most_played", "limit": 5, "variant": 1}), 200, "preview rules")
|
||||
c.expect(c.call("POST", "/smart-playlists/preview", token, map[string]any{"rules": map[string]any{"min_plays": -1}}), 400, "preview bad rules", "invalid_input")
|
||||
c.expect(c.call("PATCH", "/smart-playlists/"+id, token, map[string]any{"name": "Fresher", "rules": map[string]any{"added_within_days": 3650}}), 200, "change a smart playlist")
|
||||
c.expect(c.call("PATCH", "/smart-playlists/"+id, token, map[string]any{"limit": 0, "name": ""}), 400, "change to a bad name", "invalid_input")
|
||||
c.expect(c.call("PATCH", "/smart-playlists/999999", token, map[string]any{"name": "x"}), 404, "change an unknown smart playlist", "smart_playlist_not_found")
|
||||
c.expect(c.call("POST", "/smart-playlists/"+id+"/playlist", token, map[string]any{"name": "Kept", "variant": 1}), 201, "keep a smart playlist as a playlist")
|
||||
c.expect(c.call("POST", "/smart-playlists/"+id+"/playlist", token, nil), 201, "keep with no body")
|
||||
c.expect(c.call("POST", "/smart-playlists/999999/playlist", token, nil), 404, "keep an unknown smart playlist", "smart_playlist_not_found")
|
||||
empty := c.expect(c.call("POST", "/smart-playlists", token, map[string]any{"name": "Polka", "rules": map[string]any{"genres": []string{"polka"}}}), 201, "make a smart playlist that gives nothing")
|
||||
c.expect(c.call("POST", "/smart-playlists/"+strconv.Itoa(int(empty["id"].(float64)))+"/playlist", token, nil), 422, "keep one that gives no songs", "smart_playlist_empty")
|
||||
c.expect(c.call("DELETE", "/smart-playlists/"+id, token, nil), 204, "delete a smart playlist")
|
||||
c.expect(c.call("DELETE", "/smart-playlists/"+id, token, nil), 404, "delete it again", "smart_playlist_not_found")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -133,20 +133,5 @@ func (h *mixHandlers) save(c *gin.Context) {
|
|||
if strings.TrimSpace(name) == "" {
|
||||
name = m.Title
|
||||
}
|
||||
pl, err := h.playlists.Create(ctx, user, name, "")
|
||||
if err != nil {
|
||||
writePlaylistError(c, err)
|
||||
return
|
||||
}
|
||||
ids := make([]int64, len(tracks))
|
||||
for i, t := range tracks {
|
||||
ids[i] = t.ID
|
||||
}
|
||||
filled, err := h.playlists.AddTracks(ctx, user, pl.ID, ids, nil)
|
||||
if err != nil {
|
||||
_ = h.playlists.Delete(ctx, user, pl.ID)
|
||||
writePlaylistError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, toPlaylistJSON(filled))
|
||||
keepAsPlaylist(c, h.playlists, user, name, tracks)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/catalog"
|
||||
"codeberg.org/squidd/sonda/internal/playlists"
|
||||
)
|
||||
|
||||
|
|
@ -266,3 +267,25 @@ func (h *playlistHandlers) moveItem(c *gin.Context) {
|
|||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// keepAsPlaylist makes a playlist of the user with the songs, in order, and answers with it. A save that
|
||||
// fails half way leaves no playlist behind.
|
||||
func keepAsPlaylist(c *gin.Context, svc playlistService, user int64, name string, tracks []catalog.Track) {
|
||||
ctx := c.Request.Context()
|
||||
pl, err := svc.Create(ctx, user, name, "")
|
||||
if err != nil {
|
||||
writePlaylistError(c, err)
|
||||
return
|
||||
}
|
||||
ids := make([]int64, len(tracks))
|
||||
for i, t := range tracks {
|
||||
ids[i] = t.ID
|
||||
}
|
||||
filled, err := svc.AddTracks(ctx, user, pl.ID, ids, nil)
|
||||
if err != nil {
|
||||
_ = svc.Delete(ctx, user, pl.ID)
|
||||
writePlaylistError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, toPlaylistJSON(filled))
|
||||
}
|
||||
|
|
|
|||
235
internal/api/http/handlers_smart.go
Normal file
235
internal/api/http/handlers_smart.go
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/smart"
|
||||
)
|
||||
|
||||
// smartHandlers serve the signed-in user's smart playlists: playlists that are a set of rules, worked out
|
||||
// every time they are opened. They are theirs alone: one of somebody else answers
|
||||
// smart_playlist_not_found, as one that does not exist.
|
||||
type smartHandlers struct {
|
||||
svc smartService
|
||||
playlists playlistService
|
||||
}
|
||||
|
||||
type smartJSON struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Rules smart.Rules `json:"rules"`
|
||||
Sort string `json:"sort"`
|
||||
Limit int `json:"limit"`
|
||||
CreatedAt *string `json:"created_at"`
|
||||
UpdatedAt *string `json:"updated_at"`
|
||||
}
|
||||
|
||||
type smartPageJSON struct {
|
||||
Items []smartJSON `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Limit int `json:"limit"`
|
||||
Offset int `json:"offset"`
|
||||
}
|
||||
|
||||
type smartTracksJSON struct {
|
||||
Variant int `json:"variant"`
|
||||
Tracks []trackJSON `json:"tracks"`
|
||||
}
|
||||
|
||||
func toSmartJSON(p smart.SmartPlaylist) smartJSON {
|
||||
return smartJSON{
|
||||
ID: p.ID, Name: p.Name, Description: p.Description, Rules: p.Rules, Sort: string(p.Sort), Limit: p.Limit,
|
||||
CreatedAt: rfc3339(p.CreatedAt), UpdatedAt: rfc3339(p.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func writeSmartError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, smart.ErrInvalidInput):
|
||||
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
||||
case errors.Is(err, smart.ErrNotFound):
|
||||
writeError(c, http.StatusNotFound, "smart_playlist_not_found", "no such smart playlist")
|
||||
case errors.Is(err, smart.ErrTooMany):
|
||||
writeError(c, http.StatusConflict, "limit_reached", err.Error())
|
||||
default:
|
||||
_ = c.Error(err)
|
||||
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
||||
}
|
||||
}
|
||||
|
||||
type createSmartRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Rules smart.Rules `json:"rules"`
|
||||
Sort string `json:"sort"` // absent: random
|
||||
Limit int `json:"limit"` // absent: 50
|
||||
}
|
||||
|
||||
func (h *smartHandlers) create(c *gin.Context) {
|
||||
var req createSmartRequest
|
||||
if !bindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
p, err := h.svc.Create(c.Request.Context(), currentUserID(c), smart.Input{
|
||||
Name: req.Name, Description: req.Description, Rules: req.Rules, Sort: smart.Sort(req.Sort), Limit: req.Limit,
|
||||
})
|
||||
if err != nil {
|
||||
writeSmartError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, toSmartJSON(p))
|
||||
}
|
||||
|
||||
func (h *smartHandlers) list(c *gin.Context) {
|
||||
page, ok := pageOf(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
l, err := h.svc.List(c.Request.Context(), currentUserID(c), page.Limit, page.Offset)
|
||||
if err != nil {
|
||||
writeSmartError(c, err)
|
||||
return
|
||||
}
|
||||
out := smartPageJSON{Items: make([]smartJSON, len(l.Items)), Total: l.Total, Limit: l.Limit, Offset: l.Offset}
|
||||
for i, p := range l.Items {
|
||||
out.Items[i] = toSmartJSON(p)
|
||||
}
|
||||
c.JSON(http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (h *smartHandlers) get(c *gin.Context) {
|
||||
id, ok := pathID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
p, err := h.svc.Get(c.Request.Context(), currentUserID(c), id)
|
||||
if err != nil {
|
||||
writeSmartError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toSmartJSON(p))
|
||||
}
|
||||
|
||||
type updateSmartRequest struct {
|
||||
Name *string `json:"name"` // pointers: absent must not mean empty
|
||||
Description *string `json:"description"`
|
||||
Rules *smart.Rules `json:"rules"` // replace the old ones whole
|
||||
Sort *string `json:"sort"`
|
||||
Limit *int `json:"limit"`
|
||||
}
|
||||
|
||||
func (h *smartHandlers) update(c *gin.Context) {
|
||||
id, ok := pathID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req updateSmartRequest
|
||||
if !bindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
patch := smart.Patch{Name: req.Name, Description: req.Description, Rules: req.Rules, Limit: req.Limit}
|
||||
if req.Sort != nil {
|
||||
s := smart.Sort(*req.Sort)
|
||||
patch.Sort = &s
|
||||
}
|
||||
p, err := h.svc.Update(c.Request.Context(), currentUserID(c), id, patch)
|
||||
if err != nil {
|
||||
writeSmartError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toSmartJSON(p))
|
||||
}
|
||||
|
||||
func (h *smartHandlers) remove(c *gin.Context) {
|
||||
id, ok := pathID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.svc.Delete(c.Request.Context(), currentUserID(c), id); err != nil {
|
||||
writeSmartError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// tracks works the songs out now: the same all day, and other ones for another variant.
|
||||
func (h *smartHandlers) tracks(c *gin.Context) {
|
||||
id, ok := pathID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
variant, ok := variantOf(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
_, tracks, err := h.svc.Tracks(c.Request.Context(), currentUserID(c), id, variant)
|
||||
if err != nil {
|
||||
writeSmartError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, smartTracksJSON{Variant: variant, Tracks: toTracksJSON(tracks)})
|
||||
}
|
||||
|
||||
type previewSmartRequest struct {
|
||||
Rules smart.Rules `json:"rules"`
|
||||
Sort string `json:"sort"`
|
||||
Limit int `json:"limit"`
|
||||
Variant int `json:"variant"`
|
||||
}
|
||||
|
||||
// preview works the songs out for rules that are not kept, so they can be tried before saving.
|
||||
func (h *smartHandlers) preview(c *gin.Context) {
|
||||
var req previewSmartRequest
|
||||
if !bindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
tracks, err := h.svc.Preview(c.Request.Context(), currentUserID(c), smart.Input{
|
||||
Rules: req.Rules, Sort: smart.Sort(req.Sort), Limit: req.Limit,
|
||||
}, req.Variant)
|
||||
if err != nil {
|
||||
writeSmartError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, smartTracksJSON{Variant: req.Variant, Tracks: toTracksJSON(tracks)})
|
||||
}
|
||||
|
||||
type saveSmartRequest struct {
|
||||
Name string `json:"name"` // blank: the name of the smart playlist
|
||||
Variant *int `json:"variant"` // absent: the songs of the day
|
||||
}
|
||||
|
||||
// save makes a normal playlist of the songs the smart playlist gives now (the ones that variant shows).
|
||||
func (h *smartHandlers) save(c *gin.Context) {
|
||||
id, ok := pathID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req saveSmartRequest
|
||||
if c.Request.ContentLength != 0 && !bindJSON(c, &req) {
|
||||
return
|
||||
}
|
||||
variant := 0
|
||||
if req.Variant != nil {
|
||||
variant = *req.Variant
|
||||
}
|
||||
user := currentUserID(c)
|
||||
p, tracks, err := h.svc.Tracks(c.Request.Context(), user, id, variant)
|
||||
if err != nil {
|
||||
writeSmartError(c, err)
|
||||
return
|
||||
}
|
||||
if len(tracks) == 0 {
|
||||
writeError(c, http.StatusUnprocessableEntity, "smart_playlist_empty", "the rules give no songs to save")
|
||||
return
|
||||
}
|
||||
name := req.Name
|
||||
if strings.TrimSpace(name) == "" {
|
||||
name = p.Name
|
||||
}
|
||||
keepAsPlaylist(c, h.playlists, user, name, tracks)
|
||||
}
|
||||
|
|
@ -27,6 +27,7 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/mixes"
|
||||
"codeberg.org/squidd/sonda/internal/playlists"
|
||||
"codeberg.org/squidd/sonda/internal/providers"
|
||||
"codeberg.org/squidd/sonda/internal/smart"
|
||||
"codeberg.org/squidd/sonda/internal/streaming"
|
||||
"codeberg.org/squidd/sonda/internal/taste"
|
||||
"codeberg.org/squidd/sonda/internal/usersettings"
|
||||
|
|
@ -106,6 +107,17 @@ type mixService interface {
|
|||
Get(ctx context.Context, userID int64, id string, variant int) (mixes.Mix, []catalog.Track, error)
|
||||
}
|
||||
|
||||
// smartService is what the handlers need from smart.Service.
|
||||
type smartService interface {
|
||||
Create(ctx context.Context, userID int64, in smart.Input) (smart.SmartPlaylist, error)
|
||||
List(ctx context.Context, userID int64, limit, offset int) (smart.Listing, error)
|
||||
Get(ctx context.Context, userID, id int64) (smart.SmartPlaylist, error)
|
||||
Update(ctx context.Context, userID, id int64, p smart.Patch) (smart.SmartPlaylist, error)
|
||||
Delete(ctx context.Context, userID, id int64) error
|
||||
Tracks(ctx context.Context, userID, id int64, variant int) (smart.SmartPlaylist, []catalog.Track, error)
|
||||
Preview(ctx context.Context, userID int64, in smart.Input, variant int) ([]catalog.Track, error)
|
||||
}
|
||||
|
||||
type feedbackService interface {
|
||||
Add(ctx context.Context, userID, artistID int64, kind feedback.Kind) error
|
||||
Remove(ctx context.Context, userID, artistID int64, kind feedback.Kind) error
|
||||
|
|
@ -180,6 +192,7 @@ type Deps struct {
|
|||
Feedback feedbackService
|
||||
Playlists playlistService
|
||||
Mixes mixService
|
||||
Smart smartService
|
||||
Images coverService
|
||||
CoversJob jobService
|
||||
ArtistImagesJob jobService
|
||||
|
|
@ -263,6 +276,15 @@ func NewRouter(d Deps) *gin.Engine {
|
|||
protected.GET("/mixes", mx.list)
|
||||
protected.GET("/mixes/:id", mx.get)
|
||||
protected.POST("/mixes/:id/playlist", mx.save)
|
||||
sm := &smartHandlers{svc: d.Smart, playlists: d.Playlists}
|
||||
protected.GET("/smart-playlists", sm.list)
|
||||
protected.POST("/smart-playlists", sm.create)
|
||||
protected.POST("/smart-playlists/preview", sm.preview)
|
||||
protected.GET("/smart-playlists/:id", sm.get)
|
||||
protected.PATCH("/smart-playlists/:id", sm.update)
|
||||
protected.DELETE("/smart-playlists/:id", sm.remove)
|
||||
protected.GET("/smart-playlists/:id/tracks", sm.tracks)
|
||||
protected.POST("/smart-playlists/:id/playlist", sm.save)
|
||||
protected.GET("/wishlist", fb.list(feedback.Wishlist))
|
||||
protected.PUT("/wishlist/:id", fb.add(feedback.Wishlist))
|
||||
protected.DELETE("/wishlist/:id", fb.remove(feedback.Wishlist))
|
||||
|
|
|
|||
265
internal/api/http/smart_test.go
Normal file
265
internal/api/http/smart_test.go
Normal file
|
|
@ -0,0 +1,265 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const sp = "/api/v1/smart-playlists"
|
||||
|
||||
// smartMade makes a smart playlist of alice's and returns its id.
|
||||
func smartMade(t *testing.T, m *mixAPI, body map[string]any) int {
|
||||
t.Helper()
|
||||
r := do(t, m.h, "POST", sp, m.alice, body)
|
||||
if r.Code != http.StatusCreated {
|
||||
t.Fatalf("create: %d %v", r.Code, r.Body)
|
||||
}
|
||||
return int(r.Body["id"].(float64))
|
||||
}
|
||||
|
||||
func smartTracks(t *testing.T, m *mixAPI, id int, query string) []float64 {
|
||||
t.Helper()
|
||||
r := do(t, m.h, "GET", fmt.Sprintf("%s/%d/tracks%s", sp, id, query), m.alice, nil)
|
||||
if r.Code != http.StatusOK {
|
||||
t.Fatalf("tracks: %d %v", r.Code, r.Body)
|
||||
}
|
||||
return trackIDs(t, r)
|
||||
}
|
||||
|
||||
func TestSmartPlaylistRoutesNeedASignedInUser(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
for _, rt := range []struct{ method, path string }{
|
||||
{"GET", sp}, {"POST", sp}, {"POST", sp + "/preview"}, {"GET", sp + "/1"}, {"PATCH", sp + "/1"}, {"DELETE", sp + "/1"},
|
||||
{"GET", sp + "/1/tracks"}, {"POST", sp + "/1/playlist"},
|
||||
} {
|
||||
if r := do(t, m.h, rt.method, rt.path, "", nil); r.Code != http.StatusUnauthorized {
|
||||
t.Errorf("%s %s without a token: %d", rt.method, rt.path, r.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestASmartPlaylistIsMadeReadChangedAndDeleted(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
r := do(t, m.h, "POST", sp, m.alice, map[string]any{
|
||||
"name": " Rock, new ", "rules": map[string]any{"genres": []string{"Rock"}, "never_played": true, "formats": []string{"FLAC"}},
|
||||
})
|
||||
if r.Code != http.StatusCreated {
|
||||
t.Fatalf("create: %d %v", r.Code, r.Body)
|
||||
}
|
||||
id := int(r.Body["id"].(float64))
|
||||
rules := r.Body["rules"].(map[string]any)
|
||||
if r.Body["name"] != "Rock, new" || r.Body["sort"] != "random" || r.Body["limit"] != float64(50) || r.Body["description"] != "" ||
|
||||
r.Body["created_at"] == nil || r.Body["updated_at"] == nil ||
|
||||
rules["never_played"] != true || fmt.Sprint(rules["genres"]) != "[rock]" || fmt.Sprint(rules["formats"]) != "[flac]" {
|
||||
t.Errorf("created = %v", r.Body)
|
||||
}
|
||||
|
||||
got := do(t, m.h, "GET", fmt.Sprintf("%s/%d", sp, id), m.alice, nil)
|
||||
if got.Code != http.StatusOK || got.Body["name"] != "Rock, new" {
|
||||
t.Errorf("get: %d %v", got.Code, got.Body)
|
||||
}
|
||||
list := do(t, m.h, "GET", sp, m.alice, nil)
|
||||
if list.Body["total"] != float64(1) || list.Body["limit"] != float64(50) || list.Body["offset"] != float64(0) || len(list.Body["items"].([]any)) != 1 {
|
||||
t.Errorf("list = %v", list.Body)
|
||||
}
|
||||
|
||||
upd := do(t, m.h, "PATCH", fmt.Sprintf("%s/%d", sp, id), m.alice, map[string]any{
|
||||
"name": "Rock, again", "sort": "title", "limit": 20, "rules": map[string]any{"min_plays": 3},
|
||||
})
|
||||
rules = upd.Body["rules"].(map[string]any)
|
||||
if upd.Code != http.StatusOK || upd.Body["name"] != "Rock, again" || upd.Body["sort"] != "title" || upd.Body["limit"] != float64(20) ||
|
||||
rules["min_plays"] != float64(3) || rules["never_played"] != nil || rules["genres"] != nil {
|
||||
t.Errorf("update: %d %v: the rules are replaced whole", upd.Code, upd.Body)
|
||||
}
|
||||
// Only what is sent changes.
|
||||
again := do(t, m.h, "PATCH", fmt.Sprintf("%s/%d", sp, id), m.alice, map[string]any{"description": "d"})
|
||||
if again.Body["name"] != "Rock, again" || again.Body["description"] != "d" || again.Body["sort"] != "title" {
|
||||
t.Errorf("a patch with one field: %v", again.Body)
|
||||
}
|
||||
|
||||
if del := do(t, m.h, "DELETE", fmt.Sprintf("%s/%d", sp, id), m.alice, nil); del.Code != http.StatusNoContent {
|
||||
t.Errorf("delete: %d", del.Code)
|
||||
}
|
||||
if r := do(t, m.h, "GET", fmt.Sprintf("%s/%d", sp, id), m.alice, nil); r.Code != http.StatusNotFound || errCode(r) != "smart_playlist_not_found" {
|
||||
t.Errorf("get after delete: %d %v", r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhatIsNotAcceptedIsSaidSo(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
for name, body := range map[string]map[string]any{
|
||||
"no name": {"rules": map[string]any{}},
|
||||
"an unknown order": {"name": "a", "sort": "loudest"},
|
||||
"a limit too big": {"name": "a", "limit": 501},
|
||||
"days below zero": {"name": "a", "rules": map[string]any{"played_within_days": -1}},
|
||||
"a genre that isn't": {"name": "a", "rules": map[string]any{"genres": []string{"unknown"}}},
|
||||
"a bad format": {"name": "a", "rules": map[string]any{"formats": []string{"fl/ac"}}},
|
||||
} {
|
||||
if r := do(t, m.h, "POST", sp, m.alice, body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
|
||||
t.Errorf("%s: %d %v", name, r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
for _, path := range []string{sp + "/0", sp + "/x", sp + "/1/tracks?variant=-1", sp + "/1/tracks?variant=x", sp + "?limit=-1"} {
|
||||
if r := do(t, m.h, "GET", path, m.alice, nil); r.Code != http.StatusBadRequest {
|
||||
t.Errorf("GET %s: %d %v", path, r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
if r := do(t, m.h, "POST", sp, m.alice, nil); r.Code != http.StatusBadRequest {
|
||||
t.Errorf("no body: %d", r.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheSongsAreWorkedOutFromTheRules(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
id := smartMade(t, m, map[string]any{"name": "Hard", "sort": "title", "limit": 5, "rules": map[string]any{"genres": []string{"hard rock"}}})
|
||||
r := do(t, m.h, "GET", fmt.Sprintf("%s/%d/tracks", sp, id), m.alice, nil)
|
||||
ids := trackIDs(t, r)
|
||||
if r.Code != http.StatusOK || r.Body["variant"] != float64(0) || len(ids) != 5 {
|
||||
t.Fatalf("tracks: %d %v", r.Code, r.Body)
|
||||
}
|
||||
for _, x := range r.Body["tracks"].([]any) {
|
||||
track := x.(map[string]any)
|
||||
if !strings.HasPrefix(track["title"].(string), "hard song") || track["album"] == nil || len(track["artists"].([]any)) != 1 {
|
||||
t.Errorf("a song = %v", track)
|
||||
}
|
||||
}
|
||||
if again := smartTracks(t, m, id, ""); !slices.Equal(ids, again) {
|
||||
t.Error("the same order twice is not the same")
|
||||
}
|
||||
|
||||
// A rule that no song meets gives an empty list, which is no error.
|
||||
none := smartMade(t, m, map[string]any{"name": "Polka", "rules": map[string]any{"genres": []string{"polka"}}})
|
||||
if got := smartTracks(t, m, none, ""); len(got) != 0 {
|
||||
t.Errorf("polka: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestARandomOrderStaysTheSameForADayAndAVariantGivesAnother(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
id := smartMade(t, m, map[string]any{"name": "Any", "limit": 10, "rules": map[string]any{"genres": []string{"rock"}}})
|
||||
first := smartTracks(t, m, id, "")
|
||||
if len(first) != 10 || !slices.Equal(first, smartTracks(t, m, id, "")) {
|
||||
t.Fatalf("the random order of a day changed: %v", first)
|
||||
}
|
||||
if v := smartTracks(t, m, id, "?variant=1"); slices.Equal(first, v) {
|
||||
t.Error("another variant gave the same songs")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListeningRulesUseTheHistoryOfTheOwner(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
var played []int64
|
||||
rows, err := m.db.Query("SELECT id FROM tracks WHERE title LIKE 'hard song%' ORDER BY id LIMIT 4")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
played = append(played, id)
|
||||
}
|
||||
rows.Close()
|
||||
for i, id := range played {
|
||||
if _, err := m.db.Exec(`INSERT INTO listening_events (user_id, track_id, event_type, occurred_at, duration_played_ms, idempotency_key)
|
||||
VALUES (1, ?, 'complete', strftime('%s','now'), 60000, ?)`, id, fmt.Sprintf("k%d", i)); err != nil {
|
||||
t.Skipf("cannot add listening events by hand here: %v", err)
|
||||
}
|
||||
}
|
||||
never := smartMade(t, m, map[string]any{"name": "New hard", "limit": 500, "rules": map[string]any{"genres": []string{"hard rock"}, "never_played": true}})
|
||||
if got := smartTracks(t, m, never, ""); len(got) != 36-4 {
|
||||
t.Errorf("never played: %d songs, want 32", len(got))
|
||||
}
|
||||
heard := smartMade(t, m, map[string]any{"name": "Heard", "limit": 500, "rules": map[string]any{"played_within_days": 7}})
|
||||
if got := smartTracks(t, m, heard, ""); len(got) != 4 {
|
||||
t.Errorf("played this week: %d songs, want 4", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreviewTriesRulesWithoutKeepingAnything(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
r := do(t, m.h, "POST", sp+"/preview", m.alice, map[string]any{"rules": map[string]any{"genres": []string{"hard rock"}}, "sort": "title", "limit": 3})
|
||||
if r.Code != http.StatusOK || r.Body["variant"] != float64(0) || len(trackIDs(t, r)) != 3 {
|
||||
t.Fatalf("preview: %d %v", r.Code, r.Body)
|
||||
}
|
||||
if list := do(t, m.h, "GET", sp, m.alice, nil); list.Body["total"] != float64(0) {
|
||||
t.Errorf("a preview kept something: %v", list.Body)
|
||||
}
|
||||
r = do(t, m.h, "POST", sp+"/preview", m.alice, map[string]any{"rules": map[string]any{"genres": []string{"rock"}}, "limit": 10, "variant": 2})
|
||||
if r.Code != http.StatusOK || r.Body["variant"] != float64(2) {
|
||||
t.Errorf("preview with a variant: %d %v", r.Code, r.Body)
|
||||
}
|
||||
if r := do(t, m.h, "POST", sp+"/preview", m.alice, map[string]any{"sort": "loudest"}); r.Code != http.StatusBadRequest {
|
||||
t.Errorf("preview of a bad order: %d", r.Code)
|
||||
}
|
||||
if r := do(t, m.h, "POST", sp+"/preview", m.alice, map[string]any{"variant": -1}); r.Code != http.StatusBadRequest {
|
||||
t.Errorf("preview of a bad variant: %d", r.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestASmartPlaylistIsKeptAsAPlaylistWithTheSongsShown(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
id := smartMade(t, m, map[string]any{"name": "Hard, kept", "limit": 6, "rules": map[string]any{"genres": []string{"hard rock"}}})
|
||||
shown := smartTracks(t, m, id, "?variant=3")
|
||||
|
||||
r := do(t, m.h, "POST", fmt.Sprintf("%s/%d/playlist", sp, id), m.alice, map[string]any{"variant": 3})
|
||||
if r.Code != http.StatusCreated || r.Body["name"] != "Hard, kept" || r.Body["track_count"] != float64(6) {
|
||||
t.Fatalf("save: %d %v", r.Code, r.Body)
|
||||
}
|
||||
items := do(t, m.h, "GET", fmt.Sprintf("/api/v1/playlists/%d/tracks", int(r.Body["id"].(float64))), m.alice, nil)
|
||||
var kept []float64
|
||||
for _, x := range items.Body["items"].([]any) {
|
||||
kept = append(kept, x.(map[string]any)["track"].(map[string]any)["id"].(float64))
|
||||
}
|
||||
if !slices.Equal(kept, shown) {
|
||||
t.Errorf("the playlist has %v, the smart playlist showed %v", kept, shown)
|
||||
}
|
||||
// It is a copy: the smart playlist is still there, and a name can be given.
|
||||
named := do(t, m.h, "POST", fmt.Sprintf("%s/%d/playlist", sp, id), m.alice, map[string]any{"name": "Mine"})
|
||||
if named.Code != http.StatusCreated || named.Body["name"] != "Mine" {
|
||||
t.Errorf("named save: %d %v", named.Code, named.Body)
|
||||
}
|
||||
if r := do(t, m.h, "POST", fmt.Sprintf("%s/%d/playlist", sp, id), m.alice, nil); r.Code != http.StatusCreated {
|
||||
t.Errorf("a save with no body: %d", r.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestASmartPlaylistWithNoSongsIsNotKept(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
id := smartMade(t, m, map[string]any{"name": "Polka", "rules": map[string]any{"genres": []string{"polka"}}})
|
||||
r := do(t, m.h, "POST", fmt.Sprintf("%s/%d/playlist", sp, id), m.alice, nil)
|
||||
if r.Code != http.StatusUnprocessableEntity || errCode(r) != "smart_playlist_empty" {
|
||||
t.Errorf("save: %d %v", r.Code, r.Body)
|
||||
}
|
||||
if list := do(t, m.h, "GET", "/api/v1/playlists", m.alice, nil); list.Body["total"] != float64(0) {
|
||||
t.Errorf("a playlist was made: %v", list.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestASmartPlaylistIsItsOwnersAlone(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
_, bob := addUser(t, m.h, m.alice, "bob", false)
|
||||
id := smartMade(t, m, map[string]any{"name": "Mine", "rules": map[string]any{}})
|
||||
path := fmt.Sprintf("%s/%d", sp, id)
|
||||
for _, rt := range []struct {
|
||||
method, path string
|
||||
body any
|
||||
}{
|
||||
{"GET", path, nil}, {"PATCH", path, map[string]any{"name": "x"}}, {"DELETE", path, nil},
|
||||
{"GET", path + "/tracks", nil}, {"POST", path + "/playlist", nil},
|
||||
} {
|
||||
if r := do(t, m.h, rt.method, rt.path, bob, rt.body); r.Code != http.StatusNotFound || errCode(r) != "smart_playlist_not_found" {
|
||||
t.Errorf("bob %s %s: %d %v, want 404 smart_playlist_not_found", rt.method, rt.path, r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
if list := do(t, m.h, "GET", sp, bob, nil); list.Body["total"] != float64(0) {
|
||||
t.Errorf("bob's list: %v", list.Body)
|
||||
}
|
||||
if r := do(t, m.h, "GET", path, m.alice, nil); r.Code != http.StatusOK || r.Body["name"] != "Mine" {
|
||||
t.Errorf("alice's own changed: %d %v", r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
|
|
@ -39,6 +39,8 @@ tags:
|
|||
description: Ordered lists of tracks that belong to one user.
|
||||
- name: Mixes
|
||||
description: Playlists made for you, from what you listen to and from the genres of your music.
|
||||
- name: Smart playlists
|
||||
description: Playlists that are a set of rules, worked out again every time they are opened.
|
||||
- name: Discovery
|
||||
description: Music outside the library, from external providers an admin switched on, and suggestions scored for you.
|
||||
- name: Providers
|
||||
|
|
@ -1286,6 +1288,191 @@ paths:
|
|||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
|
||||
/smart-playlists:
|
||||
get:
|
||||
tags: [Smart playlists]
|
||||
operationId: listSmartPlaylists
|
||||
summary: Your smart playlists
|
||||
description: |
|
||||
The smart playlists you made, the one changed last first. Only yours. A smart playlist is a set
|
||||
of rules: its songs come from `GET /smart-playlists/{id}/tracks`.
|
||||
security: [{bearerAuth: []}]
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Limit'}
|
||||
- {$ref: '#/components/parameters/Offset'}
|
||||
responses:
|
||||
'200':
|
||||
description: A page of smart playlists.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/SmartPlaylistPage'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
post:
|
||||
tags: [Smart playlists]
|
||||
operationId: createSmartPlaylist
|
||||
summary: Make a smart playlist
|
||||
description: |
|
||||
Keeps a set of rules, an order and a limit under a name. Only that is stored: the songs are worked
|
||||
out again every time. A user can have 200. Names can repeat. Use `POST /smart-playlists/preview`
|
||||
to try the rules first.
|
||||
security: [{bearerAuth: []}]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/CreateSmartPlaylist'}
|
||||
responses:
|
||||
'201':
|
||||
description: The smart playlist.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/SmartPlaylist'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'409':
|
||||
description: A user can have 200 smart playlists (`limit_reached`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
|
||||
/smart-playlists/preview:
|
||||
post:
|
||||
tags: [Smart playlists]
|
||||
operationId: previewSmartPlaylist
|
||||
summary: Try rules without keeping them
|
||||
description: |
|
||||
Works the songs out for rules that are not kept, the way `GET /smart-playlists/{id}/tracks` would,
|
||||
so they can be tried while they are written. Nothing is stored.
|
||||
security: [{bearerAuth: []}]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/PreviewSmartPlaylist'}
|
||||
responses:
|
||||
'200':
|
||||
description: The songs.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/SmartTracks'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
|
||||
/smart-playlists/{id}:
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Id'}
|
||||
get:
|
||||
tags: [Smart playlists]
|
||||
operationId: getSmartPlaylist
|
||||
summary: A smart playlist
|
||||
description: |
|
||||
Its name, rules, order and limit, without the songs. One of somebody else answers
|
||||
`smart_playlist_not_found`.
|
||||
security: [{bearerAuth: []}]
|
||||
responses:
|
||||
'200':
|
||||
description: The smart playlist.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/SmartPlaylist'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404': {$ref: '#/components/responses/SmartPlaylistNotFound'}
|
||||
patch:
|
||||
tags: [Smart playlists]
|
||||
operationId: updateSmartPlaylist
|
||||
summary: Change a smart playlist
|
||||
description: |
|
||||
Changes what is given. The rules, when given, replace the old ones whole. An empty description clears
|
||||
it; a name cannot be empty.
|
||||
security: [{bearerAuth: []}]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/UpdateSmartPlaylist'}
|
||||
responses:
|
||||
'200':
|
||||
description: The smart playlist as it is now.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/SmartPlaylist'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404': {$ref: '#/components/responses/SmartPlaylistNotFound'}
|
||||
delete:
|
||||
tags: [Smart playlists]
|
||||
operationId: deleteSmartPlaylist
|
||||
summary: Delete a smart playlist
|
||||
description: Deletes the rules. Playlists that were made from it with `POST /smart-playlists/{id}/playlist` stay.
|
||||
security: [{bearerAuth: []}]
|
||||
responses:
|
||||
'204': {description: Deleted.}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404': {$ref: '#/components/responses/SmartPlaylistNotFound'}
|
||||
|
||||
/smart-playlists/{id}/tracks:
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Id'}
|
||||
get:
|
||||
tags: [Smart playlists]
|
||||
operationId: getSmartPlaylistTracks
|
||||
summary: The songs of a smart playlist, now
|
||||
description: |
|
||||
Works the rules out on the playable songs of the library, with your own listening history and taste
|
||||
for the rules that use them, puts them in the order and cuts the end at the limit. A song whose files
|
||||
are gone is never offered. Nothing is stored, and nothing is asked of a provider. With the random
|
||||
order the songs stay the same all day, and another `variant` gives other ones.
|
||||
security: [{bearerAuth: []}]
|
||||
parameters:
|
||||
- name: variant
|
||||
in: query
|
||||
description: Other songs for the random order, from 0 (the songs of the day) up.
|
||||
schema: {type: integer, minimum: 0, maximum: 1000000, default: 0}
|
||||
responses:
|
||||
'200':
|
||||
description: The songs.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/SmartTracks'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404': {$ref: '#/components/responses/SmartPlaylistNotFound'}
|
||||
|
||||
/smart-playlists/{id}/playlist:
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Id'}
|
||||
post:
|
||||
tags: [Smart playlists]
|
||||
operationId: saveSmartPlaylist
|
||||
summary: Keep a smart playlist as a playlist
|
||||
description: |
|
||||
Makes a playlist of your own with the songs that `variant` gives now, in the same order. The playlist
|
||||
is a copy: it does not change afterwards, and is yours to edit. The body is optional.
|
||||
security: [{bearerAuth: []}]
|
||||
requestBody:
|
||||
required: false
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/SaveSmartPlaylist'}
|
||||
responses:
|
||||
'201':
|
||||
description: The playlist.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Playlist'}
|
||||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'404': {$ref: '#/components/responses/SmartPlaylistNotFound'}
|
||||
'409': {$ref: '#/components/responses/PlaylistLimit'}
|
||||
'422':
|
||||
description: The rules give no songs to save (`smart_playlist_empty`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
|
||||
/playlists:
|
||||
get:
|
||||
tags: [Playlists]
|
||||
|
|
@ -2555,6 +2742,11 @@ components:
|
|||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
SmartPlaylistNotFound:
|
||||
description: No such smart playlist, or it is somebody else's (`smart_playlist_not_found`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
PlaylistNotFound:
|
||||
description: No such playlist, or it is somebody else's (`playlist_not_found`).
|
||||
content:
|
||||
|
|
@ -3193,6 +3385,113 @@ components:
|
|||
name: {type: string, maxLength: 200, description: "The name of the playlist; blank or absent means the title of the mix."}
|
||||
variant: {type: integer, minimum: 0, maximum: 1000000, description: "The variant the client showed; absent means the mix of the day."}
|
||||
|
||||
SmartRules:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
description: |
|
||||
Every rule that is set must hold; one that is absent, empty, false or 0 does not count. Of a list rule a
|
||||
song needs to match one item.
|
||||
properties:
|
||||
genres: {type: array, maxItems: 50, items: {type: string}, description: "Kept lower case, with `+`, `-`, `_` and `/` taken for spaces: `jazz+funk` and `jazz-funk` are one genre."}
|
||||
not_genres: {type: array, maxItems: 50, items: {type: string}, description: Leaves out songs of these genres.}
|
||||
artist_ids: {type: array, maxItems: 50, items: {type: integer, format: int64, minimum: 1}, description: Songs whose main artist is one of these.}
|
||||
not_artist_ids: {type: array, maxItems: 50, items: {type: integer, format: int64, minimum: 1}}
|
||||
album_ids: {type: array, maxItems: 50, items: {type: integer, format: int64, minimum: 1}}
|
||||
not_album_ids: {type: array, maxItems: 50, items: {type: integer, format: int64, minimum: 1}}
|
||||
formats: {type: array, maxItems: 50, items: {type: string}, description: "File formats in lower case (`flac`, `mpeg`). A song counts when a file of it that is not missing has one of them."}
|
||||
min_bitrate_kbps: {type: integer, minimum: 0, maximum: 100000, description: Needs a file of the song with this bitrate or more (the same file as `formats`).}
|
||||
added_within_days: {type: integer, minimum: 0, maximum: 36500, description: "About the date of the file, which is the best the index knows."}
|
||||
never_played: {type: boolean, description: Songs you never listened to.}
|
||||
played_within_days: {type: integer, minimum: 0, maximum: 36500}
|
||||
not_played_within_days: {type: integer, minimum: 0, maximum: 36500, description: Songs not played for this many days (the ones never played too).}
|
||||
min_plays: {type: integer, minimum: 0, maximum: 1000000}
|
||||
skipped_within_days: {type: integer, minimum: 0, maximum: 36500}
|
||||
not_skipped_within_days: {type: integer, minimum: 0, maximum: 36500, description: Leaves out songs skipped in the last days.}
|
||||
liked_artists_only: {type: boolean, description: Only songs by artists you like.}
|
||||
without_avoided_artists: {type: boolean, description: Leaves out songs by artists you avoid.}
|
||||
min_duration_sec: {type: integer, minimum: 0, maximum: 86400}
|
||||
max_duration_sec: {type: integer, minimum: 0, maximum: 86400}
|
||||
|
||||
SmartSort:
|
||||
type: string
|
||||
enum: [random, most_played, least_played, recently_added, title]
|
||||
description: A tie always goes to the lower song id.
|
||||
|
||||
SmartPlaylist:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [id, name, description, rules, sort, limit, created_at, updated_at]
|
||||
properties:
|
||||
id: {type: integer, format: int64}
|
||||
name: {type: string}
|
||||
description: {type: string}
|
||||
rules: {$ref: '#/components/schemas/SmartRules'}
|
||||
sort: {$ref: '#/components/schemas/SmartSort'}
|
||||
limit: {type: integer, minimum: 1, maximum: 500, description: The most songs it gives.}
|
||||
created_at: {type: string, format: date-time}
|
||||
updated_at: {type: string, format: date-time}
|
||||
|
||||
SmartPlaylistPage:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [items, total, limit, offset]
|
||||
properties:
|
||||
items:
|
||||
type: array
|
||||
items: {$ref: '#/components/schemas/SmartPlaylist'}
|
||||
total: {type: integer}
|
||||
limit: {type: integer}
|
||||
offset: {type: integer}
|
||||
|
||||
CreateSmartPlaylist:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [name]
|
||||
properties:
|
||||
name: {type: string, minLength: 1, maxLength: 200, description: Trimmed.}
|
||||
description: {type: string, maxLength: 2000}
|
||||
rules: {$ref: '#/components/schemas/SmartRules'}
|
||||
sort: {$ref: '#/components/schemas/SmartSort'}
|
||||
limit: {type: integer, minimum: 1, maximum: 500, default: 50}
|
||||
|
||||
UpdateSmartPlaylist:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
description: What is given changes; the rules replace the old ones whole.
|
||||
properties:
|
||||
name: {type: string, minLength: 1, maxLength: 200}
|
||||
description: {type: string, maxLength: 2000}
|
||||
rules: {$ref: '#/components/schemas/SmartRules'}
|
||||
sort: {$ref: '#/components/schemas/SmartSort'}
|
||||
limit: {type: integer, minimum: 1, maximum: 500}
|
||||
|
||||
PreviewSmartPlaylist:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
properties:
|
||||
rules: {$ref: '#/components/schemas/SmartRules'}
|
||||
sort: {$ref: '#/components/schemas/SmartSort'}
|
||||
limit: {type: integer, minimum: 1, maximum: 500, default: 50}
|
||||
variant: {type: integer, minimum: 0, maximum: 1000000, default: 0}
|
||||
|
||||
SmartTracks:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [variant, tracks]
|
||||
properties:
|
||||
variant: {type: integer, minimum: 0}
|
||||
tracks:
|
||||
type: array
|
||||
description: In the order of the playlist. Empty when no song meets the rules.
|
||||
items: {$ref: '#/components/schemas/Track'}
|
||||
|
||||
SaveSmartPlaylist:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
properties:
|
||||
name: {type: string, maxLength: 200, description: "The name of the playlist; blank or absent means the name of the smart playlist."}
|
||||
variant: {type: integer, minimum: 0, maximum: 1000000, description: "The variant the client showed; absent means the songs of the day."}
|
||||
|
||||
Playlist:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
|
|
|
|||
|
|
@ -159,6 +159,9 @@ func normalizeGenre(g string) string {
|
|||
return g
|
||||
}
|
||||
|
||||
// NormalizeGenre is normalizeGenre for the packages that compare genres the way the mixes do.
|
||||
func NormalizeGenre(g string) string { return normalizeGenre(g) }
|
||||
|
||||
func titleCase(s string) string {
|
||||
words := strings.Fields(s)
|
||||
for i, w := range words {
|
||||
|
|
|
|||
615
internal/smart/smart.go
Normal file
615
internal/smart/smart.go
Normal file
|
|
@ -0,0 +1,615 @@
|
|||
// Package smart keeps the smart playlists of each user: playlists that are a set of rules instead of a
|
||||
// list of songs, worked out again every time they are opened, so the songs that arrive and what the
|
||||
// listener plays change them by themselves.
|
||||
//
|
||||
// A smart playlist is made of Rules (every rule that is set must hold), an order and a limit. Only that is
|
||||
// stored. The songs come from the playable songs of the library, then from the history and the taste of the
|
||||
// owner, the way the mixes are made (see package mixes): nothing here contacts anyone, and a smart playlist
|
||||
// that belongs to somebody else is as if it did not exist (ErrNotFound).
|
||||
//
|
||||
// "Random" is not new at every request: like a mix it stays the same for a day, and a variant number asks
|
||||
// for another.
|
||||
package smart
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/catalog"
|
||||
"codeberg.org/squidd/sonda/internal/mixes"
|
||||
"codeberg.org/squidd/sonda/internal/taste"
|
||||
)
|
||||
|
||||
// Limits. They keep one user from filling the database and one request from doing too much.
|
||||
const (
|
||||
MaxNameLen = 200
|
||||
MaxDescriptionLen = 2000
|
||||
MaxListed = 50 // genres, artists, albums or formats in one rule
|
||||
MaxDays = 36_500
|
||||
MaxPlays = 1_000_000
|
||||
MaxBitrateKbps = 100_000
|
||||
MaxDurationSec = 86_400
|
||||
DefaultLimit = 50
|
||||
MaxLimit = 500
|
||||
MaxPlaylists = 200 // smart playlists of one user
|
||||
MaxVariant = mixes.MaxVariant
|
||||
DefaultPageLimit = 50
|
||||
MaxPageLimit = 200
|
||||
)
|
||||
|
||||
// Errors returned by the service and the store.
|
||||
var (
|
||||
ErrInvalidInput = errors.New("invalid input")
|
||||
ErrNotFound = errors.New("smart playlist not found")
|
||||
ErrTooMany = errors.New("limit reached")
|
||||
)
|
||||
|
||||
// Sort is the order of the songs of a smart playlist, before the limit cuts the end.
|
||||
type Sort string
|
||||
|
||||
// The orders. A tie always goes to the lower song id.
|
||||
const (
|
||||
SortRandom Sort = "random"
|
||||
SortMostPlayed Sort = "most_played"
|
||||
SortLeastPlayed Sort = "least_played"
|
||||
SortRecentlyAdded Sort = "recently_added"
|
||||
SortTitle Sort = "title"
|
||||
)
|
||||
|
||||
func (s Sort) valid() bool {
|
||||
switch s {
|
||||
case SortRandom, SortMostPlayed, SortLeastPlayed, SortRecentlyAdded, SortTitle:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Rules say which songs a smart playlist takes. A rule that is not set (empty, false or 0) does not count;
|
||||
// every rule that is set must hold. The songs of a list rule match when they are by one of the artists, in
|
||||
// one of the albums, have one of the genres or are in one of the formats.
|
||||
type Rules struct {
|
||||
// Genres are kept as they are compared: lower case, with "+", "-", "_" and "/" taken for spaces
|
||||
// ("jazz+funk" and "jazz-funk" are the same genre).
|
||||
Genres []string `json:"genres,omitempty"`
|
||||
NotGenres []string `json:"not_genres,omitempty"`
|
||||
// Artists are the main artists of the song.
|
||||
ArtistIDs []int64 `json:"artist_ids,omitempty"`
|
||||
NotArtistIDs []int64 `json:"not_artist_ids,omitempty"`
|
||||
AlbumIDs []int64 `json:"album_ids,omitempty"`
|
||||
NotAlbumIDs []int64 `json:"not_album_ids,omitempty"`
|
||||
// Formats are those of the files ("flac", "mpeg"), in lower case. A song counts when a file of it, that is
|
||||
// not missing, has one of them (and, with MinBitrateKbps, that bitrate or more).
|
||||
Formats []string `json:"formats,omitempty"`
|
||||
MinBitrateKbps int `json:"min_bitrate_kbps,omitempty"`
|
||||
// AddedWithinDays is about the date of the file, which is the best the index knows.
|
||||
AddedWithinDays int `json:"added_within_days,omitempty"`
|
||||
// The listening rules are about the owner's history: a listen counts when the play was completed or lasted
|
||||
// the minimum listen of the server, and a skip when it came before that.
|
||||
NeverPlayed bool `json:"never_played,omitempty"`
|
||||
PlayedWithinDays int `json:"played_within_days,omitempty"`
|
||||
NotPlayedWithinDays int `json:"not_played_within_days,omitempty"`
|
||||
MinPlays int `json:"min_plays,omitempty"`
|
||||
SkippedWithinDays int `json:"skipped_within_days,omitempty"`
|
||||
NotSkippedWithinDays int `json:"not_skipped_within_days,omitempty"`
|
||||
// LikedArtistsOnly keeps the songs of artists the owner likes; WithoutAvoidedArtists leaves out the ones
|
||||
// the owner avoids (see package taste).
|
||||
LikedArtistsOnly bool `json:"liked_artists_only,omitempty"`
|
||||
WithoutAvoidedArtists bool `json:"without_avoided_artists,omitempty"`
|
||||
MinDurationSec int `json:"min_duration_sec,omitempty"`
|
||||
MaxDurationSec int `json:"max_duration_sec,omitempty"`
|
||||
}
|
||||
|
||||
// SmartPlaylist is a smart playlist.
|
||||
type SmartPlaylist struct {
|
||||
ID int64
|
||||
Name string
|
||||
Description string
|
||||
Rules Rules
|
||||
Sort Sort
|
||||
Limit int
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// Input is what makes a smart playlist, or the preview of one. A zero Sort is random and a zero Limit is
|
||||
// DefaultLimit.
|
||||
type Input struct {
|
||||
Name string
|
||||
Description string
|
||||
Rules Rules
|
||||
Sort Sort
|
||||
Limit int
|
||||
}
|
||||
|
||||
// Patch changes the smart playlist in the fields that are not nil. Rules replace the old ones whole.
|
||||
type Patch struct {
|
||||
Name *string
|
||||
Description *string
|
||||
Rules *Rules
|
||||
Sort *Sort
|
||||
Limit *int
|
||||
}
|
||||
|
||||
// History is what one listener did with one song.
|
||||
type History = mixes.History
|
||||
|
||||
// Candidate is a playable song the rules may take, with what they need to know of it.
|
||||
type Candidate struct {
|
||||
TrackID int64
|
||||
ArtistID int64 // the main artist, 0 when there is none
|
||||
Title string
|
||||
DurationMs int64
|
||||
AddedAt time.Time // the date of its newest file that is not missing
|
||||
}
|
||||
|
||||
// Filter is the part of the rules that the index answers: every list that is set narrows the songs.
|
||||
// Genres are spelled as the files have them.
|
||||
type Filter struct {
|
||||
Genres, NotGenres []string
|
||||
ArtistIDs, NotArtistIDs []int64
|
||||
AlbumIDs, NotAlbumIDs []int64
|
||||
Formats []string
|
||||
MinBitrateKbps int
|
||||
}
|
||||
|
||||
// Store keeps the smart playlists and reads what they are made of. Every call that is about one playlist is
|
||||
// for one user and gives ErrNotFound for a playlist that does not exist or is not theirs.
|
||||
type Store interface {
|
||||
// Create keeps a playlist (its ID is ignored), or gives ErrTooMany when the user has MaxPlaylists.
|
||||
Create(ctx context.Context, userID int64, p SmartPlaylist, now time.Time) (SmartPlaylist, error)
|
||||
// List returns a page of the user's smart playlists, the one changed last first, and how many there are.
|
||||
List(ctx context.Context, userID int64, limit, offset int) ([]SmartPlaylist, int, error)
|
||||
Get(ctx context.Context, userID, id int64) (SmartPlaylist, error)
|
||||
// Update replaces name, description, rules, order and limit.
|
||||
Update(ctx context.Context, userID, id int64, p SmartPlaylist, now time.Time) (SmartPlaylist, error)
|
||||
Delete(ctx context.Context, userID, id int64) error
|
||||
|
||||
// Genres returns every genre of the playable songs, as spelled in the files.
|
||||
Genres(ctx context.Context) ([]mixes.Genre, error)
|
||||
// Candidates returns the playable songs of the filter.
|
||||
Candidates(ctx context.Context, f Filter) ([]Candidate, error)
|
||||
// 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)
|
||||
// Tracks returns the songs in the order given, leaving out any that cannot be played.
|
||||
Tracks(ctx context.Context, ids []int64) ([]catalog.Track, error)
|
||||
}
|
||||
|
||||
// Taster says what the listener likes: *taste.Service.
|
||||
type Taster interface {
|
||||
Learn(ctx context.Context, userID int64) (taste.Learned, error)
|
||||
}
|
||||
|
||||
// Service validates what it is given, keeps the smart playlists and works their songs out.
|
||||
type Service struct {
|
||||
store Store
|
||||
taster Taster
|
||||
minListen time.Duration
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
// 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}
|
||||
}
|
||||
|
||||
func invalid(format string, a ...any) error {
|
||||
return fmt.Errorf("%w: %s", ErrInvalidInput, fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
func cleanName(name string) (string, error) {
|
||||
name = strings.TrimSpace(name)
|
||||
if name == "" || utf8.RuneCountInString(name) > MaxNameLen {
|
||||
return "", invalid("the name must have 1 to %d characters", MaxNameLen)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
||||
func cleanDescription(d string) (string, error) {
|
||||
d = strings.TrimSpace(d)
|
||||
if utf8.RuneCountInString(d) > MaxDescriptionLen {
|
||||
return "", invalid("the description can have up to %d characters", MaxDescriptionLen)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
var formatRe = regexp.MustCompile(`^[a-z0-9]{1,16}$`)
|
||||
|
||||
func cleanGenres(rule string, in []string) ([]string, error) {
|
||||
if len(in) > MaxListed {
|
||||
return nil, invalid("%s can have up to %d genres", rule, MaxListed)
|
||||
}
|
||||
var out []string
|
||||
for _, g := range in {
|
||||
n := mixes.NormalizeGenre(g)
|
||||
if n == "" {
|
||||
return nil, invalid("%q is not a genre", g)
|
||||
}
|
||||
if !slices.Contains(out, n) {
|
||||
out = append(out, n)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func cleanFormats(in []string) ([]string, error) {
|
||||
if len(in) > MaxListed {
|
||||
return nil, invalid("formats can have up to %d items", MaxListed)
|
||||
}
|
||||
var out []string
|
||||
for _, f := range in {
|
||||
f = strings.ToLower(strings.TrimSpace(f))
|
||||
if !formatRe.MatchString(f) {
|
||||
return nil, invalid("%q is not a format", f)
|
||||
}
|
||||
if !slices.Contains(out, f) {
|
||||
out = append(out, f)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func cleanIDs(rule string, in []int64) ([]int64, error) {
|
||||
if len(in) > MaxListed {
|
||||
return nil, invalid("%s can have up to %d ids", rule, MaxListed)
|
||||
}
|
||||
var out []int64
|
||||
for _, id := range in {
|
||||
if id < 1 {
|
||||
return nil, invalid("%s must be ids, from 1", rule)
|
||||
}
|
||||
if !slices.Contains(out, id) {
|
||||
out = append(out, id)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func inRange(rule string, n, max int) error {
|
||||
if n < 0 || n > max {
|
||||
return invalid("%s must be from 0 to %d", rule, max)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// cleanRules checks the rules and returns them in the form they are kept in.
|
||||
func cleanRules(r Rules) (Rules, error) {
|
||||
var err error
|
||||
if r.Genres, err = cleanGenres("genres", r.Genres); err != nil {
|
||||
return Rules{}, err
|
||||
}
|
||||
if r.NotGenres, err = cleanGenres("not_genres", r.NotGenres); err != nil {
|
||||
return Rules{}, err
|
||||
}
|
||||
for _, l := range []struct {
|
||||
name string
|
||||
ids *[]int64
|
||||
}{{"artist_ids", &r.ArtistIDs}, {"not_artist_ids", &r.NotArtistIDs}, {"album_ids", &r.AlbumIDs}, {"not_album_ids", &r.NotAlbumIDs}} {
|
||||
if *l.ids, err = cleanIDs(l.name, *l.ids); err != nil {
|
||||
return Rules{}, err
|
||||
}
|
||||
}
|
||||
if r.Formats, err = cleanFormats(r.Formats); err != nil {
|
||||
return Rules{}, err
|
||||
}
|
||||
for _, n := range []struct {
|
||||
name string
|
||||
got, max int
|
||||
}{
|
||||
{"min_bitrate_kbps", r.MinBitrateKbps, MaxBitrateKbps},
|
||||
{"added_within_days", r.AddedWithinDays, MaxDays},
|
||||
{"played_within_days", r.PlayedWithinDays, MaxDays},
|
||||
{"not_played_within_days", r.NotPlayedWithinDays, MaxDays},
|
||||
{"skipped_within_days", r.SkippedWithinDays, MaxDays},
|
||||
{"not_skipped_within_days", r.NotSkippedWithinDays, MaxDays},
|
||||
{"min_plays", r.MinPlays, MaxPlays},
|
||||
{"min_duration_sec", r.MinDurationSec, MaxDurationSec},
|
||||
{"max_duration_sec", r.MaxDurationSec, MaxDurationSec},
|
||||
} {
|
||||
if err := inRange(n.name, n.got, n.max); err != nil {
|
||||
return Rules{}, err
|
||||
}
|
||||
}
|
||||
if r.MaxDurationSec > 0 && r.MinDurationSec > r.MaxDurationSec {
|
||||
return Rules{}, invalid("min_duration_sec is more than max_duration_sec")
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// cleanShape checks the order and the limit and gives them their defaults.
|
||||
func cleanShape(sortBy Sort, limit int) (Sort, int, error) {
|
||||
if sortBy == "" {
|
||||
sortBy = SortRandom
|
||||
}
|
||||
if !sortBy.valid() {
|
||||
return "", 0, invalid("sort must be random, most_played, least_played, recently_added or title")
|
||||
}
|
||||
if limit == 0 {
|
||||
limit = DefaultLimit
|
||||
}
|
||||
if limit < 1 || limit > MaxLimit {
|
||||
return "", 0, invalid("limit must be from 1 to %d", MaxLimit)
|
||||
}
|
||||
return sortBy, limit, nil
|
||||
}
|
||||
|
||||
// clean checks everything but the name, which a preview does not have.
|
||||
func clean(in Input) (Input, error) {
|
||||
var err error
|
||||
if in.Description, err = cleanDescription(in.Description); err != nil {
|
||||
return Input{}, err
|
||||
}
|
||||
if in.Rules, err = cleanRules(in.Rules); err != nil {
|
||||
return Input{}, err
|
||||
}
|
||||
if in.Sort, in.Limit, err = cleanShape(in.Sort, in.Limit); err != nil {
|
||||
return Input{}, err
|
||||
}
|
||||
return in, nil
|
||||
}
|
||||
|
||||
// Create keeps a smart playlist for the user.
|
||||
func (s *Service) Create(ctx context.Context, userID int64, in Input) (SmartPlaylist, error) {
|
||||
name, err := cleanName(in.Name)
|
||||
if err != nil {
|
||||
return SmartPlaylist{}, err
|
||||
}
|
||||
in, err = clean(in)
|
||||
if err != nil {
|
||||
return SmartPlaylist{}, err
|
||||
}
|
||||
return s.store.Create(ctx, userID, SmartPlaylist{Name: name, Description: in.Description, Rules: in.Rules, Sort: in.Sort, Limit: in.Limit}, s.now())
|
||||
}
|
||||
|
||||
// Listing is a page of smart playlists.
|
||||
type Listing struct {
|
||||
Items []SmartPlaylist
|
||||
Total int
|
||||
Limit int
|
||||
Offset int
|
||||
}
|
||||
|
||||
// List returns a page of the user's smart playlists, the one changed last first. A limit of 0 is
|
||||
// DefaultPageLimit, and one above MaxPageLimit is lowered to it, as for the playlists.
|
||||
func (s *Service) List(ctx context.Context, userID int64, limit, offset int) (Listing, error) {
|
||||
if limit < 0 || offset < 0 {
|
||||
return Listing{}, invalid("limit and offset cannot be negative")
|
||||
}
|
||||
switch {
|
||||
case limit == 0:
|
||||
limit = DefaultPageLimit
|
||||
case limit > MaxPageLimit:
|
||||
limit = MaxPageLimit
|
||||
}
|
||||
items, total, err := s.store.List(ctx, userID, limit, offset)
|
||||
if err != nil {
|
||||
return Listing{}, err
|
||||
}
|
||||
return Listing{Items: items, Total: total, Limit: limit, Offset: offset}, nil
|
||||
}
|
||||
|
||||
// Get returns a smart playlist of the user, without its songs.
|
||||
func (s *Service) Get(ctx context.Context, userID, id int64) (SmartPlaylist, error) {
|
||||
return s.store.Get(ctx, userID, id)
|
||||
}
|
||||
|
||||
// Update changes the fields of the patch that are not nil.
|
||||
func (s *Service) Update(ctx context.Context, userID, id int64, p Patch) (SmartPlaylist, error) {
|
||||
cur, err := s.store.Get(ctx, userID, id)
|
||||
if err != nil {
|
||||
return SmartPlaylist{}, err
|
||||
}
|
||||
in := Input{Name: cur.Name, Description: cur.Description, Rules: cur.Rules, Sort: cur.Sort, Limit: cur.Limit}
|
||||
if p.Name != nil {
|
||||
in.Name = *p.Name
|
||||
}
|
||||
if p.Description != nil {
|
||||
in.Description = *p.Description
|
||||
}
|
||||
if p.Rules != nil {
|
||||
in.Rules = *p.Rules
|
||||
}
|
||||
if p.Sort != nil {
|
||||
in.Sort = *p.Sort
|
||||
}
|
||||
if p.Limit != nil {
|
||||
in.Limit = *p.Limit
|
||||
}
|
||||
if in.Name, err = cleanName(in.Name); err != nil {
|
||||
return SmartPlaylist{}, err
|
||||
}
|
||||
if in, err = clean(in); err != nil {
|
||||
return SmartPlaylist{}, err
|
||||
}
|
||||
return s.store.Update(ctx, userID, id, SmartPlaylist{Name: in.Name, Description: in.Description, Rules: in.Rules, Sort: in.Sort, Limit: in.Limit}, s.now())
|
||||
}
|
||||
|
||||
// Delete removes a smart playlist of the user.
|
||||
func (s *Service) Delete(ctx context.Context, userID, id int64) error {
|
||||
return s.store.Delete(ctx, userID, id)
|
||||
}
|
||||
|
||||
// Tracks works the songs of a smart playlist out. variant 0 is the one of the day; with a random order,
|
||||
// another number gives other songs.
|
||||
func (s *Service) Tracks(ctx context.Context, userID, id int64, variant int) (SmartPlaylist, []catalog.Track, error) {
|
||||
if err := checkVariant(variant); err != nil {
|
||||
return SmartPlaylist{}, nil, err
|
||||
}
|
||||
p, err := s.store.Get(ctx, userID, id)
|
||||
if err != nil {
|
||||
return SmartPlaylist{}, nil, err
|
||||
}
|
||||
tracks, err := s.run(ctx, userID, p.ID, p.Rules, p.Sort, p.Limit, variant)
|
||||
return p, tracks, err
|
||||
}
|
||||
|
||||
// Preview works the songs out for rules that are not kept, to try them before saving.
|
||||
func (s *Service) Preview(ctx context.Context, userID int64, in Input, variant int) ([]catalog.Track, error) {
|
||||
if err := checkVariant(variant); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
in, err := clean(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.run(ctx, userID, 0, in.Rules, in.Sort, in.Limit, variant)
|
||||
}
|
||||
|
||||
func checkVariant(v int) error {
|
||||
if v < 0 || v > MaxVariant {
|
||||
return invalid("variant must be from 0 to %d", MaxVariant)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// spellings returns the spellings the files have of the genres (all compared as normalized).
|
||||
func spellings(all []mixes.Genre, genres []string) []string {
|
||||
var out []string
|
||||
for _, g := range all {
|
||||
if slices.Contains(genres, mixes.NormalizeGenre(g.Name)) {
|
||||
out = append(out, g.Name)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Service) run(ctx context.Context, userID, id int64, r Rules, order Sort, limit, variant int) ([]catalog.Track, error) {
|
||||
f := Filter{
|
||||
ArtistIDs: r.ArtistIDs, NotArtistIDs: r.NotArtistIDs, AlbumIDs: r.AlbumIDs, NotAlbumIDs: r.NotAlbumIDs,
|
||||
Formats: r.Formats, MinBitrateKbps: r.MinBitrateKbps,
|
||||
}
|
||||
if len(r.Genres) > 0 || len(r.NotGenres) > 0 {
|
||||
all, err := s.store.Genres(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read genres: %w", err)
|
||||
}
|
||||
f.Genres, f.NotGenres = spellings(all, r.Genres), spellings(all, r.NotGenres)
|
||||
if len(r.Genres) > 0 && len(f.Genres) == 0 {
|
||||
return []catalog.Track{}, nil // no file has any of the genres
|
||||
}
|
||||
}
|
||||
cands, err := s.store.Candidates(ctx, f)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read the songs: %w", err)
|
||||
}
|
||||
|
||||
var history map[int64]History
|
||||
if order == SortMostPlayed || order == SortLeastPlayed || r.NeverPlayed || r.MinPlays > 0 ||
|
||||
r.PlayedWithinDays > 0 || r.NotPlayedWithinDays > 0 || r.SkippedWithinDays > 0 || r.NotSkippedWithinDays > 0 {
|
||||
if history, err = s.store.History(ctx, userID, s.minListen); err != nil {
|
||||
return nil, fmt.Errorf("read history: %w", err)
|
||||
}
|
||||
}
|
||||
var learned taste.Learned
|
||||
if r.LikedArtistsOnly || r.WithoutAvoidedArtists {
|
||||
if learned, err = s.taster.Learn(ctx, userID); err != nil {
|
||||
return nil, fmt.Errorf("learn taste: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
now := s.now()
|
||||
within := func(t time.Time, days int) bool {
|
||||
return !t.IsZero() && now.Sub(t) <= time.Duration(days)*24*time.Hour
|
||||
}
|
||||
kept := make([]Candidate, 0, len(cands))
|
||||
for _, c := range cands {
|
||||
h := history[c.TrackID]
|
||||
switch {
|
||||
case r.AddedWithinDays > 0 && !within(c.AddedAt, r.AddedWithinDays),
|
||||
r.MinDurationSec > 0 && c.DurationMs < int64(r.MinDurationSec)*1000,
|
||||
r.MaxDurationSec > 0 && c.DurationMs > int64(r.MaxDurationSec)*1000,
|
||||
r.NeverPlayed && h.Plays > 0,
|
||||
r.PlayedWithinDays > 0 && !within(h.LastPlayed, r.PlayedWithinDays),
|
||||
r.NotPlayedWithinDays > 0 && within(h.LastPlayed, r.NotPlayedWithinDays),
|
||||
r.MinPlays > 0 && h.Plays < r.MinPlays,
|
||||
r.SkippedWithinDays > 0 && !within(h.SkippedAt, r.SkippedWithinDays),
|
||||
r.NotSkippedWithinDays > 0 && within(h.SkippedAt, r.NotSkippedWithinDays),
|
||||
r.LikedArtistsOnly && learned.Prefs[c.ArtistID].Preference <= 0,
|
||||
r.WithoutAvoidedArtists && learned.Prefs[c.ArtistID].Preference < 0:
|
||||
continue
|
||||
}
|
||||
kept = append(kept, c)
|
||||
}
|
||||
|
||||
sortCandidates(kept, order, history, func(c Candidate) uint64 {
|
||||
h := fnv.New64a()
|
||||
fmt.Fprintf(h, "%d|%d|%d|%d|%d", userID, id, now.Unix()/86400, variant, c.TrackID)
|
||||
return h.Sum64()
|
||||
})
|
||||
if len(kept) > limit {
|
||||
kept = kept[:limit]
|
||||
}
|
||||
picked := make([]int64, len(kept))
|
||||
for i, c := range kept {
|
||||
picked[i] = c.TrackID
|
||||
}
|
||||
tracks, err := s.store.Tracks(ctx, picked)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read songs: %w", err)
|
||||
}
|
||||
return tracks, nil
|
||||
}
|
||||
|
||||
// sortCandidates puts the songs in the order; a tie goes to the lower id. A random order sorts by the
|
||||
// hash of the song with the seed of the day, which is the same until the day or the variant changes.
|
||||
func sortCandidates(c []Candidate, order Sort, history map[int64]History, hash func(Candidate) uint64) {
|
||||
var keys map[int64]uint64
|
||||
if order == SortRandom {
|
||||
keys = make(map[int64]uint64, len(c))
|
||||
for _, x := range c {
|
||||
keys[x.TrackID] = hash(x)
|
||||
}
|
||||
}
|
||||
less := func(a, b Candidate) int {
|
||||
switch order {
|
||||
case SortRandom:
|
||||
if ka, kb := keys[a.TrackID], keys[b.TrackID]; ka != kb {
|
||||
if ka < kb {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
case SortMostPlayed, SortLeastPlayed:
|
||||
pa, pb := history[a.TrackID].Plays, history[b.TrackID].Plays
|
||||
if order == SortLeastPlayed {
|
||||
pa, pb = pb, pa
|
||||
}
|
||||
if pa != pb {
|
||||
if pa > pb {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
case SortRecentlyAdded:
|
||||
if !a.AddedAt.Equal(b.AddedAt) {
|
||||
if a.AddedAt.After(b.AddedAt) {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
case SortTitle:
|
||||
if a.Title != b.Title {
|
||||
if a.Title < b.Title {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case a.TrackID < b.TrackID:
|
||||
return -1
|
||||
case a.TrackID > b.TrackID:
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
sort.SliceStable(c, func(i, j int) bool { return less(c[i], c[j]) < 0 })
|
||||
}
|
||||
501
internal/smart/smart_test.go
Normal file
501
internal/smart/smart_test.go
Normal file
|
|
@ -0,0 +1,501 @@
|
|||
package smart
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/catalog"
|
||||
"codeberg.org/squidd/sonda/internal/mixes"
|
||||
"codeberg.org/squidd/sonda/internal/taste"
|
||||
)
|
||||
|
||||
var now = time.Date(2026, 11, 12, 10, 0, 0, 0, time.UTC)
|
||||
|
||||
func daysAgo(n int) time.Time { return now.Add(-time.Duration(n) * 24 * time.Hour) }
|
||||
|
||||
// ---- a store in memory ----
|
||||
|
||||
type fakeStore struct {
|
||||
genres []mixes.Genre
|
||||
cands []Candidate
|
||||
history map[int64]History
|
||||
asked []Filter
|
||||
saved map[int64]SmartPlaylist // by id
|
||||
nextID int64
|
||||
listened int // how many times history was read
|
||||
}
|
||||
|
||||
func newStore() *fakeStore {
|
||||
return &fakeStore{saved: map[int64]SmartPlaylist{}, history: map[int64]History{}}
|
||||
}
|
||||
|
||||
func (f *fakeStore) Genres(context.Context) ([]mixes.Genre, error) { return f.genres, nil }
|
||||
|
||||
func (f *fakeStore) Candidates(_ context.Context, flt Filter) ([]Candidate, error) {
|
||||
f.asked = append(f.asked, flt)
|
||||
return f.cands, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) History(context.Context, int64, time.Duration) (map[int64]History, error) {
|
||||
f.listened++
|
||||
return f.history, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Tracks(_ context.Context, ids []int64) ([]catalog.Track, error) {
|
||||
out := make([]catalog.Track, len(ids))
|
||||
for i, id := range ids {
|
||||
out[i] = catalog.Track{ID: id, Title: "t"}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Create(_ context.Context, userID int64, p SmartPlaylist, at time.Time) (SmartPlaylist, error) {
|
||||
f.nextID++
|
||||
p.ID, p.CreatedAt, p.UpdatedAt = f.nextID, at, at
|
||||
f.saved[p.ID] = p
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) List(context.Context, int64, int, int) ([]SmartPlaylist, int, error) {
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Get(_ context.Context, _ int64, id int64) (SmartPlaylist, error) {
|
||||
p, ok := f.saved[id]
|
||||
if !ok {
|
||||
return SmartPlaylist{}, ErrNotFound
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Update(_ context.Context, _ int64, id int64, p SmartPlaylist, at time.Time) (SmartPlaylist, error) {
|
||||
if _, ok := f.saved[id]; !ok {
|
||||
return SmartPlaylist{}, ErrNotFound
|
||||
}
|
||||
p.ID, p.UpdatedAt = id, at
|
||||
f.saved[id] = p
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Delete(context.Context, int64, int64) error { return nil }
|
||||
|
||||
type fakeTaster struct{ learned taste.Learned }
|
||||
|
||||
func (f fakeTaster) Learn(context.Context, int64) (taste.Learned, error) { return f.learned, nil }
|
||||
|
||||
func newService(s *fakeStore, learned taste.Learned) *Service {
|
||||
svc := NewService(s, fakeTaster{learned}, 20*time.Second)
|
||||
svc.now = func() time.Time { return now }
|
||||
return svc
|
||||
}
|
||||
|
||||
// songs makes n candidates, numbered from 1: song i is by artist i, is i minutes long and was added i days ago.
|
||||
func songs(n int) []Candidate {
|
||||
out := make([]Candidate, n)
|
||||
for i := range out {
|
||||
k := i + 1
|
||||
out[i] = Candidate{TrackID: int64(k), ArtistID: int64(k), Title: string(rune('a' + i)), DurationMs: int64(k) * 60_000, AddedAt: daysAgo(k)}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func ids(tracks []catalog.Track) []int64 {
|
||||
out := make([]int64, len(tracks))
|
||||
for i, t := range tracks {
|
||||
out[i] = t.ID
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// preview runs rules in the order given, with a limit big enough for every song.
|
||||
func preview(t *testing.T, svc *Service, r Rules, sort Sort) []int64 {
|
||||
t.Helper()
|
||||
tracks, err := svc.Preview(context.Background(), 1, Input{Rules: r, Sort: sort, Limit: 500}, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ids(tracks)
|
||||
}
|
||||
|
||||
// ---- what the rules keep ----
|
||||
|
||||
func TestRulesAboutListening(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(6)
|
||||
st.history = map[int64]History{
|
||||
1: {Plays: 5, LastPlayed: daysAgo(2)}, // played lately, a lot
|
||||
2: {Plays: 1, LastPlayed: daysAgo(40)}, // played long ago
|
||||
3: {Plays: 0, SkippedAt: daysAgo(3)}, // only skipped, lately
|
||||
4: {Plays: 2, LastPlayed: daysAgo(10), SkippedAt: daysAgo(1)}, // played, then skipped yesterday
|
||||
// 5 and 6 were never touched
|
||||
}
|
||||
svc := newService(st, taste.Learned{})
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
rules Rules
|
||||
want []int64
|
||||
}{
|
||||
"no rule keeps every song": {Rules{}, []int64{1, 2, 3, 4, 5, 6}},
|
||||
"never played": {Rules{NeverPlayed: true}, []int64{3, 5, 6}},
|
||||
"played in the last 7 days": {Rules{PlayedWithinDays: 7}, []int64{1}},
|
||||
"played in the last 30 days": {Rules{PlayedWithinDays: 30}, []int64{1, 4}},
|
||||
"not played for 30 days": {Rules{NotPlayedWithinDays: 30}, []int64{2, 3, 5, 6}},
|
||||
"played at least twice": {Rules{MinPlays: 2}, []int64{1, 4}},
|
||||
"skipped in the last 2 days": {Rules{SkippedWithinDays: 2}, []int64{4}},
|
||||
"skipped in the last 7 days": {Rules{SkippedWithinDays: 7}, []int64{3, 4}},
|
||||
"not skipped in the last 7 days": {Rules{NotSkippedWithinDays: 7}, []int64{1, 2, 5, 6}},
|
||||
"two rules must both hold": {Rules{NotPlayedWithinDays: 30, MinPlays: 1}, []int64{2}},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := preview(t, svc, tc.rules, SortTitle)
|
||||
if !slices.Equal(got, tc.want) {
|
||||
t.Errorf("got %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesAboutTheSongItself(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(6) // song k: k minutes, added k days ago
|
||||
svc := newService(st, taste.Learned{})
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
rules Rules
|
||||
want []int64
|
||||
}{
|
||||
"added in the last 3 days": {Rules{AddedWithinDays: 3}, []int64{1, 2, 3}},
|
||||
"at least 4 minutes": {Rules{MinDurationSec: 240}, []int64{4, 5, 6}},
|
||||
"at most 2 minutes": {Rules{MaxDurationSec: 120}, []int64{1, 2}},
|
||||
"between 2 and 4 minutes": {Rules{MinDurationSec: 120, MaxDurationSec: 240}, []int64{2, 3, 4}},
|
||||
"added lately and long": {Rules{AddedWithinDays: 5, MinDurationSec: 180}, []int64{3, 4, 5}},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := preview(t, svc, tc.rules, SortTitle)
|
||||
if !slices.Equal(got, tc.want) {
|
||||
t.Errorf("got %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRulesAboutTaste(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(4)
|
||||
learned := taste.Learned{Prefs: map[int64]taste.ArtistPref{
|
||||
1: {ArtistID: 1, Preference: 0.8}, // loved
|
||||
2: {ArtistID: 2, Preference: -0.5}, // avoided
|
||||
3: {ArtistID: 3, Preference: 0}, // neutral
|
||||
}}
|
||||
svc := newService(st, learned) // artist 4 was never listened to
|
||||
|
||||
if got := preview(t, svc, Rules{LikedArtistsOnly: true}, SortTitle); !slices.Equal(got, []int64{1}) {
|
||||
t.Errorf("liked artists only: %v, want [1]", got)
|
||||
}
|
||||
if got := preview(t, svc, Rules{WithoutAvoidedArtists: true}, SortTitle); !slices.Equal(got, []int64{1, 3, 4}) {
|
||||
t.Errorf("without avoided artists: %v, want [1 3 4]", got)
|
||||
}
|
||||
if got := preview(t, svc, Rules{}, SortTitle); !slices.Equal(got, []int64{1, 2, 3, 4}) {
|
||||
t.Errorf("no rule keeps the avoided artists too: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- the order ----
|
||||
|
||||
func TestTheOrders(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = []Candidate{
|
||||
{TrackID: 1, Title: "delta", AddedAt: daysAgo(10)},
|
||||
{TrackID: 2, Title: "alpha", AddedAt: daysAgo(30)},
|
||||
{TrackID: 3, Title: "charlie", AddedAt: daysAgo(1)},
|
||||
{TrackID: 4, Title: "bravo", AddedAt: daysAgo(20)},
|
||||
}
|
||||
st.history = map[int64]History{1: {Plays: 3}, 2: {Plays: 9}, 4: {Plays: 3}}
|
||||
svc := newService(st, taste.Learned{})
|
||||
|
||||
for name, tc := range map[string]struct {
|
||||
sort Sort
|
||||
want []int64
|
||||
}{
|
||||
"by title": {SortTitle, []int64{2, 4, 3, 1}},
|
||||
"most played first": {SortMostPlayed, []int64{2, 1, 4, 3}}, // a tie goes to the lower id
|
||||
"least played first": {SortLeastPlayed, []int64{3, 1, 4, 2}},
|
||||
"recently added first": {SortRecentlyAdded, []int64{3, 1, 4, 2}},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if got := preview(t, svc, Rules{}, tc.sort); !slices.Equal(got, tc.want) {
|
||||
t.Errorf("got %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheLimitCutsTheEndOfTheOrder(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(10)
|
||||
svc := newService(st, taste.Learned{})
|
||||
tracks, err := svc.Preview(context.Background(), 1, Input{Sort: SortTitle, Limit: 3}, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := ids(tracks); !slices.Equal(got, []int64{1, 2, 3}) {
|
||||
t.Errorf("got %v, want the first three", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandomIsTheSameAllDayAndAnotherForAnotherVariantOrDay(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(40)
|
||||
svc := newService(st, taste.Learned{})
|
||||
run := func(variant int) []int64 {
|
||||
tracks, err := svc.Preview(context.Background(), 1, Input{Sort: SortRandom, Limit: 10}, variant)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ids(tracks)
|
||||
}
|
||||
a := run(0)
|
||||
if len(a) != 10 {
|
||||
t.Fatalf("%d songs, want 10", len(a))
|
||||
}
|
||||
if b := run(0); !slices.Equal(a, b) {
|
||||
t.Errorf("the same day and variant gave %v then %v", a, b)
|
||||
}
|
||||
if b := run(1); slices.Equal(a, b) {
|
||||
t.Errorf("another variant gave the same songs: %v", a)
|
||||
}
|
||||
seen := map[int64]bool{}
|
||||
for _, id := range a {
|
||||
if seen[id] {
|
||||
t.Errorf("song %d twice in %v", id, a)
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
svc.now = func() time.Time { return now.Add(24 * time.Hour) }
|
||||
if b := run(0); slices.Equal(a, b) {
|
||||
t.Errorf("the next day gave the same songs: %v", a)
|
||||
}
|
||||
// Another listener gets their own.
|
||||
svc.now = func() time.Time { return now }
|
||||
other, err := svc.Preview(context.Background(), 2, Input{Sort: SortRandom, Limit: 10}, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if slices.Equal(a, ids(other)) {
|
||||
t.Errorf("another listener got the same songs: %v", a)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- genres, as the files spell them ----
|
||||
|
||||
func TestAGenreIsAskedInEverySpellingTheFilesHave(t *testing.T) {
|
||||
st := newStore()
|
||||
st.genres = []mixes.Genre{{Name: "Jazz+Funk"}, {Name: "jazz-funk"}, {Name: "Jazz"}, {Name: "Rock"}, {Name: "Hard Rock"}, {Name: "other"}}
|
||||
st.cands = songs(2)
|
||||
svc := newService(st, taste.Learned{})
|
||||
|
||||
preview(t, svc, Rules{Genres: []string{"Jazz Funk", "rock"}, NotGenres: []string{"hard rock"}}, SortTitle)
|
||||
got := st.asked[0]
|
||||
slices.Sort(got.Genres)
|
||||
if want := []string{"Jazz+Funk", "Rock", "jazz-funk"}; !slices.Equal(got.Genres, want) {
|
||||
t.Errorf("genres asked = %v, want %v", got.Genres, want)
|
||||
}
|
||||
if want := []string{"Hard Rock"}; !slices.Equal(got.NotGenres, want) {
|
||||
t.Errorf("genres left out = %v, want %v", got.NotGenres, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAGenreNoFileHasGivesNoSongsAndAsksNothing(t *testing.T) {
|
||||
st := newStore()
|
||||
st.genres = []mixes.Genre{{Name: "Rock"}}
|
||||
st.cands = songs(3)
|
||||
svc := newService(st, taste.Learned{})
|
||||
if got := preview(t, svc, Rules{Genres: []string{"polka"}}, SortTitle); len(got) != 0 {
|
||||
t.Errorf("got %v, want no songs", got)
|
||||
}
|
||||
if len(st.asked) != 0 {
|
||||
t.Errorf("the store was asked for songs: %v", st.asked)
|
||||
}
|
||||
// A genre to leave out that no file has leaves out nothing.
|
||||
if got := preview(t, svc, Rules{NotGenres: []string{"polka"}}, SortTitle); len(got) != 3 {
|
||||
t.Errorf("got %v, want every song", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheOtherRulesGoToTheStoreAsTheyAre(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(1)
|
||||
svc := newService(st, taste.Learned{})
|
||||
preview(t, svc, Rules{
|
||||
ArtistIDs: []int64{1, 2}, NotArtistIDs: []int64{3}, AlbumIDs: []int64{4}, NotAlbumIDs: []int64{5, 6},
|
||||
Formats: []string{"FLAC", " mpeg "}, MinBitrateKbps: 900,
|
||||
}, SortTitle)
|
||||
f := st.asked[0]
|
||||
if !slices.Equal(f.ArtistIDs, []int64{1, 2}) || !slices.Equal(f.NotArtistIDs, []int64{3}) ||
|
||||
!slices.Equal(f.AlbumIDs, []int64{4}) || !slices.Equal(f.NotAlbumIDs, []int64{5, 6}) {
|
||||
t.Errorf("filter = %+v", f)
|
||||
}
|
||||
if !slices.Equal(f.Formats, []string{"flac", "mpeg"}) || f.MinBitrateKbps != 900 {
|
||||
t.Errorf("formats = %v, bitrate = %d", f.Formats, f.MinBitrateKbps)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoryIsReadOnlyWhenARuleNeedsIt(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(3)
|
||||
svc := newService(st, taste.Learned{})
|
||||
preview(t, svc, Rules{AddedWithinDays: 30}, SortTitle)
|
||||
preview(t, svc, Rules{}, SortRandom)
|
||||
if st.listened != 0 {
|
||||
t.Errorf("history was read %d times for rules that do not use it", st.listened)
|
||||
}
|
||||
preview(t, svc, Rules{}, SortMostPlayed)
|
||||
preview(t, svc, Rules{NeverPlayed: true}, SortTitle)
|
||||
if st.listened != 2 {
|
||||
t.Errorf("history was read %d times, want 2", st.listened)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- what is accepted ----
|
||||
|
||||
func TestCreateFillsInTheDefaultsAndCleansTheText(t *testing.T) {
|
||||
st := newStore()
|
||||
svc := newService(st, taste.Learned{})
|
||||
p, err := svc.Create(context.Background(), 1, Input{Name: " Fresh ", Description: " lately added ", Rules: Rules{AddedWithinDays: 30, Genres: []string{" Rock "}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p.Name != "Fresh" || p.Description != "lately added" || p.Sort != SortRandom || p.Limit != DefaultLimit {
|
||||
t.Errorf("playlist = %+v", p)
|
||||
}
|
||||
if !slices.Equal(p.Rules.Genres, []string{"rock"}) {
|
||||
t.Errorf("genres = %v: they are kept as they are compared", p.Rules.Genres)
|
||||
}
|
||||
if p.CreatedAt != now {
|
||||
t.Errorf("created at %v, want %v", p.CreatedAt, now)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhatIsRefused(t *testing.T) {
|
||||
long := make([]string, MaxListed+1)
|
||||
for i := range long {
|
||||
long[i] = "x"
|
||||
}
|
||||
longIDs := make([]int64, MaxListed+1)
|
||||
for i := range longIDs {
|
||||
longIDs[i] = int64(i + 1)
|
||||
}
|
||||
aboutTheName := map[string]bool{"no name": true, "a name that is too long": true}
|
||||
for name, in := range map[string]Input{
|
||||
"no name": {Name: " "},
|
||||
"a name that is too long": {Name: string(make([]byte, MaxNameLen+1))},
|
||||
"a description too long": {Name: "a", Description: string(make([]rune, MaxDescriptionLen+1))},
|
||||
"an unknown order": {Name: "a", Sort: "loudest"},
|
||||
"a limit below 1": {Name: "a", Limit: -1},
|
||||
"a limit over the maximum": {Name: "a", Limit: MaxLimit + 1},
|
||||
"too many genres": {Name: "a", Rules: Rules{Genres: long}},
|
||||
"too many artists": {Name: "a", Rules: Rules{ArtistIDs: longIDs}},
|
||||
"a blank genre": {Name: "a", Rules: Rules{Genres: []string{" "}}},
|
||||
"a genre that is not one": {Name: "a", Rules: Rules{Genres: []string{"unknown"}}},
|
||||
"an id below 1": {Name: "a", Rules: Rules{AlbumIDs: []int64{0}}},
|
||||
"a format with a symbol": {Name: "a", Rules: Rules{Formats: []string{"fl/ac"}}},
|
||||
"a negative number of days": {Name: "a", Rules: Rules{PlayedWithinDays: -1}},
|
||||
"too many days": {Name: "a", Rules: Rules{AddedWithinDays: MaxDays + 1}},
|
||||
"a negative minimum": {Name: "a", Rules: Rules{MinPlays: -1}},
|
||||
"a negative bitrate": {Name: "a", Rules: Rules{MinBitrateKbps: -1}},
|
||||
"a minimum over a maximum": {Name: "a", Rules: Rules{MinDurationSec: 200, MaxDurationSec: 100}},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if _, err := newService(newStore(), taste.Learned{}).Create(context.Background(), 1, in); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("err = %v, want ErrInvalidInput", err)
|
||||
}
|
||||
if aboutTheName[name] {
|
||||
return // a preview has no name
|
||||
}
|
||||
if _, err := newService(newStore(), taste.Learned{}).Preview(context.Background(), 1, in, 0); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("preview: err = %v, want ErrInvalidInput", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
if _, err := newService(newStore(), taste.Learned{}).Preview(context.Background(), 1, Input{}, mixes.MaxVariant+1); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("a variant that is too big: err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPreviewNeedsNoName(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(2)
|
||||
if _, err := newService(st, taste.Learned{}).Preview(context.Background(), 1, Input{Sort: SortTitle}, 0); err != nil {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ---- keeping and reading a smart playlist ----
|
||||
|
||||
func TestTracksRunsTheRulesOfASavedPlaylist(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(8)
|
||||
svc := newService(st, taste.Learned{})
|
||||
p, err := svc.Create(context.Background(), 1, Input{Name: "Short", Rules: Rules{MaxDurationSec: 240}, Sort: SortTitle, Limit: 3})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, tracks, err := svc.Tracks(context.Background(), 1, p.ID, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.ID != p.ID || !slices.Equal(ids(tracks), []int64{1, 2, 3}) {
|
||||
t.Errorf("playlist %d, songs %v", got.ID, ids(tracks))
|
||||
}
|
||||
if _, _, err := svc.Tracks(context.Background(), 1, 99, 0); !errors.Is(err, ErrNotFound) {
|
||||
t.Errorf("err = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateChangesWhatWasSentAndChecksIt(t *testing.T) {
|
||||
st := newStore()
|
||||
svc := newService(st, taste.Learned{})
|
||||
p, _ := svc.Create(context.Background(), 1, Input{Name: "A", Description: "d", Rules: Rules{NeverPlayed: true}, Sort: SortTitle, Limit: 20})
|
||||
|
||||
name, sort, limit := "B", SortMostPlayed, 30
|
||||
got, err := svc.Update(context.Background(), 1, p.ID, Patch{Name: &name, Sort: &sort, Limit: &limit, Rules: &Rules{MinPlays: 3}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Name != "B" || got.Description != "d" || got.Sort != SortMostPlayed || got.Limit != 30 || got.Rules.MinPlays != 3 || got.Rules.NeverPlayed {
|
||||
t.Errorf("playlist = %+v: the rules are replaced whole, the rest only when sent", got)
|
||||
}
|
||||
if !got.UpdatedAt.Equal(now) {
|
||||
t.Errorf("updated at %v", got.UpdatedAt)
|
||||
}
|
||||
|
||||
bad := ""
|
||||
if _, err := svc.Update(context.Background(), 1, p.ID, Patch{Name: &bad}); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("blank name: err = %v", err)
|
||||
}
|
||||
if _, err := svc.Update(context.Background(), 1, p.ID, Patch{Rules: &Rules{MinPlays: -1}}); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("bad rules: err = %v", err)
|
||||
}
|
||||
if _, err := svc.Update(context.Background(), 1, 99, Patch{Name: &name}); !errors.Is(err, ErrNotFound) {
|
||||
t.Errorf("unknown playlist: err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListLowersAPageThatIsTooBigAndRefusesNegatives(t *testing.T) {
|
||||
svc := newService(newStore(), taste.Learned{})
|
||||
for _, tc := range []struct{ in, want int }{{0, DefaultPageLimit}, {10, 10}, {MaxPageLimit + 1, MaxPageLimit}} {
|
||||
l, err := svc.List(context.Background(), 1, tc.in, 0)
|
||||
if err != nil || l.Limit != tc.want {
|
||||
t.Errorf("limit %d: got %d, %v; want %d", tc.in, l.Limit, err, tc.want)
|
||||
}
|
||||
}
|
||||
if _, err := svc.List(context.Background(), 1, -1, 0); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("a negative limit: %v", err)
|
||||
}
|
||||
if _, err := svc.List(context.Background(), 1, 10, -1); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("a negative offset: %v", err)
|
||||
}
|
||||
}
|
||||
241
internal/storage/smart.go
Normal file
241
internal/storage/smart.go
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/catalog"
|
||||
"codeberg.org/squidd/sonda/internal/mixes"
|
||||
"codeberg.org/squidd/sonda/internal/smart"
|
||||
)
|
||||
|
||||
// SmartRepo stores smart playlists and reads the songs their rules choose from. It implements smart.Store.
|
||||
type SmartRepo struct {
|
||||
db *bun.DB
|
||||
mix *MixRepo // reads the genres, the history and the songs, as the mixes do
|
||||
}
|
||||
|
||||
// NewSmartRepo builds a SmartRepo.
|
||||
func NewSmartRepo(db *bun.DB) *SmartRepo { return &SmartRepo{db: db, mix: NewMixRepo(db)} }
|
||||
|
||||
const smartColumns = "id, name, description, rules, sort, max_tracks, created_at, updated_at"
|
||||
|
||||
func scanSmart(rows interface{ Scan(...any) error }) (smart.SmartPlaylist, error) {
|
||||
var (
|
||||
p smart.SmartPlaylist
|
||||
rules, sort string
|
||||
created, updated int64
|
||||
)
|
||||
if err := rows.Scan(&p.ID, &p.Name, &p.Description, &rules, &sort, &p.Limit, &created, &updated); err != nil {
|
||||
return smart.SmartPlaylist{}, err
|
||||
}
|
||||
if err := json.Unmarshal([]byte(rules), &p.Rules); err != nil {
|
||||
return smart.SmartPlaylist{}, fmt.Errorf("read the rules of smart playlist %d: %w", p.ID, err)
|
||||
}
|
||||
p.Sort = smart.Sort(sort)
|
||||
p.CreatedAt, p.UpdatedAt = time.Unix(created, 0), time.Unix(updated, 0)
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (r *SmartRepo) get(ctx context.Context, db bun.IDB, userID, id int64) (smart.SmartPlaylist, error) {
|
||||
p, err := scanSmart(db.QueryRowContext(ctx, "SELECT "+smartColumns+" FROM smart_playlists WHERE id = ? AND user_id = ?", id, userID))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return smart.SmartPlaylist{}, smart.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return smart.SmartPlaylist{}, fmt.Errorf("read smart playlist: %w", err)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Create keeps a smart playlist.
|
||||
func (r *SmartRepo) Create(ctx context.Context, userID int64, p smart.SmartPlaylist, now time.Time) (smart.SmartPlaylist, error) {
|
||||
rules, err := json.Marshal(p.Rules)
|
||||
if err != nil {
|
||||
return smart.SmartPlaylist{}, fmt.Errorf("write the rules: %w", err)
|
||||
}
|
||||
var out smart.SmartPlaylist
|
||||
err = r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||
// The row of the user is the lock that makes the count below hold for two requests at once.
|
||||
res, err := tx.ExecContext(ctx, "UPDATE users SET is_admin = is_admin WHERE id = ?", userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("lock user: %w", err)
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return smart.ErrNotFound // the account is gone
|
||||
}
|
||||
var have int
|
||||
if err := tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM smart_playlists WHERE user_id = ?", userID).Scan(&have); err != nil {
|
||||
return fmt.Errorf("count smart playlists: %w", err)
|
||||
}
|
||||
if have >= smart.MaxPlaylists {
|
||||
return fmt.Errorf("%w: a user can have %d smart playlists", smart.ErrTooMany, smart.MaxPlaylists)
|
||||
}
|
||||
var id int64
|
||||
err = tx.QueryRowContext(ctx,
|
||||
`INSERT INTO smart_playlists (user_id, name, description, rules, sort, max_tracks, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?) RETURNING id`,
|
||||
userID, p.Name, p.Description, string(rules), string(p.Sort), p.Limit, now.Unix(), now.Unix()).Scan(&id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert smart playlist: %w", err)
|
||||
}
|
||||
out, err = r.get(ctx, tx, userID, id)
|
||||
return err
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
// List returns a page of the user's smart playlists, the one changed last first.
|
||||
func (r *SmartRepo) List(ctx context.Context, userID int64, limit, offset int) ([]smart.SmartPlaylist, int, error) {
|
||||
var total int
|
||||
if err := r.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM smart_playlists WHERE user_id = ?", userID).Scan(&total); err != nil {
|
||||
return nil, 0, fmt.Errorf("count smart playlists: %w", err)
|
||||
}
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
"SELECT "+smartColumns+" FROM smart_playlists WHERE user_id = ? ORDER BY updated_at DESC, id DESC LIMIT ? OFFSET ?",
|
||||
userID, limit, offset)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("list smart playlists: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []smart.SmartPlaylist{}
|
||||
for rows.Next() {
|
||||
p, err := scanSmart(rows)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, total, rows.Err()
|
||||
}
|
||||
|
||||
// Get returns a smart playlist of the user.
|
||||
func (r *SmartRepo) Get(ctx context.Context, userID, id int64) (smart.SmartPlaylist, error) {
|
||||
return r.get(ctx, r.db, userID, id)
|
||||
}
|
||||
|
||||
// Update replaces what a smart playlist says.
|
||||
func (r *SmartRepo) Update(ctx context.Context, userID, id int64, p smart.SmartPlaylist, now time.Time) (smart.SmartPlaylist, error) {
|
||||
rules, err := json.Marshal(p.Rules)
|
||||
if err != nil {
|
||||
return smart.SmartPlaylist{}, fmt.Errorf("write the rules: %w", err)
|
||||
}
|
||||
var out smart.SmartPlaylist
|
||||
err = r.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
|
||||
res, err := tx.ExecContext(ctx,
|
||||
"UPDATE smart_playlists SET name = ?, description = ?, rules = ?, sort = ?, max_tracks = ?, updated_at = ? WHERE id = ? AND user_id = ?",
|
||||
p.Name, p.Description, string(rules), string(p.Sort), p.Limit, now.Unix(), id, userID)
|
||||
if err := affectedOrNotFoundSmart(res, err); err != nil {
|
||||
return err
|
||||
}
|
||||
out, err = r.get(ctx, tx, userID, id)
|
||||
return err
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
func affectedOrNotFoundSmart(res sql.Result, err error) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("change smart playlist: %w", err)
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return smart.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes a smart playlist of the user.
|
||||
func (r *SmartRepo) Delete(ctx context.Context, userID, id int64) error {
|
||||
res, err := r.db.ExecContext(ctx, "DELETE FROM smart_playlists WHERE id = ? AND user_id = ?", id, userID)
|
||||
return affectedOrNotFoundSmart(res, err)
|
||||
}
|
||||
|
||||
// Genres returns every genre of the playable songs as the files spell it.
|
||||
func (r *SmartRepo) Genres(ctx context.Context) ([]mixes.Genre, error) { return r.mix.Genres(ctx) }
|
||||
|
||||
// History returns what the user did with each song they did anything with.
|
||||
func (r *SmartRepo) History(ctx context.Context, userID int64, minListen time.Duration) (map[int64]smart.History, error) {
|
||||
return r.mix.History(ctx, userID, minListen)
|
||||
}
|
||||
|
||||
// Tracks returns the playable songs among ids, in the order given.
|
||||
func (r *SmartRepo) Tracks(ctx context.Context, ids []int64) ([]catalog.Track, error) {
|
||||
return r.mix.Tracks(ctx, ids)
|
||||
}
|
||||
|
||||
// Candidates returns the playable songs that the filter keeps, with their main artist (the one with the
|
||||
// lowest id when there are several), the key of the title, the length and the date of the newest file.
|
||||
func (r *SmartRepo) Candidates(ctx context.Context, f smart.Filter) ([]smart.Candidate, error) {
|
||||
vis, _ := visible("t", 0)
|
||||
conds := []string{vis}
|
||||
var args []any
|
||||
add := func(cond string, a ...any) {
|
||||
conds = append(conds, cond)
|
||||
args = append(args, a...)
|
||||
}
|
||||
const genre = "EXISTS (SELECT 1 FROM track_genres g WHERE g.track_id = t.id AND g.genre IN (?))"
|
||||
const artist = "EXISTS (SELECT 1 FROM track_artists x WHERE x.track_id = t.id AND x.role = 'main' AND x.artist_id IN (?))"
|
||||
if len(f.Genres) > 0 {
|
||||
add(genre, bun.In(f.Genres))
|
||||
}
|
||||
if len(f.NotGenres) > 0 {
|
||||
add("NOT "+genre, bun.In(f.NotGenres))
|
||||
}
|
||||
if len(f.ArtistIDs) > 0 {
|
||||
add(artist, bun.In(f.ArtistIDs))
|
||||
}
|
||||
if len(f.NotArtistIDs) > 0 {
|
||||
add("NOT "+artist, bun.In(f.NotArtistIDs))
|
||||
}
|
||||
if len(f.AlbumIDs) > 0 {
|
||||
add("t.album_id IN (?)", bun.In(f.AlbumIDs))
|
||||
}
|
||||
if len(f.NotAlbumIDs) > 0 {
|
||||
add("(t.album_id IS NULL OR t.album_id NOT IN (?))", bun.In(f.NotAlbumIDs))
|
||||
}
|
||||
if len(f.Formats) > 0 || f.MinBitrateKbps > 0 {
|
||||
file := "EXISTS (SELECT 1 FROM audio_files f WHERE f.track_id = t.id AND f.missing_at IS NULL"
|
||||
var fargs []any
|
||||
if len(f.Formats) > 0 {
|
||||
file += " AND f.format IN (?)"
|
||||
fargs = append(fargs, bun.In(f.Formats))
|
||||
}
|
||||
if f.MinBitrateKbps > 0 {
|
||||
file += " AND f.bitrate >= ?"
|
||||
fargs = append(fargs, f.MinBitrateKbps)
|
||||
}
|
||||
add(file+")", fargs...)
|
||||
}
|
||||
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),
|
||||
t.title_key, COALESCE(t.duration_ms, 0),
|
||||
COALESCE((SELECT MAX(f.mtime_unix) FROM audio_files f WHERE f.track_id = t.id AND f.missing_at IS NULL), 0)
|
||||
FROM tracks t WHERE `+strings.Join(conds, " AND ")+` ORDER BY t.id`, args...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read the songs: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []smart.Candidate{}
|
||||
for rows.Next() {
|
||||
var (
|
||||
c smart.Candidate
|
||||
added int64
|
||||
)
|
||||
if err := rows.Scan(&c.TrackID, &c.ArtistID, &c.Title, &c.DurationMs, &added); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if added > 0 {
|
||||
c.AddedAt = time.Unix(added, 0)
|
||||
}
|
||||
out = append(out, c)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
333
internal/storage/smart_test.go
Normal file
333
internal/storage/smart_test.go
Normal file
|
|
@ -0,0 +1,333 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/smart"
|
||||
)
|
||||
|
||||
// smartEnv is two users and a library of songs that differ in what the rules ask about.
|
||||
type smartEnv struct {
|
||||
t *testing.T
|
||||
db *bun.DB
|
||||
repo *SmartRepo
|
||||
alice int64
|
||||
bob int64
|
||||
tracks map[string]int64
|
||||
artist map[string]int64
|
||||
album map[string]int64
|
||||
now time.Time
|
||||
}
|
||||
|
||||
type smartSong struct {
|
||||
title, artist, album, format string
|
||||
genres []string
|
||||
bitrate int
|
||||
mtime int64
|
||||
seconds int64
|
||||
}
|
||||
|
||||
func newSmartEnv(t *testing.T, db *bun.DB, songs []smartSong) *smartEnv {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
users := NewUserRepo(db)
|
||||
a, err := users.CreateUser(ctx, "alice@example.org", "hash", false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b, err := users.CreateUser(ctx, "bob@example.org", "hash", false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lib := NewLibraryRepo(db)
|
||||
l, err := lib.CreateLibrary(ctx, "m", "/lib/m")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e := &smartEnv{t: t, db: db, repo: NewSmartRepo(db), alice: a.ID, bob: b.ID, now: time.Unix(1_700_000_000, 0),
|
||||
tracks: map[string]int64{}, artist: map[string]int64{}, album: map[string]int64{}}
|
||||
for i, s := range songs {
|
||||
f := pf("x/"+s.title+"."+s.format, s.title, s.album, s.artist, i+1, 1, s.format, s.artist)
|
||||
f.Tags.Genres = s.genres
|
||||
f.BitRate = s.bitrate
|
||||
f.MTimeUnix = s.mtime
|
||||
if s.seconds > 0 {
|
||||
f.DurationMs = s.seconds * 1000
|
||||
}
|
||||
if _, err := lib.SaveFile(ctx, l.ID, f, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var id int64
|
||||
if err := db.QueryRow("SELECT id FROM tracks WHERE title = ?", s.title).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
e.tracks[s.title] = id
|
||||
}
|
||||
for _, q := range []struct {
|
||||
sql string
|
||||
to map[string]int64
|
||||
}{{"SELECT id, name FROM artists", e.artist}, {"SELECT id, title FROM albums", e.album}} {
|
||||
rows, err := db.Query(q.sql)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
var name string
|
||||
if err := rows.Scan(&id, &name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q.to[name] = id
|
||||
}
|
||||
rows.Close()
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *smartEnv) tick() time.Time { e.now = e.now.Add(time.Minute); return e.now }
|
||||
|
||||
func smartBackends(t *testing.T, songs []smartSong, fn func(t *testing.T, e *smartEnv)) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { fn(t, newSmartEnv(t, db, songs)) })
|
||||
}
|
||||
|
||||
var smartSongs = []smartSong{
|
||||
{"a", "Queen", "News", "flac", []string{"rock", "classic rock"}, 1000, 1_000, 120},
|
||||
{"b", "Queen", "News", "mpeg", []string{"rock"}, 320, 2_000, 200},
|
||||
{"c", "Kiss", "Alive", "mpeg", []string{"rock", "hard rock"}, 192, 3_000, 240},
|
||||
{"d", "Slayer", "Reign", "flac", []string{"metal"}, 800, 4_000, 180},
|
||||
{"e", "Miles", "Blue", "flac", []string{"jazz+funk"}, 1100, 5_000, 300},
|
||||
}
|
||||
|
||||
func names(e *smartEnv, cands []smart.Candidate) []string {
|
||||
byID := map[int64]string{}
|
||||
for k, v := range e.tracks {
|
||||
byID[v] = k
|
||||
}
|
||||
out := make([]string, len(cands))
|
||||
for i, c := range cands {
|
||||
out[i] = byID[c.TrackID]
|
||||
}
|
||||
slices.Sort(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func TestSmartRepoCandidatesNarrowByEveryList(t *testing.T) {
|
||||
smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) {
|
||||
ctx := context.Background()
|
||||
for name, tc := range map[string]struct {
|
||||
f smart.Filter
|
||||
want []string
|
||||
}{
|
||||
"no filter: every playable song": {smart.Filter{}, []string{"a", "b", "c", "d", "e"}},
|
||||
"a genre": {smart.Filter{Genres: []string{"rock"}}, []string{"a", "b", "c"}},
|
||||
"one of two genres": {smart.Filter{Genres: []string{"metal", "jazz+funk"}}, []string{"d", "e"}},
|
||||
"not a genre": {smart.Filter{NotGenres: []string{"hard rock", "metal"}}, []string{"a", "b", "e"}},
|
||||
"a genre and not another": {smart.Filter{Genres: []string{"rock"}, NotGenres: []string{"hard rock"}}, []string{"a", "b"}},
|
||||
"an artist": {smart.Filter{ArtistIDs: []int64{e.artist["Queen"]}}, []string{"a", "b"}},
|
||||
"not an artist": {smart.Filter{NotArtistIDs: []int64{e.artist["Queen"], e.artist["Kiss"]}}, []string{"d", "e"}},
|
||||
"an album": {smart.Filter{AlbumIDs: []int64{e.album["Alive"]}}, []string{"c"}},
|
||||
"not an album": {smart.Filter{NotAlbumIDs: []int64{e.album["News"]}}, []string{"c", "d", "e"}},
|
||||
"a format": {smart.Filter{Formats: []string{"mpeg"}}, []string{"b", "c"}},
|
||||
"two formats": {smart.Filter{Formats: []string{"mpeg", "flac"}}, []string{"a", "b", "c", "d", "e"}},
|
||||
"a bitrate": {smart.Filter{MinBitrateKbps: 900}, []string{"a", "e"}},
|
||||
"a format and a bitrate": {smart.Filter{Formats: []string{"flac"}, MinBitrateKbps: 900}, []string{"a", "e"}},
|
||||
"a format that nothing has": {smart.Filter{Formats: []string{"ogg"}}, []string{}},
|
||||
"two lists must both hold": {smart.Filter{Genres: []string{"rock"}, Formats: []string{"flac"}}, []string{"a"}},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, err := e.repo.Candidates(ctx, tc.f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g := names(e, got); !slices.Equal(g, tc.want) {
|
||||
t.Errorf("got %v, want %v", g, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSmartRepoAFormatAndABitrateAreAboutTheSameFile(t *testing.T) {
|
||||
songs := []smartSong{{"a", "Queen", "News", "flac", nil, 500, 1, 60}}
|
||||
smartBackends(t, songs, func(t *testing.T, e *smartEnv) {
|
||||
// A second file of the same song: an mp3 with a high bitrate. flac AND 900 holds for no file.
|
||||
if _, err := e.db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix, bitrate)
|
||||
SELECT track_id, library_id, 'x/a.mp3', 'mpeg', 1, 9000, 1000 FROM audio_files LIMIT 1`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := e.repo.Candidates(context.Background(), smart.Filter{Formats: []string{"flac"}, MinBitrateKbps: 900})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("got %v: the format and the bitrate belong to one file", names(e, got))
|
||||
}
|
||||
got, _ = e.repo.Candidates(context.Background(), smart.Filter{Formats: []string{"mpeg"}, MinBitrateKbps: 900})
|
||||
if len(got) != 1 {
|
||||
t.Errorf("the mp3 file has both: got %v", names(e, got))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSmartRepoCandidatesGiveWhatTheRulesNeedAndLeaveOutSongsWithNoFile(t *testing.T) {
|
||||
smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) {
|
||||
ctx := context.Background()
|
||||
if _, err := e.db.Exec("UPDATE audio_files SET missing_at = 1 WHERE track_id = ?", e.tracks["e"]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := e.repo.Candidates(ctx, smart.Filter{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g := names(e, got); !slices.Equal(g, []string{"a", "b", "c", "d"}) {
|
||||
t.Errorf("got %v: a song whose files are gone is not offered", g)
|
||||
}
|
||||
for _, c := range got {
|
||||
if c.TrackID != e.tracks["c"] {
|
||||
continue
|
||||
}
|
||||
if c.ArtistID != e.artist["Kiss"] || c.DurationMs != 240_000 || c.Title != "c" || !c.AddedAt.Equal(time.Unix(3_000, 0)) {
|
||||
t.Errorf("candidate = %+v", c)
|
||||
}
|
||||
}
|
||||
// The date is the newest file that is not missing.
|
||||
if _, err := e.db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix)
|
||||
SELECT track_id, library_id, 'x/c2.mpeg', 'mpeg', 1, 7000 FROM audio_files WHERE track_id = ? LIMIT 1`, e.tracks["c"]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := e.db.Exec(`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix, missing_at)
|
||||
SELECT track_id, library_id, 'x/c3.mpeg', 'mpeg', 1, 9000, 5 FROM audio_files WHERE track_id = ? LIMIT 1`, e.tracks["c"]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, _ = e.repo.Candidates(ctx, smart.Filter{ArtistIDs: []int64{e.artist["Kiss"]}})
|
||||
if len(got) != 1 || !got[0].AddedAt.Equal(time.Unix(7_000, 0)) {
|
||||
t.Errorf("got %+v, want one song added at 7000", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func sample() smart.SmartPlaylist {
|
||||
return smart.SmartPlaylist{
|
||||
Name: "Fresh", Description: "new things", Sort: smart.SortRecentlyAdded, Limit: 30,
|
||||
Rules: smart.Rules{Genres: []string{"rock"}, ArtistIDs: []int64{3, 4}, Formats: []string{"flac"}, MinBitrateKbps: 900, AddedWithinDays: 30, NeverPlayed: true, MinDurationSec: 60},
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmartRepoKeepsAPlaylistAndItsRulesPerUser(t *testing.T) {
|
||||
smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) {
|
||||
ctx := context.Background()
|
||||
p, err := e.repo.Create(ctx, e.alice, sample(), e.tick())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := sample()
|
||||
want.ID, want.CreatedAt, want.UpdatedAt = p.ID, e.now, e.now
|
||||
if p.ID == 0 || p.Name != want.Name || p.Description != want.Description || p.Sort != want.Sort || p.Limit != want.Limit ||
|
||||
!slices.Equal(p.Rules.Genres, want.Rules.Genres) || !slices.Equal(p.Rules.ArtistIDs, want.Rules.ArtistIDs) ||
|
||||
!slices.Equal(p.Rules.Formats, want.Rules.Formats) || p.Rules.MinBitrateKbps != 900 || p.Rules.AddedWithinDays != 30 ||
|
||||
!p.Rules.NeverPlayed || p.Rules.MinDurationSec != 60 || !p.CreatedAt.Equal(e.now) || !p.UpdatedAt.Equal(e.now) {
|
||||
t.Errorf("created = %+v, want %+v", p, want)
|
||||
}
|
||||
got, err := e.repo.Get(ctx, e.alice, p.ID)
|
||||
if err != nil || got.Name != "Fresh" || !slices.Equal(got.Rules.Genres, []string{"rock"}) {
|
||||
t.Errorf("get = %+v, %v", got, err)
|
||||
}
|
||||
|
||||
// Somebody else's is as if it did not exist, whatever is done to it.
|
||||
if _, err := e.repo.Get(ctx, e.bob, p.ID); !errors.Is(err, smart.ErrNotFound) {
|
||||
t.Errorf("get by another user: %v", err)
|
||||
}
|
||||
if _, err := e.repo.Update(ctx, e.bob, p.ID, sample(), e.tick()); !errors.Is(err, smart.ErrNotFound) {
|
||||
t.Errorf("update by another user: %v", err)
|
||||
}
|
||||
if err := e.repo.Delete(ctx, e.bob, p.ID); !errors.Is(err, smart.ErrNotFound) {
|
||||
t.Errorf("delete by another user: %v", err)
|
||||
}
|
||||
if got, _ := e.repo.Get(ctx, e.alice, p.ID); got.Name != "Fresh" {
|
||||
t.Errorf("the playlist changed: %+v", got)
|
||||
}
|
||||
if _, err := e.repo.Get(ctx, e.alice, p.ID+100); !errors.Is(err, smart.ErrNotFound) {
|
||||
t.Errorf("get of nothing: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSmartRepoListsTheOneChangedLastFirstInPages(t *testing.T) {
|
||||
smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) {
|
||||
ctx := context.Background()
|
||||
var ids []int64
|
||||
for _, n := range []string{"one", "two", "three"} {
|
||||
p := sample()
|
||||
p.Name = n
|
||||
made, err := e.repo.Create(ctx, e.alice, p, e.tick())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ids = append(ids, made.ID)
|
||||
}
|
||||
other := sample()
|
||||
other.Name = "bob's"
|
||||
if _, err := e.repo.Create(ctx, e.bob, other, e.tick()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Changing "one" brings it to the top.
|
||||
upd := sample()
|
||||
upd.Name = "one, again"
|
||||
if _, err := e.repo.Update(ctx, e.alice, ids[0], upd, e.tick()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
list, total, err := e.repo.List(ctx, e.alice, 2, 0)
|
||||
if err != nil || total != 3 || len(list) != 2 || list[0].Name != "one, again" || list[1].Name != "three" {
|
||||
t.Fatalf("page 1 = %+v, total %d, %v", list, total, err)
|
||||
}
|
||||
list, _, _ = e.repo.List(ctx, e.alice, 2, 2)
|
||||
if len(list) != 1 || list[0].Name != "two" {
|
||||
t.Errorf("page 2 = %+v", list)
|
||||
}
|
||||
if err := e.repo.Delete(ctx, e.alice, ids[1]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, total, _ := e.repo.List(ctx, e.alice, 10, 0); total != 2 {
|
||||
t.Errorf("total after a delete = %d", total)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSmartRepoStopsAtTheMostASmartPlaylistsAUserCanHave(t *testing.T) {
|
||||
smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) {
|
||||
ctx := context.Background()
|
||||
for i := 0; i < smart.MaxPlaylists; i++ {
|
||||
if _, err := e.repo.Create(ctx, e.alice, sample(), e.tick()); err != nil {
|
||||
t.Fatalf("playlist %d: %v", i+1, err)
|
||||
}
|
||||
}
|
||||
if _, err := e.repo.Create(ctx, e.alice, sample(), e.tick()); !errors.Is(err, smart.ErrTooMany) {
|
||||
t.Errorf("err = %v, want ErrTooMany", err)
|
||||
}
|
||||
if _, err := e.repo.Create(ctx, e.bob, sample(), e.tick()); err != nil {
|
||||
t.Errorf("another user is not held back: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSmartPlaylistsGoWhenTheirOwnerGoes(t *testing.T) {
|
||||
smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) {
|
||||
ctx := context.Background()
|
||||
if _, err := e.repo.Create(ctx, e.bob, sample(), e.tick()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := e.db.Exec("DELETE FROM users WHERE id = ?", e.bob); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var n int
|
||||
if err := e.db.QueryRow("SELECT COUNT(*) FROM smart_playlists").Scan(&n); err != nil || n != 0 {
|
||||
t.Errorf("%d smart playlists left, %v", n, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
|
|||
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if applied != 19 {
|
||||
t.Errorf("applied migrations = %d, want 19", applied)
|
||||
if applied != 20 {
|
||||
t.Errorf("applied migrations = %d, want 20", applied)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
16
migrations/postgres/0020_smart_playlists.sql
Normal file
16
migrations/postgres/0020_smart_playlists.sql
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
-- Smart playlists: playlists that are a set of rules, worked out again every time they are opened.
|
||||
-- PostgreSQL flavour of sqlite/0020_smart_playlists.sql.
|
||||
|
||||
CREATE TABLE smart_playlists (
|
||||
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
rules TEXT NOT NULL,
|
||||
sort TEXT NOT NULL,
|
||||
max_tracks INTEGER NOT NULL,
|
||||
created_at BIGINT NOT NULL, -- unix seconds
|
||||
updated_at BIGINT NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_smart_playlists_user ON smart_playlists(user_id, updated_at);
|
||||
17
migrations/sqlite/0020_smart_playlists.sql
Normal file
17
migrations/sqlite/0020_smart_playlists.sql
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
-- Smart playlists: playlists that are a set of rules, worked out again every time they are opened.
|
||||
-- Only the rules are kept (as JSON, read and written by the code, never searched), with the order
|
||||
-- and the most songs they give.
|
||||
|
||||
CREATE TABLE smart_playlists (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
rules TEXT NOT NULL,
|
||||
sort TEXT NOT NULL,
|
||||
max_tracks INTEGER NOT NULL,
|
||||
created_at INTEGER NOT NULL, -- unix seconds
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_smart_playlists_user ON smart_playlists(user_id, updated_at);
|
||||
Loading…
Reference in a new issue