feat(library): Missing files says why each track waits, and can forget or export it
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

- 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>
This commit is contained in:
2026-11-22 18:34:01 -05:00
co-authored by Claude Opus 5.5
parent 9462052625
commit 492eb2fb9a
16 changed files with 621 additions and 558 deletions
+20 -56
View File
@@ -1,25 +1,17 @@
// Package tracks owns the track-level admin actions behind DELETE
// /api/admin/tracks/{id} and the duplicate merge. RemoveTrack's destructive
// part goes through library.DeleteTrackFile — the one path that deletes a
// track file.
//
// No client calls the endpoint today; it is kept for an admin surface to
// rebind.
// 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"
"fmt"
"log/slog"
"github.com/jackc/pgx/v5"
"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/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
)
@@ -30,7 +22,7 @@ import (
// same pointer.
var ErrNotFound = apierror.ErrNotFound
// Service owns RemoveTrack and MergeDuplicates.
// Service owns ForgetTracks and MergeDuplicates.
type Service struct {
pool *pgxpool.Pool
logger *slog.Logger
@@ -47,55 +39,27 @@ func NewService(pool *pgxpool.Pool, logger *slog.Logger, dataDir string) *Servic
return &Service{pool: pool, logger: logger, dataDir: dataDir}
}
// RemoveTrack deletes the track's file and then its row, and tidies away an
// album or artist the delete empties.
//
// Returns:
// - deletedAlbumID: non-nil when removing the track left the album
// empty and the album row was deleted.
// - deletedArtistID: non-nil when both album AND artist were left
// empty (only set if deletedAlbumID is also set).
// - err: ErrNotFound, or a failure before anything was deleted. When the
// file cannot be removed it is a *library.FileRemoveError and NOTHING was
// deleted — see library.DeleteTrackFile for why that order is the contract.
//
// A removal is written to the audit log with adminID as the actor. The
// track's names are read before the delete, since the row, and perhaps its
// album and artist, are gone after it.
func (s *Service) RemoveTrack(
ctx context.Context, trackID, adminID pgtype.UUID,
) (deletedAlbumID *pgtype.UUID, deletedArtistID *pgtype.UUID, err error) {
q := dbq.New(s.pool)
track, err := q.GetTrackByID(ctx, trackID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil, ErrNotFound
}
return nil, nil, fmt.Errorf("get track: %w", err)
}
meta := map[string]any{
"track_id": syncpkg.FormatUUID(track.ID),
"title": track.Title,
"file_path": track.FilePath,
}
if artist, err := q.GetArtistByID(ctx, track.ArtistID); err == nil {
meta["artist"] = artist.Name
}
if album, err := q.GetAlbumByID(ctx, track.AlbumID); err == nil {
meta["album"] = album.Title
}
deleted, err := library.DeleteTrackFile(ctx, s.pool, s.logger, s.dataDir, trackID)
// 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, nil, ErrNotFound
return nil, ErrNotFound
}
return nil, nil, fmt.Errorf("delete track: %w", err)
return nil, err
}
meta["album_deleted"] = deleted.AlbumID != nil
meta["artist_deleted"] = deleted.ArtistID != nil
audit.WriteOrLog(ctx, s.pool, s.logger, adminID, pgtype.UUID{}, audit.ActionTrackRemove, meta)
return deleted.AlbumID, deleted.ArtistID, nil
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