feat(subsonic): browse by genre and by folder
release / web (push) Successful in 1m16s
release / go (push) Successful in 1m47s
release / govulncheck (push) Successful in 18s
release / integration (push) Successful in 3m59s
release / android (push) Successful in 4m30s
release / Build signed APK (push) Successful in 4m56s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 15s
release / Verify release artifacts (tag releases only) (push) Skipped

Subsonic apps can now list genres and play one (getGenres,
getSongsByGenre), and apps that browse by folder, such as DSub, can open
an artist to its albums and an album to its songs (getMusicDirectory),
with the album lists and search they use in that mode (getAlbumList,
search2).

Genres are split and counted as the web's genre index counts them, now
with an album count beside the song count. A genre's songs leave out the
listener's quarantined ones and missing files. Folders are Minstrel's
artists and albums, under the same ids as the tag-based calls.

getAlbumList2 and search3 now share their selection with the folder
versions, so the two modes list and find the same things.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-25 16:40:21 -05:00
co-authored by Claude Opus 5.5
parent da64d95264
commit 8731e9695a
9 changed files with 737 additions and 73 deletions
+99 -66
View File
@@ -181,26 +181,36 @@ func (b *browseHandlers) getAlbum(w http.ResponseWriter, r *http.Request) {
WriteFail(w, r, ErrGeneric, "Failed to load album artist")
return
}
tracks, err := q.ListTracksByAlbum(r.Context(), dbq.ListTracksByAlbumParams{AlbumID: id})
songs, err := albumSongs(r.Context(), q, album, artist.Name)
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to load tracks")
return
}
gains := replayGains(r.Context(), 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
// album's artist rather than failing the album.
names, _ := db.TrackArtistNames(r.Context(), q, album.ID)
songs := make([]SongRef, 0, len(tracks))
for _, t := range tracks {
songs = append(songs, songRef(t, album.Title, names.For(t.ArtistID, artist.Name), gains[t.ID]))
}
Write(w, r, AlbumResponse{
Envelope: NewEnvelope("ok"),
Album: albumDetail(album, artist.Name, songs),
})
}
// albumSongs is an album's songs in disc and track order, as getAlbum and
// getMusicDirectory both list them.
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})
if err != nil {
return nil, err
}
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
// album's artist rather than failing the album.
names, _ := db.TrackArtistNames(ctx, q, album.ID)
songs := make([]SongRef, 0, len(tracks))
for _, t := range tracks {
songs = append(songs, songRef(t, album.Title, names.For(t.ArtistID, artistName), gains[t.ID]))
}
return songs, nil
}
// getSong returns a single track by id. Parent metadata (album/artist name)
// is included so clients can render the song outside an album context.
func (b *browseHandlers) getSong(w http.ResponseWriter, r *http.Request) {
@@ -240,17 +250,36 @@ func (b *browseHandlers) getSong(w http.ResponseWriter, r *http.Request) {
})
}
// getAlbumList2 returns a paged album list filtered by type. Supported types
// match the Subsonic spec § getAlbumList2. "recent" and "frequent" read the
// caller's own play history: most recently played first, and most played
// first. "starred" and "highest" are both the caller's liked albums, newest
// like first: a like is the only rating Minstrel keeps.
// getAlbumList2 returns a paged album list filtered by type (listAlbums).
func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
q := dbq.New(b.pool)
albums, ok := b.listAlbums(w, r, q)
if !ok {
return
}
refs, err := b.albumRefs(r.Context(), q, albums)
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to resolve albums")
return
}
Write(w, r, AlbumList2Response{
Envelope: NewEnvelope("ok"),
AlbumList2: AlbumList2Container{Albums: refs},
})
}
// listAlbums is the page of albums getAlbumList and getAlbumList2 ask for,
// or false once it has written the failure. Supported types match the
// Subsonic spec § getAlbumList2. "recent" and "frequent" read the caller's
// own play history: most recently played first, and most played first.
// "starred" and "highest" are both the caller's liked albums, newest like
// first: a like is the only rating Minstrel keeps.
func (b *browseHandlers) listAlbums(w http.ResponseWriter, r *http.Request, q *dbq.Queries) ([]dbq.Album, bool) {
params := r.URL.Query()
listType := params.Get("type")
if listType == "" {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: type")
return
return nil, false
}
size := clampInt(atoiDefault(params.Get("size"), 10), 1, 500)
offset := atoiDefault(params.Get("offset"), 0)
@@ -258,7 +287,6 @@ func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
offset = 0
}
q := dbq.New(b.pool)
var albums []dbq.Album
var err error
@@ -289,7 +317,7 @@ func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
genre := params.Get("genre")
if genre == "" {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: genre")
return
return nil, false
}
// The query splits tracks.genre on [;,] rather than comparing the
// whole column, so a client asking for "Rock" also reaches tracks
@@ -302,7 +330,7 @@ func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
to, terr := strconv.Atoi(params.Get("toYear"))
if ferr != nil || terr != nil {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: fromYear and toYear")
return
return nil, false
}
albums, err = q.ListAlbumsByYearOrdered(r.Context(), dbq.ListAlbumsByYearOrderedParams{
FromYear: int32(from), ToYear: int32(to), Lim: int32(size), Off: int32(offset),
@@ -311,7 +339,7 @@ func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
return nil, false
}
albums, err = q.ListLikedAlbumRows(r.Context(), dbq.ListLikedAlbumRowsParams{
UserID: user.ID, Limit: int32(size), Offset: int32(offset),
@@ -320,7 +348,7 @@ func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
return nil, false
}
if listType == "recent" {
albums, err = q.ListAlbumsRecentlyPlayedByUser(r.Context(), dbq.ListAlbumsRecentlyPlayedByUserParams{
@@ -333,37 +361,59 @@ func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
}
default:
WriteFail(w, r, ErrGeneric, "Invalid list type: "+listType)
return
return nil, false
}
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to list albums")
return
return nil, false
}
refs, err := b.albumRefs(r.Context(), q, albums)
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to resolve albums")
return
}
Write(w, r, AlbumList2Response{
Envelope: NewEnvelope("ok"),
AlbumList2: AlbumList2Container{Albums: refs},
})
return albums, true
}
// search3 runs three independent searches — artist name, album title, song
// title — each bounded by its own count/offset. The Subsonic spec lets the
// caller page each facet separately, which is why the params triplicate.
// title — each bounded by its own count/offset (search).
func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
q := dbq.New(b.pool)
hits, ok := b.search(w, r, q)
if !ok {
return
}
albums, err := b.albumRefs(r.Context(), q, hits.albums)
if err != nil {
WriteFail(w, r, ErrGeneric, "Album search failed")
return
}
Write(w, r, SearchResult3Response{
Envelope: NewEnvelope("ok"),
SearchResult3: SearchResult3Container{
Artists: hits.artists,
Albums: albums,
Songs: hits.songs,
},
})
}
// searchHits is what one search found, before its albums are rendered as
// ID3 albums (search3) or as folders (search2).
type searchHits struct {
artists []ArtistRef
albums []dbq.Album
songs []SongRef
}
// search runs the three searches search2 and search3 share, or returns false
// once it has written the failure. The Subsonic spec lets the caller page
// each facet separately, which is why the params triplicate.
//
// An empty query matches everything, paged in a stable order. Apps that keep
// a copy of the library (Symfonium) download it this way, a page of songs at
// a time; refusing it leaves them a request per album or an empty library.
// Some send the empty query as two quote marks.
func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
func (b *browseHandlers) search(w http.ResponseWriter, r *http.Request, q *dbq.Queries) (searchHits, bool) {
params := r.URL.Query()
if !params.Has("query") {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: query")
return
return searchHits{}, false
}
query := strings.TrimSpace(params.Get("query"))
if query == `""` {
@@ -376,18 +426,13 @@ func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
songCount := clampInt(atoiDefault(params.Get("songCount"), 20), 0, 500)
songOffset := atoiDefault(params.Get("songOffset"), 0)
q := dbq.New(b.pool)
// The query goes through ILIKE '%' || query || '%', so it matches
// substrings, and an empty query matches every row.
needle := &query
// JSON marshaling of nil slices produces `null`, which some clients reject.
// Initialize each facet to an empty slice so absent matches render as `[]`.
result := SearchResult3Container{
Artists: []ArtistRef{},
Albums: []AlbumRef{},
Songs: []SongRef{},
}
hits := searchHits{artists: []ArtistRef{}, songs: []SongRef{}}
if artistCount > 0 {
artists, err := q.SearchArtists(r.Context(), dbq.SearchArtistsParams{
@@ -395,15 +440,15 @@ func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
WriteFail(w, r, ErrGeneric, "Artist search failed")
return
return searchHits{}, false
}
for _, a := range artists {
albums, aerr := q.ListAlbumsByArtist(r.Context(), a.ID)
if aerr != nil {
WriteFail(w, r, ErrGeneric, "Artist search failed")
return
return searchHits{}, false
}
result.Artists = append(result.Artists, artistRef(a, len(albums)))
hits.artists = append(hits.artists, artistRef(a, len(albums)))
}
}
@@ -413,14 +458,9 @@ func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
WriteFail(w, r, ErrGeneric, "Album search failed")
return
return searchHits{}, false
}
refs, rerr := b.albumRefs(r.Context(), q, albums)
if rerr != nil {
WriteFail(w, r, ErrGeneric, "Album search failed")
return
}
result.Albums = refs
hits.albums = albums
}
if songCount > 0 {
@@ -429,25 +469,18 @@ func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
}) // UserID left zero (NULL) — Subsonic has no per-user quarantine context
if err != nil {
WriteFail(w, r, ErrGeneric, "Song search failed")
return
return searchHits{}, false
}
gains := replayGains(r.Context(), q, tracks)
// One query each for the page's albums and artists, not two per
// song: a library download asks for every song in pages of 500.
albumTitles, artistNames, perr := songParents(r.Context(), q, tracks)
if perr != nil {
songs, serr := songRefs(r.Context(), q, tracks)
if serr != nil {
WriteFail(w, r, ErrGeneric, "Song search failed")
return
}
for _, t := range tracks {
result.Songs = append(result.Songs, songRef(t, albumTitles[t.AlbumID], artistNames[t.ArtistID], gains[t.ID]))
return searchHits{}, false
}
hits.songs = songs
}
Write(w, r, SearchResult3Response{
Envelope: NewEnvelope("ok"),
SearchResult3: result,
})
return hits, true
}
// songParents reads the album titles and artist names of tracks, in one