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