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 } 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}) }