fix(subsonic): albums, folders and search list only what the listener can play
getAlbum, getMusicDirectory and search2/3 ignored the listener's quarantine and listed missing files, which an app then failed to stream; song counts counted both. They now pass the listener and leave out missing files, as the mixes already did. Subsonic can't mark a song unplayable, so leaving it out is how the native apps' greyed-out row comes across. Song search filters in SQL (SearchPlayableTracks), so a library sync paging through search3 never gets a short page that reads as the end. Album song counts come from one query for the whole page (CountPlayableTracksByAlbums) instead of one per album. tracks.sql also drops CountTracksByArtist, which nothing called; see the next commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+60
-12
@@ -138,10 +138,14 @@ func (b *browseHandlers) getArtist(w http.ResponseWriter, r *http.Request) {
|
||||
WriteFail(w, r, ErrGeneric, "Failed to load albums")
|
||||
return
|
||||
}
|
||||
counts, err := playableCounts(r.Context(), q, albums)
|
||||
if err != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Failed to load albums")
|
||||
return
|
||||
}
|
||||
refs := make([]AlbumRef, 0, len(albums))
|
||||
for _, a := range albums {
|
||||
count, _ := q.CountTracksByAlbum(r.Context(), a.ID)
|
||||
refs = append(refs, albumRef(a, artist.Name, int(count), 0))
|
||||
refs = append(refs, albumRef(a, artist.Name, counts[a.ID], 0))
|
||||
}
|
||||
Write(w, r, ArtistResponse{
|
||||
Envelope: NewEnvelope("ok"),
|
||||
@@ -194,12 +198,21 @@ func (b *browseHandlers) getAlbum(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// albumSongs is an album's songs in disc and track order, as getAlbum and
|
||||
// getMusicDirectory both list them.
|
||||
// getMusicDirectory both list them: the ones the listener can play. A
|
||||
// track they quarantined is hidden from them, as everywhere in Minstrel,
|
||||
// and a missing file is left out, since Subsonic has no way to show a song
|
||||
// as unplayable and an app would only fail to stream it.
|
||||
func albumSongs(ctx context.Context, q *dbq.Queries, album dbq.Album, artistName string) ([]SongRef, error) {
|
||||
tracks, err := q.ListTracksByAlbum(ctx, dbq.ListTracksByAlbumParams{AlbumID: album.ID})
|
||||
all, err := q.ListTracksByAlbum(ctx, dbq.ListTracksByAlbumParams{AlbumID: album.ID, UserID: listenerID(ctx)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tracks := all[:0]
|
||||
for _, t := range all {
|
||||
if !t.MissingSince.Valid {
|
||||
tracks = append(tracks, t)
|
||||
}
|
||||
}
|
||||
gains := replayGains(ctx, q, tracks)
|
||||
// Each song is credited to its own artist: on a compilation or a track
|
||||
// with a guest, that is not the album's. A failed lookup credits the
|
||||
@@ -465,9 +478,11 @@ func (b *browseHandlers) search(w http.ResponseWriter, r *http.Request, q *dbq.Q
|
||||
}
|
||||
|
||||
if songCount > 0 {
|
||||
tracks, err := q.SearchTracks(r.Context(), dbq.SearchTracksParams{
|
||||
Column1: query, Limit: int32(songCount), Offset: int32(songOffset),
|
||||
}) // UserID left zero (NULL) — Subsonic has no per-user quarantine context
|
||||
// Only songs the listener can play: see SearchPlayableTracks for why
|
||||
// the filter can't come after the page is cut.
|
||||
tracks, err := q.SearchPlayableTracks(r.Context(), dbq.SearchPlayableTracksParams{
|
||||
Query: query, UserID: listenerID(r.Context()), RowLimit: int32(songCount), RowOffset: int32(songOffset),
|
||||
})
|
||||
if err != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Song search failed")
|
||||
return searchHits{}, false
|
||||
@@ -513,9 +528,8 @@ func songParents(ctx context.Context, q *dbq.Queries, tracks []dbq.Track) (album
|
||||
}
|
||||
|
||||
// albumRefs converts a list of dbq.Album rows to AlbumRef, resolving artist
|
||||
// name and track count per album. Uses resolveArtistNames for the artist side
|
||||
// (one query per distinct artist) and CountTracksByAlbum per album. For small
|
||||
// page sizes (≤ 500) this is well within budget.
|
||||
// names (one query per distinct artist) and each album's song count: the
|
||||
// songs the listener can play, so it matches what getAlbum lists.
|
||||
func (b *browseHandlers) albumRefs(ctx context.Context, q *dbq.Queries, albums []dbq.Album) ([]AlbumRef, error) {
|
||||
// Always return a non-nil slice — JSON clients expect [] on empty results,
|
||||
// not null. Same rationale applies to search3 facet slices.
|
||||
@@ -530,14 +544,48 @@ func (b *browseHandlers) albumRefs(ctx context.Context, q *dbq.Queries, albums [
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts, err := playableCounts(ctx, q, albums)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
refs := make([]AlbumRef, 0, len(albums))
|
||||
for _, a := range albums {
|
||||
count, _ := q.CountTracksByAlbum(ctx, a.ID)
|
||||
refs = append(refs, albumRef(a, names[uuidToID(a.ArtistID)], int(count), 0))
|
||||
refs = append(refs, albumRef(a, names[uuidToID(a.ArtistID)], counts[a.ID], 0))
|
||||
}
|
||||
return refs, nil
|
||||
}
|
||||
|
||||
// playableCounts is how many of each album's tracks the listener can play,
|
||||
// by album id: present on disk and not quarantined by them. An album with
|
||||
// none is absent, so it reads as 0.
|
||||
func playableCounts(ctx context.Context, q *dbq.Queries, albums []dbq.Album) (map[pgtype.UUID]int, error) {
|
||||
ids := make([]pgtype.UUID, 0, len(albums))
|
||||
for _, a := range albums {
|
||||
ids = append(ids, a.ID)
|
||||
}
|
||||
rows, err := q.CountPlayableTracksByAlbums(ctx, dbq.CountPlayableTracksByAlbumsParams{
|
||||
AlbumIds: ids, UserID: listenerID(ctx),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make(map[pgtype.UUID]int, len(rows))
|
||||
for _, row := range rows {
|
||||
out[row.AlbumID] = int(row.TrackCount)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// listenerID is the signed-in caller. Every method that lists songs runs
|
||||
// behind the auth middleware, so it is set there; elsewhere it is the zero
|
||||
// id, which matches no one's quarantine.
|
||||
func listenerID(ctx context.Context) pgtype.UUID {
|
||||
if u, ok := UserFromContext(ctx); ok {
|
||||
return u.ID
|
||||
}
|
||||
return pgtype.UUID{}
|
||||
}
|
||||
|
||||
func atoiDefault(s string, def int) int {
|
||||
if s == "" {
|
||||
return def
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package subsonic
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Subsonic apps are shown only the songs the listener can play, as the web
|
||||
// and Android apps are: a track they quarantined is hidden from them, and a
|
||||
// missing file is left out, since Subsonic can't mark a song unplayable and
|
||||
// an app would only fail to stream it. Album song counts agree with the
|
||||
// songs listed.
|
||||
//
|
||||
// In the picks fixture the listener quarantined Ember (Lantern Hours), and
|
||||
// Jetty's file (Gone Tide's only song) is missing.
|
||||
func TestSubsonic_ListsOnlyWhatTheListenerCanPlay(t *testing.T) {
|
||||
f := seedPicks(t)
|
||||
b := &browseHandlers{pool: f.pool}
|
||||
lantern := uuidToID(f.albums["Lantern Hours"].ID)
|
||||
gone := uuidToID(f.albums["Gone Tide"].ID)
|
||||
|
||||
var album AlbumResponse
|
||||
if code := call(t, b.getAlbum, f.user, "id="+lantern, &album); code != 0 {
|
||||
t.Fatalf("getAlbum: error %d", code)
|
||||
}
|
||||
if got := titles(album.Album.Songs); !slices.Equal(got, []string{"Dusk", "Flint"}) || album.Album.SongCount != 2 {
|
||||
t.Errorf("getAlbum Lantern Hours: %v (songCount %d), want [Dusk Flint] and 2", got, album.Album.SongCount)
|
||||
}
|
||||
if code := call(t, b.getAlbum, f.user, "id="+gone, &album); code != 0 {
|
||||
t.Fatalf("getAlbum Gone Tide: error %d", code)
|
||||
}
|
||||
if len(album.Album.Songs) != 0 || album.Album.SongCount != 0 {
|
||||
t.Errorf("getAlbum Gone Tide: %v (songCount %d), want nothing", titles(album.Album.Songs), album.Album.SongCount)
|
||||
}
|
||||
|
||||
var dir DirectoryResponse
|
||||
if code := call(t, b.getMusicDirectory, f.user, "id="+lantern, &dir); code != 0 {
|
||||
t.Fatalf("getMusicDirectory: error %d", code)
|
||||
}
|
||||
if got := titles(dir.Directory.Children); !slices.Equal(got, []string{"Dusk", "Flint"}) {
|
||||
t.Errorf("getMusicDirectory Lantern Hours: %v, want [Dusk Flint]", got)
|
||||
}
|
||||
|
||||
var search SearchResult3Response
|
||||
if code := call(t, b.search3, f.user, "query=&songCount=50&albumCount=50&artistCount=0", &search); code != 0 {
|
||||
t.Fatalf("search3: error %d", code)
|
||||
}
|
||||
if got := titles(search.SearchResult3.Songs); !slices.Equal(got, []string{"Dusk", "Flint", "Gale", "Harbour", "Inlet"}) {
|
||||
t.Errorf("search3 songs: %v, want every song but Ember and Jetty", got)
|
||||
}
|
||||
// Paging through the library still reaches every playable song: the
|
||||
// hidden ones don't leave a short page that reads as the end.
|
||||
var page SearchResult3Response
|
||||
if code := call(t, b.search3, f.user, "query=&songCount=2&songOffset=2&albumCount=0&artistCount=0", &page); code != 0 {
|
||||
t.Fatalf("search3 page: error %d", code)
|
||||
}
|
||||
if got := titles(page.SearchResult3.Songs); !slices.Equal(got, []string{"Gale", "Harbour"}) {
|
||||
t.Errorf("search3 second page: %v, want [Gale Harbour]", got)
|
||||
}
|
||||
counts := map[string]int{}
|
||||
for _, a := range search.SearchResult3.Albums {
|
||||
counts[a.Name] = a.SongCount
|
||||
}
|
||||
want := map[string]int{"Lantern Hours": 2, "Paper Boats": 3, "Gone Tide": 0}
|
||||
for name, n := range want {
|
||||
if counts[name] != n {
|
||||
t.Errorf("search3 album %s songCount %d, want %d", name, counts[name], n)
|
||||
}
|
||||
}
|
||||
|
||||
var artist ArtistResponse
|
||||
if code := call(t, b.getArtist, f.user, "id="+uuidToID(f.artist.ID), &artist); code != 0 {
|
||||
t.Fatalf("getArtist: error %d", code)
|
||||
}
|
||||
for _, a := range artist.Artist.Albums {
|
||||
if a.SongCount != want[a.Name] {
|
||||
t.Errorf("getArtist album %s songCount %d, want %d", a.Name, a.SongCount, want[a.Name])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -213,16 +213,16 @@ func loadStarred(ctx context.Context, q *dbq.Queries, userID pgtype.UUID) ([]Son
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
counts, err := playableCounts(ctx, q, albumRows)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
for _, a := range albumRows {
|
||||
artist, err := q.GetArtistByID(ctx, a.ArtistID)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
count, err := q.CountTracksByAlbum(ctx, a.ID)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
albums = append(albums, albumRef(a, artist.Name, int(count), 0))
|
||||
albums = append(albums, albumRef(a, artist.Name, counts[a.ID], 0))
|
||||
}
|
||||
|
||||
artistRows, err := q.ListLikedArtistRows(ctx, dbq.ListLikedArtistRowsParams{
|
||||
|
||||
Reference in New Issue
Block a user