Files
minstrel/internal/catalogue/catalogue.go
T
bvandeusenandClaude Opus 5.5 55c7497ca7
release / go (push) Successful in 1m54s
release / web (push) Failing after 5s
release / integration (push) Failing after 3m44s
release / govulncheck (push) Successful in 20s
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 / Build signed APK (push) Canceled after 19s
release / android (push) Canceled after 2m37s
feat: Minstrel stops acquiring music and drops Discover
Minstrel is now a viewer of the library it is pointed at. Finding,
downloading and filing music moves out of it.

Removed from the server:
- the request flow (requests, approval, auto-approve) and the wanted list,
  follows, release profiles and release blocks;
- searching indexers, the download clients, the importer, the album
  lifecycle and missing-file re-acquisition;
- the catalogue's discography, search and art (the MusicBrainz release and
  track-list cache stays, for the duplicate resolver);
- Discover: its suggestions of artists not in the library, snoozes, the
  candidate-artist tag enrichment and its tuning scope;
- writes to acquisition tables from moving the library folder and from
  merging duplicates, the want-list join on Suspect sources, the sealing of
  acquisition credentials, and the release-group lookup only
  re-acquisition used.

Settling a wrong-slot duplicate no longer sets the release aside: the
copies go and their slots show as missing until a good copy is in the
library.

Removed from the web: Admin > Acquisition and Requests, /requests,
/discover, Follow on the artist page, the auto-approve toggle, and the
Collection's wanted-list tabs. The Collection is now the library's health:
an overview, Reports, Duplicates, Suspect sources and Missing files. The
README and docs no longer describe getting music.

The tables stay for now; a later migration drops them. The request
notification kinds and the Android screens are still to go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 20:51:14 -05:00

114 lines
3.7 KiB
Go

// Package catalogue keeps MusicBrainz's releases and track lists for the
// albums the library holds. The duplicate resolver reads them to tell a song
// filed in the wrong slot from one legitimately repeated.
package catalogue
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz"
)
// ErrNotFound: MusicBrainz has no release group under that id.
var ErrNotFound = errors.New("catalogue: not in MusicBrainz")
// Service reads release groups from MusicBrainz into the catalogue.
type Service struct {
pool *pgxpool.Pool
mb *musicbrainz.Client
logger *slog.Logger
}
// New makes a Service. A nil client talks to the public MusicBrainz.
func New(pool *pgxpool.Pool, mb *musicbrainz.Client, logger *slog.Logger) *Service {
if mb == nil {
mb = &musicbrainz.Client{}
}
if logger == nil {
logger = slog.Default()
}
return &Service{pool: pool, mb: mb, logger: logger}
}
func upsertGroup(ctx context.Context, q *dbq.Queries, g musicbrainz.ReleaseGroup) error {
secondary := g.SecondaryTypes
if secondary == nil {
secondary = []string{}
}
if err := q.UpsertMBReleaseGroup(ctx, dbq.UpsertMBReleaseGroupParams{
Mbid: g.MBID, Title: g.Title, PrimaryType: g.PrimaryType, SecondaryTypes: secondary,
FirstReleaseDate: g.FirstReleaseDate, ArtistCredit: g.ArtistCredit,
}); err != nil {
return fmt.Errorf("catalogue: save release group: %w", err)
}
return nil
}
// RefreshReleaseGroup reads a group's releases and track lists and replaces
// the held copy in one transaction. A group not yet held is looked up first.
func (s *Service) RefreshReleaseGroup(ctx context.Context, groupMBID string) error {
q := dbq.New(s.pool)
var group *musicbrainz.ReleaseGroup
if _, err := q.GetMBReleaseGroup(ctx, groupMBID); errors.Is(err, pgx.ErrNoRows) {
g, gerr := s.mb.ReleaseGroup(ctx, groupMBID)
if gerr != nil {
return mbErr(gerr)
}
group = &g
} else if err != nil {
return fmt.Errorf("catalogue: read release group: %w", err)
}
releases, err := s.mb.ReleaseGroupReleases(ctx, groupMBID)
if err != nil {
return mbErr(err)
}
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
tq := dbq.New(tx)
if group != nil {
if err := upsertGroup(ctx, tq, *group); err != nil {
return err
}
}
if err := tq.DeleteMBReleasesForGroup(ctx, groupMBID); err != nil {
return fmt.Errorf("catalogue: clear releases: %w", err)
}
for _, r := range releases {
if err := tq.InsertMBRelease(ctx, dbq.InsertMBReleaseParams{
Mbid: r.MBID, ReleaseGroupMbid: groupMBID, Title: r.Title, Status: r.Status,
ReleaseDate: r.Date, Country: r.Country, Disambiguation: r.Disambiguation,
Formats: r.Formats(), TrackCount: int32(r.TrackCount()),
}); err != nil {
return fmt.Errorf("catalogue: save release: %w", err)
}
for _, m := range r.Media {
for _, t := range m.Tracks {
if err := tq.InsertMBReleaseTrack(ctx, dbq.InsertMBReleaseTrackParams{
ReleaseMbid: r.MBID, MediumPosition: int32(m.Position), Position: int32(t.Position),
MediumFormat: m.Format, Number: t.Number, Title: t.Title,
LengthMs: int32(t.LengthMs), RecordingMbid: t.RecordingMBID,
}); err != nil {
return fmt.Errorf("catalogue: save track: %w", err)
}
}
}
}
return tq.MarkMBReleaseGroupReleasesRefreshed(ctx, groupMBID)
})
}
// mbErr maps MusicBrainz's not-found to the catalogue's; anything else passes
// through so callers can tell an unreachable MusicBrainz apart.
func mbErr(err error) error {
if errors.Is(err, musicbrainz.ErrNotFound) {
return fmt.Errorf("%w: %v", ErrNotFound, err)
}
return err
}