From 9462052625567679de13b73e2eb9dade833b6d72 Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sun, 22 Nov 2026 18:17:26 -0500 Subject: [PATCH] feat(library): removing a bad file keeps its track, waiting for a good copy 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 --- internal/api/admin_quarantine.go | 42 +++++ internal/api/admin_suspect_sources.go | 55 ++++++ internal/api/admin_suspect_sources_test.go | 38 ++++ internal/api/api.go | 2 + internal/audit/audit.go | 5 + internal/db/dbq/events.sql.go | 4 +- internal/db/dbq/history.sql.go | 4 +- internal/db/dbq/likes.sql.go | 4 +- internal/db/dbq/models.go | 3 + internal/db/dbq/recommendation.sql.go | 16 +- internal/db/dbq/tracks.sql.go | 68 ++++++-- .../migrations/0104_removed_tracks.down.sql | 9 + .../db/migrations/0104_removed_tracks.up.sql | 22 +++ internal/db/queries/tracks.sql | 21 ++- internal/library/delete.go | 7 +- internal/library/duplicate_settle.go | 7 +- internal/library/duplicate_settle_test.go | 8 + internal/library/remove.go | 104 +++++++++++ internal/library/remove_test.go | 164 ++++++++++++++++++ internal/quarantine/service.go | 68 +++++++- internal/quarantine/service_test.go | 71 ++++++++ 21 files changed, 694 insertions(+), 28 deletions(-) create mode 100644 internal/db/migrations/0104_removed_tracks.down.sql create mode 100644 internal/db/migrations/0104_removed_tracks.up.sql create mode 100644 internal/library/remove.go create mode 100644 internal/library/remove_test.go diff --git a/internal/api/admin_quarantine.go b/internal/api/admin_quarantine.go index 07ab3202..30653e05 100644 --- a/internal/api/admin_quarantine.go +++ b/internal/api/admin_quarantine.go @@ -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"` diff --git a/internal/api/admin_suspect_sources.go b/internal/api/admin_suspect_sources.go index 7e51109b..e96ce259 100644 --- a/internal/api/admin_suspect_sources.go +++ b/internal/api/admin_suspect_sources.go @@ -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) +} diff --git a/internal/api/admin_suspect_sources_test.go b/internal/api/admin_suspect_sources_test.go index bf812a55..8ebb1eeb 100644 --- a/internal/api/admin_suspect_sources_test.go +++ b/internal/api/admin_suspect_sources_test.go @@ -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) + } +} diff --git a/internal/api/api.go b/internal/api/api.go index 3160e62a..43852fb3 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -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) diff --git a/internal/audit/audit.go b/internal/audit/audit.go index fc86340d..c38b12ce 100644 --- a/internal/audit/audit.go +++ b/internal/audit/audit.go @@ -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" diff --git a/internal/db/dbq/events.sql.go b/internal/db/dbq/events.sql.go index 5b1ba57a..d97c5439 100644 --- a/internal/db/dbq/events.sql.go +++ b/internal/db/dbq/events.sql.go @@ -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 } diff --git a/internal/db/dbq/history.sql.go b/internal/db/dbq/history.sql.go index 4edbbed0..5187a41e 100644 --- a/internal/db/dbq/history.sql.go +++ b/internal/db/dbq/history.sql.go @@ -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 { diff --git a/internal/db/dbq/likes.sql.go b/internal/db/dbq/likes.sql.go index 0e64ff2a..7431dd30 100644 --- a/internal/db/dbq/likes.sql.go +++ b/internal/db/dbq/likes.sql.go @@ -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 } diff --git a/internal/db/dbq/models.go b/internal/db/dbq/models.go index 022e1049..166d9646 100644 --- a/internal/db/dbq/models.go +++ b/internal/db/dbq/models.go @@ -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 { diff --git a/internal/db/dbq/recommendation.sql.go b/internal/db/dbq/recommendation.sql.go index 29856743..27011556 100644 --- a/internal/db/dbq/recommendation.sql.go +++ b/internal/db/dbq/recommendation.sql.go @@ -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, diff --git a/internal/db/dbq/tracks.sql.go b/internal/db/dbq/tracks.sql.go index 44707ac9..a6a6e412 100644 --- a/internal/db/dbq/tracks.sql.go +++ b/internal/db/dbq/tracks.sql.go @@ -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 } diff --git a/internal/db/migrations/0104_removed_tracks.down.sql b/internal/db/migrations/0104_removed_tracks.down.sql new file mode 100644 index 00000000..07c453ec --- /dev/null +++ b/internal/db/migrations/0104_removed_tracks.down.sql @@ -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; diff --git a/internal/db/migrations/0104_removed_tracks.up.sql b/internal/db/migrations/0104_removed_tracks.up.sql new file mode 100644 index 00000000..88b29b7b --- /dev/null +++ b/internal/db/migrations/0104_removed_tracks.up.sql @@ -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'; diff --git a/internal/db/queries/tracks.sql b/internal/db/queries/tracks.sql index 95ca6b26..5ebdd537 100644 --- a/internal/db/queries/tracks.sql +++ b/internal/db/queries/tracks.sql @@ -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; diff --git a/internal/library/delete.go b/internal/library/delete.go index 8e65edf2..fc709dd5 100644 --- a/internal/library/delete.go +++ b/internal/library/delete.go @@ -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 diff --git a/internal/library/duplicate_settle.go b/internal/library/duplicate_settle.go index a05f3298..ba5e21aa 100644 --- a/internal/library/duplicate_settle.go +++ b/internal/library/duplicate_settle.go @@ -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 } diff --git a/internal/library/duplicate_settle_test.go b/internal/library/duplicate_settle_test.go index b80a2510..19329766 100644 --- a/internal/library/duplicate_settle_test.go +++ b/internal/library/duplicate_settle_test.go @@ -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) diff --git a/internal/library/remove.go b/internal/library/remove.go new file mode 100644 index 00000000..315ac3d9 --- /dev/null +++ b/internal/library/remove.go @@ -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 +} diff --git a/internal/library/remove_test.go b/internal/library/remove_test.go new file mode 100644 index 00000000..2d3218ce --- /dev/null +++ b/internal/library/remove_test.go @@ -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) + } +} diff --git a/internal/quarantine/service.go b/internal/quarantine/service.go index 1e676508..e7d4152d 100644 --- a/internal/quarantine/service.go +++ b/internal/quarantine/service.go @@ -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 +} diff --git a/internal/quarantine/service_test.go b/internal/quarantine/service_test.go index 6bb302fb..d698e7df 100644 --- a/internal/quarantine/service_test.go +++ b/internal/quarantine/service_test.go @@ -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")