feat(lidarrquarantine): admin actions Resolve/DeleteFile/DeleteViaLidarr
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
// Package lidarrquarantine owns the per-user track quarantine workflow.
|
||||
// Users flag a track as broken (Flag/Unflag), the SPA hides the track
|
||||
// from their views, and admins resolve the resulting reports via the
|
||||
// Service's admin actions (Resolve / DeleteFile / DeleteViaLidarr —
|
||||
// the admin action methods are added in a follow-up task).
|
||||
// Service's admin actions (Resolve / DeleteFile / DeleteViaLidarr).
|
||||
package lidarrquarantine
|
||||
|
||||
import (
|
||||
@@ -17,6 +16,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarr"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/lidarrconfig"
|
||||
)
|
||||
@@ -179,3 +179,172 @@ func validReason(r string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Resolve clears all per-user quarantine rows for a track and writes an
|
||||
// audit log row. Idempotent — a track with no rows still writes an audit
|
||||
// entry with affected_users=0 (so admin can see "I clicked resolve on a
|
||||
// track that already had no reports").
|
||||
func (s *Service) Resolve(ctx context.Context, trackID, adminID pgtype.UUID) (dbq.LidarrQuarantineAction, error) {
|
||||
q := dbq.New(s.pool)
|
||||
track, err := q.GetTrackByID(ctx, trackID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return dbq.LidarrQuarantineAction{}, ErrTrackNotFound
|
||||
}
|
||||
return dbq.LidarrQuarantineAction{}, fmt.Errorf("get track: %w", err)
|
||||
}
|
||||
snap, err := s.snapshot(ctx, q, track)
|
||||
if err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, err
|
||||
}
|
||||
|
||||
affected, err := q.CountQuarantineForTrack(ctx, trackID)
|
||||
if err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, fmt.Errorf("count: %w", err)
|
||||
}
|
||||
if err := q.DeleteQuarantineForTrack(ctx, trackID); err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, fmt.Errorf("delete rows: %w", err)
|
||||
}
|
||||
return q.WriteQuarantineAction(ctx, dbq.WriteQuarantineActionParams{
|
||||
TrackID: trackID, TrackTitle: snap.TrackTitle, ArtistName: snap.ArtistName,
|
||||
AlbumTitle: snap.AlbumTitle, Action: dbq.LidarrQuarantineActionKindResolved,
|
||||
AdminID: adminID, LidarrAlbumMbid: snap.LidarrAlbumMBID, AffectedUsers: affected,
|
||||
})
|
||||
}
|
||||
|
||||
// quarantineSnapshot is the audit-row-ready snapshot of track + album +
|
||||
// artist text columns. Used by the three admin actions.
|
||||
type quarantineSnapshot struct {
|
||||
TrackTitle string
|
||||
ArtistName string
|
||||
AlbumTitle *string
|
||||
LidarrAlbumMBID *string
|
||||
}
|
||||
|
||||
// snapshot pulls the audit-row text columns from track / album / artist.
|
||||
// AlbumTitle is materialized as *string because WriteQuarantineActionParams
|
||||
// expects nullable; we coerce the schema's NOT NULL value into a pointer.
|
||||
func (s *Service) snapshot(ctx context.Context, q *dbq.Queries, track dbq.Track) (quarantineSnapshot, error) {
|
||||
album, err := q.GetAlbumByID(ctx, track.AlbumID)
|
||||
if err != nil {
|
||||
return quarantineSnapshot{}, fmt.Errorf("get album: %w", err)
|
||||
}
|
||||
artist, err := q.GetArtistByID(ctx, track.ArtistID)
|
||||
if err != nil {
|
||||
return quarantineSnapshot{}, fmt.Errorf("get artist: %w", err)
|
||||
}
|
||||
titlePtr := album.Title
|
||||
return quarantineSnapshot{
|
||||
TrackTitle: track.Title,
|
||||
ArtistName: artist.Name,
|
||||
AlbumTitle: &titlePtr,
|
||||
LidarrAlbumMBID: album.Mbid,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DeleteFile removes the track file from disk and the tracks row, then
|
||||
// (via FK ON DELETE CASCADE) clears all per-user quarantine rows for that
|
||||
// track and writes an audit row. If the file deletion fails, the per-user
|
||||
// rows stay so admin can retry. No partial state.
|
||||
func (s *Service) DeleteFile(ctx context.Context, trackID, adminID pgtype.UUID) (dbq.LidarrQuarantineAction, error) {
|
||||
q := dbq.New(s.pool)
|
||||
track, err := q.GetTrackByID(ctx, trackID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return dbq.LidarrQuarantineAction{}, ErrTrackNotFound
|
||||
}
|
||||
return dbq.LidarrQuarantineAction{}, fmt.Errorf("get track: %w", err)
|
||||
}
|
||||
snap, err := s.snapshot(ctx, q, track)
|
||||
if err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, err
|
||||
}
|
||||
|
||||
affected, err := q.CountQuarantineForTrack(ctx, trackID)
|
||||
if err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, fmt.Errorf("count: %w", err)
|
||||
}
|
||||
if err := library.DeleteTrackFile(ctx, s.pool, trackID); err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, fmt.Errorf("delete file: %w", err)
|
||||
}
|
||||
// tracks row is gone; ON DELETE CASCADE on lidarr_quarantine.track_id
|
||||
// already cleared the per-user rows. We don't call
|
||||
// DeleteQuarantineForTrack separately.
|
||||
return q.WriteQuarantineAction(ctx, dbq.WriteQuarantineActionParams{
|
||||
TrackID: trackID, TrackTitle: snap.TrackTitle, ArtistName: snap.ArtistName,
|
||||
AlbumTitle: snap.AlbumTitle, Action: dbq.LidarrQuarantineActionKindDeletedFile,
|
||||
AdminID: adminID, LidarrAlbumMbid: snap.LidarrAlbumMBID, AffectedUsers: affected,
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteViaLidarr is the destructive admin path: tells Lidarr to remove
|
||||
// the parent album with deleteFiles=true + addImportListExclusion=true,
|
||||
// then removes Minstrel rows for all tracks of that album. The cascade
|
||||
// on lidarr_quarantine.track_id clears per-user rows automatically.
|
||||
//
|
||||
// On Lidarr failure (unreachable, auth-failed, lookup-empty), nothing
|
||||
// changes locally. Admin retries.
|
||||
//
|
||||
// Returns the audit row + the count of tracks deleted from Minstrel.
|
||||
func (s *Service) DeleteViaLidarr(ctx context.Context, trackID, adminID pgtype.UUID) (dbq.LidarrQuarantineAction, int, error) {
|
||||
cfg, err := s.lidarrCfg.Get(ctx)
|
||||
if err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, 0, fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
client := s.clientFn()
|
||||
if !cfg.Enabled || client == nil {
|
||||
return dbq.LidarrQuarantineAction{}, 0, ErrLidarrDisabled
|
||||
}
|
||||
|
||||
q := dbq.New(s.pool)
|
||||
track, err := q.GetTrackByID(ctx, trackID)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return dbq.LidarrQuarantineAction{}, 0, ErrTrackNotFound
|
||||
}
|
||||
return dbq.LidarrQuarantineAction{}, 0, fmt.Errorf("get track: %w", err)
|
||||
}
|
||||
snap, err := s.snapshot(ctx, q, track)
|
||||
if err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, 0, err
|
||||
}
|
||||
if snap.LidarrAlbumMBID == nil || *snap.LidarrAlbumMBID == "" {
|
||||
return dbq.LidarrQuarantineAction{}, 0, ErrAlbumMBIDMissing
|
||||
}
|
||||
|
||||
affected, err := q.CountQuarantineForTrack(ctx, trackID)
|
||||
if err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, 0, fmt.Errorf("count: %w", err)
|
||||
}
|
||||
|
||||
// Lidarr lookup: MBID -> internal album ID.
|
||||
album, err := client.LookupAlbumByMBID(ctx, *snap.LidarrAlbumMBID)
|
||||
if err != nil {
|
||||
if errors.Is(err, lidarr.ErrNotFound) {
|
||||
return dbq.LidarrQuarantineAction{}, 0, ErrLidarrAlbumNotFound
|
||||
}
|
||||
return dbq.LidarrQuarantineAction{}, 0, fmt.Errorf("lidarr lookup: %w", err)
|
||||
}
|
||||
|
||||
// Lidarr DELETE — both flags true.
|
||||
if err := client.DeleteAlbum(ctx, album.ID, true, true); err != nil {
|
||||
return dbq.LidarrQuarantineAction{}, 0, fmt.Errorf("lidarr delete: %w", err)
|
||||
}
|
||||
|
||||
// Now remove the local rows. CASCADE on lidarr_quarantine.track_id
|
||||
// handles per-user rows.
|
||||
res, err := s.pool.Exec(ctx, "DELETE FROM tracks WHERE album_id = $1", track.AlbumID)
|
||||
if err != nil {
|
||||
// Lidarr is already done; we couldn't update locally. Operator-
|
||||
// recoverable: re-running picks up where we left off.
|
||||
return dbq.LidarrQuarantineAction{}, 0, fmt.Errorf("delete tracks: %w", err)
|
||||
}
|
||||
deletedCount := int(res.RowsAffected())
|
||||
|
||||
action, err := q.WriteQuarantineAction(ctx, dbq.WriteQuarantineActionParams{
|
||||
TrackID: trackID, TrackTitle: snap.TrackTitle, ArtistName: snap.ArtistName,
|
||||
AlbumTitle: snap.AlbumTitle, Action: dbq.LidarrQuarantineActionKindDeletedViaLidarr,
|
||||
AdminID: adminID, LidarrAlbumMbid: snap.LidarrAlbumMBID, AffectedUsers: affected,
|
||||
})
|
||||
return action, deletedCount, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user