Files
minstrel/internal/catalogue/catalogue.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

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"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
"github.com/Fabled-Sword/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
}