diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index 6d91ef7..4da5551 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -303,6 +303,8 @@ func run() error { // Other apps (OpenSubsonic) log in with the password of a device, never with the account password. deviceSvc := devices.NewService(storage.NewDevicesRepo(db), []byte(cfg.Auth.JWTSecret)) catalogSvc := catalog.NewService(storage.NewCatalogRepo(db)) + streamSvc := streaming.NewService(libraryRepo) + subsonicRepo := storage.NewSubsonicRepo(db, cfg.Listening.MinListen()) router := httpapi.NewRouter(httpapi.Deps{ Logger: logger, @@ -315,7 +317,8 @@ func run() error { Devices: deviceSvc, OpenSubsonic: &opensubsonic.Deps{ Login: deviceSvc, Catalog: catalogSvc, Libraries: libraries, - Extras: storage.NewSubsonicRepo(db), ServerVersion: version, + Extras: subsonicRepo, Lists: subsonicRepo, Streams: streamSvc, Covers: pictures, ArtistImages: pictures, + ServerVersion: version, }, Scan: scanControl{ctx: ctx, m: scans}, Libraries: libraries, @@ -344,7 +347,7 @@ func run() error { ArtistImages: pictures, About: descriptions, Settings: userSettings, - Streams: streaming.NewService(libraryRepo), + Streams: streamSvc, Federation: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()), PeerCatalog: catalog.NewService(storage.NewSharedCatalogRepo(db)), PeerStreams: streaming.NewService(libraryRepo.Shared()), diff --git a/internal/api/http/api_test.go b/internal/api/http/api_test.go index e0dc35b..f225c21 100644 --- a/internal/api/http/api_test.go +++ b/internal/api/http/api_test.go @@ -142,9 +142,12 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun deps.ArtistImagesJob = jobs.Runner{Ctx: context.Background(), Job: images.NewArtistImagesJob(imageSvc, imageRepo)} mutate(&deps, db) if deps.OpenSubsonic == nil { + subsonic := storage.NewSubsonicRepo(db, 20*time.Second) deps.OpenSubsonic = &opensubsonic.Deps{ Login: deps.Devices.(opensubsonic.Login), Catalog: deps.Catalog.(opensubsonic.Catalog), - Libraries: deps.Libraries.(opensubsonic.Libraries), Extras: storage.NewSubsonicRepo(db), ServerVersion: "test", + Libraries: deps.Libraries.(opensubsonic.Libraries), ServerVersion: "test", + Extras: subsonic, Lists: subsonic, Streams: deps.Streams.(opensubsonic.Streams), + Covers: deps.Images.(opensubsonic.Covers), ArtistImages: deps.ArtistImages.(opensubsonic.ArtistImages), } } if deps.About == nil { diff --git a/internal/api/http/catalog_test.go b/internal/api/http/catalog_test.go index aa86711..2ec4d9a 100644 --- a/internal/api/http/catalog_test.go +++ b/internal/api/http/catalog_test.go @@ -3,6 +3,7 @@ package httpapi import ( "context" "net/http" + "reflect" "sort" "testing" @@ -198,7 +199,7 @@ func TestCatalogQueryParametersReachTheService(t *testing.T) { do(t, h, "GET", "/api/v1/tracks?q=black+sabbath&limit=25&offset=50&library_id=2&artist_id=3&album_id=4", tok, nil) want := catalog.Filter{Query: "black sabbath", Limit: 25, Offset: 50, LibraryID: 2, ArtistID: 3, AlbumID: 4} - if fc.last != want { + if !reflect.DeepEqual(fc.last, want) { t.Errorf("filter = %+v, want %+v", fc.last, want) } diff --git a/internal/api/http/covers_test.go b/internal/api/http/covers_test.go index 7b863b6..27ce185 100644 --- a/internal/api/http/covers_test.go +++ b/internal/api/http/covers_test.go @@ -9,6 +9,7 @@ import ( "image/jpeg" "net/http" "net/http/httptest" + "net/url" "os" "path/filepath" "sync/atomic" @@ -19,6 +20,7 @@ import ( "git.kmalloc.org/squid/sonda/internal/images" "git.kmalloc.org/squid/sonda/internal/library" "git.kmalloc.org/squid/sonda/internal/storage" + "git.kmalloc.org/squid/sonda/internal/streaming" ) type fakeArchive struct { @@ -80,6 +82,7 @@ func newCoversEnv(t *testing.T) *coversEnv { svc.SetAlbumRemote(e.archive) e.svc = svc d.Images = svc + d.Streams = streaming.NewService(storage.NewLibraryRepo(db)) }) e.token = registerAndLogin(t, e.h) @@ -301,3 +304,60 @@ func TestACoverOfFilesThatCannotBeReadIsServiceUnavailableNotNoCover(t *testing. t.Errorf("the archive was asked %d times about files that could not be read", n) } } + +func TestAnAppGetsCoversAndSongsOverRest(t *testing.T) { + e := newCoversEnv(t) + secret := do(t, e.h, "POST", "/api/v1/me/devices", e.token, map[string]any{"name": "Phone"}).Body["secret"].(string) + get := func(path string, q url.Values) *httptest.ResponseRecorder { + q.Set("apiKey", secret) + rec := httptest.NewRecorder() + e.h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil)) + return rec + } + + // The cover, small and large, is the one the main API makes. + for size, max := range map[string]int{"150": 300, "1200": 1000} { + rec := get("getCoverArt", url.Values{"id": {"al-" + itoa(e.withArt)}, "size": {size}}) + if rec.Code != 200 || rec.Header().Get("Content-Type") != "image/jpeg" { + t.Fatalf("size %s: %d %s %s", size, rec.Code, rec.Header().Get("Content-Type"), rec.Body.String()) + } + img, err := jpeg.Decode(rec.Body) + if err != nil || img.Bounds().Dx() > max || img.Bounds().Dx() < 100 { + t.Fatalf("size %s: %v %v", size, err, img.Bounds()) + } + } + if rec := get("getCoverArt", url.Values{"id": {"al-" + itoa(e.without)}, "f": {"json"}}); errorFrom(t, rec) != 70 { + t.Fatalf("an album with no picture: %s", rec.Body.String()) + } + + // The album, found by search, has a song that plays: the file as it is, with Range. + res := get("search3", url.Values{"query": {"With Art"}, "f": {"json"}}) + var found struct { + R struct { + S struct { + Song []struct{ ID, Suffix, CoverArt string } `json:"song"` + } `json:"searchResult3"` + } `json:"subsonic-response"` + } + if err := json.Unmarshal(res.Body.Bytes(), &found); err != nil || len(found.R.S.Song) != 1 { + t.Fatalf("search: %v %s", err, res.Body.String()) + } + song := found.R.S.Song[0] + if song.Suffix != "mp3" || song.CoverArt != "al-"+itoa(e.withArt) { + t.Fatalf("song %+v", song) + } + if rec := get("stream", url.Values{"id": {song.ID}}); rec.Code != 200 || rec.Body.String() != "not audio" || rec.Header().Get("Content-Type") != "audio/mpeg" { + t.Fatalf("stream: %d %q %q", rec.Code, rec.Body.String(), rec.Header().Get("Content-Type")) + } +} + +func errorFrom(t *testing.T, rec *httptest.ResponseRecorder) int { + t.Helper() + var out struct { + R map[string]any `json:"subsonic-response"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("%v: %s", err, rec.Body.String()) + } + return errorCode(out.R) +} diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 013c7d5..c835da3 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -36,11 +36,12 @@ const ( // Filter selects and pages a list. Zero IDs mean "any". Each list uses the // fields that make sense for it and ignores the others. type Filter struct { - Query string // case-insensitive substring of the name or title (accents are kept) - LibraryID int64 // only items with a playable file in this library - ArtistID int64 // albums: album artist; tracks: credited artist - AlbumID int64 // tracks only - Sort string // albums only: "" or SortTitle (by title), SortRecent (the newest files first) + Query string // case-insensitive substring of the name or title (accents are kept) + LibraryID int64 // only items with a playable file in this library + ArtistID int64 // albums: album artist; tracks: credited artist + AlbumID int64 // tracks only + Sort string // albums only: "" or SortTitle (by title), SortRecent (the newest files first) + IDs []int64 // albums and tracks: only these ids (at most MaxLimit); nil is no limit Limit int Offset int } @@ -125,6 +126,9 @@ func normalize(f Filter) (Filter, error) { if f.Limit < 0 || f.Offset < 0 { return f, fmt.Errorf("%w: limit and offset cannot be negative", ErrInvalidQuery) } + if len(f.IDs) > MaxLimit { + return f, fmt.Errorf("%w: at most %d ids", ErrInvalidQuery, MaxLimit) + } if f.LibraryID < 0 || f.ArtistID < 0 || f.AlbumID < 0 { return f, fmt.Errorf("%w: ids cannot be negative", ErrInvalidQuery) } diff --git a/internal/opensubsonic/handler.go b/internal/opensubsonic/handler.go index fc8e4ed..09c6259 100644 --- a/internal/opensubsonic/handler.go +++ b/internal/opensubsonic/handler.go @@ -49,6 +49,10 @@ type Deps struct { Catalog Catalog Libraries Libraries Extras Extras + Lists Lists + Streams Streams + Covers Covers + ArtistImages ArtistImages ServerVersion string } @@ -71,18 +75,29 @@ func (k *call) param(name string) string { return k.c.Request.Form.Get(name) } func Register(r gin.IRoutes, d Deps) { h := &handler{Deps: d} h.calls = map[string]func(*call) (*Response, *Error){ - "ping": func(*call) (*Response, *Error) { return &Response{}, nil }, - "getLicense": func(*call) (*Response, *Error) { return &Response{License: &License{Valid: true}}, nil }, - "tokenInfo": h.tokenInfo, - "getMusicFolders": h.musicFolders, - "getArtists": h.artists, - "getArtist": h.artist, - "getAlbum": h.album, - "getSong": h.song, - "getGenres": h.genres, - "getOpenSubsonicExtensions": nil, // answered before the login: see serve + "ping": func(*call) (*Response, *Error) { return &Response{}, nil }, + "getLicense": func(*call) (*Response, *Error) { return &Response{License: &License{Valid: true}}, nil }, + "tokenInfo": h.tokenInfo, + "getMusicFolders": h.musicFolders, + "getArtists": h.artists, + "getArtist": h.artist, + "getAlbum": h.album, + "getSong": h.song, + "getGenres": h.genres, + "getAlbumList2": h.albumList2, + "getRandomSongs": h.randomSongs, + "getSongsByGenre": h.songsByGenre, + "search3": h.search3, + "getStarred2": h.starred2, + "star": nothing, + "unstar": nothing, + "setRating": nothing, + "stream": h.stream, + "download": h.download, + "getCoverArt": h.coverArt, } r.GET("/rest/:method", h.serve) + r.HEAD("/rest/:method", h.serve) r.POST("/rest/:method", h.serve) } @@ -102,6 +117,8 @@ func (h *handler) serve(c *gin.Context) { resp, fail := h.dispatch(c, method) if fail != nil { resp = &Response{Error: fail} + } else if resp == served { + return // the call wrote its own answer } h.write(c, asJSON, resp) } @@ -111,7 +128,7 @@ func (h *handler) dispatch(c *gin.Context, method string) (*Response, *Error) { return &Response{OpenSubsonicExtensions: extensions}, nil } fn, known := h.calls[method] - if !known || fn == nil { + if !known { return nil, &Error{Code: ErrGeneric, Message: "unknown method: " + method} } k := &call{c: c, ctx: c.Request.Context()} diff --git a/internal/opensubsonic/handler_test.go b/internal/opensubsonic/handler_test.go index 66fff3d..f5f0f67 100644 --- a/internal/opensubsonic/handler_test.go +++ b/internal/opensubsonic/handler_test.go @@ -4,9 +4,11 @@ import ( "context" "encoding/json" "encoding/xml" + "fmt" "net/http" "net/http/httptest" "net/url" + "os" "strings" "testing" "time" @@ -15,7 +17,9 @@ import ( "git.kmalloc.org/squid/sonda/internal/catalog" "git.kmalloc.org/squid/sonda/internal/devices" + "git.kmalloc.org/squid/sonda/internal/images" "git.kmalloc.org/squid/sonda/internal/library" + "git.kmalloc.org/squid/sonda/internal/streaming" ) const secret = "0123456789abcdef0123456789abcdef0123456789abcdef" @@ -81,14 +85,36 @@ func (c *fakeCatalog) Albums(_ context.Context, f catalog.Filter) (catalog.Page[ if f.ArtistID == 0 || f.ArtistID == 1 { all = []catalog.Album{okComputer} } + if f.IDs != nil { + var keep []catalog.Album + for _, a := range all { + for _, id := range f.IDs { + if a.ID == id { + keep = append(keep, a) + } + } + } + all = keep + } return catalog.Page[catalog.Album]{Items: page(all, f), Total: len(all), Limit: f.Limit, Offset: f.Offset}, nil } func (c *fakeCatalog) Tracks(_ context.Context, f catalog.Filter) (catalog.Page[catalog.Track], error) { var all []catalog.Track - if f.AlbumID == 7 { + if f.AlbumID == 7 || f.IDs != nil { all = []catalog.Track{airbag, karma} } + if f.IDs != nil { + var keep []catalog.Track + for _, t := range all { + for _, id := range f.IDs { + if t.ID == id { + keep = append(keep, t) + } + } + } + all = keep + } return catalog.Page[catalog.Track]{Items: page(all, f), Total: len(all), Limit: f.Limit, Offset: f.Offset}, nil } @@ -485,3 +511,291 @@ func TestAccentsAreTakenOffForTheLetterOfAnArtist(t *testing.T) { } } } + +// ---- media, lists, search ---- + +type fakeLists struct { + albumQ []AlbumQuery + songQ []SongQuery +} + +func (f *fakeLists) AlbumIDs(_ context.Context, q AlbumQuery) ([]int64, error) { + f.albumQ = append(f.albumQ, q) + if q.Offset > 0 { + return []int64{}, nil + } + return []int64{7}, nil +} + +func (f *fakeLists) SongIDs(_ context.Context, q SongQuery) ([]int64, error) { + f.songQ = append(f.songQ, q) + return []int64{71, 70}, nil // not in the order of the catalogue +} + +type fakeStreams struct{ opened []int64 } + +func (f *fakeStreams) Open(_ context.Context, id int64) (*streaming.Stream, error) { + f.opened = append(f.opened, id) + switch id { + case 70: + file, err := os.Open(songFile) + if err != nil { + return nil, err + } + return &streaming.Stream{File: file, Name: "airbag.flac", ModTime: time.Unix(1_700_000_000, 0), ContentType: "audio/flac"}, nil + case 71: + return nil, streaming.ErrFileMissing + } + return nil, streaming.ErrNotFound +} + +type fakeCovers struct{ sizes []images.Size } + +func (f *fakeCovers) AlbumCover(_ context.Context, id int64, size images.Size) (*images.Cover, error) { + f.sizes = append(f.sizes, size) + if id != 7 { + return nil, images.ErrNoCover + } + file, err := os.Open(coverFile) + if err != nil { + return nil, err + } + return &images.Cover{File: file, ModTime: time.Unix(1_700_000_000, 0), ETag: "abc"}, nil +} + +func (f *fakeCovers) ArtistImage(_ context.Context, id int64, size images.Size) (*images.Cover, error) { + f.sizes = append(f.sizes, size) + if id != 1 { + return nil, images.ErrNoImage + } + file, err := os.Open(coverFile) + if err != nil { + return nil, err + } + return &images.Cover{File: file, ModTime: time.Unix(1_700_000_000, 0), ETag: "def"}, nil +} + +var songFile, coverFile string + +func TestMain(m *testing.M) { + dir, err := os.MkdirTemp("", "opensubsonic") + if err != nil { + panic(err) + } + songFile, coverFile = dir+"/song.flac", dir+"/cover.jpg" + _ = os.WriteFile(songFile, []byte("0123456789"), 0o600) + _ = os.WriteFile(coverFile, []byte("JPEGDATA"), 0o600) + code := m.Run() + _ = os.RemoveAll(dir) + os.Exit(code) +} + +type media struct { + h http.Handler + cat *fakeCatalog + lists *fakeLists + streams *fakeStreams + covers *fakeCovers +} + +func newMediaServer() media { + gin.SetMode(gin.TestMode) + m := media{cat: &fakeCatalog{}, lists: &fakeLists{}, streams: &fakeStreams{}, covers: &fakeCovers{}} + r := gin.New() + Register(r, Deps{ + Login: fakeLogin{}, Catalog: m.cat, Libraries: fakeLibraries{}, Extras: fakeExtras{}, ServerVersion: "9.9", + Lists: m.lists, Streams: m.streams, Covers: m.covers, ArtistImages: m.covers, + }) + m.h = r + return m +} + +func TestASongIsStreamedWithRangeAndWithoutRevealingTheToken(t *testing.T) { + m := newMediaServer() + rec := get(t, m.h, "GET", "/rest/stream", ok("id", "tr-70")) + if rec.Code != 200 || rec.Body.String() != "0123456789" || rec.Header().Get("Content-Type") != "audio/flac" { + t.Fatalf("%d %q %q", rec.Code, rec.Body.String(), rec.Header().Get("Content-Type")) + } + if rec.Header().Get("Referrer-Policy") != "no-referrer" || rec.Header().Get("X-Content-Type-Options") != "nosniff" || rec.Header().Get("Accept-Ranges") != "bytes" { + t.Fatalf("headers %v", rec.Header()) + } + + req := httptest.NewRequest("GET", "/rest/stream.view?"+ok("id", "tr-70").Encode(), nil) + req.Header.Set("Range", "bytes=2-5") + part := httptest.NewRecorder() + m.h.ServeHTTP(part, req) + if part.Code != http.StatusPartialContent || part.Body.String() != "2345" { + t.Fatalf("range: %d %q", part.Code, part.Body.String()) + } + + head := httptest.NewRecorder() + m.h.ServeHTTP(head, httptest.NewRequest("HEAD", "/rest/stream?"+ok("id", "tr-70").Encode(), nil)) + if head.Code != 200 || head.Body.Len() != 0 || head.Header().Get("Content-Length") != "10" { + t.Fatalf("head: %d %q %v", head.Code, head.Body.String(), head.Header()) + } +} + +func TestDownloadSendsTheFileAsAnAttachment(t *testing.T) { + m := newMediaServer() + rec := get(t, m.h, "GET", "/rest/download", ok("id", "tr-70")) + if rec.Code != 200 || rec.Body.String() != "0123456789" { + t.Fatalf("%d %q", rec.Code, rec.Body.String()) + } + if cd := rec.Header().Get("Content-Disposition"); !strings.HasPrefix(cd, "attachment") || !strings.Contains(cd, "airbag.flac") { + t.Fatalf("disposition %q", cd) + } +} + +func TestAStreamThatCannotBeServedIsAnErrorInTheFormatAskedFor(t *testing.T) { + m := newMediaServer() + failed(t, get(t, m.h, "GET", "/rest/stream", ok("id", "tr-99")), ErrNotFound) + failed(t, get(t, m.h, "GET", "/rest/stream", ok("id", "tr-71")), ErrNotFound) // the file is gone + failed(t, get(t, m.h, "GET", "/rest/stream", ok("id", "al-7")), ErrNotFound) + failed(t, get(t, m.h, "GET", "/rest/stream", ok()), ErrMissingParam) + failed(t, get(t, m.h, "GET", "/rest/download", ok("id", "tr-99")), ErrNotFound) + // No login, no music. + failed(t, get(t, m.h, "GET", "/rest/stream", url.Values{"id": {"tr-70"}, "f": {"json"}}), ErrMissingParam) + failed(t, get(t, m.h, "GET", "/rest/stream", url.Values{"id": {"tr-70"}, "apiKey": {"nope"}, "f": {"json"}}), ErrBadAPIKey) + if len(m.streams.opened) != 3 { + t.Fatalf("files opened without a login: %v", m.streams.opened) + } +} + +func TestACoverIsAnAlbumAnArtistOrTheAlbumOfASong(t *testing.T) { + m := newMediaServer() + for _, id := range []string{"al-7", "tr-70", "ar-1"} { + rec := get(t, m.h, "GET", "/rest/getCoverArt", ok("id", id)) + if rec.Code != 200 || rec.Body.String() != "JPEGDATA" || rec.Header().Get("Content-Type") != "image/jpeg" { + t.Fatalf("%s: %d %q", id, rec.Code, rec.Body.String()) + } + } + if rec := get(t, m.h, "GET", "/rest/getCoverArt", ok("id", "al-7")); rec.Header().Get("ETag") != `"abc"` || !strings.Contains(rec.Header().Get("Cache-Control"), "max-age") { + t.Fatalf("headers %v", rec.Header()) + } + failed(t, get(t, m.h, "GET", "/rest/getCoverArt", ok("id", "al-8")), ErrNotFound) + failed(t, get(t, m.h, "GET", "/rest/getCoverArt", ok("id", "ar-2")), ErrNotFound) + failed(t, get(t, m.h, "GET", "/rest/getCoverArt", ok("id", "tr-99")), ErrNotFound) + failed(t, get(t, m.h, "GET", "/rest/getCoverArt", ok("id", "pl-1")), ErrNotFound) + failed(t, get(t, m.h, "GET", "/rest/getCoverArt", ok()), ErrMissingParam) +} + +func TestACoverIsSmallUpTo300PixelsAndLargeAbove(t *testing.T) { + m := newMediaServer() + for _, size := range []string{"", "0", "150", "300", "301", "1200", "x"} { + get(t, m.h, "GET", "/rest/getCoverArt", ok("id", "al-7", "size", size)) + } + want := []images.Size{images.Large, images.Large, images.Small, images.Small, images.Large, images.Large, images.Large} + if fmt.Sprint(m.covers.sizes) != fmt.Sprint(want) { + t.Fatalf("sizes %v, want %v", m.covers.sizes, want) + } +} + +func TestAlbumListsAskForWhatTheAppWantsAndAnswerInTheOrderOfTheList(t *testing.T) { + m := newMediaServer() + r := good(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "byYear", "fromYear", "1990", "toYear", "2000", "size", "5", "offset", "0", "musicFolderId", "2"))) + al := r["albumList2"].(map[string]any)["album"].([]any) + if len(al) != 1 || al[0].(map[string]any)["id"] != "al-7" { + t.Fatalf("list %v", r) + } + q := m.lists.albumQ[0] + if q.Type != "byYear" || q.FromYear != 1990 || q.ToYear != 2000 || q.Size != 5 || q.LibraryID != 2 || q.UserID != 1 { + t.Fatalf("query %+v", q) + } + + get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "newest")) + if q := m.lists.albumQ[1]; q.Size != 10 || q.Offset != 0 { + t.Fatalf("defaults %+v", q) + } + get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "newest", "size", "9000")) + if q := m.lists.albumQ[2]; q.Size != 500 { + t.Fatalf("size is not limited: %+v", q) + } + get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "byGenre", "genre", "Jazz")) + if q := m.lists.albumQ[3]; q.Genre != "Jazz" { + t.Fatalf("genre %+v", q) + } + + empty := good(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "random", "offset", "5"))) + if _, ok := empty["albumList2"].(map[string]any); !ok { + t.Fatalf("an empty list is still a list: %v", empty) + } +} + +func TestAnAlbumListThatIsAskedForBadly(t *testing.T) { + m := newMediaServer() + failed(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok()), ErrMissingParam) + failed(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "nope")), ErrGeneric) + failed(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "byYear", "fromYear", "1990")), ErrMissingParam) + failed(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "byGenre")), ErrMissingParam) + failed(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "newest", "size", "many")), ErrGeneric) + failed(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "newest", "offset", "-1")), ErrGeneric) + failed(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "newest", "musicFolderId", "x")), ErrNotFound) +} + +func TestStarredThingsAreNone(t *testing.T) { + m := newMediaServer() + good(t, get(t, m.h, "GET", "/rest/star", ok("id", "tr-70"))) + good(t, get(t, m.h, "GET", "/rest/unstar", ok("id", "tr-70"))) + good(t, get(t, m.h, "GET", "/rest/setRating", ok("id", "tr-70", "rating", "5"))) + s := good(t, get(t, m.h, "GET", "/rest/getStarred2", ok()))["starred2"].(map[string]any) + if len(s) != 0 { + t.Fatalf("starred %v", s) + } + if r := good(t, get(t, m.h, "GET", "/rest/getAlbumList2", ok("type", "starred"))); r["albumList2"] == nil { + t.Fatalf("starred albums %v", r) + } +} + +func TestSongsAtRandomAndByGenre(t *testing.T) { + m := newMediaServer() + r := good(t, get(t, m.h, "GET", "/rest/getRandomSongs", ok("size", "2", "genre", "Rock", "fromYear", "1990", "toYear", "2000", "musicFolderId", "1"))) + songs := r["randomSongs"].(map[string]any)["song"].([]any) + // The songs come back in the order the list gave them. + if len(songs) != 2 || songs[0].(map[string]any)["id"] != "tr-71" || songs[1].(map[string]any)["id"] != "tr-70" { + t.Fatalf("songs %v", songs) + } + q := m.lists.songQ[0] + if !q.Random || q.Size != 2 || q.Genre != "Rock" || q.FromYear != 1990 || q.ToYear != 2000 || q.LibraryID != 1 { + t.Fatalf("query %+v", q) + } + + r = good(t, get(t, m.h, "GET", "/rest/getSongsByGenre", ok("genre", "Rock", "count", "3", "offset", "4"))) + if len(r["songsByGenre"].(map[string]any)["song"].([]any)) != 2 { + t.Fatalf("songs %v", r) + } + if q := m.lists.songQ[1]; q.Random || q.Size != 3 || q.Offset != 4 || q.Genre != "Rock" { + t.Fatalf("query %+v", q) + } + failed(t, get(t, m.h, "GET", "/rest/getSongsByGenre", ok()), ErrMissingParam) + if m.lists.songQ = nil; true { + get(t, m.h, "GET", "/rest/getRandomSongs", ok()) + if q := m.lists.songQ[0]; q.Size != 10 { + t.Fatalf("default size %+v", q) + } + } +} + +func TestSearchFindsArtistsAlbumsAndSongs(t *testing.T) { + m := newMediaServer() + r := good(t, get(t, m.h, "GET", "/rest/search3", ok("query", "", "artistCount", "2", "artistOffset", "1", "albumCount", "5", "songCount", "0")))["searchResult3"].(map[string]any) + artists := r["artist"].([]any) + if len(artists) != 2 || artists[0].(map[string]any)["name"] != "Björk" { + t.Fatalf("artists %v", artists) + } + if albums := r["album"].([]any); len(albums) != 1 || albums[0].(map[string]any)["id"] != "al-7" { + t.Fatalf("albums %v", albums) + } + if _, has := r["song"]; has { + t.Fatalf("songs were not asked for: %v", r["song"]) + } + failed(t, get(t, m.h, "GET", "/rest/search3", ok()), ErrMissingParam) +} + +func TestSearchReadsAsManyAsAskedEvenAboveAPageOfTheCatalogue(t *testing.T) { + m := newMediaServer() + // 500 songs asked: the answer is as many as there are, read page by page; the fake has none outside album 7. + r := good(t, get(t, m.h, "GET", "/rest/search3", ok("query", "x", "songCount", "500", "artistCount", "0", "albumCount", "0")))["searchResult3"].(map[string]any) + if _, has := r["artist"]; has { + t.Fatalf("%v", r) + } +} diff --git a/internal/opensubsonic/media.go b/internal/opensubsonic/media.go new file mode 100644 index 0000000..b180f33 --- /dev/null +++ b/internal/opensubsonic/media.go @@ -0,0 +1,437 @@ +package opensubsonic + +import ( + "context" + "errors" + "net/http" + "strconv" + "strings" + + "git.kmalloc.org/squid/sonda/internal/catalog" + "git.kmalloc.org/squid/sonda/internal/images" + "git.kmalloc.org/squid/sonda/internal/streaming" +) + +// Streams opens the audio file of a song: *streaming.Service. +type Streams interface { + Open(ctx context.Context, trackID int64) (*streaming.Stream, error) +} + +// Covers builds the cover of an album: *images.Service. +type Covers interface { + AlbumCover(ctx context.Context, albumID int64, size images.Size) (*images.Cover, error) +} + +// ArtistImages builds the picture of an artist: *images.Service. +type ArtistImages interface { + ArtistImage(ctx context.Context, artistID int64, size images.Size) (*images.Cover, error) +} + +// served is what a call that wrote its own answer (a file, a picture) returns: there is nothing more to write. +var served = &Response{} + +// stream sends the original file of a song, with Range, HEAD and conditional requests done by http.ServeContent. +// The file is sent as it is: Sonda does not transcode, so what an app asks for (format, bit rate) is ignored. +func (h *handler) stream(k *call) (*Response, *Error) { return h.send(k, false) } + +// download sends the original file as an attachment. +func (h *handler) download(k *call) (*Response, *Error) { return h.send(k, true) } + +func (h *handler) send(k *call, attachment bool) (*Response, *Error) { + id, bad := idParam(k, trackPrefix) + if bad != nil { + return nil, bad + } + st, err := h.Streams.Open(k.ctx, id) + switch { + case errors.Is(err, streaming.ErrNotFound), errors.Is(err, streaming.ErrFileMissing), errors.Is(err, streaming.ErrUnsafePath): + _ = k.c.Error(err) + return nil, notFound() + case err != nil: + _ = k.c.Error(err) + return nil, &Error{Code: ErrGeneric, Message: "the file could not be read"} + } + defer st.Close() + + hdr := k.c.Writer.Header() + hdr.Set("Content-Type", st.ContentType) + hdr.Set("X-Content-Type-Options", "nosniff") + // The address carries the password of the device: never leak it through Referer. + hdr.Set("Referrer-Policy", "no-referrer") + hdr.Set("Cache-Control", "private, no-cache") + if attachment { + hdr.Set("Content-Disposition", `attachment; filename="`+safeName(st.Name)+`"`) + } + http.ServeContent(k.c.Writer, k.c.Request, st.Name, st.ModTime, st.File) + return served, nil +} + +// safeName keeps a file name from closing the quotes of the header or adding a line to it. +func safeName(name string) string { + return strings.Map(func(r rune) rune { + if r == '"' || r == '\\' || r < ' ' || r == 0x7f { + return '_' + } + return r + }, name) +} + +// smallest is the biggest size, in pixels, that is served from the small picture. +const smallest = 300 + +// coverArt sends the picture of an album, of an artist, or of the album of a song. +func (h *handler) coverArt(k *call) (*Response, *Error) { + raw := k.param("id") + if raw == "" { + return nil, missing("id") + } + size := images.Large + if px, err := strconv.Atoi(k.param("size")); err == nil && px > 0 && px <= smallest { + size = images.Small + } + var ( + cover *images.Cover + err error + ) + switch { + case strings.HasPrefix(raw, artistPrefix): + id, bad := idParam(k, artistPrefix) + if bad != nil { + return nil, bad + } + cover, err = h.ArtistImages.ArtistImage(k.ctx, id, size) + case strings.HasPrefix(raw, albumPrefix), strings.HasPrefix(raw, trackPrefix): + albumID, bad := h.albumOfCover(k, raw) + if bad != nil { + return nil, bad + } + cover, err = h.Covers.AlbumCover(k.ctx, albumID, size) + default: + return nil, notFound() + } + switch { + case err == nil: + case errors.Is(err, context.Canceled): + return served, nil // the app left: nobody is waiting + case errors.Is(err, images.ErrNoCover), errors.Is(err, images.ErrAlbumNotFound), errors.Is(err, images.ErrNoImage), + errors.Is(err, images.ErrArtistNotFound), errors.Is(err, images.ErrProviderDisabled), errors.Is(err, images.ErrNoID): + return nil, notFound() + default: + _ = k.c.Error(err) + return nil, &Error{Code: ErrGeneric, Message: "the picture could not be made"} + } + defer cover.File.Close() + + hdr := k.c.Writer.Header() + hdr.Set("Content-Type", "image/jpeg") + hdr.Set("ETag", `"`+cover.ETag+`"`) + hdr.Set("Cache-Control", "private, max-age=3600") // it is the listener's own library + hdr.Set("X-Content-Type-Options", "nosniff") + hdr.Set("Referrer-Policy", "no-referrer") + http.ServeContent(k.c.Writer, k.c.Request, "cover.jpg", cover.ModTime, cover.File) + return served, nil +} + +// albumOfCover is the album an id stands for: the album itself, or the album of a song. +func (h *handler) albumOfCover(k *call, raw string) (int64, *Error) { + if strings.HasPrefix(raw, albumPrefix) { + return idParam(k, albumPrefix) + } + id, bad := idParam(k, trackPrefix) + if bad != nil { + return 0, bad + } + t, err := h.Catalog.Track(k.ctx, id) + if err != nil { + return 0, failure(err) + } + if t.Album == nil { + return 0, notFound() + } + return t.Album.ID, nil +} + +// ---- lists ---- + +const ( + defaultCount = 10 + maxCount = 500 + defaultFind = 20 +) + +// count reads a whole number parameter that must be 0 or more. An absent one is the default; the answer never +// exceeds max. +func count(k *call, name string, def, max int) (int, *Error) { + raw := k.param(name) + if raw == "" { + return def, nil + } + n, err := strconv.Atoi(raw) + if err != nil || n < 0 { + return 0, &Error{Code: ErrGeneric, Message: "invalid value for " + name} + } + return min(n, max), nil +} + +// folder reads musicFolderId: 0 is every library. +func folder(k *call) (int64, *Error) { + raw := k.param("musicFolderId") + if raw == "" { + return 0, nil + } + n, err := strconv.ParseInt(raw, 10, 64) + if err != nil || n < 1 { + return 0, notFound() + } + return n, nil +} + +var listTypes = map[string]bool{ + ListRandom: true, ListNewest: true, ListFrequent: true, ListRecent: true, ListStarred: true, + ListByName: true, ListByArtist: true, ListByYear: true, ListByGenre: true, +} + +func (h *handler) albumList2(k *call) (*Response, *Error) { + kind := k.param("type") + if kind == "" { + return nil, missing("type") + } + if !listTypes[kind] { + return nil, &Error{Code: ErrGeneric, Message: "unknown list type: " + kind} + } + q := AlbumQuery{Type: kind, UserID: k.principal.UserID, Genre: k.param("genre")} + var bad *Error + if q.Size, bad = count(k, "size", defaultCount, maxCount); bad != nil { + return nil, bad + } + if q.Offset, bad = count(k, "offset", 0, 1<<30); bad != nil { + return nil, bad + } + if q.LibraryID, bad = folder(k); bad != nil { + return nil, bad + } + switch kind { + case ListByYear: + if k.param("fromYear") == "" || k.param("toYear") == "" { + return nil, missing("fromYear and toYear") + } + if q.FromYear, bad = count(k, "fromYear", 0, 9999); bad != nil { + return nil, bad + } + if q.ToYear, bad = count(k, "toYear", 0, 9999); bad != nil { + return nil, bad + } + case ListByGenre: + if q.Genre == "" { + return nil, missing("genre") + } + } + ids, err := h.Lists.AlbumIDs(k.ctx, q) + if err != nil { + return nil, failure(err) + } + albums, err := h.albumsByID(k.ctx, ids) + if err != nil { + return nil, failure(err) + } + out, err := h.albums(k.ctx, albums) + if err != nil { + return nil, failure(err) + } + return &Response{AlbumList2: &AlbumList2{Album: out}}, nil +} + +func (h *handler) randomSongs(k *call) (*Response, *Error) { + q := SongQuery{Random: true, Genre: k.param("genre")} + var bad *Error + if q.Size, bad = count(k, "size", defaultCount, maxCount); bad != nil { + return nil, bad + } + if q.FromYear, bad = count(k, "fromYear", 0, 9999); bad != nil { + return nil, bad + } + if q.ToYear, bad = count(k, "toYear", 0, 9999); bad != nil { + return nil, bad + } + if q.LibraryID, bad = folder(k); bad != nil { + return nil, bad + } + songs, fail := h.songList(k, q) + if fail != nil { + return nil, fail + } + return &Response{RandomSongs: &SongList{Song: songs}}, nil +} + +func (h *handler) songsByGenre(k *call) (*Response, *Error) { + q := SongQuery{Genre: k.param("genre")} + if q.Genre == "" { + return nil, missing("genre") + } + var bad *Error + if q.Size, bad = count(k, "count", defaultCount, maxCount); bad != nil { + return nil, bad + } + if q.Offset, bad = count(k, "offset", 0, 1<<30); bad != nil { + return nil, bad + } + if q.LibraryID, bad = folder(k); bad != nil { + return nil, bad + } + songs, fail := h.songList(k, q) + if fail != nil { + return nil, fail + } + return &Response{SongsByGenre: &SongList{Song: songs}}, nil +} + +func (h *handler) songList(k *call, q SongQuery) ([]Child, *Error) { + ids, err := h.Lists.SongIDs(k.ctx, q) + if err != nil { + return nil, failure(err) + } + tracks, err := h.tracksByID(k.ctx, ids) + if err != nil { + return nil, failure(err) + } + songs, err := h.songs(k.ctx, tracks) + if err != nil { + return nil, failure(err) + } + return songs, nil +} + +// albumsByID reads albums by their ids, in the order of the ids; an id that cannot be played is left out. +func (h *handler) albumsByID(ctx context.Context, ids []int64) ([]catalog.Album, error) { + byID := map[int64]catalog.Album{} + rest := ids + for len(rest) > 0 { + chunk := rest[:min(len(rest), catalog.MaxLimit)] + rest = rest[len(chunk):] + p, err := h.Catalog.Albums(ctx, catalog.Filter{IDs: chunk, Limit: len(chunk)}) + if err != nil { + return nil, err + } + for _, a := range p.Items { + byID[a.ID] = a + } + } + return inOrder(ids, byID), nil +} + +// tracksByID is the same for songs. +func (h *handler) tracksByID(ctx context.Context, ids []int64) ([]catalog.Track, error) { + byID := map[int64]catalog.Track{} + rest := ids + for len(rest) > 0 { + chunk := rest[:min(len(rest), catalog.MaxLimit)] + rest = rest[len(chunk):] + p, err := h.Catalog.Tracks(ctx, catalog.Filter{IDs: chunk, Limit: len(chunk)}) + if err != nil { + return nil, err + } + for _, t := range p.Items { + byID[t.ID] = t + } + } + return inOrder(ids, byID), nil +} + +func inOrder[T any](ids []int64, byID map[int64]T) []T { + out := make([]T, 0, len(ids)) + for _, id := range ids { + if v, ok := byID[id]; ok { + out = append(out, v) + } + } + return out +} + +// ---- search ---- + +func (h *handler) search3(k *call) (*Response, *Error) { + if _, given := k.c.Request.Form["query"]; !given { + return nil, missing("query") + } + query := strings.Trim(k.param("query"), `"`) // some apps send an empty search as "" + lib, bad := folder(k) + if bad != nil { + return nil, bad + } + var n [3]struct{ count, offset int } + for i, name := range []string{"artist", "album", "song"} { + if n[i].count, bad = count(k, name+"Count", defaultFind, maxCount); bad != nil { + return nil, bad + } + if n[i].offset, bad = count(k, name+"Offset", 0, 1<<30); bad != nil { + return nil, bad + } + } + filter := catalog.Filter{Query: query, LibraryID: lib} + + res := &SearchResult{} + if n[0].count > 0 { + artists, err := window(n[0].count, n[0].offset, func(limit, offset int) (catalog.Page[catalog.Artist], error) { + f := filter + f.Limit, f.Offset = limit, offset + return h.Catalog.Artists(k.ctx, f) + }) + if err != nil { + return nil, failure(err) + } + for _, a := range artists { + res.Artist = append(res.Artist, artistOf(a)) + } + } + if n[1].count > 0 { + albums, err := window(n[1].count, n[1].offset, func(limit, offset int) (catalog.Page[catalog.Album], error) { + f := filter + f.Limit, f.Offset = limit, offset + return h.Catalog.Albums(k.ctx, f) + }) + if err != nil { + return nil, failure(err) + } + if res.Album, err = h.albums(k.ctx, albums); err != nil { + return nil, failure(err) + } + } + if n[2].count > 0 { + tracks, err := window(n[2].count, n[2].offset, func(limit, offset int) (catalog.Page[catalog.Track], error) { + f := filter + f.Limit, f.Offset = limit, offset + return h.Catalog.Tracks(k.ctx, f) + }) + if err != nil { + return nil, failure(err) + } + if res.Song, err = h.songs(k.ctx, tracks); err != nil { + return nil, failure(err) + } + } + return &Response{SearchResult3: res}, nil +} + +// window reads n items from an offset, a page of the catalogue at a time: an app may ask for more than a page holds, +// and one that gets fewer than it asked for takes the list for ended. +func window[T any](n, offset int, fetch func(limit, offset int) (catalog.Page[T], error)) ([]T, error) { + var out []T + for len(out) < n { + p, err := fetch(min(n-len(out), catalog.MaxLimit), offset+len(out)) + if err != nil { + return nil, err + } + out = append(out, p.Items...) + if len(p.Items) == 0 || offset+len(out) >= p.Total { + break + } + } + return out, nil +} + +func (h *handler) starred2(*call) (*Response, *Error) { + return &Response{Starred2: &SearchResult{}}, nil +} + +// nothing answers the calls that star, unstar or rate: Sonda has no stars yet, and an app should not break for it. +func nothing(*call) (*Response, *Error) { return &Response{}, nil } diff --git a/internal/opensubsonic/response.go b/internal/opensubsonic/response.go index f952ae3..7856283 100644 --- a/internal/opensubsonic/response.go +++ b/internal/opensubsonic/response.go @@ -29,6 +29,28 @@ type Response struct { Album *AlbumWithSongs `xml:"album" json:"album,omitempty"` Song *Child `xml:"song" json:"song,omitempty"` Genres *Genres `xml:"genres" json:"genres,omitempty"` + AlbumList2 *AlbumList2 `xml:"albumList2" json:"albumList2,omitempty"` + RandomSongs *SongList `xml:"randomSongs" json:"randomSongs,omitempty"` + SongsByGenre *SongList `xml:"songsByGenre" json:"songsByGenre,omitempty"` + SearchResult3 *SearchResult `xml:"searchResult3" json:"searchResult3,omitempty"` + Starred2 *SearchResult `xml:"starred2" json:"starred2,omitempty"` +} + +// AlbumList2 is a list of albums. +type AlbumList2 struct { + Album []AlbumID3 `xml:"album" json:"album,omitempty"` +} + +// SongList is a list of songs. +type SongList struct { + Song []Child `xml:"song" json:"song,omitempty"` +} + +// SearchResult is what a search found: artists, albums and songs. The starred things have the same shape. +type SearchResult struct { + Artist []ArtistID3 `xml:"artist" json:"artist,omitempty"` + Album []AlbumID3 `xml:"album" json:"album,omitempty"` + Song []Child `xml:"song" json:"song,omitempty"` } // Error codes of the Subsonic API. diff --git a/internal/opensubsonic/store.go b/internal/opensubsonic/store.go index 86cebf4..e6ec9bf 100644 --- a/internal/opensubsonic/store.go +++ b/internal/opensubsonic/store.go @@ -38,3 +38,46 @@ type Extras interface { // Genres returns every genre of the songs that can be played. Genres(ctx context.Context) ([]Genre, error) } + +// The kinds of list of albums an app can ask for. +const ( + ListRandom = "random" + ListNewest = "newest" + ListFrequent = "frequent" + ListRecent = "recent" + ListStarred = "starred" + ListByName = "alphabeticalByName" + ListByArtist = "alphabeticalByArtist" + ListByYear = "byYear" + ListByGenre = "byGenre" +) + +// AlbumQuery asks for a list of albums. +type AlbumQuery struct { + Type string // one of the List constants + Size int + Offset int + LibraryID int64 // 0 is every library + FromYear int // byYear: from FromYear to ToYear, backwards when FromYear is the later + ToYear int + Genre string // byGenre + UserID int64 // frequent and recent: whose listens +} + +// SongQuery asks for a list of songs. +type SongQuery struct { + Random bool // in any order, otherwise by album + Size int + Offset int + LibraryID int64 + FromYear int // 0 is no limit + ToYear int + Genre string // "" is any genre +} + +// Lists answers the calls that ask for albums and songs in an order that the catalogue does not know. They answer +// with ids, only of what can be played; the catalogue describes them. +type Lists interface { + AlbumIDs(ctx context.Context, q AlbumQuery) ([]int64, error) + SongIDs(ctx context.Context, q SongQuery) ([]int64, error) +} diff --git a/internal/storage/catalog.go b/internal/storage/catalog.go index 69b5bb6..8be76ce 100644 --- a/internal/storage/catalog.go +++ b/internal/storage/catalog.go @@ -271,6 +271,9 @@ func (r *CatalogRepo) listAlbums(ctx context.Context, f catalog.Filter, id int64 if id != 0 { w.add("al.id = ?", id) } + if f.IDs != nil { + w.add("al.id IN (?)", bun.In(f.IDs)) + } if f.ArtistID != 0 { // Same meaning as an artist's album_count: albums the artist owns, plus // albums where the artist is credited on a playable track. @@ -388,6 +391,9 @@ func (r *CatalogRepo) listTracks(ctx context.Context, f catalog.Filter, id int64 if id != 0 { w.add("t.id = ?", id) } + if f.IDs != nil { + w.add("t.id IN (?)", bun.In(f.IDs)) + } if f.AlbumID != 0 { w.add("t.album_id = ?", f.AlbumID) } diff --git a/internal/storage/catalog_test.go b/internal/storage/catalog_test.go index 456e613..deaa507 100644 --- a/internal/storage/catalog_test.go +++ b/internal/storage/catalog_test.go @@ -586,3 +586,30 @@ func TestCatalogAlbumsCanComeNewestFirst(t *testing.T) { } }) } + +func TestAListCanBeLimitedToTheIdsOfItsItems(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + s := seedCatalog(t, db) + ctx := context.Background() + got, err := s.repo.ListTracks(ctx, catalog.Filter{IDs: []int64{s.track["Airbag"], s.track["Karma Police"], s.track["Ghost"]}, Limit: 50}) + if err != nil { + t.Fatal(err) + } + // Ghost has no file to play, so it is not there whatever the ids say. + if titles := trackTitles(got); len(titles) != 2 || got.Total != 2 { + t.Fatalf("tracks of ids = %v (%d)", titles, got.Total) + } + albums, err := s.repo.ListAlbums(ctx, catalog.Filter{Query: "", Limit: 50}) + if err != nil || len(albums.Items) < 2 { + t.Fatalf("albums %v, %v", albums, err) + } + one, err := s.repo.ListAlbums(ctx, catalog.Filter{IDs: []int64{albums.Items[1].ID}, Limit: 50}) + if err != nil || len(one.Items) != 1 || one.Items[0].ID != albums.Items[1].ID { + t.Fatalf("albums of an id = %+v, %v", one.Items, err) + } + none, err := s.repo.ListTracks(ctx, catalog.Filter{IDs: []int64{}, Limit: 50}) + if err != nil || len(none.Items) != 0 { + t.Fatalf("an empty list of ids is no item, got %v, %v", trackTitles(none), err) + } + }) +} diff --git a/internal/storage/subsonic.go b/internal/storage/subsonic.go index bc41d5c..e4a59f0 100644 --- a/internal/storage/subsonic.go +++ b/internal/storage/subsonic.go @@ -2,22 +2,31 @@ package storage import ( "context" + "database/sql" + "errors" "fmt" "path" "strings" "time" "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect" "git.kmalloc.org/squid/sonda/internal/opensubsonic" ) // SubsonicRepo reads what the OpenSubsonic API needs and the catalogue does not carry. It implements -// opensubsonic.Extras. Only files that are not missing count, like everywhere else. -type SubsonicRepo struct{ db *bun.DB } +// opensubsonic.Extras and opensubsonic.Lists. Only files that are not missing count, like everywhere else. +type SubsonicRepo struct { + db *bun.DB + // minListen is how long a play must last to count as a listen. + minListen time.Duration +} -// NewSubsonicRepo builds a SubsonicRepo. -func NewSubsonicRepo(db *bun.DB) *SubsonicRepo { return &SubsonicRepo{db: db} } +// NewSubsonicRepo builds a SubsonicRepo. minListen is how long a play must last to count as a listen. +func NewSubsonicRepo(db *bun.DB, minListen time.Duration) *SubsonicRepo { + return &SubsonicRepo{db: db, minListen: minListen} +} // Tracks returns the extras of the songs that can be played, with three queries whatever the number of songs. func (r *SubsonicRepo) Tracks(ctx context.Context, ids []int64) (map[int64]opensubsonic.TrackExtra, error) { @@ -147,3 +156,113 @@ func (r *SubsonicRepo) Genres(ctx context.Context) ([]opensubsonic.Genre, error) } return out, rows.Err() } + +// ErrUnknownList is a kind of list of albums that is not known. +var ErrUnknownList = errors.New("unknown kind of list") + +// albumYear is the earliest year of the songs of an album, NULL when none has one. +const albumYear = "(SELECT MIN(ty.year) FROM tracks ty WHERE ty.album_id = al.id AND ty.year > 0)" + +// AlbumIDs returns the ids of a page of the albums that can be played, in the order asked for. +func (r *SubsonicRepo) AlbumIDs(ctx context.Context, q opensubsonic.AlbumQuery) ([]int64, error) { + vis, visArgs := visible("t", q.LibraryID) + var w where + w.add("EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = al.id AND "+vis+")", visArgs...) + join, order := "", "" + var joinArgs []any + + switch q.Type { + case opensubsonic.ListRandom: + order = "RANDOM()" + case opensubsonic.ListByName: + order = r.binary("al.title_key") + ", al.id" + case opensubsonic.ListByArtist: + join = "LEFT JOIN artists ar ON ar.id = al.artist_id" + order = r.binary("COALESCE(ar.name_key, '')") + ", " + r.binary("al.title_key") + ", al.id" + case opensubsonic.ListNewest: + order = `COALESCE((SELECT MAX(af.mtime_unix) FROM audio_files af JOIN tracks tt ON tt.id = af.track_id + WHERE tt.album_id = al.id AND af.missing_at IS NULL), 0) DESC, al.id DESC` + case opensubsonic.ListByYear: + from, to := q.FromYear, q.ToYear + dir := "ASC" + if from > to { + from, to, dir = to, from, "DESC" + } + w.add(albumYear+" BETWEEN ? AND ?", from, to) + order = albumYear + " " + dir + ", " + r.binary("al.title_key") + ", al.id" + case opensubsonic.ListByGenre: + w.add("EXISTS (SELECT 1 FROM tracks tg JOIN track_genres g ON g.track_id = tg.id WHERE tg.album_id = al.id AND g.genre = ?)", strings.ToLower(q.Genre)) + order = r.binary("al.title_key") + ", al.id" + case opensubsonic.ListFrequent, opensubsonic.ListRecent: + // Only the albums the listener listened to, by how many listens or by the last one. + measure, dir := "COUNT(*)", "DESC" + if q.Type == opensubsonic.ListRecent { + measure = "MAX(e.occurred_at)" + } + join = `JOIN (SELECT tt.album_id AS album_id, ` + measure + ` AS score FROM listening_events e JOIN tracks tt ON tt.id = e.track_id + WHERE e.user_id = ? AND ` + counted + ` AND tt.album_id IS NOT NULL GROUP BY tt.album_id) lis ON lis.album_id = al.id` + joinArgs = []any{q.UserID, r.minListen.Milliseconds()} + order = "lis.score " + dir + ", al.id DESC" + case opensubsonic.ListStarred: + return []int64{}, nil // nothing is starred: there are no stars yet + default: + return nil, fmt.Errorf("%w: %q", ErrUnknownList, q.Type) + } + + args := append(append([]any{}, joinArgs...), w.args...) + args = append(args, q.Size, q.Offset) + rows, err := r.db.QueryContext(ctx, + "SELECT al.id FROM albums al "+join+" WHERE "+w.sql()+" ORDER BY "+order+" LIMIT ? OFFSET ?", args...) + if err != nil { + return nil, fmt.Errorf("list albums: %w", err) + } + return scanIDs(rows) +} + +// SongIDs returns the ids of a page of the songs that can be played: by album, or in any order. +func (r *SubsonicRepo) SongIDs(ctx context.Context, q opensubsonic.SongQuery) ([]int64, error) { + vis, visArgs := visible("t", q.LibraryID) + var w where + w.add(vis, visArgs...) + if q.Genre != "" { + w.add("EXISTS (SELECT 1 FROM track_genres g WHERE g.track_id = t.id AND g.genre = ?)", strings.ToLower(q.Genre)) + } + if q.FromYear > 0 { + w.add("t.year >= ?", q.FromYear) + } + if q.ToYear > 0 { + w.add("t.year <= ?", q.ToYear) + } + order := r.binary("COALESCE(al.title_key, '')") + ", COALESCE(t.album_id, 0), COALESCE(t.disc_no, 0), COALESCE(t.track_no, 0), t.id" + if q.Random { + order = "RANDOM()" + } + rows, err := r.db.QueryContext(ctx, + "SELECT t.id FROM tracks t LEFT JOIN albums al ON al.id = t.album_id WHERE "+w.sql()+" ORDER BY "+order+" LIMIT ? OFFSET ?", + append(w.args, q.Size, q.Offset)...) + if err != nil { + return nil, fmt.Errorf("list songs: %w", err) + } + return scanIDs(rows) +} + +func scanIDs(rows *sql.Rows) ([]int64, error) { + defer rows.Close() + out := []int64{} + for rows.Next() { + var id int64 + if err := rows.Scan(&id); err != nil { + return nil, err + } + out = append(out, id) + } + return out, rows.Err() +} + +// binary orders by plain byte value, so that SQLite and PostgreSQL order alike (see CatalogRepo.binary). +func (r *SubsonicRepo) binary(expr string) string { + if r.db.Dialect().Name() == dialect.PG { + return expr + ` COLLATE "C"` + } + return expr +} diff --git a/internal/storage/subsonic_lists_test.go b/internal/storage/subsonic_lists_test.go new file mode 100644 index 0000000..888d458 --- /dev/null +++ b/internal/storage/subsonic_lists_test.go @@ -0,0 +1,191 @@ +package storage + +import ( + "context" + "reflect" + "testing" + "time" + + "github.com/uptrace/bun" + + "git.kmalloc.org/squid/sonda/internal/opensubsonic" +) + +type albumWorld struct { + repo *SubsonicRepo + album map[string]int64 // album id by title + user int64 + ctx context.Context + s seeded + db *bun.DB +} + +func newAlbumWorld(t *testing.T, db *bun.DB) albumWorld { + t.Helper() + w := albumWorld{ctx: context.Background(), s: seedCatalog(t, db), repo: NewSubsonicRepo(db, 20*time.Second), album: map[string]int64{}, db: db} + rows, err := db.Query("SELECT id, title FROM albums") + if err != nil { + t.Fatal(err) + } + for rows.Next() { + var id int64 + var title string + if err := rows.Scan(&id, &title); err != nil { + t.Fatal(err) + } + w.album[title] = id + } + rows.Close() + w.user = seedUser(t, db, "l@example.org") + w.exec("UPDATE tracks SET year = 1997 WHERE album_id = ?", w.album["OK Computer"]) + w.exec("UPDATE tracks SET year = 1995 WHERE album_id = ?", w.album["Post"]) + w.exec("UPDATE tracks SET year = 1959 WHERE album_id = ?", w.album["Kind of Blue"]) + w.exec("UPDATE audio_files SET mtime_unix = 100 WHERE track_id IN (SELECT id FROM tracks WHERE album_id = ?)", w.album["OK Computer"]) + w.exec("UPDATE audio_files SET mtime_unix = 400 WHERE track_id IN (SELECT id FROM tracks WHERE album_id = ?)", w.album["Post"]) + w.exec("UPDATE audio_files SET mtime_unix = 300 WHERE track_id IN (SELECT id FROM tracks WHERE album_id = ?)", w.album["Kind of Blue"]) + w.exec("UPDATE audio_files SET mtime_unix = 200 WHERE track_id IN (SELECT id FROM tracks WHERE album_id = ?)", w.album["Various Hits"]) + w.exec("INSERT INTO track_genres (track_id, genre) SELECT id, 'jazz' FROM tracks WHERE album_id = ?", w.album["Kind of Blue"]) + w.exec("INSERT INTO track_genres (track_id, genre) SELECT id, 'rock' FROM tracks WHERE album_id = ?", w.album["OK Computer"]) + return w +} + +func (w albumWorld) exec(q string, args ...any) { + if _, err := w.db.ExecContext(w.ctx, q, args...); err != nil { + panic(err) + } +} + +func (w albumWorld) listen(track string, at int64, kind string, ms int64) { + w.exec(`INSERT INTO listening_events (user_id, track_id, event_type, occurred_at, duration_played_ms, idempotency_key) + VALUES (?, ?, ?, ?, ?, ?)`, w.user, w.s.track[track], kind, at, ms, kind+track+time.Unix(at, 0).String()) +} + +func (w albumWorld) titles(t *testing.T, ids []int64, err error) []string { + t.Helper() + if err != nil { + t.Fatal(err) + } + byID := map[int64]string{} + for title, id := range w.album { + byID[id] = title + } + out := []string{} + for _, id := range ids { + out = append(out, byID[id]) + } + return out +} + +func TestAlbumListsComeInTheOrderAskedFor(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + w := newAlbumWorld(t, db) + ask := func(q opensubsonic.AlbumQuery) []string { + t.Helper() + if q.Size == 0 { + q.Size = 50 + } + ids, err := w.repo.AlbumIDs(w.ctx, q) + return w.titles(t, ids, err) + } + check := func(name string, got []string, want ...string) { + t.Helper() + if len(got) == 0 && len(want) == 0 { + return + } + if !reflect.DeepEqual(got, want) { + t.Errorf("%s = %v, want %v", name, got, want) + } + } + // The album of files that are all gone is never in a list. + check("by name", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByName}), "Kind of Blue", "OK Computer", "Post", "Various Hits") + check("by artist", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByArtist}), "Post", "Kind of Blue", "OK Computer", "Various Hits") + check("newest", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListNewest}), "Post", "Kind of Blue", "Various Hits", "OK Computer") + check("by year", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByYear, FromYear: 1990, ToYear: 2000}), "Post", "OK Computer") + check("by year backwards", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByYear, FromYear: 2000, ToYear: 1990}), "OK Computer", "Post") + check("by genre", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByGenre, Genre: "Jazz"}), "Kind of Blue") + check("by an unknown genre", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByGenre, Genre: "polka"})) + check("paged", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByName, Size: 2, Offset: 1}), "OK Computer", "Post") + check("past the end", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByName, Offset: 10})) + check("in a library", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListByName, LibraryID: w.s.jazz}), "Kind of Blue", "OK Computer", "Various Hits") // a second encoding of Airbag is in it + check("starred", ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListStarred})) + + random := ask(opensubsonic.AlbumQuery{Type: opensubsonic.ListRandom, Size: 3}) + seen := map[string]bool{} + for _, r := range random { + seen[r] = true + } + if len(random) != 3 || len(seen) != 3 { + t.Errorf("random = %v", random) + } + + if _, err := w.repo.AlbumIDs(w.ctx, opensubsonic.AlbumQuery{Type: "nope", Size: 5}); err == nil { + t.Error("an unknown kind of list was answered") + } + }) +} + +func TestFrequentAndRecentAlbumsAreOfTheListenerAndOfListensThatCount(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + w := newAlbumWorld(t, db) + other := seedUser(t, db, "o@example.org") + w.listen("So What", 1000, "complete", 1000) + w.listen("So What", 2000, "complete", 1000) + w.listen("Freddie Freeloader", 3000, "play", 30_000) // long enough to count + w.listen("Airbag", 4000, "complete", 1000) + w.listen("Karma Police", 5000, "skip", 2000) // too short: not a listen + w.exec(`INSERT INTO listening_events (user_id, track_id, event_type, occurred_at, duration_played_ms, idempotency_key) + VALUES (?, ?, 'complete', 9000, 1000, 'x')`, other, w.s.track["Army of Me"]) + + freq, err := w.repo.AlbumIDs(w.ctx, opensubsonic.AlbumQuery{Type: opensubsonic.ListFrequent, Size: 10, UserID: w.user}) + if got := w.titles(t, freq, err); !reflect.DeepEqual(got, []string{"Kind of Blue", "OK Computer"}) { + t.Errorf("frequent = %v", got) + } + recent, err := w.repo.AlbumIDs(w.ctx, opensubsonic.AlbumQuery{Type: opensubsonic.ListRecent, Size: 10, UserID: w.user}) + if got := w.titles(t, recent, err); !reflect.DeepEqual(got, []string{"OK Computer", "Kind of Blue"}) { + t.Errorf("recent = %v", got) + } + }) +} + +func TestSongListsAreByGenreAndRandom(t *testing.T) { + forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { + w := newAlbumWorld(t, db) + ids, err := w.repo.SongIDs(w.ctx, opensubsonic.SongQuery{Genre: "JAZZ", Size: 10}) + if err != nil || len(ids) != 3 { + t.Fatalf("jazz songs = %v, %v", ids, err) + } + // In the order of the album: So What (1), Freddie Freeloader (2), Blue in Green (3). + if ids[0] != w.s.track["So What"] || ids[2] != w.s.track["Blue in Green"] { + t.Errorf("jazz songs in the wrong order: %v", ids) + } + page, _ := w.repo.SongIDs(w.ctx, opensubsonic.SongQuery{Genre: "jazz", Size: 1, Offset: 1}) + if len(page) != 1 || page[0] != w.s.track["Freddie Freeloader"] { + t.Errorf("second jazz song = %v", page) + } + + years, _ := w.repo.SongIDs(w.ctx, opensubsonic.SongQuery{Random: true, FromYear: 1990, ToYear: 2000, Size: 50}) + if len(years) != 4 { // 3 of OK Computer that can be played (Ghost cannot), 1 of Post + t.Errorf("songs of the nineties = %d", len(years)) + } + some, _ := w.repo.SongIDs(w.ctx, opensubsonic.SongQuery{Random: true, Size: 5}) + seen := map[int64]bool{} + for _, id := range some { + seen[id] = true + } + if len(some) != 5 || len(seen) != 5 { + t.Errorf("random songs = %v", some) + } + if ghost, _ := w.repo.SongIDs(w.ctx, opensubsonic.SongQuery{Random: true, Size: 500}); contains(ghost, w.s.track["Ghost"]) || contains(ghost, w.s.track["Hidden Song"]) { + t.Error("a song with no file to play is in a list") + } + }) +} + +func contains(ids []int64, id int64) bool { + for _, x := range ids { + if x == id { + return true + } + } + return false +} diff --git a/internal/storage/subsonic_test.go b/internal/storage/subsonic_test.go index 08ea10a..2ffeabf 100644 --- a/internal/storage/subsonic_test.go +++ b/internal/storage/subsonic_test.go @@ -3,6 +3,7 @@ package storage import ( "context" "testing" + "time" "github.com/uptrace/bun" ) @@ -11,7 +12,7 @@ func TestTheExtrasOfSongsAndAlbumsComeFromTheirTagsAndFiles(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { ctx := context.Background() s := seedCatalog(t, db) - repo := NewSubsonicRepo(db) + repo := NewSubsonicRepo(db, 20*time.Second) exec := func(q string, args ...any) { t.Helper() @@ -72,7 +73,7 @@ func TestTheExtrasOfSongsAndAlbumsComeFromTheirTagsAndFiles(t *testing.T) { func TestNoIdsNoExtras(t *testing.T) { forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) { - repo := NewSubsonicRepo(db) + repo := NewSubsonicRepo(db, 20*time.Second) if m, err := repo.Tracks(context.Background(), nil); err != nil || len(m) != 0 { t.Fatalf("%v, %v", m, err) }