feat(library): removing a bad file keeps its track, waiting for a good copy
release / go (push) Successful in 2m6s
release / web (push) Successful in 1m15s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m46s
release / android (push) Successful in 4m47s
release / Build signed APK (push) Successful in 5m38s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 31s
release / Verify release artifacts (tag releases only) (push) Skipped

Reports, Duplicates and Suspect sources now end the same way: the file
goes and the track stays, marked missing with its likes, plays and
playlist places, until a good copy lands in the library and move
adoption or missing-pair folding brings it back.

- library.RemoveFile removes one track's file and marks the track
  missing with the queue it was removed from and who removed it
  (tracks.removed_reason / removed_by, migration 0104). The duplicate
  resolver's wrong-slot removal shares the same marking.
- POST /api/admin/quarantine/{id}/remove-file removes a reported file,
  clears the listeners' reports, marks open playback errors deleted and
  records removed_file in the Reports history. A wrong-file or
  wrong-tags report drops the file's recording MBID.
- POST /api/admin/library/suspect-sources/{id}/remove removes a video
  rip's file and its MBID, and writes a suspect_file_remove audit row.
- The removal reason clears with the missing mark when a file returns.

Grimoire #7743 (milestone 635).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 18:17:26 -05:00
co-authored by Claude Opus 5.5
parent e5f11647ee
commit 9462052625
21 changed files with 694 additions and 28 deletions
+42
View File
@@ -156,6 +156,48 @@ func (h *handlers) handleDeleteQuarantineFile(w http.ResponseWriter, r *http.Req
})
}
// handleRemoveQuarantineFile implements POST
// /api/admin/quarantine/{track_id}/remove-file: the reported file goes and the
// track stays, missing, until a good copy lands in the library. Its reports
// and open playback errors are settled with it.
//
// Errors: 404 track_not_found; 409 library_not_writable / 500
// file_delete_failed when the file could not be removed.
func (h *handlers) handleRemoveQuarantineFile(w http.ResponseWriter, r *http.Request) {
admin, ok := auth.UserFromContext(r.Context())
if !ok {
writeAdminJSONErr(w, http.StatusUnauthorized, "unauthorized")
return
}
id, ok := parseUUID(chi.URLParam(r, "track_id"))
if !ok {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
return
}
action, err := h.quarantine.RemoveFile(r.Context(), id, admin.ID)
if err != nil {
if apiErr, ok := fileRemoveAPIError(err); ok {
logFileRemoveFailure(h.logger, apiErr, "track_id", uuidToString(id))
writeErr(w, apiErr)
return
}
switch {
case errors.Is(err, quarantine.ErrTrackNotFound):
writeAdminJSONErr(w, http.StatusNotFound, "track_not_found")
default:
h.logger.Error("admin: remove reported file", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "file_delete_failed")
}
return
}
// Broadcast: every listener's report on it is cleared.
h.publishQuarantineEvent("quarantine.resolved", pgtype.UUID{}, id, true)
writeJSON(w, http.StatusOK, actionResultView{
ActionID: uuidToString(action.ID),
AffectedUsers: action.AffectedUsers,
})
}
// actionLogView is the row shape returned by GET /api/admin/quarantine/actions.
type actionLogView struct {
ID string `json:"id"`
+55
View File
@@ -2,11 +2,15 @@ package api
import (
"encoding/json"
"errors"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/audit"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
)
@@ -159,3 +163,54 @@ func (h *handlers) handleSetSuspectSourceVerdict(w http.ResponseWriter, r *http.
}
writeJSON(w, http.StatusOK, map[string]string{"verdict": req.Verdict})
}
// handleRemoveSuspectSourceFile implements POST
// /api/admin/library/suspect-sources/{id}/remove: the rip's file goes and the
// track stays, missing, until a clean copy lands in the library. Its
// recording MBID goes with the file, since a video rip is often another
// recording of the song (a live take, a video edit).
//
// Errors: 404 suspect_track_not_found when the track is gone, missing, or its
// file name carries no video-title marker; 409 library_not_writable / 500
// file_delete_failed when the file could not be removed.
func (h *handlers) handleRemoveSuspectSourceFile(w http.ResponseWriter, r *http.Request) {
admin, ok := requireUser(w, r)
if !ok {
return
}
id, ok := parseUUID(chi.URLParam(r, "id"))
if !ok {
writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id")
return
}
track, err := dbq.New(h.pool).GetTrackByID(r.Context(), id)
if errors.Is(err, pgx.ErrNoRows) || (err == nil && (track.MissingSince.Valid || len(library.SourceMarkersFor(track.FilePath)) == 0)) {
writeAdminJSONErr(w, http.StatusNotFound, "suspect_track_not_found")
return
}
if err != nil {
h.logger.Error("admin: read suspect track", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "server_error")
return
}
if _, err := library.RemoveFile(r.Context(), h.pool, h.logger, library.RemoveFileParams{
TrackID: id, ActorID: admin.ID, Reason: library.RemovedForSuspect, ForgetMBID: true,
}); err != nil {
if apiErr, ok := fileRemoveAPIError(err); ok {
logFileRemoveFailure(h.logger, apiErr, "track_id", uuidToString(id))
writeErr(w, apiErr)
return
}
if errors.Is(err, library.ErrTrackNotFound) {
writeAdminJSONErr(w, http.StatusNotFound, "suspect_track_not_found")
return
}
h.logger.Error("admin: remove suspect file", "err", err)
writeAdminJSONErr(w, http.StatusInternalServerError, "file_delete_failed")
return
}
audit.WriteOrLog(r.Context(), h.pool, h.logger, admin.ID, pgtype.UUID{}, audit.ActionSuspectFileRemove, map[string]any{
"track_id": uuidToString(id), "title": track.Title, "file_path": track.FilePath,
})
w.WriteHeader(http.StatusNoContent)
}
@@ -67,3 +67,41 @@ func TestHandleListSuspectSources_Integration(t *testing.T) {
}
}
}
// Removing a suspect source keeps its track, missing, and only a track whose
// file name carries a video-title marker can be removed this way.
func TestHandleRemoveSuspectSourceFile_Integration(t *testing.T) {
h, pool := testHandlers(t)
truncateLibrary(t, pool)
admin := seedUser(t, pool, "suspect-remover", "pw", true)
artist := seedArtist(t, pool, "Gorillaz")
humanz := seedAlbum(t, pool, artist.ID, "Humanz", 2017)
rip := seedTrack(t, pool, humanz.ID, artist.ID, "Saturnz Barz (Official Video)", 1, 180_000)
clean := seedTrack(t, pool, humanz.ID, artist.ID, "Chain Reaction", 4, 190_000)
remove := func(id string) int {
req := httptest.NewRequest(http.MethodPost, "/api/admin/library/suspect-sources/"+id+"/remove", nil)
req = withURLParam(withUser(req, admin), "id", id)
w := httptest.NewRecorder()
h.handleRemoveSuspectSourceFile(w, req)
return w.Code
}
if code := remove(uuidToString(clean.ID)); code != http.StatusNotFound {
t.Errorf("removing a clean file's track: status %d, want 404", code)
}
if code := remove(uuidToString(rip.ID)); code != http.StatusNoContent {
t.Fatalf("removing the rip: status %d, want 204", code)
}
var reason *string
var missing bool
if err := pool.QueryRow(context.Background(),
`SELECT removed_reason, missing_since IS NOT NULL FROM tracks WHERE id = $1`, rip.ID).Scan(&reason, &missing); err != nil {
t.Fatalf("the rip's track went with its file: %v", err)
}
if !missing || reason == nil || *reason != "suspect" {
t.Errorf("rip's track = missing %v, reason %v; want missing for a suspect source", missing, reason)
}
if code := remove(uuidToString(rip.ID)); code != http.StatusNotFound {
t.Errorf("removing it again: status %d, want 404 for a track already waiting", code)
}
}
+2
View File
@@ -202,6 +202,7 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev
admin.Get("/playback-errors", h.handleListAdminPlaybackErrors)
admin.Post("/playback-errors/{id}/resolve", h.handleResolvePlaybackError)
admin.Post("/quarantine/{track_id}/delete-file", h.handleDeleteQuarantineFile)
admin.Post("/quarantine/{track_id}/remove-file", h.handleRemoveQuarantineFile)
admin.Get("/quarantine/actions", h.handleListQuarantineActions)
admin.Delete("/tracks/{id}", h.handleRemoveTrack)
@@ -226,6 +227,7 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev
admin.Get("/library/missing", h.handleListMissingTracks)
admin.Get("/library/suspect-sources", h.handleListSuspectSources)
admin.Put("/library/suspect-sources/{id}", h.handleSetSuspectSourceVerdict)
admin.Post("/library/suspect-sources/{id}/remove", h.handleRemoveSuspectSourceFile)
admin.Get("/library/coverage", h.handleGetLibraryCoverage)
admin.Get("/library/fingerprints", h.handleGetFingerprintCoverage)
+5
View File
@@ -67,6 +67,11 @@ const (
// the track and its file, so the log can say what went after the row is
// gone.
ActionTrackRemove Action = "track_remove"
// An admin removing a suspect source's file and keeping its track, which
// waits, missing, for a clean copy. (A report's removal is recorded in
// the Reports history instead, and a duplicate's under
// duplicate_wrong_file.)
ActionSuspectFileRemove Action = "suspect_file_remove"
// Subsonic password: generated in Settings for t/s-only clients.
ActionSubsonicPasswordSet Action = "subsonic_password_set"
+3 -1
View File
@@ -261,7 +261,7 @@ func (q *Queries) InsertSkipEvent(ctx context.Context, arg InsertSkipEventParams
}
const listRecentSessionTracks = `-- name: ListRecentSessionTracks :many
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at FROM tracks t
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by FROM tracks t
JOIN play_events pe ON pe.track_id = t.id
WHERE pe.session_id = $1
AND pe.started_at < $2
@@ -313,6 +313,8 @@ func (q *Queries) ListRecentSessionTracks(ctx context.Context, arg ListRecentSes
&i.SourceVerdict,
&i.SourceVerdictAt,
&i.EmptiedAt,
&i.RemovedReason,
&i.RemovedBy,
); err != nil {
return nil, err
}
+3 -1
View File
@@ -14,7 +14,7 @@ import (
const listUserHistory = `-- name: ListUserHistory :many
SELECT pe.id AS event_id,
pe.started_at,
t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at,
t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by,
albums.title AS album_title,
artists.name AS artist_name
FROM play_events pe
@@ -87,6 +87,8 @@ func (q *Queries) ListUserHistory(ctx context.Context, arg ListUserHistoryParams
&i.Track.SourceVerdict,
&i.Track.SourceVerdictAt,
&i.Track.EmptiedAt,
&i.Track.RemovedReason,
&i.Track.RemovedBy,
&i.AlbumTitle,
&i.ArtistName,
); err != nil {
+3 -1
View File
@@ -282,7 +282,7 @@ func (q *Queries) ListLikedTrackIDs(ctx context.Context, userID pgtype.UUID) ([]
}
const listLikedTrackRows = `-- name: ListLikedTrackRows :many
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at FROM tracks t
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by FROM tracks t
JOIN (SELECT DISTINCT ON (lt.song_key) l.track_id, l.liked_at
FROM general_likes l
JOIN tracks lt ON lt.id = l.track_id
@@ -337,6 +337,8 @@ func (q *Queries) ListLikedTrackRows(ctx context.Context, arg ListLikedTrackRows
&i.SourceVerdict,
&i.SourceVerdictAt,
&i.EmptiedAt,
&i.RemovedReason,
&i.RemovedBy,
); err != nil {
return nil, err
}
+3
View File
@@ -17,6 +17,7 @@ const (
QuarantineActionKindResolved QuarantineActionKind = "resolved"
QuarantineActionKindDeletedFile QuarantineActionKind = "deleted_file"
QuarantineActionKindDeletedAlbum QuarantineActionKind = "deleted_album"
QuarantineActionKindRemovedFile QuarantineActionKind = "removed_file"
)
func (e *QuarantineActionKind) Scan(src interface{}) error {
@@ -971,6 +972,8 @@ type Track struct {
SourceVerdict *string
SourceVerdictAt pgtype.Timestamptz
EmptiedAt pgtype.Timestamptz
RemovedReason *string
RemovedBy pgtype.UUID
}
type TrackAcoustidLookup struct {
+12 -4
View File
@@ -208,7 +208,7 @@ WITH plays AS (
WHERE user_id = $2 AND was_skipped = false
GROUP BY track_id
)
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at,
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by,
albums.title AS album_title,
artists.name AS artist_name
FROM plays p
@@ -276,6 +276,8 @@ func (q *Queries) ListMostPlayedTracksForArtist(ctx context.Context, arg ListMos
&i.Track.SourceVerdict,
&i.Track.SourceVerdictAt,
&i.Track.EmptiedAt,
&i.Track.RemovedReason,
&i.Track.RemovedBy,
&i.AlbumTitle,
&i.ArtistName,
); err != nil {
@@ -296,7 +298,7 @@ WITH plays AS (
WHERE user_id = $1 AND was_skipped = false
GROUP BY track_id
)
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at,
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by,
albums.title AS album_title,
artists.name AS artist_name
FROM plays p
@@ -366,6 +368,8 @@ func (q *Queries) ListMostPlayedTracksForUser(ctx context.Context, arg ListMostP
&i.Track.SourceVerdict,
&i.Track.SourceVerdictAt,
&i.Track.EmptiedAt,
&i.Track.RemovedReason,
&i.Track.RemovedBy,
&i.AlbumTitle,
&i.ArtistName,
); err != nil {
@@ -707,7 +711,7 @@ func (q *Queries) ListRediscoverArtistsForUser(ctx context.Context, arg ListRedi
const loadRadioCandidates = `-- name: LoadRadioCandidates :many
SELECT
t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at,
t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by,
(l.user_id IS NOT NULL)::bool AS is_liked,
pe.last_played_at::timestamptz AS last_played_at,
pe.play_count,
@@ -794,6 +798,8 @@ func (q *Queries) LoadRadioCandidates(ctx context.Context, arg LoadRadioCandidat
&i.Track.SourceVerdict,
&i.Track.SourceVerdictAt,
&i.Track.EmptiedAt,
&i.Track.RemovedReason,
&i.Track.RemovedBy,
&i.IsLiked,
&i.LastPlayedAt,
&i.PlayCount,
@@ -954,7 +960,7 @@ random_fill AS (
LIMIT $9
)
SELECT
t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at,
t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by,
(l.user_id IS NOT NULL)::bool AS is_liked,
pe.last_played_at::timestamptz AS last_played_at,
pe.play_count,
@@ -1098,6 +1104,8 @@ func (q *Queries) LoadRadioCandidatesV2(ctx context.Context, arg LoadRadioCandid
&i.Track.SourceVerdict,
&i.Track.SourceVerdictAt,
&i.Track.EmptiedAt,
&i.Track.RemovedReason,
&i.Track.RemovedBy,
&i.IsLiked,
&i.LastPlayedAt,
&i.PlayCount,
+58 -10
View File
@@ -14,7 +14,9 @@ import (
const adoptTrackPath = `-- name: AdoptTrackPath :execrows
UPDATE tracks
SET file_path = $1,
missing_since = NULL
missing_since = NULL,
removed_reason = NULL,
removed_by = NULL
WHERE id = $2
AND missing_since IS NOT NULL
`
@@ -31,6 +33,7 @@ type AdoptTrackPathParams struct {
//
// `missing_since IS NOT NULL` again, this time as a race guard: two files can't
// both adopt the same row, and :execrows reports 0 to whichever loses.
// A removal's reason goes with the mark: the track has its file again.
func (q *Queries) AdoptTrackPath(ctx context.Context, arg AdoptTrackPathParams) (int64, error) {
result, err := q.db.Exec(ctx, adoptTrackPath, arg.FilePath, arg.ID)
if err != nil {
@@ -74,7 +77,9 @@ func (q *Queries) ClearTrackMbids(ctx context.Context, ids []pgtype.UUID) error
const clearTracksMissing = `-- name: ClearTracksMissing :execrows
UPDATE tracks
SET missing_since = NULL
SET missing_since = NULL,
removed_reason = NULL,
removed_by = NULL
WHERE id = ANY($1::uuid[])
AND missing_since IS NOT NULL
`
@@ -299,7 +304,7 @@ func (q *Queries) FlagSuspectSourceTracks(ctx context.Context, pattern string) (
}
const getTrackByID = `-- name: GetTrackByID :one
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at FROM tracks WHERE id = $1
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at, removed_reason, removed_by FROM tracks WHERE id = $1
`
func (q *Queries) GetTrackByID(ctx context.Context, id pgtype.UUID) (Track, error) {
@@ -331,12 +336,14 @@ func (q *Queries) GetTrackByID(ctx context.Context, id pgtype.UUID) (Track, erro
&i.SourceVerdict,
&i.SourceVerdictAt,
&i.EmptiedAt,
&i.RemovedReason,
&i.RemovedBy,
)
return i, err
}
const getTrackByPath = `-- name: GetTrackByPath :one
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at FROM tracks WHERE file_path = $1
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at, removed_reason, removed_by FROM tracks WHERE file_path = $1
`
func (q *Queries) GetTrackByPath(ctx context.Context, filePath string) (Track, error) {
@@ -368,12 +375,14 @@ func (q *Queries) GetTrackByPath(ctx context.Context, filePath string) (Track, e
&i.SourceVerdict,
&i.SourceVerdictAt,
&i.EmptiedAt,
&i.RemovedReason,
&i.RemovedBy,
)
return i, err
}
const getTracksByIDs = `-- name: GetTracksByIDs :many
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at FROM tracks WHERE id = ANY($1::uuid[])
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at, removed_reason, removed_by FROM tracks WHERE id = ANY($1::uuid[])
`
// Batched lookup used by /api/library/sync to hydrate upsert payloads
@@ -413,6 +422,8 @@ func (q *Queries) GetTracksByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([
&i.SourceVerdict,
&i.SourceVerdictAt,
&i.EmptiedAt,
&i.RemovedReason,
&i.RemovedBy,
); err != nil {
return nil, err
}
@@ -425,7 +436,7 @@ func (q *Queries) GetTracksByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([
}
const listArtistTracksForUser = `-- name: ListArtistTracksForUser :many
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at,
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by,
albums.title AS album_title,
artists.name AS artist_name
FROM tracks t
@@ -491,6 +502,8 @@ func (q *Queries) ListArtistTracksForUser(ctx context.Context, arg ListArtistTra
&i.Track.SourceVerdict,
&i.Track.SourceVerdictAt,
&i.Track.EmptiedAt,
&i.Track.RemovedReason,
&i.Track.RemovedBy,
&i.AlbumTitle,
&i.ArtistName,
); err != nil {
@@ -590,7 +603,7 @@ func (q *Queries) ListMissingTracks(ctx context.Context, arg ListMissingTracksPa
}
const listRandomTracksForUser = `-- name: ListRandomTracksForUser :many
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at,
SELECT t.id, t.title, t.album_id, t.artist_id, t.track_number, t.disc_number, t.duration_ms, t.file_path, t.file_size, t.file_format, t.bitrate, t.mbid, t.genre, t.added_at, t.updated_at, t.tag_source, t.tag_sources_version, t.tag_read_version, t.missing_since, t.mbid_source, t.song_id, t.song_key, t.source_verdict, t.source_verdict_at, t.emptied_at, t.removed_reason, t.removed_by,
albums.title AS album_title,
artists.name AS artist_name
FROM tracks t
@@ -653,6 +666,8 @@ func (q *Queries) ListRandomTracksForUser(ctx context.Context, arg ListRandomTra
&i.Track.SourceVerdict,
&i.Track.SourceVerdictAt,
&i.Track.EmptiedAt,
&i.Track.RemovedReason,
&i.Track.RemovedBy,
&i.AlbumTitle,
&i.ArtistName,
); err != nil {
@@ -828,7 +843,7 @@ func (q *Queries) ListTrackPathsForReconcile(ctx context.Context) ([]ListTrackPa
}
const listTracksByAlbum = `-- name: ListTracksByAlbum :many
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at FROM tracks
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at, removed_reason, removed_by FROM tracks
WHERE album_id = $1
AND NOT EXISTS (
SELECT 1 FROM quarantine q
@@ -880,6 +895,8 @@ func (q *Queries) ListTracksByAlbum(ctx context.Context, arg ListTracksByAlbumPa
&i.SourceVerdict,
&i.SourceVerdictAt,
&i.EmptiedAt,
&i.RemovedReason,
&i.RemovedBy,
); err != nil {
return nil, err
}
@@ -967,8 +984,35 @@ func (q *Queries) MarkTracksMissing(ctx context.Context, ids []pgtype.UUID) (int
return result.RowsAffected(), nil
}
const markTracksRemoved = `-- name: MarkTracksRemoved :execrows
UPDATE tracks
SET missing_since = COALESCE(missing_since, now()),
removed_reason = $1::text,
removed_by = $2::uuid
WHERE id = ANY($3::uuid[])
`
type MarkTracksRemovedParams struct {
Reason string
RemovedBy pgtype.UUID
Ids []pgtype.UUID
}
// Marks rows whose file an admin, or the duplicate resolver, removed on
// purpose: missing, like a file the walk did not see, with the queue it was
// removed from and who removed it (NULL for the resolver). The row keeps its
// likes, plays and playlist places, and a good copy that lands later is
// adopted or folded into it. A row already missing keeps the time it went.
func (q *Queries) MarkTracksRemoved(ctx context.Context, arg MarkTracksRemovedParams) (int64, error) {
result, err := q.db.Exec(ctx, markTracksRemoved, arg.Reason, arg.RemovedBy, arg.Ids)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const searchTracks = `-- name: SearchTracks :many
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at FROM tracks
SELECT id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at, removed_reason, removed_by FROM tracks
WHERE title ILIKE '%' || $1::text || '%'
AND NOT EXISTS (
SELECT 1 FROM quarantine q
@@ -1027,6 +1071,8 @@ func (q *Queries) SearchTracks(ctx context.Context, arg SearchTracksParams) ([]T
&i.SourceVerdict,
&i.SourceVerdictAt,
&i.EmptiedAt,
&i.RemovedReason,
&i.RemovedBy,
); err != nil {
return nil, err
}
@@ -1105,7 +1151,7 @@ ON CONFLICT (file_path) DO UPDATE SET
-- next scan can short-circuit them again.
tag_read_version = EXCLUDED.tag_read_version,
updated_at = now()
RETURNING id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at
RETURNING id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path, file_size, file_format, bitrate, mbid, genre, added_at, updated_at, tag_source, tag_sources_version, tag_read_version, missing_since, mbid_source, song_id, song_key, source_verdict, source_verdict_at, emptied_at, removed_reason, removed_by
`
type UpsertTrackParams struct {
@@ -1170,6 +1216,8 @@ func (q *Queries) UpsertTrack(ctx context.Context, arg UpsertTrackParams) (Track
&i.SourceVerdict,
&i.SourceVerdictAt,
&i.EmptiedAt,
&i.RemovedReason,
&i.RemovedBy,
)
return i, err
}
@@ -0,0 +1,9 @@
-- Postgres cannot drop an enum value, so removed_file stays in
-- quarantine_action_kind; its history rows read as deleted_file again.
UPDATE quarantine_actions SET action = 'deleted_file' WHERE action = 'removed_file';
ALTER TABLE tracks
DROP CONSTRAINT IF EXISTS tracks_removed_only_missing,
DROP CONSTRAINT IF EXISTS tracks_removed_reason_check,
DROP COLUMN IF EXISTS removed_by,
DROP COLUMN IF EXISTS removed_reason;
@@ -0,0 +1,22 @@
-- 0104_removed_tracks.up.sql — a track whose file an admin removed waits for a
-- good copy, and says why.
--
-- Removing a bad file from Reports, Duplicates or Suspect sources keeps the
-- track: it is marked missing with its likes, plays and playlist places, and
-- a good copy that lands in the library later is adopted or folded into it.
-- removed_reason says which queue it was removed from and removed_by who did
-- it (NULL when the duplicate resolver did it on its own). Both describe the
-- current missing spell only, so they are cleared with the missing mark, and
-- the CHECK keeps them off a present track.
--
-- removed_file is the Reports history's name for that removal, beside
-- deleted_file, which removes the track as well.
ALTER TABLE tracks
ADD COLUMN removed_reason text,
ADD COLUMN removed_by uuid REFERENCES users(id) ON DELETE SET NULL,
ADD CONSTRAINT tracks_removed_reason_check
CHECK (removed_reason IN ('report', 'duplicate', 'suspect')),
ADD CONSTRAINT tracks_removed_only_missing
CHECK (missing_since IS NOT NULL OR (removed_reason IS NULL AND removed_by IS NULL));
ALTER TYPE quarantine_action_kind ADD VALUE IF NOT EXISTS 'removed_file';
+19 -2
View File
@@ -188,9 +188,12 @@ SELECT t.id, t.file_path
--
-- `missing_since IS NOT NULL` again, this time as a race guard: two files can't
-- both adopt the same row, and :execrows reports 0 to whichever loses.
-- A removal's reason goes with the mark: the track has its file again.
UPDATE tracks
SET file_path = sqlc.arg(file_path),
missing_since = NULL
missing_since = NULL,
removed_reason = NULL,
removed_by = NULL
WHERE id = sqlc.arg(id)
AND missing_since IS NOT NULL;
@@ -216,6 +219,18 @@ UPDATE tracks
WHERE id = ANY(sqlc.arg(ids)::uuid[])
AND missing_since IS NULL;
-- name: MarkTracksRemoved :execrows
-- Marks rows whose file an admin, or the duplicate resolver, removed on
-- purpose: missing, like a file the walk did not see, with the queue it was
-- removed from and who removed it (NULL for the resolver). The row keeps its
-- likes, plays and playlist places, and a good copy that lands later is
-- adopted or folded into it. A row already missing keeps the time it went.
UPDATE tracks
SET missing_since = COALESCE(missing_since, now()),
removed_reason = sqlc.arg(reason)::text,
removed_by = sqlc.narg(removed_by)::uuid
WHERE id = ANY(sqlc.arg(ids)::uuid[]);
-- name: ClearTrackMbids :exec
-- Forgets the recording MBID of rows whose file was another song's (the
-- duplicate resolver's wrong-slot removal). The MBID came from that file, so it
@@ -240,7 +255,9 @@ UPDATE tracks
-- skip check, so a file that reappears unchanged is un-marked even though the
-- scanner skips re-reading its tags.
UPDATE tracks
SET missing_since = NULL
SET missing_since = NULL,
removed_reason = NULL,
removed_by = NULL
WHERE id = ANY(sqlc.arg(ids)::uuid[])
AND missing_since IS NOT NULL;
+4 -3
View File
@@ -78,9 +78,10 @@ type DeletedTrack struct {
}
// DeleteTrackFile removes a track's file from disk and then its row, tidying
// away an album or artist the delete leaves empty. It is the ONLY path that
// deletes a track file: the admin remove-track endpoint and quarantine's Delete
// file both come through here.
// away an album or artist the delete leaves empty. It is the only path that
// deletes a track with its file: the admin remove-track endpoint and Reports'
// "Remove from library" both come through here. RemoveFile is the other
// path that deletes a file, and it keeps the track.
//
// Order is the whole contract. The file goes first, and if it cannot go — a
// read-only mount, a permission denial, an I/O error — nothing else happens and
+2 -5
View File
@@ -503,8 +503,8 @@ func removeWrongSlots(
}
err := pgx.BeginFunc(ctx, pool, func(tx pgx.Tx) error {
tq := dbq.New(tx)
if _, err := tq.MarkTracksMissing(ctx, gone); err != nil {
return fmt.Errorf("mark missing: %w", err)
if err := markRemoved(ctx, tq, gone, RemovedForDuplicate, actorID); err != nil {
return err
}
if err := tq.ClearTrackMbids(ctx, gone); err != nil {
return fmt.Errorf("clear mbids: %w", err)
@@ -512,9 +512,6 @@ func removeWrongSlots(
if err := tq.MarkTracksEmptied(ctx, emptied); err != nil {
return fmt.Errorf("mark emptied: %w", err)
}
if err := logTrackChanges(ctx, tq, gone); err != nil {
return fmt.Errorf("log changes: %w", err)
}
if removeErr != nil {
return nil
}
@@ -82,6 +82,14 @@ func TestSettleWrongSlot_GetItAgainRemovesEveryCopy_Integration(t *testing.T) {
if !emptiedTrack(t, f, id) {
t.Errorf("track %s is not marked missing with its slot emptied", syncpkg.FormatUUID(id))
}
got, err := dbq.New(f.pool).GetTrackByID(ctx, id)
if err != nil {
t.Fatal(err)
}
if got.RemovedReason == nil || *got.RemovedReason != string(RemovedForDuplicate) || got.RemovedBy != op.ID {
t.Errorf("track %s waits with reason %v by %v, want duplicate by the admin",
syncpkg.FormatUUID(id), got.RemovedReason, got.RemovedBy)
}
}
settledByHand(t, f, op)
+104
View File
@@ -0,0 +1,104 @@
package library
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/db/dbq"
)
// RemovedFor is the Collection queue a track's file was removed from. Every
// queue ends the same way: the bad file goes and the track waits, missing,
// for a good copy (tracks.removed_reason).
type RemovedFor string
const (
RemovedForReport RemovedFor = "report"
RemovedForDuplicate RemovedFor = "duplicate"
RemovedForSuspect RemovedFor = "suspect"
)
// RemoveFileParams is one track whose file an admin is removing.
type RemoveFileParams struct {
TrackID pgtype.UUID
// ActorID is the admin removing it.
ActorID pgtype.UUID
Reason RemovedFor
// ForgetMBID drops the track's recording MBID. Set it when the file was
// not the track's song, or its tags are not to be trusted: the MBID came
// from that file, and a good copy carrying the real song's MBID must not
// be kept from folding in by it, nor the slot folded into another song.
ForgetMBID bool
// Within runs on the same transaction as the marking, for what the
// caller's queue records about the removal (a report's history row). An
// error from it undoes the marking, though not the removed file.
Within func(ctx context.Context, tq *dbq.Queries) error
}
// RemoveFile removes a track's file from disk and keeps the track: it is
// marked missing, with its likes, plays and playlist places, and the queue it
// was removed from. A good copy that lands in the library later is adopted
// into it (moved.go) or folded into it (missing_pairs.go), so the track comes
// back with its history.
//
// The file goes first. If it cannot, nothing is marked and a
// *FileRemoveError comes back. A file that is already gone is not a failure.
func RemoveFile(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, p RemoveFileParams) (dbq.Track, error) {
if logger == nil {
logger = slog.Default()
}
q := dbq.New(pool)
track, err := q.GetTrackByID(ctx, p.TrackID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return dbq.Track{}, ErrTrackNotFound
}
return dbq.Track{}, fmt.Errorf("get track: %w", err)
}
if err := removeTrackFileOnDisk(track.FilePath); err != nil {
return dbq.Track{}, err
}
err = pgx.BeginFunc(ctx, pool, func(tx pgx.Tx) error {
tq := dbq.New(tx)
ids := []pgtype.UUID{p.TrackID}
if err := markRemoved(ctx, tq, ids, p.Reason, p.ActorID); err != nil {
return err
}
if p.ForgetMBID {
if err := tq.ClearTrackMbids(ctx, ids); err != nil {
return fmt.Errorf("clear mbid: %w", err)
}
}
if p.Within != nil {
return p.Within(ctx, tq)
}
return nil
})
if err != nil {
return dbq.Track{}, err
}
logger.Info("library: removed a file and kept its track",
"track_id", track.ID.String(), "file_path", track.FilePath, "reason", string(p.Reason))
return track, nil
}
// markRemoved marks tracks whose files were removed on purpose, and tells
// clients they are no longer playable. actorID is invalid when the
// duplicate resolver removed them on its own.
func markRemoved(ctx context.Context, tq *dbq.Queries, ids []pgtype.UUID, reason RemovedFor, actorID pgtype.UUID) error {
if _, err := tq.MarkTracksRemoved(ctx, dbq.MarkTracksRemovedParams{
Reason: string(reason), RemovedBy: actorID, Ids: ids,
}); err != nil {
return fmt.Errorf("mark removed: %w", err)
}
if err := logTrackChanges(ctx, tq, ids); err != nil {
return fmt.Errorf("log changes: %w", err)
}
return nil
}
+164
View File
@@ -0,0 +1,164 @@
package library
import (
"context"
"errors"
"io"
"log/slog"
"os"
"path/filepath"
"syscall"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
)
// A removed file's track stays, missing, with its likes and plays and the
// reason it waits, and the good copy that lands later takes its history over.
func TestRemoveFile_KeepsTheTrackUntilAGoodCopyLands(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
q := dbq.New(pool)
admin, err := q.CreateUser(ctx, dbq.CreateUserParams{
Username: dbtest.TestUserPrefix + "remover", PasswordHash: "x", ApiTokenHash: "remover", IsAdmin: true,
})
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
bad := filepath.Join(dir, "01 bad.mp3")
if err := os.WriteFile(bad, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
track, album, artist := seedTrack(t, pool, bad)
mbid := "rec-kept"
if _, err := pool.Exec(ctx, `UPDATE tracks SET mbid = $2, mbid_source = 'tag' WHERE id = $1`, track.ID, mbid); err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(ctx, `INSERT INTO general_likes (user_id, track_id) VALUES ($1, $2)`, admin.ID, track.ID); err != nil {
t.Fatal(err)
}
if _, err := RemoveFile(ctx, pool, slog.New(slog.NewTextHandler(io.Discard, nil)), RemoveFileParams{
TrackID: track.ID, ActorID: admin.ID, Reason: RemovedForReport,
}); err != nil {
t.Fatalf("RemoveFile: %v", err)
}
if _, err := os.Stat(bad); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("the file is still there: %v", err)
}
waiting, err := q.GetTrackByID(ctx, track.ID)
if err != nil {
t.Fatalf("the track went with its file: %v", err)
}
if !waiting.MissingSince.Valid || waiting.RemovedReason == nil || *waiting.RemovedReason != "report" ||
waiting.RemovedBy != admin.ID {
t.Fatalf("waiting track = missing %v, reason %v, by %v; want missing, report, the admin",
waiting.MissingSince.Valid, waiting.RemovedReason, waiting.RemovedBy)
}
if waiting.Mbid == nil || *waiting.Mbid != mbid {
t.Fatalf("mbid = %v, want %q kept for a report that does not doubt it", waiting.Mbid, mbid)
}
// A good copy lands on the album under the song's title.
good, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: track.Title, AlbumID: album.ID, ArtistID: artist.ID,
DurationMs: 1000, FilePath: filepath.Join(dir, "01 good.flac"), FileSize: 100, FileFormat: "flac",
})
if err != nil {
t.Fatal(err)
}
if _, err := PairMissingTracks(ctx, pool, nil); err != nil {
t.Fatalf("PairMissingTracks: %v", err)
}
var likes int
if err := pool.QueryRow(ctx, `SELECT count(*) FROM general_likes WHERE track_id = $1`, good.ID).Scan(&likes); err != nil {
t.Fatal(err)
}
if likes != 1 {
t.Fatalf("the good copy has %d likes, want the removed track's one", likes)
}
}
// ForgetMBID drops the recording MBID that came from a file that was not the
// track's song.
func TestRemoveFile_ForgetsADoubtedMBID(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
path := filepath.Join(t.TempDir(), "rip.mp3")
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
track, _, _ := seedTrack(t, pool, path)
if _, err := pool.Exec(ctx, `UPDATE tracks SET mbid = 'rec-wrong', mbid_source = 'tag' WHERE id = $1`, track.ID); err != nil {
t.Fatal(err)
}
if _, err := RemoveFile(ctx, pool, nil, RemoveFileParams{
TrackID: track.ID, Reason: RemovedForSuspect, ForgetMBID: true,
}); err != nil {
t.Fatalf("RemoveFile: %v", err)
}
got, err := dbq.New(pool).GetTrackByID(ctx, track.ID)
if err != nil {
t.Fatal(err)
}
if got.Mbid != nil {
t.Fatalf("mbid = %q, want it forgotten", *got.Mbid)
}
}
// A file that cannot be removed leaves the track as it was.
func TestRemoveFile_FailedRemovalMarksNothing(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
path := filepath.Join(t.TempDir(), "stuck.mp3")
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
track, _, _ := seedTrack(t, pool, path)
stubRemoveFile(t, func(p string) error {
return &os.PathError{Op: "remove", Path: p, Err: syscall.EROFS}
})
_, err := RemoveFile(ctx, pool, nil, RemoveFileParams{TrackID: track.ID, Reason: RemovedForReport})
var fre *FileRemoveError
if !errors.As(err, &fre) {
t.Fatalf("err = %v, want a FileRemoveError", err)
}
got, err := dbq.New(pool).GetTrackByID(ctx, track.ID)
if err != nil {
t.Fatal(err)
}
if got.MissingSince.Valid || got.RemovedReason != nil {
t.Fatalf("a track whose file stayed was marked: missing %v, reason %v", got.MissingSince.Valid, got.RemovedReason)
}
}
// The reason describes one missing spell: a file back at its path clears it
// with the missing mark.
func TestRemoveFile_ReasonGoesWhenTheFileIsBack(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
path := filepath.Join(t.TempDir(), "back.mp3")
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
track, _, _ := seedTrack(t, pool, path)
if _, err := RemoveFile(ctx, pool, nil, RemoveFileParams{TrackID: track.ID, Reason: RemovedForReport}); err != nil {
t.Fatalf("RemoveFile: %v", err)
}
q := dbq.New(pool)
if _, err := q.ClearTracksMissing(ctx, []pgtype.UUID{track.ID}); err != nil {
t.Fatalf("ClearTracksMissing: %v", err)
}
got, err := q.GetTrackByID(ctx, track.ID)
if err != nil {
t.Fatal(err)
}
if got.MissingSince.Valid || got.RemovedReason != nil || got.RemovedBy.Valid {
t.Fatalf("back track = missing %v, reason %v, by %v; want all cleared",
got.MissingSince.Valid, got.RemovedReason, got.RemovedBy.Valid)
}
}
+67 -1
View File
@@ -1,7 +1,7 @@
// Package quarantine 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).
// Service's admin actions (Resolve / RemoveFile / DeleteFile).
package quarantine
import (
@@ -265,3 +265,69 @@ func (s *Service) DeleteFile(ctx context.Context, trackID, adminID pgtype.UUID)
AdminID: adminID, AffectedUsers: affected,
})
}
// RemoveFile removes a reported track's file and keeps the track: it waits,
// missing, for a good copy, with its likes and plays (library.RemoveFile).
// The reports are settled with it: every listener's report is cleared, so the
// track is back in their views once a good copy lands, its open playback
// errors are marked deleted, and the Reports history records the removal.
//
// A report saying the file is another song, or that its tags are wrong, means
// the file's recording MBID is not to be trusted, so it goes with the file.
//
// If the file cannot be removed, nothing changes and the error wraps a
// *library.FileRemoveError.
func (s *Service) RemoveFile(ctx context.Context, trackID, adminID pgtype.UUID) (dbq.QuarantineAction, error) {
q := dbq.New(s.pool)
track, err := q.GetTrackByID(ctx, trackID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return dbq.QuarantineAction{}, ErrTrackNotFound
}
return dbq.QuarantineAction{}, fmt.Errorf("get track: %w", err)
}
snap, err := s.snapshot(ctx, q, track)
if err != nil {
return dbq.QuarantineAction{}, err
}
reports, err := q.ListQuarantineReportsForTrack(ctx, trackID)
if err != nil {
return dbq.QuarantineAction{}, fmt.Errorf("list reports: %w", err)
}
forget := false
for _, r := range reports {
if r.Reason == dbq.QuarantineReasonWrongFile || r.Reason == dbq.QuarantineReasonWrongTags {
forget = true
}
}
var action dbq.QuarantineAction
deleted := "deleted"
_, err = library.RemoveFile(ctx, s.pool, nil, library.RemoveFileParams{
TrackID: trackID, ActorID: adminID, Reason: library.RemovedForReport, ForgetMBID: forget,
Within: func(ctx context.Context, tq *dbq.Queries) error {
if err := tq.DeleteQuarantineForTrack(ctx, trackID); err != nil {
return fmt.Errorf("delete rows: %w", err)
}
if _, err := tq.ResolveAllPlaybackErrorsForTrack(ctx, dbq.ResolveAllPlaybackErrorsForTrackParams{
TrackID: trackID, ResolvedBy: adminID, Resolution: &deleted,
}); err != nil {
return fmt.Errorf("resolve playback errors: %w", err)
}
var err error
action, err = tq.WriteQuarantineAction(ctx, dbq.WriteQuarantineActionParams{
TrackID: trackID, TrackTitle: snap.TrackTitle, ArtistName: snap.ArtistName,
AlbumTitle: snap.AlbumTitle, Action: dbq.QuarantineActionKindRemovedFile,
AdminID: adminID, AffectedUsers: int32(len(reports)),
})
return err
},
})
if err != nil {
if errors.Is(err, library.ErrTrackNotFound) {
return dbq.QuarantineAction{}, ErrTrackNotFound
}
return dbq.QuarantineAction{}, fmt.Errorf("remove file: %w", err)
}
return action, nil
}
+71
View File
@@ -329,6 +329,77 @@ func TestDeleteFile_RemovesFileAndAuditsAffected(t *testing.T) {
}
}
// Remove file keeps the reported track, missing, and settles its reports:
// the listeners' rows go, open playback errors read deleted, and the history
// says the file was removed. A "wrong file" report drops the file's MBID.
func TestRemoveFile_KeepsTheTrackAndSettlesItsReports(t *testing.T) {
pool := newPool(t)
ctx := context.Background()
alice := seedUser(t, pool, "alice")
admin := seedUser(t, pool, "admin")
q := dbq.New(pool)
path := filepath.Join(t.TempDir(), "track.mp3")
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
artist, _ := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "A", SortName: "A"})
album, _ := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Al", SortTitle: "Al", ArtistID: artist.ID})
mbid := "rec-of-another-song"
track, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "T", AlbumID: album.ID, ArtistID: artist.ID, Mbid: &mbid,
DurationMs: 1000, FilePath: path, FileSize: 1, FileFormat: "mp3",
})
if err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(ctx, `UPDATE tracks SET mbid_source = 'tag' WHERE id = $1`, track.ID); err != nil {
t.Fatal(err)
}
svc := NewService(pool, "")
if _, err := svc.Flag(ctx, alice.ID, track.ID, "wrong_file", ""); err != nil {
t.Fatalf("Flag: %v", err)
}
if _, err := q.InsertPlaybackError(ctx, dbq.InsertPlaybackErrorParams{
TrackID: track.ID, UserID: alice.ID, ClientID: "web", Kind: "load_failed",
}); err != nil {
t.Fatal(err)
}
action, err := svc.RemoveFile(ctx, track.ID, admin.ID)
if err != nil {
t.Fatalf("RemoveFile: %v", err)
}
if action.Action != dbq.QuarantineActionKindRemovedFile || action.AffectedUsers != 1 {
t.Errorf("history = %v for %d users, want removed_file for 1", action.Action, action.AffectedUsers)
}
if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {
t.Errorf("file still exists: %v", err)
}
got, err := q.GetTrackByID(ctx, track.ID)
if err != nil {
t.Fatalf("the track went with its file: %v", err)
}
if !got.MissingSince.Valid || got.RemovedReason == nil || *got.RemovedReason != "report" {
t.Errorf("track = missing %v, reason %v; want missing for a report", got.MissingSince.Valid, got.RemovedReason)
}
if got.Mbid != nil {
t.Errorf("mbid = %q, want it dropped for a wrong-file report", *got.Mbid)
}
if n, _ := q.CountQuarantineForTrack(ctx, track.ID); n != 0 {
t.Errorf("%d reports left, want them cleared", n)
}
var open, deleted int
if err := pool.QueryRow(ctx, `SELECT count(*) FILTER (WHERE resolved_at IS NULL),
count(*) FILTER (WHERE resolution = 'deleted')
FROM playback_errors WHERE track_id = $1`, track.ID).Scan(&open, &deleted); err != nil {
t.Fatal(err)
}
if open != 0 || deleted != 1 {
t.Errorf("playback errors: %d open, %d deleted; want 0 and 1", open, deleted)
}
}
func TestDeleteFile_TrackNotFound(t *testing.T) {
pool := newPool(t)
user := seedUser(t, pool, "alice")