fix(subsonic): answer what the OpenSubsonic spec and Subsonic apps require
- getOpenSubsonicExtensions answers, without credentials, as the spec requires of a server that says openSubsonic=true; it lists apiKeyAuthentication. An apiKey sent with u is error 43 and an unknown apiKey error 44, the extension's codes; a wrong key still counts toward the sign-in throttle. - search3 with an empty query (or two quote marks) matches everything, paged in a stable order, so apps that keep a copy of the library (Symfonium) can download it. A page of songs reads its albums and artists in two queries, not two per song. - getAlbumList2 serves type=starred and highest (the listener's liked albums) and byYear (oldest first, newest first for a backwards range). - Internet radio, podcasts, bookmarks, shares, chat and now-playing answer with an empty list instead of "Method not implemented", so an app shows an empty tab rather than an error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+69
-16
@@ -243,7 +243,8 @@ 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.
|
||||
// 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) getAlbumList2(w http.ResponseWriter, r *http.Request) {
|
||||
params := r.URL.Query()
|
||||
listType := params.Get("type")
|
||||
@@ -296,6 +297,25 @@ func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
|
||||
albums, err = q.ListAlbumsByGenre(r.Context(), dbq.ListAlbumsByGenreParams{
|
||||
Genre: genre, Lim: int32(size), Off: int32(offset),
|
||||
})
|
||||
case "byYear":
|
||||
from, ferr := strconv.Atoi(params.Get("fromYear"))
|
||||
to, terr := strconv.Atoi(params.Get("toYear"))
|
||||
if ferr != nil || terr != nil {
|
||||
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: fromYear and toYear")
|
||||
return
|
||||
}
|
||||
albums, err = q.ListAlbumsByYearOrdered(r.Context(), dbq.ListAlbumsByYearOrderedParams{
|
||||
FromYear: int32(from), ToYear: int32(to), Lim: int32(size), Off: int32(offset),
|
||||
})
|
||||
case "starred", "highest":
|
||||
user, ok := UserFromContext(r.Context())
|
||||
if !ok {
|
||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
||||
return
|
||||
}
|
||||
albums, err = q.ListLikedAlbumRows(r.Context(), dbq.ListLikedAlbumRowsParams{
|
||||
UserID: user.ID, Limit: int32(size), Offset: int32(offset),
|
||||
})
|
||||
case "recent", "frequent":
|
||||
user, ok := UserFromContext(r.Context())
|
||||
if !ok {
|
||||
@@ -334,13 +354,21 @@ func (b *browseHandlers) getAlbumList2(w http.ResponseWriter, r *http.Request) {
|
||||
// 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.
|
||||
//
|
||||
// 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) {
|
||||
params := r.URL.Query()
|
||||
query := params.Get("query")
|
||||
if query == "" {
|
||||
if !params.Has("query") {
|
||||
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: query")
|
||||
return
|
||||
}
|
||||
query := strings.TrimSpace(params.Get("query"))
|
||||
if query == `""` {
|
||||
query = ""
|
||||
}
|
||||
artistCount := clampInt(atoiDefault(params.Get("artistCount"), 20), 0, 500)
|
||||
artistOffset := atoiDefault(params.Get("artistOffset"), 0)
|
||||
albumCount := clampInt(atoiDefault(params.Get("albumCount"), 20), 0, 500)
|
||||
@@ -349,8 +377,8 @@ func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
|
||||
songOffset := atoiDefault(params.Get("songOffset"), 0)
|
||||
|
||||
q := dbq.New(b.pool)
|
||||
// Subsonic clients send "" as a wildcard in some implementations; we pass
|
||||
// the literal query through sqlc's ILIKE param so it matches substrings.
|
||||
// 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.
|
||||
@@ -404,18 +432,15 @@ func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
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 {
|
||||
WriteFail(w, r, ErrGeneric, "Song search failed")
|
||||
return
|
||||
}
|
||||
for _, t := range tracks {
|
||||
album, aerr := q.GetAlbumByID(r.Context(), t.AlbumID)
|
||||
if aerr != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Song search failed")
|
||||
return
|
||||
}
|
||||
artist, aerr := q.GetArtistByID(r.Context(), t.ArtistID)
|
||||
if aerr != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Song search failed")
|
||||
return
|
||||
}
|
||||
result.Songs = append(result.Songs, songRef(t, album.Title, artist.Name, gains[t.ID]))
|
||||
result.Songs = append(result.Songs, songRef(t, albumTitles[t.AlbumID], artistNames[t.ArtistID], gains[t.ID]))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -425,6 +450,34 @@ func (b *browseHandlers) search3(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// songParents reads the album titles and artist names of tracks, in one
|
||||
// query for each.
|
||||
func songParents(ctx context.Context, q *dbq.Queries, tracks []dbq.Track) (albumTitles, artistNames map[pgtype.UUID]string, err error) {
|
||||
albumIDs := make([]pgtype.UUID, 0, len(tracks))
|
||||
artistIDs := make([]pgtype.UUID, 0, len(tracks))
|
||||
for _, t := range tracks {
|
||||
albumIDs = append(albumIDs, t.AlbumID)
|
||||
artistIDs = append(artistIDs, t.ArtistID)
|
||||
}
|
||||
albums, err := q.GetAlbumsByIDs(ctx, albumIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
artists, err := q.GetArtistsByIDs(ctx, artistIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
albumTitles = make(map[pgtype.UUID]string, len(albums))
|
||||
for _, a := range albums {
|
||||
albumTitles[a.ID] = a.Title
|
||||
}
|
||||
artistNames = make(map[pgtype.UUID]string, len(artists))
|
||||
for _, a := range artists {
|
||||
artistNames[a.ID] = a.Name
|
||||
}
|
||||
return albumTitles, artistNames, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
Reference in New Issue
Block a user