Files
minstrel/internal/subsonic/info.go
T
bvandeusenandClaude Opus 5.5 f87aba5b86
release / go (push) Failing after 1m15s
release / web (push) Successful in 1m18s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 3m57s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 4m33s
release / Build signed APK (push) Canceled after 4m9s
feat(subsonic): scan status, artist and album info, lyrics, ratings and users
The last Subsonic methods apps call that Minstrel didn't answer:

- getScanStatus counts the songs whose files are present and says
  whether a scan is running; startScan, for an admin, starts the scan the
  web app's Rescan starts, and answers "scanning" straight away since the
  scan records itself a moment later.
- getArtistInfo and getArtistInfo2 give the artist's MusicBrainz id and the
  artists in this library that sound like them, from an artist, album or
  song id; getAlbumInfo and getAlbumInfo2 give the album's MusicBrainz id.
  Minstrel fetches no biographies or outside images, so none are sent.
- getLyrics (by artist and title) and getLyricsBySongId (OpenSubsonic
  songLyrics, now declared) read lyrics when asked: a .lrc file beside the
  song gives timed lyrics, otherwise the song's own lyrics tag.
- setRating accepts a rating and keeps none: a like is Minstrel's only
  rating, and a low rating is never read as a dislike.
- getUsers lists every user, for an admin only.

ArtistRef no longer fixes its XML element name, so it can appear as
similarArtist; every field holding it names the element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 17:43:13 -05:00

151 lines
4.6 KiB
Go

package subsonic
import (
"context"
"errors"
"net/http"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
)
// Artist and album info is what Minstrel knows beyond the tags: the
// MusicBrainz id, and for an artist the artists in this library that sound
// like them. Minstrel fetches no biographies, album notes or outside images,
// so those are left out rather than sent empty.
// ArtistInfo is the body of getArtistInfo and getArtistInfo2.
type ArtistInfo struct {
MusicBrainzID string `json:"musicBrainzId,omitempty" xml:"musicBrainzId,omitempty"`
SimilarArtists []ArtistRef `json:"similarArtist" xml:"similarArtist"`
}
type ArtistInfoResponse struct {
Envelope
ArtistInfo ArtistInfo `json:"artistInfo" xml:"artistInfo"`
}
type ArtistInfo2Response struct {
Envelope
ArtistInfo2 ArtistInfo `json:"artistInfo2" xml:"artistInfo2"`
}
// AlbumInfo is the body of getAlbumInfo and getAlbumInfo2.
type AlbumInfo struct {
MusicBrainzID string `json:"musicBrainzId,omitempty" xml:"musicBrainzId,omitempty"`
}
type AlbumInfoResponse struct {
Envelope
AlbumInfo AlbumInfo `json:"albumInfo" xml:"albumInfo"`
}
func (b *browseHandlers) getArtistInfo(w http.ResponseWriter, r *http.Request) {
if info, ok := b.artistInfo(w, r); ok {
Write(w, r, ArtistInfoResponse{Envelope: NewEnvelope("ok"), ArtistInfo: info})
}
}
func (b *browseHandlers) getArtistInfo2(w http.ResponseWriter, r *http.Request) {
if info, ok := b.artistInfo(w, r); ok {
Write(w, r, ArtistInfo2Response{Envelope: NewEnvelope("ok"), ArtistInfo2: info})
}
}
// artistInfo reads the artist named by id (an artist, or an album or song of
// theirs, as the spec allows) and up to count similar artists in this
// library, most similar first; count defaults to 20. It returns false once
// it has written the failure.
func (b *browseHandlers) artistInfo(w http.ResponseWriter, r *http.Request) (ArtistInfo, bool) {
params := r.URL.Query()
idStr := params.Get("id")
if idStr == "" {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: id")
return ArtistInfo{}, false
}
id, ok := parseUUID(idStr)
if !ok {
WriteFail(w, r, ErrDataNotFound, "Artist not found")
return ArtistInfo{}, false
}
count := clampInt(atoiDefault(params.Get("count"), 20), 0, 100)
q := dbq.New(b.pool)
artist, err := artistOf(r.Context(), q, id)
if errors.Is(err, pgx.ErrNoRows) {
WriteFail(w, r, ErrDataNotFound, "Artist not found")
return ArtistInfo{}, false
}
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to load artist")
return ArtistInfo{}, false
}
info := ArtistInfo{SimilarArtists: []ArtistRef{}}
if artist.Mbid != nil {
info.MusicBrainzID = *artist.Mbid
}
if count > 0 {
rows, err := q.ListSimilarArtistsForArtist(r.Context(), dbq.ListSimilarArtistsForArtistParams{
SeedArtistID: artist.ID, ResultLimit: int32(count),
})
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to load similar artists")
return ArtistInfo{}, false
}
for _, row := range rows {
info.SimilarArtists = append(info.SimilarArtists, artistRef(row.Artist, int(row.AlbumCount)))
}
}
return info, true
}
// artistOf is the artist an id names: the artist itself, or the artist of an
// album or a song. pgx.ErrNoRows when it names none of them.
func artistOf(ctx context.Context, q *dbq.Queries, id pgtype.UUID) (dbq.Artist, error) {
artist, err := q.GetArtistByID(ctx, id)
if !errors.Is(err, pgx.ErrNoRows) {
return artist, err
}
artistID := pgtype.UUID{}
if album, err := q.GetAlbumByID(ctx, id); err == nil {
artistID = album.ArtistID
} else if !errors.Is(err, pgx.ErrNoRows) {
return dbq.Artist{}, err
} else if track, err := q.GetTrackByID(ctx, id); err == nil {
artistID = track.ArtistID
} else {
return dbq.Artist{}, err
}
return q.GetArtistByID(ctx, artistID)
}
// getAlbumInfo serves both getAlbumInfo and getAlbumInfo2: the two take an
// album id and answer the same albumInfo element.
func (b *browseHandlers) getAlbumInfo(w http.ResponseWriter, r *http.Request) {
idStr := r.URL.Query().Get("id")
if idStr == "" {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: id")
return
}
id, ok := parseUUID(idStr)
if !ok {
WriteFail(w, r, ErrDataNotFound, "Album not found")
return
}
album, err := dbq.New(b.pool).GetAlbumByID(r.Context(), id)
if errors.Is(err, pgx.ErrNoRows) {
WriteFail(w, r, ErrDataNotFound, "Album not found")
return
}
if err != nil {
WriteFail(w, r, ErrGeneric, "Failed to load album")
return
}
info := AlbumInfo{}
if album.Mbid != nil {
info.MusicBrainzID = *album.Mbid
}
Write(w, r, AlbumInfoResponse{Envelope: NewEnvelope("ok"), AlbumInfo: info})
}