Files
minstrel/internal/tracks/service.go
T
bvandeusenandClaude Opus 5.5 492eb2fb9a
release / go (push) Successful in 1m49s
release / web (push) Successful in 1m26s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m29s
release / android (push) Successful in 4m43s
release / Build signed APK (push) Successful in 5m1s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
feat(library): Missing files says why each track waits, and can forget or export it
- The missing list carries each track's disc and track number, its
  recording and release-group MBIDs, the Collection queue its file was
  removed from, and the admin who removed it.
- POST /api/admin/library/missing/forget deletes waiting tracks for good,
  tidying away an album or artist they leave empty, and writes a
  track_forget audit row naming each. It is all or nothing: a track that
  has its file again is refused with 409 track_has_file.
- GET /api/admin/library/missing.csv exports every waiting track (artist,
  album, disc, track, title, MBIDs, why, since) to hand to whatever finds
  music.
- DELETE /api/admin/tracks/{id} is retired. No client called it; its
  service becomes tracks.Service.ForgetTracks.

Grimoire #7744 (milestone 635).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 18:34:01 -05:00

92 lines
3.4 KiB
Go

// Package tracks owns the track-level admin actions that are written to the
// audit log: forgetting tracks that wait for a file, and the duplicate merge.
package tracks
import (
"context"
"errors"
"log/slog"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"git.fabledsword.com/bvandeusen/minstrel/internal/apierror"
"git.fabledsword.com/bvandeusen/minstrel/internal/audit"
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
)
// ErrNotFound is returned when the track id doesn't resolve. Aliased
// to apierror.ErrNotFound so handlers can errors.Is against the shared
// sentinel; existing tracks.ErrNotFound callsites still resolve to the
// same pointer.
var ErrNotFound = apierror.ErrNotFound
// Service owns ForgetTracks and MergeDuplicates.
type Service struct {
pool *pgxpool.Pool
logger *slog.Logger
dataDir string
}
// NewService constructs a Service. logger may be nil (defaults to
// slog.Default). dataDir is the on-disk root for cached artifacts; used to
// clean up artist-art on artist delete. Empty string disables the cleanup.
func NewService(pool *pgxpool.Pool, logger *slog.Logger, dataDir string) *Service {
if logger == nil {
logger = slog.Default()
}
return &Service{pool: pool, logger: logger, dataDir: dataDir}
}
// ForgetTracks removes tracks waiting for a file that an admin has given up
// on (library.ForgetMissingTracks), and writes the forget to the audit log
// with adminID as the actor, naming each track and the file it had. It is all
// or nothing: a track that has its file again is library.ErrTrackPresent, and
// an unknown one is ErrNotFound.
func (s *Service) ForgetTracks(ctx context.Context, ids []pgtype.UUID, adminID pgtype.UUID) ([]library.ForgottenTrack, error) {
gone, err := library.ForgetMissingTracks(ctx, s.pool, s.logger, s.dataDir, ids)
if err != nil {
if errors.Is(err, library.ErrTrackNotFound) {
return nil, ErrNotFound
}
return nil, err
}
named := make([]map[string]string, 0, len(gone))
for _, t := range gone {
named = append(named, map[string]string{
"track_id": syncpkg.FormatUUID(t.ID), "title": t.Title, "file_path": t.FilePath,
})
}
audit.WriteOrLog(ctx, s.pool, s.logger, adminID, pgtype.UUID{}, audit.ActionTrackForget, map[string]any{"tracks": named})
return gone, nil
}
// MergeDuplicates merges a duplicate group into the copy to keep
// (library.MergeDuplicateGroup) and records the merge in the audit log.
func (s *Service) MergeDuplicates(
ctx context.Context, groupID, survivorID, actorID pgtype.UUID,
) (library.MergeResult, error) {
res, err := library.MergeDuplicateGroup(ctx, s.pool, s.logger, s.dataDir, groupID, survivorID)
if err != nil {
return res, err
}
removed := make([]map[string]string, 0, len(res.Removed))
for _, c := range res.Removed {
removed = append(removed, map[string]string{"track_id": syncpkg.FormatUUID(c.TrackID), "file_path": c.FilePath})
}
audit.WriteOrLog(ctx, s.pool, s.logger, actorID, pgtype.UUID{}, audit.ActionDuplicateMerge, map[string]any{
"group_id": syncpkg.FormatUUID(groupID),
"tier": res.Tier,
"survivor_track_id": syncpkg.FormatUUID(res.Survivor.TrackID),
"survivor_path": res.Survivor.FilePath,
"removed": removed,
"moved": map[string]any{
"play_events": res.PlayEvents, "skip_events": res.SkipEvents,
"likes": res.Likes, "playlist_entries": res.PlaylistEntries,
},
})
return res, nil
}