Files
minstrel/internal/subsonic/info.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

152 lines
4.6 KiB
Go

package subsonic
import (
"context"
"errors"
"net/http"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/Fabled-Sword/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
}
album, err := q.GetAlbumByID(ctx, id)
if err == nil {
return q.GetArtistByID(ctx, album.ArtistID)
}
if !errors.Is(err, pgx.ErrNoRows) {
return dbq.Artist{}, err
}
track, err := q.GetTrackByID(ctx, id)
if err != nil {
return dbq.Artist{}, err
}
return q.GetArtistByID(ctx, track.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})
}