From 55c7497ca7cdf33d34f79ea011f6b5de9a19993f Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sat, 21 Nov 2026 20:51:14 -0500 Subject: [PATCH] feat: Minstrel stops acquiring music and drops Discover Minstrel is now a viewer of the library it is pointed at. Finding, downloading and filing music moves out of it. Removed from the server: - the request flow (requests, approval, auto-approve) and the wanted list, follows, release profiles and release blocks; - searching indexers, the download clients, the importer, the album lifecycle and missing-file re-acquisition; - the catalogue's discography, search and art (the MusicBrainz release and track-list cache stays, for the duplicate resolver); - Discover: its suggestions of artists not in the library, snoozes, the candidate-artist tag enrichment and its tuning scope; - writes to acquisition tables from moving the library folder and from merging duplicates, the want-list join on Suspect sources, the sealing of acquisition credentials, and the release-group lookup only re-acquisition used. Settling a wrong-slot duplicate no longer sets the release aside: the copies go and their slots show as missing until a good copy is in the library. Removed from the web: Admin > Acquisition and Requests, /requests, /discover, Follow on the artist page, the auto-approve toggle, and the Collection's wanted-list tabs. The Collection is now the library's health: an overview, Reports, Duplicates, Suspect sources and Missing files. The README and docs no longer describe getting music. The tables stay for now; a later migration drops them. The request notification kinds and the Android screens are still to go. Co-Authored-By: Claude Opus 5.5 --- README.md | 21 +- cmd/minstrel/main.go | 63 +- config.example.yaml | 2 +- docs/hosting.md | 4 +- docs/security.md | 1 - internal/acquire/acquire_test.go | 180 -- internal/acquire/build.go | 45 - internal/acquire/fanout.go | 164 -- internal/acquire/grab.go | 338 ---- internal/acquire/memory.go | 199 -- internal/acquire/pace.go | 145 -- internal/acquire/search.go | 408 ---- internal/acquire/search_test.go | 291 --- internal/acquire/settings.go | 215 --- internal/acquire/store.go | 200 -- internal/acquire/store_test.go | 118 -- internal/acquire/test.go | 134 -- internal/api/admin_acquisition.go | 210 -- internal/api/admin_duplicates.go | 15 +- internal/api/admin_import.go | 285 --- internal/api/admin_library_missing.go | 128 +- .../api/admin_library_missing_wants_test.go | 84 - internal/api/admin_reacquisition.go | 78 - internal/api/admin_recommendation_tuning.go | 19 - internal/api/admin_requests.go | 190 -- internal/api/admin_requests_test.go | 335 ---- internal/api/admin_search.go | 340 ---- internal/api/admin_status.go | 61 +- internal/api/admin_status_test.go | 2 +- internal/api/admin_summary.go | 2 - internal/api/admin_summary_test.go | 13 +- internal/api/admin_suspect_sources.go | 9 - internal/api/admin_users.go | 100 +- internal/api/admin_users_test.go | 38 - internal/api/admin_wanted.go | 821 -------- internal/api/admin_wanted_test.go | 371 ---- internal/api/api.go | 137 +- internal/api/auth_test.go | 13 +- internal/api/catalogue.go | 284 --- internal/api/catalogue_test.go | 254 --- internal/api/convert.go | 8 + internal/api/diagnostics.go | 13 +- internal/api/events_publish.go | 21 - internal/api/events_stream.go | 2 +- internal/api/library_test.go | 3 +- internal/api/notify_producers.go | 20 - internal/api/notify_producers_test.go | 93 - internal/api/requests.go | 264 --- internal/api/requests_test.go | 396 ---- internal/api/suggestions.go | 261 --- internal/api/suggestions_test.go | 104 - internal/audit/audit.go | 2 +- internal/catalogue/art.go | 214 --- internal/catalogue/art_test.go | 165 -- internal/catalogue/catalogue.go | 217 +-- internal/catalogue/catalogue_test.go | 160 +- internal/config/config.go | 2 +- internal/db/dbq/acquisition.sql.go | 362 ---- internal/db/dbq/admin_status.sql.go | 40 +- internal/db/dbq/admin_summary.sql.go | 6 - internal/db/dbq/albums.sql.go | 35 - internal/db/dbq/candidate_artist_tags.sql.go | 228 --- internal/db/dbq/catalogue.sql.go | 209 +- internal/db/dbq/imports.sql.go | 902 --------- internal/db/dbq/library_settings.sql.go | 39 - internal/db/dbq/lifecycle.sql.go | 408 ---- internal/db/dbq/merge.sql.go | 18 - internal/db/dbq/reacquisition.sql.go | 272 --- internal/db/dbq/recommendation.sql.go | 149 -- internal/db/dbq/recommendation_tuning.sql.go | 60 - internal/db/dbq/requests.sql.go | 528 ----- internal/db/dbq/search.sql.go | 680 ------- internal/db/dbq/similarity.sql.go | 32 - internal/db/dbq/suggestion_snoozes.sql.go | 126 -- internal/db/dbq/tracks.sql.go | 14 +- internal/db/dbq/users.sql.go | 39 +- internal/db/dbq/wanted.sql.go | 1697 ----------------- internal/db/queries/acquisition.sql | 76 - internal/db/queries/admin_status.sql | 18 +- internal/db/queries/admin_summary.sql | 2 - internal/db/queries/albums.sql | 16 - internal/db/queries/candidate_artist_tags.sql | 103 - internal/db/queries/catalogue.sql | 51 +- internal/db/queries/imports.sql | 176 -- internal/db/queries/library_settings.sql | 11 - internal/db/queries/lifecycle.sql | 102 - internal/db/queries/merge.sql | 4 - internal/db/queries/reacquisition.sql | 93 - internal/db/queries/recommendation.sql | 103 - internal/db/queries/recommendation_tuning.sql | 18 - internal/db/queries/requests.sql | 119 -- internal/db/queries/search.sql | 141 -- internal/db/queries/similarity.sql | 12 - internal/db/queries/suggestion_snoozes.sql | 39 - internal/db/queries/tracks.sql | 10 +- internal/db/queries/users.sql | 10 +- internal/db/queries/wanted.sql | 501 ----- internal/downloader/downloader_test.go | 156 -- internal/downloader/driver.go | 197 -- internal/downloader/exchange.go | 340 ---- internal/downloader/pathmap.go | 107 -- internal/downloader/qbittorrent.go | 367 ---- internal/eventbus/eventbus.go | 2 +- internal/exchange/exchange.go | 181 -- internal/exchange/exchange_test.go | 108 -- internal/exchange/search.go | 169 -- internal/gc/worker.go | 11 - internal/importer/importer.go | 610 ------ internal/importer/importer_test.go | 268 --- internal/importer/inspect.go | 176 -- internal/importer/name.go | 109 -- internal/importer/place.go | 210 -- internal/importer/upgrade.go | 304 --- internal/importer/upgrade_test.go | 53 - internal/importer/verify.go | 437 ----- internal/importer/verify_test.go | 239 --- internal/indexer/indexer.go | 156 -- internal/indexer/magnet.go | 58 - internal/indexer/ratelimit.go | 50 - internal/indexer/torznab.go | 253 --- internal/indexer/torznab_test.go | 188 -- internal/library/duplicate_merge.go | 3 - internal/library/duplicate_resolve.go | 2 +- internal/library/duplicate_resolve_test.go | 42 +- internal/library/duplicate_settle.go | 74 +- internal/library/duplicate_settle_test.go | 34 +- internal/library/library_paths.go | 6 - internal/library/mbids.go | 4 +- internal/library/scanner.go | 6 +- internal/lifecycle/lifecycle.go | 682 ------- internal/lifecycle/lifecycle_test.go | 488 ----- internal/prowlarr/prowlarr.go | 176 -- internal/prowlarr/prowlarr_test.go | 78 - internal/reacquisition/settings.go | 116 -- internal/reacquisition/settings_test.go | 41 - internal/reacquisition/sweeper.go | 167 -- internal/reacquisition/sweeper_test.go | 199 -- .../candidate_tags_integration_test.go | 455 ----- .../fixtures_integration_test.go | 121 ++ internal/recommendation/suggestions.go | 322 ---- .../suggestions_integration_test.go | 687 ------- .../recommendation/suggestions_select_test.go | 169 -- internal/recommendation/tagoverlap.go | 152 -- internal/recommendation/tagoverlap_test.go | 243 --- internal/recsettings/patch.go | 64 - internal/recsettings/service.go | 100 +- internal/recsettings/service_test.go | 112 -- .../recsettings/songs_like_weights_test.go | 2 +- internal/release/agree.go | 40 - internal/release/agree_test.go | 19 - internal/release/files.go | 76 - internal/release/match.go | 210 -- internal/release/parse.go | 289 --- internal/release/quality.go | 271 --- internal/release/release_test.go | 311 --- internal/release/torrent.go | 178 -- internal/release/version.go | 79 - internal/release/version_test.go | 32 - internal/requests/reconciler.go | 198 -- internal/requests/requests_test.go | 310 --- internal/requests/service.go | 263 --- internal/secret/secret_test.go | 2 +- internal/secret/stored.go | 21 +- internal/secret/stored_test.go | 10 - internal/server/server.go | 25 +- internal/server/server_test.go | 100 - internal/similarity/worker.go | 37 +- internal/tags/enricher.go | 148 +- internal/tags/provider_musicbrainz.go | 57 - internal/tags/provider_musicbrainz_test.go | 34 - internal/tags/worker.go | 45 +- internal/wanted/library.go | 174 -- internal/wanted/library_test.go | 321 ---- internal/wanted/manual.go | 170 -- internal/wanted/manual_test.go | 166 -- internal/wanted/match.go | 404 ---- internal/wanted/profiles.go | 228 --- internal/wanted/search.go | 360 ---- internal/wanted/search_test.go | 213 --- internal/wanted/upgrade.go | 125 -- internal/wanted/upgrade_test.go | 173 -- internal/wanted/wanted.go | 542 ------ internal/wanted/wanted_test.go | 481 ----- web/src/lib/admin/queues.ts | 9 +- web/src/lib/api/admin.suspect-sources.test.ts | 2 - web/src/lib/api/admin.test.ts | 103 +- web/src/lib/api/admin.ts | 163 +- web/src/lib/api/catalogue.test.ts | 73 - web/src/lib/api/catalogue.ts | 26 - web/src/lib/api/queries.ts | 26 - web/src/lib/api/requests.test.ts | 183 -- web/src/lib/api/requests.ts | 87 - web/src/lib/api/suggestions.test.ts | 100 - web/src/lib/api/suggestions.ts | 49 - web/src/lib/api/tuning.ts | 12 +- web/src/lib/api/types.ts | 466 +---- web/src/lib/api/wanted.ts | 308 --- web/src/lib/components/AcquisitionCard.svelte | 166 -- .../lib/components/AcquisitionCard.test.ts | 168 -- .../components/AcquisitionServiceRow.svelte | 394 ---- web/src/lib/components/CollectionRail.svelte | 21 + .../lib/components/DiscoverResultCard.svelte | 202 -- .../lib/components/DiscoverResultCard.test.ts | 188 -- web/src/lib/components/DiscoverTabs.svelte | 36 - web/src/lib/components/DiscoverTabs.test.ts | 34 - web/src/lib/components/FollowControl.svelte | 155 -- web/src/lib/components/FollowControl.test.ts | 67 - web/src/lib/components/KindPill.svelte | 4 +- .../lib/components/NotificationBell.test.ts | 4 +- .../ReacquisitionSettingsCard.svelte | 110 -- .../ReacquisitionSettingsCard.test.ts | 118 -- .../lib/components/ReleaseProfilesCard.svelte | 233 --- .../components/ReleaseProfilesCard.test.ts | 91 - web/src/lib/components/RequestRow.svelte | 74 - web/src/lib/components/RequestRow.test.ts | 60 - .../lib/components/SearchSettingsCard.svelte | 240 --- .../lib/components/SearchSettingsCard.test.ts | 127 -- web/src/lib/components/Shell.svelte | 8 +- web/src/lib/components/Shell.test.ts | 11 +- web/src/lib/components/StatusPill.svelte | 54 - web/src/lib/components/StatusPill.test.ts | 25 - web/src/lib/components/SuggestionFeed.svelte | 212 -- web/src/lib/components/SuggestionFeed.test.ts | 292 --- web/src/lib/components/WantCandidates.svelte | 181 -- web/src/lib/components/WantCandidates.test.ts | 90 - .../lib/components/wanted/BlockList.svelte | 95 - .../lib/components/wanted/BlockList.test.ts | 57 - .../wanted/CatalogueArtistCard.svelte | 46 - .../components/wanted/CatalogueGrid.svelte | 72 - .../components/wanted/CatalogueGrid.test.ts | 86 - .../components/wanted/CollectionRail.svelte | 33 - .../lib/components/wanted/DownloadList.svelte | 264 --- .../components/wanted/DownloadList.test.ts | 160 -- .../lib/components/wanted/FollowList.svelte | 118 -- .../lib/components/wanted/FollowList.test.ts | 73 - .../components/wanted/UnmatchedList.svelte | 150 -- .../components/wanted/UnmatchedList.test.ts | 109 -- .../components/wanted/UnmatchedMatch.svelte | 106 - web/src/lib/components/wanted/WantList.svelte | 308 --- .../lib/components/wanted/WantList.test.ts | 345 ---- .../lib/components/wanted/WantTracks.svelte | 52 - web/src/lib/wanted/downloadState.test.ts | 23 - web/src/lib/wanted/downloadState.ts | 20 - web/src/lib/wanted/wantStatus.ts | 71 - web/src/routes/admin/+layout.svelte | 28 +- web/src/routes/admin/+page.svelte | 51 +- web/src/routes/admin/acquisition/+page.svelte | 25 - web/src/routes/admin/admin.test.ts | 63 +- web/src/routes/admin/library/+page.svelte | 8 +- web/src/routes/admin/requests/+page.svelte | 279 --- .../routes/admin/requests/requests.test.ts | 139 -- web/src/routes/admin/tuning/+page.svelte | 26 +- web/src/routes/admin/tuning/tuning.test.ts | 68 +- web/src/routes/admin/users/+page.svelte | 33 +- web/src/routes/admin/users/users.test.ts | 13 - web/src/routes/artists/[id]/+page.svelte | 8 - web/src/routes/artists/[id]/artist.test.ts | 49 - web/src/routes/collection/+layout.svelte | 78 +- web/src/routes/collection/+page.svelte | 45 +- .../routes/collection/activity/+page.svelte | 8 - .../collection/artist/[mbid]/+page.svelte | 15 - .../routes/collection/blocked/+page.svelte | 8 - web/src/routes/collection/collection.test.ts | 93 +- .../routes/collection/duplicates/+page.svelte | 31 +- .../collection/duplicates/duplicates.test.ts | 21 +- .../routes/collection/following/+page.svelte | 12 - .../routes/collection/missing/+page.svelte | 158 +- .../routes/collection/missing/missing.test.ts | 135 +- .../collection/suspect-sources/+page.svelte | 15 +- .../suspect-sources/suspect-sources.test.ts | 15 - .../routes/collection/unmatched/+page.svelte | 8 - .../routes/collection/upgrades/+page.svelte | 8 - web/src/routes/collection/wanted/+page.svelte | 8 - web/src/routes/discover/+page.svelte | 232 --- web/src/routes/discover/discover.test.ts | 319 ---- web/src/routes/requests/+page.svelte | 101 - web/src/routes/requests/requests.test.ts | 165 -- 277 files changed, 521 insertions(+), 40021 deletions(-) delete mode 100644 internal/acquire/acquire_test.go delete mode 100644 internal/acquire/build.go delete mode 100644 internal/acquire/fanout.go delete mode 100644 internal/acquire/grab.go delete mode 100644 internal/acquire/memory.go delete mode 100644 internal/acquire/pace.go delete mode 100644 internal/acquire/search.go delete mode 100644 internal/acquire/search_test.go delete mode 100644 internal/acquire/settings.go delete mode 100644 internal/acquire/store.go delete mode 100644 internal/acquire/store_test.go delete mode 100644 internal/acquire/test.go delete mode 100644 internal/api/admin_acquisition.go delete mode 100644 internal/api/admin_import.go delete mode 100644 internal/api/admin_library_missing_wants_test.go delete mode 100644 internal/api/admin_reacquisition.go delete mode 100644 internal/api/admin_requests.go delete mode 100644 internal/api/admin_requests_test.go delete mode 100644 internal/api/admin_search.go delete mode 100644 internal/api/admin_wanted.go delete mode 100644 internal/api/admin_wanted_test.go delete mode 100644 internal/api/catalogue.go delete mode 100644 internal/api/catalogue_test.go delete mode 100644 internal/api/requests.go delete mode 100644 internal/api/requests_test.go delete mode 100644 internal/api/suggestions.go delete mode 100644 internal/api/suggestions_test.go delete mode 100644 internal/catalogue/art.go delete mode 100644 internal/catalogue/art_test.go delete mode 100644 internal/db/dbq/acquisition.sql.go delete mode 100644 internal/db/dbq/candidate_artist_tags.sql.go delete mode 100644 internal/db/dbq/imports.sql.go delete mode 100644 internal/db/dbq/lifecycle.sql.go delete mode 100644 internal/db/dbq/reacquisition.sql.go delete mode 100644 internal/db/dbq/requests.sql.go delete mode 100644 internal/db/dbq/search.sql.go delete mode 100644 internal/db/dbq/suggestion_snoozes.sql.go delete mode 100644 internal/db/dbq/wanted.sql.go delete mode 100644 internal/db/queries/acquisition.sql delete mode 100644 internal/db/queries/candidate_artist_tags.sql delete mode 100644 internal/db/queries/imports.sql delete mode 100644 internal/db/queries/lifecycle.sql delete mode 100644 internal/db/queries/reacquisition.sql delete mode 100644 internal/db/queries/requests.sql delete mode 100644 internal/db/queries/search.sql delete mode 100644 internal/db/queries/suggestion_snoozes.sql delete mode 100644 internal/db/queries/wanted.sql delete mode 100644 internal/downloader/downloader_test.go delete mode 100644 internal/downloader/driver.go delete mode 100644 internal/downloader/exchange.go delete mode 100644 internal/downloader/pathmap.go delete mode 100644 internal/downloader/qbittorrent.go delete mode 100644 internal/exchange/exchange.go delete mode 100644 internal/exchange/exchange_test.go delete mode 100644 internal/exchange/search.go delete mode 100644 internal/importer/importer.go delete mode 100644 internal/importer/importer_test.go delete mode 100644 internal/importer/inspect.go delete mode 100644 internal/importer/name.go delete mode 100644 internal/importer/place.go delete mode 100644 internal/importer/upgrade.go delete mode 100644 internal/importer/upgrade_test.go delete mode 100644 internal/importer/verify.go delete mode 100644 internal/importer/verify_test.go delete mode 100644 internal/indexer/indexer.go delete mode 100644 internal/indexer/magnet.go delete mode 100644 internal/indexer/ratelimit.go delete mode 100644 internal/indexer/torznab.go delete mode 100644 internal/indexer/torznab_test.go delete mode 100644 internal/lifecycle/lifecycle.go delete mode 100644 internal/lifecycle/lifecycle_test.go delete mode 100644 internal/prowlarr/prowlarr.go delete mode 100644 internal/prowlarr/prowlarr_test.go delete mode 100644 internal/reacquisition/settings.go delete mode 100644 internal/reacquisition/settings_test.go delete mode 100644 internal/reacquisition/sweeper.go delete mode 100644 internal/reacquisition/sweeper_test.go delete mode 100644 internal/recommendation/candidate_tags_integration_test.go create mode 100644 internal/recommendation/fixtures_integration_test.go delete mode 100644 internal/recommendation/suggestions.go delete mode 100644 internal/recommendation/suggestions_integration_test.go delete mode 100644 internal/recommendation/suggestions_select_test.go delete mode 100644 internal/recommendation/tagoverlap.go delete mode 100644 internal/recommendation/tagoverlap_test.go delete mode 100644 internal/release/agree.go delete mode 100644 internal/release/agree_test.go delete mode 100644 internal/release/files.go delete mode 100644 internal/release/match.go delete mode 100644 internal/release/parse.go delete mode 100644 internal/release/quality.go delete mode 100644 internal/release/release_test.go delete mode 100644 internal/release/torrent.go delete mode 100644 internal/release/version.go delete mode 100644 internal/release/version_test.go delete mode 100644 internal/requests/reconciler.go delete mode 100644 internal/requests/requests_test.go delete mode 100644 internal/requests/service.go delete mode 100644 internal/wanted/library.go delete mode 100644 internal/wanted/library_test.go delete mode 100644 internal/wanted/manual.go delete mode 100644 internal/wanted/manual_test.go delete mode 100644 internal/wanted/match.go delete mode 100644 internal/wanted/profiles.go delete mode 100644 internal/wanted/search.go delete mode 100644 internal/wanted/search_test.go delete mode 100644 internal/wanted/upgrade.go delete mode 100644 internal/wanted/upgrade_test.go delete mode 100644 internal/wanted/wanted.go delete mode 100644 internal/wanted/wanted_test.go delete mode 100644 web/src/lib/api/catalogue.test.ts delete mode 100644 web/src/lib/api/catalogue.ts delete mode 100644 web/src/lib/api/requests.test.ts delete mode 100644 web/src/lib/api/requests.ts delete mode 100644 web/src/lib/api/suggestions.test.ts delete mode 100644 web/src/lib/api/suggestions.ts delete mode 100644 web/src/lib/api/wanted.ts delete mode 100644 web/src/lib/components/AcquisitionCard.svelte delete mode 100644 web/src/lib/components/AcquisitionCard.test.ts delete mode 100644 web/src/lib/components/AcquisitionServiceRow.svelte create mode 100644 web/src/lib/components/CollectionRail.svelte delete mode 100644 web/src/lib/components/DiscoverResultCard.svelte delete mode 100644 web/src/lib/components/DiscoverResultCard.test.ts delete mode 100644 web/src/lib/components/DiscoverTabs.svelte delete mode 100644 web/src/lib/components/DiscoverTabs.test.ts delete mode 100644 web/src/lib/components/FollowControl.svelte delete mode 100644 web/src/lib/components/FollowControl.test.ts delete mode 100644 web/src/lib/components/ReacquisitionSettingsCard.svelte delete mode 100644 web/src/lib/components/ReacquisitionSettingsCard.test.ts delete mode 100644 web/src/lib/components/ReleaseProfilesCard.svelte delete mode 100644 web/src/lib/components/ReleaseProfilesCard.test.ts delete mode 100644 web/src/lib/components/RequestRow.svelte delete mode 100644 web/src/lib/components/RequestRow.test.ts delete mode 100644 web/src/lib/components/SearchSettingsCard.svelte delete mode 100644 web/src/lib/components/SearchSettingsCard.test.ts delete mode 100644 web/src/lib/components/StatusPill.svelte delete mode 100644 web/src/lib/components/StatusPill.test.ts delete mode 100644 web/src/lib/components/SuggestionFeed.svelte delete mode 100644 web/src/lib/components/SuggestionFeed.test.ts delete mode 100644 web/src/lib/components/WantCandidates.svelte delete mode 100644 web/src/lib/components/WantCandidates.test.ts delete mode 100644 web/src/lib/components/wanted/BlockList.svelte delete mode 100644 web/src/lib/components/wanted/BlockList.test.ts delete mode 100644 web/src/lib/components/wanted/CatalogueArtistCard.svelte delete mode 100644 web/src/lib/components/wanted/CatalogueGrid.svelte delete mode 100644 web/src/lib/components/wanted/CatalogueGrid.test.ts delete mode 100644 web/src/lib/components/wanted/CollectionRail.svelte delete mode 100644 web/src/lib/components/wanted/DownloadList.svelte delete mode 100644 web/src/lib/components/wanted/DownloadList.test.ts delete mode 100644 web/src/lib/components/wanted/FollowList.svelte delete mode 100644 web/src/lib/components/wanted/FollowList.test.ts delete mode 100644 web/src/lib/components/wanted/UnmatchedList.svelte delete mode 100644 web/src/lib/components/wanted/UnmatchedList.test.ts delete mode 100644 web/src/lib/components/wanted/UnmatchedMatch.svelte delete mode 100644 web/src/lib/components/wanted/WantList.svelte delete mode 100644 web/src/lib/components/wanted/WantList.test.ts delete mode 100644 web/src/lib/components/wanted/WantTracks.svelte delete mode 100644 web/src/lib/wanted/downloadState.test.ts delete mode 100644 web/src/lib/wanted/downloadState.ts delete mode 100644 web/src/lib/wanted/wantStatus.ts delete mode 100644 web/src/routes/admin/acquisition/+page.svelte delete mode 100644 web/src/routes/admin/requests/+page.svelte delete mode 100644 web/src/routes/admin/requests/requests.test.ts delete mode 100644 web/src/routes/collection/activity/+page.svelte delete mode 100644 web/src/routes/collection/artist/[mbid]/+page.svelte delete mode 100644 web/src/routes/collection/blocked/+page.svelte delete mode 100644 web/src/routes/collection/following/+page.svelte delete mode 100644 web/src/routes/collection/unmatched/+page.svelte delete mode 100644 web/src/routes/collection/upgrades/+page.svelte delete mode 100644 web/src/routes/collection/wanted/+page.svelte delete mode 100644 web/src/routes/discover/+page.svelte delete mode 100644 web/src/routes/discover/discover.test.ts delete mode 100644 web/src/routes/requests/+page.svelte delete mode 100644 web/src/routes/requests/requests.test.ts diff --git a/README.md b/README.md index acb40640..ecb80bfe 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

A self-hosted music server that picks what to play next.

-Minstrel serves your music library to the web, to its own Android app, and to any Subsonic client. Shuffle, radio, likes and play history are worked out on the server, so every device gets the same mixes and the same history. It can also find, download and file the albums you want, through a search service and a download client you run yourself. It is for anyone who keeps their own music collection and wants it to sound like more than a random shuffle. +Minstrel serves your music library to the web, to its own Android app, and to any Subsonic client. Shuffle, radio, likes and play history are worked out on the server, so every device gets the same mixes and the same history. It is for anyone who keeps their own music collection and wants it to sound like more than a random shuffle. Minstrel home — your library at a glance @@ -49,7 +49,7 @@ Open `http://:4533/register` and create the first account with that setup 1. **Let the first scan finish.** Minstrel scans the library on startup. Watch it, or scan again, on the **Admin** page (`/admin`). Later scans only pick up what changed. 2. **Install the Android app.** In **Settings** (`/settings`), the *Install the Android app* card downloads the app this server ships. It updates itself from your server after that. 3. **Invite the household.** In **Admin › Users** (`/admin/users`), make an invite token or turn on open registration. Each person gets their own likes, history and recommendations. -4. **Optional: connect services.** Add a search service and a download client in **Admin › Acquisition** (`/admin/acquisition`). Each person adds their own ListenBrainz token in **Settings › Connections**. +4. **Optional: connect ListenBrainz.** Each person adds their own ListenBrainz token in **Settings › Connections**. The Admin page, where the library scan runs and reports progress @@ -64,11 +64,11 @@ The quick start sets everything that's required. These are the settings people m | `MINSTREL_BRANDING_APP_NAME` | The name shown in the header, the browser tab and link previews. | | `MINSTREL_LOG_LEVEL` | `debug`, `info`, `warn` or `error`. | -Search services, download clients, ListenBrainz, email and recommendation tuning are set in the web UI and take effect without a restart. The full file-based surface is in [`config.example.yaml`](./config.example.yaml); to use it, mount your copy at `/etc/smartmusic/config.yaml`. +ListenBrainz, email, metadata sources and recommendation tuning are set in the web UI and take effect without a restart. The full file-based surface is in [`config.example.yaml`](./config.example.yaml); to use it, mount your copy at `/etc/smartmusic/config.yaml`. ## Setup pitfalls -- **The library mount has to be writable by uid 1000.** Minstrel files the albums it downloads into the library, and it deletes a file when an admin asks it to. It never moves, renames or retags files that were already there. Mount the library `:ro` to forbid writes: those actions then refuse and say why. +- **The library mount has to be writable by uid 1000.** Minstrel deletes a file when an admin asks it to, and when the duplicate resolver removes a copy. It never moves, renames or retags your files. Mount the library `:ro` to forbid writes: those actions then refuse and say why. - **`MINSTREL_LIBRARY_SCAN_PATHS` names the path inside the container** (`/music`), not the folder on your host. - **On the internet, put it behind HTTPS.** Plain `http://` is fine on a network you trust. For a public server, bind the port to `127.0.0.1` and put a reverse proxy in front: see [docs/hosting.md](docs/hosting.md). @@ -103,11 +103,6 @@ Every image carries a signed Android app, all with the same key, so a phone can - Duplicates, missing files and suspect copies are found and listed under **Collection** for an admin to fix. - Missing album art and MusicBrainz ids are filled in automatically. -**Getting music** -- A wanted list: requests from the household, artists you follow, and albums whose files went missing. -- Minstrel searches the indexers you configure, hands the best release to your download client, checks what arrives against MusicBrainz, tags it, and files it. A better copy replaces a poor one. -- Off until you add a search service and a download client. - **Clients and accounts** - A full web app, built into the server. - A native Android app that ships inside the server image, keeps playing what it has cached when the server is out of reach, and updates itself from your server. @@ -116,13 +111,11 @@ Every image carries a signed Android app, all with the same key, so a phone can ## Scope and responsible use -Minstrel indexes the files you point it at and streams them to your own clients. It ships no indexers, no trackers, no torrent, Usenet or NZB engine of its own, and no DRM circumvention. +Minstrel indexes the files you point it at and streams them to your own clients. It does not find or download music, and it does no DRM circumvention. -Getting music is optional and does nothing until you configure it. It searches only the search service (Prowlarr or a Torznab indexer) and the download client (qBittorrent) you add yourself. +What you put in your library is your responsibility. Copyright law applies to your collection as it does to any other software that plays a file; please respect it, and the terms of any service you connect. -What you put in your library, and the sources you configure, are your responsibility. Copyright law applies to your collection as it does to any other software that plays a file; please respect it, and the terms of any service you connect. - -Minstrel is not affiliated with or endorsed by ListenBrainz, MusicBrainz, Prowlarr, qBittorrent or Subsonic. +Minstrel is not affiliated with or endorsed by ListenBrainz, MusicBrainz or Subsonic. ## Development diff --git a/cmd/minstrel/main.go b/cmd/minstrel/main.go index c77c7e84..cbd6c607 100644 --- a/cmd/minstrel/main.go +++ b/cmd/minstrel/main.go @@ -25,16 +25,12 @@ import ( "git.fabledsword.com/bvandeusen/minstrel/internal/db" "git.fabledsword.com/bvandeusen/minstrel/internal/eventbus" "git.fabledsword.com/bvandeusen/minstrel/internal/gc" - "git.fabledsword.com/bvandeusen/minstrel/internal/importer" "git.fabledsword.com/bvandeusen/minstrel/internal/library" - "git.fabledsword.com/bvandeusen/minstrel/internal/lifecycle" "git.fabledsword.com/bvandeusen/minstrel/internal/logging" "git.fabledsword.com/bvandeusen/minstrel/internal/mailer" "git.fabledsword.com/bvandeusen/minstrel/internal/notifications" "git.fabledsword.com/bvandeusen/minstrel/internal/playlists" - "git.fabledsword.com/bvandeusen/minstrel/internal/reacquisition" "git.fabledsword.com/bvandeusen/minstrel/internal/recsettings" - "git.fabledsword.com/bvandeusen/minstrel/internal/requests" "git.fabledsword.com/bvandeusen/minstrel/internal/scrobble" "git.fabledsword.com/bvandeusen/minstrel/internal/scrobble/listenbrainz" "git.fabledsword.com/bvandeusen/minstrel/internal/secret" @@ -43,7 +39,6 @@ import ( "git.fabledsword.com/bvandeusen/minstrel/internal/subsonic" syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync" "git.fabledsword.com/bvandeusen/minstrel/internal/tags" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" ) func main() { @@ -203,8 +198,8 @@ func run() error { if acoustIDErr != nil { logger.Warn("acoustid settings: using defaults (lookup off)", "err", acoustIDErr) } - // One AcoustID client, so the lookup worker and the import's check share - // its pace (AcoustID allows three requests a second per key). + // One AcoustID client, so every lookup shares its pace (AcoustID allows + // three requests a second per key). acoustIDClient := acoustid.New(acoustid.DefaultBaseURL) acoustIDLookup := library.NewAcoustIDLookupWorker(pool, logger.With("component", "acoustid_lookup"), acoustIDSettings, acoustIDClient) @@ -355,44 +350,12 @@ func run() error { // same pool and bus. notifier := notifications.New(pool, bus, logger.With("component", "notifications")) - // The request reconciler completes approved requests once the library - // holds what they asked for. - requestReconciler := requests.NewReconciler(pool, logger.With("component", "requests"), bus) - requestReconciler.SetNotifier(notifier) library.SetNotifier(notifier) - go requestReconciler.Run(ctx) - // Release catalogue: re-reads discographies older than a - // week from MusicBrainz, a batch an hour, through the process-wide - // MusicBrainz pacer it shares with tag enrichment. + // Release track lists from MusicBrainz, read on demand by the duplicate + // resolver through the process-wide MusicBrainz pacer it shares with tag + // enrichment. cat := catalogue.New(pool, nil, logger.With("component", "catalogue")) - go cat.Run(ctx) - - // The wanted list: reads followed artists' discographies - // daily for new releases and marks wants held once the library has them. - go wanted.New(pool, cat, logger.With("component", "wanted")).Run(ctx) - - // Search and grab: every ten minutes, searches the - // configured indexers for wants due, paced by each source's - // searches-per-hour, and hands the best release to the download client - // when auto-grab is on. - go wanted.NewSearcher(pool, cat, logger.With("component", "search")).Run(ctx) - - // Import with verification: every minute, finished - // downloads are checked against their album (AcoustID, lengths, tags) - // and filed into the library with MusicBrainz tags, or refused. - var printer importer.Printer - if acoustIDSettings != nil { - printer = &importer.AcoustIDPrinter{Settings: acoustIDSettings, Client: acoustIDClient} - } - imp := importer.New(pool, scanner, cat, printer, scanner.Roots, logger.With("component", "import")) - imp.DataDir = cfg.Storage.DataDir - go imp.Run(ctx) - - // Download lifecycle: every ten minutes, a download that - // failed or that nobody shares is retried, then given up and its album - // searched again; an imported one seeds, then is removed. - go lifecycle.New(pool, logger.With("component", "lifecycle")).Run(ctx) // Duplicate resolver: classifies the sweep's groups and, with auto-resolve // on, merges the copies that are safe to merge. @@ -404,19 +367,6 @@ func run() error { TrackLists: cat.RefreshReleaseGroup, Identify: acoustIDLookup.IdentifyRecordings, }).Run(ctx) - // Missing-file re-acquisition: wants back albums whose files have been - // gone longer than the grace window, for the search schedule to find. - // Hourly, because the grace window is measured in hours. - // - // A settings-load failure is logged, not fatal: NewSettingsService always - // returns a usable service holding the defaults. - reacqSettings, reacqErr := reacquisition.NewSettingsService( - ctx, pool, logger.With("component", "reacquisition")) - if reacqErr != nil { - logger.Warn("reacquisition: using default settings", "err", reacqErr) - } - go reacquisition.NewSweeper(pool, reacqSettings, logger.With("component", "reacquisition")).Run(ctx) - // library_changes compactor: daily, deletes // rows older than the configured retention so the change-log table // doesn't grow unbounded. Clients that drop offline longer than @@ -495,9 +445,6 @@ func run() error { srv.FingerprintSettings = fpSettings srv.LoudnessSettings = loudSettings srv.AcoustIDLookup = acoustIDLookup - // The sweeper above holds this same instance, so a save from the admin - // card changes what it does on its next tick. - srv.ReacqSettings = reacqSettings // The server's own backup: the loop runs the nightly schedule, and the // admin page starts, tests and configures the same instance. backups := backup.NewService(pool, logger.With("component", "backup"), buildinfo.Version) diff --git a/config.example.yaml b/config.example.yaml index b5b7bf7c..cdf70ede 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -30,7 +30,7 @@ library: branding: app_name: "Minstrel" # MINSTREL_BRANDING_APP_NAME - description: "Self-hosted music server with Subsonic compatibility, smart shuffle, and music acquisition." # MINSTREL_BRANDING_DESCRIPTION + description: "Self-hosted music server with Subsonic compatibility and smart shuffle." # MINSTREL_BRANDING_DESCRIPTION subsonic: # Accept the plaintext `p=` password on /rest/*. Leave off unless a client diff --git a/docs/hosting.md b/docs/hosting.md index 2e553d8a..92384518 100644 --- a/docs/hosting.md +++ b/docs/hosting.md @@ -86,7 +86,7 @@ changes on every restart until an account exists. ## Backups Minstrel can back itself up once a night: accounts, likes, playlists, -listening history, requests and settings, in one file. It is off until an +listening history and settings, in one file. It is off until an admin switches it on under **Admin › Backups**. Anything Minstrel can fetch or work out again (artwork, fingerprints, MusicBrainz data) is left out, and so are the keys and passwords Minstrel keeps for other services, which are @@ -145,7 +145,7 @@ the place of what the scan found. The library scan then finds each file, and the artwork, tags, fingerprints and loudness are fetched or worked out again over the following hours. Keys for other services (ListenBrainz, AcoustID, artwork and tag providers, -email, download clients) and the backup's own destination are entered again +email) and the backup's own destination are entered again under **Admin**. An app that synced with the old server starts its sync over. ## Android app diff --git a/docs/security.md b/docs/security.md index a295f345..9904eca6 100644 --- a/docs/security.md +++ b/docs/security.md @@ -53,7 +53,6 @@ they aren't checked. Some credentials can't be hashed, because Minstrel has to send or compare the value itself: -- the API keys and passwords for indexers, Prowlarr and download clients; - the AcoustID key, and the cover-art and tag provider keys; - the SMTP password; - the secret key of the S3 bucket backups go to; diff --git a/internal/acquire/acquire_test.go b/internal/acquire/acquire_test.go deleted file mode 100644 index ac0ebf16..00000000 --- a/internal/acquire/acquire_test.go +++ /dev/null @@ -1,180 +0,0 @@ -package acquire - -import ( - "context" - "errors" - "net/http" - "net/http/httptest" - "path/filepath" - "strings" - "testing" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/exchange" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" - "git.fabledsword.com/bvandeusen/minstrel/internal/prowlarr" -) - -func validUpdate() Update { - return Update{Label: "Prowlarr", BaseURL: "http://prowlarr:9696", TimeoutMs: 30000, LocalPath: "/downloads", SearchesPerHour: 120} -} - -func TestUpdateValidate(t *testing.T) { - if err := validUpdate().validate(); err != nil { - t.Fatalf("a valid form: %v", err) - } - for name, tc := range map[string]struct { - edit func(*Update) - field string - }{ - "no name": {func(u *Update) { u.Label = "" }, "label"}, - "switched on with no address": {func(u *Update) { u.Enabled, u.BaseURL = true, "" }, "base_url"}, - "an address with no scheme": {func(u *Update) { u.BaseURL = "prowlarr:9696" }, "base_url"}, - "a timeout too short": {func(u *Update) { u.TimeoutMs = 10 }, "timeout_ms"}, - "a relative downloads folder": {func(u *Update) { u.LocalPath = "downloads" }, "local_path"}, - "a relative client folder": {func(u *Update) { u.RemotePath = "data" }, "remote_path"}, - "too many searches": {func(u *Update) { u.SearchesPerHour = 4000 }, "searches_per_hour"}, - } { - u := validUpdate() - tc.edit(&u) - var invalid *InvalidError - if err := u.validate(); !errors.As(err, &invalid) || invalid.Field != tc.field { - t.Errorf("%s: err = %v, want an InvalidError on %s", name, err, tc.field) - } - } - // Switched off, an empty address is a row still being set up. - u := validUpdate() - u.BaseURL = "" - if err := u.validate(); err != nil { - t.Errorf("switched off with no address: %v", err) - } -} - -func TestSourceFor_BuildsTheClientTheProtocolNames(t *testing.T) { - for protocol, check := range map[string]func(Source) bool{ - ProtocolProwlarr: func(s Source) bool { _, ok := s.Search.(*prowlarr.Client); return ok && s.Fetch != nil }, - ProtocolTorznab: func(s Source) bool { _, ok := s.Search.(*indexer.Torznab); return ok && s.Fetch != nil }, - // Exchange's results are all magnets: nothing to fetch through it. - ProtocolExchange: func(s Source) bool { _, ok := s.Search.(*exchange.Client); return ok && s.Fetch == nil }, - } { - src := SourceFor(dbq.AcquisitionService{Code: protocol, Protocol: protocol, SearchesPerHour: 7}) - if !check(src) || src.Code != protocol || src.PerHour != 7 { - t.Errorf("%s: source %+v", protocol, src) - } - } -} - -func TestJobs_AnExchangeDoesWhatItsSwitchesAllow(t *testing.T) { - ex := dbq.AcquisitionService{Kind: KindDownload, Protocol: ProtocolExchange, ServesSearch: true} - if !Searches(ex) || Fetches(ex) { - t.Error("an exchange with only search on should search and not fetch") - } - // A qBittorrent's switches mean nothing: it fetches, whatever they say. - qb := dbq.AcquisitionService{Kind: KindDownload, Protocol: ProtocolQBittorrent, ServesSearch: true} - if Searches(qb) || !Fetches(qb) { - t.Error("a qBittorrent fetches and never searches") - } -} - -func TestTest_NoAddressIsNothingToReach(t *testing.T) { - r := Test(context.Background(), dbq.AcquisitionService{Protocol: ProtocolProwlarr, Kind: KindSearch}) - if r.OK || !strings.Contains(r.Detail, "No address") { - t.Fatalf("result = %+v", r) - } -} - -func TestTest_SearchReportsResultsAndARefusedKey(t *testing.T) { - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("X-Api-Key") != "good" { - w.WriteHeader(http.StatusUnauthorized) - return - } - _, _ = w.Write([]byte(`[{"title": "Radiohead - OK Computer"}]`)) - })) - defer srv.Close() - - row := dbq.AcquisitionService{Kind: KindSearch, Protocol: ProtocolProwlarr, BaseUrl: srv.URL, ApiKey: "good", TimeoutMs: 5000} - if r := Test(context.Background(), row); !r.OK || !strings.Contains(r.Detail, "1 results") { - t.Errorf("a working key: %+v", r) - } - row.ApiKey = "bad" - if r := Test(context.Background(), row); r.OK || !strings.Contains(r.Detail, "refused") { - t.Errorf("a refused key: %+v", r) - } -} - -// fakeExchange answers the three native APIs. -func fakeExchange(t *testing.T) *httptest.Server { - t.Helper() - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.URL.Path == "/api/downloads": - _, _ = w.Write([]byte(`{"enabled": true, "running": true, "inbound_conns": 3, "downloads": []}`)) - case r.URL.Path == "/api/search": - _, _ = w.Write([]byte(`{"total": 0, "results": []}`)) - case strings.HasPrefix(r.URL.Path, "/api/torrents/"): - w.WriteHeader(http.StatusNotFound) - _, _ = w.Write([]byte(`{"error": "unknown torrent"}`)) - default: - w.WriteHeader(http.StatusNotFound) - } - })) - t.Cleanup(srv.Close) - return srv -} - -func TestTest_AnExchangeReportsEachJobSwitchedOn(t *testing.T) { - srv := fakeExchange(t) - row := dbq.AcquisitionService{ - Kind: KindDownload, Protocol: ProtocolExchange, BaseUrl: srv.URL, TimeoutMs: 5000, - ServesDownloads: true, ServesSearch: true, ServesEnrichment: true, LocalPath: t.TempDir(), - } - r := Test(context.Background(), row) - if !r.OK { - t.Fatalf("all three jobs: %+v", r) - } - for _, job := range []string{"Downloads:", "Search:", "Release details:"} { - if !strings.Contains(r.Detail, job) { - t.Errorf("detail %q does not report %s", r.Detail, job) - } - } - row.ServesDownloads, row.ServesEnrichment = false, false - if r := Test(context.Background(), row); strings.Contains(r.Detail, "Downloads:") || !strings.Contains(r.Detail, "Search:") { - t.Errorf("only search on: %+v", r) - } - row.ServesSearch = false - if r := Test(context.Background(), row); r.OK || !strings.Contains(r.Detail, "switched off") { - t.Errorf("every job off: %+v", r) - } -} - -func TestTest_ADownloadClientNeedsAFolderMinstrelCanSee(t *testing.T) { - srv := fakeExchange(t) - row := dbq.AcquisitionService{ - Kind: KindDownload, Protocol: ProtocolExchange, BaseUrl: srv.URL, TimeoutMs: 5000, - ServesDownloads: true, Category: DefaultCategory, - LocalPath: filepath.Join(t.TempDir(), "not-mounted"), - } - r := Test(context.Background(), row) - if r.OK || !strings.Contains(r.Detail, "cannot see its downloads folder") { - t.Fatalf("a missing folder: %+v", r) - } - row.LocalPath = t.TempDir() - if r := Test(context.Background(), row); !r.OK || !strings.Contains(r.Detail, `"minstrel" category`) { - t.Errorf("a visible, writable folder: %+v", r) - } -} - -func TestDownloadClient_WrapsThePathMapping(t *testing.T) { - d, err := DownloadClient(dbq.AcquisitionService{Protocol: ProtocolQBittorrent, LocalPath: "/downloads", RemotePath: "/data"}) - if err != nil { - t.Fatal(err) - } - if _, plain := d.(*downloader.Client); plain { - t.Error("a row naming two folders should come back wrapped in its mapping") - } - if _, err := DownloadClient(dbq.AcquisitionService{Protocol: ProtocolProwlarr}); err == nil { - t.Error("a search protocol is not a download client") - } -} diff --git a/internal/acquire/build.go b/internal/acquire/build.go deleted file mode 100644 index 15760b53..00000000 --- a/internal/acquire/build.go +++ /dev/null @@ -1,45 +0,0 @@ -package acquire - -import ( - "context" - "fmt" - - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// Build makes a Service from the settings as they stand: every enabled -// search source behind one paced Fanout, the first enabled download client, -// and the quality profile. Built afresh for each pass and each request, so a -// changed setting takes effect on the next one. -func Build(ctx context.Context, pool *pgxpool.Pool) (*Service, dbq.AcquisitionSetting, error) { - q := dbq.New(pool) - settings, err := q.GetAcquisitionSettings(ctx) - if err != nil { - return nil, settings, fmt.Errorf("acquire: read settings: %w", err) - } - sources, err := Sources(ctx, pool) - if err != nil { - return nil, settings, fmt.Errorf("acquire: read search sources: %w", err) - } - clients, err := usableServices(ctx, q, JobDownload) - if err != nil { - return nil, settings, fmt.Errorf("acquire: read download clients: %w", err) - } - s := &Service{Profile: release.NewProfile(settings.AllowedQualities), Memory: NewMemory(pool)} - fan := &Fanout{Sources: sources, Pace: SearchPace} - if len(sources) > 0 { - s.Search = fan - } - if len(clients) > 0 { - row := clients[0] - client, err := DownloadClient(row) - if err != nil { - return nil, settings, fmt.Errorf("acquire: download client %s: %w", row.Code, err) - } - s.Grabber = &Downloader{Client: client, Code: row.Code, Landing: row.LocalPath, Fetch: fan} - } - return s, settings, nil -} diff --git a/internal/acquire/fanout.go b/internal/acquire/fanout.go deleted file mode 100644 index c4f607c8..00000000 --- a/internal/acquire/fanout.go +++ /dev/null @@ -1,164 +0,0 @@ -package acquire - -import ( - "context" - "errors" - "fmt" - "strings" - "sync" - - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -// Searching every source at once. A source that is down narrows a search rather -// than failing it: the results come back with the list of who did not answer. - -// SourceFailure is one source that could not be reached. -type SourceFailure struct { - Code string - Label string - Err error -} - -// PartialError is a search that reached some sources and not others. It is -// returned alongside the results, not instead of them. -type PartialError struct { - Failures []SourceFailure - // Answered is how many sources did answer. Zero means the search failed. - Answered int -} - -func (e *PartialError) Error() string { - if e == nil || len(e.Failures) == 0 { - return "some sources did not answer" - } - parts := make([]string, 0, len(e.Failures)) - for _, f := range e.Failures { - parts = append(parts, fmt.Sprintf("%s: %s", f.Label, indexer.FirstLine(f.Err.Error()))) - } - return "some sources did not answer — " + strings.Join(parts, "; ") -} - -// Unwrap lets errors.Is find indexer.ErrUnreachable through a partial failure. -func (e *PartialError) Unwrap() []error { - if e == nil { - return nil - } - out := make([]error, 0, len(e.Failures)) - for _, f := range e.Failures { - out = append(out, f.Err) - } - return out -} - -// Fanout searches every source and merges what comes back. It is both the -// Searcher and the Fetcher, routing a .torrent fetch to the source that -// offered the result. -type Fanout struct { - // Sources in code order: the fold keeps a duplicate's first arrival, so - // the order decides which source a grab is fetched through. - Sources []Source - // Pace spaces each source's searches. Nil paces nothing. - Pace *Pacer -} - -// Search asks every source concurrently. Each client bounds its own call by -// its configured timeout, so there is no shared deadline here. -func (f *Fanout) Search(ctx context.Context, query indexer.Query) ([]indexer.Result, error) { - if f == nil || len(f.Sources) == 0 { - return nil, indexer.ErrUnconfigured - } - found := make([][]indexer.Result, len(f.Sources)) - errs := make([]error, len(f.Sources)) - var wg sync.WaitGroup - for i, src := range f.Sources { - if src.Search == nil { - continue - } - f.Pace.spend(src) - wg.Add(1) - go func(i int, src Source) { - defer wg.Done() - found[i], errs[i] = src.Search.Search(ctx, query) - }(i, src) - } - wg.Wait() - - out := make([]indexer.Result, 0, 32) - var failures []SourceFailure - answered, unconfigured := 0, 0 - for i, src := range f.Sources { - if src.Search == nil { - continue - } - if errors.Is(errs[i], indexer.ErrUnconfigured) { - unconfigured++ - continue - } - if rl, ok := indexer.AsRateLimited(errs[i]); ok { - f.Pace.hold(src.Code, rl) - } - if errs[i] != nil { - failures = append(failures, SourceFailure{Code: src.Code, Label: src.Label, Err: errs[i]}) - continue - } - answered++ - for j := range found[i] { - if found[i][j].Source == "" { - found[i][j].Source = src.Code - } - } - out = append(out, found[i]...) - } - switch { - case answered == 0 && len(failures) > 0: - return nil, &PartialError{Failures: failures} - case len(failures) > 0: - return out, &PartialError{Failures: failures, Answered: answered} - case answered == 0 && unconfigured > 0: - return nil, indexer.ErrUnconfigured - } - return out, nil -} - -// Download fetches a result's .torrent from the source that offered it: a -// Prowlarr link carries Prowlarr's key and only Prowlarr can redeem it. -func (f *Fanout) Download(ctx context.Context, r indexer.Result) ([]byte, error) { - if f == nil { - return nil, indexer.ErrUnconfigured - } - for _, src := range f.Sources { - if src.Code != r.Source { - continue - } - if src.Fetch == nil { - return nil, fmt.Errorf("%w: %s cannot fetch torrent files", indexer.ErrUnconfigured, src.Label) - } - return src.Fetch.Download(ctx, r) - } - if r.Source == "" { - return nil, fmt.Errorf("%w: this result does not say which source it came from", indexer.ErrUnconfigured) - } - return nil, fmt.Errorf("%w: %q is no longer a configured source", indexer.ErrUnconfigured, r.Source) -} - -// Narrowing is one source that did not answer, as a page shows it. -type Narrowing struct { - Source string `json:"source"` - Label string `json:"label"` - Reason string `json:"reason"` -} - -// narrowings reads a PartialError out of err, and whether any source -// answered. Nil when err is not a partial failure. -func narrowings(err error) ([]Narrowing, bool) { - var partial *PartialError - if !errors.As(err, &partial) || len(partial.Failures) == 0 { - return nil, false - } - out := make([]Narrowing, 0, len(partial.Failures)) - for _, f := range partial.Failures { - out = append(out, Narrowing{Source: f.Code, Label: f.Label, Reason: indexer.FirstLine(f.Err.Error())}) - } - return out, partial.Answered > 0 -} diff --git a/internal/acquire/grab.go b/internal/acquire/grab.go deleted file mode 100644 index 001814bd..00000000 --- a/internal/acquire/grab.go +++ /dev/null @@ -1,338 +0,0 @@ -package acquire - -import ( - "context" - "errors" - "path" - "strings" - "sync" - "time" - - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// Taking a release. One live grab per want: a second is tie-broken against -// the download already running. - -// Tag goes on every torrent Minstrel adds, so they can be told apart from -// anything else in the client. -const Tag = "minstrel" - -// MissingClient is Taken.Missing when no download client is set up. -const MissingClient = "download_client" - -// nearlyDone is how far along a running download stays whatever would -// replace it. -const nearlyDone = 0.9 - -// Downloader is the grab half of a Service. -type Downloader struct { - Client downloader.Driver - // Code is the client's acquisition_services row. - Code string - // Landing is the client's downloads folder as Minstrel sees it; each - // grab lands in a folder of its own under it. - Landing string - // Fetch gets a result's .torrent from the source that offered it. - Fetch Fetcher -} - -// Origin says who took a release. -type Origin struct { - By string // OriginSchedule or OriginOperator - User pgtype.UUID -} - -// Contest is how a grab fared against the download already running. -type Contest struct { - // Kept: the running one stays and this grab was not made. Replaced: - // this grab took its place. - Kept, Replaced bool - Reason string - Rival dbq.Grab -} - -// Taken is what came of a grab. -type Taken struct { - Chosen *Candidate - // Added is the only field that means a download started. - Added bool - SavePath string - Grab dbq.Grab - // Gone: the search no longer offers the release. - Gone bool - // Refused: not taken; the reason is on Chosen. - Refused bool - // Unconfigured: Missing names what is not set up ("" for search). - Unconfigured bool - Missing string - Contest *Contest - Error string -} - -// Take searches again and grabs the candidate named by key: a person's -// grab. Searching again keeps the indexer's download link (which can carry -// its key) on the server, and finds a release gone since it was shown. -// force takes what the verdict would not. -func (s *Service) Take(ctx context.Context, want Want, key string, force bool, origin Origin) Taken { - if s.Grabber == nil { - return Taken{Unconfigured: true, Missing: MissingClient} - } - found := s.For(ctx, want) - switch { - case found.Unconfigured: - return Taken{Unconfigured: true} - case found.Error != "": - return Taken{Error: found.Error} - } - chosen := pick(found.Candidates, key) - if chosen == nil { - return Taken{Gone: true} - } - return s.Grab(ctx, want, chosen, found.Candidates, force, origin) -} - -// Grab hands a judged candidate to the download client. The schedule calls -// it with the candidate it has just judged; Take calls it after searching -// again. -func (s *Service) Grab(ctx context.Context, want Want, chosen *Candidate, all []Candidate, force bool, origin Origin) Taken { - out := Taken{Chosen: chosen} - if s.Grabber == nil || s.Grabber.Client == nil { - out.Unconfigured, out.Missing = true, MissingClient - return out - } - if chosen.Verdict == Blocked || (chosen.Verdict != Grab && !force) { - out.Refused = true - return out - } - - unlock := lockWant(want.ID) - defer unlock() - contest := s.contest(ctx, want, chosen, all, origin) - if contest != nil && contest.Kept { - out.Contest = contest - return out - } - - add := downloader.Add{Magnet: chosen.Source.Magnet, Tags: []string{Tag}} - hash := chosen.InfoHash - if add.Magnet == "" { - if s.Grabber.Fetch == nil { - out.Error = "This release is a .torrent link, and nothing is set up to fetch it." - return out - } - body, err := s.Grabber.Fetch.Download(ctx, chosen.Source) - if magnet, ok := indexer.AsMagnet(err); ok { - add.Magnet, err = magnet, nil - } - if err != nil { - out.Error = indexer.FirstLine(err.Error()) - return out - } - if add.Magnet == "" { - add.Torrent = body - if meta, err := release.ParseTorrent(body); err == nil { - if hash == "" { - hash = meta.InfoHash - } - if refused := s.judgeFiles(ctx, want, chosen, hash, meta.Files, force); refused { - out.Refused = true - return out - } - } - } - } - if hash == "" && add.Magnet != "" { - hash = indexer.MagnetHash(add.Magnet) - } - add.SavePath = landingFor(s.Grabber.Landing, hash, chosen.Name) - - err := s.Grabber.Client.AddTorrent(ctx, add) - if errors.Is(err, downloader.ErrDuplicate) && s.heldElsewhere(ctx, hash) { - why := "Already in the download client outside Minstrel's category, where Minstrel cannot follow it (" + - strings.TrimPrefix(indexer.FirstLine(err.Error()), downloader.ErrDuplicate.Error()+": ") + ")" - if force { - out.Error = why + "." - return out - } - set(chosen, Blocked, Hard, CodeBlocked, why+".") - if s.Memory != nil && hash != "" { - _ = s.Memory.Block(ctx, dbq.BlockReleaseParams{ - InfoHash: hash, Name: chosen.Name, Reason: BlockGone, Detail: why + ".", WantID: want.ID, - ExpiresAt: pgtype.Timestamptz{Time: time.Now().Add(GoneBlock), Valid: true}, - }) - } - out.Refused = true - return out - } - if err != nil && !errors.Is(err, downloader.ErrDuplicate) { - if errors.Is(err, downloader.ErrUnconfigured) { - out.Unconfigured, out.Missing = true, MissingClient - } else { - out.Error = indexer.FirstLine(err.Error()) - } - return out - } - out.Added, out.SavePath = true, add.SavePath - if s.Memory == nil { - return out - } - - var replaced pgtype.UUID - why := "" - if contest != nil && contest.Replaced { - out.Contest = contest - replaced, why = contest.Rival.ID, "Replaced by "+chosen.Name+": "+contest.Reason+"." - } - g, err := s.Memory.Grabbed(ctx, dbq.InsertGrabParams{ - WantID: want.ID, InfoHash: hash, Name: chosen.Name, Key: Key(*chosen), Indexer: chosen.Indexer, - Source: chosen.Source.Source, Client: s.Grabber.Code, SavePath: add.SavePath, - Quality: string(chosen.Tier), Origin: origin.By, Forced: force, GrabbedBy: origin.User, - }, replaced, why) - if err != nil { - out.Error = "The download started, but could not be recorded: " + indexer.FirstLine(err.Error()) - return out - } - out.Grab = g - // Removed only after the new download was added and recorded, so a - // failure never leaves the want with nothing running. - if replaced.Valid && contest.Rival.InfoHash != "" { - if err := s.Grabber.Client.Delete(ctx, true, contest.Rival.InfoHash); err != nil { - contest.Reason += ". The download it replaced could not be removed from the client: " + indexer.FirstLine(err.Error()) - } - } - return out -} - -// heldElsewhere reports whether a release the client says it already holds is -// one it does not list for Minstrel: held under another category. Recorded -// as a grab, it would read as gone half an hour later and be taken again, -// every pass. A client that -// cannot answer now is given the benefit of the doubt, as the add was. -func (s *Service) heldElsewhere(ctx context.Context, hash string) bool { - if hash == "" { - return false - } - listed, err := s.Grabber.Client.Torrents(ctx, indexer.NormalizeHash(hash)) - return err == nil && len(listed) == 0 -} - -// judgeFiles judges a chosen release again on its .torrent's file list, and -// sets it aside when the files say it is not the album, so no later search -// offers it again. force still takes it. -func (s *Service) judgeFiles(ctx context.Context, want Want, chosen *Candidate, hash string, files []release.File, force bool) bool { - chosen.Files = files - if chosen.FilesCount == 0 { - chosen.FilesCount = len(files) - } - veto, reason, _ := release.JudgeFiles(want.Want, files) - if veto == "" || force { - return false - } - set(chosen, Rejected, Hard, veto, reason) - if s.Memory != nil && hash != "" { - _ = s.Memory.Block(ctx, dbq.BlockReleaseParams{ - InfoHash: hash, Name: chosen.Name, Reason: BlockFiles, Detail: reason, WantID: want.ID, - }) - } - return true -} - -var wantLocks sync.Map // want id → *sync.Mutex - -// lockWant settles two grabs for one want one after the other. The -// one-live-grab index is the backstop for anything outside this process. -func lockWant(id pgtype.UUID) func() { - if !id.Valid { - return func() {} - } - m, _ := wantLocks.LoadOrStore(id.Bytes, &sync.Mutex{}) - mu := m.(*sync.Mutex) - mu.Lock() - return mu.Unlock -} - -// contest settles a grab against the download running for its want: a -// person's grab beats the schedule's; between two of a kind the better -// candidate wins; a tie, a download nearly done, or one that cannot be -// removed stays. Nil when nothing is running. -func (s *Service) contest(ctx context.Context, want Want, chosen *Candidate, all []Candidate, origin Origin) *Contest { - if s.Memory == nil || !want.ID.Valid { - return nil - } - running, ok, err := s.Memory.LiveGrab(ctx, want.ID) - if err != nil || !ok { - return nil - } - c := &Contest{Rival: running} - if running.InfoHash != "" && running.InfoHash == chosen.InfoHash { - c.Kept, c.Reason = true, "That release is already downloading for this album." - return c - } - switch { - case origin.By == OriginOperator && running.Origin == OriginSchedule: - c.Reason = "you chose it over the one the schedule started" - case origin.By == OriginSchedule && running.Origin == OriginOperator: - c.Kept, c.Reason = true, "A download you started for this album is already running." - return c - default: - var theirs *Candidate - for i := range all { - if (running.InfoHash != "" && all[i].InfoHash == running.InfoHash) || Key(all[i]) == running.Key { - theirs = &all[i] - break - } - } - if theirs == nil || !better(*chosen, *theirs) { - c.Kept, c.Reason = true, "A download for this album is already running, and this release is no better." - return c - } - c.Reason = "it ranks above the one that was running" - } - if running.InfoHash == "" { - c.Kept, c.Reason = true, "A download for this album is already running, and it cannot be identified to replace it." - return c - } - if ts, err := s.Grabber.Client.Torrents(ctx, running.InfoHash); err == nil && len(ts) > 0 && ts[0].Progress >= nearlyDone { - c.Kept, c.Reason = true, "A download for this album is already running and nearly done." - return c - } - c.Replaced = true - return c -} - -// landingFor is the folder a download lands in: one per torrent, named by -// its infohash, so two grabs never unpack into the same place. -func landingFor(root, hash, name string) string { - dir := hash - if dir == "" { - dir = slug(name) - } - if dir == "" { - dir = "unnamed" - } - return path.Join(root, dir) -} - -// slug keeps a folder name to characters every filesystem in the chain -// takes. -func slug(s string) string { - var b strings.Builder - for _, r := range strings.ToLower(strings.TrimSpace(s)) { - if b.Len() >= 80 { - break - } - switch { - case r >= 'a' && r <= 'z', r >= '0' && r <= '9': - b.WriteRune(r) - case r == '-', r == '_', r == ' ', r == '.': - b.WriteRune('-') - } - } - return strings.Trim(b.String(), "-") -} diff --git a/internal/acquire/memory.go b/internal/acquire/memory.go deleted file mode 100644 index 8e553e9f..00000000 --- a/internal/acquire/memory.go +++ /dev/null @@ -1,199 +0,0 @@ -package acquire - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "time" - - "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" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// Block reasons, matching release_blocks_reason_check. -const ( - BlockOperator = "operator" - BlockFiles = "files" - // BlockImport: the import found its files were not the album. - BlockImport = "import" - // BlockStalled: given up on because it would not finish; held - // back until the block's expires_at. - BlockStalled = "stalled" - // BlockGone: its download client would not let Minstrel follow it, held - // outside Minstrel's category or leaving the client twice; held - // back for GoneBlock. - BlockGone = "gone" -) - -// GoneBlock is how long a release its client lost track of is held back: -// long enough that a release which keeps vanishing is tried once a day rather -// than on every pass, short enough that it is taken once the client lets go. -const GoneBlock = 24 * time.Hour - -// Grab origins, matching grabs_origin_check. -const ( - OriginSchedule = "schedule" - OriginOperator = "operator" -) - -// Memory is what searches and grabs leave behind: every candidate of a -// want's last search, the grabs, and the releases set aside. -type Memory struct { - pool *pgxpool.Pool -} - -// NewMemory returns a Memory over pool. -func NewMemory(pool *pgxpool.Pool) *Memory { return &Memory{pool: pool} } - -// Keep replaces a want's candidates with this search's, in order. -func (m *Memory) Keep(ctx context.Context, wantID pgtype.UUID, cands []Candidate) error { - return pgx.BeginFunc(ctx, m.pool, func(tx pgx.Tx) error { - q := dbq.New(tx) - if err := q.DeleteWantCandidates(ctx, wantID); err != nil { - return err - } - for i, c := range cands { - reasons, err := json.Marshal(c.Reasons) - if err != nil { - return err - } - if c.Reasons == nil { - reasons = []byte("[]") - } - folded := c.FoldedFrom - if folded == nil { - folded = []string{} - } - if err := q.InsertWantCandidate(ctx, dbq.InsertWantCandidateParams{ - WantID: wantID, Key: Key(c), Name: c.Name, InfoHash: c.InfoHash, Indexer: c.Indexer, - Source: c.Source.Source, SizeBytes: c.SizeBytes, Seeders: int32(c.Seeders), - FilesCount: int32(c.FilesCount), Format: release.Describe(c.Release), Quality: string(c.Tier), - Year: int32(c.Year), Verdict: string(c.Verdict), Reason: c.Reason, Reasons: reasons, - FoldedFrom: folded, Rank: int32(i), - }); err != nil { - return err - } - } - return nil - }) -} - -// blocked is every release set aside, by infohash, with the sentence a -// candidate shows. A failed read blocks nothing: offering a release set -// aside costs one grab a person can undo, while blocking everything on a -// database blip would hide every result. -func (m *Memory) blocked(ctx context.Context) map[string]string { - if m == nil { - return nil - } - rows, err := dbq.New(m.pool).ListReleaseBlocks(ctx) - if err != nil { - return nil - } - out := make(map[string]string, len(rows)) - for _, r := range rows { - out[indexer.NormalizeHash(r.InfoHash)] = BlockSentence(r.Reason, r.Detail) - } - return out -} - -// BlockSentence is what a blocked candidate says. -func BlockSentence(reason, detail string) string { - switch reason { - case BlockFiles: - if detail != "" { - return "Set aside for what its files hold: " + detail - } - return "Set aside for what its files hold." - case BlockImport: - if detail != "" { - return "Refused on import: " + detail - } - return "Refused on import: its files were not the album." - case BlockGone: - if detail != "" { - return "Held back for now: " + detail - } - return "Held back for now: its download client lost track of it." - case BlockStalled: - if detail != "" { - return "Given up on for now: " + detail - } - return "Given up on for now: it would not finish." - } - return "You set this release aside." -} - -// Block sets a release aside and marks every kept candidate of it blocked. -func (m *Memory) Block(ctx context.Context, p dbq.BlockReleaseParams) error { - p.InfoHash = indexer.NormalizeHash(p.InfoHash) - if p.InfoHash == "" { - return errors.New("a release without an infohash cannot be set aside") - } - return pgx.BeginFunc(ctx, m.pool, func(tx pgx.Tx) error { - q := dbq.New(tx) - if err := q.BlockRelease(ctx, p); err != nil { - return err - } - return q.MarkCandidatesBlocked(ctx, dbq.MarkCandidatesBlockedParams{ - InfoHash: p.InfoHash, Reason: BlockSentence(p.Reason, p.Detail), - }) - }) -} - -// Unblock lets a release be taken again. Its kept candidates wait for a -// person until the next search judges them afresh. -func (m *Memory) Unblock(ctx context.Context, hash string) (bool, error) { - hash = indexer.NormalizeHash(hash) - var n int64 - err := pgx.BeginFunc(ctx, m.pool, func(tx pgx.Tx) error { - q := dbq.New(tx) - var err error - if n, err = q.UnblockRelease(ctx, hash); err != nil { - return err - } - return q.MarkCandidatesUnblocked(ctx, dbq.MarkCandidatesUnblockedParams{ - InfoHash: hash, Reason: "Allowed again. The next search judges it afresh.", - }) - }) - return n > 0, err -} - -// LiveGrab is the download running for a want, if there is one. -func (m *Memory) LiveGrab(ctx context.Context, wantID pgtype.UUID) (dbq.Grab, bool, error) { - g, err := dbq.New(m.pool).GetLiveGrab(ctx, wantID) - if errors.Is(err, pgx.ErrNoRows) { - return g, false, nil - } - if err != nil { - return g, false, err - } - return g, true, nil -} - -// Grabbed records a download started, ending the grab it replaced first so -// the one-live-grab index holds. -func (m *Memory) Grabbed(ctx context.Context, p dbq.InsertGrabParams, replaced pgtype.UUID, why string) (dbq.Grab, error) { - var g dbq.Grab - err := pgx.BeginFunc(ctx, m.pool, func(tx pgx.Tx) error { - q := dbq.New(tx) - if replaced.Valid { - if err := q.EndGrab(ctx, dbq.EndGrabParams{ID: replaced, EndReason: "superseded", EndDetail: why}); err != nil { - return err - } - } - var err error - g, err = q.InsertGrab(ctx, p) - return err - }) - if err != nil { - return g, fmt.Errorf("record grab: %w", err) - } - return g, nil -} diff --git a/internal/acquire/pace.go b/internal/acquire/pace.go deleted file mode 100644 index fb2352e3..00000000 --- a/internal/acquire/pace.go +++ /dev/null @@ -1,145 +0,0 @@ -package acquire - -import ( - "context" - "sync" - "time" - - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -// SearchPace is the one Pacer every search shares. Process-wide because a -// Fanout is built fresh for each search; a Pacer per Fanout would start empty -// and pace nothing. -var SearchPace = &Pacer{} - -// holdWithoutRetryAfter is how long a source that answered 429 without saying -// for how long is left alone: one schedule tick. -const holdWithoutRetryAfter = 10 * time.Minute - -// Pacer spaces the searches sent to each source by its searches_per_hour: a -// spacing, not a burst bucket, because Prowlarr answers 429 to a few requests -// in quick succession. Every search spends, the schedule's and a -// person's alike; only the schedule waits. -type Pacer struct { - // Now and Sleep are the clock, replaceable in tests. - Now func() time.Time - Sleep func(context.Context, time.Duration) error - - mu sync.Mutex - next map[string]time.Time -} - -func (p *Pacer) now() time.Time { - if p.Now != nil { - return p.Now() - } - return time.Now() -} - -func (p *Pacer) sleep(ctx context.Context, d time.Duration) error { - if p.Sleep != nil { - return p.Sleep(ctx, d) - } - t := time.NewTimer(d) - defer t.Stop() - select { - case <-t.C: - return nil - case <-ctx.Done(): - return ctx.Err() - } -} - -// gap is the spacing a source's number asks for; zero is unpaced. -func gap(perHour int) time.Duration { - if perHour <= 0 { - return 0 - } - return time.Hour / time.Duration(perHour) -} - -// readyAt is when every source may next be asked. -func (p *Pacer) readyAt(sources []Source) time.Time { - p.mu.Lock() - defer p.mu.Unlock() - var at time.Time - for _, s := range sources { - if s.Search == nil { - continue - } - if n := p.next[s.Code]; n.After(at) { - at = n - } - } - return at -} - -// Wait holds until every source may be asked, and answers false, without -// waiting, when that is after the deadline or ctx ends first. All of them, -// not any: a search while one is held back would be narrowed, and the want -// recorded as looked for. -func (p *Pacer) Wait(ctx context.Context, sources []Source, deadline time.Time) bool { - if p == nil { - return true - } - at := p.readyAt(sources) - if !deadline.IsZero() && at.After(deadline) { - return false - } - if d := at.Sub(p.now()); d > 0 { - return p.sleep(ctx, d) == nil - } - return ctx.Err() == nil -} - -// spend takes one search's slot from a source. -func (p *Pacer) spend(s Source) { - g := gap(s.PerHour) - if p == nil || g == 0 { - return - } - p.mu.Lock() - defer p.mu.Unlock() - if p.next == nil { - p.next = map[string]time.Time{} - } - at := p.next[s.Code] - if t := p.now(); at.Before(t) { - at = t - } - p.next[s.Code] = at.Add(g) -} - -// hold leaves a source alone after it answered 429, for as long as it asked, -// whatever its searches_per_hour says. -func (p *Pacer) hold(code string, rl *indexer.RateLimited) { - if p == nil { - return - } - d := rl.RetryAfter - if d <= 0 { - d = holdWithoutRetryAfter - } - p.mu.Lock() - defer p.mu.Unlock() - if p.next == nil { - p.next = map[string]time.Time{} - } - if until := p.now().Add(d); until.After(p.next[code]) { - p.next[code] = until - } -} - -// Ready holds until every source this service searches may be asked again, -// and answers false when that is past the deadline. The schedule calls it -// before each want; a person's search never does. -func (s *Service) Ready(ctx context.Context, deadline time.Time) bool { - if s == nil { - return ctx.Err() == nil - } - if f, ok := s.Search.(*Fanout); ok && f != nil { - return f.Pace.Wait(ctx, f.Sources, deadline) - } - return ctx.Err() == nil -} diff --git a/internal/acquire/search.go b/internal/acquire/search.go deleted file mode 100644 index 73960b3a..00000000 --- a/internal/acquire/search.go +++ /dev/null @@ -1,408 +0,0 @@ -package acquire - -import ( - "context" - "errors" - "sort" - "strings" - - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// One search for one want, and what could be done about each result. -// internal/release -// says whether a name is the album and what quality it is; this package -// asks in the right order and says why in words a person can act on. - -// Verdict is what can be done about one result. -type Verdict string - -const ( - // Grab: the album, in a quality the profile takes. - Grab Verdict = "grab" - // Review: maybe the album; a person decides. - Review Verdict = "review" - // Refused: the album, in a quality the profile does not take. The only - // verdict that names a setting. - Refused Verdict = "refused" - // Rejected: not the album. - Rejected Verdict = "rejected" - // Blocked: a release set aside, by a person or by its files. - Blocked Verdict = "blocked" -) - -// order sorts verdicts: what can be taken first, what was given up on last. -var order = map[Verdict]int{Grab: 0, Review: 1, Refused: 2, Rejected: 3, Blocked: 4} - -// Reason kinds: a hard reason is a fact about the bytes, a soft one about the -// moment or a setting. -const ( - Hard = "hard" - Soft = "soft" -) - -// Reason codes beyond internal/release's vetoes. -const ( - CodeBlocked = "blocked" - CodeProfile = "profile" - CodeNoSeeders = "no_seeders" - CodeDoubt = "doubt" - // CodeNotBetter: the album, but no better than the copy the library - // holds, for a want that is an upgrade. - CodeNotBetter = "not_better" -) - -// Reason is one thing a verdict rests on. -type Reason struct { - Kind string `json:"kind"` - Code string `json:"code"` - Text string `json:"text"` -} - -// Want is the album searched for: its wants row and how the catalogue -// describes it. -type Want struct { - ID pgtype.UUID - release.Want - // Floor is the tier of the copy the library holds, for an album wanted - // for a better one: a release must rank above it. "" for an - // album not held, and for a video rip, which any clean copy beats. - Floor release.Tier - // Held says the floor in words: "MP3, about 192 kbps". - Held string -} - -// Candidate is one result with everything decided about it. -type Candidate struct { - release.Release - // Source is the indexer's own result. It never leaves the server: its - // DownloadURL can carry the indexer's key. - Source indexer.Result - InfoHash string - Indexer string - SizeBytes int64 - Seeders int - SwarmKnown bool - Category int - FilesCount int - // Files is the torrent's file list once its .torrent has been read. - Files []release.File - FoldedFrom []string - - Tier release.Tier - YearAgrees bool - Verdict Verdict - Reason string - Reasons []Reason -} - -// Key is how a candidate is named across a search and a grab: its infohash, -// or its name when the source gave none. -func Key(c Candidate) string { - if c.InfoHash != "" { - return c.InfoHash - } - return c.Name -} - -// Result is one search. -type Result struct { - Query string - // Unconfigured: no search source is set up. - Unconfigured bool - // Error is a search that failed outright. - Error string - // Narrowed names sources that did not answer on a search that otherwise - // did. - Narrowed []Narrowing - Considered int - Folded int - // Looked is how many .torrent files were read for their file lists. - Looked int - Candidates []Candidate - // KeepError: the candidates could not be stored. - KeepError string -} - -// Grabbable is what may be taken, best first. -func (r Result) Grabbable() []Candidate { - var out []Candidate - for _, c := range r.Candidates { - if c.Verdict == Grab { - out = append(out, c) - } - } - return out -} - -// ForAPerson says whether anything found is worth a person's look: a -// release that may be the album, whatever its quality. -func (r Result) ForAPerson() bool { - for _, c := range r.Candidates { - if c.Verdict == Grab || c.Verdict == Review || c.Verdict == Refused { - return true - } - } - return false -} - -// Service searches and grabs. -type Service struct { - // Search is nil when no source is configured. - Search Searcher - // Grabber is nil when no download client is configured. - Grabber *Downloader - Profile release.Profile - // Memory stores candidates, grabs and blocks. Nil keeps nothing. - Memory *Memory -} - -// Query is the text sent to the indexers: the artist and the title, without -// a trailing bracketed part the release names usually leave off. -func Query(w release.Want) string { - title := w.Title - if i := strings.IndexAny(title, "(["); i > 0 { - title = title[:i] - } - return strings.Join(strings.Fields(strings.TrimSpace(w.Artist)+" "+strings.TrimSpace(title)), " ") -} - -// For searches for one want and judges everything that comes back. It never -// returns an error: a search that could not be made is a Result saying so. -func (s *Service) For(ctx context.Context, want Want) Result { - r := Result{Query: Query(want.Want)} - if s == nil || s.Search == nil { - r.Unconfigured = true - return r - } - found, err := s.Search.Search(ctx, indexer.Query{Text: r.Query, Categories: indexer.MusicCategories}) - // A partial search arrives as results and an error together. - if narrowed, answered := narrowings(err); narrowed != nil && answered { - r.Narrowed, err = narrowed, nil - } - switch { - case errors.Is(err, indexer.ErrUnconfigured): - r.Unconfigured = true - return r - case err != nil: - r.Error = indexer.FirstLine(err.Error()) - return r - } - r.Considered = len(found) - cands, folded := fold(found) - r.Folded = folded - - blocked := s.Memory.blocked(ctx) - for i := range cands { - decide(&cands[i], want, s.Profile, blocked) - } - sortCandidates(cands) - for _, i := range s.peek(ctx, cands) { - decide(&cands[i], want, s.Profile, blocked) - r.Looked++ - } - sortCandidates(cands) - r.Candidates = cands - if s.Memory != nil && want.ID.Valid { - if err := s.Memory.Keep(ctx, want.ID, cands); err != nil { - r.KeepError = indexer.FirstLine(err.Error()) - } - } - return r -} - -// decide works out one candidate's verdict, in order: given up on, then -// whether it is the album, then whether the profile takes its quality, then -// whether anything leaves it to a person. A quality refusal said about the -// wrong album would be noise, so identity comes first. -func decide(c *Candidate, want Want, profile release.Profile, blocked map[string]string) { - w := want.Want - c.Verdict, c.Reason, c.Reasons = "", "", nil - if why, ok := blocked[c.InfoHash]; ok && c.InfoHash != "" { - set(c, Blocked, Hard, CodeBlocked, why) - return - } - m := release.Judge(w, c.Release) - c.YearAgrees = m.YearAgrees - if m.Veto != "" { - set(c, Rejected, Hard, m.Veto, m.Reason) - return - } - doubt := m.Doubt - if len(c.Files) > 0 { - if in := release.Inspect(c.Files); in.Format != "" { - c.Format = in.Format - } - veto, reason, fileDoubt := release.JudgeFiles(w, c.Files) - if veto != "" { - set(c, Rejected, Hard, veto, reason) - return - } - if doubt == "" { - doubt = fileDoubt - } - } - c.Tier = release.TierOf(c.Release, c.Category) - if !profile.Allows(c.Tier) { - set(c, Refused, Soft, CodeProfile, "Your quality settings don’t take "+strings.ToLower(release.Label(c.Tier))+".") - return - } - if want.Floor != "" && release.Rank(c.Tier) <= release.Rank(want.Floor) { - held := want.Held - if held == "" { - held = strings.ToLower(release.Label(want.Floor)) - } - set(c, Refused, Soft, CodeNotBetter, "No better than the copy you have ("+held+").") - return - } - if doubt != "" { - set(c, Review, Soft, CodeDoubt, doubt) - return - } - if c.Seeders == 0 { - set(c, Review, Soft, CodeNoSeeders, "Nobody is sharing it right now.") - return - } - c.Verdict = Grab - c.Reason = "The album, " + strings.ToLower(release.Label(c.Tier)) + "." - if d := release.Describe(c.Release); d != "" { - c.Reason = "The album, " + d + "." - } -} - -func set(c *Candidate, v Verdict, kind, code, text string) { - c.Verdict, c.Reason = v, text - c.Reasons = append(c.Reasons, Reason{Kind: kind, Code: code, Text: text}) -} - -// sortCandidates puts the best first, stably so the same search proposes the -// same grab twice. -func sortCandidates(cands []Candidate) { - sort.SliceStable(cands, func(a, b int) bool { return better(cands[a], cands[b]) }) -} - -// better says whether x ranks above y: verdict, then quality, then how many -// share it, then whether its year agrees. -func better(x, y Candidate) bool { - if order[x.Verdict] != order[y.Verdict] { - return order[x.Verdict] < order[y.Verdict] - } - if rx, ry := release.Rank(x.Tier), release.Rank(y.Tier); rx != ry { - return rx > ry - } - if x.Seeders != y.Seeders { - return x.Seeders > y.Seeders - } - return x.YearAgrees && !y.YearAgrees -} - -// fold merges results that are the same torrent, by infohash. Prowlarr -// aggregates, so one release arrives once per tracker that lists it. A -// result without a hash is never folded: an absent hash is not a shared one. -func fold(found []indexer.Result) ([]Candidate, int) { - out := make([]Candidate, 0, len(found)) - at := make(map[string]int, len(found)) - folded := 0 - for _, f := range found { - if strings.TrimSpace(f.Title) == "" { - continue - } - hash := indexer.NormalizeHash(f.InfoHash) - if hash == "" && f.Magnet != "" { - hash = indexer.MagnetHash(f.Magnet) - } - if hash != "" { - if i, seen := at[hash]; seen { - folded++ - if f.Indexer != "" { - out[i].FoldedFrom = append(out[i].FoldedFrom, f.Indexer) - } - // A measured swarm beats a reported one; among like, the - // highest count. - switch { - case f.SwarmKnown && !out[i].SwarmKnown: - out[i].Seeders, out[i].SwarmKnown = f.Seeders, true - case f.SwarmKnown == out[i].SwarmKnown && f.Seeders > out[i].Seeders: - out[i].Seeders = f.Seeders - } - if f.FilesCount > 0 && out[i].FilesCount == 0 { - out[i].FilesCount = f.FilesCount - } - continue - } - at[hash] = len(out) - } - out = append(out, Candidate{ - Release: release.Parse(f.Title), Source: f, InfoHash: hash, Indexer: f.Indexer, - SizeBytes: f.SizeBytes, Seeders: f.Seeders, SwarmKnown: f.SwarmKnown, - Category: f.Category, FilesCount: f.FilesCount, - }) - } - return out, folded -} - -// maxPeeks is how many .torrent files one search reads. Few: each goes -// through Prowlarr to a tracker, and Prowlarr answers 429 to a burst. -const maxPeeks = 3 - -// peek reads the .torrent of candidates that are the album but whose name -// states no format, so their files can say what they are; it returns which -// now carry a file list. A failed fetch leaves the candidate as it was. -func (s *Service) peek(ctx context.Context, cands []Candidate) []int { - f, ok := s.Search.(Fetcher) - if !ok { - return nil - } - var looked []int - for i := range cands { - if len(looked) >= maxPeeks || ctx.Err() != nil { - break - } - c := &cands[i] - if c.Tier != release.TierUnknown || (c.Verdict != Grab && c.Verdict != Refused) || - c.Files != nil || c.Source.DownloadURL == "" || c.Source.Magnet != "" { - continue - } - body, err := f.Download(ctx, c.Source) - if err != nil { - continue - } - meta, err := release.ParseTorrent(body) - if err != nil { - continue - } - c.Files = meta.Files - if c.InfoHash == "" { - c.InfoHash = meta.InfoHash - } - if c.FilesCount == 0 { - c.FilesCount = len(meta.Files) - } - looked = append(looked, i) - } - return looked -} - -// pick finds a candidate by key: the infohash first, the exact name for a -// result that has none. -func pick(cands []Candidate, key string) *Candidate { - key = strings.TrimSpace(key) - if key == "" { - return nil - } - hash := indexer.NormalizeHash(key) - for i := range cands { - if cands[i].InfoHash != "" && cands[i].InfoHash == hash { - return &cands[i] - } - } - for i := range cands { - if cands[i].InfoHash == "" && cands[i].Name == key { - return &cands[i] - } - } - return nil -} diff --git a/internal/acquire/search_test.go b/internal/acquire/search_test.go deleted file mode 100644 index 63022d0e..00000000 --- a/internal/acquire/search_test.go +++ /dev/null @@ -1,291 +0,0 @@ -package acquire - -import ( - "context" - "errors" - "fmt" - "strings" - "testing" - "time" - - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -type fakeSearch struct { - results []indexer.Result - err error - asked []indexer.Query -} - -func (f *fakeSearch) Search(_ context.Context, q indexer.Query) ([]indexer.Result, error) { - f.asked = append(f.asked, q) - return f.results, f.err -} - -var ram = Want{Want: release.Want{ - Artist: "Daft Punk", Title: "Random Access Memories", Year: 2013, PrimaryType: "Album", - MinTracks: 13, MaxTracks: 15, -}} - -var anyQuality = release.NewProfile([]string{ - "lossless_hires", "lossless", "lossy_high", "lossy_standard", "lossy_low", "lossy_poor", "lossy_trash", -}) - -func result(title, hash string, seeders int) indexer.Result { - return indexer.Result{Title: title, InfoHash: hash, Seeders: seeders, Indexer: "Knaben", Source: "prowlarr", - Magnet: "magnet:?xt=urn:btih:" + hash} -} - -func TestFor_JudgesSortsAndFolds(t *testing.T) { - search := &fakeSearch{results: []indexer.Result{ - result("Daft Punk - Random Access Memories (2013) [MP3 320]", "aaaa", 40), - result("Daft Punk Random Access Memories (Drumless Edition 2023) [Flac 24-88]", "bbbb", 90), - result("Daft Punk - Random Access Memories (2013) [FLAC]", "cccc", 12), - result("Daft Punk - Random Access Memories (2013) [FLAC]", "CCCC", 30), // the same torrent from another tracker - result("Daft Punk - Discovery (2001) [FLAC]", "dddd", 50), - }} - s := &Service{Search: search, Profile: anyQuality} - r := s.For(context.Background(), ram) - - if len(search.asked) != 1 || search.asked[0].Text != "Daft Punk Random Access Memories" { - t.Fatalf("asked %+v", search.asked) - } - if r.Considered != 5 || r.Folded != 1 || len(r.Candidates) != 4 { - t.Fatalf("considered %d folded %d candidates %d", r.Considered, r.Folded, len(r.Candidates)) - } - // Lossless before high-quality lossy among what can be taken; the - // folded copy keeps the higher seeder count. - first, second := r.Candidates[0], r.Candidates[1] - if first.InfoHash != "cccc" || first.Verdict != Grab || first.Seeders != 30 || first.Tier != release.TierLossless { - t.Errorf("first = %s %s %d %s", first.Name, first.Verdict, first.Seeders, first.Tier) - } - if second.InfoHash != "aaaa" || second.Verdict != Grab { - t.Errorf("second = %s %s", second.Name, second.Verdict) - } - for _, c := range r.Candidates[2:] { - if c.Verdict != Rejected || len(c.Reasons) != 1 || c.Reasons[0].Kind != Hard { - t.Errorf("%s: %s %+v", c.Name, c.Verdict, c.Reasons) - } - } - if len(r.Grabbable()) != 2 || !r.ForAPerson() { - t.Errorf("grabbable %d", len(r.Grabbable())) - } -} - -func TestFor_ProfileSeedersAndDoubt(t *testing.T) { - search := &fakeSearch{results: []indexer.Result{ - result("Daft Punk - Random Access Memories [MP3 128]", "aaaa", 5), - result("Daft Punk - Random Access Memories [FLAC]", "bbbb", 0), - result("Daft Punk - Random Access Memories", "cccc", 9), - }} - s := &Service{Search: search, Profile: release.NewProfile([]string{"lossless", "lossy_high"})} - r := s.For(context.Background(), ram) - verdicts := map[string]Verdict{} - for _, c := range r.Candidates { - verdicts[c.InfoHash] = c.Verdict - } - if verdicts["aaaa"] != Refused || verdicts["bbbb"] != Review || verdicts["cccc"] != Refused { - t.Errorf("verdicts = %v", verdicts) - } - if len(r.Grabbable()) != 0 || !r.ForAPerson() { - t.Error("nothing grabbable, but something for a person") - } -} - -func TestFor_BlockedReleasesAreSetAside(t *testing.T) { - c := Candidate{Release: release.Parse("Daft Punk - Random Access Memories [FLAC]"), InfoHash: "aaaa", Seeders: 3} - decide(&c, ram, anyQuality, map[string]string{"aaaa": "You set this release aside."}) - if c.Verdict != Blocked || c.Reason != "You set this release aside." { - t.Errorf("verdict %s %q", c.Verdict, c.Reason) - } -} - -func TestFor_UnconfiguredAndFailedSearches(t *testing.T) { - if r := (&Service{}).For(context.Background(), ram); !r.Unconfigured { - t.Error("no source is unconfigured") - } - r := (&Service{Search: &fakeSearch{err: indexer.ErrUnconfigured}}).For(context.Background(), ram) - if !r.Unconfigured { - t.Error("ErrUnconfigured is unconfigured") - } - r = (&Service{Search: &fakeSearch{err: errors.New("dial tcp: refused")}}).For(context.Background(), ram) - if r.Error == "" { - t.Error("a failed search says so") - } -} - -func TestFanout_ANarrowedSearchKeepsWhatAnswered(t *testing.T) { - up := &fakeSearch{results: []indexer.Result{result("Daft Punk - Random Access Memories [FLAC]", "aaaa", 3)}} - down := &fakeSearch{err: indexer.ErrUnreachable} - f := &Fanout{Sources: []Source{{Code: "prowlarr", Label: "Prowlarr", Search: up}, {Code: "torznab", Label: "Feed", Search: down}}} - r := (&Service{Search: f, Profile: anyQuality}).For(context.Background(), ram) - if r.Error != "" || len(r.Candidates) != 1 || len(r.Narrowed) != 1 || r.Narrowed[0].Source != "torznab" { - t.Errorf("narrowed search: error %q, %d candidates, narrowed %+v", r.Error, len(r.Candidates), r.Narrowed) - } - f.Sources = f.Sources[1:] - if r := (&Service{Search: f}).For(context.Background(), ram); r.Error == "" { - t.Error("every source down is a failed search") - } -} - -func TestPacer_SpacesSearchesAndHoldsAfterA429(t *testing.T) { - now := time.Unix(0, 0) - var slept time.Duration - p := &Pacer{Now: func() time.Time { return now }, Sleep: func(_ context.Context, d time.Duration) error { slept += d; return nil }} - src := []Source{{Code: "prowlarr", PerHour: 120, Search: &fakeSearch{}}} - p.spend(src[0]) - if !p.Wait(context.Background(), src, now.Add(time.Hour)) || slept != 30*time.Second { - t.Errorf("slept %s, want 30s", slept) - } - if p.Wait(context.Background(), src, now.Add(time.Second)) { - t.Error("waited past the deadline") - } - p.hold("prowlarr", &indexer.RateLimited{RetryAfter: 5 * time.Minute}) - if at := p.readyAt(src); at.Sub(now) != 5*time.Minute { - t.Errorf("held until %s", at.Sub(now)) - } -} - -type fakeClient struct { - downloader.Driver - added []downloader.Add - addErr error - deleted []string - progress float64 - // elsewhere: the client holds it but does not list it for Minstrel. - elsewhere bool -} - -func (c *fakeClient) AddTorrent(_ context.Context, a downloader.Add) error { - c.added = append(c.added, a) - return c.addErr -} - -func (c *fakeClient) Torrents(_ context.Context, hashes ...string) ([]downloader.Torrent, error) { - if c.elsewhere { - return nil, nil - } - return []downloader.Torrent{{Hash: hashes[0], Progress: c.progress}}, nil -} - -func (c *fakeClient) Delete(_ context.Context, _ bool, hashes ...string) error { - c.deleted = append(c.deleted, hashes...) - return nil -} - -func TestGrab_AddsAMagnetToItsOwnFolder(t *testing.T) { - client := &fakeClient{} - s := &Service{Search: &fakeSearch{results: []indexer.Result{ - result("Daft Punk - Random Access Memories (2013) [FLAC]", "ABCD", 8), - }}, Profile: anyQuality, Grabber: &Downloader{Client: client, Code: "qbittorrent", Landing: "/downloads"}} - r := s.For(context.Background(), ram) - took := s.Grab(context.Background(), ram, &r.Candidates[0], r.Candidates, false, Origin{By: OriginSchedule}) - if !took.Added || took.SavePath != "/downloads/abcd" { - t.Fatalf("took %+v", took) - } - if len(client.added) != 1 || !strings.Contains(strings.ToLower(client.added[0].Magnet), "abcd") || client.added[0].Tags[0] != Tag { - t.Errorf("added %+v", client.added) - } -} - -func TestGrab_RefusesWhatTheVerdictWouldNotTake(t *testing.T) { - client := &fakeClient{} - s := &Service{Grabber: &Downloader{Client: client, Landing: "/downloads"}} - review := Candidate{Verdict: Review, InfoHash: "aaaa", Source: indexer.Result{Magnet: "magnet:?xt=urn:btih:aaaa"}} - if took := s.Grab(context.Background(), ram, &review, nil, false, Origin{By: OriginSchedule}); !took.Refused { - t.Error("a review was taken unforced") - } - if took := s.Grab(context.Background(), ram, &review, nil, true, Origin{By: OriginOperator}); !took.Added { - t.Errorf("a forced review was not taken: %+v", took) - } - blocked := Candidate{Verdict: Blocked, InfoHash: "bbbb"} - if took := s.Grab(context.Background(), ram, &blocked, nil, true, Origin{By: OriginOperator}); !took.Refused { - t.Error("forcing took a release set aside") - } - if took := (&Service{}).Grab(context.Background(), ram, &review, nil, true, Origin{}); !took.Unconfigured || took.Missing != MissingClient { - t.Errorf("no client: %+v", took) - } -} - -func TestGrab_AClientThatAlreadyHasItCounts(t *testing.T) { - client := &fakeClient{addErr: downloader.ErrDuplicate} - s := &Service{Grabber: &Downloader{Client: client, Landing: "/downloads"}} - c := Candidate{Verdict: Grab, InfoHash: "aaaa", Source: indexer.Result{Magnet: "magnet:?xt=urn:btih:aaaa"}} - if took := s.Grab(context.Background(), ram, &c, nil, false, Origin{By: OriginSchedule}); !took.Added { - t.Errorf("a duplicate: %+v", took) - } -} - -// A duplicate the client does not list for Minstrel sits under another -// category, where it would read as gone half an hour later. It is set -// aside rather than taken; forced, it is refused with the client's reason. -func TestGrab_ADuplicateHeldOutsideMinstrelIsNotTaken(t *testing.T) { - client := &fakeClient{addErr: fmt.Errorf("%w: already downloading Ohio Players (lidarr)", downloader.ErrDuplicate), elsewhere: true} - s := &Service{Grabber: &Downloader{Client: client, Landing: "/downloads"}} - c := Candidate{Verdict: Grab, InfoHash: "aaaa", Source: indexer.Result{Magnet: "magnet:?xt=urn:btih:aaaa"}} - took := s.Grab(context.Background(), ram, &c, nil, false, Origin{By: OriginSchedule}) - if took.Added || !took.Refused { - t.Fatalf("took %+v", took) - } - if c.Verdict != Blocked || !strings.Contains(c.Reason, "(already downloading Ohio Players (lidarr))") { - t.Errorf("candidate %s: %q", c.Verdict, c.Reason) - } - - forced := Candidate{Verdict: Grab, InfoHash: "aaaa", Source: indexer.Result{Magnet: "magnet:?xt=urn:btih:aaaa"}} - took = s.Grab(context.Background(), ram, &forced, nil, true, Origin{By: OriginOperator}) - if took.Added || !strings.Contains(took.Error, "outside Minstrel's category") { - t.Errorf("forced: %+v", took) - } -} - -func TestBetter_QualityThenSeedersThenYear(t *testing.T) { - flac := Candidate{Verdict: Grab, Tier: release.TierLossless, Seeders: 2} - mp3 := Candidate{Verdict: Grab, Tier: release.TierLossyHigh, Seeders: 90} - if !better(flac, mp3) || better(mp3, flac) { - t.Error("quality first among grabs") - } - busy := Candidate{Verdict: Grab, Tier: release.TierLossless, Seeders: 20} - if !better(busy, flac) { - t.Error("then seeders") - } - dated := Candidate{Verdict: Grab, Tier: release.TierLossless, Seeders: 2, YearAgrees: true} - if !better(dated, flac) { - t.Error("then the year") - } - if better(Candidate{Verdict: Review}, flac) || !better(flac, Candidate{Verdict: Review}) { - t.Error("a grab ranks above a review") - } -} - -func TestLandingFor_OneFolderPerTorrent(t *testing.T) { - if got := landingFor("/downloads", "abcd", "x"); got != "/downloads/abcd" { - t.Error(got) - } - if got := landingFor("/downloads", "", "Daft Punk: R.A.M. [FLAC]"); got != "/downloads/daft-punk-r-a-m--flac" { - t.Error(got) - } -} - -func TestDecide_AnUpgradeTakesOnlyABetterCopy(t *testing.T) { - up := ram - up.Floor, up.Held = release.TierLossyLow, "MP3, about 192 kbps" - same := Candidate{Release: release.Parse("Daft Punk - Random Access Memories [MP3 192]"), InfoHash: "aaaa", Seeders: 3} - decide(&same, up, anyQuality, nil) - if same.Verdict != Refused || same.Reasons[0].Code != CodeNotBetter || - same.Reason != "No better than the copy you have (MP3, about 192 kbps)." { - t.Errorf("same quality: %s %q", same.Verdict, same.Reason) - } - flac := Candidate{Release: release.Parse("Daft Punk - Random Access Memories [FLAC]"), InfoHash: "bbbb", Seeders: 3} - decide(&flac, up, anyQuality, nil) - if flac.Verdict != Grab { - t.Errorf("a FLAC over a 192 MP3: %s %q", flac.Verdict, flac.Reason) - } - // A video rip has no floor: any clean copy is better. - rip := ram - decide(&same, rip, anyQuality, nil) - if same.Verdict != Grab { - t.Errorf("no floor: %s %q", same.Verdict, same.Reason) - } -} diff --git a/internal/acquire/settings.go b/internal/acquire/settings.go deleted file mode 100644 index 37726d61..00000000 --- a/internal/acquire/settings.go +++ /dev/null @@ -1,215 +0,0 @@ -// Package acquire turns the configured indexers and download clients into -// working clients. -// -// ONE BUILDER PER KIND. SourceFor makes a search source from a row and -// DownloadClient makes a download client, path mapping included. The grab, -// the importer, the stall watchdog and the settings page's Test all build -// through them, so a Test that passes vouches for the path real traffic takes. -// Clients are rebuilt from the row on every use, so a changed address or a new -// key takes effect on the next call. -package acquire - -import ( - "context" - "errors" - "fmt" - "time" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/exchange" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" - "git.fabledsword.com/bvandeusen/minstrel/internal/prowlarr" - "git.fabledsword.com/bvandeusen/minstrel/internal/secret" -) - -// Kinds: which list a row appears in. -const ( - KindSearch = "search" - KindDownload = "download" -) - -// Jobs a row can do. An exchange row does each its switch allows; every -// other row does the one its kind names. -const ( - JobSearch = "search" - JobDownload = "download" - JobEnrichment = "enrichment" -) - -// Protocols: which client a row builds. -const ( - ProtocolProwlarr = "prowlarr" - ProtocolTorznab = "torznab" - ProtocolQBittorrent = "qbittorrent" - ProtocolExchange = "exchange" -) - -// DefaultCategory files everything Minstrel adds to a download client. -const DefaultCategory = "minstrel" - -// addable is what a new row of a protocol starts as. -type addable struct { - kind string - label string - category string - timeoutMs int32 - perHour int32 -} - -// catalogue is every protocol an admin can add. Exchange starts unpaced -// (it searches its own database); the others at 120 searches an hour, which -// keeps a Prowlarr's indexers under the rate most trackers allow. -var catalogue = map[string]addable{ - ProtocolProwlarr: {kind: KindSearch, label: "Prowlarr", timeoutMs: 60000, perHour: 120}, - ProtocolTorznab: {kind: KindSearch, label: "Torznab feed", timeoutMs: 30000, perHour: 120}, - ProtocolQBittorrent: {kind: KindDownload, label: "qBittorrent", category: DefaultCategory, timeoutMs: 15000, perHour: 0}, - ProtocolExchange: {kind: KindDownload, label: "FabledExchange", category: DefaultCategory, timeoutMs: 30000, perHour: 0}, -} - -// Addable says whether protocol is one an admin can add. -func Addable(protocol string) bool { - _, ok := catalogue[protocol] - return ok -} - -// Searches says whether a row is a search source. -func Searches(row dbq.AcquisitionService) bool { - if row.Protocol == ProtocolExchange { - return row.ServesSearch - } - return row.Kind == KindSearch -} - -// Fetches says whether a row is a download client. -func Fetches(row dbq.AcquisitionService) bool { - if row.Protocol == ProtocolExchange { - return row.ServesDownloads - } - return row.Kind == KindDownload -} - -// Searcher is a search source. -type Searcher interface { - Search(ctx context.Context, q indexer.Query) ([]indexer.Result, error) -} - -// Fetcher fetches a result's .torrent through the source that offered it. -type Fetcher interface { - Download(ctx context.Context, r indexer.Result) ([]byte, error) -} - -// Source is one configured search source. -type Source struct { - Code string - Label string - // PerHour is the most searches the schedule sends it an hour; 0 = unpaced. - PerHour int - Search Searcher - // Fetch is nil for a source whose results are all magnets. - Fetch Fetcher -} - -// SourceFor builds the search source a row describes. -func SourceFor(row dbq.AcquisitionService) Source { - src := Source{Code: row.Code, Label: row.Label, PerHour: int(row.SearchesPerHour)} - timeout := msDuration(row.TimeoutMs) - switch row.Protocol { - case ProtocolProwlarr: - c := &prowlarr.Client{Config: prowlarr.Config{ - Code: row.Code, BaseURL: row.BaseUrl, APIKey: row.ApiKey, Timeout: timeout, - }} - src.Search, src.Fetch = c, c - case ProtocolExchange: - src.Search = ExchangeFor(row) - default: - c := &indexer.Torznab{Config: indexer.TorznabConfig{ - Code: row.Code, FeedURL: row.BaseUrl, APIKey: row.ApiKey, Timeout: timeout, - }} - src.Search, src.Fetch = c, c - } - return src -} - -// ExchangeFor builds the search and enrichment client for an exchange row. -func ExchangeFor(row dbq.AcquisitionService) *exchange.Client { - return &exchange.Client{ - Code: row.Code, - Config: exchange.Config{ - BaseURL: row.BaseUrl, APIKey: row.ApiKey, Timeout: msDuration(row.TimeoutMs), - }, - } -} - -// DownloadClient builds the download client a row describes, wrapped in its -// path mapping. -func DownloadClient(row dbq.AcquisitionService) (downloader.Driver, error) { - d, err := downloader.For(row.Protocol, downloader.Config{ - BaseURL: row.BaseUrl, Username: row.Username, Password: row.Password, - APIKey: row.ApiKey, Category: row.Category, Timeout: msDuration(row.TimeoutMs), - }) - if err != nil { - return nil, err - } - return downloader.Mapped(d, downloader.PathMap{Local: row.LocalPath, Remote: row.RemotePath}), nil -} - -// DownloadClientFor builds the download client a grab went to, by the code -// of its acquisition_services row. -func DownloadClientFor(ctx context.Context, pool *pgxpool.Pool, code string) (downloader.Driver, error) { - row, err := getService(ctx, dbq.New(pool), code) - if err != nil { - if errors.Is(err, pgx.ErrNoRows) { - return nil, fmt.Errorf("the download client %q is no longer set up", code) - } - return nil, err - } - return DownloadClient(row) -} - -// Sources builds every enabled search source. -func Sources(ctx context.Context, pool *pgxpool.Pool) ([]Source, error) { - rows, err := usableServices(ctx, dbq.New(pool), JobSearch) - if err != nil { - return nil, err - } - out := make([]Source, len(rows)) - for i, r := range rows { - out[i] = SourceFor(r) - } - return out, nil -} - -// getService and usableServices are the only reads of a full -// acquisition_services row. They hand back the credentials opened, so the -// clients built from a row are given the key and password themselves. -func getService(ctx context.Context, q *dbq.Queries, code string) (dbq.AcquisitionService, error) { - row, err := q.GetAcquisitionService(ctx, code) - if err != nil { - return row, err - } - return revealed(row), nil -} - -func usableServices(ctx context.Context, q *dbq.Queries, job string) ([]dbq.AcquisitionService, error) { - rows, err := q.ListUsableAcquisitionServices(ctx, job) - if err != nil { - return nil, err - } - for i := range rows { - rows[i] = revealed(rows[i]) - } - return rows, nil -} - -// revealed is row with its sealed credentials opened. -func revealed(row dbq.AcquisitionService) dbq.AcquisitionService { - row.ApiKey = secret.Reveal(secret.AcquisitionAPIKey.Purpose(), row.Code, row.ApiKey) - row.Password = secret.Reveal(secret.AcquisitionPassword.Purpose(), row.Code, row.Password) - return row -} - -func msDuration(ms int32) time.Duration { return time.Duration(ms) * time.Millisecond } diff --git a/internal/acquire/store.go b/internal/acquire/store.go deleted file mode 100644 index b7141fda..00000000 --- a/internal/acquire/store.go +++ /dev/null @@ -1,200 +0,0 @@ -package acquire - -import ( - "context" - "errors" - "fmt" - "net/url" - "strings" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/secret" -) - -// ErrNotFound is a code with no row. -var ErrNotFound = errors.New("no such service") - -// ErrTooMany is a twenty-first row of one protocol. -var ErrTooMany = errors.New("there are already twenty of these") - -// InvalidError is a setting an admin has to change, in words for the -// settings card. -type InvalidError struct { - Field string - Message string -} - -func (e *InvalidError) Error() string { return e.Message } - -func invalid(field, msg string) error { return &InvalidError{Field: field, Message: msg} } - -// maxPerProtocol bounds the codes tried for a new row. Nobody runs twenty -// Prowlarrs; the bound keeps a loop over a full table finite. -const maxPerProtocol = 20 - -// Store reads and writes acquisition_services. -type Store struct { - pool *pgxpool.Pool -} - -// NewStore returns a Store over pool. -func NewStore(pool *pgxpool.Pool) *Store { return &Store{pool: pool} } - -// List returns every row without its secrets. -func (s *Store) List(ctx context.Context) ([]dbq.ListAcquisitionServicesRow, error) { - return dbq.New(s.pool).ListAcquisitionServices(ctx) -} - -// Get returns one row, secrets included: for building a client only. -func (s *Store) Get(ctx context.Context, code string) (dbq.AcquisitionService, error) { - row, err := getService(ctx, dbq.New(s.pool), code) - if errors.Is(err, pgx.ErrNoRows) { - return row, ErrNotFound - } - return row, err -} - -// Create adds a row of protocol, switched off and with no address. Its code -// is the protocol for the first, then protocol-2, protocol-3. -func (s *Store) Create(ctx context.Context, protocol string) (dbq.AcquisitionService, error) { - a, ok := catalogue[protocol] - if !ok { - return dbq.AcquisitionService{}, invalid("protocol", "One of prowlarr, torznab, qbittorrent, exchange.") - } - q := dbq.New(s.pool) - for n := 1; n <= maxPerProtocol; n++ { - code, label := protocol, a.label - if n > 1 { - code, label = fmt.Sprintf("%s-%d", protocol, n), fmt.Sprintf("%s %d", a.label, n) - } - row, err := q.CreateAcquisitionService(ctx, dbq.CreateAcquisitionServiceParams{ - Code: code, Kind: a.kind, Protocol: protocol, Label: label, - Category: a.category, TimeoutMs: a.timeoutMs, SearchesPerHour: a.perHour, - }) - if errors.Is(err, pgx.ErrNoRows) { - continue - } - return row, err - } - return dbq.AcquisitionService{}, ErrTooMany -} - -// Update is everything on the settings form except the credentials. -type Update struct { - Label string - Enabled bool - BaseURL string - Username string - Category string - TimeoutMs int32 - ServesSearch bool - ServesDownloads bool - ServesEnrichment bool - LocalPath string - RemotePath string - SearchesPerHour int32 -} - -// Save validates u and writes it to code's row. -func (s *Store) Save(ctx context.Context, code string, u Update) (dbq.AcquisitionService, error) { - u.Label = strings.TrimSpace(u.Label) - u.BaseURL = strings.TrimSpace(u.BaseURL) - u.Username = strings.TrimSpace(u.Username) - u.Category = strings.TrimSpace(u.Category) - u.LocalPath = strings.TrimSpace(u.LocalPath) - u.RemotePath = strings.TrimSpace(u.RemotePath) - if err := u.validate(); err != nil { - return dbq.AcquisitionService{}, err - } - row, err := dbq.New(s.pool).UpdateAcquisitionService(ctx, dbq.UpdateAcquisitionServiceParams{ - Code: code, Label: u.Label, Enabled: u.Enabled, BaseUrl: u.BaseURL, - Username: u.Username, Category: u.Category, TimeoutMs: u.TimeoutMs, - ServesSearch: u.ServesSearch, ServesDownloads: u.ServesDownloads, - ServesEnrichment: u.ServesEnrichment, LocalPath: u.LocalPath, - RemotePath: u.RemotePath, SearchesPerHour: u.SearchesPerHour, - }) - if errors.Is(err, pgx.ErrNoRows) { - return row, ErrNotFound - } - return row, err -} - -// validate mirrors the table's CHECKs, so a bad value is named in the card's -// own terms before Postgres refuses it. -func (u Update) validate() error { - if u.Label == "" { - return invalid("label", "Name: give it a name.") - } - if u.Enabled && u.BaseURL == "" { - return invalid("base_url", "Address: give the address before switching this on.") - } - if u.BaseURL != "" { - parsed, err := url.Parse(u.BaseURL) - if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" { - return invalid("base_url", "Address: start with http:// or https://.") - } - } - if u.TimeoutMs < 1000 || u.TimeoutMs > 300000 { - return invalid("timeout_ms", "Give up after: between 1000 and 300000 milliseconds.") - } - if !strings.HasPrefix(u.LocalPath, "/") { - return invalid("local_path", "Downloads folder as Minstrel sees it: a full path, starting with /.") - } - if u.RemotePath != "" && !strings.HasPrefix(u.RemotePath, "/") { - return invalid("remote_path", "Downloads folder as the client sees it: a full path starting with /, or empty when it is the same path.") - } - if u.SearchesPerHour < 0 || u.SearchesPerHour > 3600 { - return invalid("searches_per_hour", "Searches per hour: between 0 and 3600. 0 means no limit.") - } - return nil -} - -// Delete removes code's row. -func (s *Store) Delete(ctx context.Context, code string) error { - n, err := dbq.New(s.pool).DeleteAcquisitionService(ctx, code) - if err != nil { - return err - } - if n == 0 { - return ErrNotFound - } - return nil -} - -// SetAPIKey stores a key, trimmed and sealed; empty clears it. -func (s *Store) SetAPIKey(ctx context.Context, code, key string) error { - sealed, err := secret.Seal(secret.AcquisitionAPIKey.Purpose(), code, strings.TrimSpace(key)) - if err != nil { - return err - } - n, err := dbq.New(s.pool).SetAcquisitionServiceAPIKey(ctx, dbq.SetAcquisitionServiceAPIKeyParams{ - Code: code, ApiKey: sealed, - }) - return affected(n, err) -} - -// SetPassword stores a password as typed (spaces are part of a password), -// sealed; empty clears it. -func (s *Store) SetPassword(ctx context.Context, code, password string) error { - sealed, err := secret.Seal(secret.AcquisitionPassword.Purpose(), code, password) - if err != nil { - return err - } - n, err := dbq.New(s.pool).SetAcquisitionServicePassword(ctx, dbq.SetAcquisitionServicePasswordParams{ - Code: code, Password: sealed, - }) - return affected(n, err) -} - -func affected(n int64, err error) error { - if err != nil { - return err - } - if n == 0 { - return ErrNotFound - } - return nil -} diff --git a/internal/acquire/store_test.go b/internal/acquire/store_test.go deleted file mode 100644 index 420a38b0..00000000 --- a/internal/acquire/store_test.go +++ /dev/null @@ -1,118 +0,0 @@ -package acquire - -import ( - "context" - "errors" - "io" - "log/slog" - "os" - "testing" - - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db" - "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" - "git.fabledsword.com/bvandeusen/minstrel/internal/secret" -) - -func newPool(t *testing.T) *pgxpool.Pool { - t.Helper() - if testing.Short() { - t.Skip("skipping integration test in -short mode") - } - dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") - if dsn == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { - t.Fatalf("migrate: %v", err) - } - pool, err := pgxpool.New(context.Background(), dsn) - if err != nil { - t.Fatalf("pool: %v", err) - } - t.Cleanup(pool.Close) - dbtest.ResetDB(t, pool) - return pool -} - -func TestStore_AddsConfiguresAndRemoves_Integration(t *testing.T) { - ctx := context.Background() - s := NewStore(newPool(t)) - - first, err := s.Create(ctx, ProtocolExchange) - if err != nil { - t.Fatalf("create: %v", err) - } - second, err := s.Create(ctx, ProtocolExchange) - if err != nil { - t.Fatalf("create a second: %v", err) - } - if first.Code != "exchange" || second.Code != "exchange-2" || second.Label != "FabledExchange 2" { - t.Errorf("codes = %q, %q (%q); want exchange, exchange-2", first.Code, second.Code, second.Label) - } - // A new exchange is off, does every job, files under minstrel and is - // unpaced. - if first.Enabled || first.Kind != KindDownload || !first.ServesSearch || !first.ServesDownloads || - !first.ServesEnrichment || first.Category != DefaultCategory || first.SearchesPerHour != 0 || first.LocalPath != "/downloads" { - t.Errorf("a new exchange = %+v", first) - } - if _, err := s.Create(ctx, "transmission"); !errors.As(err, new(*InvalidError)) { - t.Errorf("an unknown protocol: err = %v", err) - } - - u := Update{ - Label: "Exchange", Enabled: true, BaseURL: "http://exchange:8080", Category: "minstrel", - TimeoutMs: 20000, ServesSearch: true, LocalPath: "/downloads", RemotePath: "/data/torrents", - } - saved, err := s.Save(ctx, first.Code, u) - if err != nil { - t.Fatalf("save: %v", err) - } - if !saved.Enabled || saved.ServesDownloads || saved.RemotePath != "/data/torrents" { - t.Errorf("saved = %+v", saved) - } - if _, err := s.Save(ctx, "nope", u); !errors.Is(err, ErrNotFound) { - t.Errorf("saving a missing row: err = %v", err) - } - - // Secrets are write-only: the list says whether one is set, never what. - if err := s.SetAPIKey(ctx, first.Code, " key "); err != nil { - t.Fatalf("set key: %v", err) - } - full, err := s.Get(ctx, first.Code) - if err != nil || full.ApiKey != "key" { - t.Errorf("stored key = %q (err %v), want it trimmed", full.ApiKey, err) - } - // The column holds the key sealed, never the key. - var raw string - if err := s.pool.QueryRow(ctx, - "SELECT api_key FROM acquisition_services WHERE code = $1", first.Code).Scan(&raw); err != nil { - t.Fatalf("read api_key: %v", err) - } - if raw == "key" || !secret.IsSealed(raw) { - t.Errorf("api_key column = %q, want it sealed", raw) - } - rows, err := s.List(ctx) - if err != nil || len(rows) != 2 { - t.Fatalf("list: %d rows, err %v", len(rows), err) - } - for _, r := range rows { - if r.Code == first.Code && !r.HasApiKey { - t.Error("the list should say a key is set") - } - } - - // Only the exchange with search switched on is a search source. - srcs, err := Sources(ctx, s.pool) - if err != nil || len(srcs) != 1 || srcs[0].Code != first.Code { - t.Errorf("sources = %+v, err %v; want only the enabled exchange", srcs, err) - } - - if err := s.Delete(ctx, second.Code); err != nil { - t.Fatalf("delete: %v", err) - } - if err := s.Delete(ctx, second.Code); !errors.Is(err, ErrNotFound) { - t.Errorf("deleting twice: err = %v", err) - } -} diff --git a/internal/acquire/test.go b/internal/acquire/test.go deleted file mode 100644 index 9ec6d07b..00000000 --- a/internal/acquire/test.go +++ /dev/null @@ -1,134 +0,0 @@ -package acquire - -import ( - "context" - "errors" - "fmt" - "os" - "strings" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/exchange" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -// TestResult is what the settings card says after Test. -type TestResult struct { - OK bool `json:"ok"` - Detail string `json:"detail"` -} - -// testQuery is a search every music indexer should answer. -const testQuery = "Radiohead" - -// Test reaches the service a row describes, through the same builders real -// traffic uses. It runs whether or not the row is switched on: paste the -// address, test it, then switch it on. -func Test(ctx context.Context, row dbq.AcquisitionService) TestResult { - if strings.TrimSpace(row.BaseUrl) == "" { - return TestResult{Detail: "No address is set, so there is nothing to reach yet."} - } - if row.Protocol == ProtocolExchange { - return testExchange(ctx, row) - } - if row.Kind == KindSearch { - return testSearch(ctx, row) - } - return testDownload(ctx, row) -} - -// testExchange runs each job the row has switched on and reports them all. -func testExchange(ctx context.Context, row dbq.AcquisitionService) TestResult { - var parts []string - ok := true - run := func(name string, on bool, test func() TestResult) { - if !on { - return - } - r := test() - ok = ok && r.OK - parts = append(parts, name+": "+r.Detail) - } - run("Downloads", row.ServesDownloads, func() TestResult { return testDownload(ctx, row) }) - run("Search", row.ServesSearch, func() TestResult { return testSearch(ctx, row) }) - run("Release details", row.ServesEnrichment, func() TestResult { return testEnrichment(ctx, row) }) - if len(parts) == 0 { - return TestResult{Detail: "Every job of this FabledExchange is switched off, so there is nothing to test."} - } - return TestResult{OK: ok, Detail: strings.Join(parts, " ")} -} - -// testSearch runs a real search. -func testSearch(ctx context.Context, row dbq.AcquisitionService) TestResult { - src := SourceFor(row) - results, err := src.Search.Search(ctx, indexer.Query{Text: testQuery, Categories: indexer.MusicCategories}) - switch { - case errors.Is(err, indexer.ErrUnconfigured): - return TestResult{Detail: "Add this service's address, and the API key it shows on its own settings page."} - case errors.Is(err, indexer.ErrAuth): - return TestResult{Detail: indexer.FirstLine(err.Error()) + ". Check it on the service's own settings page."} - case err != nil: - return TestResult{Detail: indexer.FirstLine(err.Error())} - case len(results) == 0: - return TestResult{OK: true, Detail: "Connected, but a test search for music found nothing. Check that it has an indexer with music categories."} - } - return TestResult{OK: true, Detail: fmt.Sprintf("Connected. %d results for a test search.", len(results))} -} - -// testDownload lists the client's torrents, which proves the session and not -// only the login, then checks the downloads folder from Minstrel's side. -func testDownload(ctx context.Context, row dbq.AcquisitionService) TestResult { - c, err := DownloadClient(row) - if err != nil { - return TestResult{Detail: err.Error()} - } - ts, err := c.Torrents(ctx) - if err != nil { - return TestResult{Detail: indexer.FirstLine(err.Error())} - } - detail := fmt.Sprintf("Connected. %d torrents.", len(ts)) - if row.Category != "" { - detail = fmt.Sprintf("Connected. %d torrents in the %q category.", len(ts), row.Category) - } - if up, err := c.Connected(ctx); err == nil && !up { - detail += " The client reports it is not connected to any peers, so downloads will stall." - } - if problem := landingProblem(row.LocalPath); problem != "" { - return TestResult{Detail: detail + " " + problem} - } - return TestResult{OK: true, Detail: detail} -} - -// landingProblem checks Minstrel can see and write the downloads folder: it -// moves finished downloads out of it. Only the local path can be checked; -// the client's own name for it cannot be seen from here. -func landingProblem(dir string) string { - info, err := os.Stat(dir) - switch { - case err != nil: - return fmt.Sprintf("But Minstrel cannot see its downloads folder at %s. Mount the download client's downloads folder there, or change where Minstrel looks for it.", dir) - case !info.IsDir(): - return fmt.Sprintf("But %s is a file, not the downloads folder.", dir) - } - probe, err := os.CreateTemp(dir, ".minstrel-probe-*") - if err != nil { - return fmt.Sprintf("But Minstrel cannot write to %s, so it could not move a finished download out of it. Check the mount is not read-only and Minstrel's user can write there.", dir) - } - name := probe.Name() - _ = probe.Close() - _ = os.Remove(name) - return "" -} - -// testEnrichment asks about a hash no torrent has. A JSON "not crawled" is a -// healthy answer; a 404 from something that is not Exchange is not. -func testEnrichment(ctx context.Context, row dbq.AcquisitionService) TestResult { - _, err := ExchangeFor(row).Torrent(ctx, strings.Repeat("0", 40)) - switch { - case err == nil, errors.Is(err, exchange.ErrUnknown): - return TestResult{OK: true, Detail: "Connected. The index answered."} - case errors.Is(err, exchange.ErrUnconfigured): - return TestResult{Detail: "No address is set, so there is nothing to reach yet."} - } - return TestResult{Detail: indexer.FirstLine(err.Error())} -} diff --git a/internal/api/admin_acquisition.go b/internal/api/admin_acquisition.go deleted file mode 100644 index f6144e61..00000000 --- a/internal/api/admin_acquisition.go +++ /dev/null @@ -1,210 +0,0 @@ -package api - -import ( - "encoding/json" - "errors" - "io" - "net/http" - - "github.com/go-chi/chi/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -// Indexers and download clients, admin side. Rows an admin -// adds and configures; nothing is seeded. Secrets are write-only: no response -// carries one, only whether it is set. - -// acquisitionBodyLimit caps a settings body. The largest is a full form of -// short strings. -const acquisitionBodyLimit = 64 << 10 - -type acquisitionServiceResp struct { - Code string `json:"code"` - Kind string `json:"kind"` - Protocol string `json:"protocol"` - Label string `json:"label"` - Enabled bool `json:"enabled"` - BaseURL string `json:"base_url"` - HasAPIKey bool `json:"has_api_key"` - HasPassword bool `json:"has_password"` - Username string `json:"username"` - Category string `json:"category"` - TimeoutMs int32 `json:"timeout_ms"` - ServesSearch bool `json:"serves_search"` - ServesDownloads bool `json:"serves_downloads"` - ServesEnrichment bool `json:"serves_enrichment"` - LocalPath string `json:"local_path"` - RemotePath string `json:"remote_path"` - SearchesPerHour int32 `json:"searches_per_hour"` - // Usable is switched on and addressed: the state the jobs read. - Usable bool `json:"usable"` -} - -func acquisitionRespOf(r dbq.AcquisitionService) acquisitionServiceResp { - return acquisitionServiceResp{ - Code: r.Code, Kind: r.Kind, Protocol: r.Protocol, Label: r.Label, - Enabled: r.Enabled, BaseURL: r.BaseUrl, - HasAPIKey: r.ApiKey != "", HasPassword: r.Password != "", - Username: r.Username, Category: r.Category, TimeoutMs: r.TimeoutMs, - ServesSearch: r.ServesSearch, ServesDownloads: r.ServesDownloads, - ServesEnrichment: r.ServesEnrichment, - LocalPath: r.LocalPath, RemotePath: r.RemotePath, - SearchesPerHour: r.SearchesPerHour, - Usable: r.Enabled && r.BaseUrl != "", - } -} - -// handleListAcquisitionServices implements GET /api/admin/acquisition/services. -func (h *handlers) handleListAcquisitionServices(w http.ResponseWriter, r *http.Request) { - rows, err := h.acquisition.List(r.Context()) - if err != nil { - writeErrWithLog(w, h.logger, "admin: list acquisition services", apierror.Internal(err)) - return - } - out := make([]acquisitionServiceResp, len(rows)) - for i, row := range rows { - out[i] = acquisitionRespOf(dbq.AcquisitionService{ - Code: row.Code, Kind: row.Kind, Protocol: row.Protocol, Label: row.Label, - Enabled: row.Enabled, BaseUrl: row.BaseUrl, Username: row.Username, - Category: row.Category, TimeoutMs: row.TimeoutMs, - ServesSearch: row.ServesSearch, ServesDownloads: row.ServesDownloads, - ServesEnrichment: row.ServesEnrichment, LocalPath: row.LocalPath, - RemotePath: row.RemotePath, SearchesPerHour: row.SearchesPerHour, - }) - out[i].HasAPIKey, out[i].HasPassword = row.HasApiKey, row.HasPassword - } - writeJSON(w, http.StatusOK, map[string]any{"services": out}) -} - -// handleCreateAcquisitionService implements POST -// /api/admin/acquisition/services with {"protocol": -// "prowlarr"|"torznab"|"qbittorrent"|"exchange"}. The row starts switched off, -// with no address. -func (h *handlers) handleCreateAcquisitionService(w http.ResponseWriter, r *http.Request) { - var body struct { - Protocol string `json:"protocol"` - } - if !decodeAcquisitionBody(w, r, &body) { - return - } - row, err := h.acquisition.Create(r.Context(), body.Protocol) - if err != nil { - h.writeAcquisitionErr(w, "admin: create acquisition service", err) - return - } - writeJSON(w, http.StatusCreated, acquisitionRespOf(row)) -} - -type updateAcquisitionBody struct { - Label string `json:"label"` - Enabled bool `json:"enabled"` - BaseURL string `json:"base_url"` - Username string `json:"username"` - Category string `json:"category"` - TimeoutMs int32 `json:"timeout_ms"` - ServesSearch bool `json:"serves_search"` - ServesDownloads bool `json:"serves_downloads"` - ServesEnrichment bool `json:"serves_enrichment"` - LocalPath string `json:"local_path"` - RemotePath string `json:"remote_path"` - SearchesPerHour int32 `json:"searches_per_hour"` -} - -// handleUpdateAcquisitionService implements PUT -// /api/admin/acquisition/services/{code}: the whole form except credentials. -func (h *handlers) handleUpdateAcquisitionService(w http.ResponseWriter, r *http.Request) { - var body updateAcquisitionBody - if !decodeAcquisitionBody(w, r, &body) { - return - } - row, err := h.acquisition.Save(r.Context(), chi.URLParam(r, "code"), acquire.Update(body)) - if err != nil { - h.writeAcquisitionErr(w, "admin: update acquisition service", err) - return - } - writeJSON(w, http.StatusOK, acquisitionRespOf(row)) -} - -// handleDeleteAcquisitionService implements DELETE -// /api/admin/acquisition/services/{code}. -func (h *handlers) handleDeleteAcquisitionService(w http.ResponseWriter, r *http.Request) { - if err := h.acquisition.Delete(r.Context(), chi.URLParam(r, "code")); err != nil { - h.writeAcquisitionErr(w, "admin: delete acquisition service", err) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// handleTestAcquisitionService implements POST -// /api/admin/acquisition/services/{code}/test. POST because it reaches -// another machine, which a prefetch must not trigger. Always 200 with -// {ok, detail}: a failed connection is the answer, not an error. -func (h *handlers) handleTestAcquisitionService(w http.ResponseWriter, r *http.Request) { - row, err := h.acquisition.Get(r.Context(), chi.URLParam(r, "code")) - if err != nil { - h.writeAcquisitionErr(w, "admin: test acquisition service", err) - return - } - writeJSON(w, http.StatusOK, acquire.Test(r.Context(), row)) -} - -// handleSetAcquisitionSecret implements PUT and DELETE on -// /api/admin/acquisition/services/{code}/api-key and .../password. PUT takes -// {"value": "..."}; DELETE clears it. 204 with no body either way: echoing -// anything here is how a secret ends up in a response. -func (h *handlers) handleSetAcquisitionSecret(field string) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - value := "" - if r.Method == http.MethodPut { - var body struct { - Value string `json:"value"` - } - if !decodeAcquisitionBody(w, r, &body) { - return - } - if body.Value == "" { - writeErr(w, apierror.BadRequest("invalid_setting", "Paste a value, or use Remove to clear the one that is set.")) - return - } - value = body.Value - } - code := chi.URLParam(r, "code") - var err error - if field == "password" { - err = h.acquisition.SetPassword(r.Context(), code, value) - } else { - err = h.acquisition.SetAPIKey(r.Context(), code, value) - } - if err != nil { - h.writeAcquisitionErr(w, "admin: set acquisition "+field, err) - return - } - w.WriteHeader(http.StatusNoContent) - } -} - -func decodeAcquisitionBody(w http.ResponseWriter, r *http.Request, into any) bool { - err := json.NewDecoder(http.MaxBytesReader(w, r.Body, acquisitionBodyLimit)).Decode(into) - if err != nil && !errors.Is(err, io.EOF) { - writeErr(w, apierror.BadRequest("invalid_body", "malformed JSON")) - return false - } - return true -} - -func (h *handlers) writeAcquisitionErr(w http.ResponseWriter, msg string, err error) { - var invalid *acquire.InvalidError - switch { - case errors.As(err, &invalid): - writeErr(w, apierror.BadRequest("invalid_setting", invalid.Message)) - case errors.Is(err, acquire.ErrNotFound): - writeErr(w, apierror.NotFound("service")) - case errors.Is(err, acquire.ErrTooMany): - writeErr(w, apierror.Conflict("too_many_services", "There are already twenty of these. Remove one you no longer use first.")) - default: - writeErrWithLog(w, h.logger, msg, apierror.Internal(err)) - } -} diff --git a/internal/api/admin_duplicates.go b/internal/api/admin_duplicates.go index 970478f6..b43cea18 100644 --- a/internal/api/admin_duplicates.go +++ b/internal/api/admin_duplicates.go @@ -459,21 +459,19 @@ type refetchDuplicateRequest struct { KeepTrackID string `json:"keep_track_id"` } -// refetchDuplicateResponse reports what went. Blocked names the releases set -// aside; empty when the album came from before Minstrel downloaded anything. +// refetchDuplicateResponse reports what went. type refetchDuplicateResponse struct { KeptTrackID *string `json:"kept_track_id"` RemovedPaths []string `json:"removed_paths"` - Blocked []string `json:"blocked"` } // handleRefetchDuplicateGroup implements POST // /api/admin/library/duplicates/{id}/refetch: settle a group of one // recording in several slots of an album the way the resolver would, with an // admin's answer. The copies in slots that are not the recording's are -// removed and marked missing, the release they came from is set aside, and the -// album is wanted again. Merge is the wrong action there: it keeps one title -// whether or not it is the audio's, and fetches nothing. +// removed and marked missing, so the slot shows as missing until a good copy +// arrives in the library. Merge is the wrong action there: it keeps one title +// whether or not it is the audio's. // // Errors: // - 409 library_not_writable / 500 file_delete_failed when a file could not be @@ -525,10 +523,7 @@ func (h *handlers) handleRefetchDuplicateGroup(w http.ResponseWriter, r *http.Re return } - resp := refetchDuplicateResponse{RemovedPaths: make([]string, 0, len(res.Removed)), Blocked: res.Blocked} - if resp.Blocked == nil { - resp.Blocked = []string{} - } + resp := refetchDuplicateResponse{RemovedPaths: make([]string, 0, len(res.Removed))} if res.Kept != nil { id := uuidToString(res.Kept.TrackID) resp.KeptTrackID = &id diff --git a/internal/api/admin_import.go b/internal/api/admin_import.go deleted file mode 100644 index 207283ef..00000000 --- a/internal/api/admin_import.go +++ /dev/null @@ -1,285 +0,0 @@ -package api - -import ( - "encoding/json" - "errors" - "net/http" - - "github.com/jackc/pgx/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/importer" - "git.fabledsword.com/bvandeusen/minstrel/internal/lifecycle" -) - -// Downloads and their imports: the Wanted view's Activity tab, -// a person's "import anyway", "refuse" and "retry", and the Blocked tab. - -// downloadView is one grab and what its import made of it. -type downloadView struct { - ID string `json:"id"` - WantID *string `json:"want_id"` - WantTitle string `json:"want_title"` - WantArtist string `json:"want_artist"` - Name string `json:"name"` - Indexer string `json:"indexer"` - Client string `json:"client"` - Quality string `json:"quality"` - Origin string `json:"origin"` - GrabbedAt string `json:"grabbed_at"` - EndedAt *string `json:"ended_at"` - EndReason string `json:"end_reason"` - Progress float32 `json:"progress"` - ImportState string `json:"import_state"` - ImportReason string `json:"import_reason"` - ImportedAt *string `json:"imported_at"` - ImportedFiles int32 `json:"imported_files"` - Destination string `json:"destination"` - Edition string `json:"edition,omitempty"` - Evidence *importer.Evidence `json:"evidence,omitempty"` - Files []string `json:"files"` - Skipped int `json:"skipped"` - RehomedFrom string `json:"rehomed_from,omitempty"` - // EndDetail says why an ended download ended. - EndDetail string `json:"end_detail"` - // Seeding: imported and still sharing; Ratio its share ratio as last - // read; SeedReleasedAt when it was removed once it had shared enough. - Seeding bool `json:"seeding"` - Ratio float32 `json:"ratio"` - SeedReleasedAt *string `json:"seed_released_at"` - // Condition is what is wrong with it now, if anything. - Condition *conditionView `json:"condition"` -} - -// conditionView is an open row of the intervention ledger: what was seen, -// since when, and how many times it was retried. -type conditionView struct { - Trigger string `json:"trigger"` - Detail string `json:"detail"` - Retries int32 `json:"retries"` - Since string `json:"since"` -} - -func downloadFrom(g dbq.ListGrabsRow) downloadView { - v := downloadView{ - ID: uuidToString(g.ID), Name: g.Name, Indexer: g.Indexer, Client: g.Client, Quality: g.Quality, - Origin: g.Origin, GrabbedAt: formatTimestamp(g.GrabbedAt), EndReason: g.EndReason, - Progress: g.Progress, ImportState: g.ImportState, ImportReason: g.ImportReason, - ImportedFiles: g.ImportedFiles, Destination: g.Destination, Files: []string{}, - EndDetail: g.EndDetail, Seeding: g.Seeding, Ratio: g.Ratio, - } - if g.SeedReleasedAt.Valid { - at := formatTimestamp(g.SeedReleasedAt) - v.SeedReleasedAt = &at - } - if g.Condition != nil { - v.Condition = &conditionView{Trigger: *g.Condition, Detail: deref(g.ConditionDetail), - Since: formatTimestamp(g.ConditionSince)} - if g.ConditionRetries != nil { - v.Condition.Retries = *g.ConditionRetries - } - } - if g.WantID.Valid { - id := uuidToString(g.WantID) - v.WantID = &id - } - if g.WantTitle != nil { - v.WantTitle = *g.WantTitle - } - if g.WantArtist != nil { - v.WantArtist = *g.WantArtist - } - if g.EndedAt.Valid { - at := formatTimestamp(g.EndedAt) - v.EndedAt = &at - } - if g.ImportedAt.Valid { - at := formatTimestamp(g.ImportedAt) - v.ImportedAt = &at - } - var det importer.Detail - if len(g.ImportDetail) > 0 && json.Unmarshal(g.ImportDetail, &det) == nil && det.Verdict.Outcome != "" { - ev := det.Verdict.Evidence - v.Evidence, v.Edition, v.Skipped, v.RehomedFrom = &ev, det.Edition, det.Skipped, det.RehomedFrom - if det.Files != nil { - v.Files = det.Files - } - } - return v -} - -type downloadsPage struct { - Items []downloadView `json:"items"` - Limit int `json:"limit"` - Offset int `json:"offset"` - // ClientsDown are the download clients not answering now. - ClientsDown []clientDownView `json:"clients_down"` -} - -type clientDownView struct { - Client string `json:"client"` - Detail string `json:"detail"` - Since string `json:"since"` -} - -// handleListDownloads implements GET /api/admin/downloads?offset=: running -// downloads first, then the rest newest first. -func (h *handlers) handleListDownloads(w http.ResponseWriter, r *http.Request) { - limit, offset, err := parsePaging(r.URL.Query()) - if err != nil { - writeErr(w, apierror.BadRequest("invalid_paging", "limit and offset must be numbers")) - return - } - rows, err := dbq.New(h.pool).ListGrabs(r.Context(), dbq.ListGrabsParams{MaxRows: int32(limit), Skip: int32(offset)}) - if err != nil { - writeErrWithLog(w, h.logger, "admin downloads: list", apierror.Internal(err)) - return - } - down, err := dbq.New(h.pool).ListClientsDown(r.Context()) - if err != nil { - writeErrWithLog(w, h.logger, "admin downloads: clients down", apierror.Internal(err)) - return - } - out := downloadsPage{Items: make([]downloadView, 0, len(rows)), Limit: limit, Offset: offset, - ClientsDown: make([]clientDownView, 0, len(down))} - for _, g := range rows { - out.Items = append(out.Items, downloadFrom(g)) - } - for _, c := range down { - out.ClientsDown = append(out.ClientsDown, clientDownView{Client: c.Client, Detail: c.Detail, - Since: formatTimestamp(c.FirstSeen)}) - } - writeJSON(w, http.StatusOK, out) -} - -// handleAbandonDownload implements POST /api/admin/downloads/{id}/abandon: a -// running download is given up, its release set aside, the download removed -// and the album searched again. -func (h *handlers) handleAbandonDownload(w http.ResponseWriter, r *http.Request) { - admin, ok := requireUser(w, r) - if !ok { - return - } - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - if _, err := h.lifecycle.Abandon(r.Context(), id, admin.ID); err != nil { - switch { - case errors.Is(err, pgx.ErrNoRows), errors.Is(err, lifecycle.ErrNotAbandonable): - writeErr(w, apierror.Conflict("not_running", "that download is no longer running")) - default: - writeErrWithLog(w, h.logger, "admin downloads: abandon", apierror.Internal(err)) - } - return - } - w.WriteHeader(http.StatusNoContent) -} - -// handleImportAnyway implements POST /api/admin/downloads/{id}/import: a -// download the check held for a person (or one that failed) is imported on -// the next pass whatever the check says. -func (h *handlers) handleImportAnyway(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - if _, err := dbq.New(h.pool).ForceGrabImport(r.Context(), id); err != nil { - h.writeImportErr(w, "import anyway", err) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// handleRetryImport implements POST /api/admin/downloads/{id}/retry. -func (h *handlers) handleRetryImport(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - if _, err := dbq.New(h.pool).RetryGrabImport(r.Context(), id); err != nil { - h.writeImportErr(w, "retry", err) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// handleRefuseDownload implements POST /api/admin/downloads/{id}/refuse: the -// release is blocked, the download removed and the album searched again. -func (h *handlers) handleRefuseDownload(w http.ResponseWriter, r *http.Request) { - admin, ok := requireUser(w, r) - if !ok { - return - } - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - if _, err := h.importer.Refuse(r.Context(), id, admin.ID); err != nil { - h.writeImportErr(w, "refuse", err) - return - } - w.WriteHeader(http.StatusNoContent) -} - -func (h *handlers) writeImportErr(w http.ResponseWriter, what string, err error) { - switch { - case errors.Is(err, pgx.ErrNoRows), errors.Is(err, importer.ErrNotDecidable): - writeErr(w, apierror.Conflict("not_decidable", "that download is not waiting on a decision")) - default: - writeErrWithLog(w, h.logger, "admin downloads: "+what, apierror.Internal(err)) - } -} - -type releaseBlockView struct { - InfoHash string `json:"info_hash"` - Name string `json:"name"` - Reason string `json:"reason"` - Detail string `json:"detail"` - BlockedAt string `json:"blocked_at"` - // ExpiresAt is when a release given up on may be taken again; null is - // for good. - ExpiresAt *string `json:"expires_at"` - WantTitle string `json:"want_title"` - WantArtist string `json:"want_artist"` -} - -type releaseBlocksPage struct { - Items []releaseBlockView `json:"items"` - Limit int `json:"limit"` - Offset int `json:"offset"` -} - -// handleListReleaseBlocks implements GET /api/admin/release-blocks?offset=: -// every release set aside, by a person or by a check, newest first. -func (h *handlers) handleListReleaseBlocks(w http.ResponseWriter, r *http.Request) { - limit, offset, err := parsePaging(r.URL.Query()) - if err != nil { - writeErr(w, apierror.BadRequest("invalid_paging", "limit and offset must be numbers")) - return - } - rows, err := dbq.New(h.pool).ListReleaseBlocksPage(r.Context(), dbq.ListReleaseBlocksPageParams{ - MaxRows: int32(limit), Skip: int32(offset)}) - if err != nil { - writeErrWithLog(w, h.logger, "admin release blocks: list", apierror.Internal(err)) - return - } - out := releaseBlocksPage{Items: make([]releaseBlockView, 0, len(rows)), Limit: limit, Offset: offset} - for _, b := range rows { - v := releaseBlockView{InfoHash: b.InfoHash, Name: b.Name, Reason: b.Reason, Detail: b.Detail, - BlockedAt: formatTimestamp(b.BlockedAt)} - if b.ExpiresAt.Valid { - at := formatTimestamp(b.ExpiresAt) - v.ExpiresAt = &at - } - if b.WantTitle != nil { - v.WantTitle = *b.WantTitle - } - if b.WantArtist != nil { - v.WantArtist = *b.WantArtist - } - out.Items = append(out.Items, v) - } - writeJSON(w, http.StatusOK, out) -} diff --git a/internal/api/admin_library_missing.go b/internal/api/admin_library_missing.go index 30a3a4a8..59fd5ff4 100644 --- a/internal/api/admin_library_missing.go +++ b/internal/api/admin_library_missing.go @@ -2,9 +2,6 @@ package api import ( "net/http" - "time" - - "github.com/jackc/pgx/v5/pgtype" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" ) @@ -15,7 +12,7 @@ import ( // the point: the recording is still a known thing with a title, an artist and // a play history — only its bytes are absent. LastPlayedAt is nullable because // plenty of missing files were never played, and that is exactly the signal an -// operator wants when deciding whether to bother re-acquiring one. +// operator wants when deciding whether a lost file is worth replacing. type missingTrackView struct { TrackID string `json:"track_id"` Title string `json:"title"` @@ -29,18 +26,6 @@ type missingTrackView struct { LastPlayedAt *string `json:"last_played_at"` } -// reacquisitionStateView is what the wanted list says about one album: when -// it was wanted back, and when the library -// held it again. A missing album with no want yet is still inside the grace -// window, or MusicBrainz cannot name it. Want is that want as the wanted list -// sends it, so the Missing page can show its search and download and act on -// it. -type reacquisitionStateView struct { - WantedAt *string `json:"wanted_at"` - HeldAt *string `json:"held_at"` - Want *wantView `json:"want"` -} - // missingGroupView is a directory's worth of missing tracks. // // Grouping is the whole ergonomic argument for this surface: a reorganised @@ -52,10 +37,6 @@ type missingGroupView struct { Directory string `json:"directory"` MissingSince string `json:"missing_since"` Tracks []missingTrackView `json:"tracks"` - // Nil when nothing has been attempted for this group's album — the - // common case for a folder that just went missing, and distinct from - // an attempts:0 record, which cannot occur. - Reacquisition *reacquisitionStateView `json:"reacquisition"` } // adminMissingResponse is the paged envelope. Total counts TRACKS, not @@ -99,7 +80,6 @@ func (h *handlers) handleListMissingTracks(w http.ResponseWriter, r *http.Reques } groups := groupMissingByDirectory(rows) - h.attachReacquisitionState(r, q, rows, groups) out := adminMissingResponse{ Total: total, @@ -154,109 +134,3 @@ func groupMissingByDirectory(rows []dbq.ListMissingTracksRow) []missingGroupView } return groups } - -// attachReacquisitionState decorates each group with its album's want. -// -// Best-effort: this is context on a list whose primary job is showing what is -// missing, so a failure here leaves the groups bare rather than failing the -// page. One batched query for the whole page, not one per group. -// -// A group is keyed by directory while a want is keyed by album, and those -// line up in practice (an album's files live in one folder) but are not -// guaranteed to — a directory holding two albums takes the first album's -// state, which is the same album its first track belongs to. -func (h *handlers) attachReacquisitionState( - r *http.Request, - q *dbq.Queries, - rows []dbq.ListMissingTracksRow, - groups []missingGroupView, -) { - if len(groups) == 0 { - return - } - // Directory -> the album its first row belongs to, matching the order - // groupMissingByDirectory folded them in. - dirAlbum := make(map[string]pgtype.UUID, len(groups)) - ids := make([]pgtype.UUID, 0, len(groups)) - // The latest loss in each folder: a want held before it is the album - // as it was before its files went, not the library holding it again. - dirLost := make(map[string]time.Time, len(groups)) - for _, row := range rows { - if row.MissingSince.Valid && row.MissingSince.Time.After(dirLost[row.Directory]) { - dirLost[row.Directory] = row.MissingSince.Time - } - if _, seen := dirAlbum[row.Directory]; seen { - continue - } - dirAlbum[row.Directory] = row.AlbumID - ids = append(ids, row.AlbumID) - } - - wants, err := q.GetWantsForAlbums(r.Context(), ids) - if err != nil { - h.logger.Warn("admin missing: want lookup failed", "err", err) - return - } - // Newest want first, so the first seen per album is its current one. - byAlbum := make(map[string]dbq.GetWantsForAlbumsRow, len(wants)) - for _, w := range wants { - if _, seen := byAlbum[uuidToString(w.AlbumID)]; !seen { - byAlbum[uuidToString(w.AlbumID)] = w - } - } - full := h.wantViewsByID(r, q, byAlbum) - for i := range groups { - albumID, ok := dirAlbum[groups[i].Directory] - if !ok { - continue - } - w, ok := byAlbum[uuidToString(albumID)] - if !ok { - continue - } - // Held before its files went (every library album is on the list, - // held): not wanted back yet, which the sweep does once - // the grace window has passed. - if w.FulfilledAt.Valid && !w.FulfilledAt.Time.After(dirLost[groups[i].Directory]) { - continue - } - view := &reacquisitionStateView{} - wantedAt := formatTimestamp(w.WantedAt) - view.WantedAt = &wantedAt - if w.FulfilledAt.Valid { - heldAt := formatTimestamp(w.FulfilledAt) - view.HeldAt = &heldAt - } - if v, ok := full[uuidToString(w.WantID)]; ok { - view.Want = &v - } - groups[i].Reacquisition = view - } -} - -// wantViewsByID reads each album's current want in full, keyed by want id. -// Best-effort like the state it decorates: a failure leaves the groups with -// their dates and no want. -func (h *handlers) wantViewsByID( - r *http.Request, - q *dbq.Queries, - byAlbum map[string]dbq.GetWantsForAlbumsRow, -) map[string]wantView { - ids := make([]pgtype.UUID, 0, len(byAlbum)) - for _, w := range byAlbum { - ids = append(ids, w.WantID) - } - out := make(map[string]wantView, len(ids)) - if len(ids) == 0 { - return out - } - rows, err := q.ListWants(r.Context(), dbq.ListWantsParams{State: "all", WantIds: ids, MaxRows: int32(len(ids))}) - if err != nil { - h.logger.Warn("admin missing: want detail lookup failed", "err", err) - return out - } - for _, row := range rows { - out[uuidToString(row.ID)] = wantViewOf(row) - } - return out -} diff --git a/internal/api/admin_library_missing_wants_test.go b/internal/api/admin_library_missing_wants_test.go deleted file mode 100644 index 6710580d..00000000 --- a/internal/api/admin_library_missing_wants_test.go +++ /dev/null @@ -1,84 +0,0 @@ -package api - -import ( - "context" - "encoding/json" - "net/http" - "net/http/httptest" - "testing" - - "github.com/jackc/pgx/v5/pgtype" -) - -// seedMissingWant marks a track's file missing for two days and puts its -// album on the wanted list, held at heldDaysAgo (0 for an open want) and -// last searched with nothing found. -func seedMissingWant(t *testing.T, h *handlers, trackID, albumID pgtype.UUID, group string, heldDaysAgo int) string { - t.Helper() - ctx := context.Background() - if _, err := h.pool.Exec(ctx, - `UPDATE tracks SET missing_since = now() - interval '2 days' WHERE id = $1`, trackID); err != nil { - t.Fatalf("mark missing: %v", err) - } - if _, err := h.pool.Exec(ctx, `DELETE FROM wants WHERE release_group_mbid = $1`, group); err != nil { - t.Fatalf("clear want: %v", err) - } - var id pgtype.UUID - if err := h.pool.QueryRow(ctx, - `INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id, - wanted_at, fulfilled_at, searched_at, search_outcome) - VALUES ($1, 'artist-'||$1, 'Artist', 'Album '||$1, 'library', $2, - now() - interval '30 days', - CASE WHEN $3::int > 0 THEN now() - make_interval(days => $3::int) END, - now() - interval '1 hour', 'nothing') - RETURNING id`, group, albumID, heldDaysAgo).Scan(&id); err != nil { - t.Fatalf("insert want: %v", err) - } - return uuidToString(id) -} - -// A folder's album that was held before its files went is not wanted back -// yet (every library album is on the list, held); an open want comes with -// its search, so the Missing page can show it and act on it. -func TestListMissingTracks_WantState(t *testing.T) { - h, pool := testHandlers(t) - truncateLibrary(t, pool) - admin := seedUser(t, pool, "admin", "pw", true) - - heldTrack, _ := seedTrackOnDisk(t, h, "missheld") - openTrack, _ := seedTrackOnDisk(t, h, "missopen") - seedMissingWant(t, h, heldTrack.ID, heldTrack.AlbumID, "rg-missing-held", 10) - openID := seedMissingWant(t, h, openTrack.ID, openTrack.AlbumID, "rg-missing-open", 0) - - req := withUser(httptest.NewRequest(http.MethodGet, "/api/admin/library/missing", nil), admin) - w := httptest.NewRecorder() - h.handleListMissingTracks(w, req) - if w.Code != http.StatusOK { - t.Fatalf("status %d: %s", w.Code, w.Body.String()) - } - var resp adminMissingResponse - if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v", err) - } - if len(resp.Groups) != 2 { - t.Fatalf("groups = %d, want 2", len(resp.Groups)) - } - byTitle := map[string]missingGroupView{} - for _, g := range resp.Groups { - byTitle[g.Tracks[0].Title] = g - } - - if r := byTitle["AdmQ Track missheld"].Reacquisition; r != nil { - t.Errorf("held before the loss: reacquisition = %+v, want none", r) - } - r := byTitle["AdmQ Track missopen"].Reacquisition - if r == nil || r.WantedAt == nil || r.HeldAt != nil { - t.Fatalf("open want: reacquisition = %+v, want wanted and not held", r) - } - if r.Want == nil || r.Want.ID != openID { - t.Fatalf("open want: want = %+v, want id %s", r.Want, openID) - } - if r.Want.Search == nil || r.Want.Search.Outcome != "nothing" { - t.Errorf("open want: search = %+v, want outcome nothing", r.Want.Search) - } -} diff --git a/internal/api/admin_reacquisition.go b/internal/api/admin_reacquisition.go deleted file mode 100644 index 01d841c1..00000000 --- a/internal/api/admin_reacquisition.go +++ /dev/null @@ -1,78 +0,0 @@ -package api - -import ( - "encoding/json" - "errors" - "net/http" - - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/reacquisition" -) - -// reacquisitionSettingsResp is the admin card's payload. -// -// Carries more than the stored settings: [UnnameableAlbums] is the count of -// albums with missing files that can never be wanted automatically because -// neither they nor their artist has an MBID. The card states that number -// rather than leaving an admin to wonder why some rows never get wanted — -// a feature that silently does nothing for part of its input reads as broken. -type reacquisitionSettingsResp struct { - Enabled bool `json:"enabled"` - GraceHours int32 `json:"grace_hours"` - MaxPerPass int32 `json:"max_per_pass"` - - UnnameableAlbums int64 `json:"unnameable_albums"` -} - -// handleGetReacquisitionSettings implements GET -// /api/admin/library/reacquisition. -func (h *handlers) handleGetReacquisitionSettings(w http.ResponseWriter, r *http.Request) { - writeJSON(w, http.StatusOK, h.reacquisitionPayload(r)) -} - -// handleUpdateReacquisitionSettings implements PUT -// /api/admin/library/reacquisition. -func (h *handlers) handleUpdateReacquisitionSettings(w http.ResponseWriter, r *http.Request) { - var req reacquisitionSettingsResp - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - writeErr(w, apierror.BadRequest("invalid_body", "malformed JSON")) - return - } - _, err := h.reacqSettings.Set(r.Context(), reacquisition.Settings{ - Enabled: req.Enabled, - GraceHours: req.GraceHours, - MaxPerPass: req.MaxPerPass, - }) - if err != nil { - // The Go-side validation mirrors migration 0056's CHECKs so the - // operator gets a readable message naming the field, rather than a - // constraint-violation string leaking through as a 500. - if errors.Is(err, reacquisition.ErrOutOfRange) { - writeErr(w, apierror.BadRequest("invalid_setting", err.Error())) - return - } - writeErrWithLog(w, h.logger, "admin reacquisition: update failed", apierror.Internal(err)) - return - } - // Echo the payload recomputed under the new values so the card reflects - // what it just did without a reload. - writeJSON(w, http.StatusOK, h.reacquisitionPayload(r)) -} - -func (h *handlers) reacquisitionPayload(r *http.Request) reacquisitionSettingsResp { - cur := h.reacqSettings.Get() - out := reacquisitionSettingsResp{ - Enabled: cur.Enabled, - GraceHours: cur.GraceHours, - MaxPerPass: cur.MaxPerPass, - } - // Best-effort: the settings are the point of this endpoint, and failing - // the whole card because a count query hiccuped would be the wrong trade. - if n, err := dbq.New(h.pool).CountAlbumsMissingWithoutMbid(r.Context()); err == nil { - out.UnnameableAlbums = n - } else { - h.logger.Warn("admin reacquisition: unnameable count failed", "err", err) - } - return out -} diff --git a/internal/api/admin_recommendation_tuning.go b/internal/api/admin_recommendation_tuning.go index d5e69799..4f77ae02 100644 --- a/internal/api/admin_recommendation_tuning.go +++ b/internal/api/admin_recommendation_tuning.go @@ -67,30 +67,15 @@ func tasteRespFrom(t recsettings.TasteTuning) tasteTuningResp { } } -// discoverTuningResp is the Discover scope on the wire. -type discoverTuningResp struct { - TagOverlapWeight float64 `json:"tag_overlap_weight"` - SnoozeDays float64 `json:"snooze_days"` -} - -func discoverRespFrom(d recsettings.DiscoverTuning) discoverTuningResp { - return discoverTuningResp{ - TagOverlapWeight: d.TagOverlapWeight, - SnoozeDays: d.SnoozeDays, - } -} - // tuningSnapshot is both the GET response and the post-mutation echo: // current values alongside shipped defaults so the card can mark // which knobs deviate. type tuningSnapshot struct { Profiles map[string]weightsResp `json:"profiles"` Taste tasteTuningResp `json:"taste"` - Discover discoverTuningResp `json:"discover"` Shipped struct { Profiles map[string]weightsResp `json:"profiles"` Taste tasteTuningResp `json:"taste"` - Discover discoverTuningResp `json:"discover"` } `json:"shipped"` } @@ -102,14 +87,12 @@ func (h *handlers) tuningSnapshot() tuningSnapshot { recsettings.ScopeSongsLike: weightsRespFrom(h.recSettings.Weights(recsettings.ScopeSongsLike)), } out.Taste = tasteRespFrom(h.recSettings.Taste()) - out.Discover = discoverRespFrom(h.recSettings.Discover()) out.Shipped.Profiles = map[string]weightsResp{ recsettings.ScopeRadio: weightsRespFrom(recsettings.ShippedRadioWeights()), recsettings.ScopeDailyMix: weightsRespFrom(recsettings.ShippedDailyMixWeights()), recsettings.ScopeSongsLike: weightsRespFrom(recsettings.ShippedSongsLikeWeights()), } out.Shipped.Taste = tasteRespFrom(recsettings.ShippedTasteTuning()) - out.Shipped.Discover = discoverRespFrom(recsettings.ShippedDiscoverTuning()) return out } @@ -143,8 +126,6 @@ func (h *handlers) handlePatchRecommendationTuning(w http.ResponseWriter, r *htt switch scope { case recsettings.ScopeTaste: err = h.recSettings.UpdateTaste(r.Context(), body.Values) - case recsettings.ScopeDiscover: - err = h.recSettings.UpdateDiscover(r.Context(), body.Values) default: err = h.recSettings.UpdateProfile(r.Context(), scope, body.Values) } diff --git a/internal/api/admin_requests.go b/internal/api/admin_requests.go deleted file mode 100644 index 4fd395eb..00000000 --- a/internal/api/admin_requests.go +++ /dev/null @@ -1,190 +0,0 @@ -package api - -import ( - "context" - "encoding/json" - "errors" - "net/http" - "strconv" - - "github.com/go-chi/chi/v5" - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/auth" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/notifications" - "git.fabledsword.com/bvandeusen/minstrel/internal/requests" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" -) - -// validRequestStatuses is the set of allowed values for the ?status= param. -var validRequestStatuses = map[string]bool{ - "pending": true, - "approved": true, - "rejected": true, - "completed": true, - "failed": true, -} - -// handleListAdminRequests implements GET /api/admin/requests. -// ?status= defaults to "pending"; ?limit= defaults to 50, max 200. -func (h *handlers) handleListAdminRequests(w http.ResponseWriter, r *http.Request) { - status := r.URL.Query().Get("status") - if status == "" { - status = "pending" - } - if !validRequestStatuses[status] { - writeAdminJSONErr(w, http.StatusBadRequest, "invalid_status") - return - } - - limitStr := r.URL.Query().Get("limit") - limit := 50 - if limitStr != "" { - v, err := strconv.Atoi(limitStr) - if err != nil || v <= 0 { - writeAdminJSONErr(w, http.StatusBadRequest, "invalid_limit") - return - } - if v > 200 { - v = 200 - } - limit = v - } - - rows, err := h.requests.ListByStatus(r.Context(), status, int32(limit)) - if err != nil { - h.logger.Error("admin: list requests", "err", err) - writeAdminJSONErr(w, http.StatusInternalServerError, "server_error") - return - } - - q := dbq.New(h.pool) - names := requesterNames(r.Context(), q, rows) - out := make([]requestView, 0, len(rows)) - for _, row := range rows { - v := requestViewFrom(row) - v.Username = names[row.UserID] - fillProgress(r.Context(), q, row, &v) - out = append(out, v) - } - writeJSON(w, http.StatusOK, out) -} - -// requesterNames maps each request's user id to that user's username, in one -// query for the whole page. Like fillProgress, a failed lookup is swallowed: -// the names are a reporting nicety, and the page falls back to the id. -func requesterNames(ctx context.Context, q *dbq.Queries, rows []dbq.Request) map[pgtype.UUID]string { - ids := make([]pgtype.UUID, 0, len(rows)) - for _, row := range rows { - ids = append(ids, row.UserID) - } - names := make(map[pgtype.UUID]string, len(rows)) - users, err := q.ListUsernamesByIDs(ctx, ids) - if err != nil { - return names - } - for _, u := range users { - names[u.ID] = u.Username - } - return names -} - -// approveRequestBody is the optional JSON body for POST -// /api/admin/requests/:id/approve. profile_id picks the release-type profile an -// artist request is followed on; omitted, the default profile. -type approveRequestBody struct { - ProfileID string `json:"profile_id"` -} - -// handleApproveRequest implements POST /api/admin/requests/:id/approve. The -// request goes on the wanted list: an album or track request -// wants its album, an artist request follows the artist. -func (h *handlers) handleApproveRequest(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, "id")) - if !ok { - writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id") - return - } - - var body approveRequestBody - // Body is entirely optional; ignore decode errors. - _ = json.NewDecoder(r.Body).Decode(&body) - var profile pgtype.UUID - if body.ProfileID != "" { - if profile, ok = parseUUID(body.ProfileID); !ok { - writeAdminJSONErr(w, http.StatusBadRequest, "invalid_profile") - return - } - } - - row, err := h.requests.Approve(r.Context(), id, admin.ID, profile) - if err != nil { - switch { - case errors.Is(err, requests.ErrNotFound): - writeAdminJSONErr(w, http.StatusNotFound, "request_not_found") - case errors.Is(err, requests.ErrNotPending): - writeAdminJSONErr(w, http.StatusConflict, "request_not_pending") - case errors.Is(err, wanted.ErrProfileNotFound): - writeAdminJSONErr(w, http.StatusBadRequest, "profile_not_found") - case errors.Is(err, wanted.ErrNotFollowable): - writeAdminJSONErr(w, http.StatusBadRequest, "not_followable") - default: - h.logger.Error("admin: approve request", "err", err) - writeAdminJSONErr(w, http.StatusInternalServerError, "server_error") - } - return - } - - h.notifyRequestDecided(r.Context(), notifications.KindRequestApproved, admin, row, "") - h.publishRequestStatusChanged(row) - writeJSON(w, http.StatusOK, requestViewFrom(row)) -} - -// rejectRequestBody is the optional JSON body for POST -// /api/admin/requests/:id/reject. -type rejectRequestBody struct { - Notes string `json:"notes"` -} - -// handleRejectRequest implements POST /api/admin/requests/:id/reject. -func (h *handlers) handleRejectRequest(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, "id")) - if !ok { - writeAdminJSONErr(w, http.StatusBadRequest, "invalid_id") - return - } - - var body rejectRequestBody - _ = json.NewDecoder(r.Body).Decode(&body) - - row, err := h.requests.Reject(r.Context(), id, admin.ID, body.Notes) - if err != nil { - switch { - case errors.Is(err, requests.ErrNotFound): - writeAdminJSONErr(w, http.StatusNotFound, "request_not_found") - case errors.Is(err, requests.ErrNotPending): - writeAdminJSONErr(w, http.StatusConflict, "request_not_pending") - default: - h.logger.Error("admin: reject request", "err", err) - writeAdminJSONErr(w, http.StatusInternalServerError, "server_error") - } - return - } - - h.notifyRequestDecided(r.Context(), notifications.KindRequestRejected, admin, row, body.Notes) - h.publishRequestStatusChanged(row) - writeJSON(w, http.StatusOK, requestViewFrom(row)) -} diff --git a/internal/api/admin_requests_test.go b/internal/api/admin_requests_test.go deleted file mode 100644 index 734eadfc..00000000 --- a/internal/api/admin_requests_test.go +++ /dev/null @@ -1,335 +0,0 @@ -package api - -import ( - "bytes" - "context" - "encoding/json" - "net/http" - "net/http/httptest" - "testing" - - "github.com/go-chi/chi/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/auth" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -// newAdminRequestsRouter builds a test chi router with the three admin/requests -// endpoints. RequireAdmin middleware is applied. Tests inject a user into -// context directly (skipping RequireUser). -func newAdminRequestsRouter(h *handlers) chi.Router { - r := chi.NewRouter() - r.Route("/api/admin", func(admin chi.Router) { - admin.Use(auth.RequireAdmin()) - admin.Get("/requests", h.handleListAdminRequests) - admin.Post("/requests/{id}/approve", h.handleApproveRequest) - admin.Post("/requests/{id}/reject", h.handleRejectRequest) - }) - return r -} - -// doAdminRequestReq fires an HTTP request at the admin requests router with -// the given user injected into context (bypassing RequireUser). -func doAdminRequestReq(t *testing.T, h *handlers, method, path string, body []byte, user dbq.User) *httptest.ResponseRecorder { - t.Helper() - var reqBody *bytes.Buffer - if body != nil { - reqBody = bytes.NewBuffer(body) - } else { - reqBody = bytes.NewBuffer(nil) - } - req := httptest.NewRequest(method, path, reqBody) - if body != nil { - req.Header.Set("Content-Type", "application/json") - } - req = withUser(req, user) - w := httptest.NewRecorder() - newAdminRequestsRouter(h).ServeHTTP(w, req) - return w -} - -// seedPendingArtistRequest creates a pending artist-kind request row -// for the given user and returns its parsed requestView. -func seedPendingArtistRequest(t *testing.T, h *handlers, user dbq.User, artistMBID, artistName string) requestView { - t.Helper() - return createArtistRequest(t, h, user, artistMBID, artistName) -} - -// TestHandleListAdminRequests_DefaultStatus_Pending seeds 2 pending + 1 -// rejected and verifies that GET without ?status returns only the 2 pending -// rows. -func TestHandleListAdminRequests_DefaultStatus_Pending(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, h.pool, "alice", "pw", false) - admin := seedUser(t, h.pool, "admin", "pw", true) - - createArtistRequest(t, h, alice, "mbid-pending-1", "Pending Artist 1") - createArtistRequest(t, h, alice, "mbid-pending-2", "Pending Artist 2") - - // Reject one request directly via service. - rv3 := createArtistRequest(t, h, alice, "mbid-reject-1", "Reject Me") - _, err := h.requests.Reject(context.Background(), rv3.ID, admin.ID, "test") - if err != nil { - t.Fatalf("reject seed: %v", err) - } - - w := doAdminRequestReq(t, h, http.MethodGet, "/api/admin/requests", nil, admin) - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - - var results []requestView - if err := json.Unmarshal(w.Body.Bytes(), &results); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if len(results) != 2 { - t.Fatalf("len(results) = %d, want 2 (pending only)", len(results)) - } - for _, rv := range results { - if rv.Status != "pending" { - t.Errorf("result status = %q, want pending", rv.Status) - } - if rv.Username != alice.Username { - t.Errorf("result username = %q, want %q (the requester)", rv.Username, alice.Username) - } - } -} - -// TestHandleListAdminRequests_StatusFilter verifies GET ?status=rejected -// returns only rejected rows. -func TestHandleListAdminRequests_StatusFilter(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, h.pool, "alice", "pw", false) - admin := seedUser(t, h.pool, "admin", "pw", true) - - createArtistRequest(t, h, alice, "mbid-pending-f1", "Pending Filter 1") - - rv2 := createArtistRequest(t, h, alice, "mbid-rejected-f1", "Rejected Filter 1") - _, err := h.requests.Reject(context.Background(), rv2.ID, admin.ID, "") - if err != nil { - t.Fatalf("reject seed: %v", err) - } - - w := doAdminRequestReq(t, h, http.MethodGet, "/api/admin/requests?status=rejected", nil, admin) - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - - var results []requestView - if err := json.Unmarshal(w.Body.Bytes(), &results); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if len(results) != 1 { - t.Fatalf("len(results) = %d, want 1 rejected row", len(results)) - } - if results[0].Status != "rejected" { - t.Errorf("status = %q, want rejected", results[0].Status) - } -} - -// TestHandleListAdminRequests_BadStatus_400 verifies GET with an invalid -// ?status returns 400. -func TestHandleListAdminRequests_BadStatus_400(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - admin := seedUser(t, h.pool, "admin", "pw", true) - - w := doAdminRequestReq(t, h, http.MethodGet, "/api/admin/requests?status=garbage", nil, admin) - if w.Code != http.StatusBadRequest { - t.Fatalf("status = %d, want 400; body = %s", w.Code, w.Body.String()) - } - var resp map[string]string - if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if resp["error"] != "invalid_status" { - t.Errorf("error = %q, want invalid_status", resp["error"]) - } -} - -// TestHandleApproveRequest_NotPending_409 pre-rejects a row and verifies -// POST /approve → 409 request_not_pending. -func TestHandleApproveRequest_NotPending_409(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, h.pool, "alice", "pw", false) - admin := seedUser(t, h.pool, "admin", "pw", true) - - rv := seedPendingArtistRequest(t, h, alice, "artist-mbid-notpending", "Not Pending") - - // Pre-reject the row. - _, err := h.requests.Reject(context.Background(), rv.ID, admin.ID, "pre-rejected") - if err != nil { - t.Fatalf("pre-reject: %v", err) - } - - w := doAdminRequestReq(t, h, http.MethodPost, "/api/admin/requests/"+uuidToString(rv.ID)+"/approve", nil, admin) - if w.Code != http.StatusConflict { - t.Fatalf("status = %d, want 409; body = %s", w.Code, w.Body.String()) - } - var resp map[string]string - if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if resp["error"] != "request_not_pending" { - t.Errorf("error = %q, want request_not_pending", resp["error"]) - } -} - -// TestHandleRejectRequest_HappyPath verifies POST /reject with notes returns -// 200 and the row transitions to rejected with notes saved. -func TestHandleRejectRequest_HappyPath(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, h.pool, "alice", "pw", false) - admin := seedUser(t, h.pool, "admin", "pw", true) - - rv := seedPendingArtistRequest(t, h, alice, "artist-mbid-reject", "Reject Me") - - body := []byte(`{"notes":"low-quality release; will look for the remaster"}`) - w := doAdminRequestReq(t, h, http.MethodPost, "/api/admin/requests/"+uuidToString(rv.ID)+"/reject", body, admin) - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - - var got requestView - if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if got.Status != "rejected" { - t.Errorf("status = %q, want rejected", got.Status) - } - if got.Notes == nil || *got.Notes != "low-quality release; will look for the remaster" { - t.Errorf("notes = %v, want expected string", got.Notes) - } -} - -// TestHandleRejectRequest_NoNotes_OK verifies POST /reject with empty body -// returns 200 (notes are optional). -func TestHandleRejectRequest_NoNotes_OK(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, h.pool, "alice", "pw", false) - admin := seedUser(t, h.pool, "admin", "pw", true) - - rv := seedPendingArtistRequest(t, h, alice, "artist-mbid-reject-nonotes", "Reject No Notes") - - w := doAdminRequestReq(t, h, http.MethodPost, "/api/admin/requests/"+uuidToString(rv.ID)+"/reject", nil, admin) - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - - var got requestView - if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if got.Status != "rejected" { - t.Errorf("status = %q, want rejected", got.Status) - } -} - -// TestAdminRequestsEndpoints_NonAdmin403 is a table-driven test verifying that -// all 3 admin request endpoints reject non-admin users with 403. -func TestAdminRequestsEndpoints_NonAdmin403(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - nonAdmin := seedUser(t, h.pool, "regular", "pw", false) - - // Use a placeholder UUID for the ID endpoints. - fakeID := "00000000-0000-0000-0000-000000000001" - - cases := []struct { - method string - path string - body []byte - }{ - {http.MethodGet, "/api/admin/requests", nil}, - {http.MethodPost, "/api/admin/requests/" + fakeID + "/approve", nil}, - {http.MethodPost, "/api/admin/requests/" + fakeID + "/reject", nil}, - } - - for _, tc := range cases { - tc := tc - t.Run(tc.method+" "+tc.path, func(t *testing.T) { - w := doAdminRequestReq(t, h, tc.method, tc.path, tc.body, nonAdmin) - if w.Code != http.StatusForbidden { - t.Fatalf("status = %d, want 403; body = %s", w.Code, w.Body.String()) - } - var resp map[string]string - if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if resp["error"] != "not_authorized" { - t.Errorf("error = %q, want not_authorized", resp["error"]) - } - }) - } -} - -// TestHandleApproveRequest_AlbumBecomesAWant: approving an album request puts -// its release group on the wanted list, as the requester's. -func TestHandleApproveRequest_AlbumBecomesAWant(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - alice := seedUser(t, h.pool, "alice", "pw", false) - admin := seedUser(t, h.pool, "admin", "pw", true) - - w := doCreateRequest(h, alice, `{"kind":"album","artist_mbid":"artist-x","artist_name":"Gorillaz","album_mbid":"group-x","album_title":"Demon Days"}`) - if w.Code != http.StatusCreated { - t.Fatalf("create: %d %s", w.Code, w.Body.String()) - } - var rv requestView - _ = json.Unmarshal(w.Body.Bytes(), &rv) - - resp := doAdminRequestReq(t, h, http.MethodPost, "/api/admin/requests/"+uuidToString(rv.ID)+"/approve", nil, admin) - if resp.Code != http.StatusOK { - t.Fatalf("approve: %d %s", resp.Code, resp.Body.String()) - } - want, err := dbq.New(h.pool).GetWantByReleaseGroup(context.Background(), "group-x") - if err != nil { - t.Fatalf("no want recorded: %v", err) - } - if want.Title != "Demon Days" || want.CreatedBy != alice.ID || want.Reason != "request" { - t.Errorf("want = %+v", want) - } -} - -// TestHandleApproveRequest_ArtistOnTheChosenProfile: an artist request is -// followed on the profile the admin picked; an unknown profile is a 400. -func TestHandleApproveRequest_ArtistOnTheChosenProfile(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - alice := seedUser(t, h.pool, "alice", "pw", false) - admin := seedUser(t, h.pool, "admin", "pw", true) - ctx := context.Background() - - profile, err := dbq.New(h.pool).CreateReleaseTypeProfile(ctx, dbq.CreateReleaseTypeProfileParams{ - Name: "Albums only", PrimaryTypes: []string{"Album"}, SecondaryTypes: []string{}, - }) - if err != nil { - t.Fatal(err) - } - rv := seedPendingArtistRequest(t, h, alice, "artist-profiled", "Profiled") - path := "/api/admin/requests/" + uuidToString(rv.ID) + "/approve" - - if resp := doAdminRequestReq(t, h, http.MethodPost, path, []byte(`{"profile_id":"00000000-0000-4000-8000-000000000000"}`), admin); resp.Code != http.StatusBadRequest { - t.Fatalf("unknown profile: %d %s", resp.Code, resp.Body.String()) - } - body := []byte(`{"profile_id":"` + uuidToString(profile.ID) + `"}`) - if resp := doAdminRequestReq(t, h, http.MethodPost, path, body, admin); resp.Code != http.StatusOK { - t.Fatalf("approve: %d %s", resp.Code, resp.Body.String()) - } - f, err := dbq.New(h.pool).GetFollow(ctx, "artist-profiled") - if err != nil || f.ProfileID != profile.ID || !f.WantExisting { - t.Errorf("follow = %+v, %v: on Albums only, wanting what exists", f, err) - } -} diff --git a/internal/api/admin_search.go b/internal/api/admin_search.go deleted file mode 100644 index aaebdb50..00000000 --- a/internal/api/admin_search.go +++ /dev/null @@ -1,340 +0,0 @@ -package api - -import ( - "context" - "encoding/json" - "errors" - "net/http" - "slices" - "time" - - "github.com/go-chi/chi/v5" - "github.com/jackc/pgx/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" -) - -// Searching for wants and grabbing releases: the schedule's -// settings, each want's candidates, and a person's Search, Grab and "never -// this one". - -// searchTimeout bounds a search a person waits on: a Prowlarr fanning out to -// many trackers can take most of a minute. -const searchTimeout = 2 * time.Minute - -type searchSettingsView struct { - AutoGrab bool `json:"auto_grab"` - SearchIntervalHours int32 `json:"search_interval_hours"` - AllowedQualities []string `json:"allowed_qualities"` - Tiers []release.TierInfo `json:"tiers"` - LastPassAt *string `json:"last_pass_at"` - LastPass json.RawMessage `json:"last_pass"` - // ImportRoot is the library folder new music is filed into; "" - // is the first of ScanPaths, the library's folders. - ImportRoot string `json:"import_root"` - ScanPaths []string `json:"scan_paths"` - // The download lifecycle: seed an imported download to this - // ratio or for this many days; give up on one nobody shares after this - // many hours. 0 is: remove once imported, no time limit, never. - SeedRatio float32 `json:"seed_ratio"` - SeedDays int32 `json:"seed_days"` - StallHours int32 `json:"stall_hours"` - // UpgradeUntil is the tier a held album is upgraded to; "" is - // no quality upgrades. - UpgradeUntil string `json:"upgrade_until"` -} - -func searchSettings(s dbq.AcquisitionSetting, roots []string) searchSettingsView { - v := searchSettingsView{ - AutoGrab: s.AutoGrab, SearchIntervalHours: s.SearchIntervalHours, - AllowedQualities: s.AllowedQualities, Tiers: release.Tiers, LastPass: s.LastPass, - ImportRoot: s.ImportRoot, ScanPaths: roots, - SeedRatio: s.SeedRatio, SeedDays: s.SeedDays, StallHours: s.StallHours, - UpgradeUntil: s.UpgradeUntil, - } - if v.ScanPaths == nil { - v.ScanPaths = []string{} - } - if v.AllowedQualities == nil { - v.AllowedQualities = []string{} - } - if len(v.LastPass) == 0 { - v.LastPass = json.RawMessage("{}") - } - if s.LastPassAt.Valid { - at := formatTimestamp(s.LastPassAt) - v.LastPassAt = &at - } - return v -} - -// handleGetSearchSettings implements GET /api/admin/acquisition/settings. -func (h *handlers) handleGetSearchSettings(w http.ResponseWriter, r *http.Request) { - s, err := dbq.New(h.pool).GetAcquisitionSettings(r.Context()) - if err != nil { - writeErrWithLog(w, h.logger, "admin search settings: get", apierror.Internal(err)) - return - } - writeJSON(w, http.StatusOK, searchSettings(s, h.importer.Roots())) -} - -type putSearchSettingsBody struct { - AutoGrab bool `json:"auto_grab"` - SearchIntervalHours int32 `json:"search_interval_hours"` - AllowedQualities []string `json:"allowed_qualities"` - ImportRoot string `json:"import_root"` - SeedRatio float32 `json:"seed_ratio"` - SeedDays int32 `json:"seed_days"` - StallHours int32 `json:"stall_hours"` - UpgradeUntil string `json:"upgrade_until"` -} - -// handlePutSearchSettings implements PUT /api/admin/acquisition/settings. -func (h *handlers) handlePutSearchSettings(w http.ResponseWriter, r *http.Request) { - var body putSearchSettingsBody - if !decodeBody(w, r, &body) { - return - } - if body.SearchIntervalHours < 0 || body.SearchIntervalHours > 720 { - writeErr(w, apierror.BadRequest("bad_interval", "search_interval_hours must be between 0 and 720")) - return - } - if len(body.AllowedQualities) == 0 { - writeErr(w, apierror.BadRequest("bad_quality", "allow at least one quality")) - return - } - for _, t := range body.AllowedQualities { - if !release.KnownTier(t) { - writeErr(w, apierror.BadRequest("bad_quality", "unknown quality "+t)) - return - } - } - if body.UpgradeUntil != "" && !release.KnownTier(body.UpgradeUntil) { - writeErr(w, apierror.BadRequest("bad_upgrade_until", "unknown quality "+body.UpgradeUntil)) - return - } - if body.ImportRoot != "" && !slices.Contains(h.importer.Roots(), body.ImportRoot) { - writeErr(w, apierror.BadRequest("bad_import_root", "file new music into one of the library's folders")) - return - } - if body.SeedRatio < 0 || body.SeedRatio > 100 || body.SeedDays < 0 || body.SeedDays > 365 || - body.StallHours < 0 || body.StallHours > 720 { - writeErr(w, apierror.BadRequest("bad_lifecycle", - "seed ratio must be 0 to 100, seed days 0 to 365 and stall hours 0 to 720")) - return - } - s, err := dbq.New(h.pool).UpdateAcquisitionSettings(r.Context(), dbq.UpdateAcquisitionSettingsParams{ - AutoGrab: body.AutoGrab, SearchIntervalHours: body.SearchIntervalHours, AllowedQualities: body.AllowedQualities, - ImportRoot: body.ImportRoot, SeedRatio: body.SeedRatio, SeedDays: body.SeedDays, StallHours: body.StallHours, - UpgradeUntil: body.UpgradeUntil, - }) - if err != nil { - writeErrWithLog(w, h.logger, "admin search settings: put", apierror.Internal(err)) - return - } - writeJSON(w, http.StatusOK, searchSettings(s, h.importer.Roots())) -} - -// candidateView is one release a want's last search found. -type candidateView struct { - Key string `json:"key"` - Name string `json:"name"` - InfoHash string `json:"info_hash"` - Indexer string `json:"indexer"` - SizeBytes int64 `json:"size_bytes"` - Seeders int32 `json:"seeders"` - FilesCount int32 `json:"files_count"` - Format string `json:"format"` - Quality string `json:"quality"` - Year int32 `json:"year"` - Verdict string `json:"verdict"` - Reason string `json:"reason"` - Reasons json.RawMessage `json:"reasons"` - FoldedFrom []string `json:"folded_from"` - SearchedAt string `json:"searched_at"` - Downloading bool `json:"downloading"` -} - -// handleListCandidates implements GET /api/admin/wanted/{id}/candidates. -func (h *handlers) handleListCandidates(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - q := dbq.New(h.pool) - rows, err := q.ListWantCandidates(r.Context(), id) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: candidates", apierror.Internal(err)) - return - } - live, err := q.GetLiveGrab(r.Context(), id) - if err != nil && !errors.Is(err, pgx.ErrNoRows) { - writeErrWithLog(w, h.logger, "admin wanted: live grab", apierror.Internal(err)) - return - } - out := make([]candidateView, 0, len(rows)) - for _, c := range rows { - v := candidateView{ - Key: c.Key, Name: c.Name, InfoHash: c.InfoHash, Indexer: c.Indexer, SizeBytes: c.SizeBytes, - Seeders: c.Seeders, FilesCount: c.FilesCount, Format: c.Format, Quality: c.Quality, Year: c.Year, - Verdict: c.Verdict, Reason: c.Reason, Reasons: c.Reasons, FoldedFrom: c.FoldedFrom, - SearchedAt: formatTimestamp(c.SearchedAt), - Downloading: live.ID.Valid && live.Key == c.Key, - } - if v.FoldedFrom == nil { - v.FoldedFrom = []string{} - } - if len(v.Reasons) == 0 { - v.Reasons = json.RawMessage("[]") - } - out = append(out, v) - } - writeJSON(w, http.StatusOK, map[string]any{"items": out}) -} - -// handleSearchWant implements POST /api/admin/wanted/{id}/search: search -// now, as the schedule would, taking the best release when auto-grab is on. -func (h *handlers) handleSearchWant(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - ctx, cancel := context.WithTimeout(r.Context(), searchTimeout) - defer cancel() - res, err := h.searcher.SearchNow(ctx, id) - if h.writeSearchErr(w, err) { - return - } - writeJSON(w, http.StatusOK, res) -} - -type grabBody struct { - Key string `json:"key"` - Force bool `json:"force"` -} - -// grabView is what came of a person's grab. -type grabView struct { - Added bool `json:"added"` - Refused bool `json:"refused"` - Gone bool `json:"gone"` - Unconfigured bool `json:"unconfigured"` - Missing string `json:"missing,omitempty"` - Kept bool `json:"kept"` - Replaced bool `json:"replaced"` - Verdict string `json:"verdict,omitempty"` - Reason string `json:"reason,omitempty"` - Error string `json:"error,omitempty"` -} - -// handleGrabCandidate implements POST /api/admin/wanted/{id}/grab: take the -// release named by key. The search runs again first, so the indexer's link -// never leaves the server; force takes what the verdict would not. -func (h *handlers) handleGrabCandidate(w http.ResponseWriter, r *http.Request) { - admin, ok := requireUser(w, r) - if !ok { - return - } - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - var body grabBody - if !decodeBody(w, r, &body) { - return - } - ctx, cancel := context.WithTimeout(r.Context(), searchTimeout) - defer cancel() - took, err := h.searcher.Take(ctx, id, body.Key, body.Force, admin.ID) - if h.writeSearchErr(w, err) { - return - } - v := grabView{ - Added: took.Added, Refused: took.Refused, Gone: took.Gone, Unconfigured: took.Unconfigured, - Missing: took.Missing, Error: took.Error, - } - if took.Chosen != nil { - v.Verdict, v.Reason = string(took.Chosen.Verdict), took.Chosen.Reason - } - if took.Contest != nil { - v.Kept, v.Replaced = took.Contest.Kept, took.Contest.Replaced - if took.Contest.Kept { - v.Reason = took.Contest.Reason - } - } - writeJSON(w, http.StatusOK, v) -} - -type blockBody struct { - Key string `json:"key"` -} - -// handleBlockCandidate implements POST /api/admin/wanted/{id}/block: never -// take this release again, for any want. -func (h *handlers) handleBlockCandidate(w http.ResponseWriter, r *http.Request) { - admin, ok := requireUser(w, r) - if !ok { - return - } - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - var body blockBody - if !decodeBody(w, r, &body) { - return - } - c, err := dbq.New(h.pool).GetWantCandidate(r.Context(), dbq.GetWantCandidateParams{WantID: id, Key: body.Key}) - if errors.Is(err, pgx.ErrNoRows) { - writeErr(w, apierror.NotFound("candidate")) - return - } - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: block", apierror.Internal(err)) - return - } - if c.InfoHash == "" { - writeErr(w, apierror.BadRequest("no_infohash", "this release has no infohash to set aside by")) - return - } - if err := acquire.NewMemory(h.pool).Block(r.Context(), dbq.BlockReleaseParams{ - InfoHash: c.InfoHash, Name: c.Name, Reason: acquire.BlockOperator, WantID: id, BlockedBy: admin.ID, - }); err != nil { - writeErrWithLog(w, h.logger, "admin wanted: block", apierror.Internal(err)) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// handleUnblockRelease implements DELETE /api/admin/release-blocks/{hash}. -func (h *handlers) handleUnblockRelease(w http.ResponseWriter, r *http.Request) { - removed, err := acquire.NewMemory(h.pool).Unblock(r.Context(), chi.URLParam(r, "hash")) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: unblock", apierror.Internal(err)) - return - } - if !removed { - writeErr(w, apierror.NotFound("block")) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// writeSearchErr answers a Searcher error, and says whether there was one. -func (h *handlers) writeSearchErr(w http.ResponseWriter, err error) bool { - switch { - case err == nil: - return false - case errors.Is(err, pgx.ErrNoRows): - writeErr(w, apierror.NotFound("want")) - case errors.Is(err, wanted.ErrWantHeld): - writeErr(w, apierror.Conflict("want_held", "the library already holds this album, and it isn't wanted for a better copy")) - default: - writeErrWithLog(w, h.logger, "admin wanted: search", apierror.Internal(err)) - } - return true -} diff --git a/internal/api/admin_status.go b/internal/api/admin_status.go index 17c6f8fa..e7866907 100644 --- a/internal/api/admin_status.go +++ b/internal/api/admin_status.go @@ -11,7 +11,7 @@ import ( ) // The admin Overview's status sections: after the verdict, who is -// listening, what is downloading, what the library holds and how much room +// listening, what the library holds and how much room // it has, and how the server itself is doing. One read for the page, polled // while it is open. @@ -36,17 +36,6 @@ type listeningStatusView struct { LastPlayAt *string `json:"last_play_at"` } -type downloadsStatusView struct { - Running int64 `json:"running"` - NeedsYou int64 `json:"needs_you"` - NeedsYouSince *string `json:"needs_you_since"` - ImportedDay int64 `json:"imported_day"` - GivenUpDay int64 `json:"given_up_day"` - // Live is the downloads not yet ended, the ones needing a person first. - Live []downloadView `json:"live"` - ClientsDown []clientDownView `json:"clients_down"` -} - // diskView is the filesystem one library folder, or the data folder, sits // on. type diskView struct { @@ -91,15 +80,10 @@ type serverStatusView struct { type adminStatusView struct { Listening listeningStatusView `json:"listening"` - Downloads downloadsStatusView `json:"downloads"` Library libraryStatusView `json:"library"` Server serverStatusView `json:"server"` } -// overviewLiveDownloads is how many running downloads the Overview lists; -// Activity has the rest. -const overviewLiveDownloads = 5 - // overviewErrors is how many recent errors the Overview lists. const overviewErrors = 10 @@ -136,12 +120,6 @@ func (h *handlers) handleGetAdminStatus(w http.ResponseWriter, r *http.Request) out.Listening.LastPlayAt = &at } - if err := h.downloadsStatus(r, q, &out.Downloads); err != nil { - h.logger.Error("admin status: downloads", "err", err) - writeAdminJSONErr(w, http.StatusInternalServerError, "server_error") - return - } - lib, err := q.LibraryStatus(ctx) if err != nil { h.logger.Error("admin status: library", "err", err) @@ -150,8 +128,8 @@ func (h *handlers) handleGetAdminStatus(w http.ResponseWriter, r *http.Request) } out.Library = libraryStatusView{Tracks: lib.Tracks, Albums: lib.Albums, Artists: lib.Artists, Bytes: lib.Bytes, Disks: []diskView{}} - if h.importer != nil { - for _, root := range h.importer.Roots() { + if h.scanner != nil { + for _, root := range h.scanner.Roots() { out.Library.Disks = append(out.Library.Disks, diskOf(root)) } } @@ -170,39 +148,6 @@ func (h *handlers) handleGetAdminStatus(w http.ResponseWriter, r *http.Request) writeJSON(w, http.StatusOK, out) } -func (h *handlers) downloadsStatus(r *http.Request, q *dbq.Queries, out *downloadsStatusView) error { - st, err := q.DownloadsStatus(r.Context()) - if err != nil { - return err - } - *out = downloadsStatusView{Running: st.Running, NeedsYou: st.NeedsYou, - ImportedDay: st.ImportedDay, GivenUpDay: st.GivenUpDay, - Live: []downloadView{}, ClientsDown: []clientDownView{}} - if st.NeedsYou > 0 && st.NeedsYouSince.Valid { - since := formatTimestamp(st.NeedsYouSince) - out.NeedsYouSince = &since - } - // ListGrabs puts the ones needing a person first, then the running ones. - rows, err := q.ListGrabs(r.Context(), dbq.ListGrabsParams{MaxRows: overviewLiveDownloads}) - if err != nil { - return err - } - for _, g := range rows { - if !g.EndedAt.Valid { - out.Live = append(out.Live, downloadFrom(g)) - } - } - down, err := q.ListClientsDown(r.Context()) - if err != nil { - return err - } - for _, c := range down { - out.ClientsDown = append(out.ClientsDown, clientDownView{Client: c.Client, Detail: c.Detail, - Since: formatTimestamp(c.FirstSeen)}) - } - return nil -} - // diskOf reads the size and free space of the filesystem path is on. func diskOf(path string) diskView { var st syscall.Statfs_t diff --git a/internal/api/admin_status_test.go b/internal/api/admin_status_test.go index 4e2bcc07..cc76037e 100644 --- a/internal/api/admin_status_test.go +++ b/internal/api/admin_status_test.go @@ -76,7 +76,7 @@ func TestAdminStatus_ListeningAndServer(t *testing.T) { if got.Library.Tracks != 2 || got.Library.Albums != 2 || got.Library.Bytes != 10 { t.Errorf("library = %+v, want 2 tracks, 2 albums, 10 bytes", got.Library) } - if got.Downloads.Live == nil || got.Downloads.ClientsDown == nil || got.Library.Disks == nil { + if got.Library.Disks == nil { t.Error("lists must be [], never null") } diff --git a/internal/api/admin_summary.go b/internal/api/admin_summary.go index 9a870497..98f5c6a5 100644 --- a/internal/api/admin_summary.go +++ b/internal/api/admin_summary.go @@ -19,7 +19,6 @@ type adminQueueView struct { // every queue an admin acts on, in one response, so each admin page pays one // cheap read for its rail. type adminSummaryView struct { - Requests adminQueueView `json:"requests"` Reports adminQueueView `json:"reports"` Duplicates adminQueueView `json:"duplicates"` Suspect adminQueueView `json:"suspect"` @@ -37,7 +36,6 @@ func queueView(count int64, since pgtype.Timestamptz) adminQueueView { func adminSummaryFrom(row dbq.AdminSummaryRow) adminSummaryView { return adminSummaryView{ - Requests: queueView(row.Requests, row.RequestsSince), Reports: queueView(row.Reports, row.ReportsSince), Duplicates: queueView(row.Duplicates, row.DuplicatesSince), Suspect: queueView(row.Suspect, row.SuspectSince), diff --git a/internal/api/admin_summary_test.go b/internal/api/admin_summary_test.go index f6970512..dffa9b69 100644 --- a/internal/api/admin_summary_test.go +++ b/internal/api/admin_summary_test.go @@ -35,6 +35,7 @@ func getAdminSummary(t *testing.T, h *handlers, user dbq.User) (int, adminSummar // oldest item has waited; an empty queue has no since. func TestAdminSummary_CountsTheQueuesAndSinceWhen(t *testing.T) { h, _ := testHandlers(t) + truncateLibrary(t, h.pool) admin := seedUser(t, h.pool, "admin", "pw", true) listener := seedUser(t, h.pool, "listener", "pw", false) ctx := context.Background() @@ -43,19 +44,17 @@ func TestAdminSummary_CountsTheQueuesAndSinceWhen(t *testing.T) { if code != http.StatusOK { t.Fatalf("status = %d", code) } - if empty.Requests.Count != 0 || empty.Requests.Since != nil || empty.Missing.Count != 0 { + if empty.Missing.Count != 0 || empty.Missing.Since != nil { t.Errorf("empty summary = %+v", empty) } - if _, err := dbq.New(h.pool).CreateRequest(ctx, dbq.CreateRequestParams{ - UserID: listener.ID, Kind: dbq.RequestKindArtist, ArtistMbid: "a0000000-0000-4000-8000-000000000001", - ArtistName: "Annadale", - }); err != nil { + track := seedQuarantineTrack(t, h, "sum-missing") + if _, err := h.pool.Exec(ctx, `UPDATE tracks SET missing_since = now() WHERE id = $1`, track.ID); err != nil { t.Fatal(err) } _, got := getAdminSummary(t, h, admin) - if got.Requests.Count != 1 || got.Requests.Since == nil { - t.Errorf("requests = %+v, want 1 with a since", got.Requests) + if got.Missing.Count != 1 || got.Missing.Since == nil { + t.Errorf("missing = %+v, want 1 with a since", got.Missing) } if code, _ := getAdminSummary(t, h, listener); code != http.StatusForbidden { diff --git a/internal/api/admin_suspect_sources.go b/internal/api/admin_suspect_sources.go index 8e5a0e90..7e51109b 100644 --- a/internal/api/admin_suspect_sources.go +++ b/internal/api/admin_suspect_sources.go @@ -31,10 +31,6 @@ type suspectTrackView struct { // first pass. VerdictAt is when it was last set. Verdict *string `json:"verdict"` VerdictAt *string `json:"verdict_at"` - // Replacing: the album is on the wanted list for a clean copy; - // SearchedAt is when one was last searched for, null before the first. - Replacing bool `json:"replacing"` - SearchedAt *string `json:"searched_at"` } // suspectGroupView is a folder's worth of flagged tracks. As on the @@ -116,11 +112,6 @@ func groupSuspectByDirectory(rows []dbq.ListSuspectSourceTracksRow) []suspectGro at := row.SourceVerdictAt.Time.UTC().Format(time.RFC3339) t.VerdictAt = &at } - t.Replacing = row.WantUpgrade == "rip" - if t.Replacing && row.SearchedAt.Valid { - at := row.SearchedAt.Time.UTC().Format(time.RFC3339) - t.SearchedAt = &at - } if n := len(groups); n > 0 && groups[n-1].Directory == row.Directory { groups[n-1].Tracks = append(groups[n-1].Tracks, t) continue diff --git a/internal/api/admin_users.go b/internal/api/admin_users.go index 9c729e79..6115e9dd 100644 --- a/internal/api/admin_users.go +++ b/internal/api/admin_users.go @@ -17,13 +17,12 @@ import ( ) type adminUserView struct { - ID string `json:"id"` - Username string `json:"username"` - DisplayName *string `json:"display_name"` - IsAdmin bool `json:"is_admin"` - AutoApproveRequests bool `json:"auto_approve_requests"` - DebugModeEnabled bool `json:"debug_mode_enabled"` - CreatedAt string `json:"created_at"` + ID string `json:"id"` + Username string `json:"username"` + DisplayName *string `json:"display_name"` + IsAdmin bool `json:"is_admin"` + DebugModeEnabled bool `json:"debug_mode_enabled"` + CreatedAt string `json:"created_at"` } type listUsersResp struct { @@ -40,13 +39,12 @@ func (h *handlers) handleAdminListUsers(w http.ResponseWriter, r *http.Request) out := make([]adminUserView, 0, len(rows)) for _, row := range rows { v := adminUserView{ - ID: uuidToString(row.ID), - Username: row.Username, - DisplayName: row.DisplayName, - IsAdmin: row.IsAdmin, - AutoApproveRequests: row.AutoApproveRequests, - DebugModeEnabled: row.DebugModeEnabled, - CreatedAt: row.CreatedAt.Time.UTC().Format(time.RFC3339), + ID: uuidToString(row.ID), + Username: row.Username, + DisplayName: row.DisplayName, + IsAdmin: row.IsAdmin, + DebugModeEnabled: row.DebugModeEnabled, + CreatedAt: row.CreatedAt.Time.UTC().Format(time.RFC3339), } out = append(out, v) } @@ -117,13 +115,12 @@ func (h *handlers) handleUpdateUserAdmin(w http.ResponseWriter, r *http.Request) audit.WriteOrLog(r.Context(), h.pool, h.logger, caller.ID, targetID, action, nil) writeJSON(w, http.StatusOK, adminUserView{ - ID: uuidToString(updated.ID), - Username: updated.Username, - DisplayName: updated.DisplayName, - IsAdmin: updated.IsAdmin, - AutoApproveRequests: updated.AutoApproveRequests, - DebugModeEnabled: updated.DebugModeEnabled, - CreatedAt: updated.CreatedAt.Time.UTC().Format(time.RFC3339), + ID: uuidToString(updated.ID), + Username: updated.Username, + DisplayName: updated.DisplayName, + IsAdmin: updated.IsAdmin, + DebugModeEnabled: updated.DebugModeEnabled, + CreatedAt: updated.CreatedAt.Time.UTC().Format(time.RFC3339), }) } @@ -187,13 +184,12 @@ func (h *handlers) handleAdminCreateUser(w http.ResponseWriter, r *http.Request) map[string]any{"is_admin": req.IsAdmin}) writeJSON(w, http.StatusOK, adminUserView{ - ID: uuidToString(user.ID), - Username: user.Username, - DisplayName: user.DisplayName, - IsAdmin: user.IsAdmin, - AutoApproveRequests: user.AutoApproveRequests, - DebugModeEnabled: user.DebugModeEnabled, - CreatedAt: user.CreatedAt.Time.UTC().Format(time.RFC3339), + ID: uuidToString(user.ID), + Username: user.Username, + DisplayName: user.DisplayName, + IsAdmin: user.IsAdmin, + DebugModeEnabled: user.DebugModeEnabled, + CreatedAt: user.CreatedAt.Time.UTC().Format(time.RFC3339), }) } @@ -302,51 +298,3 @@ func (h *handlers) handleAdminResetPassword(w http.ResponseWriter, r *http.Reque w.WriteHeader(http.StatusNoContent) } - -// --- Admin auto-approve toggle --------------------------------------------- - -type adminAutoApproveReq struct { - AutoApprove bool `json:"auto_approve"` -} - -func (h *handlers) handleAdminAutoApproveToggle(w http.ResponseWriter, r *http.Request) { - caller, ok := requireUser(w, r) - if !ok { - return - } - targetID, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - var req adminAutoApproveReq - if !decodeBody(w, r, &req) { - return - } - - q := dbq.New(h.pool) - updated, err := q.UpdateUserAutoApprove(r.Context(), dbq.UpdateUserAutoApproveParams{ - ID: targetID, - AutoApproveRequests: req.AutoApprove, - }) - if err != nil { - if errors.Is(err, pgx.ErrNoRows) { - writeErr(w, &apierror.Error{Status: http.StatusNotFound, Code: "not_found", Message: ""}) - return - } - writeErrWithLog(w, h.logger, "admin auto-approve: update failed", apierror.Internal(err)) - return - } - - audit.WriteOrLog(r.Context(), h.pool, h.logger, caller.ID, targetID, audit.ActionAutoApproveToggle, - map[string]any{"auto_approve": req.AutoApprove}) - - writeJSON(w, http.StatusOK, adminUserView{ - ID: uuidToString(updated.ID), - Username: updated.Username, - DisplayName: updated.DisplayName, - IsAdmin: updated.IsAdmin, - AutoApproveRequests: updated.AutoApproveRequests, - DebugModeEnabled: updated.DebugModeEnabled, - CreatedAt: updated.CreatedAt.Time.UTC().Format(time.RFC3339), - }) -} diff --git a/internal/api/admin_users_test.go b/internal/api/admin_users_test.go index 6895fb72..5536ce47 100644 --- a/internal/api/admin_users_test.go +++ b/internal/api/admin_users_test.go @@ -25,7 +25,6 @@ func newAdminUsersRouter(h *handlers) chi.Router { admin.Post("/users", h.handleAdminCreateUser) admin.Delete("/users/{id}", h.handleAdminDeleteUser) admin.Post("/users/{id}/reset-password", h.handleAdminResetPassword) - admin.Put("/users/{id}/auto-approve", h.handleAdminAutoApproveToggle) }) return r } @@ -271,40 +270,3 @@ func TestAdminResetPassword_TooShort_400(t *testing.T) { t.Errorf("status = %d, want 400", rec.Code) } } - -func TestAdminAutoApproveToggle(t *testing.T) { - if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - h, pool := testHandlers(t) - admin := seedUser(t, pool, "aacaller", "pw", true) - target := seedUser(t, pool, "aatarget", "pw", false) - - body := `{"auto_approve":true}` - req := httptest.NewRequest(http.MethodPut, "/api/admin/users/"+uuidToString(target.ID)+"/auto-approve", - bytes.NewReader([]byte(body))) - req = withUser(req, admin) - rec := httptest.NewRecorder() - newAdminUsersRouter(h).ServeHTTP(rec, req) - - if rec.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String()) - } - var resp adminUserView - if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v", err) - } - if !resp.AutoApproveRequests { - t.Errorf("auto_approve_requests = false on response; expected true") - } - - // Verify DB. - var aa bool - if err := pool.QueryRow(context.Background(), - "SELECT auto_approve_requests FROM users WHERE id = $1", target.ID).Scan(&aa); err != nil { - t.Fatalf("verify: %v", err) - } - if !aa { - t.Errorf("DB row not updated") - } -} diff --git a/internal/api/admin_wanted.go b/internal/api/admin_wanted.go deleted file mode 100644 index 0cb28b80..00000000 --- a/internal/api/admin_wanted.go +++ /dev/null @@ -1,821 +0,0 @@ -package api - -import ( - "context" - "encoding/json" - "errors" - "net/http" - "strconv" - "strings" - - "github.com/go-chi/chi/v5" - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" -) - -// The wanted list's admin surface: wants, follows and -// release-type profiles. - -// wantView is one row of GET /api/admin/wanted. -type wantView struct { - ID string `json:"id"` - ReleaseGroupMBID string `json:"release_group_mbid"` - Title string `json:"title"` - ArtistMBID string `json:"artist_mbid"` - ArtistName string `json:"artist_name"` - ArtistID string `json:"artist_id,omitempty"` - PrimaryType string `json:"primary_type"` - SecondaryTypes []string `json:"secondary_types"` - Year string `json:"year"` - Reason string `json:"reason"` - RequestedBy string `json:"requested_by,omitempty"` - AlbumID string `json:"album_id,omitempty"` - WantedAt string `json:"wanted_at"` - HeldAt *string `json:"held_at"` - ImageURL string `json:"image_url"` - // Upgrade: why a held album is wanted again, quality or rip, and - // HeldQuality what the library's copy is. - Upgrade string `json:"upgrade,omitempty"` - HeldQuality string `json:"held_quality,omitempty"` - // Search is the schedule's last look; Grab the download running. - Search *wantSearchView `json:"search"` - Grab *wantGrabView `json:"grab"` -} - -type wantSearchView struct { - SearchedAt string `json:"searched_at"` - Outcome string `json:"outcome"` - Considered int32 `json:"considered"` - BestRelease string `json:"best_release"` - BestVerdict string `json:"best_verdict"` - Error string `json:"error"` -} - -// wantGrabView is the want's latest download, running or ended, and what -// its import made of it. -type wantGrabView struct { - ID string `json:"id"` - Name string `json:"name"` - GrabbedAt string `json:"grabbed_at"` - Origin string `json:"origin"` - Quality string `json:"quality"` - Live bool `json:"live"` - EndReason string `json:"end_reason"` - Progress float32 `json:"progress"` - ImportState string `json:"import_state"` - ImportReason string `json:"import_reason"` - EndDetail string `json:"end_detail"` - ImportedAt *string `json:"imported_at"` - ImportedFiles int32 `json:"imported_files"` -} - -// wantCounts sizes each tab of the Wanted page. Artists: how many artists -// the list holds albums of, its main page. -type wantCounts struct { - Artists int64 `json:"artists"` - // Unmatched: library albums waiting to be matched by hand. - Unmatched int64 `json:"unmatched"` - Wanted int64 `json:"wanted"` - Missing int64 `json:"missing"` - Held int64 `json:"held"` - // Upgrades: held albums wanted for a better copy. - Upgrades int64 `json:"upgrades"` - Following int64 `json:"following"` - // Activity: downloads running; Attention: of those, the ones waiting on - // a person; Blocked: releases set aside. - Activity int64 `json:"activity"` - Attention int64 `json:"attention"` - Blocked int64 `json:"blocked"` -} - -type wantsPage struct { - Items []wantView `json:"items"` - Total int64 `json:"total"` - Limit int `json:"limit"` - Offset int `json:"offset"` - Counts wantCounts `json:"counts"` - // Library is the one-time listing of the library's albums. - Library libraryListingView `json:"library"` - // Artist heads one artist's page, when the list is narrowed to them. - Artist *wantArtistView `json:"artist,omitempty"` -} - -// wantArtistView is one artist on the Wanted view's main page: how -// many of their albums the list holds, of how many. -type wantArtistView struct { - ArtistMBID string `json:"artist_mbid"` - ArtistName string `json:"artist_name"` - SortName string `json:"sort_name"` - ArtistID string `json:"artist_id,omitempty"` - ImageURL string `json:"image_url"` - Albums int32 `json:"albums"` - Held int32 `json:"held"` - Wanted int32 `json:"wanted"` - Missing int32 `json:"missing"` - Downloading int32 `json:"downloading"` - Following bool `json:"following"` -} - -type wantArtistsPage struct { - Items []wantArtistView `json:"items"` - Total int64 `json:"total"` - Limit int `json:"limit"` - Offset int `json:"offset"` - Counts wantCounts `json:"counts"` - Library libraryListingView `json:"library"` -} - -// libraryListingView: whether the library's albums are all on the list -// yet, and how many could not be matched to MusicBrainz; those are -// listed in the Collection's Unmatched section. -type libraryListingView struct { - ListedAt *string `json:"listed_at"` - Unlisted int32 `json:"unlisted"` -} - -// unmatchedAlbumView is one album in the Collection's Unmatched section: why -// no MusicBrainz album matched it, or that a person left it off. -type unmatchedAlbumView struct { - AlbumID string `json:"album_id"` - Title string `json:"title"` - Artist string `json:"artist"` - Detail string `json:"detail"` - Tracks int32 `json:"tracks"` - LeftOff bool `json:"left_off"` - DecidedAt string `json:"decided_at"` -} - -// unmatchedShown bounds the Unmatched section: a library a listing could -// not match more of than this has a bigger problem than one page shows. -const unmatchedShown = 500 - -const wantsPageSize = 50 - -// wantArtistsPageSize fills a few rows of the artist grid a page. -const wantArtistsPageSize = 96 - -// wantsHeader is what every Collection page carries: each section's count, -// and how the one-time library listing went. -func (h *handlers) wantsHeader(r *http.Request, q *dbq.Queries) (wantCounts, libraryListingView, error) { - counts, err := q.CountWants(r.Context()) - if err != nil { - return wantCounts{}, libraryListingView{}, err - } - listing, err := q.GetLibraryListing(r.Context()) - if err != nil { - return wantCounts{}, libraryListingView{}, err - } - lib := libraryListingView{Unlisted: listing.LibraryUnlisted} - if listing.LibraryListedAt.Valid { - at := formatTimestamp(listing.LibraryListedAt) - lib.ListedAt = &at - } - return wantCounts{Artists: counts.Artists, Unmatched: counts.Unmatched, Wanted: counts.Wanted, Missing: counts.Missing, Held: counts.Held, - Upgrades: counts.Upgrades, Following: counts.Following, Activity: counts.Activity, Attention: counts.Attention, Blocked: counts.Blocked}, lib, nil -} - -func offsetParam(r *http.Request) int { - offset, _ := strconv.Atoi(r.URL.Query().Get("offset")) - return max(offset, 0) -} - -func wantArtistViewOf(row dbq.ListWantArtistsRow) wantArtistView { - v := wantArtistView{ - ArtistMBID: row.ArtistMbid, ArtistName: row.ArtistName, SortName: row.SortName, - ImageURL: artistArtURL(row.ArtistMbid, row.ArtistName), - Albums: row.Albums, Held: row.Held, Wanted: row.Wanted, Missing: row.Missing, - Downloading: row.Downloading, Following: row.Following, - } - if row.LibraryArtistID.Valid { - v.ArtistID = uuidToString(row.LibraryArtistID) - } - return v -} - -// handleListWantArtists implements GET /api/admin/wanted/artists?q=&offset=: -// the Wanted view's main page, every artist on the list A–Z. -func (h *handlers) handleListWantArtists(w http.ResponseWriter, r *http.Request) { - offset := offsetParam(r) - q := dbq.New(h.pool) - rows, err := q.ListWantArtists(r.Context(), dbq.ListWantArtistsParams{ - Q: strings.TrimSpace(r.URL.Query().Get("q")), Skip: int32(offset), MaxRows: wantArtistsPageSize}) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: artists", apierror.Internal(err)) - return - } - counts, lib, err := h.wantsHeader(r, q) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: counts", apierror.Internal(err)) - return - } - out := wantArtistsPage{Items: make([]wantArtistView, 0, len(rows)), Limit: wantArtistsPageSize, Offset: offset, - Counts: counts, Library: lib} - for _, row := range rows { - out.Total = row.Total - out.Items = append(out.Items, wantArtistViewOf(row)) - } - writeJSON(w, http.StatusOK, out) -} - -// handleListWants implements GET -// /api/admin/wanted?state=wanted|missing|upgrade|held|all&artist=&q=&offset=. -// wanted is everything open but missing files; upgrade the held albums wanted -// for a better copy. Each list runs artist by artist, A–Z, like the library; q -// narrows it to an artist or album, and artist to one artist's page, which it -// then heads. -func (h *handlers) handleListWants(w http.ResponseWriter, r *http.Request) { - state := r.URL.Query().Get("state") - if state == "" { - state = "wanted" - } - if state != "wanted" && state != "missing" && state != "upgrade" && state != "held" && state != "all" { - writeErr(w, apierror.BadRequest("bad_state", "state must be wanted, missing, upgrade, held or all")) - return - } - offset := offsetParam(r) - artist := strings.TrimSpace(r.URL.Query().Get("artist")) - q := dbq.New(h.pool) - rows, err := q.ListWants(r.Context(), dbq.ListWantsParams{State: state, ArtistMbid: artist, - Q: strings.TrimSpace(r.URL.Query().Get("q")), Skip: int32(offset), MaxRows: wantsPageSize}) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: list", apierror.Internal(err)) - return - } - counts, lib, err := h.wantsHeader(r, q) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: counts", apierror.Internal(err)) - return - } - out := wantsPage{Library: lib, Counts: counts, - Items: make([]wantView, 0, len(rows)), Limit: wantsPageSize, Offset: offset} - if artist != "" { - heads, err := q.ListWantArtists(r.Context(), dbq.ListWantArtistsParams{ArtistMbid: artist, MaxRows: 1}) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: artist", apierror.Internal(err)) - return - } - if len(heads) == 0 { - writeErr(w, apierror.NotFound("artist")) - return - } - v := wantArtistViewOf(heads[0]) - out.Artist = &v - } - for _, row := range rows { - out.Total = row.Total - out.Items = append(out.Items, wantViewOf(row)) - } - writeJSON(w, http.StatusOK, out) -} - -// wantViewOf is one ListWants row as the wanted list and the Missing page -// send it. -func wantViewOf(row dbq.ListWantsRow) wantView { - v := wantView{ - ID: uuidToString(row.ID), ReleaseGroupMBID: row.ReleaseGroupMbid, Title: row.Title, - ArtistMBID: row.ArtistMbid, ArtistName: row.ArtistName, - PrimaryType: row.PrimaryType, SecondaryTypes: row.SecondaryTypes, - Year: first4(row.FirstReleaseDate), Reason: row.Reason, - WantedAt: formatTimestamp(row.WantedAt), Upgrade: row.Upgrade, HeldQuality: row.HeldQuality, - ImageURL: groupArtURL(musicbrainz.ReleaseGroup{MBID: row.ReleaseGroupMbid, Title: row.Title, ArtistCredit: row.ArtistName}), - } - if row.LibraryArtistID.Valid { - v.ArtistID = uuidToString(row.LibraryArtistID) - } - if row.AlbumID.Valid { - v.AlbumID = uuidToString(row.AlbumID) - } - if row.RequestedBy != nil { - v.RequestedBy = *row.RequestedBy - } - if row.FulfilledAt.Valid { - held := formatTimestamp(row.FulfilledAt) - v.HeldAt = &held - } - if v.SecondaryTypes == nil { - v.SecondaryTypes = []string{} - } - if row.SearchedAt.Valid { - v.Search = &wantSearchView{ - SearchedAt: formatTimestamp(row.SearchedAt), Outcome: row.SearchOutcome, - Considered: row.SearchConsidered, BestRelease: row.BestRelease, - BestVerdict: row.BestVerdict, Error: row.SearchError, - } - } - if row.GrabName != nil { - v.Grab = &wantGrabView{ID: uuidToString(row.GrabID), Name: *row.GrabName, - GrabbedAt: formatTimestamp(row.GrabbedAt), Live: !row.GrabEndedAt.Valid, - Origin: deref(row.GrabOrigin), Quality: deref(row.GrabQuality), EndReason: deref(row.GrabEndReason), - ImportState: deref(row.GrabImportState), ImportReason: deref(row.GrabImportReason), - EndDetail: deref(row.GrabEndDetail)} - if row.GrabProgress != nil { - v.Grab.Progress = *row.GrabProgress - } - if row.GrabImportedFiles != nil { - v.Grab.ImportedFiles = *row.GrabImportedFiles - } - if row.GrabImportedAt.Valid { - at := formatTimestamp(row.GrabImportedAt) - v.Grab.ImportedAt = &at - } - } - return v -} - -// wantTracksView is an album's track list for the Wanted view: the -// library's own tracks for an album it holds or held, else MusicBrainz's -// standard edition of what is wanted. -type wantTracksView struct { - // library, musicbrainz, or none when MusicBrainz lists no edition. - Source string `json:"source"` - // Edition names the MusicBrainz release the list is from. - Edition string `json:"edition,omitempty"` - Tracks []wantTrackView `json:"tracks"` -} - -type wantTrackView struct { - Disc int32 `json:"disc"` - Number int32 `json:"number"` - Title string `json:"title"` - LengthMs int32 `json:"length_ms"` - // Missing: the library's file for it is gone. - Missing bool `json:"missing"` -} - -// handleWantTracks implements GET /api/admin/wanted/{id}/tracks. -func (h *handlers) handleWantTracks(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - q := dbq.New(h.pool) - want, err := q.GetWantAlbum(r.Context(), id) - if errors.Is(err, pgx.ErrNoRows) { - writeErr(w, apierror.NotFound("want")) - return - } - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: tracks", apierror.Internal(err)) - return - } - out := wantTracksView{Tracks: []wantTrackView{}} - if want.AlbumID.Valid { - rows, err := q.ListLibraryAlbumTracks(r.Context(), want.AlbumID) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: library tracks", apierror.Internal(err)) - return - } - out.Source = "library" - for _, t := range rows { - v := wantTrackView{Title: t.Title, LengthMs: t.DurationMs, Missing: t.Missing} - if t.DiscNumber != nil { - v.Disc = *t.DiscNumber - } - if t.TrackNumber != nil { - v.Number = *t.TrackNumber - } - out.Tracks = append(out.Tracks, v) - } - writeJSON(w, http.StatusOK, out) - return - } - eds, err := h.catalogue.Releases(r.Context(), want.ReleaseGroupMbid) - switch { - case errors.Is(err, catalogue.ErrNotFound): - out.Source = "none" - writeJSON(w, http.StatusOK, out) - return - case errors.Is(err, musicbrainz.ErrUnreachable), errors.Is(err, musicbrainz.ErrBadResponse): - writeErr(w, &apierror.Error{Status: 503, Code: "musicbrainz_unreachable", Message: "MusicBrainz can't be reached", Cause: err}) - return - case err != nil: - writeErrWithLog(w, h.logger, "admin wanted: editions", apierror.Internal(err)) - return - } - e, ok := standardEdition(eds.Editions) - if !ok { - out.Source = "none" - writeJSON(w, http.StatusOK, out) - return - } - out.Source = "musicbrainz" - out.Edition = editionLabel(e.Release) - for _, t := range e.Tracks { - out.Tracks = append(out.Tracks, wantTrackView{Disc: t.MediumPosition, Number: t.Position, Title: t.Title, LengthMs: t.LengthMs}) - } - writeJSON(w, http.StatusOK, out) -} - -// standardEdition is the edition a person would call the album: an official -// one with the fewest tracks, else any with the fewest. The catalogue lists -// official editions first by date, so a tie goes to the earliest. -func standardEdition(eds []catalogue.Edition) (catalogue.Edition, bool) { - best := -1 - for i, e := range eds { - if len(e.Tracks) == 0 { - continue - } - if best < 0 { - best = i - continue - } - b := eds[best] - bo, eo := b.Release.Status == "Official", e.Release.Status == "Official" - if (eo && !bo) || (eo == bo && len(e.Tracks) < len(b.Tracks)) { - best = i - } - } - if best < 0 { - return catalogue.Edition{}, false - } - return eds[best], true -} - -// editionLabel names a release for a person: "2023 · CD · GB". -func editionLabel(r dbq.MbRelease) string { - var parts []string - for _, p := range []string{first4(r.ReleaseDate), r.Formats, r.Country} { - if p != "" { - parts = append(parts, p) - } - } - return strings.Join(parts, " · ") -} - -func first4(date string) string { - if len(date) < 4 { - return "" - } - return date[:4] -} - -// handleRemoveWant implements DELETE /api/admin/wanted/{id}. -func (h *handlers) handleRemoveWant(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - removed, err := h.wanted.RemoveWant(r.Context(), id) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: remove", apierror.Internal(err)) - return - } - if !removed { - writeErr(w, apierror.NotFound("want")) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// followView is a followed artist. -type followView struct { - ArtistMBID string `json:"artist_mbid"` - ArtistName string `json:"artist_name"` - ArtistID string `json:"artist_id,omitempty"` - ProfileID string `json:"profile_id"` - ProfileName string `json:"profile_name"` - FollowedAt string `json:"followed_at"` - CheckedAt *string `json:"checked_at"` - OpenWants int64 `json:"open_wants"` - ImageURL string `json:"image_url"` -} - -// handleListFollows implements GET /api/admin/follows. -func (h *handlers) handleListFollows(w http.ResponseWriter, r *http.Request) { - rows, err := dbq.New(h.pool).ListFollows(r.Context()) - if err != nil { - writeErrWithLog(w, h.logger, "admin follows: list", apierror.Internal(err)) - return - } - out := make([]followView, 0, len(rows)) - for _, f := range rows { - v := followView{ - ArtistMBID: f.ArtistMbid, ArtistName: f.ArtistName, - ProfileID: uuidToString(f.ProfileID), ProfileName: f.ProfileName, - FollowedAt: formatTimestamp(f.FollowedAt), OpenWants: f.OpenWants, - ImageURL: artistArtURL(f.ArtistMbid, f.ArtistName), - } - if f.ArtistID.Valid { - v.ArtistID = uuidToString(f.ArtistID) - } - if f.CheckedAt.Valid { - checked := formatTimestamp(f.CheckedAt) - v.CheckedAt = &checked - } - out = append(out, v) - } - writeJSON(w, http.StatusOK, out) -} - -// followStateView answers whether one artist is followed, for the control on -// an artist page. -type followStateView struct { - Following bool `json:"following"` - ProfileID string `json:"profile_id,omitempty"` - ProfileName string `json:"profile_name,omitempty"` -} - -// handleGetFollow implements GET /api/admin/follows/{mbid}. -func (h *handlers) handleGetFollow(w http.ResponseWriter, r *http.Request) { - f, err := dbq.New(h.pool).GetFollow(r.Context(), chi.URLParam(r, "mbid")) - if errors.Is(err, pgx.ErrNoRows) { - writeJSON(w, http.StatusOK, followStateView{}) - return - } - if err != nil { - writeErrWithLog(w, h.logger, "admin follows: get", apierror.Internal(err)) - return - } - writeJSON(w, http.StatusOK, followStateView{ - Following: true, ProfileID: uuidToString(f.ProfileID), ProfileName: f.ProfileName, - }) -} - -type putFollowBody struct { - ArtistName string `json:"artist_name"` - ProfileID string `json:"profile_id"` - WantExisting bool `json:"want_existing"` -} - -// handlePutFollow implements PUT /api/admin/follows/{mbid}: follow an artist, -// or change the profile it is followed on. want_existing also wants what the -// artist has already released that the profile passes and isn't held. -func (h *handlers) handlePutFollow(w http.ResponseWriter, r *http.Request) { - admin, ok := requireUser(w, r) - if !ok { - return - } - var body putFollowBody - if !decodeBody(w, r, &body) { - return - } - var profile pgtype.UUID - if body.ProfileID != "" { - if profile, ok = parseUUID(body.ProfileID); !ok { - writeErr(w, apierror.BadRequest("bad_profile", "profile_id is not an id")) - return - } - } - mbid := chi.URLParam(r, "mbid") - if _, err := h.wanted.Follow(r.Context(), wanted.FollowParams{ - ArtistMBID: mbid, ArtistName: body.ArtistName, ProfileID: profile, - By: admin.ID, WantExisting: body.WantExisting, - }); err != nil { - switch { - case errors.Is(err, wanted.ErrNotFollowable): - writeErr(w, apierror.BadRequest("not_followable", "Various Artists and MusicBrainz placeholders can't be followed")) - case errors.Is(err, wanted.ErrProfileNotFound): - writeErr(w, apierror.BadRequest("profile_not_found", "no such profile")) - default: - writeErrWithLog(w, h.logger, "admin follows: put", apierror.Internal(err)) - } - return - } - h.handleGetFollow(w, r) -} - -// handleDeleteFollow implements DELETE /api/admin/follows/{mbid}. What the -// follow already wanted stays on the list. -func (h *handlers) handleDeleteFollow(w http.ResponseWriter, r *http.Request) { - removed, err := h.wanted.Unfollow(r.Context(), chi.URLParam(r, "mbid")) - if err != nil { - writeErrWithLog(w, h.logger, "admin follows: delete", apierror.Internal(err)) - return - } - if !removed { - writeErr(w, apierror.NotFound("follow")) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// releaseProfileView is a release-type profile. -type releaseProfileView struct { - ID string `json:"id"` - Name string `json:"name"` - PrimaryTypes []string `json:"primary_types"` - SecondaryTypes []string `json:"secondary_types"` - IsDefault bool `json:"is_default"` - FollowCount int64 `json:"follow_count"` -} - -// releaseProfilesResponse also carries MusicBrainz's types, so the editor -// offers exactly what a profile may hold. -type releaseProfilesResponse struct { - Profiles []releaseProfileView `json:"profiles"` - PrimaryTypes []string `json:"primary_types"` - SecondaryTypes []string `json:"secondary_types"` -} - -// handleListReleaseProfiles implements GET /api/admin/release-profiles. -func (h *handlers) handleListReleaseProfiles(w http.ResponseWriter, r *http.Request) { - rows, err := dbq.New(h.pool).ListReleaseTypeProfiles(r.Context()) - if err != nil { - writeErrWithLog(w, h.logger, "admin release profiles: list", apierror.Internal(err)) - return - } - out := releaseProfilesResponse{ - Profiles: make([]releaseProfileView, 0, len(rows)), - PrimaryTypes: wanted.PrimaryTypes, SecondaryTypes: wanted.SecondaryTypes, - } - for _, p := range rows { - out.Profiles = append(out.Profiles, releaseProfileView{ - ID: uuidToString(p.ID), Name: p.Name, PrimaryTypes: p.PrimaryTypes, - SecondaryTypes: p.SecondaryTypes, IsDefault: p.IsDefault, FollowCount: p.FollowCount, - }) - } - writeJSON(w, http.StatusOK, out) -} - -type releaseProfileBody struct { - Name string `json:"name"` - PrimaryTypes []string `json:"primary_types"` - SecondaryTypes []string `json:"secondary_types"` - IsDefault bool `json:"is_default"` -} - -func (b releaseProfileBody) input() wanted.ProfileInput { - return wanted.ProfileInput{ - Name: b.Name, PrimaryTypes: b.PrimaryTypes, SecondaryTypes: b.SecondaryTypes, IsDefault: b.IsDefault, - } -} - -// handleCreateReleaseProfile implements POST /api/admin/release-profiles. -func (h *handlers) handleCreateReleaseProfile(w http.ResponseWriter, r *http.Request) { - var body releaseProfileBody - if !decodeBody(w, r, &body) { - return - } - p, err := h.wanted.CreateProfile(r.Context(), body.input()) - if err != nil { - h.writeProfileErr(w, err) - return - } - writeJSON(w, http.StatusCreated, releaseProfileView{ - ID: uuidToString(p.ID), Name: p.Name, PrimaryTypes: p.PrimaryTypes, - SecondaryTypes: p.SecondaryTypes, IsDefault: p.IsDefault, - }) -} - -// handleUpdateReleaseProfile implements PUT /api/admin/release-profiles/{id}. -// A follow on the profile judges new releases by the new types from its next -// check; releases it has already judged are not judged again. -func (h *handlers) handleUpdateReleaseProfile(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - var body releaseProfileBody - if !decodeBody(w, r, &body) { - return - } - p, err := h.wanted.UpdateProfile(r.Context(), id, body.input()) - if err != nil { - h.writeProfileErr(w, err) - return - } - writeJSON(w, http.StatusOK, releaseProfileView{ - ID: uuidToString(p.ID), Name: p.Name, PrimaryTypes: p.PrimaryTypes, - SecondaryTypes: p.SecondaryTypes, IsDefault: p.IsDefault, - }) -} - -// handleDeleteReleaseProfile implements DELETE -// /api/admin/release-profiles/{id}. -func (h *handlers) handleDeleteReleaseProfile(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - if err := h.wanted.DeleteProfile(r.Context(), id); err != nil { - h.writeProfileErr(w, err) - return - } - w.WriteHeader(http.StatusNoContent) -} - -func (h *handlers) writeProfileErr(w http.ResponseWriter, err error) { - switch { - case errors.Is(err, wanted.ErrProfileInvalid): - writeErr(w, apierror.BadRequest("profile_invalid", err.Error())) - case errors.Is(err, wanted.ErrProfileNotFound): - writeErr(w, apierror.NotFound("profile")) - case errors.Is(err, wanted.ErrProfileNameUsed): - writeErr(w, apierror.Conflict("profile_name_taken", "a profile already has that name")) - case errors.Is(err, wanted.ErrProfileInUse): - writeErr(w, apierror.Conflict("profile_in_use", "artists are followed on this profile; move them first")) - case errors.Is(err, wanted.ErrProfileDefault): - writeErr(w, apierror.Conflict("profile_is_default", "the default profile can't be deleted")) - default: - writeErrWithLog(w, h.logger, "admin release profiles", apierror.Internal(err)) - } -} - -// deref is a nullable column's text, or "". -func deref(s *string) string { - if s == nil { - return "" - } - return *s -} - -// handleListUnmatched implements GET /api/admin/wanted/unmatched: the -// Collection's Unmatched section, the albums no MusicBrainz album -// matched and, after them, the ones a person left off. -func (h *handlers) handleListUnmatched(w http.ResponseWriter, r *http.Request) { - rows, err := dbq.New(h.pool).ListUnmatchedLibraryAlbums(r.Context(), unmatchedShown) - if err != nil { - writeErrWithLog(w, h.logger, "admin wanted: unmatched", apierror.Internal(err)) - return - } - out := struct { - Items []unmatchedAlbumView `json:"items"` - }{Items: make([]unmatchedAlbumView, 0, len(rows))} - for _, u := range rows { - out.Items = append(out.Items, unmatchedAlbumView{ - AlbumID: uuidToString(u.AlbumID), Title: u.Title, Artist: u.ArtistName, Detail: u.Detail, - Tracks: u.Tracks, LeftOff: u.Outcome == "dismissed", DecidedAt: formatTimestamp(u.DecidedAt), - }) - } - writeJSON(w, http.StatusOK, out) -} - -type manualMatchView struct { - ReleaseGroupMBID string `json:"release_group_mbid"` - Title string `json:"title"` - Artist string `json:"artist"` - Agree int `json:"agree"` - Tracks int `json:"tracks"` -} - -// handleMatchUnmatched implements POST /api/admin/wanted/unmatched/{id}/match -// {"ref": ""}: the album goes on the list as held, -// as that MusicBrainz album. -func (h *handlers) handleMatchUnmatched(w http.ResponseWriter, r *http.Request) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - var body struct { - Ref string `json:"ref"` - } - if err := json.NewDecoder(r.Body).Decode(&body); err != nil { - writeErr(w, apierror.BadRequest("bad_body", "expected {\"ref\": \"…\"}")) - return - } - m, err := h.wanted.MatchAlbumTo(r.Context(), id, body.Ref) - switch { - case errors.Is(err, wanted.ErrMatchRef): - writeErr(w, apierror.BadRequest("bad_ref", "That isn't a MusicBrainz album link or id.")) - return - case errors.Is(err, wanted.ErrMatchNoGroup): - writeErr(w, &apierror.Error{Status: http.StatusUnprocessableEntity, Code: "no_such_album", - Message: "MusicBrainz has no album at that link.", Cause: err}) - return - case errors.Is(err, wanted.ErrAlbumNotUnmatched): - writeErr(w, apierror.NotFound("unmatched album")) - return - case errors.Is(err, musicbrainz.ErrUnreachable), errors.Is(err, musicbrainz.ErrBadResponse): - writeErr(w, &apierror.Error{Status: http.StatusServiceUnavailable, Code: "musicbrainz_unreachable", - Message: "MusicBrainz can't be reached", Cause: err}) - return - case err != nil: - writeErrWithLog(w, h.logger, "admin wanted: match by hand", apierror.Internal(err)) - return - } - writeJSON(w, http.StatusOK, manualMatchView{ReleaseGroupMBID: m.Group, Title: m.Title, Artist: m.Artist, - Agree: m.Agree, Tracks: m.Tracks}) -} - -// handleLeaveUnmatchedOff implements POST -// /api/admin/wanted/unmatched/{id}/leave-off, and handlePutUnmatchedBack its -// undoing, POST …/{id}/put-back. -func (h *handlers) handleLeaveUnmatchedOff(w http.ResponseWriter, r *http.Request) { - h.setUnmatched(w, r, h.wanted.LeaveAlbumOff) -} - -func (h *handlers) handlePutUnmatchedBack(w http.ResponseWriter, r *http.Request) { - h.setUnmatched(w, r, h.wanted.PutAlbumBack) -} - -func (h *handlers) setUnmatched(w http.ResponseWriter, r *http.Request, set func(context.Context, pgtype.UUID) error) { - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - err := set(r.Context(), id) - switch { - case errors.Is(err, wanted.ErrAlbumNotUnmatched): - writeErr(w, apierror.NotFound("unmatched album")) - case err != nil: - writeErrWithLog(w, h.logger, "admin wanted: unmatched", apierror.Internal(err)) - default: - w.WriteHeader(http.StatusNoContent) - } -} diff --git a/internal/api/admin_wanted_test.go b/internal/api/admin_wanted_test.go deleted file mode 100644 index e80a2edb..00000000 --- a/internal/api/admin_wanted_test.go +++ /dev/null @@ -1,371 +0,0 @@ -package api - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "net/http/httptest" - "net/url" - "strings" - "testing" - - "github.com/go-chi/chi/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/auth" - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" -) - -func doWantedReq(t *testing.T, h *handlers, path string, user dbq.User) *httptest.ResponseRecorder { - t.Helper() - return doWanted(t, h, http.MethodGet, path, "", user) -} - -func doWanted(t *testing.T, h *handlers, method, path, body string, user dbq.User) *httptest.ResponseRecorder { - t.Helper() - r := chi.NewRouter() - r.Route("/api/admin", func(admin chi.Router) { - admin.Use(auth.RequireAdmin()) - admin.Get("/wanted", h.handleListWants) - admin.Get("/wanted/artists", h.handleListWantArtists) - admin.Get("/wanted/unmatched", h.handleListUnmatched) - admin.Post("/wanted/unmatched/{id}/match", h.handleMatchUnmatched) - admin.Post("/wanted/unmatched/{id}/leave-off", h.handleLeaveUnmatchedOff) - admin.Post("/wanted/unmatched/{id}/put-back", h.handlePutUnmatchedBack) - admin.Get("/wanted/{id}/tracks", h.handleWantTracks) - }) - w := httptest.NewRecorder() - r.ServeHTTP(w, withUser(httptest.NewRequest(method, path, strings.NewReader(body)), user)) - return w -} - -func seedWant(t *testing.T, h *handlers, group, artistMBID, artist, title, date string) dbq.Want { - t.Helper() - w, err := wanted.WantWith(context.Background(), dbq.New(h.pool), wanted.WantParams{ - GroupMBID: group, ArtistMBID: artistMBID, ArtistName: artist, Title: title, - PrimaryType: "Album", FirstReleaseDate: date, Reason: wanted.ReasonFollow, - }) - if err != nil { - t.Fatal(err) - } - return w -} - -func listWanted(t *testing.T, h *handlers, user dbq.User, q string) wantsPage { - t.Helper() - w := doWantedReq(t, h, "/api/admin/wanted?"+url.Values{"q": {q}}.Encode(), user) - if w.Code != http.StatusOK { - t.Fatalf("status = %d; body = %s", w.Code, w.Body.String()) - } - var page wantsPage - if err := json.Unmarshal(w.Body.Bytes(), &page); err != nil { - t.Fatal(err) - } - return page -} - -// The list runs like the library: artists A–Z by the library's sort -// name where the library holds the artist, each artist's albums oldest -// first; q narrows it to an artist or an album. -func TestListWants_ArtistsAToZThenTheirAlbumsByDate(t *testing.T) { - h, _ := testHandlers(t) - admin := seedUser(t, h.pool, "admin", "pw", true) - ctx := context.Background() - beatles := "a0000000-0000-4000-8000-000000000002" - if _, err := dbq.New(h.pool).UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "The Beatles", SortName: "Beatles, The", Mbid: &beatles}); err != nil { - t.Fatal(err) - } - seedWant(t, h, "g0000000-0000-4000-8000-000000000001", "a0000000-0000-4000-8000-000000000003", "Zeds", "Zed Album", "2001") - seedWant(t, h, "g0000000-0000-4000-8000-000000000002", beatles, "The Beatles", "Abbey Road", "1969-09-26") - seedWant(t, h, "g0000000-0000-4000-8000-000000000003", beatles, "The Beatles", "Please Please Me", "1963-03-22") - seedWant(t, h, "g0000000-0000-4000-8000-000000000004", "a0000000-0000-4000-8000-000000000001", "Camel", "Moonmadness", "1976") - - page := listWanted(t, h, admin, "") - var got []string - for _, it := range page.Items { - got = append(got, it.ArtistName+"/"+it.Title) - } - want := "[The Beatles/Please Please Me The Beatles/Abbey Road Camel/Moonmadness Zeds/Zed Album]" - if fmt.Sprint(got) != want { - t.Errorf("order = %v, want %s", got, want) - } - if page.Items[0].ArtistID == "" || page.Items[2].ArtistID != "" { - t.Errorf("artist ids = %q, %q: only an artist the library holds has one", page.Items[0].ArtistID, page.Items[2].ArtistID) - } - - page = listWanted(t, h, admin, "BEATLES") - if len(page.Items) != 2 || page.Total != 2 { - t.Errorf("q=BEATLES: %d items, total %d; want the two Beatles albums", len(page.Items), page.Total) - } - page = listWanted(t, h, admin, "moon") - if len(page.Items) != 1 || page.Items[0].Title != "Moonmadness" || page.Total != 1 { - t.Errorf("q=moon: %+v, total %d", page.Items, page.Total) - } - if page = listWanted(t, h, admin, "nothing like it"); len(page.Items) != 0 || page.Total != 0 { - t.Errorf("no match: %d items, total %d", len(page.Items), page.Total) - } -} - -// The Collection's catalogue: every artist on the list A–Z, each -// with how many of their albums are held; an artist's page lists every album -// of theirs, held or not, under that artist. -func TestListWantArtists_CatalogueAndAnArtistsPage(t *testing.T) { - h, _ := testHandlers(t) - admin := seedUser(t, h.pool, "admin", "pw", true) - ctx := context.Background() - q := dbq.New(h.pool) - beatles := "a0000000-0000-4000-8000-000000000002" - camel := "a0000000-0000-4000-8000-000000000001" - if _, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "The Beatles", SortName: "Beatles, The", Mbid: &beatles}); err != nil { - t.Fatal(err) - } - seedWant(t, h, "g0000000-0000-4000-8000-000000000002", beatles, "The Beatles", "Abbey Road", "1969") - held := seedWant(t, h, "g0000000-0000-4000-8000-000000000003", beatles, "The Beatles", "Please Please Me", "1963") - if _, err := h.pool.Exec(ctx, `UPDATE wants SET fulfilled_at = now() WHERE id = $1`, held.ID); err != nil { - t.Fatal(err) - } - seedWant(t, h, "g0000000-0000-4000-8000-000000000004", camel, "Camel", "Moonmadness", "1976") - if _, err := h.pool.Exec(ctx, `INSERT INTO follows (artist_mbid, artist_name, profile_id) - SELECT $1, 'Camel', id FROM release_type_profiles WHERE is_default`, camel); err != nil { - t.Fatal(err) - } - - w := doWantedReq(t, h, "/api/admin/wanted/artists", admin) - if w.Code != http.StatusOK { - t.Fatalf("status = %d; body = %s", w.Code, w.Body.String()) - } - var page wantArtistsPage - if err := json.Unmarshal(w.Body.Bytes(), &page); err != nil { - t.Fatal(err) - } - if len(page.Items) != 2 || page.Total != 2 || page.Counts.Artists != 2 { - t.Fatalf("catalogue = %+v, total %d, artists %d", page.Items, page.Total, page.Counts.Artists) - } - b, c := page.Items[0], page.Items[1] - if b.ArtistName != "The Beatles" || b.SortName != "Beatles, The" || b.Albums != 2 || b.Held != 1 || b.Wanted != 1 || - b.ArtistID == "" || b.Following || b.ImageURL == "" { - t.Errorf("Beatles = %+v", b) - } - if c.ArtistName != "Camel" || c.Albums != 1 || c.Held != 0 || !c.Following || c.ArtistID != "" { - t.Errorf("Camel = %+v", c) - } - - // q finds an artist by an album's title. - w = doWantedReq(t, h, "/api/admin/wanted/artists?q=moon", admin) - page = wantArtistsPage{} - if err := json.Unmarshal(w.Body.Bytes(), &page); err != nil { - t.Fatal(err) - } - if len(page.Items) != 1 || page.Items[0].ArtistName != "Camel" { - t.Errorf("q=moon: %+v", page.Items) - } - - // The Beatles' page: both albums, held or not, headed by the artist. - w = doWantedReq(t, h, "/api/admin/wanted?state=all&artist="+beatles, admin) - var albums wantsPage - if err := json.Unmarshal(w.Body.Bytes(), &albums); err != nil { - t.Fatal(err) - } - if len(albums.Items) != 2 || albums.Items[0].Title != "Please Please Me" || albums.Items[0].HeldAt == nil || - albums.Artist == nil || albums.Artist.Held != 1 { - t.Errorf("artist page = %+v, head %+v", albums.Items, albums.Artist) - } - if w := doWantedReq(t, h, "/api/admin/wanted?state=all&artist=nobody", admin); w.Code != http.StatusNotFound { - t.Errorf("an artist with nothing on the list: status %d, want 404", w.Code) - } -} - -// fakeEditions is a MusicBrainz holding one group with two editions, the -// shorter official one the standard, or 503 when down is set. -func fakeEditions(t *testing.T, group string, down bool) *httptest.Server { - t.Helper() - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if down { - w.WriteHeader(http.StatusServiceUnavailable) - return - } - switch { - case r.URL.Path == "/release-group/"+group: - _, _ = fmt.Fprintf(w, `{"id": %q, "title": "Moonmadness", "primary-type": "Album", "first-release-date": "1976"}`, group) - case r.URL.Path == "/release" && r.URL.Query().Get("release-group") == group: - _, _ = fmt.Fprint(w, `{"release-count": 2, "releases": [ - {"id": "r-deluxe", "title": "Moonmadness", "status": "Official", "date": "2009", "country": "GB", - "media": [{"position": 1, "format": "CD", "tracks": [ - {"position": 1, "number": "1", "title": "Aristillus", "length": 120000, "recording": {"id": "rec-1"}}, - {"position": 2, "number": "2", "title": "Song Within a Song", "length": 433000, "recording": {"id": "rec-2"}}, - {"position": 3, "number": "3", "title": "Bonus", "length": 200000, "recording": {"id": "rec-3"}}]}]}, - {"id": "r-orig", "title": "Moonmadness", "status": "Official", "date": "1976-03", "country": "GB", - "media": [{"position": 1, "format": "12\" Vinyl", "tracks": [ - {"position": 1, "number": "A1", "title": "Aristillus", "length": 120000, "recording": {"id": "rec-1"}}, - {"position": 2, "number": "A2", "title": "Song Within a Song", "length": 433000, "recording": {"id": "rec-2"}}]}]}]}`) - default: - w.WriteHeader(http.StatusNotFound) - } - })) - t.Cleanup(srv.Close) - return srv -} - -func wantTracks(t *testing.T, h *handlers, user dbq.User, id string) (int, wantTracksView) { - t.Helper() - w := doWantedReq(t, h, "/api/admin/wanted/"+id+"/tracks", user) - var out wantTracksView - if w.Code == http.StatusOK { - if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil { - t.Fatal(err) - } - } - return w.Code, out -} - -// An album not yet held shows MusicBrainz's standard edition: the shortest -// official one. -func TestWantTracks_StandardEditionFromMusicBrainz(t *testing.T) { - const group = "g0000000-0000-4000-8000-000000000010" - for _, down := range []bool{false, true} { - h, pool := testHandlers(t) - admin := seedUser(t, h.pool, "admin", "pw", true) - h.catalogue = catalogue.New(pool, &musicbrainz.Client{BaseURL: fakeEditions(t, group, down).URL, - Pace: &musicbrainz.Pacer{}, Retries: -1}, h.logger) - w := seedWant(t, h, group, "a0000000-0000-4000-8000-000000000001", "Camel", "Moonmadness", "1976") - - code, out := wantTracks(t, h, admin, uuidToString(w.ID)) - if down { - if code != http.StatusServiceUnavailable { - t.Errorf("MusicBrainz down: status %d, want 503", code) - } - continue - } - if code != http.StatusOK || out.Source != "musicbrainz" || len(out.Tracks) != 2 { - t.Fatalf("status %d, %+v: want the two-track original", code, out) - } - if out.Edition != "1976 · 12\" Vinyl · GB" { - t.Errorf("edition = %q", out.Edition) - } - if tr := out.Tracks[1]; tr.Disc != 1 || tr.Number != 2 || tr.Title != "Song Within a Song" || tr.LengthMs != 433000 { - t.Errorf("track 2 = %+v", tr) - } - } -} - -// A held album shows the library's own tracks, a lost file marked. -func TestWantTracks_HeldAlbumShowsTheLibrarysTracks(t *testing.T) { - h, _ := testHandlers(t) - admin := seedUser(t, h.pool, "admin", "pw", true) - ctx := context.Background() - q := dbq.New(h.pool) - mbid := "a0000000-0000-4000-8000-000000000001" - artist, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Camel", SortName: "Camel", Mbid: &mbid}) - if err != nil { - t.Fatal(err) - } - album, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Moonmadness", SortTitle: "Moonmadness", ArtistID: artist.ID}) - if err != nil { - t.Fatal(err) - } - for i, title := range []string{"Aristillus", "Song Within a Song"} { - num := int32(i + 1) - if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ - Title: title, AlbumID: album.ID, ArtistID: artist.ID, TrackNumber: &num, DurationMs: 120000, - FilePath: fmt.Sprintf("/music/camel/%02d.flac", i+1), FileSize: 1, FileFormat: "flac", - }); err != nil { - t.Fatal(err) - } - } - if _, err := h.pool.Exec(ctx, `UPDATE tracks SET missing_since = now() WHERE title = 'Aristillus'`); err != nil { - t.Fatal(err) - } - w := seedWant(t, h, "g0000000-0000-4000-8000-000000000011", mbid, "Camel", "Moonmadness", "1976") - if err := q.FulfillWant(ctx, dbq.FulfillWantParams{ID: w.ID, AlbumID: album.ID}); err != nil { - t.Fatal(err) - } - - code, out := wantTracks(t, h, admin, uuidToString(w.ID)) - if code != http.StatusOK || out.Source != "library" || len(out.Tracks) != 2 { - t.Fatalf("status %d, %+v", code, out) - } - if !out.Tracks[0].Missing || out.Tracks[1].Missing || out.Tracks[1].Number != 2 { - t.Errorf("tracks = %+v: the first is missing, the second on disk", out.Tracks) - } -} - -// The Unmatched section: the albums matching could not place, a -// local band's album left off and put back, and a link that names no -// MusicBrainz album refused before MusicBrainz is asked. -func TestUnmatched_ListLeaveOffPutBack(t *testing.T) { - h, _ := testHandlers(t) - admin := seedUser(t, h.pool, "admin", "pw", true) - ctx := context.Background() - q := dbq.New(h.pool) - artist, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Annadale", SortName: "Annadale"}) - if err != nil { - t.Fatal(err) - } - album, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Dial Tone", SortTitle: "Dial Tone", ArtistID: artist.ID}) - if err != nil { - t.Fatal(err) - } - if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ - Title: "Intro", AlbumID: album.ID, ArtistID: artist.ID, DurationMs: 120000, - FilePath: "/music/annadale/01.flac", FileSize: 1, FileFormat: "flac", - }); err != nil { - t.Fatal(err) - } - if err := q.RecordAlbumMatch(ctx, dbq.RecordAlbumMatchParams{ - AlbumID: album.ID, Outcome: "unmatched", Detail: "MusicBrainz has no album by that name", Matcher: 1}); err != nil { - t.Fatal(err) - } - id := uuidToString(album.ID) - - list := func() []unmatchedAlbumView { - t.Helper() - w := doWantedReq(t, h, "/api/admin/wanted/unmatched", admin) - if w.Code != http.StatusOK { - t.Fatalf("status = %d; body = %s", w.Code, w.Body.String()) - } - var out struct { - Items []unmatchedAlbumView `json:"items"` - } - if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil { - t.Fatal(err) - } - return out.Items - } - if got := list(); len(got) != 1 || got[0].AlbumID != id || got[0].Artist != "Annadale" || got[0].Tracks != 1 || - got[0].LeftOff || got[0].Detail == "" || got[0].DecidedAt == "" { - t.Fatalf("unmatched = %+v", got) - } - - for _, c := range []struct { - path, body string - code int - }{ - {"/api/admin/wanted/unmatched/" + id + "/match", `{"ref":"Dial Tone"}`, http.StatusBadRequest}, - {"/api/admin/wanted/unmatched/" + id + "/match", `nope`, http.StatusBadRequest}, - {"/api/admin/wanted/unmatched/not-a-uuid/leave-off", "", http.StatusBadRequest}, - {"/api/admin/wanted/unmatched/" + id + "/put-back", "", http.StatusNotFound}, - {"/api/admin/wanted/unmatched/" + id + "/leave-off", "", http.StatusNoContent}, - {"/api/admin/wanted/unmatched/" + id + "/leave-off", "", http.StatusNotFound}, - } { - if w := doWanted(t, h, http.MethodPost, c.path, c.body, admin); w.Code != c.code { - t.Errorf("POST %s %s = %d, want %d; body = %s", c.path, c.body, w.Code, c.code, w.Body.String()) - } - } - if got := list(); len(got) != 1 || !got[0].LeftOff { - t.Fatalf("left off = %+v", got) - } - if w := doWanted(t, h, http.MethodPost, "/api/admin/wanted/unmatched/"+id+"/put-back", "", admin); w.Code != http.StatusNoContent { - t.Fatalf("put back = %d; body = %s", w.Code, w.Body.String()) - } - if got := list(); len(got) != 1 || got[0].LeftOff { - t.Errorf("put back = %+v", got) - } - - user := seedUser(t, h.pool, "listener", "pw", false) - if w := doWantedReq(t, h, "/api/admin/wanted/unmatched", user); w.Code != http.StatusForbidden { - t.Errorf("a listener reads the unmatched section: %d", w.Code) - } -} diff --git a/internal/api/api.go b/internal/api/api.go index 688fa1a2..3160e62a 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -13,35 +13,28 @@ import ( "github.com/go-chi/chi/v5" "github.com/jackc/pgx/v5/pgxpool" - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" "git.fabledsword.com/bvandeusen/minstrel/internal/auth" "git.fabledsword.com/bvandeusen/minstrel/internal/backup" - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" "git.fabledsword.com/bvandeusen/minstrel/internal/config" "git.fabledsword.com/bvandeusen/minstrel/internal/coverart" "git.fabledsword.com/bvandeusen/minstrel/internal/eventbus" - "git.fabledsword.com/bvandeusen/minstrel/internal/importer" "git.fabledsword.com/bvandeusen/minstrel/internal/library" - "git.fabledsword.com/bvandeusen/minstrel/internal/lifecycle" "git.fabledsword.com/bvandeusen/minstrel/internal/mailer" "git.fabledsword.com/bvandeusen/minstrel/internal/netsettings" "git.fabledsword.com/bvandeusen/minstrel/internal/notifications" "git.fabledsword.com/bvandeusen/minstrel/internal/playevents" "git.fabledsword.com/bvandeusen/minstrel/internal/playlists" "git.fabledsword.com/bvandeusen/minstrel/internal/quarantine" - "git.fabledsword.com/bvandeusen/minstrel/internal/reacquisition" "git.fabledsword.com/bvandeusen/minstrel/internal/recommendation" "git.fabledsword.com/bvandeusen/minstrel/internal/recsettings" - "git.fabledsword.com/bvandeusen/minstrel/internal/requests" "git.fabledsword.com/bvandeusen/minstrel/internal/tags" "git.fabledsword.com/bvandeusen/minstrel/internal/tracks" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" ) // Mount attaches /api/* handlers to r. Public endpoints (login) are outside // RequireUser; everything else is gated by the middleware. The events writer // is shared with the Subsonic mount so /rest/scrobble feeds the same store. -func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playevents.Writer, recCfg config.RecommendationConfig, recSettings *recsettings.Service, requestSvc *requests.Service, quarantineSvc *quarantine.Service, tracksSvc *tracks.Service, playlistsSvc *playlists.Service, coverEnricher *coverart.Enricher, coverSettings *coverart.SettingsService, tagSettings *tags.SettingsService, scanner *library.Scanner, scanCfg library.RunScanConfig, dataDir string, sender mailer.Sender, bus *eventbus.Bus, playlistScheduler *playlists.Scheduler, streamSecret []byte, netSettings *netsettings.Service, reacqSettings *reacquisition.SettingsService, fpSettings *library.FingerprintSettingsService, loudSettings *library.LoudnessSettingsService, acoustIDLookup *library.AcoustIDLookupWorker, backups *backup.Service, restart func()) { +func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playevents.Writer, recCfg config.RecommendationConfig, recSettings *recsettings.Service, quarantineSvc *quarantine.Service, tracksSvc *tracks.Service, playlistsSvc *playlists.Service, coverEnricher *coverart.Enricher, coverSettings *coverart.SettingsService, tagSettings *tags.SettingsService, scanner *library.Scanner, scanCfg library.RunScanConfig, dataDir string, sender mailer.Sender, bus *eventbus.Bus, playlistScheduler *playlists.Scheduler, streamSecret []byte, netSettings *netsettings.Service, fpSettings *library.FingerprintSettingsService, loudSettings *library.LoudnessSettingsService, acoustIDLookup *library.AcoustIDLookupWorker, backups *backup.Service, restart func()) { rng := rand.New(rand.NewSource(rand.Int63())) setupToken, err := auth.NewSetupToken() if err != nil { @@ -50,12 +43,10 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev panic("api: mint setup token: " + err.Error()) } logSetupTokenIfNeeded(pool, logger, setupToken) - cat := catalogue.New(pool, nil, logger) h := &handlers{ pool: pool, logger: logger, events: events, recCfg: recCfg, recSettings: recSettings, rng: rng.Float64, - requests: requestSvc, quarantine: quarantineSvc, tracks: tracksSvc, playlists: playlistsSvc, @@ -71,20 +62,11 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev playlistScheduler: playlistScheduler, streamSecret: streamSecret, netSettings: netSettings, - reacqSettings: reacqSettings, fingerprintSettings: fpSettings, loudnessSettings: loudSettings, acoustIDLookup: acoustIDLookup, backups: backups, restart: restart, - acquisition: acquire.NewStore(pool), - catalogue: cat, - wanted: wanted.New(pool, cat, logger), - searcher: wanted.NewSearcher(pool, cat, logger), - importer: importer.New(pool, scanner, cat, nil, scannerRoots(scanner), logger), - lifecycle: lifecycle.New(pool, logger), - catalogueSearches: newSearchCache(), - catalogueArt: catalogue.NewArt(dataDir, artSources(coverSettings)), leveled: newLeveledRenderer(dataDir, loudSettings, logger), librarySize: recommendation.NewLibrarySize(nil), loginGuard: auth.NewLoginGuard(), @@ -170,12 +152,6 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev // it can accept either a session or a signed token. authed.Get("/search", h.handleSearch) authed.Get("/radio", h.handleRadio) - authed.Get("/discover/suggestions", h.handleListSuggestions) - // Snooze = "not right now", time-boxed and self-expiring. - // Not a dislike — see the migration for why. - authed.Post("/discover/suggestions/{mbid}/snooze", h.handleSnoozeSuggestion) - authed.Delete("/discover/suggestions/{mbid}/snooze", h.handleUnsnoozeSuggestion) - authed.Get("/discover/snoozes", h.handleListSuggestionSnoozes) authed.Get("/home", h.handleGetHome) authed.Get("/home/index", h.handleGetHomeIndex) authed.Post("/events", h.handleEvents) @@ -194,17 +170,6 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev authed.Get("/likes/artists", h.handleListLikedArtists) authed.Get("/likes/ids", h.handleGetLikedIDs) - // The release catalogue: request search asks - // MusicBrainz, and art for what isn't held comes through - // Minstrel from the enabled cover-art sources. - authed.Get("/catalogue/search", h.handleCatalogueSearch) - authed.Get("/catalogue/art/{kind}/{mbid}", h.handleCatalogueArt) - - authed.Post("/requests", h.handleCreateRequest) - authed.Get("/requests", h.handleListRequests) - authed.Get("/requests/{id}", h.handleGetRequest) - authed.Delete("/requests/{id}", h.handleCancelRequest) - authed.Post("/quarantine", h.handleFlag) authed.Delete("/quarantine/{track_id}", h.handleUnflag) authed.Get("/quarantine/mine", h.handleListMyQuarantine) @@ -227,57 +192,10 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev authed.Route("/admin", func(admin chi.Router) { admin.Use(auth.RequireAdmin()) - admin.Get("/acquisition/services", h.handleListAcquisitionServices) - admin.Post("/acquisition/services", h.handleCreateAcquisitionService) - admin.Put("/acquisition/services/{code}", h.handleUpdateAcquisitionService) - admin.Delete("/acquisition/services/{code}", h.handleDeleteAcquisitionService) - admin.Post("/acquisition/services/{code}/test", h.handleTestAcquisitionService) - admin.Put("/acquisition/services/{code}/api-key", h.handleSetAcquisitionSecret("api_key")) - admin.Delete("/acquisition/services/{code}/api-key", h.handleSetAcquisitionSecret("api_key")) - admin.Put("/acquisition/services/{code}/password", h.handleSetAcquisitionSecret("password")) - admin.Delete("/acquisition/services/{code}/password", h.handleSetAcquisitionSecret("password")) // The queues that wait on an admin, for the rail and the // Overview. admin.Get("/summary", h.handleGetAdminSummary) admin.Get("/status", h.handleGetAdminStatus) - admin.Get("/requests", h.handleListAdminRequests) - admin.Post("/requests/{id}/approve", h.handleApproveRequest) - admin.Post("/requests/{id}/reject", h.handleRejectRequest) - - // The wanted list: what Minstrel is looking - // for, the artists it follows, and the release-type - // profiles that decide which of their releases count. - admin.Get("/wanted", h.handleListWants) - admin.Get("/wanted/artists", h.handleListWantArtists) - admin.Get("/wanted/unmatched", h.handleListUnmatched) - admin.Post("/wanted/unmatched/{id}/match", h.handleMatchUnmatched) - admin.Post("/wanted/unmatched/{id}/leave-off", h.handleLeaveUnmatchedOff) - admin.Post("/wanted/unmatched/{id}/put-back", h.handlePutUnmatchedBack) - admin.Delete("/wanted/{id}", h.handleRemoveWant) - // Searching and grabbing. - admin.Get("/acquisition/settings", h.handleGetSearchSettings) - admin.Put("/acquisition/settings", h.handlePutSearchSettings) - admin.Get("/wanted/{id}/candidates", h.handleListCandidates) - admin.Get("/wanted/{id}/tracks", h.handleWantTracks) - admin.Post("/wanted/{id}/search", h.handleSearchWant) - admin.Post("/wanted/{id}/grab", h.handleGrabCandidate) - admin.Post("/wanted/{id}/block", h.handleBlockCandidate) - admin.Delete("/release-blocks/{hash}", h.handleUnblockRelease) - admin.Get("/release-blocks", h.handleListReleaseBlocks) - admin.Get("/downloads", h.handleListDownloads) - admin.Post("/downloads/{id}/import", h.handleImportAnyway) - admin.Post("/downloads/{id}/retry", h.handleRetryImport) - admin.Post("/downloads/{id}/refuse", h.handleRefuseDownload) - admin.Post("/downloads/{id}/abandon", h.handleAbandonDownload) - admin.Get("/follows", h.handleListFollows) - admin.Get("/follows/{mbid}", h.handleGetFollow) - admin.Put("/follows/{mbid}", h.handlePutFollow) - admin.Delete("/follows/{mbid}", h.handleDeleteFollow) - admin.Get("/release-profiles", h.handleListReleaseProfiles) - admin.Post("/release-profiles", h.handleCreateReleaseProfile) - admin.Put("/release-profiles/{id}", h.handleUpdateReleaseProfile) - admin.Delete("/release-profiles/{id}", h.handleDeleteReleaseProfile) - admin.Get("/quarantine", h.handleListAdminQuarantine) admin.Post("/quarantine/{track_id}/resolve", h.handleResolveQuarantine) @@ -294,12 +212,6 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev admin.Get("/network-settings", h.handleGetNetworkSettings) admin.Put("/network-settings", h.handleUpdateNetworkSettings) - // Policy for wanting back an album whose files went missing - // Sits beside the missing-files list it governs, because an - // admin meets it on that surface. - admin.Get("/library/reacquisition", h.handleGetReacquisitionSettings) - admin.Put("/library/reacquisition", h.handleUpdateReacquisitionSettings) - // Where the library lives. A changed folder moves its // tracks rather than re-adding them. admin.Get("/library/folders", h.handleGetLibraryFolders) @@ -345,7 +257,6 @@ func Mount(r chi.Router, pool *pgxpool.Pool, logger *slog.Logger, events *playev admin.Post("/users", h.handleAdminCreateUser) admin.Delete("/users/{id}", h.handleAdminDeleteUser) admin.Post("/users/{id}/reset-password", h.handleAdminResetPassword) - admin.Put("/users/{id}/auto-approve", h.handleAdminAutoApproveToggle) admin.Put("/users/{id}/debug-mode", h.handleAdminDebugModeToggle) // Device diagnostics timeline + device overview. @@ -408,7 +319,6 @@ type handlers struct { // Held here rather than counted per request: the count is a // full table scan, and library size only moves when a scan runs. librarySize *recommendation.LibrarySize - requests *requests.Service quarantine *quarantine.Service tracks *tracks.Service playlists *playlists.Service @@ -424,10 +334,6 @@ type handlers struct { // notifier records nothing, which is what most handler tests want. notifier *notifications.Notifier playlistScheduler *playlists.Scheduler - // reacqSettings is the DB-backed policy for auto re-acquisition of - // missing files — grace window, backoff, attempt caps. - // Cached in the service, so the admin card reads it without a query. - reacqSettings *reacquisition.SettingsService // fingerprintSettings is the fingerprinting policy, the same // instance the scanner and the fingerprint workers read, so a save from the // admin card reaches them without a restart. Nil serves the defaults. @@ -443,26 +349,6 @@ type handlers struct { // restart stops the server and starts it again in place, so a restored // backup's settings are read at boot. Nil in tests. restart func() - // acquisition is the configured indexers and download clients. - acquisition *acquire.Store - // catalogue is the release catalogue read from MusicBrainz, and - // catalogueArt the images for what isn't held. - catalogue *catalogue.Service - catalogueArt *catalogue.Art - // wanted is the wanted list: follows, release-type profiles and wants. - // Its worker runs in main; the API reads and edits. - wanted *wanted.Service - // searcher searches for wants and grabs releases for them. - searcher *wanted.Searcher - // importer refuses a download at a person's word. The import - // loop itself runs in main; this one only acts on a request. - importer *importer.Importer - // lifecycle gives up on a download at a person's word; its loop - // runs in main. - lifecycle *lifecycle.Service - // catalogueSearches reuses recent MusicBrainz answers; nil asks every - // time, which is what handler tests get. - catalogueSearches *searchCache // leveled renders the leveled streams handed to Sonos/UPnP speakers. Nil // when its cache directory cannot be made: a level request then gets the // plain stream. @@ -502,24 +388,3 @@ func newLeveledRenderer(dataDir string, settings *library.LoudnessSettingsServic } return r } - -// artSources hands the art cache the cover-art settings, or nothing when -// there are none: a nil *SettingsService inside the interface would not -// compare equal to nil and would be called. -func artSources(s *coverart.SettingsService) catalogue.ArtSources { - if s == nil { - return nil - } - return s -} - -// scannerRoots reads the library folders, or none when the API runs without -// a scanner (tests). -func scannerRoots(s *library.Scanner) func() []string { - return func() []string { - if s == nil { - return nil - } - return s.Roots() - } -} diff --git a/internal/api/auth_test.go b/internal/api/auth_test.go index a09a1a0a..a0a45a4c 100644 --- a/internal/api/auth_test.go +++ b/internal/api/auth_test.go @@ -18,7 +18,6 @@ import ( "golang.org/x/crypto/bcrypt" "git.fabledsword.com/bvandeusen/minstrel/internal/auth" - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" "git.fabledsword.com/bvandeusen/minstrel/internal/config" "git.fabledsword.com/bvandeusen/minstrel/internal/db" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" @@ -30,9 +29,7 @@ import ( "git.fabledsword.com/bvandeusen/minstrel/internal/playlists" "git.fabledsword.com/bvandeusen/minstrel/internal/quarantine" "git.fabledsword.com/bvandeusen/minstrel/internal/recsettings" - "git.fabledsword.com/bvandeusen/minstrel/internal/requests" "git.fabledsword.com/bvandeusen/minstrel/internal/tracks" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" ) // testHandlers spins up a handlers instance against MINSTREL_TEST_DATABASE_URL. @@ -65,13 +62,11 @@ func testHandlers(t *testing.T) (*handlers, *pgxpool.Pool) { if err != nil { t.Fatalf("recsettings: %v", err) } - requestSvc := requests.NewService(pool) - cat := catalogue.New(pool, nil, logger) quarantineSvc := quarantine.NewService(pool, "") dataDir := t.TempDir() tracksSvc := tracks.NewService(pool, logger, dataDir) playlistsSvc := playlists.NewService(pool, logger, dataDir) - h := &handlers{pool: pool, logger: logger, events: w, recCfg: recCfg, recSettings: recSettings, rng: func() float64 { return 0.5 }, requests: requestSvc, catalogue: cat, wanted: wanted.New(pool, cat, logger), quarantine: quarantineSvc, tracks: tracksSvc, playlists: playlistsSvc, dataDir: dataDir, scanner: nil, scanCfg: library.RunScanConfig{}, mailer: &mailer.FakeSender{}, notifier: notifications.New(pool, nil, nil)} + h := &handlers{pool: pool, logger: logger, events: w, recCfg: recCfg, recSettings: recSettings, rng: func() float64 { return 0.5 }, quarantine: quarantineSvc, tracks: tracksSvc, playlists: playlistsSvc, dataDir: dataDir, scanner: nil, scanCfg: library.RunScanConfig{}, mailer: &mailer.FakeSender{}, notifier: notifications.New(pool, nil, nil)} return h, pool } @@ -223,6 +218,12 @@ func TestHandleLogin_MalformedBodyReturns400(t *testing.T) { func userCtxKeyForTest() any { return auth.UserCtxKeyForTest() } +// withUser injects a user into the request context the same way RequireUser +// does. +func withUser(req *http.Request, user dbq.User) *http.Request { + return req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user)) +} + func TestHandleLogout_DeletesSessionAndClearsCookie(t *testing.T) { h, pool := testHandlers(t) user := seedUser(t, pool, "alice", "hunter2", false) diff --git a/internal/api/catalogue.go b/internal/api/catalogue.go deleted file mode 100644 index 9b883600..00000000 --- a/internal/api/catalogue.go +++ /dev/null @@ -1,284 +0,0 @@ -package api - -import ( - "context" - "errors" - "net/http" - "net/url" - "slices" - "strings" - "sync" - "time" - - "github.com/go-chi/chi/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" -) - -// catalogueSearchLimit is how many results one search shows. -const catalogueSearchLimit = 25 - -// catalogueSearchResult is one row of GET /api/catalogue/search, read by the -// Discover page and request rows. artist_mbid and album_mbid are the parents a -// request needs: empty on an artist, the artist on an album, both on a track. -// An album is a MusicBrainz release group. -type catalogueSearchResult struct { - MBID string `json:"mbid"` - Name string `json:"name"` - SecondaryText string `json:"secondary_text"` - ImageURL string `json:"image_url"` - ArtistMBID string `json:"artist_mbid"` - AlbumMBID string `json:"album_mbid"` - InLibrary bool `json:"in_library"` - Requested bool `json:"requested"` -} - -// handleCatalogueSearch implements GET -// /api/catalogue/search?q=&kind=artist|album|track asking MusicBrainz directly. -// An unreachable MusicBrainz is a 503 the page shows as such, not an empty -// result. -func (h *handlers) handleCatalogueSearch(w http.ResponseWriter, r *http.Request) { - kind := strings.TrimSpace(r.URL.Query().Get("kind")) - switch kind { - case "artist", "album", "track": - default: - writeErr(w, apierror.BadRequest("bad_kind", "kind must be artist, album, or track")) - return - } - q := strings.TrimSpace(r.URL.Query().Get("q")) - if q == "" { - writeErr(w, apierror.BadRequest("missing_query", "q is required")) - return - } - - out, err := h.catalogueSearches.get(r.Context(), kind, q, h.searchCatalogue) - if err != nil { - switch { - case errors.Is(err, musicbrainz.ErrUnreachable), errors.Is(err, musicbrainz.ErrBadResponse): - writeErr(w, &apierror.Error{Status: 503, Code: "musicbrainz_unreachable", Message: "MusicBrainz can't be reached", Cause: err}) - case r.Context().Err() != nil: - return - default: - h.logger.Error("api: catalogue search", "kind", kind, "err", err) - writeErr(w, apierror.InternalMsg("search failed", err)) - } - return - } - - dbQ := dbq.New(h.pool) - for i := range out { - inLib, err := h.catalogueInLibrary(r.Context(), kind, out[i].MBID) - if err != nil { - writeErr(w, apierror.InternalMsg("library check failed", err)) - return - } - requested, err := dbQ.HasNonTerminalRequestForMBID(r.Context(), out[i].MBID) - if err != nil { - writeErr(w, apierror.InternalMsg("request check failed", err)) - return - } - out[i].InLibrary, out[i].Requested = inLib, requested - } - writeJSON(w, http.StatusOK, out) -} - -func (h *handlers) searchCatalogue(ctx context.Context, kind, q string) ([]catalogueSearchResult, error) { - mb := h.catalogue.MusicBrainz() - out := []catalogueSearchResult{} - switch kind { - case "artist": - artists, err := mb.SearchArtists(ctx, q, catalogueSearchLimit) - if err != nil { - return nil, err - } - for _, a := range artists { - out = append(out, catalogueSearchResult{ - MBID: a.MBID, Name: a.Name, SecondaryText: artistLine(a), - ImageURL: artistArtURL(a.MBID, a.Name), - }) - } - case "album": - groups, err := mb.SearchReleaseGroups(ctx, q, catalogueSearchLimit) - if err != nil { - return nil, err - } - for _, g := range groups { - out = append(out, catalogueSearchResult{ - MBID: g.MBID, Name: g.Title, SecondaryText: joinNonEmpty(g.ArtistCredit, g.Year(), groupType(g)), - ImageURL: groupArtURL(g), ArtistMBID: first(g.ArtistMBIDs), - }) - } - case "track": - recs, err := mb.SearchRecordings(ctx, q, catalogueSearchLimit) - if err != nil { - return nil, err - } - for _, rec := range recs { - res := catalogueSearchResult{ - MBID: rec.MBID, Name: rec.Title, - SecondaryText: joinNonEmpty(rec.ReleaseGroup.Title, rec.ArtistCredit), - ArtistMBID: first(rec.ArtistMBIDs), AlbumMBID: rec.ReleaseGroup.MBID, - } - if rec.ReleaseGroup.MBID != "" { - res.ImageURL = groupArtURL(rec.ReleaseGroup) - } - out = append(out, res) - } - } - return out, nil -} - -// catalogueSearchTTL is how long a search's MusicBrainz answer is reused. -// MusicBrainz allows a request a second, and Discover searches as the user -// types, so the same words are often asked again within moments. -const catalogueSearchTTL = 10 * time.Minute - -// catalogueSearchCacheMax bounds the cache; past it, the cache starts over. -const catalogueSearchCacheMax = 500 - -// searchCache keeps recent MusicBrainz answers by kind and query. A nil -// cache asks every time. Whether a result is held or requested is not -// cached: the handler works that out on every request. -type searchCache struct { - mu sync.Mutex - entries map[string]searchEntry - now func() time.Time -} - -type searchEntry struct { - at time.Time - results []catalogueSearchResult -} - -func newSearchCache() *searchCache { - return &searchCache{entries: map[string]searchEntry{}, now: time.Now} -} - -func (c *searchCache) get(ctx context.Context, kind, q string, - ask func(context.Context, string, string) ([]catalogueSearchResult, error), -) ([]catalogueSearchResult, error) { - if c == nil { - return ask(ctx, kind, q) - } - key := kind + "\x00" + strings.ToLower(q) - c.mu.Lock() - e, ok := c.entries[key] - c.mu.Unlock() - if ok && c.now().Sub(e.at) < catalogueSearchTTL { - return slices.Clone(e.results), nil - } - results, err := ask(ctx, kind, q) - if err != nil { - return nil, err - } - c.mu.Lock() - if len(c.entries) >= catalogueSearchCacheMax { - c.entries = map[string]searchEntry{} - } - c.entries[key] = searchEntry{at: c.now(), results: slices.Clone(results)} - c.mu.Unlock() - return results, nil -} - -// artistLine is what tells two artists of one name apart: MusicBrainz's own -// disambiguation when it has one, else the kind of artist and the country. -func artistLine(a musicbrainz.Artist) string { - if a.Disambiguation != "" { - return a.Disambiguation - } - return joinNonEmpty(a.Type, a.Country) -} - -// groupType names an album's kind the way people do: "Album", "EP", -// "Album · Live", "Single · Remix". -func groupType(g musicbrainz.ReleaseGroup) string { - return joinNonEmpty(append([]string{g.PrimaryType}, g.SecondaryTypes...)...) -} - -func joinNonEmpty(parts ...string) string { - kept := parts[:0:0] - for _, p := range parts { - if p != "" { - kept = append(kept, p) - } - } - return strings.Join(kept, " · ") -} - -func first(s []string) string { - if len(s) == 0 { - return "" - } - return s[0] -} - -// artistArtURL and groupArtURL point at Minstrel's own art endpoints. The -// names ride along for the sources that look art up by name. -func artistArtURL(mbid, name string) string { - if mbid == "" { - return "" - } - return "/api/catalogue/art/artist/" + mbid + "?" + url.Values{"name": {name}}.Encode() -} - -func groupArtURL(g musicbrainz.ReleaseGroup) string { - return "/api/catalogue/art/release-group/" + g.MBID + "?" + - url.Values{"artist": {g.ArtistCredit}, "title": {g.Title}}.Encode() -} - -// catalogueInLibrary reports whether a result is already held: an artist by -// its MBID, an album by its release group (or a release id that happens to be -// the same), a track by its recording. -func (h *handlers) catalogueInLibrary(ctx context.Context, kind, mbid string) (bool, error) { - var query string - switch kind { - case "artist": - query = `SELECT EXISTS(SELECT 1 FROM artists WHERE mbid = $1)` - case "album": - query = `SELECT EXISTS(SELECT 1 FROM albums WHERE release_group_mbid = $1 OR mbid = $1)` - case "track": - query = `SELECT EXISTS(SELECT 1 FROM tracks WHERE mbid = $1)` - default: - return false, nil - } - var exists bool - err := h.pool.QueryRow(ctx, query, mbid).Scan(&exists) - return exists, err -} - -// handleCatalogueArt implements GET /api/catalogue/art/{kind}/{mbid} for an -// artist or a release group Minstrel doesn't hold. The image comes from the -// enabled cover-art sources and is kept on disk, so the browser -// never fetches a third-party host. A 404 shows the placeholder. -func (h *handlers) handleCatalogueArt(w http.ResponseWriter, r *http.Request) { - kind, mbid := chi.URLParam(r, "kind"), chi.URLParam(r, "mbid") - if !catalogue.ValidMBID(mbid) { - writeErr(w, apierror.NotFound("art")) - return - } - var ( - body []byte - err error - ) - switch kind { - case "artist": - body, err = h.catalogueArt.Artist(r.Context(), mbid, r.URL.Query().Get("name")) - case "release-group": - body, err = h.catalogueArt.ReleaseGroup(r.Context(), mbid, r.URL.Query().Get("artist"), r.URL.Query().Get("title")) - default: - writeErr(w, apierror.NotFound("art")) - return - } - if err != nil { - if r.Context().Err() == nil { - writeErr(w, apierror.NotFound("art")) - } - return - } - w.Header().Set("Content-Type", http.DetectContentType(body)) - w.Header().Set("Cache-Control", "private, max-age=86400") - _, _ = w.Write(body) -} diff --git a/internal/api/catalogue_test.go b/internal/api/catalogue_test.go deleted file mode 100644 index 0dfdf7f6..00000000 --- a/internal/api/catalogue_test.go +++ /dev/null @@ -1,254 +0,0 @@ -package api - -import ( - "context" - "encoding/json" - "net/http" - "net/http/httptest" - "net/url" - "testing" - "time" - - "github.com/go-chi/chi/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/coverart" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" - "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" -) - -const ( - searchArtistMBID = "a74b1b7f-71a5-4011-9441-d0b5e4122711" - searchGroupMBID = "b1392450-e666-3926-a536-22c65f834433" - searchRecMBID = "c1a0e1b2-0000-4000-8000-000000000001" -) - -// fakeMusicBrainz answers the three searches with one Radiohead result each, -// or 503 when down is set. -func fakeMusicBrainz(t *testing.T, down bool) *httptest.Server { - t.Helper() - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if down { - w.WriteHeader(http.StatusServiceUnavailable) - return - } - credit := `[{"name": "Radiohead", "artist": {"id": "` + searchArtistMBID + `"}}]` - group := `{"id": "` + searchGroupMBID + `", "title": "OK Computer", "primary-type": "Album", - "first-release-date": "1997-05-21", "artist-credit": ` + credit + `}` - switch r.URL.Path { - case "/artist": - _, _ = w.Write([]byte(`{"artists": [{"id": "` + searchArtistMBID + `", "name": "Radiohead", "type": "Group", "country": "GB"}]}`)) - case "/release-group": - _, _ = w.Write([]byte(`{"release-groups": [` + group + `]}`)) - case "/recording": - _, _ = w.Write([]byte(`{"recordings": [{"id": "` + searchRecMBID + `", "title": "Karma Police", - "artist-credit": ` + credit + `, - "releases": [{"id": "r1", "status": "Official", "release-group": ` + group + `}]}]}`)) - default: - w.WriteHeader(http.StatusNotFound) - } - })) - t.Cleanup(srv.Close) - return srv -} - -func catalogueHandlers(t *testing.T, down bool) *handlers { - t.Helper() - h, pool := testHandlers(t) - resetRequests(t, h) - mb := &musicbrainz.Client{BaseURL: fakeMusicBrainz(t, down).URL, Pace: &musicbrainz.Pacer{}, Retries: -1} - h.catalogue = catalogue.New(pool, mb, h.logger) - return h -} - -func doCatalogueSearch(t *testing.T, h *handlers, q, kind string) *httptest.ResponseRecorder { - t.Helper() - req := httptest.NewRequest(http.MethodGet, "/api/catalogue/search?"+url.Values{"q": {q}, "kind": {kind}}.Encode(), nil) - w := httptest.NewRecorder() - h.handleCatalogueSearch(w, req) - return w -} - -func decodeResults(t *testing.T, w *httptest.ResponseRecorder) []catalogueSearchResult { - t.Helper() - if w.Code != http.StatusOK { - t.Fatalf("status = %d, body = %s", w.Code, w.Body.String()) - } - var out []catalogueSearchResult - if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - return out -} - -func errorCode(t *testing.T, w *httptest.ResponseRecorder) string { - t.Helper() - var resp struct { - Error struct { - Code string `json:"code"` - } `json:"error"` - } - if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - return resp.Error.Code -} - -func TestCatalogueSearch_RefusesABadKindOrNoQuery(t *testing.T) { - h := catalogueHandlers(t, false) - if w := doCatalogueSearch(t, h, "x", "playlist"); w.Code != http.StatusBadRequest || errorCode(t, w) != "bad_kind" { - t.Errorf("bad kind: %d %s", w.Code, w.Body.String()) - } - if w := doCatalogueSearch(t, h, " ", "artist"); w.Code != http.StatusBadRequest || errorCode(t, w) != "missing_query" { - t.Errorf("no query: %d %s", w.Code, w.Body.String()) - } -} - -func TestCatalogueSearch_EachKindAsksMusicBrainz(t *testing.T) { - h := catalogueHandlers(t, false) - - artists := decodeResults(t, doCatalogueSearch(t, h, "radiohead", "artist")) - if len(artists) != 1 || artists[0].MBID != searchArtistMBID || artists[0].SecondaryText != "Group · GB" { - t.Fatalf("artists = %+v", artists) - } - if artists[0].ImageURL != "/api/catalogue/art/artist/"+searchArtistMBID+"?name=Radiohead" { - t.Errorf("artist art = %q: it must be Minstrel's own endpoint", artists[0].ImageURL) - } - - albums := decodeResults(t, doCatalogueSearch(t, h, "ok computer", "album")) - if len(albums) != 1 || albums[0].MBID != searchGroupMBID || albums[0].ArtistMBID != searchArtistMBID || - albums[0].SecondaryText != "Radiohead · 1997 · Album" { - t.Fatalf("albums = %+v", albums) - } - - tracks := decodeResults(t, doCatalogueSearch(t, h, "karma police", "track")) - if len(tracks) != 1 || tracks[0].AlbumMBID != searchGroupMBID || tracks[0].ArtistMBID != searchArtistMBID || - tracks[0].SecondaryText != "OK Computer · Radiohead" { - t.Fatalf("tracks = %+v", tracks) - } -} - -func TestCatalogueSearch_MarksWhatIsHeldAndWhatIsRequested(t *testing.T) { - h := catalogueHandlers(t, false) - ctx := context.Background() - q := dbq.New(h.pool) - - artistMBID := searchArtistMBID - artist, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: dbtest.TestUserPrefix + "Radiohead", SortName: "Radiohead", Mbid: &artistMBID}) - if err != nil { - t.Fatal(err) - } - // The album is held under a release id; its release group is what matches. - if _, err := h.pool.Exec(ctx, - `INSERT INTO albums (title, sort_title, artist_id, mbid, release_group_mbid) VALUES ('OK Computer', 'OK Computer', $1, 'some-release-id', $2)`, - artist.ID, searchGroupMBID); err != nil { - t.Fatal(err) - } - if got := decodeResults(t, doCatalogueSearch(t, h, "radiohead", "artist")); !got[0].InLibrary { - t.Error("the artist is held") - } - if got := decodeResults(t, doCatalogueSearch(t, h, "ok computer", "album")); !got[0].InLibrary { - t.Error("the album is held, matched by release group") - } - - user := seedUser(t, h.pool, "requester", "pw", false) - track := searchRecMBID - if _, err := q.CreateRequest(ctx, dbq.CreateRequestParams{ - UserID: user.ID, Kind: dbq.RequestKindTrack, ArtistMbid: searchArtistMBID, - TrackMbid: &track, ArtistName: "Radiohead", - }); err != nil { - t.Fatal(err) - } - got := decodeResults(t, doCatalogueSearch(t, h, "karma police", "track")) - if got[0].InLibrary || !got[0].Requested { - t.Errorf("track = %+v, want requested and not held", got[0]) - } -} - -func TestCatalogueSearch_UnreachableMusicBrainzIs503(t *testing.T) { - h := catalogueHandlers(t, true) - w := doCatalogueSearch(t, h, "radiohead", "artist") - if w.Code != http.StatusServiceUnavailable || errorCode(t, w) != "musicbrainz_unreachable" { - t.Fatalf("status %d, body %s", w.Code, w.Body.String()) - } -} - -// artSource is an artist-art source with one image. -type artSource struct{ apiTestAlbumProvider } - -func (artSource) FetchArtistArt(_ context.Context, _ coverart.ArtistRef) ([]byte, []byte, error) { - return []byte("\x89PNG\r\n\x1a\nrest"), nil, nil -} - -type fakeArtSources struct{ artists []coverart.ArtistArtProvider } - -func (s fakeArtSources) EnabledArtistProviders() []coverart.ArtistArtProvider { return s.artists } -func (fakeArtSources) EnabledAlbumProviders() []coverart.AlbumCoverProvider { return nil } - -func TestCatalogueArt_ServesTheImageOrAPlaceholder404(t *testing.T) { - h := &handlers{catalogueArt: catalogue.NewArt(t.TempDir(), fakeArtSources{artists: []coverart.ArtistArtProvider{&artSource{}}})} - r := chi.NewRouter() - r.Get("/api/catalogue/art/{kind}/{mbid}", h.handleCatalogueArt) - - get := func(path string) *httptest.ResponseRecorder { - w := httptest.NewRecorder() - r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil)) - return w - } - if w := get("/api/catalogue/art/artist/" + searchArtistMBID + "?name=Radiohead"); w.Code != http.StatusOK || w.Header().Get("Content-Type") != "image/png" { - t.Errorf("artist: %d %q", w.Code, w.Header().Get("Content-Type")) - } - // No album source is enabled: a 404, which the page shows as its - // placeholder. - if w := get("/api/catalogue/art/release-group/" + searchGroupMBID); w.Code != http.StatusNotFound { - t.Errorf("release group with no source: %d", w.Code) - } - for _, bad := range []string{"/api/catalogue/art/artist/not-an-id", "/api/catalogue/art/label/" + searchArtistMBID} { - if w := get(bad); w.Code != http.StatusNotFound { - t.Errorf("%s: %d", bad, w.Code) - } - } -} - -func TestSuggestionArt_PointsAtMinstrelsOwnEndpoint(t *testing.T) { - views := []suggestionView{{MBID: searchArtistMBID, Name: "Sigur Rós"}, {Name: "No MBID"}} - resolveSuggestionArt(views) - if views[0].ImageURL != "/api/catalogue/art/artist/"+searchArtistMBID+"?name=Sigur+R%C3%B3s" { - t.Errorf("image_url = %q", views[0].ImageURL) - } - if views[1].ImageURL != "" { - t.Errorf("no MBID, no image: %q", views[1].ImageURL) - } -} - -func TestSearchCache_ReusesAnAnswerButNotTheFlags(t *testing.T) { - c := newSearchCache() - now := time.Now() - c.now = func() time.Time { return now } - asks := 0 - ask := func(context.Context, string, string) ([]catalogueSearchResult, error) { - asks++ - return []catalogueSearchResult{{MBID: "m"}}, nil - } - first, _ := c.get(context.Background(), "artist", "Radiohead", ask) - first[0].InLibrary = true // the handler sets flags on what it is given - again, _ := c.get(context.Background(), "artist", "radiohead", ask) - if asks != 1 { - t.Errorf("asks = %d: the same words in another case are the same search", asks) - } - if again[0].InLibrary { - t.Error("a flag set on one answer leaked into the cache") - } - if _, _ = c.get(context.Background(), "album", "radiohead", ask); asks != 2 { - t.Error("another kind is another search") - } - now = now.Add(catalogueSearchTTL + time.Second) - if _, _ = c.get(context.Background(), "artist", "radiohead", ask); asks != 3 { - t.Error("an old answer is asked again") - } - var none *searchCache - if _, _ = none.get(context.Background(), "artist", "x", ask); asks != 4 { - t.Error("a nil cache asks every time") - } -} diff --git a/internal/api/convert.go b/internal/api/convert.go index a48ba868..a4ea7c34 100644 --- a/internal/api/convert.go +++ b/internal/api/convert.go @@ -194,3 +194,11 @@ func nonNilStrings(in []string) []string { } return in } + +// deref is a nullable column's text, or "". +func deref(s *string) string { + if s == nil { + return "" + } + return *s +} diff --git a/internal/api/diagnostics.go b/internal/api/diagnostics.go index 83ec7cb2..dffc6feb 100644 --- a/internal/api/diagnostics.go +++ b/internal/api/diagnostics.go @@ -280,13 +280,12 @@ func (h *handlers) handleAdminDebugModeToggle(w http.ResponseWriter, r *http.Req h.logger.Info("admin: debug mode toggled", "target_user", uuidToString(targetID), "enabled", req.Enabled) writeJSON(w, http.StatusOK, adminUserView{ - ID: uuidToString(updated.ID), - Username: updated.Username, - DisplayName: updated.DisplayName, - IsAdmin: updated.IsAdmin, - AutoApproveRequests: updated.AutoApproveRequests, - DebugModeEnabled: updated.DebugModeEnabled, - CreatedAt: updated.CreatedAt.Time.UTC().Format(time.RFC3339), + ID: uuidToString(updated.ID), + Username: updated.Username, + DisplayName: updated.DisplayName, + IsAdmin: updated.IsAdmin, + DebugModeEnabled: updated.DebugModeEnabled, + CreatedAt: updated.CreatedAt.Time.UTC().Format(time.RFC3339), }) } diff --git a/internal/api/events_publish.go b/internal/api/events_publish.go index 40eadb46..4a3d17d4 100644 --- a/internal/api/events_publish.go +++ b/internal/api/events_publish.go @@ -6,7 +6,6 @@ package api import ( - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/eventbus" "github.com/jackc/pgx/v5/pgtype" ) @@ -102,23 +101,3 @@ func (h *handlers) publishSystemRebuilt(userID pgtype.UUID) { Data: map[string]any{}, }) } - -// publishRequestStatusChanged broadcasts a request status flip to -// the request's original requester so their /requests page reflects the -// new state without manual refresh. Admin actors (approve / reject) still -// route to the requester's user_id, not the admin's — the original user -// is who needs to see the update. -func (h *handlers) publishRequestStatusChanged(row dbq.Request) { - if h.eventbus == nil { - return - } - h.eventbus.Publish(eventbus.Event{ - Kind: "request.status_changed", - UserID: uuidToString(row.UserID), - Data: map[string]any{ - "request_id": uuidToString(row.ID), - "status": string(row.Status), - "kind": string(row.Kind), - }, - }) -} diff --git a/internal/api/events_stream.go b/internal/api/events_stream.go index f9fccf8f..c9e8c566 100644 --- a/internal/api/events_stream.go +++ b/internal/api/events_stream.go @@ -1,7 +1,7 @@ // Live event stream — Server-Sent Events at GET /api/events/stream. // // The web app and the Android app subscribe to be notified of state changes -// elsewhere in the system (likes, request status flips, quarantine +// elsewhere in the system (likes, quarantine // resolutions, scan progress, playlist mutations) so static screens // can invalidate cached data without explicit user action. // diff --git a/internal/api/library_test.go b/internal/api/library_test.go index 1733d3d0..d5d29756 100644 --- a/internal/api/library_test.go +++ b/internal/api/library_test.go @@ -468,7 +468,7 @@ func TestRoutesRegisteredInMount(t *testing.T) { r := chi.NewRouter() w := playevents.NewWriter(h.pool, slog.New(slog.NewTextHandler(io.Discard, nil)), 30*time.Minute, 0.5, 30000) - Mount(r, h.pool, h.logger, w, config.RecommendationConfig{RadioSize: 50, RadioSizeMax: 200, RecentlyPlayedHours: 1}, h.recSettings, h.requests, h.quarantine, h.tracks, h.playlists, h.coverart, h.coverSettings, h.tagSettings, h.scanner, h.scanCfg, h.dataDir, nil, eventbus.New(), nil, nil, h.netSettings, nil, nil, nil, nil, backup.NewService(h.pool, h.logger, "test"), nil) + Mount(r, h.pool, h.logger, w, config.RecommendationConfig{RadioSize: 50, RadioSizeMax: 200, RecentlyPlayedHours: 1}, h.recSettings, h.quarantine, h.tracks, h.playlists, h.coverart, h.coverSettings, h.tagSettings, h.scanner, h.scanCfg, h.dataDir, nil, eventbus.New(), nil, nil, h.netSettings, nil, nil, nil, backup.NewService(h.pool, h.logger, "test"), nil) paths := []string{ "/api/artists", @@ -486,7 +486,6 @@ func TestRoutesRegisteredInMount(t *testing.T) { // admin group, so reaching the auth check is what proves they are // wired. "/api/admin/library/missing", - "/api/admin/library/reacquisition", "/api/admin/library/fingerprints", "/api/admin/library/fingerprint-settings", "/api/admin/library/duplicates", diff --git a/internal/api/notify_producers.go b/internal/api/notify_producers.go index 3cd2e5fc..53fa083c 100644 --- a/internal/api/notify_producers.go +++ b/internal/api/notify_producers.go @@ -8,7 +8,6 @@ import ( "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/library" "git.fabledsword.com/bvandeusen/minstrel/internal/notifications" - "git.fabledsword.com/bvandeusen/minstrel/internal/requests" ) // Helpers for the handlers that produce notifications. Every call is @@ -23,25 +22,6 @@ func userLabel(u dbq.User) string { return u.Username } -func requestPayload(row dbq.Request) notifications.Payload { - return notifications.Payload{ - RequestID: uuidToString(row.ID), - RequestKind: string(row.Kind), - Name: requests.DisplayName(row), - } -} - -// notifyRequestDecided tells the requester an admin approved or declined -// their request. An admin deciding their own request needs no notice. -func (h *handlers) notifyRequestDecided(ctx context.Context, kind notifications.Kind, admin dbq.User, row dbq.Request, reason string) { - if row.UserID == admin.ID { - return - } - p := requestPayload(row) - p.Reason = reason - h.notifier.NotifyLogged(ctx, kind, notifications.ToUser(row.UserID), p.Map()) -} - // quarantineReasonLabel is a flag reason as the flag popover words it, in // lower case to sit inside a sentence ("flagged X: bad rip"). func quarantineReasonLabel(reason string) string { diff --git a/internal/api/notify_producers_test.go b/internal/api/notify_producers_test.go index bee25be8..ab2c131f 100644 --- a/internal/api/notify_producers_test.go +++ b/internal/api/notify_producers_test.go @@ -53,99 +53,6 @@ func wantInbox(t *testing.T, who string, got []inboxItem, kinds ...notifications } } -func TestNotify_NewRequestReachesOtherAdminsOnce(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "np-alice", "pw", false) - admin := seedUser(t, pool, "np-admin", "pw", true) - selfAdmin := seedUser(t, pool, "np-self", "pw", true) - - rv := createArtistRequest(t, h, alice, "np-mbid-1", "Pending Band") - // The same request again dedups into the first and is not announced twice. - createArtistRequest(t, h, alice, "np-mbid-1", "Pending Band") - - got := inboxOf(t, pool, admin) - wantInbox(t, "admin", got, notifications.KindRequestPending) - if got[0].Payload.RequestID != uuidToString(rv.ID) || got[0].Payload.Name != "Pending Band" || got[0].Payload.Actor != alice.Username { - t.Errorf("pending payload = %+v", got[0].Payload) - } - wantInbox(t, "requester", inboxOf(t, pool, alice)) - - // An admin's own request goes to the other admins, never back to them. - createArtistRequest(t, h, selfAdmin, "np-mbid-2", "Own Band") - wantInbox(t, "self-admin", inboxOf(t, pool, selfAdmin), notifications.KindRequestPending) - if n := len(inboxOf(t, pool, admin)); n != 2 { - t.Errorf("admin inbox = %d, want 2 (alice's and the self-admin's)", n) - } -} - -func TestNotify_AutoApprovedRequestWaitsOnNobody(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, h.pool, "np-auto", "pw", false) - admin := seedUser(t, h.pool, "np-auto-admin", "pw", true) - if _, err := h.pool.Exec(context.Background(), - "UPDATE users SET auto_approve_requests = true WHERE id = $1", alice.ID); err != nil { - t.Fatalf("set auto_approve: %v", err) - } - alice, err := dbq.New(h.pool).GetUserByID(context.Background(), alice.ID) - if err != nil { - t.Fatalf("reload user: %v", err) - } - - rv := createArtistRequest(t, h, alice, "np-auto-mbid", "Auto Band") - if rv.Status != "approved" { - t.Fatalf("status = %q, want approved — the stub should accept the add", rv.Status) - } - wantInbox(t, "admin", inboxOf(t, h.pool, admin)) - wantInbox(t, "requester", inboxOf(t, h.pool, alice)) -} - -func TestNotify_DecisionsReachTheRequester(t *testing.T) { - h, _ := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, h.pool, "np-dec-alice", "pw", false) - admin := seedUser(t, h.pool, "np-dec-admin", "pw", true) - - approved := seedPendingArtistRequest(t, h, alice, "np-dec-1", "Yes Band") - rejected := seedPendingArtistRequest(t, h, alice, "np-dec-2", "No Band") - own := seedPendingArtistRequest(t, h, admin, "np-dec-3", "Own Band") - - for _, c := range []struct { - id string - verb string - body []byte - }{ - {uuidToString(approved.ID), "approve", nil}, - {uuidToString(rejected.ID), "reject", []byte(`{"notes":"Only a live bootleg exists"}`)}, - {uuidToString(own.ID), "approve", nil}, - } { - w := doAdminRequestReq(t, h, http.MethodPost, "/api/admin/requests/"+c.id+"/"+c.verb, c.body, admin) - if w.Code != http.StatusOK { - t.Fatalf("%s %s: status = %d; body = %s", c.verb, c.id, w.Code, w.Body.String()) - } - } - - got := inboxOf(t, h.pool, alice) - wantInbox(t, "requester", got, notifications.KindRequestRejected, notifications.KindRequestApproved) - if got[0].Payload.Name != "No Band" || got[0].Payload.Reason != "Only a live bootleg exists" { - t.Errorf("rejected payload = %+v", got[0].Payload) - } - if got[1].Payload.Name != "Yes Band" || got[1].Payload.Reason != "" { - t.Errorf("approved payload = %+v", got[1].Payload) - } - // The admin decided their own request: no notice. alice's two pending - // notices are all the admin holds. - for _, it := range inboxOf(t, h.pool, admin) { - if it.Kind != string(notifications.KindRequestPending) { - t.Errorf("admin inbox holds %q, want only request_pending", it.Kind) - } - } -} - func TestNotify_FlagReachesAdminsButNotTheFlagger(t *testing.T) { h, pool := testHandlers(t) truncateLibrary(t, pool) diff --git a/internal/api/requests.go b/internal/api/requests.go deleted file mode 100644 index 887da75b..00000000 --- a/internal/api/requests.go +++ /dev/null @@ -1,264 +0,0 @@ -package api - -import ( - "context" - "errors" - "net/http" - - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/notifications" - "git.fabledsword.com/bvandeusen/minstrel/internal/requests" -) - -// requestView is the JSON shape returned by all /api/requests handlers. -// Field names follow the lowercase snake_case convention for /api/* responses. -type requestView struct { - ID pgtype.UUID `json:"id"` - UserID pgtype.UUID `json:"user_id"` - // Username names the requester. Only the admin list fills it; a user's - // own /requests page has no need to name them back to themselves. - Username string `json:"username,omitempty"` - Status string `json:"status"` - Kind string `json:"kind"` - ArtistMBID string `json:"artist_mbid"` - AlbumMBID *string `json:"album_mbid,omitempty"` - TrackMBID *string `json:"track_mbid,omitempty"` - ArtistName string `json:"artist_name"` - AlbumTitle *string `json:"album_title,omitempty"` - TrackTitle *string `json:"track_title,omitempty"` - DecidedAt pgtype.Timestamptz `json:"decided_at,omitempty"` - DecidedBy pgtype.UUID `json:"decided_by,omitempty"` - Notes *string `json:"notes,omitempty"` - CompletedAt pgtype.Timestamptz `json:"completed_at,omitempty"` - MatchedTrackID pgtype.UUID `json:"matched_track_id,omitempty"` - MatchedAlbumID pgtype.UUID `json:"matched_album_id,omitempty"` - MatchedArtistID pgtype.UUID `json:"matched_artist_id,omitempty"` - RequestedAt pgtype.Timestamptz `json:"requested_at"` - UpdatedAt pgtype.Timestamptz `json:"updated_at"` - // Progress counters, computed at response time from the matched - // entity's children. 0 when nothing has been ingested yet (or when - // the request has no matched entity yet). For artist requests both - // counters fill; for album/track requests only ImportedTrackCount. - ImportedAlbumCount int `json:"imported_album_count"` - ImportedTrackCount int `json:"imported_track_count"` -} - -func requestViewFrom(row dbq.Request) requestView { - return requestView{ - ID: row.ID, - UserID: row.UserID, - Status: string(row.Status), - Kind: string(row.Kind), - ArtistMBID: row.ArtistMbid, - AlbumMBID: row.AlbumMbid, - TrackMBID: row.TrackMbid, - ArtistName: row.ArtistName, - AlbumTitle: row.AlbumTitle, - TrackTitle: row.TrackTitle, - DecidedAt: row.DecidedAt, - DecidedBy: row.DecidedBy, - Notes: row.Notes, - CompletedAt: row.CompletedAt, - MatchedTrackID: row.MatchedTrackID, - MatchedAlbumID: row.MatchedAlbumID, - MatchedArtistID: row.MatchedArtistID, - RequestedAt: row.RequestedAt, - UpdatedAt: row.UpdatedAt, - } -} - -// fillProgress fills ImportedAlbumCount + ImportedTrackCount on a view by -// querying the count of children for the request's matched entity. -// -// kind=artist → counts all albums + tracks under the matched artist. -// kind=album → counts tracks under the matched album. -// kind=track → ImportedTrackCount = 1 once matched_track_id is set. -// -// N+1 against the request list: acceptable at admin scale (dozens of -// in-flight requests at most). Errors are swallowed — progress is a -// reporting nicety, not a correctness gate. -func fillProgress(ctx context.Context, q *dbq.Queries, row dbq.Request, view *requestView) { - switch row.Kind { - case dbq.RequestKindArtist: - if !row.MatchedArtistID.Valid { - return - } - if n, err := q.CountAlbumsByArtist(ctx, row.MatchedArtistID); err == nil { - view.ImportedAlbumCount = int(n) - } - if n, err := q.CountTracksByArtist(ctx, row.MatchedArtistID); err == nil { - view.ImportedTrackCount = int(n) - } - case dbq.RequestKindAlbum: - if !row.MatchedAlbumID.Valid { - return - } - view.ImportedAlbumCount = 1 - if n, err := q.CountTracksByAlbum(ctx, row.MatchedAlbumID); err == nil { - view.ImportedTrackCount = int(n) - } - case dbq.RequestKindTrack: - if row.MatchedTrackID.Valid { - view.ImportedTrackCount = 1 - } - } -} - -// createRequestBody is the decoded JSON for POST /api/requests. album_mbid -// is the album's MusicBrainz release group, as catalogue search returns it. -type createRequestBody struct { - Kind string `json:"kind"` - ArtistMBID string `json:"artist_mbid"` - AlbumMBID string `json:"album_mbid"` - TrackMBID string `json:"track_mbid"` - ArtistName string `json:"artist_name"` - AlbumTitle string `json:"album_title"` - TrackTitle string `json:"track_title"` -} - -// handleCreateRequest implements POST /api/requests. -func (h *handlers) handleCreateRequest(w http.ResponseWriter, r *http.Request) { - user, ok := requireUser(w, r) - if !ok { - return - } - - var body createRequestBody - if !decodeBody(w, r, &body) { - return - } - - row, created, err := h.requests.CreateTracked(r.Context(), user.ID, requests.CreateParams{ - Kind: body.Kind, - ArtistMBID: body.ArtistMBID, - AlbumMBID: body.AlbumMBID, - TrackMBID: body.TrackMBID, - ArtistName: body.ArtistName, - AlbumTitle: body.AlbumTitle, - TrackTitle: body.TrackTitle, - }) - if err != nil { - if errors.Is(err, requests.ErrInvalidKindFields) { - writeErr(w, apierror.BadRequest("mbid_required", err.Error())) - return - } - h.logger.Error("api: create request", "err", err) - writeErr(w, apierror.InternalMsg("create failed", err)) - return - } - - // Auto-approve: when an admin has set the user's - // auto_approve_requests flag, the new request goes straight onto the - // wanted list as if approved. A failure leaves it pending for an admin, - // the same as a manual approval hitting the same error. The approver is - // the user themselves, acting under the privilege the admin granted; the - // grant is in audit_log (ActionAutoApproveToggle). - if user.AutoApproveRequests { - approved, aerr := h.requests.Approve(r.Context(), row.ID, user.ID, pgtype.UUID{}) - if aerr == nil { - row = approved - } else { - h.logger.Warn("api: auto-approve failed; request stays pending", - "user_id", uuidToString(user.ID), - "request_id", uuidToString(row.ID), - "err", aerr) - } - } - - // A new request still pending after any auto-approval waits on an admin. - // One that deduped into a request already in flight was announced when it - // was first made, and the requester, if they are an admin themselves, - // needs no notice of their own request. - if created && row.Status == dbq.RequestStatusPending { - p := requestPayload(row) - p.Actor = userLabel(user) - h.notifier.NotifyLogged(r.Context(), notifications.KindRequestPending, notifications.ToAdmins(user.ID), p.Map()) - } - - h.publishRequestStatusChanged(row) - writeJSON(w, http.StatusCreated, requestViewFrom(row)) -} - -// handleListRequests implements GET /api/requests — returns caller's own -// requests. -func (h *handlers) handleListRequests(w http.ResponseWriter, r *http.Request) { - user, ok := requireUser(w, r) - if !ok { - return - } - - rows, err := h.requests.ListForUser(r.Context(), user.ID, 100) - if err != nil { - h.logger.Error("api: list requests", "err", err) - writeErr(w, apierror.InternalMsg("list failed", err)) - return - } - - q := dbq.New(h.pool) - out := make([]requestView, 0, len(rows)) - for _, row := range rows { - v := requestViewFrom(row) - fillProgress(r.Context(), q, row, &v) - out = append(out, v) - } - writeJSON(w, http.StatusOK, out) -} - -// handleGetRequest implements GET /api/requests/:id. -// The caller must own the request, or be an admin. -func (h *handlers) handleGetRequest(w http.ResponseWriter, r *http.Request) { - user, ok := requireUser(w, r) - if !ok { - return - } - - row, apiErr := resolveByID(r, "id", dbq.New(h.pool).GetRequestByID, "request") - if apiErr != nil { - writeErr(w, apiErr) - return - } - - // Allow if caller owns the request or is an admin. - if row.UserID != user.ID && !user.IsAdmin { - writeErr(w, apierror.NotFound("request")) - return - } - - v := requestViewFrom(row) - fillProgress(r.Context(), dbq.New(h.pool), row, &v) - writeJSON(w, http.StatusOK, v) -} - -// handleCancelRequest implements DELETE /api/requests/:id. -// Cancels the caller's own pending request. -func (h *handlers) handleCancelRequest(w http.ResponseWriter, r *http.Request) { - user, ok := requireUser(w, r) - if !ok { - return - } - - id, ok := requireURLUUID(w, r, "id") - if !ok { - return - } - - row, err := h.requests.Cancel(r.Context(), id, user.ID) - if err != nil { - switch { - case errors.Is(err, requests.ErrNotPending): - writeErr(w, apierror.Conflict("request_not_pending", "request is not pending")) - case errors.Is(err, requests.ErrNotFound): - writeErr(w, apierror.NotFound("request")) - default: - h.logger.Error("api: cancel request", "err", err) - writeErr(w, apierror.InternalMsg("cancel failed", err)) - } - return - } - - h.publishRequestStatusChanged(row) - writeJSON(w, http.StatusOK, requestViewFrom(row)) -} diff --git a/internal/api/requests_test.go b/internal/api/requests_test.go deleted file mode 100644 index 3e922eec..00000000 --- a/internal/api/requests_test.go +++ /dev/null @@ -1,396 +0,0 @@ -package api - -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "net/http" - "net/http/httptest" - "testing" - - "github.com/go-chi/chi/v5" - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -// newRequestsRouter builds a test chi router wiring the /api/requests handlers -// with chi URL params but without RequireUser middleware. Tests inject a user -// into the context manually. -func newRequestsRouter(h *handlers) chi.Router { - r := chi.NewRouter() - r.Post("/api/requests", h.handleCreateRequest) - r.Get("/api/requests", h.handleListRequests) - r.Get("/api/requests/{id}", h.handleGetRequest) - r.Delete("/api/requests/{id}", h.handleCancelRequest) - return r -} - -// withUser injects a user into the request context the same way RequireUser -// does. -func withUser(req *http.Request, user dbq.User) *http.Request { - return req.WithContext(context.WithValue(req.Context(), userCtxKeyForTest(), user)) -} - -// doCreateRequest fires POST /api/requests with the given JSON body as the -// given user. -func doCreateRequest(h *handlers, user dbq.User, body string) *httptest.ResponseRecorder { - req := httptest.NewRequest(http.MethodPost, "/api/requests", bytes.NewBufferString(body)) - req.Header.Set("Content-Type", "application/json") - req = withUser(req, user) - w := httptest.NewRecorder() - newRequestsRouter(h).ServeHTTP(w, req) - return w -} - -// doListRequests fires GET /api/requests as the given user. -func doListRequests(h *handlers, user dbq.User) *httptest.ResponseRecorder { - req := httptest.NewRequest(http.MethodGet, "/api/requests", nil) - req = withUser(req, user) - w := httptest.NewRecorder() - newRequestsRouter(h).ServeHTTP(w, req) - return w -} - -// doGetRequest fires GET /api/requests/:id as the given user. -func doGetRequest(h *handlers, user dbq.User, id pgtype.UUID) *httptest.ResponseRecorder { - req := httptest.NewRequest(http.MethodGet, "/api/requests/"+uuidToString(id), nil) - req = withUser(req, user) - w := httptest.NewRecorder() - newRequestsRouter(h).ServeHTTP(w, req) - return w -} - -// doCancelRequest fires DELETE /api/requests/:id as the given user. -func doCancelRequest(h *handlers, user dbq.User, id pgtype.UUID) *httptest.ResponseRecorder { - req := httptest.NewRequest(http.MethodDelete, "/api/requests/"+uuidToString(id), nil) - req = withUser(req, user) - w := httptest.NewRecorder() - newRequestsRouter(h).ServeHTTP(w, req) - return w -} - -// createArtistRequest is a convenience wrapper that seeds a valid artist -// request via the handler and returns the parsed response. -func createArtistRequest(t *testing.T, h *handlers, user dbq.User, artistMBID, artistName string) requestView { - t.Helper() - body := fmt.Sprintf(`{"kind":"artist","artist_mbid":%q,"artist_name":%q}`, artistMBID, artistName) - w := doCreateRequest(h, user, body) - if w.Code != http.StatusCreated { - t.Fatalf("createArtistRequest: status = %d, want 201; body = %s", w.Code, w.Body.String()) - } - var rv requestView - if err := json.Unmarshal(w.Body.Bytes(), &rv); err != nil { - t.Fatalf("createArtistRequest: decode: %v; body = %s", err, w.Body.String()) - } - return rv -} - -// TestHandleCreateRequest_AutoApprove_GoesStraightOnTheWantedList verifies -// that a user with auto_approve_requests=true gets their request approved on -// creation: an artist request follows the artist at once. -func TestHandleCreateRequest_AutoApprove_GoesStraightOnTheWantedList(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice", "pw", false) - if _, err := pool.Exec(context.Background(), - "UPDATE users SET auto_approve_requests = true WHERE id = $1", alice.ID); err != nil { - t.Fatalf("set auto_approve: %v", err) - } - // Re-fetch so the User row carried into the request context has the flag. - refreshed, err := dbq.New(pool).GetUserByID(context.Background(), alice.ID) - if err != nil { - t.Fatalf("reload user: %v", err) - } - - body := `{"kind":"artist","artist_mbid":"auto-mbid-1","artist_name":"Autoband"}` - w := doCreateRequest(h, refreshed, body) - if w.Code != http.StatusCreated { - t.Fatalf("status = %d, want 201; body = %s", w.Code, w.Body.String()) - } - var rv requestView - if err := json.Unmarshal(w.Body.Bytes(), &rv); err != nil { - t.Fatalf("decode: %v", err) - } - if rv.Status != "approved" { - t.Errorf("status = %q, want approved", rv.Status) - } - if _, err := dbq.New(pool).GetFollow(context.Background(), "auto-mbid-1"); err != nil { - t.Errorf("the artist should be followed: %v", err) - } -} - -// TestHandleCreateRequest_Artist_HappyPath verifies a valid artist request -// returns 201 with kind=artist and status=pending. -func TestHandleCreateRequest_Artist_HappyPath(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice", "pw", false) - body := `{"kind":"artist","artist_mbid":"artist-mbid-1","artist_name":"The Beatles"}` - w := doCreateRequest(h, alice, body) - - if w.Code != http.StatusCreated { - t.Fatalf("status = %d, want 201; body = %s", w.Code, w.Body.String()) - } - var rv requestView - if err := json.Unmarshal(w.Body.Bytes(), &rv); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if string(rv.Kind) != "artist" { - t.Errorf("kind = %q, want artist", rv.Kind) - } - if rv.Status != "pending" { - t.Errorf("status = %q, want pending", rv.Status) - } - if rv.ArtistMBID != "artist-mbid-1" { - t.Errorf("artist_mbid = %q", rv.ArtistMBID) - } - if rv.ArtistName != "The Beatles" { - t.Errorf("artist_name = %q", rv.ArtistName) - } - if !rv.ID.Valid { - t.Error("id not set") - } -} - -// TestHandleCreateRequest_AlbumWithoutAlbumMBID_400 verifies that sending -// kind=album without album_mbid returns 400 with mbid_required. -func TestHandleCreateRequest_AlbumWithoutAlbumMBID_400(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice", "pw", false) - // Missing album_mbid and album_title. - body := `{"kind":"album","artist_mbid":"artist-mbid-1","artist_name":"The Beatles"}` - w := doCreateRequest(h, alice, body) - - if w.Code != http.StatusBadRequest { - t.Fatalf("status = %d, want 400; body = %s", w.Code, w.Body.String()) - } - var resp struct { - Error struct { - Code string `json:"code"` - } `json:"error"` - } - if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if resp.Error.Code != "mbid_required" { - t.Errorf("error.code = %q, want mbid_required", resp.Error.Code) - } -} - -// TestHandleCreateRequest_TrackWithoutTrackFields_400 verifies that kind=track -// with missing track_mbid/track_title returns 400 with mbid_required. -func TestHandleCreateRequest_TrackWithoutTrackFields_400(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice", "pw", false) - // Provides album fields but misses track_mbid and track_title. - body := `{"kind":"track","artist_mbid":"artist-mbid-1","artist_name":"Beatles","album_mbid":"album-mbid-1","album_title":"Abbey Road"}` - w := doCreateRequest(h, alice, body) - - if w.Code != http.StatusBadRequest { - t.Fatalf("status = %d, want 400; body = %s", w.Code, w.Body.String()) - } - var resp struct { - Error struct { - Code string `json:"code"` - } `json:"error"` - } - if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if resp.Error.Code != "mbid_required" { - t.Errorf("error.code = %q, want mbid_required", resp.Error.Code) - } -} - -// TestHandleListRequests_OnlyOwnRequests verifies alice sees only her own -// requests, not bob's. -func TestHandleListRequests_OnlyOwnRequests(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice", "pw", false) - bob := seedUser(t, pool, "bob", "pw", false) - - createArtistRequest(t, h, alice, "alice-mbid-1", "Alice Artist 1") - createArtistRequest(t, h, alice, "alice-mbid-2", "Alice Artist 2") - createArtistRequest(t, h, bob, "bob-mbid-1", "Bob Artist 1") - - w := doListRequests(h, alice) - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - var results []requestView - if err := json.Unmarshal(w.Body.Bytes(), &results); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if len(results) != 2 { - t.Fatalf("len(results) = %d, want 2", len(results)) - } - for _, rv := range results { - if rv.UserID != alice.ID { - t.Errorf("result belongs to wrong user: %v", rv.UserID) - } - } -} - -// TestHandleGetRequest_OwnReturns200 verifies a caller can fetch their own -// request. -func TestHandleGetRequest_OwnReturns200(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice", "pw", false) - rv := createArtistRequest(t, h, alice, "artist-mbid-get", "Get Artist") - - w := doGetRequest(h, alice, rv.ID) - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - var got requestView - if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if got.ID != rv.ID { - t.Errorf("id mismatch") - } -} - -// TestHandleGetRequest_OtherUserNotAdmin_404 verifies a non-admin caller -// cannot see another user's request. -func TestHandleGetRequest_OtherUserNotAdmin_404(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - bob := seedUser(t, pool, "bob", "pw", false) - charlie := seedUser(t, pool, "charlie", "pw", false) - - rv := createArtistRequest(t, h, bob, "bob-artist-mbid", "Bob's Artist") - - // Charlie is not an admin, tries to fetch Bob's request. - w := doGetRequest(h, charlie, rv.ID) - if w.Code != http.StatusNotFound { - t.Fatalf("status = %d, want 404; body = %s", w.Code, w.Body.String()) - } - var resp struct { - Error struct { - Code string `json:"code"` - } `json:"error"` - } - if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if resp.Error.Code != "request_not_found" { - t.Errorf("error.code = %q, want request_not_found", resp.Error.Code) - } -} - -// TestHandleGetRequest_OtherUserAsAdmin_200 verifies an admin can fetch any -// user's request. -func TestHandleGetRequest_OtherUserAsAdmin_200(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - bob := seedUser(t, pool, "bob", "pw", false) - admin := seedUser(t, pool, "admin", "pw", true) - - rv := createArtistRequest(t, h, bob, "bob-admin-mbid", "Bob's Artist For Admin") - - // Admin fetches Bob's request — should succeed. - w := doGetRequest(h, admin, rv.ID) - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - var got requestView - if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if got.ID != rv.ID { - t.Errorf("id mismatch") - } -} - -// TestHandleCancelRequest_PendingHappyPath verifies that cancelling a pending -// request returns 200 with status=rejected. -func TestHandleCancelRequest_PendingHappyPath(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice", "pw", false) - rv := createArtistRequest(t, h, alice, "cancel-mbid-1", "Cancel Me") - - w := doCancelRequest(h, alice, rv.ID) - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - var got requestView - if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { - t.Fatalf("decode: %v; body = %s", err, w.Body.String()) - } - if got.Status != "rejected" { - t.Errorf("status = %q, want rejected", got.Status) - } -} - -// TestHandleCancelRequest_NonPending_409 verifies that cancelling an already -// rejected request returns 409 with request_not_pending. -func TestHandleCancelRequest_NonPending_409(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice", "pw", false) - rv := createArtistRequest(t, h, alice, "cancel-mbid-2", "Cancel Twice") - - // First cancel transitions to rejected. - w1 := doCancelRequest(h, alice, rv.ID) - if w1.Code != http.StatusOK { - t.Fatalf("first cancel: status = %d, want 200; body = %s", w1.Code, w1.Body.String()) - } - - // Second cancel on a now-rejected row → ErrNotPending → 409. The cancel SQL - // checks user_id = $2 AND status = 'pending'; zero rows → ErrNotPending. - // For this test we use dbq directly to reject via admin so Cancel returns - // ErrNotPending rather than the ownership miss. We already did first cancel - // so row is rejected. - w2 := doCancelRequest(h, alice, rv.ID) - if w2.Code != http.StatusConflict { - t.Fatalf("second cancel: status = %d, want 409; body = %s", w2.Code, w2.Body.String()) - } - var resp struct { - Error struct { - Code string `json:"code"` - } `json:"error"` - } - if err := json.Unmarshal(w2.Body.Bytes(), &resp); err != nil { - t.Fatalf("decode: %v; body = %s", err, w2.Body.String()) - } - if resp.Error.Code != "request_not_pending" { - t.Errorf("error.code = %q, want request_not_pending", resp.Error.Code) - } -} - -// TestHandleCancelRequest_WrongUser_409 verifies that attempting to cancel -// another user's request returns 409 (not_pending from the ownership miss). -func TestHandleCancelRequest_WrongUser_409(t *testing.T) { - h, pool := testHandlers(t) - resetRequests(t, h) - - alice := seedUser(t, pool, "alice-cancel-owner", "pw", false) - bob := seedUser(t, pool, "bob-cancel-other", "pw", false) - - rv := createArtistRequest(t, h, alice, "cancel-wrong-user-mbid", "Alice's Artist") - - // Bob tries to cancel Alice's request: SQL WHERE user_id=bob AND - // status=pending → 0 rows. - w := doCancelRequest(h, bob, rv.ID) - if w.Code != http.StatusConflict { - t.Fatalf("status = %d, want 409; body = %s", w.Code, w.Body.String()) - } -} diff --git a/internal/api/suggestions.go b/internal/api/suggestions.go deleted file mode 100644 index 96cca9dd..00000000 --- a/internal/api/suggestions.go +++ /dev/null @@ -1,261 +0,0 @@ -package api - -import ( - "encoding/json" - "errors" - "io" - "net/http" - "strconv" - "strings" - - "github.com/go-chi/chi/v5" - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/recommendation" -) - -// suggestionView is the wire shape returned by GET /api/discover/suggestions. -type suggestionView struct { - MBID string `json:"mbid"` - Name string `json:"name"` - Score float64 `json:"score"` - Attribution []seedContributionView `json:"attribution"` - // MatchedTags are the candidate's tags that overlap the user's taste - // profile, strongest first — the "matches: shoegaze, melancholic" - // line. Omitted when empty, which is common: tag coverage for - // out-of-library artists is permanently partial, and the card falls back - // to the seed attribution. - MatchedTags []string `json:"matched_tags,omitempty"` - // ImageURL is Minstrel's catalogue art endpoint for the candidate - // (out-of-library artists have no local art row). It - // answers 404 when no enabled source has an image, and the client - // falls back to a placeholder. - ImageURL string `json:"image_url,omitempty"` -} - -type seedContributionView struct { - ArtistID pgtype.UUID `json:"artist_id"` - Name string `json:"name"` - Contribution float64 `json:"contribution"` - IsLiked bool `json:"is_liked"` - PlayCount int64 `json:"play_count"` -} - -// handleListSuggestions implements GET /api/discover/suggestions. -// -// Query params: -// - limit (default 12, capped at 50) -// - half_life_days (default 30) -// -// Returns 200 with a JSON array (possibly empty). Read-only; no admin gate. -func (h *handlers) handleListSuggestions(w http.ResponseWriter, r *http.Request) { - user, ok := requireUser(w, r) - if !ok { - return - } - limit := 12 - if v := r.URL.Query().Get("limit"); v != "" { - n, err := strconv.Atoi(v) - if err != nil || n < 1 { - writeErr(w, apierror.BadRequest("bad_request", "invalid limit")) - return - } - limit = n - } - halfLife := 30.0 - if v := r.URL.Query().Get("half_life_days"); v != "" { - f, err := strconv.ParseFloat(v, 64) - if err != nil || f <= 0 { - writeErr(w, apierror.BadRequest("bad_request", "invalid half_life_days")) - return - } - halfLife = f - } - - // Read the tuned weight per request so an admin change takes effect on the - // next refresh, no restart. - tagWeight := h.recSettings.Discover().TagOverlapWeight - suggestions, err := recommendation.SuggestArtists( - r.Context(), h.pool, user.ID, halfLife, limit, tagWeight) - if err != nil { - h.logger.Error("api: list suggestions", "err", err) - writeErr(w, apierror.InternalMsg("failed to load suggestions", err)) - return - } - - out := make([]suggestionView, 0, len(suggestions)) - for _, s := range suggestions { - attr := make([]seedContributionView, 0, len(s.Attribution)) - for _, a := range s.Attribution { - attr = append(attr, seedContributionView{ - ArtistID: a.ArtistID, - Name: a.Name, - Contribution: a.Contribution, - IsLiked: a.IsLiked, - PlayCount: a.PlayCount, - }) - } - out = append(out, suggestionView{ - MBID: s.MBID, Name: s.Name, Score: s.Score, Attribution: attr, - MatchedTags: s.MatchedTags, - }) - } - resolveSuggestionArt(out) - writeJSON(w, http.StatusOK, out) -} - -// maxSnoozeDays caps a client-supplied duration. The DEFAULT is not here: it's -// a DB-backed knob on the admin tuning card, read per request via -// recSettings.Discover().SnoozeDays. -const maxSnoozeDays = 365.0 - -// snoozeRequest is the POST body. Both fields are optional in the JSON sense -// (an absent body snoozes for the default), but Name is required in practice: -// candidates are out-of-library, so the server has no artists row to resolve a -// display name from and the un-snooze list would have nothing to show. The -// client always has it — it just rendered the card. -type snoozeRequest struct { - Name string `json:"name"` - Days float64 `json:"days"` -} - -// snoozeView is one row of GET /api/discover/snoozes. -type snoozeView struct { - MBID string `json:"mbid"` - Name string `json:"name"` - SnoozedUntil pgtype.Timestamptz `json:"snoozed_until"` - CreatedAt pgtype.Timestamptz `json:"created_at"` -} - -// handleSnoozeSuggestion implements -// POST /api/discover/suggestions/{mbid}/snooze. -// -// Parks a candidate for `days` (default 90, capped at 365). Idempotent: -// snoozing an already-snoozed candidate extends it rather than conflicting. -// -// This is NOT negative feedback. It records no verdict on the artist and is -// never read by internal/taste — see 0049_suggestion_snoozes.up.sql for why. -// Returns 204. -func (h *handlers) handleSnoozeSuggestion(w http.ResponseWriter, r *http.Request) { - user, ok := requireUser(w, r) - if !ok { - return - } - mbid := strings.TrimSpace(chi.URLParam(r, "mbid")) - if mbid == "" { - writeErr(w, apierror.BadRequest("invalid_id", "missing mbid")) - return - } - - // An empty body is a valid "snooze this for the default period", so EOF - // is not an error here — decodeBody would reject it as a malformed body. - var body snoozeRequest - if err := json.NewDecoder(r.Body).Decode(&body); err != nil && !errors.Is(err, io.EOF) { - writeErr(w, apierror.BadRequest("invalid_body", "")) - return - } - name := strings.TrimSpace(body.Name) - if name == "" { - writeErr(w, apierror.BadRequest("invalid_body", "name is required")) - return - } - days := body.Days - if days <= 0 { - days = h.recSettings.Discover().SnoozeDays - } - if days > maxSnoozeDays { - // Clamp rather than reject: a client asking for longer than we allow - // still means "park this", and failing the write would leave the card - // sitting there as if the tap did nothing. - days = maxSnoozeDays - } - - q := dbq.New(h.pool) - if err := q.SnoozeSuggestion(r.Context(), dbq.SnoozeSuggestionParams{ - UserID: user.ID, - CandidateMbid: mbid, - CandidateName: name, - Column4: days, - }); err != nil { - h.logger.Error("api: snooze suggestion", "err", err) - writeErr(w, apierror.InternalMsg("failed to snooze suggestion", err)) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// handleUnsnoozeSuggestion implements -// DELETE /api/discover/suggestions/{mbid}/snooze. -// -// Brings a parked candidate back immediately. 404s an MBID this user never -// snoozed, so the client can tell "undone" from "there was nothing there". -func (h *handlers) handleUnsnoozeSuggestion(w http.ResponseWriter, r *http.Request) { - user, ok := requireUser(w, r) - if !ok { - return - } - mbid := strings.TrimSpace(chi.URLParam(r, "mbid")) - if mbid == "" { - writeErr(w, apierror.BadRequest("invalid_id", "missing mbid")) - return - } - q := dbq.New(h.pool) - rows, err := q.UnsnoozeSuggestion(r.Context(), dbq.UnsnoozeSuggestionParams{ - UserID: user.ID, - CandidateMbid: mbid, - }) - if err != nil { - h.logger.Error("api: unsnooze suggestion", "err", err) - writeErr(w, apierror.InternalMsg("failed to unsnooze suggestion", err)) - return - } - if rows == 0 { - writeErr(w, apierror.NotFound("snooze")) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// handleListSuggestionSnoozes implements GET /api/discover/snoozes. -// -// The un-snooze surface needs this: a parked candidate is by definition -// absent from the suggestion deck, so without a list there is no way to -// reach the DELETE above. Scoped to the caller. Expired rows are -// already filtered by the query — the hourly gc sweep only reclaims space. -func (h *handlers) handleListSuggestionSnoozes(w http.ResponseWriter, r *http.Request) { - user, ok := requireUser(w, r) - if !ok { - return - } - rows, err := dbq.New(h.pool).ListActiveSuggestionSnoozes(r.Context(), user.ID) - if err != nil { - h.logger.Error("api: list suggestion snoozes", "err", err) - writeErr(w, apierror.InternalMsg("failed to load snoozes", err)) - return - } - out := make([]snoozeView, 0, len(rows)) - for _, row := range rows { - out = append(out, snoozeView{ - MBID: row.CandidateMbid, - Name: row.CandidateName, - SnoozedUntil: row.SnoozedUntil, - CreatedAt: row.CreatedAt, - }) - } - writeJSON(w, http.StatusOK, out) -} - -// resolveSuggestionArt points each suggestion's ImageURL at Minstrel's -// catalogue art endpoint. Nothing is fetched here: the image is -// looked up from the enabled cover-art sources when the browser asks for it, -// and kept, so listing suggestions makes no outbound call. A candidate with -// no MBID keeps an empty ImageURL and the client renders its placeholder. -func resolveSuggestionArt(views []suggestionView) { - for i := range views { - if views[i].MBID != "" { - views[i].ImageURL = artistArtURL(views[i].MBID, views[i].Name) - } - } -} diff --git a/internal/api/suggestions_test.go b/internal/api/suggestions_test.go deleted file mode 100644 index be7bb5de..00000000 --- a/internal/api/suggestions_test.go +++ /dev/null @@ -1,104 +0,0 @@ -package api - -import ( - "context" - "encoding/json" - "net/http" - "net/http/httptest" - "testing" - - "github.com/go-chi/chi/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -func newSuggestionsRouter(h *handlers) chi.Router { - r := chi.NewRouter() - r.Get("/api/discover/suggestions", h.handleListSuggestions) - return r -} - -func doListSuggestions(h *handlers, user dbq.User, query string) *httptest.ResponseRecorder { - url := "/api/discover/suggestions" - if query != "" { - url += "?" + query - } - req := httptest.NewRequest(http.MethodGet, url, nil) - req = withUser(req, user) - w := httptest.NewRecorder() - newSuggestionsRouter(h).ServeHTTP(w, req) - return w -} - -func TestSuggestions_HappyPath(t *testing.T) { - h, pool := testHandlers(t) - user := seedUser(t, pool, "alice", "pw", false) - - q := dbq.New(pool) - seed, err := q.UpsertArtist(context.Background(), dbq.UpsertArtistParams{ - Name: "Seed", SortName: "Seed", - }) - if err != nil { - t.Fatalf("UpsertArtist: %v", err) - } - if _, err := pool.Exec(context.Background(), - `INSERT INTO general_likes_artists (user_id, artist_id) VALUES ($1, $2)`, - user.ID, seed.ID, - ); err != nil { - t.Fatalf("like artist: %v", err) - } - if err := q.UpsertArtistSimilarityUnmatched(context.Background(), dbq.UpsertArtistSimilarityUnmatchedParams{ - SeedArtistID: seed.ID, - CandidateMbid: "out-mbid", - CandidateName: "Outsider", - Score: 0.9, - Source: "listenbrainz", - }); err != nil { - t.Fatalf("UpsertArtistSimilarityUnmatched: %v", err) - } - - w := doListSuggestions(h, user, "") - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - var got []suggestionView - if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { - t.Fatalf("decode: %v", err) - } - if len(got) != 1 { - t.Fatalf("len = %d, want 1; body = %s", len(got), w.Body.String()) - } - if got[0].MBID != "out-mbid" || got[0].Name != "Outsider" { - t.Errorf("got = %+v", got[0]) - } - if len(got[0].Attribution) != 1 || got[0].Attribution[0].Name != "Seed" { - t.Errorf("attribution = %+v, want one entry named Seed", got[0].Attribution) - } - if !got[0].Attribution[0].IsLiked { - t.Errorf("attribution.IsLiked = false, want true") - } -} - -func TestSuggestions_EmptyForNewUser(t *testing.T) { - h, pool := testHandlers(t) - user := seedUser(t, pool, "newbie", "pw", false) - - w := doListSuggestions(h, user, "") - if w.Code != http.StatusOK { - t.Fatalf("status = %d, want 200; body = %s", w.Code, w.Body.String()) - } - body := w.Body.String() - if body != "[]" && body != "[]\n" { - t.Errorf("body = %q, want []", body) - } -} - -func TestSuggestions_BadLimit(t *testing.T) { - h, pool := testHandlers(t) - user := seedUser(t, pool, "alice", "pw", false) - - w := doListSuggestions(h, user, "limit=not-a-number") - if w.Code != http.StatusBadRequest { - t.Errorf("status = %d, want 400; body = %s", w.Code, w.Body.String()) - } -} diff --git a/internal/audit/audit.go b/internal/audit/audit.go index 07b37ba2..fc86340d 100644 --- a/internal/audit/audit.go +++ b/internal/audit/audit.go @@ -58,7 +58,7 @@ const ( // The resolver settling a group without a merge: // dismissing two versions of a song that are not duplicates, and removing // copies that filled another song's track slot (their tracks are kept, - // marked missing, and fetched again). + // marked missing, until a good copy is in the library). ActionDuplicateDismiss Action = "duplicate_dismiss" ActionDuplicateWrongFile Action = "duplicate_wrong_file" diff --git a/internal/catalogue/art.go b/internal/catalogue/art.go deleted file mode 100644 index 0d1a1b63..00000000 --- a/internal/catalogue/art.go +++ /dev/null @@ -1,214 +0,0 @@ -package catalogue - -import ( - "context" - "errors" - "fmt" - "os" - "path/filepath" - "regexp" - "sync" - "time" - - "git.fabledsword.com/bvandeusen/minstrel/internal/coverart" -) - -// NoArtRetry is how long "no source has art for this" is believed before -// the sources are asked again. -const NoArtRetry = 7 * 24 * time.Hour - -// fetchTimeout bounds one look-up across every source. -const fetchTimeout = 30 * time.Second - -// ErrNoArt: no enabled source has an image, or the sources could not be -// reached. The caller shows its placeholder. -var ErrNoArt = errors.New("catalogue: no art") - -var mbidPattern = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`) - -// ValidMBID reports whether s is shaped like a MusicBrainz id. It is the -// guard that keeps a request path out of the cache directory's parents. -func ValidMBID(s string) bool { return mbidPattern.MatchString(s) } - -// Art serves images for artists and release groups that are not in the -// library (search results, Discover suggestions, wanted albums), from the -// enabled cover-art sources. Each image is fetched once and kept -// under the data directory, so the browser only ever talks to Minstrel. -type Art struct { - dir string - artistSources func() []coverart.ArtistArtProvider - albumSources func() []coverart.AlbumCoverProvider - now func() time.Time - - mu sync.Mutex - inflight map[string]*artCall -} - -type artCall struct { - done chan struct{} - body []byte - err error -} - -// ArtSources lists the image sources to ask, in order. The cover-art -// settings service is one: it answers with the sources an admin enabled. -type ArtSources interface { - EnabledArtistProviders() []coverart.ArtistArtProvider - EnabledAlbumProviders() []coverart.AlbumCoverProvider -} - -// NewArt keeps images under dataDir/catalogue-art and asks the given -// sources. Nil sources ask nothing, so every image is the placeholder. -func NewArt(dataDir string, sources ArtSources) *Art { - a := &Art{dir: filepath.Join(dataDir, "catalogue-art"), now: time.Now, inflight: map[string]*artCall{}} - if sources != nil { - a.artistSources = sources.EnabledArtistProviders - a.albumSources = sources.EnabledAlbumProviders - } - return a -} - -// Artist returns an image of the artist: the first enabled artist source -// with one wins, its thumbnail preferred over its backdrop. -func (a *Art) Artist(ctx context.Context, mbid, name string) ([]byte, error) { - return a.cached(ctx, "artist", mbid, func(ctx context.Context) ([]byte, error) { - if a.artistSources == nil { - return nil, coverart.ErrNotFound - } - ref := coverart.ArtistRef{MBID: mbid, Name: name} - return firstOf(a.artistSources(), func(p coverart.ArtistArtProvider) ([]byte, error) { - thumb, fanart, err := p.FetchArtistArt(ctx, ref) - if err == nil && len(thumb) == 0 { - thumb = fanart - } - return thumb, err - }) - }) -} - -// ReleaseGroup returns a cover for the release group: a source that can -// answer by group (the Cover Art Archive) is asked by its id, the rest by -// artist and title. -func (a *Art) ReleaseGroup(ctx context.Context, mbid, artist, title string) ([]byte, error) { - return a.cached(ctx, "release-group", mbid, func(ctx context.Context) ([]byte, error) { - if a.albumSources == nil { - return nil, coverart.ErrNotFound - } - byName := coverart.AlbumRef{ArtistName: artist, AlbumTitle: title} - return firstOf(a.albumSources(), func(p coverart.AlbumCoverProvider) ([]byte, error) { - if g, ok := p.(coverart.ReleaseGroupCoverProvider); ok { - return g.FetchReleaseGroupCover(ctx, mbid) - } - if artist == "" || title == "" { - return nil, coverart.ErrNotFound - } - return p.FetchAlbumCover(ctx, byName) - }) - }) -} - -// firstOf asks each source in turn. It reports ErrNotFound only when every -// source said so; if any could not be reached, the miss is transient. -func firstOf[P any](sources []P, ask func(P) ([]byte, error)) ([]byte, error) { - transient := false - for _, p := range sources { - body, err := ask(p) - if err == nil && len(body) > 0 { - return body, nil - } - if err != nil && !errors.Is(err, coverart.ErrNotFound) { - transient = true - } - } - if transient { - return nil, coverart.ErrTransient - } - return nil, coverart.ErrNotFound -} - -// cached serves kind/mbid from disk, or fetches it once however many -// requests ask at the same moment. A definite "nobody has one" is written -// down and believed for NoArtRetry; an unreachable source is not, so the -// next request asks again. -func (a *Art) cached(ctx context.Context, kind, mbid string, fetch func(context.Context) ([]byte, error)) ([]byte, error) { - if !ValidMBID(mbid) { - return nil, ErrNoArt - } - dir := filepath.Join(a.dir, kind) - imgPath := filepath.Join(dir, mbid) - nonePath := imgPath + ".none" - - if body, err := os.ReadFile(imgPath); err == nil && len(body) > 0 { - return body, nil - } - if fi, err := os.Stat(nonePath); err == nil && a.now().Sub(fi.ModTime()) < NoArtRetry { - return nil, ErrNoArt - } - - key := kind + "/" + mbid - a.mu.Lock() - if call, ok := a.inflight[key]; ok { - a.mu.Unlock() - select { - case <-call.done: - return call.body, call.err - case <-ctx.Done(): - return nil, ctx.Err() - } - } - call := &artCall{done: make(chan struct{})} - a.inflight[key] = call - a.mu.Unlock() - - defer func() { - a.mu.Lock() - delete(a.inflight, key) - a.mu.Unlock() - close(call.done) - }() - - // The fetch outlives a browser that navigates away: a second request is - // likely, and the image is worth keeping. - fctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), fetchTimeout) - defer cancel() - body, err := fetch(fctx) - switch { - case err == nil && len(body) > 0: - if werr := writeFile(dir, imgPath, body); werr != nil { - // Serving it uncached is still better than a placeholder. - call.body = body - return body, nil - } - _ = os.Remove(nonePath) - call.body = body - return body, nil - case err == nil || errors.Is(err, coverart.ErrNotFound): - _ = writeFile(dir, nonePath, nil) - } - call.err = ErrNoArt - return nil, ErrNoArt -} - -func writeFile(dir, path string, body []byte) error { - if err := os.MkdirAll(dir, 0o755); err != nil { - return fmt.Errorf("catalogue art: %w", err) - } - tmp, err := os.CreateTemp(dir, ".part-*") - if err != nil { - return fmt.Errorf("catalogue art: %w", err) - } - if _, err := tmp.Write(body); err != nil { - _ = tmp.Close() - _ = os.Remove(tmp.Name()) - return fmt.Errorf("catalogue art: %w", err) - } - if err := tmp.Close(); err != nil { - _ = os.Remove(tmp.Name()) - return fmt.Errorf("catalogue art: %w", err) - } - if err := os.Rename(tmp.Name(), path); err != nil { - _ = os.Remove(tmp.Name()) - return fmt.Errorf("catalogue art: %w", err) - } - return nil -} diff --git a/internal/catalogue/art_test.go b/internal/catalogue/art_test.go deleted file mode 100644 index 70d099e9..00000000 --- a/internal/catalogue/art_test.go +++ /dev/null @@ -1,165 +0,0 @@ -package catalogue - -import ( - "context" - "errors" - "os" - "path/filepath" - "sync" - "sync/atomic" - "testing" - "time" - - "git.fabledsword.com/bvandeusen/minstrel/internal/coverart" -) - -const someMBID = "e21857d5-3256-4547-afb3-4b6ded592596" - -// fakeSource is an artist and album source answering with fixed results. -type fakeSource struct { - thumb, fanart, cover []byte - err error - calls atomic.Int32 - gate chan struct{} // when set, each fetch waits for it - lastRef coverart.AlbumRef - mu sync.Mutex -} - -func (f *fakeSource) ID() string { return "fake" } -func (f *fakeSource) DisplayName() string { return "Fake" } -func (f *fakeSource) RequiresAPIKey() bool { return false } -func (f *fakeSource) DefaultEnabled() bool { return true } -func (f *fakeSource) Configure(_ coverart.ProviderSettings) error { return nil } - -func (f *fakeSource) wait() { - f.calls.Add(1) - if f.gate != nil { - <-f.gate - } -} - -func (f *fakeSource) FetchArtistArt(_ context.Context, _ coverart.ArtistRef) ([]byte, []byte, error) { - f.wait() - return f.thumb, f.fanart, f.err -} - -func (f *fakeSource) FetchAlbumCover(_ context.Context, ref coverart.AlbumRef) ([]byte, error) { - f.wait() - f.mu.Lock() - f.lastRef = ref - f.mu.Unlock() - return f.cover, f.err -} - -// groupSource can also answer by release group, as the Cover Art Archive does. -type groupSource struct{ *fakeSource } - -func (g groupSource) FetchReleaseGroupCover(_ context.Context, _ string) ([]byte, error) { - g.wait() - return []byte("by-group"), nil -} - -type sources struct { - artists []coverart.ArtistArtProvider - albums []coverart.AlbumCoverProvider -} - -func (s sources) EnabledArtistProviders() []coverart.ArtistArtProvider { return s.artists } -func (s sources) EnabledAlbumProviders() []coverart.AlbumCoverProvider { return s.albums } - -func TestArt_FetchesOnceAndKeepsIt(t *testing.T) { - src := &fakeSource{fanart: []byte("backdrop")} - a := NewArt(t.TempDir(), sources{artists: []coverart.ArtistArtProvider{src}}) - - body, err := a.Artist(context.Background(), someMBID, "Gorillaz") - if err != nil || string(body) != "backdrop" { - t.Fatalf("with only a backdrop, use it: %q, %v", body, err) - } - if body, _ := a.Artist(context.Background(), someMBID, "Gorillaz"); string(body) != "backdrop" || src.calls.Load() != 1 { - t.Errorf("the second ask should come from disk: %q after %d calls", body, src.calls.Load()) - } -} - -func TestArt_NobodyHasOneIsBelievedForAWeek(t *testing.T) { - src := &fakeSource{err: coverart.ErrNotFound} - a := NewArt(t.TempDir(), sources{artists: []coverart.ArtistArtProvider{src}}) - - if _, err := a.Artist(context.Background(), someMBID, "x"); !errors.Is(err, ErrNoArt) { - t.Fatalf("err = %v, want ErrNoArt", err) - } - if _, err := a.Artist(context.Background(), someMBID, "x"); !errors.Is(err, ErrNoArt) || src.calls.Load() != 1 { - t.Errorf("a definite miss is remembered: %d calls", src.calls.Load()) - } - a.now = func() time.Time { return time.Now().Add(NoArtRetry + time.Hour) } - src.err, src.thumb = nil, []byte("thumb") - if body, err := a.Artist(context.Background(), someMBID, "x"); err != nil || string(body) != "thumb" { - t.Errorf("after a week the sources are asked again: %q, %v", body, err) - } -} - -func TestArt_AnUnreachableSourceIsAskedAgain(t *testing.T) { - src := &fakeSource{err: coverart.ErrTransient} - a := NewArt(t.TempDir(), sources{artists: []coverart.ArtistArtProvider{src}}) - for range 2 { - if _, err := a.Artist(context.Background(), someMBID, "x"); !errors.Is(err, ErrNoArt) { - t.Fatalf("err = %v", err) - } - } - if src.calls.Load() != 2 { - t.Errorf("calls = %d: an outage must not be written down as 'no art'", src.calls.Load()) - } -} - -func TestArt_ReleaseGroupAsksByGroupThenByName(t *testing.T) { - byName := &fakeSource{cover: []byte("by-name")} - caa := groupSource{&fakeSource{}} - a := NewArt(t.TempDir(), sources{albums: []coverart.AlbumCoverProvider{caa, byName}}) - if body, _ := a.ReleaseGroup(context.Background(), someMBID, "Gorillaz", "Demon Days"); string(body) != "by-group" { - t.Errorf("a source that answers by group is asked by group: %q", body) - } - - other := "0c4d5b5f-2f3c-3d31-9c4e-4b4f6b4c1a01" - a = NewArt(t.TempDir(), sources{albums: []coverart.AlbumCoverProvider{byName}}) - if body, _ := a.ReleaseGroup(context.Background(), other, "Gorillaz", "Demon Days"); string(body) != "by-name" { - t.Errorf("by name: %q", body) - } - if byName.lastRef.MBID != "" || byName.lastRef.AlbumTitle != "Demon Days" { - t.Errorf("a release-group id is not a release id, so it is not passed as one: %+v", byName.lastRef) - } -} - -func TestArt_ConcurrentAsksShareOneFetch(t *testing.T) { - src := &fakeSource{thumb: []byte("t"), gate: make(chan struct{})} - a := NewArt(t.TempDir(), sources{artists: []coverart.ArtistArtProvider{src}}) - var wg sync.WaitGroup - for range 5 { - wg.Add(1) - go func() { - defer wg.Done() - if body, err := a.Artist(context.Background(), someMBID, "x"); err != nil || string(body) != "t" { - t.Errorf("%q, %v", body, err) - } - }() - } - // Let the fetch through once every caller has had time to queue on it. - time.Sleep(50 * time.Millisecond) - close(src.gate) - wg.Wait() - if src.calls.Load() != 1 { - t.Errorf("calls = %d, want one fetch for five asks", src.calls.Load()) - } -} - -func TestArt_RefusesWhatIsNotAnMBID(t *testing.T) { - dir := t.TempDir() - a := NewArt(dir, sources{artists: []coverart.ArtistArtProvider{&fakeSource{thumb: []byte("t")}}}) - if _, err := a.Artist(context.Background(), "../../etc/passwd", "x"); !errors.Is(err, ErrNoArt) { - t.Fatalf("err = %v", err) - } - if _, err := os.Stat(filepath.Join(dir, "catalogue-art")); !os.IsNotExist(err) { - t.Error("nothing should be written for a refused id") - } - if _, err := NewArt(dir, nil).Artist(context.Background(), someMBID, "x"); !errors.Is(err, ErrNoArt) { - t.Errorf("no sources: err = %v", err) - } -} diff --git a/internal/catalogue/catalogue.go b/internal/catalogue/catalogue.go index ada4fe47..f9adb9ef 100644 --- a/internal/catalogue/catalogue.go +++ b/internal/catalogue/catalogue.go @@ -1,7 +1,6 @@ -// Package catalogue is Minstrel's own record of what artists have released, -// read from MusicBrainz and kept in the database. The wanted -// list, search and import read it; MusicBrainz is asked only when what is -// held is missing or old. +// Package catalogue keeps MusicBrainz's releases and track lists for the +// albums the library holds. The duplicate resolver reads them to tell a song +// filed in the wrong slot from one legitimately repeated. package catalogue import ( @@ -9,34 +8,22 @@ import ( "errors" "fmt" "log/slog" - "time" "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" "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" ) -const ( - // DiscographyMaxAge is how long an artist's list of release groups is - // trusted before it is read again. New releases appear on this cadence. - DiscographyMaxAge = 7 * 24 * time.Hour - // ReleasesMaxAge is how long a release group's editions and track lists - // are trusted. They change rarely once a group is out. - ReleasesMaxAge = 30 * 24 * time.Hour -) - -// ErrNotFound: MusicBrainz has no artist or release group under that id. +// ErrNotFound: MusicBrainz has no release group under that id. var ErrNotFound = errors.New("catalogue: not in MusicBrainz") -// Service reads and refreshes the catalogue. +// Service reads release groups from MusicBrainz into the catalogue. type Service struct { pool *pgxpool.Pool mb *musicbrainz.Client logger *slog.Logger - now func() time.Time } // New makes a Service. A nil client talks to the public MusicBrainz. @@ -47,91 +34,7 @@ func New(pool *pgxpool.Pool, mb *musicbrainz.Client, logger *slog.Logger) *Servi if logger == nil { logger = slog.Default() } - return &Service{pool: pool, mb: mb, logger: logger, now: time.Now} -} - -// MusicBrainz is the client the service asks, for callers that search. -func (s *Service) MusicBrainz() *musicbrainz.Client { return s.mb } - -// Discography is an artist and the release groups credited to them. -type Discography struct { - Artist dbq.MbArtist - Groups []dbq.MbReleaseGroup - // Stale: MusicBrainz could not be asked, so this is the last copy read. - Stale bool -} - -// Discography returns the artist's release groups, reading them from -// MusicBrainz first when they have never been read or are older than -// DiscographyMaxAge. When MusicBrainz is unreachable, a copy already held is -// returned marked Stale; with nothing held, the error is returned. -func (s *Service) Discography(ctx context.Context, artistMBID string) (Discography, error) { - return s.DiscographyWithin(ctx, artistMBID, DiscographyMaxAge) -} - -// DiscographyWithin is Discography with the age past which the held copy is -// read again: a followed artist is checked for new releases more often than -// one the catalogue merely knows. -func (s *Service) DiscographyWithin(ctx context.Context, artistMBID string, maxAge time.Duration) (Discography, error) { - q := dbq.New(s.pool) - artist, err := q.GetMBArtist(ctx, artistMBID) - held := err == nil && artist.DiscographyRefreshedAt.Valid - if err != nil && !errors.Is(err, pgx.ErrNoRows) { - return Discography{}, fmt.Errorf("catalogue: read artist: %w", err) - } - stale := false - if !held || s.now().Sub(artist.DiscographyRefreshedAt.Time) > maxAge { - if rerr := s.RefreshArtist(ctx, artistMBID); rerr != nil { - if !held || !errors.Is(rerr, musicbrainz.ErrUnreachable) { - return Discography{}, rerr - } - stale = true - } - if artist, err = q.GetMBArtist(ctx, artistMBID); err != nil { - return Discography{}, fmt.Errorf("catalogue: read artist: %w", err) - } - } - groups, err := q.ListMBArtistReleaseGroups(ctx, artistMBID) - if err != nil { - return Discography{}, fmt.Errorf("catalogue: read discography: %w", err) - } - return Discography{Artist: artist, Groups: groups, Stale: stale}, nil -} - -// RefreshArtist reads the artist and every release group credited to them, -// and replaces the held discography in one transaction. -func (s *Service) RefreshArtist(ctx context.Context, artistMBID string) error { - artist, err := s.mb.Artist(ctx, artistMBID) - if err != nil { - return mbErr(err) - } - groups, err := s.mb.ArtistReleaseGroups(ctx, artistMBID) - if err != nil { - return mbErr(err) - } - return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { - q := dbq.New(tx) - if err := q.UpsertMBArtist(ctx, dbq.UpsertMBArtistParams{ - Mbid: artist.MBID, Name: artist.Name, SortName: artist.SortName, - Disambiguation: artist.Disambiguation, ArtistType: artist.Type, Country: artist.Country, - }); err != nil { - return fmt.Errorf("catalogue: save artist: %w", err) - } - if err := q.DeleteMBArtistReleaseGroups(ctx, artist.MBID); err != nil { - return fmt.Errorf("catalogue: clear discography: %w", err) - } - for _, g := range groups { - if err := upsertGroup(ctx, q, g); err != nil { - return err - } - if err := q.AddMBArtistReleaseGroup(ctx, dbq.AddMBArtistReleaseGroupParams{ - ArtistMbid: artist.MBID, ReleaseGroupMbid: g.MBID, - }); err != nil { - return fmt.Errorf("catalogue: link release group: %w", err) - } - } - return q.MarkMBArtistDiscographyRefreshed(ctx, artist.MBID) - }) + return &Service{pool: pool, mb: mb, logger: logger} } func upsertGroup(ctx context.Context, q *dbq.Queries, g musicbrainz.ReleaseGroup) error { @@ -148,60 +51,6 @@ func upsertGroup(ctx context.Context, q *dbq.Queries, g musicbrainz.ReleaseGroup return nil } -// Edition is one release of a group with its track list. -type Edition struct { - Release dbq.MbRelease - Tracks []dbq.MbReleaseTrack -} - -// Editions is a release group and its releases. -type Editions struct { - Group dbq.MbReleaseGroup - Editions []Edition - Stale bool -} - -// Releases returns a release group's releases with their track lists, read -// from MusicBrainz first when never read or older than ReleasesMaxAge. Like -// Discography, an unreachable MusicBrainz serves the held copy as Stale. -func (s *Service) Releases(ctx context.Context, groupMBID string) (Editions, error) { - q := dbq.New(s.pool) - group, err := q.GetMBReleaseGroup(ctx, groupMBID) - if err != nil && !errors.Is(err, pgx.ErrNoRows) { - return Editions{}, fmt.Errorf("catalogue: read release group: %w", err) - } - held := err == nil && group.ReleasesRefreshedAt.Valid - stale := false - if !held || s.now().Sub(group.ReleasesRefreshedAt.Time) > ReleasesMaxAge { - if rerr := s.RefreshReleaseGroup(ctx, groupMBID); rerr != nil { - if !held || !errors.Is(rerr, musicbrainz.ErrUnreachable) { - return Editions{}, rerr - } - stale = true - } - if group, err = q.GetMBReleaseGroup(ctx, groupMBID); err != nil { - return Editions{}, fmt.Errorf("catalogue: read release group: %w", err) - } - } - releases, err := q.ListMBReleasesForGroup(ctx, groupMBID) - if err != nil { - return Editions{}, fmt.Errorf("catalogue: read releases: %w", err) - } - tracks, err := q.ListMBReleaseTracksForGroup(ctx, groupMBID) - if err != nil { - return Editions{}, fmt.Errorf("catalogue: read track lists: %w", err) - } - byRelease := make(map[string][]dbq.MbReleaseTrack, len(releases)) - for _, t := range tracks { - byRelease[t.ReleaseMbid] = append(byRelease[t.ReleaseMbid], t) - } - out := Editions{Group: group, Stale: stale} - for _, r := range releases { - out.Editions = append(out.Editions, Edition{Release: r, Tracks: byRelease[r.Mbid]}) - } - return out, nil -} - // RefreshReleaseGroup reads a group's releases and track lists and replaces // the held copy in one transaction. A group not yet held is looked up first. func (s *Service) RefreshReleaseGroup(ctx context.Context, groupMBID string) error { @@ -254,60 +103,6 @@ func (s *Service) RefreshReleaseGroup(ctx context.Context, groupMBID string) err }) } -// RefreshDue re-reads up to limit discographies that are older than -// DiscographyMaxAge, never-read first. It stops at the first unreachable -// MusicBrainz, since the rest would fail the same way. -func (s *Service) RefreshDue(ctx context.Context, limit int) (refreshed int, err error) { - due, err := dbq.New(s.pool).ListMBArtistsDueForRefresh(ctx, dbq.ListMBArtistsDueForRefreshParams{ - Before: pgtype.Timestamptz{Time: s.now().Add(-DiscographyMaxAge), Valid: true}, - MaxRows: int32(limit), - }) - if err != nil { - return 0, fmt.Errorf("catalogue: list due artists: %w", err) - } - for _, mbid := range due { - if err := s.RefreshArtist(ctx, mbid); err != nil { - if errors.Is(err, musicbrainz.ErrUnreachable) || ctx.Err() != nil { - return refreshed, err - } - // One artist MusicBrainz cannot describe (merged away, say) - // must not stop the rest. - s.logger.Warn("catalogue: refresh artist", "mbid", mbid, "err", err) - continue - } - refreshed++ - } - return refreshed, nil -} - -// RefreshInterval is how often Run looks for discographies due a re-read, -// and RefreshBatch how many it reads per look: 20 artists is about a minute -// of MusicBrainz's budget. -const ( - RefreshInterval = time.Hour - RefreshBatch = 20 -) - -// Run refreshes due discographies every RefreshInterval until ctx ends. -func (s *Service) Run(ctx context.Context) { - ticker := time.NewTicker(RefreshInterval) - defer ticker.Stop() - for { - select { - case <-ctx.Done(): - return - case <-ticker.C: - } - n, err := s.RefreshDue(ctx, RefreshBatch) - switch { - case err != nil && ctx.Err() == nil: - s.logger.Warn("catalogue: refresh pass stopped", "refreshed", n, "err", err) - case n > 0: - s.logger.Info("catalogue: refresh pass complete", "refreshed", n) - } - } -} - // mbErr maps MusicBrainz's not-found to the catalogue's; anything else passes // through so callers can tell an unreachable MusicBrainz apart. func mbErr(err error) error { diff --git a/internal/catalogue/catalogue_test.go b/internal/catalogue/catalogue_test.go index 5b495122..76021a6f 100644 --- a/internal/catalogue/catalogue_test.go +++ b/internal/catalogue/catalogue_test.go @@ -9,14 +9,13 @@ import ( "net/http" "net/http/httptest" "os" - "strings" "sync/atomic" "testing" - "time" "github.com/jackc/pgx/v5/pgxpool" "git.fabledsword.com/bvandeusen/minstrel/internal/db" + "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" ) @@ -44,15 +43,13 @@ func newPool(t *testing.T) *pgxpool.Pool { const ( gorillaz = "e21857d5-3256-4547-afb3-4b6ded592596" - deLaSoul = "4f2a6b9e-9ae6-4f34-9a41-3e9f2f7c4c1d" demonDays = "0c4d5b5f-2f3c-3d31-9c4e-4b4f6b4c1a01" - feelGood = "0c4d5b5f-2f3c-3d31-9c4e-4b4f6b4c1a02" // credited to both demonDaysR = "1a2b3c4d-0000-4000-8000-000000000001" demonDaysJ = "1a2b3c4d-0000-4000-8000-000000000002" ) -// fakeMB is a MusicBrainz with two artists, a collaboration between them, -// and one album with two editions. down makes every request a 503. +// fakeMB is a MusicBrainz with one album in two editions. down makes every +// request a 503. type fakeMB struct { srv *httptest.Server calls atomic.Int32 @@ -70,21 +67,6 @@ func newFakeMB(t *testing.T) *fakeMB { } q := r.URL.Query() switch { - case r.URL.Path == "/artist/"+gorillaz: - _, _ = fmt.Fprintf(w, `{"id": %q, "name": "Gorillaz", "sort-name": "Gorillaz", "type": "Group", "country": "GB"}`, gorillaz) - case r.URL.Path == "/artist/"+deLaSoul: - _, _ = fmt.Fprintf(w, `{"id": %q, "name": "De La Soul", "type": "Group", "country": "US"}`, deLaSoul) - case r.URL.Path == "/release-group" && q.Get("artist") == gorillaz: - _, _ = fmt.Fprintf(w, `{"release-group-count": 2, "release-groups": [ - {"id": %q, "title": "Demon Days", "primary-type": "Album", "first-release-date": "2005-05-11", - "artist-credit": [{"name": "Gorillaz", "artist": {"id": %q}}]}, - {"id": %q, "title": "Feel Good Inc.", "primary-type": "Single", "secondary-types": [], "first-release-date": "2005-05-09", - "artist-credit": [{"name": "Gorillaz", "joinphrase": " feat. ", "artist": {"id": %q}}, {"name": "De La Soul", "artist": {"id": %q}}]} - ]}`, demonDays, gorillaz, feelGood, gorillaz, deLaSoul) - case r.URL.Path == "/release-group" && q.Get("artist") == deLaSoul: - _, _ = fmt.Fprintf(w, `{"release-group-count": 1, "release-groups": [ - {"id": %q, "title": "Feel Good Inc.", "primary-type": "Single", "first-release-date": "2005-05-09"} - ]}`, feelGood) case r.URL.Path == "/release-group/"+demonDays: _, _ = fmt.Fprintf(w, `{"id": %q, "title": "Demon Days", "primary-type": "Album", "first-release-date": "2005-05-11", "artist-credit": [{"name": "Gorillaz", "artist": {"id": %q}}]}`, demonDays, gorillaz) @@ -110,138 +92,60 @@ func (f *fakeMB) client() *musicbrainz.Client { return &musicbrainz.Client{BaseURL: f.srv.URL, Pace: &musicbrainz.Pacer{}, Retries: -1} } -func TestDiscography_ReadsOnceThenServesTheHeldCopy_Integration(t *testing.T) { - ctx := context.Background() - mb := newFakeMB(t) - s := New(newPool(t), mb.client(), nil) - - d, err := s.Discography(ctx, gorillaz) - if err != nil { - t.Fatal(err) - } - if d.Artist.Name != "Gorillaz" || d.Artist.Country != "GB" || len(d.Groups) != 2 || d.Stale { - t.Fatalf("discography = %+v", d) - } - // Oldest first: the single came out two days before the album. - if d.Groups[0].Mbid != feelGood || d.Groups[1].PrimaryType != "Album" { - t.Errorf("order = %s, %s", d.Groups[0].Title, d.Groups[1].Title) - } - if d.Groups[0].ArtistCredit != "Gorillaz feat. De La Soul" { - t.Errorf("credit = %q", d.Groups[0].ArtistCredit) - } - - before := mb.calls.Load() - if _, err := s.Discography(ctx, gorillaz); err != nil { - t.Fatal(err) - } - if mb.calls.Load() != before { - t.Error("a fresh discography should be served without asking MusicBrainz") - } - - // The collaboration belongs to both artists. - dls, err := s.Discography(ctx, deLaSoul) - if err != nil { - t.Fatal(err) - } - if len(dls.Groups) != 1 || dls.Groups[0].Mbid != feelGood { - t.Errorf("De La Soul's discography = %+v", dls.Groups) - } - if again, _ := s.Discography(ctx, gorillaz); len(again.Groups) != 2 { - t.Error("refreshing one artist must not take a shared group from the other") - } -} - -func TestDiscography_UnreachableServesStaleOrFails_Integration(t *testing.T) { +func TestRefreshReleaseGroup_ReadsEditionsAndTrackLists_Integration(t *testing.T) { ctx := context.Background() mb := newFakeMB(t) pool := newPool(t) s := New(pool, mb.client(), nil) - - mb.down.Store(true) - if _, err := s.Discography(ctx, gorillaz); !errors.Is(err, musicbrainz.ErrUnreachable) { - t.Fatalf("nothing held and MusicBrainz down: err = %v, want ErrUnreachable", err) - } - - mb.down.Store(false) - if _, err := s.Discography(ctx, gorillaz); err != nil { - t.Fatal(err) - } - // A week and a day later, MusicBrainz is down: the held copy is served. - s.now = func() time.Time { return time.Now().Add(DiscographyMaxAge + 24*time.Hour) } - mb.down.Store(true) - d, err := s.Discography(ctx, gorillaz) - if err != nil || !d.Stale || len(d.Groups) != 2 { - t.Fatalf("stale copy: %+v, %v", d, err) - } -} - -func TestDiscography_UnknownArtistIsNotFound_Integration(t *testing.T) { - s := New(newPool(t), newFakeMB(t).client(), nil) - if _, err := s.Discography(context.Background(), "00000000-0000-4000-8000-000000000000"); !errors.Is(err, ErrNotFound) { - t.Fatalf("err = %v, want ErrNotFound", err) - } -} - -func TestReleases_ReadsEditionsAndTrackLists_Integration(t *testing.T) { - ctx := context.Background() - mb := newFakeMB(t) - s := New(newPool(t), mb.client(), nil) + q := dbq.New(pool) // The group isn't held yet: it is looked up first. - e, err := s.Releases(ctx, demonDays) + if err := s.RefreshReleaseGroup(ctx, demonDays); err != nil { + t.Fatal(err) + } + group, err := q.GetMBReleaseGroup(ctx, demonDays) if err != nil { t.Fatal(err) } - if e.Group.Title != "Demon Days" || len(e.Editions) != 2 { - t.Fatalf("editions = %+v", e) + if group.Title != "Demon Days" || !group.ReleasesRefreshedAt.Valid { + t.Fatalf("group = %+v", group) } - official := e.Editions[0] - if official.Release.Mbid != demonDaysR || official.Release.TrackCount != 2 || official.Release.Formats != "CD" { - t.Errorf("the official edition should come first: %+v", official.Release) - } - if len(official.Tracks) != 2 || official.Tracks[1].RecordingMbid != "rec-lls" || official.Tracks[1].LengthMs != 190000 { - t.Errorf("tracks = %+v", official.Tracks) - } - - before := mb.calls.Load() - if _, err := s.Releases(ctx, demonDays); err != nil { + tracks, err := q.ListMBReleaseTracksForGroup(ctx, demonDays) + if err != nil { t.Fatal(err) } - if mb.calls.Load() != before { - t.Error("fresh editions should be served without asking MusicBrainz") + if len(tracks) != 3 { + t.Fatalf("want 3 tracks across both editions, got %+v", tracks) } + var lls *dbq.MbReleaseTrack + for i := range tracks { + if tracks[i].RecordingMbid == "rec-lls" { + lls = &tracks[i] + } + } + if lls == nil || lls.ReleaseMbid != demonDaysR || lls.LengthMs != 190000 { + t.Errorf("Last Living Souls = %+v", lls) + } + // Refreshing replaces rather than adds. if err := s.RefreshReleaseGroup(ctx, demonDays); err != nil { t.Fatal(err) } - if again, _ := s.Releases(ctx, demonDays); len(again.Editions) != 2 || len(again.Editions[0].Tracks) != 2 { - t.Errorf("after a refresh: %+v", again) + if again, _ := q.ListMBReleaseTracksForGroup(ctx, demonDays); len(again) != 3 { + t.Errorf("after a refresh: %d tracks, want 3", len(again)) } } -func TestRefreshDue_ReadsWhatIsOldAndStopsWhenMusicBrainzIsDown_Integration(t *testing.T) { +func TestRefreshReleaseGroup_UnknownAndUnreachable_Integration(t *testing.T) { ctx := context.Background() mb := newFakeMB(t) s := New(newPool(t), mb.client(), nil) - for _, a := range []string{gorillaz, deLaSoul} { - if err := s.RefreshArtist(ctx, a); err != nil { - t.Fatal(err) - } - } - if n, err := s.RefreshDue(ctx, 10); err != nil || n != 0 { - t.Fatalf("nothing is due yet: %d, %v", n, err) - } - s.now = func() time.Time { return time.Now().Add(DiscographyMaxAge + time.Hour) } - if n, err := s.RefreshDue(ctx, 1); err != nil || n != 1 { - t.Errorf("one due, limit one: %d, %v", n, err) + if err := s.RefreshReleaseGroup(ctx, "00000000-0000-4000-8000-000000000000"); !errors.Is(err, ErrNotFound) { + t.Errorf("unknown group: err = %v, want ErrNotFound", err) } mb.down.Store(true) - n, err := s.RefreshDue(ctx, 10) - if !errors.Is(err, musicbrainz.ErrUnreachable) || n != 0 { - t.Errorf("MusicBrainz down: %d, %v", n, err) - } - if !strings.Contains(err.Error(), "503") { - t.Errorf("the error should say what MusicBrainz answered: %v", err) + if err := s.RefreshReleaseGroup(ctx, demonDays); !errors.Is(err, musicbrainz.ErrUnreachable) { + t.Errorf("MusicBrainz down: err = %v, want ErrUnreachable", err) } } diff --git a/internal/config/config.go b/internal/config/config.go index 645a21e3..1f52ef7a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -99,7 +99,7 @@ func Default() Config { Log: LogConfig{Level: "info", Format: "text"}, Branding: BrandingConfig{ AppName: "Minstrel", - Description: "Self-hosted music server with Subsonic compatibility, smart shuffle, and music acquisition.", + Description: "Self-hosted music server with Subsonic compatibility and smart shuffle.", }, Events: EventsConfig{ SessionTimeoutMinutes: 30, diff --git a/internal/db/dbq/acquisition.sql.go b/internal/db/dbq/acquisition.sql.go deleted file mode 100644 index d5ff1b48..00000000 --- a/internal/db/dbq/acquisition.sql.go +++ /dev/null @@ -1,362 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: acquisition.sql - -package dbq - -import ( - "context" - - "github.com/jackc/pgx/v5/pgtype" -) - -const createAcquisitionService = `-- name: CreateAcquisitionService :one -INSERT INTO acquisition_services - (code, kind, protocol, label, category, timeout_ms, searches_per_hour) -VALUES - ($1, $2, $3, $4, - $5, $6, $7) -ON CONFLICT (code) DO NOTHING -RETURNING code, kind, protocol, label, enabled, base_url, api_key, username, password, category, timeout_ms, serves_search, serves_downloads, serves_enrichment, local_path, remote_path, searches_per_hour, created_at, updated_at -` - -type CreateAcquisitionServiceParams struct { - Code string - Kind string - Protocol string - Label string - Category string - TimeoutMs int32 - SearchesPerHour int32 -} - -// ON CONFLICT so the caller can try the next code when this one is taken. -func (q *Queries) CreateAcquisitionService(ctx context.Context, arg CreateAcquisitionServiceParams) (AcquisitionService, error) { - row := q.db.QueryRow(ctx, createAcquisitionService, - arg.Code, - arg.Kind, - arg.Protocol, - arg.Label, - arg.Category, - arg.TimeoutMs, - arg.SearchesPerHour, - ) - var i AcquisitionService - err := row.Scan( - &i.Code, - &i.Kind, - &i.Protocol, - &i.Label, - &i.Enabled, - &i.BaseUrl, - &i.ApiKey, - &i.Username, - &i.Password, - &i.Category, - &i.TimeoutMs, - &i.ServesSearch, - &i.ServesDownloads, - &i.ServesEnrichment, - &i.LocalPath, - &i.RemotePath, - &i.SearchesPerHour, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const deleteAcquisitionService = `-- name: DeleteAcquisitionService :execrows -DELETE FROM acquisition_services WHERE code = $1 -` - -func (q *Queries) DeleteAcquisitionService(ctx context.Context, code string) (int64, error) { - result, err := q.db.Exec(ctx, deleteAcquisitionService, code) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const getAcquisitionService = `-- name: GetAcquisitionService :one -SELECT code, kind, protocol, label, enabled, base_url, api_key, username, password, category, timeout_ms, serves_search, serves_downloads, serves_enrichment, local_path, remote_path, searches_per_hour, created_at, updated_at FROM acquisition_services WHERE code = $1 -` - -// The full row, credentials included. For building a client, never for a -// response. -func (q *Queries) GetAcquisitionService(ctx context.Context, code string) (AcquisitionService, error) { - row := q.db.QueryRow(ctx, getAcquisitionService, code) - var i AcquisitionService - err := row.Scan( - &i.Code, - &i.Kind, - &i.Protocol, - &i.Label, - &i.Enabled, - &i.BaseUrl, - &i.ApiKey, - &i.Username, - &i.Password, - &i.Category, - &i.TimeoutMs, - &i.ServesSearch, - &i.ServesDownloads, - &i.ServesEnrichment, - &i.LocalPath, - &i.RemotePath, - &i.SearchesPerHour, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const listAcquisitionServices = `-- name: ListAcquisitionServices :many - -SELECT code, kind, protocol, label, enabled, base_url, - (api_key <> '')::boolean AS has_api_key, - (password <> '')::boolean AS has_password, - username, category, timeout_ms, - serves_search, serves_downloads, serves_enrichment, - local_path, remote_path, searches_per_hour, - created_at, updated_at -FROM acquisition_services -ORDER BY kind, label, code -` - -type ListAcquisitionServicesRow struct { - Code string - Kind string - Protocol string - Label string - Enabled bool - BaseUrl string - HasApiKey bool - HasPassword bool - Username string - Category string - TimeoutMs int32 - ServesSearch bool - ServesDownloads bool - ServesEnrichment bool - LocalPath string - RemotePath string - SearchesPerHour int32 - CreatedAt pgtype.Timestamptz - UpdatedAt pgtype.Timestamptz -} - -// Indexers and download clients. -// Returns no secret, only whether one is set: this feeds the settings page. -func (q *Queries) ListAcquisitionServices(ctx context.Context) ([]ListAcquisitionServicesRow, error) { - rows, err := q.db.Query(ctx, listAcquisitionServices) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListAcquisitionServicesRow - for rows.Next() { - var i ListAcquisitionServicesRow - if err := rows.Scan( - &i.Code, - &i.Kind, - &i.Protocol, - &i.Label, - &i.Enabled, - &i.BaseUrl, - &i.HasApiKey, - &i.HasPassword, - &i.Username, - &i.Category, - &i.TimeoutMs, - &i.ServesSearch, - &i.ServesDownloads, - &i.ServesEnrichment, - &i.LocalPath, - &i.RemotePath, - &i.SearchesPerHour, - &i.CreatedAt, - &i.UpdatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listUsableAcquisitionServices = `-- name: ListUsableAcquisitionServices :many -SELECT code, kind, protocol, label, enabled, base_url, api_key, username, password, category, timeout_ms, serves_search, serves_downloads, serves_enrichment, local_path, remote_path, searches_per_hour, created_at, updated_at FROM acquisition_services -WHERE enabled AND base_url <> '' - AND CASE WHEN protocol = 'exchange' THEN - CASE $1::text - WHEN 'search' THEN serves_search - WHEN 'download' THEN serves_downloads - WHEN 'enrichment' THEN serves_enrichment - ELSE false - END - ELSE kind = $1::text - END -ORDER BY code -` - -// Every enabled, addressed service doing one job. An exchange row does a job -// when its switch for it is on; any other row does the job its kind names. -// Ordered by code so a fan-out is deterministic. -func (q *Queries) ListUsableAcquisitionServices(ctx context.Context, job string) ([]AcquisitionService, error) { - rows, err := q.db.Query(ctx, listUsableAcquisitionServices, job) - if err != nil { - return nil, err - } - defer rows.Close() - var items []AcquisitionService - for rows.Next() { - var i AcquisitionService - if err := rows.Scan( - &i.Code, - &i.Kind, - &i.Protocol, - &i.Label, - &i.Enabled, - &i.BaseUrl, - &i.ApiKey, - &i.Username, - &i.Password, - &i.Category, - &i.TimeoutMs, - &i.ServesSearch, - &i.ServesDownloads, - &i.ServesEnrichment, - &i.LocalPath, - &i.RemotePath, - &i.SearchesPerHour, - &i.CreatedAt, - &i.UpdatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const setAcquisitionServiceAPIKey = `-- name: SetAcquisitionServiceAPIKey :execrows -UPDATE acquisition_services SET api_key = $1, updated_at = now() -WHERE code = $2 -` - -type SetAcquisitionServiceAPIKeyParams struct { - ApiKey string - Code string -} - -func (q *Queries) SetAcquisitionServiceAPIKey(ctx context.Context, arg SetAcquisitionServiceAPIKeyParams) (int64, error) { - result, err := q.db.Exec(ctx, setAcquisitionServiceAPIKey, arg.ApiKey, arg.Code) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const setAcquisitionServicePassword = `-- name: SetAcquisitionServicePassword :execrows -UPDATE acquisition_services SET password = $1, updated_at = now() -WHERE code = $2 -` - -type SetAcquisitionServicePasswordParams struct { - Password string - Code string -} - -func (q *Queries) SetAcquisitionServicePassword(ctx context.Context, arg SetAcquisitionServicePasswordParams) (int64, error) { - result, err := q.db.Exec(ctx, setAcquisitionServicePassword, arg.Password, arg.Code) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const updateAcquisitionService = `-- name: UpdateAcquisitionService :one -UPDATE acquisition_services SET - label = $1, - enabled = $2, - base_url = $3, - username = $4, - category = $5, - timeout_ms = $6, - serves_search = $7, - serves_downloads = $8, - serves_enrichment = $9, - local_path = $10, - remote_path = $11, - searches_per_hour = $12, - updated_at = now() -WHERE code = $13 -RETURNING code, kind, protocol, label, enabled, base_url, api_key, username, password, category, timeout_ms, serves_search, serves_downloads, serves_enrichment, local_path, remote_path, searches_per_hour, created_at, updated_at -` - -type UpdateAcquisitionServiceParams struct { - Label string - Enabled bool - BaseUrl string - Username string - Category string - TimeoutMs int32 - ServesSearch bool - ServesDownloads bool - ServesEnrichment bool - LocalPath string - RemotePath string - SearchesPerHour int32 - Code string -} - -// Everything on the settings form except the credentials, which have their -// own write-only calls. -func (q *Queries) UpdateAcquisitionService(ctx context.Context, arg UpdateAcquisitionServiceParams) (AcquisitionService, error) { - row := q.db.QueryRow(ctx, updateAcquisitionService, - arg.Label, - arg.Enabled, - arg.BaseUrl, - arg.Username, - arg.Category, - arg.TimeoutMs, - arg.ServesSearch, - arg.ServesDownloads, - arg.ServesEnrichment, - arg.LocalPath, - arg.RemotePath, - arg.SearchesPerHour, - arg.Code, - ) - var i AcquisitionService - err := row.Scan( - &i.Code, - &i.Kind, - &i.Protocol, - &i.Label, - &i.Enabled, - &i.BaseUrl, - &i.ApiKey, - &i.Username, - &i.Password, - &i.Category, - &i.TimeoutMs, - &i.ServesSearch, - &i.ServesDownloads, - &i.ServesEnrichment, - &i.LocalPath, - &i.RemotePath, - &i.SearchesPerHour, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} diff --git a/internal/db/dbq/admin_status.sql.go b/internal/db/dbq/admin_status.sql.go index 84b47755..8446813d 100644 --- a/internal/db/dbq/admin_status.sql.go +++ b/internal/db/dbq/admin_status.sql.go @@ -11,42 +11,6 @@ import ( "github.com/jackc/pgx/v5/pgtype" ) -const downloadsStatus = `-- name: DownloadsStatus :one -SELECT COUNT(*) FILTER (WHERE ended_at IS NULL AND import_state IN ('', 'downloading', 'waiting', 'importing'))::bigint AS running, - COUNT(*) FILTER (WHERE ended_at IS NULL AND import_state IN ('review', 'failed'))::bigint AS needs_you, - MIN(COALESCE(import_updated_at, grabbed_at)) FILTER (WHERE ended_at IS NULL AND import_state IN ('review', 'failed'))::timestamptz AS needs_you_since, - COUNT(*) FILTER (WHERE imported_at > now() - INTERVAL '24 hours')::bigint AS imported_day, - COUNT(*) FILTER (WHERE ended_at > now() - INTERVAL '24 hours' - AND end_reason IN ('stalled', 'failed', 'gone', 'abandoned'))::bigint AS given_up_day - FROM grabs - WHERE ended_at IS NULL - OR ended_at > now() - INTERVAL '24 hours' - OR imported_at > now() - INTERVAL '24 hours' -` - -type DownloadsStatusRow struct { - Running int64 - NeedsYou int64 - NeedsYouSince pgtype.Timestamptz - ImportedDay int64 - GivenUpDay int64 -} - -// Downloads at a glance: running, waiting on a person (and since when), and -// the last day's imports and give-ups. -func (q *Queries) DownloadsStatus(ctx context.Context) (DownloadsStatusRow, error) { - row := q.db.QueryRow(ctx, downloadsStatus) - var i DownloadsStatusRow - err := row.Scan( - &i.Running, - &i.NeedsYou, - &i.NeedsYouSince, - &i.ImportedDay, - &i.GivenUpDay, - ) - return i, err -} - const libraryStatus = `-- name: LibraryStatus :one SELECT COUNT(*)::bigint AS tracks, COUNT(DISTINCT album_id)::bigint AS albums, @@ -109,8 +73,8 @@ type ListListeningNowRow struct { AlbumTitle string } -// The admin Overview's status sections: who is listening, what is -// downloading, what the library holds. Each is one cheap read. +// The admin Overview's status sections: who is listening and what the +// library holds. Each is one cheap read. // Plays in progress: open play events still inside their track's length, // with a few minutes' grace for a pause. A client that never says a play // ended would otherwise read as listening until the 24-hour sweep closes it. diff --git a/internal/db/dbq/admin_summary.sql.go b/internal/db/dbq/admin_summary.sql.go index 91860ec5..95d1ee77 100644 --- a/internal/db/dbq/admin_summary.sql.go +++ b/internal/db/dbq/admin_summary.sql.go @@ -13,8 +13,6 @@ import ( const adminSummary = `-- name: AdminSummary :one SELECT - (SELECT count(*) FROM requests WHERE status = 'pending')::bigint AS requests, - (SELECT min(requested_at) FROM requests WHERE status = 'pending')::timestamptz AS requests_since, -- A broken track is one report however it was found: a listener flagged -- it, or it failed to play. Counted by track, as the Reports -- page lists them. @@ -41,8 +39,6 @@ SELECT ` type AdminSummaryRow struct { - Requests int64 - RequestsSince pgtype.Timestamptz Reports int64 ReportsSince pgtype.Timestamptz Duplicates int64 @@ -62,8 +58,6 @@ func (q *Queries) AdminSummary(ctx context.Context) (AdminSummaryRow, error) { row := q.db.QueryRow(ctx, adminSummary) var i AdminSummaryRow err := row.Scan( - &i.Requests, - &i.RequestsSince, &i.Reports, &i.ReportsSince, &i.Duplicates, diff --git a/internal/db/dbq/albums.sql.go b/internal/db/dbq/albums.sql.go index fc3c8768..639f9a81 100644 --- a/internal/db/dbq/albums.sql.go +++ b/internal/db/dbq/albums.sql.go @@ -267,22 +267,6 @@ func (q *Queries) GetAlbumsByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([ return items, nil } -const getReleaseGroupForReleaseMbid = `-- name: GetReleaseGroupForReleaseMbid :one -SELECT release_group_mbid::text - FROM albums - WHERE mbid = $1 AND release_group_mbid IS NOT NULL - LIMIT 1 -` - -// The release group the library already knows for a release id, so a -// request carrying a release id can be repaired without asking MusicBrainz. -func (q *Queries) GetReleaseGroupForReleaseMbid(ctx context.Context, mbid *string) (string, error) { - row := q.db.QueryRow(ctx, getReleaseGroupForReleaseMbid, mbid) - var release_group_mbid string - err := row.Scan(&release_group_mbid) - return release_group_mbid, err -} - const listAlbumsAlphaByArtist = `-- name: ListAlbumsAlphaByArtist :many SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.sort_name AS artist_sort_name FROM albums @@ -1015,25 +999,6 @@ func (q *Queries) SetAlbumReleaseGroupMbidIfNull(ctx context.Context, arg SetAlb return err } -const setReleaseGroupForReleaseMbidIfNull = `-- name: SetReleaseGroupForReleaseMbidIfNull :exec -UPDATE albums - SET release_group_mbid = $1::text, - updated_at = now() - WHERE mbid = $2::text AND release_group_mbid IS NULL -` - -type SetReleaseGroupForReleaseMbidIfNullParams struct { - ReleaseGroupMbid string - ReleaseMbid string -} - -// Cache a MusicBrainz-resolved release group onto the album row that -// holds the release, filling only a NULL. -func (q *Queries) SetReleaseGroupForReleaseMbidIfNull(ctx context.Context, arg SetReleaseGroupForReleaseMbidIfNullParams) error { - _, err := q.db.Exec(ctx, setReleaseGroupForReleaseMbidIfNull, arg.ReleaseGroupMbid, arg.ReleaseMbid) - return err -} - const upsertAlbum = `-- name: UpsertAlbum :one INSERT INTO albums (title, sort_title, artist_id, release_date, mbid, cover_art_path, release_group_mbid) VALUES ($1, $2, $3, $4, $5, $6, $7) diff --git a/internal/db/dbq/candidate_artist_tags.sql.go b/internal/db/dbq/candidate_artist_tags.sql.go deleted file mode 100644 index 12a9ceba..00000000 --- a/internal/db/dbq/candidate_artist_tags.sql.go +++ /dev/null @@ -1,228 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: candidate_artist_tags.sql - -package dbq - -import ( - "context" -) - -const countCandidateArtistTagCoverage = `-- name: CountCandidateArtistTagCoverage :one -SELECT count(*)::bigint AS processed, - count(*) FILTER (WHERE tag_source <> 'none')::bigint AS with_tags - FROM candidate_artist_tag_state -` - -type CountCandidateArtistTagCoverageRow struct { - Processed int64 - WithTags int64 -} - -// Operator-facing coverage: how many distinct candidates have been processed, -// and how many of those actually yielded tags. The gap is the honest ceiling -// from the task — obscure artists with no MBID presence or no upstream tags -// stay thin no matter how long the worker runs, and that is worth being able -// to see rather than inferring from a silent surface. -func (q *Queries) CountCandidateArtistTagCoverage(ctx context.Context) (CountCandidateArtistTagCoverageRow, error) { - row := q.db.QueryRow(ctx, countCandidateArtistTagCoverage) - var i CountCandidateArtistTagCoverageRow - err := row.Scan(&i.Processed, &i.WithTags) - return i, err -} - -const deleteCandidateArtistTags = `-- name: DeleteCandidateArtistTags :exec -DELETE FROM candidate_artist_tags WHERE candidate_mbid = $1 -` - -// Clear a candidate's cached tags before rewriting (atomic replace by the -// caller, same shape as DeleteTrackTags). -func (q *Queries) DeleteCandidateArtistTags(ctx context.Context, candidateMbid string) error { - _, err := q.db.Exec(ctx, deleteCandidateArtistTags, candidateMbid) - return err -} - -const gcDeleteOrphanedCandidateArtistTagState = `-- name: GcDeleteOrphanedCandidateArtistTagState :execrows -DELETE FROM candidate_artist_tag_state s - WHERE NOT EXISTS ( - SELECT 1 FROM artist_similarity_unmatched u - WHERE u.candidate_mbid = s.candidate_mbid - ) - OR EXISTS (SELECT 1 FROM artists a WHERE a.mbid = s.candidate_mbid) -` - -// Same sweep for the bookkeeping rows. Kept as a separate statement rather -// than a cascade: the two tables are independent by design (a 'none' outcome -// has state but no tags), so neither can be the parent of the other. -func (q *Queries) GcDeleteOrphanedCandidateArtistTagState(ctx context.Context) (int64, error) { - result, err := q.db.Exec(ctx, gcDeleteOrphanedCandidateArtistTagState) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const gcDeleteOrphanedCandidateArtistTags = `-- name: GcDeleteOrphanedCandidateArtistTags :execrows -DELETE FROM candidate_artist_tags t - WHERE NOT EXISTS ( - SELECT 1 FROM artist_similarity_unmatched u - WHERE u.candidate_mbid = t.candidate_mbid - ) - OR EXISTS (SELECT 1 FROM artists a WHERE a.mbid = t.candidate_mbid) -` - -// Drops cached tags for candidates that no longer appear in the similarity -// feed, or that have since been added to the library (their tags now live in -// track_tags). The feed is refetched periodically and churns, so without this -// the cache only ever grows. -func (q *Queries) GcDeleteOrphanedCandidateArtistTags(ctx context.Context) (int64, error) { - result, err := q.db.Exec(ctx, gcDeleteOrphanedCandidateArtistTags) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const insertCandidateArtistTag = `-- name: InsertCandidateArtistTag :exec -INSERT INTO candidate_artist_tags (candidate_mbid, tag, weight) -VALUES ($1, $2, $3) -ON CONFLICT (candidate_mbid, tag) - DO UPDATE SET weight = GREATEST(candidate_artist_tags.weight, EXCLUDED.weight) -` - -type InsertCandidateArtistTagParams struct { - CandidateMbid string - Tag string - Weight float64 -} - -// Upsert one (candidate, tag); keep the stronger weight when two providers -// agree on a tag with different folksonomy strengths. -func (q *Queries) InsertCandidateArtistTag(ctx context.Context, arg InsertCandidateArtistTagParams) error { - _, err := q.db.Exec(ctx, insertCandidateArtistTag, arg.CandidateMbid, arg.Tag, arg.Weight) - return err -} - -const listCandidateArtistTagsForMbids = `-- name: ListCandidateArtistTagsForMbids :many -SELECT candidate_mbid, tag, weight - FROM candidate_artist_tags - WHERE candidate_mbid = ANY($1::text[]) -` - -// Cached tags for a set of candidates, for the taste-overlap ranking. -// One row per (candidate, tag). -func (q *Queries) ListCandidateArtistTagsForMbids(ctx context.Context, dollar_1 []string) ([]CandidateArtistTag, error) { - rows, err := q.db.Query(ctx, listCandidateArtistTagsForMbids, dollar_1) - if err != nil { - return nil, err - } - defer rows.Close() - var items []CandidateArtistTag - for rows.Next() { - var i CandidateArtistTag - if err := rows.Scan(&i.CandidateMbid, &i.Tag, &i.Weight); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listCandidateArtistsMissingTags = `-- name: ListCandidateArtistsMissingTags :many - -SELECT u.candidate_mbid, - coalesce(max(u.candidate_name), '')::text AS candidate_name, - sum(u.score)::float8 AS total_score - FROM artist_similarity_unmatched u - LEFT JOIN candidate_artist_tag_state s ON s.candidate_mbid = u.candidate_mbid - WHERE NOT EXISTS (SELECT 1 FROM artists a WHERE a.mbid = u.candidate_mbid) - AND ( - s.candidate_mbid IS NULL - OR (s.tag_source = 'none' AND s.tag_sources_version < $1) - ) - GROUP BY u.candidate_mbid - ORDER BY total_score DESC, u.candidate_mbid - LIMIT $2 -` - -type ListCandidateArtistsMissingTagsParams struct { - TagSourcesVersion int32 - Limit int32 -} - -type ListCandidateArtistsMissingTagsRow struct { - CandidateMbid string - CandidateName string - TotalScore float64 -} - -// Folksonomy tags for out-of-library Discover candidates. Parallel to -// track_tags.sql, but keyed by MBID because the artist has no local row. See -// 0050_candidate_artist_tags.up.sql for why the bookkeeping is its own table. -// Candidates eligible for tag enrichment: never processed (no state row) or -// settled 'none' under an older provider version. -// -// artist_similarity_unmatched holds one row per (seed, candidate, source), so -// this GROUPs to one row per candidate — enriching the same MBID once per seed -// that pointed at it would multiply the API calls for no gain. -// -// ORDER BY summed similarity DESC is the load-bearing part. The candidate pool -// is O(library artists x neighbours per artist) — thousands — and MusicBrainz -// allows ~1 req/s, so it can NEVER be fully enriched in one pass. Draining in -// strength order means the candidates most likely to actually reach a user's -// deck get tags first, and the long tail fills in over subsequent ticks -// instead of starving behind it. -// -// Already-in-library candidates are skipped: they have an artists row, so -// their tags belong in track_tags, and the suggestion query filters them out -// anyway. $1 = current tag_sources_version, $2 = limit. -// candidate_name is coalesced to the empty string so it lands non-nullable in -// Go: the name is only a Last.fm lookup key, and empty simply means "MBID-keyed -// providers only", which the provider chain already handles. max() is an -// arbitrary-but-deterministic pick when several seeds spell one MBID -// differently. -func (q *Queries) ListCandidateArtistsMissingTags(ctx context.Context, arg ListCandidateArtistsMissingTagsParams) ([]ListCandidateArtistsMissingTagsRow, error) { - rows, err := q.db.Query(ctx, listCandidateArtistsMissingTags, arg.TagSourcesVersion, arg.Limit) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListCandidateArtistsMissingTagsRow - for rows.Next() { - var i ListCandidateArtistsMissingTagsRow - if err := rows.Scan(&i.CandidateMbid, &i.CandidateName, &i.TotalScore); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const setCandidateArtistTagState = `-- name: SetCandidateArtistTagState :exec -INSERT INTO candidate_artist_tag_state (candidate_mbid, tag_source, tag_sources_version) -VALUES ($1, $2, $3) -ON CONFLICT (candidate_mbid) DO UPDATE - SET tag_source = EXCLUDED.tag_source, - tag_sources_version = EXCLUDED.tag_sources_version, - updated_at = now() -` - -type SetCandidateArtistTagStateParams struct { - CandidateMbid string - TagSource string - TagSourcesVersion int32 -} - -// Stamp the enrichment outcome so the drainer skips settled candidates. -// $2 = 'musicbrainz' | 'lastfm' | 'mixed' | 'none', $3 = current version. -func (q *Queries) SetCandidateArtistTagState(ctx context.Context, arg SetCandidateArtistTagStateParams) error { - _, err := q.db.Exec(ctx, setCandidateArtistTagState, arg.CandidateMbid, arg.TagSource, arg.TagSourcesVersion) - return err -} diff --git a/internal/db/dbq/catalogue.sql.go b/internal/db/dbq/catalogue.sql.go index 018c0727..47b9d2bc 100644 --- a/internal/db/dbq/catalogue.sql.go +++ b/internal/db/dbq/catalogue.sql.go @@ -7,35 +7,8 @@ package dbq import ( "context" - - "github.com/jackc/pgx/v5/pgtype" ) -const addMBArtistReleaseGroup = `-- name: AddMBArtistReleaseGroup :exec -INSERT INTO mb_artist_release_groups (artist_mbid, release_group_mbid) -VALUES ($1, $2) -ON CONFLICT DO NOTHING -` - -type AddMBArtistReleaseGroupParams struct { - ArtistMbid string - ReleaseGroupMbid string -} - -func (q *Queries) AddMBArtistReleaseGroup(ctx context.Context, arg AddMBArtistReleaseGroupParams) error { - _, err := q.db.Exec(ctx, addMBArtistReleaseGroup, arg.ArtistMbid, arg.ReleaseGroupMbid) - return err -} - -const deleteMBArtistReleaseGroups = `-- name: DeleteMBArtistReleaseGroups :exec -DELETE FROM mb_artist_release_groups WHERE artist_mbid = $1 -` - -func (q *Queries) DeleteMBArtistReleaseGroups(ctx context.Context, artistMbid string) error { - _, err := q.db.Exec(ctx, deleteMBArtistReleaseGroups, artistMbid) - return err -} - const deleteMBReleasesForGroup = `-- name: DeleteMBReleasesForGroup :exec DELETE FROM mb_releases WHERE release_group_mbid = $1 ` @@ -46,27 +19,6 @@ func (q *Queries) DeleteMBReleasesForGroup(ctx context.Context, releaseGroupMbid return err } -const getMBArtist = `-- name: GetMBArtist :one -SELECT mbid, name, sort_name, disambiguation, artist_type, country, discography_refreshed_at, created_at, updated_at FROM mb_artists WHERE mbid = $1 -` - -func (q *Queries) GetMBArtist(ctx context.Context, mbid string) (MbArtist, error) { - row := q.db.QueryRow(ctx, getMBArtist, mbid) - var i MbArtist - err := row.Scan( - &i.Mbid, - &i.Name, - &i.SortName, - &i.Disambiguation, - &i.ArtistType, - &i.Country, - &i.DiscographyRefreshedAt, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - const getMBReleaseGroup = `-- name: GetMBReleaseGroup :one SELECT mbid, title, primary_type, secondary_types, first_release_date, artist_credit, releases_refreshed_at, created_at, updated_at FROM mb_release_groups WHERE mbid = $1 ` @@ -165,78 +117,6 @@ func (q *Queries) InsertMBReleaseTrack(ctx context.Context, arg InsertMBReleaseT return err } -const listMBArtistReleaseGroups = `-- name: ListMBArtistReleaseGroups :many -SELECT g.mbid, g.title, g.primary_type, g.secondary_types, g.first_release_date, g.artist_credit, g.releases_refreshed_at, g.created_at, g.updated_at FROM mb_release_groups g -JOIN mb_artist_release_groups ag ON ag.release_group_mbid = g.mbid -WHERE ag.artist_mbid = $1 -ORDER BY g.first_release_date = '', g.first_release_date, g.title, g.mbid -` - -// An artist's discography, oldest first; undated groups last. -func (q *Queries) ListMBArtistReleaseGroups(ctx context.Context, artistMbid string) ([]MbReleaseGroup, error) { - rows, err := q.db.Query(ctx, listMBArtistReleaseGroups, artistMbid) - if err != nil { - return nil, err - } - defer rows.Close() - var items []MbReleaseGroup - for rows.Next() { - var i MbReleaseGroup - if err := rows.Scan( - &i.Mbid, - &i.Title, - &i.PrimaryType, - &i.SecondaryTypes, - &i.FirstReleaseDate, - &i.ArtistCredit, - &i.ReleasesRefreshedAt, - &i.CreatedAt, - &i.UpdatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listMBArtistsDueForRefresh = `-- name: ListMBArtistsDueForRefresh :many -SELECT mbid FROM mb_artists -WHERE discography_refreshed_at IS NULL OR discography_refreshed_at < $1 -ORDER BY discography_refreshed_at NULLS FIRST, mbid -LIMIT $2 -` - -type ListMBArtistsDueForRefreshParams struct { - Before pgtype.Timestamptz - MaxRows int32 -} - -// Artists whose discography has never been read, or was read before the -// cutoff: never-read first, then oldest first. -func (q *Queries) ListMBArtistsDueForRefresh(ctx context.Context, arg ListMBArtistsDueForRefreshParams) ([]string, error) { - rows, err := q.db.Query(ctx, listMBArtistsDueForRefresh, arg.Before, arg.MaxRows) - if err != nil { - return nil, err - } - defer rows.Close() - var items []string - for rows.Next() { - var mbid string - if err := rows.Scan(&mbid); err != nil { - return nil, err - } - items = append(items, mbid) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - const listMBReleaseTracksForGroup = `-- name: ListMBReleaseTracksForGroup :many SELECT t.release_mbid, t.medium_position, t.position, t.medium_format, t.number, t.title, t.length_ms, t.recording_mbid FROM mb_release_tracks t JOIN mb_releases r ON r.mbid = t.release_mbid @@ -273,53 +153,6 @@ func (q *Queries) ListMBReleaseTracksForGroup(ctx context.Context, releaseGroupM return items, nil } -const listMBReleasesForGroup = `-- name: ListMBReleasesForGroup :many -SELECT mbid, release_group_mbid, title, status, release_date, country, disambiguation, formats, track_count, created_at FROM mb_releases -WHERE release_group_mbid = $1 -ORDER BY status <> 'Official', release_date = '', release_date, title, mbid -` - -// Official editions first, then by date; undated last. -func (q *Queries) ListMBReleasesForGroup(ctx context.Context, releaseGroupMbid string) ([]MbRelease, error) { - rows, err := q.db.Query(ctx, listMBReleasesForGroup, releaseGroupMbid) - if err != nil { - return nil, err - } - defer rows.Close() - var items []MbRelease - for rows.Next() { - var i MbRelease - if err := rows.Scan( - &i.Mbid, - &i.ReleaseGroupMbid, - &i.Title, - &i.Status, - &i.ReleaseDate, - &i.Country, - &i.Disambiguation, - &i.Formats, - &i.TrackCount, - &i.CreatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const markMBArtistDiscographyRefreshed = `-- name: MarkMBArtistDiscographyRefreshed :exec -UPDATE mb_artists SET discography_refreshed_at = now() WHERE mbid = $1 -` - -func (q *Queries) MarkMBArtistDiscographyRefreshed(ctx context.Context, mbid string) error { - _, err := q.db.Exec(ctx, markMBArtistDiscographyRefreshed, mbid) - return err -} - const markMBReleaseGroupReleasesRefreshed = `-- name: MarkMBReleaseGroupReleasesRefreshed :exec UPDATE mb_release_groups SET releases_refreshed_at = now() WHERE mbid = $1 ` @@ -329,43 +162,8 @@ func (q *Queries) MarkMBReleaseGroupReleasesRefreshed(ctx context.Context, mbid return err } -const upsertMBArtist = `-- name: UpsertMBArtist :exec - -INSERT INTO mb_artists (mbid, name, sort_name, disambiguation, artist_type, country) -VALUES ($1, $2, $3, $4, - $5, $6) -ON CONFLICT (mbid) DO UPDATE SET - name = EXCLUDED.name, - sort_name = EXCLUDED.sort_name, - disambiguation = EXCLUDED.disambiguation, - artist_type = EXCLUDED.artist_type, - country = EXCLUDED.country, - updated_at = now() -` - -type UpsertMBArtistParams struct { - Mbid string - Name string - SortName string - Disambiguation string - ArtistType string - Country string -} - -// The release catalogue read from MusicBrainz. -func (q *Queries) UpsertMBArtist(ctx context.Context, arg UpsertMBArtistParams) error { - _, err := q.db.Exec(ctx, upsertMBArtist, - arg.Mbid, - arg.Name, - arg.SortName, - arg.Disambiguation, - arg.ArtistType, - arg.Country, - ) - return err -} - const upsertMBReleaseGroup = `-- name: UpsertMBReleaseGroup :exec + INSERT INTO mb_release_groups (mbid, title, primary_type, secondary_types, first_release_date, artist_credit) VALUES ($1, $2, $3, $4::text[], $5, $6) @@ -387,8 +185,9 @@ type UpsertMBReleaseGroupParams struct { ArtistCredit string } -// Leaves releases_refreshed_at alone: re-reading a discography says nothing -// about whether the group's track lists are current. +// The release catalogue read from MusicBrainz. +// Leaves releases_refreshed_at alone: only a full read of the group's +// releases and track lists marks it. func (q *Queries) UpsertMBReleaseGroup(ctx context.Context, arg UpsertMBReleaseGroupParams) error { _, err := q.db.Exec(ctx, upsertMBReleaseGroup, arg.Mbid, diff --git a/internal/db/dbq/imports.sql.go b/internal/db/dbq/imports.sql.go deleted file mode 100644 index b7ce6aec..00000000 --- a/internal/db/dbq/imports.sql.go +++ /dev/null @@ -1,902 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: imports.sql - -package dbq - -import ( - "context" - - "github.com/jackc/pgx/v5/pgtype" -) - -const claimGrabImport = `-- name: ClaimGrabImport :one -UPDATE grabs - SET import_state = 'importing', import_attempts = import_attempts + 1, - import_updated_at = now(), progress = 1 - WHERE id = $1 AND ended_at IS NULL AND import_state IN ('', 'downloading', 'waiting') -RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at -` - -// Takes a finished download for import. The update is the lock: a second -// pass finds nothing to claim. -func (q *Queries) ClaimGrabImport(ctx context.Context, id pgtype.UUID) (Grab, error) { - row := q.db.QueryRow(ctx, claimGrabImport, id) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const clearTrackSourceVerdict = `-- name: ClearTrackSourceVerdict :exec -UPDATE tracks SET source_verdict = NULL, source_verdict_at = NULL - WHERE id = $1 AND source_verdict = 'suspect' -` - -// A track whose file was replaced in place is no longer the rip it was. -func (q *Queries) ClearTrackSourceVerdict(ctx context.Context, id pgtype.UUID) error { - _, err := q.db.Exec(ctx, clearTrackSourceVerdict, id) - return err -} - -const finishUpgrade = `-- name: FinishUpgrade :exec -UPDATE wants - SET album_id = COALESCE($1, album_id), - upgrade = '', - upgrade_checked_at = NULL, - updated_at = now() - WHERE id = $2 -` - -type FinishUpgradeParams struct { - AlbumID pgtype.UUID - ID pgtype.UUID -} - -// An upgrade was imported: the want holds the album the new tracks are in, -// and is judged again by the next upgrade check. -func (q *Queries) FinishUpgrade(ctx context.Context, arg FinishUpgradeParams) error { - _, err := q.db.Exec(ctx, finishUpgrade, arg.AlbumID, arg.ID) - return err -} - -const forceGrabImport = `-- name: ForceGrabImport :one -UPDATE grabs - SET import_state = '', import_forced = true, import_reason = '', import_updated_at = now() - WHERE id = $1 AND ended_at IS NULL AND import_state IN ('review', 'failed') -RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at -` - -// "Import anyway": the next pass imports the download whatever the check said. -func (q *Queries) ForceGrabImport(ctx context.Context, id pgtype.UUID) (Grab, error) { - row := q.db.QueryRow(ctx, forceGrabImport, id) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const getGrab = `-- name: GetGrab :one -SELECT id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at FROM grabs WHERE id = $1 -` - -func (q *Queries) GetGrab(ctx context.Context, id pgtype.UUID) (Grab, error) { - row := q.db.QueryRow(ctx, getGrab, id) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const heldPositions = `-- name: HeldPositions :many -SELECT DISTINCT COALESCE(t.disc_number, 1)::int AS disc, t.track_number::int AS track - FROM tracks t - JOIN albums a ON a.id = t.album_id - WHERE a.release_group_mbid = $1 - AND t.missing_since IS NULL AND t.track_number IS NOT NULL -` - -type HeldPositionsRow struct { - Disc int32 - Track int32 -} - -// The disc and track positions the library holds for an album of this group. -func (q *Queries) HeldPositions(ctx context.Context, releaseGroupMbid *string) ([]HeldPositionsRow, error) { - rows, err := q.db.Query(ctx, heldPositions, releaseGroupMbid) - if err != nil { - return nil, err - } - defer rows.Close() - var items []HeldPositionsRow - for rows.Next() { - var i HeldPositionsRow - if err := rows.Scan(&i.Disc, &i.Track); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const heldRecordings = `-- name: HeldRecordings :many -SELECT DISTINCT mbid::text AS mbid FROM tracks - WHERE mbid = ANY($1::text[]) AND missing_since IS NULL -` - -// Which of these recordings the library holds a file for. -func (q *Queries) HeldRecordings(ctx context.Context, recordings []string) ([]string, error) { - rows, err := q.db.Query(ctx, heldRecordings, recordings) - if err != nil { - return nil, err - } - defer rows.Close() - var items []string - for rows.Next() { - var mbid string - if err := rows.Scan(&mbid); err != nil { - return nil, err - } - items = append(items, mbid) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listGrabs = `-- name: ListGrabs :many -SELECT g.id, g.want_id, g.name, g.indexer, g.client, g.quality, g.origin, g.forced, - g.grabbed_at, g.ended_at, g.end_reason, g.end_detail, g.progress, g.import_state, - g.import_reason, g.import_detail, g.import_updated_at, g.imported_at, g.imported_files, - g.destination, g.seeding, g.ratio, g.seed_released_at, - w.title AS want_title, w.artist_name AS want_artist, - di.trigger AS condition, di.detail AS condition_detail, di.remediations AS condition_retries, - di.first_seen AS condition_since - FROM grabs g - LEFT JOIN wants w ON w.id = g.want_id - LEFT JOIN download_interventions di - ON di.id = (SELECT x.id FROM download_interventions x - WHERE x.grab_id = g.id AND x.outcome = 'open' - ORDER BY x.last_seen DESC LIMIT 1) - ORDER BY (g.ended_at IS NULL AND g.import_state IN ('review', 'failed')) DESC, - g.ended_at IS NULL DESC, g.grabbed_at DESC, g.id - LIMIT $2 OFFSET $1 -` - -type ListGrabsParams struct { - Skip int32 - MaxRows int32 -} - -type ListGrabsRow struct { - ID pgtype.UUID - WantID pgtype.UUID - Name string - Indexer string - Client string - Quality string - Origin string - Forced bool - GrabbedAt pgtype.Timestamptz - EndedAt pgtype.Timestamptz - EndReason string - EndDetail string - Progress float32 - ImportState string - ImportReason string - ImportDetail []byte - ImportUpdatedAt pgtype.Timestamptz - ImportedAt pgtype.Timestamptz - ImportedFiles int32 - Destination string - Seeding bool - Ratio float32 - SeedReleasedAt pgtype.Timestamptz - WantTitle *string - WantArtist *string - Condition *string - ConditionDetail *string - ConditionRetries *int32 - ConditionSince pgtype.Timestamptz -} - -// The Wanted view's Activity tab: downloads waiting on a person first, then -// the rest still running, then the ended ones newest first. Each carries -// what is wrong with it now, if anything. -func (q *Queries) ListGrabs(ctx context.Context, arg ListGrabsParams) ([]ListGrabsRow, error) { - rows, err := q.db.Query(ctx, listGrabs, arg.Skip, arg.MaxRows) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListGrabsRow - for rows.Next() { - var i ListGrabsRow - if err := rows.Scan( - &i.ID, - &i.WantID, - &i.Name, - &i.Indexer, - &i.Client, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - &i.WantTitle, - &i.WantArtist, - &i.Condition, - &i.ConditionDetail, - &i.ConditionRetries, - &i.ConditionSince, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listImportedGrabsHolding = `-- name: ListImportedGrabsHolding :many -SELECT id, info_hash, name, want_id - FROM grabs - WHERE import_state = 'imported' - AND info_hash <> '' - AND destination <> '' - AND starts_with($1::text, rtrim(destination, '/') || '/') -` - -type ListImportedGrabsHoldingRow struct { - ID pgtype.UUID - InfoHash string - Name string - WantID pgtype.UUID -} - -// The downloads Minstrel imported into the folder holding path: the releases a -// file there came from, to block when the files proved not to be the album -// . A file imported before Minstrel downloaded anything has none. -func (q *Queries) ListImportedGrabsHolding(ctx context.Context, path string) ([]ListImportedGrabsHoldingRow, error) { - rows, err := q.db.Query(ctx, listImportedGrabsHolding, path) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListImportedGrabsHoldingRow - for rows.Next() { - var i ListImportedGrabsHoldingRow - if err := rows.Scan( - &i.ID, - &i.InfoHash, - &i.Name, - &i.WantID, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listLiveGrabs = `-- name: ListLiveGrabs :many - -SELECT g.id, g.want_id, g.info_hash, g.name, g.key, g.indexer, g.source, g.client, g.save_path, g.quality, g.origin, g.forced, g.grabbed_by, g.grabbed_at, g.ended_at, g.end_reason, g.end_detail, g.progress, g.import_state, g.import_reason, g.import_detail, g.import_forced, g.import_attempts, g.import_updated_at, g.imported_at, g.imported_files, g.destination, g.seeding, g.ratio, g.seed_released_at, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title AS want_title, - w.reason AS want_reason, w.album_id AS want_album_id - FROM grabs g - LEFT JOIN wants w ON w.id = g.want_id - WHERE g.ended_at IS NULL - ORDER BY g.grabbed_at, g.id -` - -type ListLiveGrabsRow struct { - ID pgtype.UUID - WantID pgtype.UUID - InfoHash string - Name string - Key string - Indexer string - Source string - Client string - SavePath string - Quality string - Origin string - Forced bool - GrabbedBy pgtype.UUID - GrabbedAt pgtype.Timestamptz - EndedAt pgtype.Timestamptz - EndReason string - EndDetail string - Progress float32 - ImportState string - ImportReason string - ImportDetail []byte - ImportForced bool - ImportAttempts int32 - ImportUpdatedAt pgtype.Timestamptz - ImportedAt pgtype.Timestamptz - ImportedFiles int32 - Destination string - Seeding bool - Ratio float32 - SeedReleasedAt pgtype.Timestamptz - ReleaseGroupMbid *string - ArtistMbid *string - ArtistName *string - WantTitle *string - WantReason *string - WantAlbumID pgtype.UUID -} - -// Import with verification. -// Every download still running or waiting on its import, oldest first. -func (q *Queries) ListLiveGrabs(ctx context.Context) ([]ListLiveGrabsRow, error) { - rows, err := q.db.Query(ctx, listLiveGrabs) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListLiveGrabsRow - for rows.Next() { - var i ListLiveGrabsRow - if err := rows.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - &i.ReleaseGroupMbid, - &i.ArtistMbid, - &i.ArtistName, - &i.WantTitle, - &i.WantReason, - &i.WantAlbumID, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listReleaseBlocksPage = `-- name: ListReleaseBlocksPage :many -SELECT b.info_hash, b.name, b.reason, b.detail, b.blocked_at, b.expires_at, w.title AS want_title, - w.artist_name AS want_artist - FROM release_blocks b - LEFT JOIN wants w ON w.id = b.want_id - WHERE b.expires_at IS NULL OR b.expires_at > now() - ORDER BY b.blocked_at DESC, b.info_hash - LIMIT $2 OFFSET $1 -` - -type ListReleaseBlocksPageParams struct { - Skip int32 - MaxRows int32 -} - -type ListReleaseBlocksPageRow struct { - InfoHash string - Name string - Reason string - Detail string - BlockedAt pgtype.Timestamptz - ExpiresAt pgtype.Timestamptz - WantTitle *string - WantArtist *string -} - -func (q *Queries) ListReleaseBlocksPage(ctx context.Context, arg ListReleaseBlocksPageParams) ([]ListReleaseBlocksPageRow, error) { - rows, err := q.db.Query(ctx, listReleaseBlocksPage, arg.Skip, arg.MaxRows) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListReleaseBlocksPageRow - for rows.Next() { - var i ListReleaseBlocksPageRow - if err := rows.Scan( - &i.InfoHash, - &i.Name, - &i.Reason, - &i.Detail, - &i.BlockedAt, - &i.ExpiresAt, - &i.WantTitle, - &i.WantArtist, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listUpgradeTracks = `-- name: ListUpgradeTracks :many -SELECT id, file_path, mbid, COALESCE(disc_number, 1)::int AS disc, track_number, - file_format, file_size, duration_ms, source_verdict - FROM tracks - WHERE album_id = $1 AND missing_since IS NULL -` - -type ListUpgradeTracksRow struct { - ID pgtype.UUID - FilePath string - Mbid *string - Disc int32 - TrackNumber *int32 - FileFormat string - FileSize int64 - DurationMs int32 - SourceVerdict *string -} - -// The tracks an upgrade may replace: the present tracks of the album -// the want holds. -func (q *Queries) ListUpgradeTracks(ctx context.Context, albumID pgtype.UUID) ([]ListUpgradeTracksRow, error) { - rows, err := q.db.Query(ctx, listUpgradeTracks, albumID) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListUpgradeTracksRow - for rows.Next() { - var i ListUpgradeTracksRow - if err := rows.Scan( - &i.ID, - &i.FilePath, - &i.Mbid, - &i.Disc, - &i.TrackNumber, - &i.FileFormat, - &i.FileSize, - &i.DurationMs, - &i.SourceVerdict, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const markWantSearched = `-- name: MarkWantSearched :exec -UPDATE wants SET searched_at = now() WHERE id = $1 -` - -// Imported: nothing to search for until the wanted pass settles the want. -func (q *Queries) MarkWantSearched(ctx context.Context, id pgtype.UUID) error { - _, err := q.db.Exec(ctx, markWantSearched, id) - return err -} - -const openWantsForGroups = `-- name: OpenWantsForGroups :many -SELECT w.id, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title, w.primary_type, w.first_release_date, w.reason, w.album_id, w.created_by, w.wanted_at, w.fulfilled_at, w.created_at, w.updated_at, w.searched_at, w.search_outcome, w.search_considered, w.best_release, w.best_verdict, w.search_error, w.upgrade, w.held_tier, w.held_quality, w.upgrade_checked_at FROM wants w - WHERE w.fulfilled_at IS NULL AND w.release_group_mbid = ANY($1::text[]) - AND NOT EXISTS (SELECT 1 FROM grabs g WHERE g.want_id = w.id AND g.ended_at IS NULL) -` - -// Open wants for these release groups with no download running. -func (q *Queries) OpenWantsForGroups(ctx context.Context, groups []string) ([]Want, error) { - rows, err := q.db.Query(ctx, openWantsForGroups, groups) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Want - for rows.Next() { - var i Want - if err := rows.Scan( - &i.ID, - &i.ReleaseGroupMbid, - &i.ArtistMbid, - &i.ArtistName, - &i.Title, - &i.PrimaryType, - &i.FirstReleaseDate, - &i.Reason, - &i.AlbumID, - &i.CreatedBy, - &i.WantedAt, - &i.FulfilledAt, - &i.CreatedAt, - &i.UpdatedAt, - &i.SearchedAt, - &i.SearchOutcome, - &i.SearchConsidered, - &i.BestRelease, - &i.BestVerdict, - &i.SearchError, - &i.Upgrade, - &i.HeldTier, - &i.HeldQuality, - &i.UpgradeCheckedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const reclaimStaleImports = `-- name: ReclaimStaleImports :execrows -UPDATE grabs - SET import_state = 'waiting', - import_reason = 'The last import was cut short, so it is being tried again.' - WHERE ended_at IS NULL AND import_state = 'importing' - AND import_updated_at < now() - make_interval(mins => $1::int) -` - -// An import cut short (a restart mid-copy) is tried again. -func (q *Queries) ReclaimStaleImports(ctx context.Context, minutes int32) (int64, error) { - result, err := q.db.Exec(ctx, reclaimStaleImports, minutes) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const recordImport = `-- name: RecordImport :one -UPDATE grabs - SET import_state = $1, - import_reason = $2, - import_detail = $3, - import_updated_at = now(), - import_forced = false, - imported_at = CASE WHEN $1::text = 'imported' THEN now() ELSE imported_at END, - -- The files were copied, so an imported download goes on sharing - -- until the lifecycle removes it. - seeding = $1::text = 'imported', - imported_files = $4, - destination = $5, - ended_at = CASE WHEN $6::text <> '' THEN now() ELSE ended_at END, - end_reason = $6, - end_detail = CASE WHEN $6::text <> '' THEN $2::text ELSE end_detail END - WHERE id = $7 -RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at -` - -type RecordImportParams struct { - ImportState string - ImportReason string - ImportDetail []byte - ImportedFiles int32 - Destination string - EndReason string - ID pgtype.UUID -} - -// The import's outcome. An end reason ends the grab: imported, refused or -// held. Without one the grab stays live: review and failed wait for a person. -func (q *Queries) RecordImport(ctx context.Context, arg RecordImportParams) (Grab, error) { - row := q.db.QueryRow(ctx, recordImport, - arg.ImportState, - arg.ImportReason, - arg.ImportDetail, - arg.ImportedFiles, - arg.Destination, - arg.EndReason, - arg.ID, - ) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const rehomeGrab = `-- name: RehomeGrab :one -UPDATE grabs g - SET want_id = $1 - WHERE g.id = $2 AND g.ended_at IS NULL - AND NOT EXISTS (SELECT 1 FROM grabs o WHERE o.want_id = $1 AND o.ended_at IS NULL) -RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at -` - -type RehomeGrabParams struct { - WantID pgtype.UUID - ID pgtype.UUID -} - -// A download that turned out to be another wanted album is that want's grab, -// when the want has none running. -func (q *Queries) RehomeGrab(ctx context.Context, arg RehomeGrabParams) (Grab, error) { - row := q.db.QueryRow(ctx, rehomeGrab, arg.WantID, arg.ID) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const retryGrabImport = `-- name: RetryGrabImport :one -UPDATE grabs - SET import_state = '', import_reason = '', import_updated_at = now() - WHERE id = $1 AND ended_at IS NULL AND import_state = 'failed' -RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at -` - -func (q *Queries) RetryGrabImport(ctx context.Context, id pgtype.UUID) (Grab, error) { - row := q.db.QueryRow(ctx, retryGrabImport, id) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const searchWantAgain = `-- name: SearchWantAgain :exec -UPDATE wants SET searched_at = NULL, search_outcome = '' WHERE id = $1 -` - -// Refused: search again on the next pass. -func (q *Queries) SearchWantAgain(ctx context.Context, id pgtype.UUID) error { - _, err := q.db.Exec(ctx, searchWantAgain, id) - return err -} - -const setGrabProgress = `-- name: SetGrabProgress :exec -UPDATE grabs - SET progress = $1, - import_state = $2, - import_reason = $3, - import_updated_at = now() - WHERE id = $4 AND ended_at IS NULL - AND import_state IN ('', 'downloading', 'waiting') -` - -type SetGrabProgressParams struct { - Progress float32 - ImportState string - ImportReason string - ID pgtype.UUID -} - -// What the download client says of a download not yet finished, or finished -// and waiting for what the import needs. Leaves a claimed, decided or failed -// import alone. -func (q *Queries) SetGrabProgress(ctx context.Context, arg SetGrabProgressParams) error { - _, err := q.db.Exec(ctx, setGrabProgress, - arg.Progress, - arg.ImportState, - arg.ImportReason, - arg.ID, - ) - return err -} diff --git a/internal/db/dbq/library_settings.sql.go b/internal/db/dbq/library_settings.sql.go index 7287bdb4..57baf9e9 100644 --- a/internal/db/dbq/library_settings.sql.go +++ b/internal/db/dbq/library_settings.sql.go @@ -50,45 +50,6 @@ func (q *Queries) RerootAlbumCoverPaths(ctx context.Context, arg RerootAlbumCove return result.RowsAffected(), nil } -const rerootGrabDestinations = `-- name: RerootGrabDestinations :execrows -UPDATE grabs - SET destination = $1::text || substr(destination, length($2::text) + 1) - WHERE destination = $2::text - OR starts_with(destination, $2::text || '/') -` - -type RerootGrabDestinationsParams struct { - NewRoot string - OldRoot string -} - -func (q *Queries) RerootGrabDestinations(ctx context.Context, arg RerootGrabDestinationsParams) (int64, error) { - result, err := q.db.Exec(ctx, rerootGrabDestinations, arg.NewRoot, arg.OldRoot) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const rerootImportRoot = `-- name: RerootImportRoot :execrows -UPDATE acquisition_settings - SET import_root = $1::text - WHERE import_root = $2::text -` - -type RerootImportRootParams struct { - NewRoot string - OldRoot string -} - -func (q *Queries) RerootImportRoot(ctx context.Context, arg RerootImportRootParams) (int64, error) { - result, err := q.db.Exec(ctx, rerootImportRoot, arg.NewRoot, arg.OldRoot) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - const rerootTrackPaths = `-- name: RerootTrackPaths :execrows UPDATE tracks diff --git a/internal/db/dbq/lifecycle.sql.go b/internal/db/dbq/lifecycle.sql.go deleted file mode 100644 index 09aa6f5a..00000000 --- a/internal/db/dbq/lifecycle.sql.go +++ /dev/null @@ -1,408 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: lifecycle.sql - -package dbq - -import ( - "context" - - "github.com/jackc/pgx/v5/pgtype" -) - -const countGoneGrabs = `-- name: CountGoneGrabs :one -SELECT count(*) FROM grabs - WHERE info_hash = $1 AND end_reason = 'gone' AND ended_at > $2 -` - -type CountGoneGrabsParams struct { - InfoHash string - Since pgtype.Timestamptz -} - -// How many of a release's grabs ended gone since a time: a release that keeps -// leaving its client is held back rather than taken on every pass. -func (q *Queries) CountGoneGrabs(ctx context.Context, arg CountGoneGrabsParams) (int64, error) { - row := q.db.QueryRow(ctx, countGoneGrabs, arg.InfoHash, arg.Since) - var count int64 - err := row.Scan(&count) - return count, err -} - -const countRemediation = `-- name: CountRemediation :one -UPDATE download_interventions - SET remediations = remediations + 1, - action = $1, - last_seen = now(), - outcome = CASE WHEN $1::text IN ('given_up', 'removed') THEN 'resolved' ELSE outcome END, - settled_at = CASE WHEN $1::text IN ('given_up', 'removed') THEN now() ELSE settled_at END - WHERE client = $2 AND info_hash = $3 - AND trigger = $4 AND outcome = 'open' -RETURNING remediations -` - -type CountRemediationParams struct { - Action string - Client string - InfoHash string - Trigger string -} - -// Minstrel acted on an open condition. A retry climbs the ladder; giving up -// or removing ends the condition, since what it was about is gone. Written -// before the act, so a retry that happened is never uncounted. -func (q *Queries) CountRemediation(ctx context.Context, arg CountRemediationParams) (int32, error) { - row := q.db.QueryRow(ctx, countRemediation, - arg.Action, - arg.Client, - arg.InfoHash, - arg.Trigger, - ) - var remediations int32 - err := row.Scan(&remediations) - return remediations, err -} - -const deleteExpiredBlocks = `-- name: DeleteExpiredBlocks :execrows -DELETE FROM release_blocks WHERE expires_at IS NOT NULL AND expires_at <= now() -` - -// A release given up on is held back for a while; after that the search may -// take it again. -func (q *Queries) DeleteExpiredBlocks(ctx context.Context) (int64, error) { - result, err := q.db.Exec(ctx, deleteExpiredBlocks) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const giveUpGrab = `-- name: GiveUpGrab :one -UPDATE grabs - SET ended_at = now(), end_reason = $1, end_detail = $2, - import_updated_at = now() - WHERE id = $3 AND ended_at IS NULL AND import_state = ANY($4::text[]) -RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at -` - -type GiveUpGrabParams struct { - EndReason string - EndDetail string - ID pgtype.UUID - States []string -} - -// Ends a running grab, given up on. Only from the states named: the update -// is the lock against an import that claimed the download meanwhile. -func (q *Queries) GiveUpGrab(ctx context.Context, arg GiveUpGrabParams) (Grab, error) { - row := q.db.QueryRow(ctx, giveUpGrab, - arg.EndReason, - arg.EndDetail, - arg.ID, - arg.States, - ) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const listClientsDown = `-- name: ListClientsDown :many -SELECT client, detail, first_seen FROM download_interventions - WHERE trigger = 'client_unreachable' AND outcome = 'open' - ORDER BY client -` - -type ListClientsDownRow struct { - Client string - Detail string - FirstSeen pgtype.Timestamptz -} - -// Download clients not answering now, for the Activity tab. -func (q *Queries) ListClientsDown(ctx context.Context) ([]ListClientsDownRow, error) { - rows, err := q.db.Query(ctx, listClientsDown) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListClientsDownRow - for rows.Next() { - var i ListClientsDownRow - if err := rows.Scan(&i.Client, &i.Detail, &i.FirstSeen); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listLeftovers = `-- name: ListLeftovers :many -SELECT id, client, info_hash, grab_id, name, trigger, action, outcome, detail, observations, remediations, first_seen, last_seen, settled_at FROM download_interventions - WHERE trigger = 'not_removed' AND outcome = 'open' AND info_hash <> '' - ORDER BY first_seen -` - -// Downloads given up on that the client would not remove; each pass asks -// again. -func (q *Queries) ListLeftovers(ctx context.Context) ([]DownloadIntervention, error) { - rows, err := q.db.Query(ctx, listLeftovers) - if err != nil { - return nil, err - } - defer rows.Close() - var items []DownloadIntervention - for rows.Next() { - var i DownloadIntervention - if err := rows.Scan( - &i.ID, - &i.Client, - &i.InfoHash, - &i.GrabID, - &i.Name, - &i.Trigger, - &i.Action, - &i.Outcome, - &i.Detail, - &i.Observations, - &i.Remediations, - &i.FirstSeen, - &i.LastSeen, - &i.SettledAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listSeedingGrabs = `-- name: ListSeedingGrabs :many -SELECT id, client, info_hash, name, imported_at FROM grabs - WHERE seeding AND seed_released_at IS NULL - ORDER BY imported_at, id -` - -type ListSeedingGrabsRow struct { - ID pgtype.UUID - Client string - InfoHash string - Name string - ImportedAt pgtype.Timestamptz -} - -// Imported downloads still sharing, oldest import first. -func (q *Queries) ListSeedingGrabs(ctx context.Context) ([]ListSeedingGrabsRow, error) { - rows, err := q.db.Query(ctx, listSeedingGrabs) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListSeedingGrabsRow - for rows.Next() { - var i ListSeedingGrabsRow - if err := rows.Scan( - &i.ID, - &i.Client, - &i.InfoHash, - &i.Name, - &i.ImportedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const liveGrabOnHash = `-- name: LiveGrabOnHash :one -SELECT EXISTS (SELECT 1 FROM grabs WHERE info_hash = $1 AND ended_at IS NULL)::bool -` - -func (q *Queries) LiveGrabOnHash(ctx context.Context, infoHash string) (bool, error) { - row := q.db.QueryRow(ctx, liveGrabOnHash, infoHash) - var column_1 bool - err := row.Scan(&column_1) - return column_1, err -} - -const raiseIntervention = `-- name: RaiseIntervention :one - -INSERT INTO download_interventions (client, info_hash, grab_id, name, trigger, detail) -VALUES ($1, $2, $3, $4, - $5, $6) -ON CONFLICT (client, info_hash, trigger) WHERE outcome = 'open' DO UPDATE SET - observations = download_interventions.observations + 1, - last_seen = now(), - detail = EXCLUDED.detail, - grab_id = COALESCE(EXCLUDED.grab_id, download_interventions.grab_id), - name = CASE WHEN EXCLUDED.name <> '' THEN EXCLUDED.name ELSE download_interventions.name END -RETURNING id, client, info_hash, grab_id, name, trigger, action, outcome, detail, observations, remediations, first_seen, last_seen, settled_at -` - -type RaiseInterventionParams struct { - Client string - InfoHash string - GrabID pgtype.UUID - Name string - Trigger string - Detail string -} - -// The download lifecycle: the intervention ledger, giving a -// download up, and seeding after import. -// A condition seen, or seen again: a second pass lands on the open row, so -// a download stuck for a week is one row that says how long. The name only -// improves, and the detail is the latest word. -func (q *Queries) RaiseIntervention(ctx context.Context, arg RaiseInterventionParams) (DownloadIntervention, error) { - row := q.db.QueryRow(ctx, raiseIntervention, - arg.Client, - arg.InfoHash, - arg.GrabID, - arg.Name, - arg.Trigger, - arg.Detail, - ) - var i DownloadIntervention - err := row.Scan( - &i.ID, - &i.Client, - &i.InfoHash, - &i.GrabID, - &i.Name, - &i.Trigger, - &i.Action, - &i.Outcome, - &i.Detail, - &i.Observations, - &i.Remediations, - &i.FirstSeen, - &i.LastSeen, - &i.SettledAt, - ) - return i, err -} - -const releaseSeed = `-- name: ReleaseSeed :exec -UPDATE grabs SET seed_released_at = now() WHERE id = $1 AND seed_released_at IS NULL -` - -// The seeding download was removed, or found gone. -func (q *Queries) ReleaseSeed(ctx context.Context, id pgtype.UUID) error { - _, err := q.db.Exec(ctx, releaseSeed, id) - return err -} - -const setGrabRatio = `-- name: SetGrabRatio :exec -UPDATE grabs SET ratio = $1 WHERE id = $2 -` - -type SetGrabRatioParams struct { - Ratio float32 - ID pgtype.UUID -} - -func (q *Queries) SetGrabRatio(ctx context.Context, arg SetGrabRatioParams) error { - _, err := q.db.Exec(ctx, setGrabRatio, arg.Ratio, arg.ID) - return err -} - -const settleEndedGrabs = `-- name: SettleEndedGrabs :execrows -UPDATE download_interventions di - SET outcome = 'resolved', settled_at = now(), last_seen = now() - FROM grabs g - WHERE g.id = di.grab_id AND g.ended_at IS NOT NULL - AND di.outcome = 'open' AND di.trigger <> 'not_removed' -` - -// What was wrong with a download whose grab has ended (imported, refused, -// given up) is over, except a download left in the client to remove. -func (q *Queries) SettleEndedGrabs(ctx context.Context) (int64, error) { - result, err := q.db.Exec(ctx, settleEndedGrabs) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const settleIntervention = `-- name: SettleIntervention :execrows -UPDATE download_interventions - SET outcome = 'resolved', settled_at = now(), last_seen = now() - WHERE client = $1 AND info_hash = $2 - AND trigger = $3 AND outcome = 'open' -` - -type SettleInterventionParams struct { - Client string - InfoHash string - Trigger string -} - -// The condition cleared. -func (q *Queries) SettleIntervention(ctx context.Context, arg SettleInterventionParams) (int64, error) { - result, err := q.db.Exec(ctx, settleIntervention, arg.Client, arg.InfoHash, arg.Trigger) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const settleInterventionsFor = `-- name: SettleInterventionsFor :execrows -UPDATE download_interventions - SET outcome = 'resolved', settled_at = now(), last_seen = now() - WHERE client = $1 AND info_hash = $2 AND outcome = 'open' -` - -type SettleInterventionsForParams struct { - Client string - InfoHash string -} - -// Everything open about one download is over: it is gone from the client. -func (q *Queries) SettleInterventionsFor(ctx context.Context, arg SettleInterventionsForParams) (int64, error) { - result, err := q.db.Exec(ctx, settleInterventionsFor, arg.Client, arg.InfoHash) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} diff --git a/internal/db/dbq/merge.sql.go b/internal/db/dbq/merge.sql.go index eee18b55..e5979740 100644 --- a/internal/db/dbq/merge.sql.go +++ b/internal/db/dbq/merge.sql.go @@ -297,24 +297,6 @@ func (q *Queries) MergeRepointPlaylistTracks(ctx context.Context, arg MergeRepoi return items, nil } -const mergeRepointRequests = `-- name: MergeRepointRequests :execrows -UPDATE requests SET matched_track_id = $1::uuid - WHERE matched_track_id = $2::uuid -` - -type MergeRepointRequestsParams struct { - SurvivorID pgtype.UUID - LoserID pgtype.UUID -} - -func (q *Queries) MergeRepointRequests(ctx context.Context, arg MergeRepointRequestsParams) (int64, error) { - result, err := q.db.Exec(ctx, mergeRepointRequests, arg.SurvivorID, arg.LoserID) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - const mergeRepointSkipEvents = `-- name: MergeRepointSkipEvents :execrows UPDATE skip_events SET track_id = $1::uuid WHERE track_id = $2::uuid ` diff --git a/internal/db/dbq/reacquisition.sql.go b/internal/db/dbq/reacquisition.sql.go deleted file mode 100644 index f82244f6..00000000 --- a/internal/db/dbq/reacquisition.sql.go +++ /dev/null @@ -1,272 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: reacquisition.sql - -package dbq - -import ( - "context" - - "github.com/jackc/pgx/v5/pgtype" -) - -const countAlbumsMissingWithoutMbid = `-- name: CountAlbumsMissingWithoutMbid :one -SELECT COUNT(DISTINCT albums.id)::bigint - FROM albums - JOIN artists ON artists.id = albums.artist_id - JOIN tracks ON tracks.album_id = albums.id - WHERE tracks.missing_since IS NOT NULL - AND ((albums.mbid IS NULL AND albums.release_group_mbid IS NULL) OR artists.mbid IS NULL) -` - -// Albums with missing files that can never be wanted automatically because -// nothing identifies them to MusicBrainz. Surfaced on the admin card so the -// gap is visible: silently doing nothing for these would read as broken. -func (q *Queries) CountAlbumsMissingWithoutMbid(ctx context.Context) (int64, error) { - row := q.db.QueryRow(ctx, countAlbumsMissingWithoutMbid) - var column_1 int64 - err := row.Scan(&column_1) - return column_1, err -} - -const getReacquisitionSettings = `-- name: GetReacquisitionSettings :one - -SELECT id, enabled, grace_hours, max_per_pass FROM reacquisition_settings WHERE id = true -` - -// Re-acquisition of missing files. -// The unit is the album: an album whose files went missing becomes a want for -// its release group, and the search schedule looks for it. -func (q *Queries) GetReacquisitionSettings(ctx context.Context) (ReacquisitionSetting, error) { - row := q.db.QueryRow(ctx, getReacquisitionSettings) - var i ReacquisitionSetting - err := row.Scan( - &i.ID, - &i.Enabled, - &i.GraceHours, - &i.MaxPerPass, - ) - return i, err -} - -const getWantsForAlbums = `-- name: GetWantsForAlbums :many -SELECT a.id AS album_id, w.id AS want_id, w.wanted_at, w.fulfilled_at - FROM albums a - JOIN wants w ON w.album_id = a.id OR w.release_group_mbid = a.release_group_mbid - WHERE a.id = ANY($1::uuid[]) - ORDER BY w.wanted_at DESC -` - -type GetWantsForAlbumsRow struct { - AlbumID pgtype.UUID - WantID pgtype.UUID - WantedAt pgtype.Timestamptz - FulfilledAt pgtype.Timestamptz -} - -// What the wanted list says about each album on the missing-files page. -func (q *Queries) GetWantsForAlbums(ctx context.Context, albumIds []pgtype.UUID) ([]GetWantsForAlbumsRow, error) { - rows, err := q.db.Query(ctx, getWantsForAlbums, albumIds) - if err != nil { - return nil, err - } - defer rows.Close() - var items []GetWantsForAlbumsRow - for rows.Next() { - var i GetWantsForAlbumsRow - if err := rows.Scan( - &i.AlbumID, - &i.WantID, - &i.WantedAt, - &i.FulfilledAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listAlbumsDueReacquisition = `-- name: ListAlbumsDueReacquisition :many -SELECT albums.id AS album_id, - albums.title AS album_title, - albums.mbid AS album_mbid, - albums.release_group_mbid AS album_release_group_mbid, - artists.name AS artist_name, - artists.mbid AS artist_mbid, - COUNT(tracks.id)::bigint AS missing_track_count, - MAX(tracks.missing_since)::timestamptz AS last_missing_since - FROM albums - JOIN artists ON artists.id = albums.artist_id - JOIN tracks ON tracks.album_id = albums.id - WHERE tracks.missing_since IS NOT NULL - AND tracks.missing_since <= now() - make_interval(hours => $1::int) - AND (albums.mbid IS NOT NULL OR albums.release_group_mbid IS NOT NULL) - AND artists.mbid IS NOT NULL - GROUP BY albums.id, albums.title, albums.mbid, albums.release_group_mbid, - artists.name, artists.mbid -HAVING EXISTS ( - SELECT 1 FROM wants w - WHERE w.album_id = albums.id OR w.release_group_mbid = albums.release_group_mbid) - AND NOT EXISTS ( - SELECT 1 FROM wants w - WHERE (w.album_id = albums.id OR w.release_group_mbid = albums.release_group_mbid) - AND (w.fulfilled_at IS NULL OR w.fulfilled_at > MAX(tracks.missing_since))) - ORDER BY MIN(tracks.missing_since), albums.sort_title - LIMIT $2 -` - -type ListAlbumsDueReacquisitionParams struct { - GraceHours int32 - PageLimit int32 -} - -type ListAlbumsDueReacquisitionRow struct { - AlbumID pgtype.UUID - AlbumTitle string - AlbumMbid *string - AlbumReleaseGroupMbid *string - ArtistName string - ArtistMbid *string - MissingTrackCount int64 - LastMissingSince pgtype.Timestamptz -} - -// The sweeper's selection. An album qualifies when: -// -// - it still has at least one track whose file has been missing longer than -// the grace window. Measured on missing_since, which reconcile never -// re-stamps, so it is a genuine "gone since" clock; -// - MusicBrainz can name it: the artist has an MBID, and the album a -// release MBID or a release group. The release MBID is only ever used to -// find the group, and an album matched to a group may carry no release -// id. Albums failing this are counted separately -// (CountAlbumsMissingWithoutMbid); -// - it is on the wanted list: an album taken off the list, or -// dropped into the library by hand, is not fetched back; -// - no want already covers it: none open, and none held since its files -// went missing. A want held after the loss means the library has a full -// copy despite the missing rows, so asking again would loop. -func (q *Queries) ListAlbumsDueReacquisition(ctx context.Context, arg ListAlbumsDueReacquisitionParams) ([]ListAlbumsDueReacquisitionRow, error) { - rows, err := q.db.Query(ctx, listAlbumsDueReacquisition, arg.GraceHours, arg.PageLimit) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListAlbumsDueReacquisitionRow - for rows.Next() { - var i ListAlbumsDueReacquisitionRow - if err := rows.Scan( - &i.AlbumID, - &i.AlbumTitle, - &i.AlbumMbid, - &i.AlbumReleaseGroupMbid, - &i.ArtistName, - &i.ArtistMbid, - &i.MissingTrackCount, - &i.LastMissingSince, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const updateReacquisitionSettings = `-- name: UpdateReacquisitionSettings :one -UPDATE reacquisition_settings - SET enabled = $1, - grace_hours = $2, - max_per_pass = $3 - WHERE id = true -RETURNING id, enabled, grace_hours, max_per_pass -` - -type UpdateReacquisitionSettingsParams struct { - Enabled bool - GraceHours int32 - MaxPerPass int32 -} - -// Whole-row write from the admin card; the CHECKs in migration 0056 are the -// validation, so a bad value fails loudly rather than being clamped silently. -func (q *Queries) UpdateReacquisitionSettings(ctx context.Context, arg UpdateReacquisitionSettingsParams) (ReacquisitionSetting, error) { - row := q.db.QueryRow(ctx, updateReacquisitionSettings, arg.Enabled, arg.GraceHours, arg.MaxPerPass) - var i ReacquisitionSetting - err := row.Scan( - &i.ID, - &i.Enabled, - &i.GraceHours, - &i.MaxPerPass, - ) - return i, err -} - -const wantMissingAlbum = `-- name: WantMissingAlbum :one -INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id) -VALUES ($1, $2, $3, - $4, 'missing', $5) -ON CONFLICT (release_group_mbid) DO UPDATE - SET reason = 'missing', - album_id = EXCLUDED.album_id, - wanted_at = now(), - fulfilled_at = NULL, - updated_at = now() - WHERE wants.fulfilled_at IS NOT NULL -RETURNING id, release_group_mbid, artist_mbid, artist_name, title, primary_type, first_release_date, reason, album_id, created_by, wanted_at, fulfilled_at, created_at, updated_at, searched_at, search_outcome, search_considered, best_release, best_verdict, search_error, upgrade, held_tier, held_quality, upgrade_checked_at -` - -type WantMissingAlbumParams struct { - ReleaseGroupMbid string - ArtistMbid string - ArtistName string - Title string - AlbumID pgtype.UUID -} - -// Records an album with missing files as wanted. A held want for the same -// group is reopened; an open one is left as it is, and no row comes back. -func (q *Queries) WantMissingAlbum(ctx context.Context, arg WantMissingAlbumParams) (Want, error) { - row := q.db.QueryRow(ctx, wantMissingAlbum, - arg.ReleaseGroupMbid, - arg.ArtistMbid, - arg.ArtistName, - arg.Title, - arg.AlbumID, - ) - var i Want - err := row.Scan( - &i.ID, - &i.ReleaseGroupMbid, - &i.ArtistMbid, - &i.ArtistName, - &i.Title, - &i.PrimaryType, - &i.FirstReleaseDate, - &i.Reason, - &i.AlbumID, - &i.CreatedBy, - &i.WantedAt, - &i.FulfilledAt, - &i.CreatedAt, - &i.UpdatedAt, - &i.SearchedAt, - &i.SearchOutcome, - &i.SearchConsidered, - &i.BestRelease, - &i.BestVerdict, - &i.SearchError, - &i.Upgrade, - &i.HeldTier, - &i.HeldQuality, - &i.UpgradeCheckedAt, - ) - return i, err -} diff --git a/internal/db/dbq/recommendation.sql.go b/internal/db/dbq/recommendation.sql.go index 80c8aede..e8c16f90 100644 --- a/internal/db/dbq/recommendation.sql.go +++ b/internal/db/dbq/recommendation.sql.go @@ -1112,152 +1112,3 @@ func (q *Queries) LoadRadioCandidatesV2(ctx context.Context, arg LoadRadioCandid } return items, nil } - -const suggestArtistsForUser = `-- name: SuggestArtistsForUser :many -WITH artist_plays AS ( - -- Completed plays only: counting every play_event would make skipping an - -- artist repeatedly INCREASE its signal and push more of its neighbours - -- at the user. - SELECT t.artist_id, count(*)::bigint AS play_count - FROM play_events pe - JOIN tracks t ON t.id = pe.track_id - WHERE pe.user_id = $1 AND pe.was_skipped = false - GROUP BY t.artist_id -), -profile_seeds AS ( - SELECT tpa.artist_id, tpa.weight AS raw_signal - FROM taste_profile_artists tpa - WHERE tpa.user_id = $1 AND tpa.weight > 0 -), -fallback_seeds AS ( - SELECT a.id AS artist_id, - 5.0 * (CASE WHEN gla.artist_id IS NOT NULL THEN 1 ELSE 0 END) - + COALESCE(SUM(EXP(- EXTRACT(epoch FROM now() - pe.started_at) / ($2::float8 * 86400.0))), 0) - AS raw_signal - FROM artists a - LEFT JOIN general_likes_artists gla ON gla.artist_id = a.id AND gla.user_id = $1 - LEFT JOIN tracks t ON t.artist_id = a.id - LEFT JOIN play_events pe - ON pe.track_id = t.id AND pe.user_id = $1 AND pe.was_skipped = false - WHERE (gla.artist_id IS NOT NULL OR pe.id IS NOT NULL) - AND NOT EXISTS (SELECT 1 FROM profile_seeds) - GROUP BY a.id, gla.artist_id -), -seeds AS ( - SELECT s.artist_id, - ln(1.0 + s.raw_signal) AS signal, - (gla.artist_id IS NOT NULL) AS is_liked, - COALESCE(ap.play_count, 0)::bigint AS play_count - FROM ( - SELECT artist_id, raw_signal FROM profile_seeds - UNION ALL - SELECT artist_id, raw_signal FROM fallback_seeds - ) s - LEFT JOIN general_likes_artists gla - ON gla.artist_id = s.artist_id AND gla.user_id = $1 - LEFT JOIN artist_plays ap ON ap.artist_id = s.artist_id - WHERE s.raw_signal > 0 -), -contributions AS ( - SELECT u.candidate_mbid, - u.candidate_name, - seeds.artist_id AS seed_id, - seeds.is_liked, - seeds.play_count, - seeds.signal * u.score AS contribution - FROM artist_similarity_unmatched u - JOIN seeds ON seeds.artist_id = u.seed_artist_id - WHERE NOT EXISTS (SELECT 1 FROM artists WHERE mbid = u.candidate_mbid) - AND NOT EXISTS ( - SELECT 1 FROM requests r - WHERE r.user_id = $1 - AND r.artist_mbid = u.candidate_mbid - AND r.status NOT IN ('rejected', 'failed') - ) - -- Snoozed by this user and not yet expired. Time-boxed and - -- per-user: the candidate returns on its own once snoozed_until - -- passes, and stays visible to everyone else meanwhile. Deliberately - -- filtered at the candidate stage, NOT folded into the score — a - -- snooze carries no opinion about the music, so it must not become a - -- ranking signal. - AND NOT EXISTS ( - SELECT 1 FROM suggestion_snoozes s - WHERE s.user_id = $1 - AND s.candidate_mbid = u.candidate_mbid - AND s.snoozed_until > now() - ) -) -SELECT candidate_mbid, - candidate_name, - SUM(contribution)::float8 AS total_score, - ((array_agg(seed_id ORDER BY contribution DESC))[1:3])::uuid[] AS top_seed_ids, - ((array_agg(contribution ORDER BY contribution DESC))[1:3])::float8[] AS top_contributions, - ((array_agg(is_liked ORDER BY contribution DESC))[1:3])::boolean[] AS top_is_liked, - ((array_agg(play_count ORDER BY contribution DESC))[1:3])::bigint[] AS top_play_counts -FROM contributions -GROUP BY candidate_mbid, candidate_name -ORDER BY total_score DESC -LIMIT $3 -` - -type SuggestArtistsForUserParams struct { - UserID pgtype.UUID - Column2 float64 - Limit int32 -} - -type SuggestArtistsForUserRow struct { - CandidateMbid string - CandidateName string - TotalScore float64 - TopSeedIds []pgtype.UUID - TopContributions []float64 - TopIsLiked []bool - TopPlayCounts []int64 -} - -// Per-user artist suggestions ranked by taste signal x similarity, projected -// through artist_similarity_unmatched (out-of-library candidates only). -// -// Seeds are TIERED so the surface never empties: -// -// tier 1 - taste_profile_artists.weight: engagement-graded, time-decayed and -// SIGNED by internal/taste, so an artist the user has drifted away -// from stops contributing instead of accumulating forever. -// tier 2 - likes + completed plays, used ONLY when the profile has no rows -// (new account, or before the first daily recompute). -// -// The signal is log-damped: contribution is signal x similarity, and an -// undamped sum would let one heavily-played artist's neighbours take every -// slot -- entrenching harder the MORE the user listened. -// -// Candidates already in the library, already requested and not terminal, or -// snoozed by this user, are excluded. $1=user_id, $2=half_life_days (tier 2 -// decay), $3=limit. -func (q *Queries) SuggestArtistsForUser(ctx context.Context, arg SuggestArtistsForUserParams) ([]SuggestArtistsForUserRow, error) { - rows, err := q.db.Query(ctx, suggestArtistsForUser, arg.UserID, arg.Column2, arg.Limit) - if err != nil { - return nil, err - } - defer rows.Close() - var items []SuggestArtistsForUserRow - for rows.Next() { - var i SuggestArtistsForUserRow - if err := rows.Scan( - &i.CandidateMbid, - &i.CandidateName, - &i.TotalScore, - &i.TopSeedIds, - &i.TopContributions, - &i.TopIsLiked, - &i.TopPlayCounts, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} diff --git a/internal/db/dbq/recommendation_tuning.sql.go b/internal/db/dbq/recommendation_tuning.sql.go index 1e1f76c3..84cb968a 100644 --- a/internal/db/dbq/recommendation_tuning.sql.go +++ b/internal/db/dbq/recommendation_tuning.sql.go @@ -9,22 +9,6 @@ import ( "context" ) -const getDiscoverTuning = `-- name: GetDiscoverTuning :one -SELECT singleton, tag_overlap_weight, snooze_days, updated_at FROM discover_tuning WHERE singleton = true -` - -func (q *Queries) GetDiscoverTuning(ctx context.Context) (DiscoverTuning, error) { - row := q.db.QueryRow(ctx, getDiscoverTuning) - var i DiscoverTuning - err := row.Scan( - &i.Singleton, - &i.TagOverlapWeight, - &i.SnoozeDays, - &i.UpdatedAt, - ) - return i, err -} - const getTasteTuning = `-- name: GetTasteTuning :one SELECT singleton, half_life_days, engagement_hard_skip, engagement_neutral, engagement_full, updated_at, enriched_tag_scale, era_scale, mood_scale FROM taste_tuning WHERE singleton = true ` @@ -134,32 +118,6 @@ func (q *Queries) ListWeightProfiles(ctx context.Context) ([]RecommendationWeigh return items, nil } -const updateDiscoverTuning = `-- name: UpdateDiscoverTuning :one -UPDATE discover_tuning - SET tag_overlap_weight = $1, - snooze_days = $2, - updated_at = now() - WHERE singleton = true -RETURNING singleton, tag_overlap_weight, snooze_days, updated_at -` - -type UpdateDiscoverTuningParams struct { - TagOverlapWeight float64 - SnoozeDays float64 -} - -func (q *Queries) UpdateDiscoverTuning(ctx context.Context, arg UpdateDiscoverTuningParams) (DiscoverTuning, error) { - row := q.db.QueryRow(ctx, updateDiscoverTuning, arg.TagOverlapWeight, arg.SnoozeDays) - var i DiscoverTuning - err := row.Scan( - &i.Singleton, - &i.TagOverlapWeight, - &i.SnoozeDays, - &i.UpdatedAt, - ) - return i, err -} - const updateTasteTuning = `-- name: UpdateTasteTuning :one UPDATE taste_tuning SET half_life_days = $1, @@ -268,24 +226,6 @@ func (q *Queries) UpdateWeightProfile(ctx context.Context, arg UpdateWeightProfi return i, err } -const upsertDiscoverTuningDefaults = `-- name: UpsertDiscoverTuningDefaults :exec -INSERT INTO discover_tuning (singleton, tag_overlap_weight, snooze_days) -VALUES (true, $1, $2) -ON CONFLICT (singleton) DO NOTHING -` - -type UpsertDiscoverTuningDefaultsParams struct { - TagOverlapWeight float64 - SnoozeDays float64 -} - -// Boot reconcile for the Discover scope. Never overwrites -// admin-tuned values, same contract as the other two. -func (q *Queries) UpsertDiscoverTuningDefaults(ctx context.Context, arg UpsertDiscoverTuningDefaultsParams) error { - _, err := q.db.Exec(ctx, upsertDiscoverTuningDefaults, arg.TagOverlapWeight, arg.SnoozeDays) - return err -} - const upsertTasteTuningDefaults = `-- name: UpsertTasteTuningDefaults :exec INSERT INTO taste_tuning ( singleton, half_life_days, engagement_hard_skip, diff --git a/internal/db/dbq/requests.sql.go b/internal/db/dbq/requests.sql.go deleted file mode 100644 index c2e2b6d1..00000000 --- a/internal/db/dbq/requests.sql.go +++ /dev/null @@ -1,528 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: requests.sql - -package dbq - -import ( - "context" - - "github.com/jackc/pgx/v5/pgtype" -) - -const approveRequest = `-- name: ApproveRequest :one -UPDATE requests - SET status = 'approved', - decided_at = now(), - decided_by = $2, - updated_at = now() - WHERE id = $1 AND status = 'pending' - RETURNING id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at -` - -type ApproveRequestParams struct { - ID pgtype.UUID - DecidedBy pgtype.UUID -} - -func (q *Queries) ApproveRequest(ctx context.Context, arg ApproveRequestParams) (Request, error) { - row := q.db.QueryRow(ctx, approveRequest, arg.ID, arg.DecidedBy) - var i Request - err := row.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ) - return i, err -} - -const cancelRequest = `-- name: CancelRequest :one -UPDATE requests - SET status = 'rejected', - notes = 'cancelled by user', - decided_at = now(), - decided_by = $2, - updated_at = now() - WHERE id = $1 AND user_id = $2 AND status = 'pending' - RETURNING id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at -` - -type CancelRequestParams struct { - ID pgtype.UUID - DecidedBy pgtype.UUID -} - -// $2 serves as both the ownership guard (user_id = $2) and the -// decided_by audit field. The generated parameter struct names it -// DecidedBy — callers must pass the cancelling user's UUID, and -// a wrong UUID will silently no-op (zero rows updated) rather than -// erroring at the SQL layer. -func (q *Queries) CancelRequest(ctx context.Context, arg CancelRequestParams) (Request, error) { - row := q.db.QueryRow(ctx, cancelRequest, arg.ID, arg.DecidedBy) - var i Request - err := row.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ) - return i, err -} - -const completeRequest = `-- name: CompleteRequest :one -UPDATE requests - SET status = 'completed', - matched_track_id = $2, - matched_album_id = $3, - matched_artist_id = $4, - completed_at = now(), - updated_at = now() - WHERE id = $1 AND status = 'approved' - RETURNING id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at -` - -type CompleteRequestParams struct { - ID pgtype.UUID - MatchedTrackID pgtype.UUID - MatchedAlbumID pgtype.UUID - MatchedArtistID pgtype.UUID -} - -// Reconciler transitions an approved request to completed when its -// target track/album/artist has appeared in the library. Callers -// should set ONLY the matched_*_id corresponding to the request's -// kind and pass pgtype.UUID{} (Valid: false) for the others — -// per-kind one-of-three is the contract, NULL for the others -// preserves auditability if the matched row is later evicted. -func (q *Queries) CompleteRequest(ctx context.Context, arg CompleteRequestParams) (Request, error) { - row := q.db.QueryRow(ctx, completeRequest, - arg.ID, - arg.MatchedTrackID, - arg.MatchedAlbumID, - arg.MatchedArtistID, - ) - var i Request - err := row.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ) - return i, err -} - -const createRequest = `-- name: CreateRequest :one -INSERT INTO requests ( - user_id, kind, - artist_mbid, album_mbid, track_mbid, - artist_name, album_title, track_title -) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) -RETURNING id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at -` - -type CreateRequestParams struct { - UserID pgtype.UUID - Kind RequestKind - ArtistMbid string - AlbumMbid *string - TrackMbid *string - ArtistName string - AlbumTitle *string - TrackTitle *string -} - -func (q *Queries) CreateRequest(ctx context.Context, arg CreateRequestParams) (Request, error) { - row := q.db.QueryRow(ctx, createRequest, - arg.UserID, - arg.Kind, - arg.ArtistMbid, - arg.AlbumMbid, - arg.TrackMbid, - arg.ArtistName, - arg.AlbumTitle, - arg.TrackTitle, - ) - var i Request - err := row.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ) - return i, err -} - -const getNonTerminalRequestForMBID = `-- name: GetNonTerminalRequestForMBID :one -SELECT id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at FROM requests - WHERE status IN ('pending', 'approved', 'completed') - AND ((kind = 'artist' AND artist_mbid = $1) - OR (kind = 'album' AND album_mbid = $1) - OR (kind = 'track' AND track_mbid = $1)) - ORDER BY requested_at ASC - LIMIT 1 -` - -// Returns the oldest non-terminal (pending/approved/completed) request -// whose own-kind MBID matches @mbid, or pgx.ErrNoRows when none. Create() -// uses this to dedupe: a re-request for something already in flight -// returns the existing row instead of inserting a duplicate. -func (q *Queries) GetNonTerminalRequestForMBID(ctx context.Context, mbid string) (Request, error) { - row := q.db.QueryRow(ctx, getNonTerminalRequestForMBID, mbid) - var i Request - err := row.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ) - return i, err -} - -const getRequestByID = `-- name: GetRequestByID :one -SELECT id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at FROM requests WHERE id = $1 -` - -func (q *Queries) GetRequestByID(ctx context.Context, id pgtype.UUID) (Request, error) { - row := q.db.QueryRow(ctx, getRequestByID, id) - var i Request - err := row.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ) - return i, err -} - -const hasNonTerminalRequestForMBID = `-- name: HasNonTerminalRequestForMBID :one -SELECT (EXISTS ( - SELECT 1 FROM requests r - WHERE r.status IN ('pending', 'approved', 'completed') - AND ((r.kind = 'artist' AND r.artist_mbid = $1) - OR (r.kind = 'album' AND r.album_mbid = $1) - OR (r.kind = 'track' AND r.track_mbid = $1)) -) OR EXISTS ( - SELECT 1 FROM wants w WHERE w.release_group_mbid = $1 AND w.fulfilled_at IS NULL -) OR EXISTS ( - SELECT 1 FROM follows f WHERE f.artist_mbid = $1 -))::bool AS requested -` - -// Whether something is already on its way: a pending, approved or completed -// request at the given level, an open want for the release group, or a -// follow of the artist. Sets the `requested` flag on catalogue search. -// Terminal-status (rejected, failed) requests do not count. MBIDs are unique -// per entity type, so checking one id against every column cannot -// false-match. -func (q *Queries) HasNonTerminalRequestForMBID(ctx context.Context, mbid string) (bool, error) { - row := q.db.QueryRow(ctx, hasNonTerminalRequestForMBID, mbid) - var requested bool - err := row.Scan(&requested) - return requested, err -} - -const listApprovedRequestsForReconcile = `-- name: ListApprovedRequestsForReconcile :many -SELECT id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at FROM requests -WHERE status = 'approved' -ORDER BY decided_at ASC -LIMIT $1 -` - -func (q *Queries) ListApprovedRequestsForReconcile(ctx context.Context, limit int32) ([]Request, error) { - rows, err := q.db.Query(ctx, listApprovedRequestsForReconcile, limit) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Request - for rows.Next() { - var i Request - if err := rows.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listRequestsByStatus = `-- name: ListRequestsByStatus :many -SELECT id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at FROM requests -WHERE status = $1 -ORDER BY requested_at DESC -LIMIT $2 -` - -type ListRequestsByStatusParams struct { - Status RequestStatus - Limit int32 -} - -func (q *Queries) ListRequestsByStatus(ctx context.Context, arg ListRequestsByStatusParams) ([]Request, error) { - rows, err := q.db.Query(ctx, listRequestsByStatus, arg.Status, arg.Limit) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Request - for rows.Next() { - var i Request - if err := rows.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listRequestsForUser = `-- name: ListRequestsForUser :many -SELECT id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at FROM requests -WHERE user_id = $1 -ORDER BY requested_at DESC -LIMIT $2 -` - -type ListRequestsForUserParams struct { - UserID pgtype.UUID - Limit int32 -} - -func (q *Queries) ListRequestsForUser(ctx context.Context, arg ListRequestsForUserParams) ([]Request, error) { - rows, err := q.db.Query(ctx, listRequestsForUser, arg.UserID, arg.Limit) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Request - for rows.Next() { - var i Request - if err := rows.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const rejectRequest = `-- name: RejectRequest :one -UPDATE requests - SET status = 'rejected', - notes = $2, - decided_at = now(), - decided_by = $3, - updated_at = now() - WHERE id = $1 AND status = 'pending' - RETURNING id, user_id, status, kind, artist_mbid, album_mbid, track_mbid, artist_name, album_title, track_title, decided_at, decided_by, notes, completed_at, matched_track_id, matched_album_id, matched_artist_id, requested_at, updated_at -` - -type RejectRequestParams struct { - ID pgtype.UUID - Notes *string - DecidedBy pgtype.UUID -} - -func (q *Queries) RejectRequest(ctx context.Context, arg RejectRequestParams) (Request, error) { - row := q.db.QueryRow(ctx, rejectRequest, arg.ID, arg.Notes, arg.DecidedBy) - var i Request - err := row.Scan( - &i.ID, - &i.UserID, - &i.Status, - &i.Kind, - &i.ArtistMbid, - &i.AlbumMbid, - &i.TrackMbid, - &i.ArtistName, - &i.AlbumTitle, - &i.TrackTitle, - &i.DecidedAt, - &i.DecidedBy, - &i.Notes, - &i.CompletedAt, - &i.MatchedTrackID, - &i.MatchedAlbumID, - &i.MatchedArtistID, - &i.RequestedAt, - &i.UpdatedAt, - ) - return i, err -} - -const repointRequestsAlbumMbid = `-- name: RepointRequestsAlbumMbid :exec -UPDATE requests - SET album_mbid = $1, - updated_at = now() - WHERE album_mbid = $2 -` - -type RepointRequestsAlbumMbidParams struct { - NewMbid *string - OldMbid *string -} - -// A want moved from a release id to its group takes the requests -// that name it along, so the reconciler finds it. -func (q *Queries) RepointRequestsAlbumMbid(ctx context.Context, arg RepointRequestsAlbumMbidParams) error { - _, err := q.db.Exec(ctx, repointRequestsAlbumMbid, arg.NewMbid, arg.OldMbid) - return err -} diff --git a/internal/db/dbq/search.sql.go b/internal/db/dbq/search.sql.go deleted file mode 100644 index 3e5986e1..00000000 --- a/internal/db/dbq/search.sql.go +++ /dev/null @@ -1,680 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: search.sql - -package dbq - -import ( - "context" - - "github.com/jackc/pgx/v5/pgtype" -) - -const blockRelease = `-- name: BlockRelease :exec -INSERT INTO release_blocks (info_hash, name, reason, detail, want_id, blocked_by, expires_at) -VALUES ($1, $2, $3, $4, - $5, $6, $7) -ON CONFLICT (info_hash) DO UPDATE - SET reason = EXCLUDED.reason, detail = EXCLUDED.detail, blocked_at = now(), - expires_at = EXCLUDED.expires_at, blocked_by = EXCLUDED.blocked_by - WHERE release_blocks.expires_at IS NOT NULL OR EXCLUDED.expires_at IS NULL -` - -type BlockReleaseParams struct { - InfoHash string - Name string - Reason string - Detail string - WantID pgtype.UUID - BlockedBy pgtype.UUID - ExpiresAt pgtype.Timestamptz -} - -// A release set aside. expires_at NULL is for good. A block for good is never -// shortened by a timed one: only a timed block, or another for good, is -// replaced. -func (q *Queries) BlockRelease(ctx context.Context, arg BlockReleaseParams) error { - _, err := q.db.Exec(ctx, blockRelease, - arg.InfoHash, - arg.Name, - arg.Reason, - arg.Detail, - arg.WantID, - arg.BlockedBy, - arg.ExpiresAt, - ) - return err -} - -const deleteWantCandidates = `-- name: DeleteWantCandidates :exec -DELETE FROM want_candidates WHERE want_id = $1 -` - -func (q *Queries) DeleteWantCandidates(ctx context.Context, wantID pgtype.UUID) error { - _, err := q.db.Exec(ctx, deleteWantCandidates, wantID) - return err -} - -const editionTrackSpan = `-- name: EditionTrackSpan :one -SELECT COALESCE(MIN(track_count) FILTER (WHERE status = 'Official'), MIN(track_count), 0)::int AS min_tracks, - COALESCE(MAX(track_count), 0)::int AS max_tracks - FROM mb_releases - WHERE release_group_mbid = $1 AND track_count > 0 -` - -type EditionTrackSpanRow struct { - MinTracks int32 - MaxTracks int32 -} - -// The fewest tracks an official edition of the group has (any edition when -// none is official), and the most any has. Zero when none is catalogued. -func (q *Queries) EditionTrackSpan(ctx context.Context, releaseGroupMbid string) (EditionTrackSpanRow, error) { - row := q.db.QueryRow(ctx, editionTrackSpan, releaseGroupMbid) - var i EditionTrackSpanRow - err := row.Scan(&i.MinTracks, &i.MaxTracks) - return i, err -} - -const endGrab = `-- name: EndGrab :exec -UPDATE grabs - SET ended_at = now(), end_reason = $1, end_detail = $2 - WHERE id = $3 AND ended_at IS NULL -` - -type EndGrabParams struct { - EndReason string - EndDetail string - ID pgtype.UUID -} - -func (q *Queries) EndGrab(ctx context.Context, arg EndGrabParams) error { - _, err := q.db.Exec(ctx, endGrab, arg.EndReason, arg.EndDetail, arg.ID) - return err -} - -const getAcquisitionSettings = `-- name: GetAcquisitionSettings :one - -SELECT id, auto_grab, search_interval_hours, allowed_qualities, last_pass_at, last_pass, updated_at, import_root, seed_ratio, seed_days, stall_hours, library_listing_started_at, library_listed_at, library_unlisted, upgrade_until FROM acquisition_settings WHERE id = true -` - -// Searching for wants and grabbing releases. -func (q *Queries) GetAcquisitionSettings(ctx context.Context) (AcquisitionSetting, error) { - row := q.db.QueryRow(ctx, getAcquisitionSettings) - var i AcquisitionSetting - err := row.Scan( - &i.ID, - &i.AutoGrab, - &i.SearchIntervalHours, - &i.AllowedQualities, - &i.LastPassAt, - &i.LastPass, - &i.UpdatedAt, - &i.ImportRoot, - &i.SeedRatio, - &i.SeedDays, - &i.StallHours, - &i.LibraryListingStartedAt, - &i.LibraryListedAt, - &i.LibraryUnlisted, - &i.UpgradeUntil, - ) - return i, err -} - -const getLiveGrab = `-- name: GetLiveGrab :one -SELECT id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at FROM grabs WHERE want_id = $1 AND ended_at IS NULL -` - -func (q *Queries) GetLiveGrab(ctx context.Context, wantID pgtype.UUID) (Grab, error) { - row := q.db.QueryRow(ctx, getLiveGrab, wantID) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const getWantCandidate = `-- name: GetWantCandidate :one -SELECT want_id, key, name, info_hash, indexer, source, size_bytes, seeders, files_count, format, quality, year, verdict, reason, reasons, folded_from, rank, searched_at FROM want_candidates WHERE want_id = $1 AND key = $2 -` - -type GetWantCandidateParams struct { - WantID pgtype.UUID - Key string -} - -func (q *Queries) GetWantCandidate(ctx context.Context, arg GetWantCandidateParams) (WantCandidate, error) { - row := q.db.QueryRow(ctx, getWantCandidate, arg.WantID, arg.Key) - var i WantCandidate - err := row.Scan( - &i.WantID, - &i.Key, - &i.Name, - &i.InfoHash, - &i.Indexer, - &i.Source, - &i.SizeBytes, - &i.Seeders, - &i.FilesCount, - &i.Format, - &i.Quality, - &i.Year, - &i.Verdict, - &i.Reason, - &i.Reasons, - &i.FoldedFrom, - &i.Rank, - &i.SearchedAt, - ) - return i, err -} - -const getWantForSearch = `-- name: GetWantForSearch :one -SELECT w.id, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title, w.reason, w.fulfilled_at, - w.upgrade, w.held_tier, w.held_quality, w.album_id, - COALESCE(NULLIF(w.primary_type, ''), g.primary_type, '')::text AS primary_type, - COALESCE(g.secondary_types, '{}')::text[] AS secondary_types, - COALESCE(NULLIF(w.first_release_date, ''), g.first_release_date, '')::text AS first_release_date - FROM wants w - LEFT JOIN mb_release_groups g ON g.mbid = w.release_group_mbid - WHERE w.id = $1 -` - -type GetWantForSearchRow struct { - ID pgtype.UUID - ReleaseGroupMbid string - ArtistMbid string - ArtistName string - Title string - Reason string - FulfilledAt pgtype.Timestamptz - Upgrade string - HeldTier string - HeldQuality string - AlbumID pgtype.UUID - PrimaryType string - SecondaryTypes []string - FirstReleaseDate string -} - -func (q *Queries) GetWantForSearch(ctx context.Context, id pgtype.UUID) (GetWantForSearchRow, error) { - row := q.db.QueryRow(ctx, getWantForSearch, id) - var i GetWantForSearchRow - err := row.Scan( - &i.ID, - &i.ReleaseGroupMbid, - &i.ArtistMbid, - &i.ArtistName, - &i.Title, - &i.Reason, - &i.FulfilledAt, - &i.Upgrade, - &i.HeldTier, - &i.HeldQuality, - &i.AlbumID, - &i.PrimaryType, - &i.SecondaryTypes, - &i.FirstReleaseDate, - ) - return i, err -} - -const insertGrab = `-- name: InsertGrab :one -INSERT INTO grabs (want_id, info_hash, name, key, indexer, source, client, save_path, quality, - origin, forced, grabbed_by) -VALUES ($1, $2, $3, $4, $5, - $6, $7, $8, $9, - $10, $11, $12) -RETURNING id, want_id, info_hash, name, key, indexer, source, client, save_path, quality, origin, forced, grabbed_by, grabbed_at, ended_at, end_reason, end_detail, progress, import_state, import_reason, import_detail, import_forced, import_attempts, import_updated_at, imported_at, imported_files, destination, seeding, ratio, seed_released_at -` - -type InsertGrabParams struct { - WantID pgtype.UUID - InfoHash string - Name string - Key string - Indexer string - Source string - Client string - SavePath string - Quality string - Origin string - Forced bool - GrabbedBy pgtype.UUID -} - -func (q *Queries) InsertGrab(ctx context.Context, arg InsertGrabParams) (Grab, error) { - row := q.db.QueryRow(ctx, insertGrab, - arg.WantID, - arg.InfoHash, - arg.Name, - arg.Key, - arg.Indexer, - arg.Source, - arg.Client, - arg.SavePath, - arg.Quality, - arg.Origin, - arg.Forced, - arg.GrabbedBy, - ) - var i Grab - err := row.Scan( - &i.ID, - &i.WantID, - &i.InfoHash, - &i.Name, - &i.Key, - &i.Indexer, - &i.Source, - &i.Client, - &i.SavePath, - &i.Quality, - &i.Origin, - &i.Forced, - &i.GrabbedBy, - &i.GrabbedAt, - &i.EndedAt, - &i.EndReason, - &i.EndDetail, - &i.Progress, - &i.ImportState, - &i.ImportReason, - &i.ImportDetail, - &i.ImportForced, - &i.ImportAttempts, - &i.ImportUpdatedAt, - &i.ImportedAt, - &i.ImportedFiles, - &i.Destination, - &i.Seeding, - &i.Ratio, - &i.SeedReleasedAt, - ) - return i, err -} - -const insertWantCandidate = `-- name: InsertWantCandidate :exec -INSERT INTO want_candidates (want_id, key, name, info_hash, indexer, source, size_bytes, seeders, - files_count, format, quality, year, verdict, reason, reasons, folded_from, rank) -VALUES ($1, $2, $3, $4, $5, - $6, $7, $8, $9, $10, - $11, $12, $13, $14, $15, - $16, $17) -ON CONFLICT (want_id, key) DO NOTHING -` - -type InsertWantCandidateParams struct { - WantID pgtype.UUID - Key string - Name string - InfoHash string - Indexer string - Source string - SizeBytes int64 - Seeders int32 - FilesCount int32 - Format string - Quality string - Year int32 - Verdict string - Reason string - Reasons []byte - FoldedFrom []string - Rank int32 -} - -func (q *Queries) InsertWantCandidate(ctx context.Context, arg InsertWantCandidateParams) error { - _, err := q.db.Exec(ctx, insertWantCandidate, - arg.WantID, - arg.Key, - arg.Name, - arg.InfoHash, - arg.Indexer, - arg.Source, - arg.SizeBytes, - arg.Seeders, - arg.FilesCount, - arg.Format, - arg.Quality, - arg.Year, - arg.Verdict, - arg.Reason, - arg.Reasons, - arg.FoldedFrom, - arg.Rank, - ) - return err -} - -const listReleaseBlocks = `-- name: ListReleaseBlocks :many -SELECT info_hash, name, reason, detail, want_id, blocked_by, blocked_at, expires_at FROM release_blocks - WHERE expires_at IS NULL OR expires_at > now() - ORDER BY blocked_at DESC -` - -// Every release set aside and still held back: a stalled one lifts by itself -// at expires_at. -func (q *Queries) ListReleaseBlocks(ctx context.Context) ([]ReleaseBlock, error) { - rows, err := q.db.Query(ctx, listReleaseBlocks) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ReleaseBlock - for rows.Next() { - var i ReleaseBlock - if err := rows.Scan( - &i.InfoHash, - &i.Name, - &i.Reason, - &i.Detail, - &i.WantID, - &i.BlockedBy, - &i.BlockedAt, - &i.ExpiresAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listWantCandidates = `-- name: ListWantCandidates :many -SELECT want_id, key, name, info_hash, indexer, source, size_bytes, seeders, files_count, format, quality, year, verdict, reason, reasons, folded_from, rank, searched_at FROM want_candidates WHERE want_id = $1 ORDER BY rank, key -` - -func (q *Queries) ListWantCandidates(ctx context.Context, wantID pgtype.UUID) ([]WantCandidate, error) { - rows, err := q.db.Query(ctx, listWantCandidates, wantID) - if err != nil { - return nil, err - } - defer rows.Close() - var items []WantCandidate - for rows.Next() { - var i WantCandidate - if err := rows.Scan( - &i.WantID, - &i.Key, - &i.Name, - &i.InfoHash, - &i.Indexer, - &i.Source, - &i.SizeBytes, - &i.Seeders, - &i.FilesCount, - &i.Format, - &i.Quality, - &i.Year, - &i.Verdict, - &i.Reason, - &i.Reasons, - &i.FoldedFrom, - &i.Rank, - &i.SearchedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listWantsDueForSearch = `-- name: ListWantsDueForSearch :many -SELECT w.id, w.release_group_mbid, w.artist_name, w.title, w.reason, w.upgrade, w.held_tier, w.held_quality, - COALESCE(NULLIF(w.primary_type, ''), g.primary_type, '')::text AS primary_type, - COALESCE(g.secondary_types, '{}')::text[] AS secondary_types, - COALESCE(NULLIF(w.first_release_date, ''), g.first_release_date, '')::text AS first_release_date - FROM wants w - LEFT JOIN mb_release_groups g ON g.mbid = w.release_group_mbid - WHERE (w.fulfilled_at IS NULL OR w.upgrade <> '') - AND NOT EXISTS (SELECT 1 FROM grabs gr WHERE gr.want_id = w.id AND gr.ended_at IS NULL) - AND (w.searched_at IS NULL - OR w.searched_at < now() - make_interval(hours => CASE WHEN w.fulfilled_at IS NULL - THEN $1::int - ELSE GREATEST($1::int, 168) END)) - ORDER BY w.fulfilled_at IS NOT NULL, - w.searched_at IS NOT NULL, - CASE w.reason WHEN 'request' THEN 0 WHEN 'missing' THEN 1 ELSE 2 END, - w.searched_at NULLS FIRST, w.wanted_at, w.id - LIMIT $2 -` - -type ListWantsDueForSearchParams struct { - Hours int32 - MaxRows int32 -} - -type ListWantsDueForSearchRow struct { - ID pgtype.UUID - ReleaseGroupMbid string - ArtistName string - Title string - Reason string - Upgrade string - HeldTier string - HeldQuality string - PrimaryType string - SecondaryTypes []string - FirstReleaseDate string -} - -// Wants with no download running that were never searched or not since -// @hours ago: open wants first (never-searched, then requests, then missing -// files, then followed artists' releases), then held albums wanted for an -// upgrade, which are searched at most weekly. -func (q *Queries) ListWantsDueForSearch(ctx context.Context, arg ListWantsDueForSearchParams) ([]ListWantsDueForSearchRow, error) { - rows, err := q.db.Query(ctx, listWantsDueForSearch, arg.Hours, arg.MaxRows) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListWantsDueForSearchRow - for rows.Next() { - var i ListWantsDueForSearchRow - if err := rows.Scan( - &i.ID, - &i.ReleaseGroupMbid, - &i.ArtistName, - &i.Title, - &i.Reason, - &i.Upgrade, - &i.HeldTier, - &i.HeldQuality, - &i.PrimaryType, - &i.SecondaryTypes, - &i.FirstReleaseDate, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const markCandidatesBlocked = `-- name: MarkCandidatesBlocked :exec -UPDATE want_candidates - SET verdict = 'blocked', reason = $1, - reasons = jsonb_build_array(jsonb_build_object('kind', 'hard', 'code', 'blocked', 'text', $1::text)) - WHERE info_hash = $2 -` - -type MarkCandidatesBlockedParams struct { - Reason string - InfoHash string -} - -func (q *Queries) MarkCandidatesBlocked(ctx context.Context, arg MarkCandidatesBlockedParams) error { - _, err := q.db.Exec(ctx, markCandidatesBlocked, arg.Reason, arg.InfoHash) - return err -} - -const markCandidatesUnblocked = `-- name: MarkCandidatesUnblocked :exec -UPDATE want_candidates - SET verdict = 'review', reason = $1, - reasons = jsonb_build_array(jsonb_build_object('kind', 'soft', 'code', 'unblocked', 'text', $1::text)) - WHERE info_hash = $2 AND verdict = 'blocked' -` - -type MarkCandidatesUnblockedParams struct { - Reason string - InfoHash string -} - -func (q *Queries) MarkCandidatesUnblocked(ctx context.Context, arg MarkCandidatesUnblockedParams) error { - _, err := q.db.Exec(ctx, markCandidatesUnblocked, arg.Reason, arg.InfoHash) - return err -} - -const recordSearchPass = `-- name: RecordSearchPass :exec -UPDATE acquisition_settings SET last_pass_at = now(), last_pass = $1 WHERE id = true -` - -func (q *Queries) RecordSearchPass(ctx context.Context, report []byte) error { - _, err := q.db.Exec(ctx, recordSearchPass, report) - return err -} - -const recordWantSearch = `-- name: RecordWantSearch :exec -UPDATE wants - SET searched_at = now(), - search_outcome = $1, - search_considered = $2, - best_release = $3, - best_verdict = $4, - search_error = $5, - updated_at = now() - WHERE id = $6 -` - -type RecordWantSearchParams struct { - Outcome string - Considered int32 - BestRelease string - BestVerdict string - SearchError string - ID pgtype.UUID -} - -func (q *Queries) RecordWantSearch(ctx context.Context, arg RecordWantSearchParams) error { - _, err := q.db.Exec(ctx, recordWantSearch, - arg.Outcome, - arg.Considered, - arg.BestRelease, - arg.BestVerdict, - arg.SearchError, - arg.ID, - ) - return err -} - -const unblockRelease = `-- name: UnblockRelease :execrows -DELETE FROM release_blocks WHERE info_hash = $1 -` - -func (q *Queries) UnblockRelease(ctx context.Context, infoHash string) (int64, error) { - result, err := q.db.Exec(ctx, unblockRelease, infoHash) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const updateAcquisitionSettings = `-- name: UpdateAcquisitionSettings :one -UPDATE acquisition_settings - SET auto_grab = $1, - search_interval_hours = $2, - allowed_qualities = $3, - import_root = $4, - seed_ratio = $5, - seed_days = $6, - stall_hours = $7, - upgrade_until = $8, - updated_at = now() - WHERE id = true -RETURNING id, auto_grab, search_interval_hours, allowed_qualities, last_pass_at, last_pass, updated_at, import_root, seed_ratio, seed_days, stall_hours, library_listing_started_at, library_listed_at, library_unlisted, upgrade_until -` - -type UpdateAcquisitionSettingsParams struct { - AutoGrab bool - SearchIntervalHours int32 - AllowedQualities []string - ImportRoot string - SeedRatio float32 - SeedDays int32 - StallHours int32 - UpgradeUntil string -} - -func (q *Queries) UpdateAcquisitionSettings(ctx context.Context, arg UpdateAcquisitionSettingsParams) (AcquisitionSetting, error) { - row := q.db.QueryRow(ctx, updateAcquisitionSettings, - arg.AutoGrab, - arg.SearchIntervalHours, - arg.AllowedQualities, - arg.ImportRoot, - arg.SeedRatio, - arg.SeedDays, - arg.StallHours, - arg.UpgradeUntil, - ) - var i AcquisitionSetting - err := row.Scan( - &i.ID, - &i.AutoGrab, - &i.SearchIntervalHours, - &i.AllowedQualities, - &i.LastPassAt, - &i.LastPass, - &i.UpdatedAt, - &i.ImportRoot, - &i.SeedRatio, - &i.SeedDays, - &i.StallHours, - &i.LibraryListingStartedAt, - &i.LibraryListedAt, - &i.LibraryUnlisted, - &i.UpgradeUntil, - ) - return i, err -} diff --git a/internal/db/dbq/similarity.sql.go b/internal/db/dbq/similarity.sql.go index d1f69b8a..2357be71 100644 --- a/internal/db/dbq/similarity.sql.go +++ b/internal/db/dbq/similarity.sql.go @@ -340,35 +340,3 @@ func (q *Queries) UpsertArtistSimilarity(ctx context.Context, arg UpsertArtistSi _, err := q.db.Exec(ctx, upsertArtistSimilarity, arg.ArtistAID, arg.ArtistBID, arg.Score) return err } - -const upsertArtistSimilarityUnmatched = `-- name: UpsertArtistSimilarityUnmatched :exec -INSERT INTO artist_similarity_unmatched ( - seed_artist_id, candidate_mbid, candidate_name, score, source -) VALUES ($1, $2, $3, $4, $5) -ON CONFLICT (seed_artist_id, candidate_mbid, source) DO UPDATE SET - candidate_name = EXCLUDED.candidate_name, - score = EXCLUDED.score, - fetched_at = now() -` - -type UpsertArtistSimilarityUnmatchedParams struct { - SeedArtistID pgtype.UUID - CandidateMbid string - CandidateName string - Score float64 - Source string -} - -// Persists an out-of-library similar-artist MBID. Idempotent on -// (seed_artist_id, candidate_mbid, source) — re-fetches refresh the -// name/score and bump fetched_at. -func (q *Queries) UpsertArtistSimilarityUnmatched(ctx context.Context, arg UpsertArtistSimilarityUnmatchedParams) error { - _, err := q.db.Exec(ctx, upsertArtistSimilarityUnmatched, - arg.SeedArtistID, - arg.CandidateMbid, - arg.CandidateName, - arg.Score, - arg.Source, - ) - return err -} diff --git a/internal/db/dbq/suggestion_snoozes.sql.go b/internal/db/dbq/suggestion_snoozes.sql.go deleted file mode 100644 index 5da3eb77..00000000 --- a/internal/db/dbq/suggestion_snoozes.sql.go +++ /dev/null @@ -1,126 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: suggestion_snoozes.sql - -package dbq - -import ( - "context" - - "github.com/jackc/pgx/v5/pgtype" -) - -const gcDeleteExpiredSuggestionSnoozes = `-- name: GcDeleteExpiredSuggestionSnoozes :execrows -DELETE FROM suggestion_snoozes WHERE snoozed_until < now() -` - -// Keeps the table from growing without bound. Every read already filters on -// snoozed_until > now(), so deleting an expired row changes no behaviour — -// this is purely reclamation. -func (q *Queries) GcDeleteExpiredSuggestionSnoozes(ctx context.Context) (int64, error) { - result, err := q.db.Exec(ctx, gcDeleteExpiredSuggestionSnoozes) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const listActiveSuggestionSnoozes = `-- name: ListActiveSuggestionSnoozes :many -SELECT candidate_mbid, candidate_name, snoozed_until, created_at - FROM suggestion_snoozes - WHERE user_id = $1 AND snoozed_until > now() - ORDER BY snoozed_until, candidate_mbid -` - -type ListActiveSuggestionSnoozesRow struct { - CandidateMbid string - CandidateName string - SnoozedUntil pgtype.Timestamptz - CreatedAt pgtype.Timestamptz -} - -// Backs the manage / un-snooze surface. Expired rows are filtered HERE -// rather than left to the sweeper: gc runs on an hourly tick, so a row can -// outlive its expiry by up to a tick and must not read as still-snoozed in -// the meantime. -func (q *Queries) ListActiveSuggestionSnoozes(ctx context.Context, userID pgtype.UUID) ([]ListActiveSuggestionSnoozesRow, error) { - rows, err := q.db.Query(ctx, listActiveSuggestionSnoozes, userID) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListActiveSuggestionSnoozesRow - for rows.Next() { - var i ListActiveSuggestionSnoozesRow - if err := rows.Scan( - &i.CandidateMbid, - &i.CandidateName, - &i.SnoozedUntil, - &i.CreatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const snoozeSuggestion = `-- name: SnoozeSuggestion :exec - -INSERT INTO suggestion_snoozes (user_id, candidate_mbid, candidate_name, snoozed_until) -VALUES ($1, $2, $3, now() + ($4::float8 * INTERVAL '1 day')) -ON CONFLICT (user_id, candidate_mbid) DO UPDATE - SET snoozed_until = EXCLUDED.snoozed_until, - candidate_name = EXCLUDED.candidate_name -` - -type SnoozeSuggestionParams struct { - UserID pgtype.UUID - CandidateMbid string - CandidateName string - Column4 float64 -} - -// Time-boxed "not right now" on a Discover artist suggestion. -// -// See 0049_suggestion_snoozes.up.sql for why this is a snooze and not a -// dismissal: it records no verdict on the music, expires on its own, and -// must never reach the taste profile. Nothing in internal/taste may read -// this table. -// Upsert, so re-snoozing an already-snoozed candidate EXTENDS it instead of -// erroring on the PK. The name is refreshed too — a later suggestion may -// carry a corrected spelling from the similarity feed. -// $1=user_id, $2=candidate_mbid, $3=candidate_name, $4=duration in days. -func (q *Queries) SnoozeSuggestion(ctx context.Context, arg SnoozeSuggestionParams) error { - _, err := q.db.Exec(ctx, snoozeSuggestion, - arg.UserID, - arg.CandidateMbid, - arg.CandidateName, - arg.Column4, - ) - return err -} - -const unsnoozeSuggestion = `-- name: UnsnoozeSuggestion :execrows -DELETE FROM suggestion_snoozes - WHERE user_id = $1 AND candidate_mbid = $2 -` - -type UnsnoozeSuggestionParams struct { - UserID pgtype.UUID - CandidateMbid string -} - -// Row count is returned so the handler can 404 an MBID that was never -// snoozed rather than reporting success for a no-op. -func (q *Queries) UnsnoozeSuggestion(ctx context.Context, arg UnsnoozeSuggestionParams) (int64, error) { - result, err := q.db.Exec(ctx, unsnoozeSuggestion, arg.UserID, arg.CandidateMbid) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} diff --git a/internal/db/dbq/tracks.sql.go b/internal/db/dbq/tracks.sql.go index 5f136d09..44707ac9 100644 --- a/internal/db/dbq/tracks.sql.go +++ b/internal/db/dbq/tracks.sql.go @@ -716,18 +716,10 @@ SELECT t.id, artists.id AS artist_id, artists.name AS artist_name, t.source_verdict, - t.source_verdict_at, - -- The album's place on the wanted list: wanted for a clean copy, and - -- when that was last searched for. - COALESCE(w.upgrade, '')::text AS want_upgrade, - w.searched_at AS searched_at + t.source_verdict_at FROM tracks t JOIN albums ON albums.id = t.album_id JOIN artists ON artists.id = t.artist_id - LEFT JOIN LATERAL ( - SELECT x.upgrade, x.searched_at FROM wants x - WHERE x.album_id = albums.id AND x.fulfilled_at IS NOT NULL LIMIT 1 - ) w ON true WHERE t.missing_since IS NULL AND regexp_replace(t.file_path, '^.*/', '') ~* $1::text ORDER BY directory, t.disc_number NULLS FIRST, t.track_number NULLS FIRST, t.title @@ -754,8 +746,6 @@ type ListSuspectSourceTracksRow struct { ArtistName string SourceVerdict *string SourceVerdictAt pgtype.Timestamptz - WantUpgrade string - SearchedAt pgtype.Timestamptz } // The admin report of present tracks whose filename looks like a video rip @@ -791,8 +781,6 @@ func (q *Queries) ListSuspectSourceTracks(ctx context.Context, arg ListSuspectSo &i.ArtistName, &i.SourceVerdict, &i.SourceVerdictAt, - &i.WantUpgrade, - &i.SearchedAt, ); err != nil { return nil, err } diff --git a/internal/db/dbq/users.sql.go b/internal/db/dbq/users.sql.go index d21696fa..35533e22 100644 --- a/internal/db/dbq/users.sql.go +++ b/internal/db/dbq/users.sql.go @@ -397,8 +397,7 @@ type ListUsernamesByIDsRow struct { Username string } -// Names a list's people in one round trip, e.g. who made each request on -// /admin/requests. +// Names a list's people in one round trip. func (q *Queries) ListUsernamesByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]ListUsernamesByIDsRow, error) { rows, err := q.db.Query(ctx, listUsernamesByIDs, dollar_1) if err != nil { @@ -627,42 +626,6 @@ func (q *Queries) UpdateUserAdmin(ctx context.Context, arg UpdateUserAdminParams return i, err } -const updateUserAutoApprove = `-- name: UpdateUserAutoApprove :one -UPDATE users - SET auto_approve_requests = $2 - WHERE id = $1 -RETURNING id, username, password_hash, is_admin, created_at, subsonic_password, listenbrainz_token, listenbrainz_enabled, display_name, auto_approve_requests, email, timezone, timezone_updated_at, debug_mode_enabled, api_token_hash -` - -type UpdateUserAutoApproveParams struct { - ID pgtype.UUID - AutoApproveRequests bool -} - -// Toggle the per-user auto_approve_requests flag. -func (q *Queries) UpdateUserAutoApprove(ctx context.Context, arg UpdateUserAutoApproveParams) (User, error) { - row := q.db.QueryRow(ctx, updateUserAutoApprove, arg.ID, arg.AutoApproveRequests) - var i User - err := row.Scan( - &i.ID, - &i.Username, - &i.PasswordHash, - &i.IsAdmin, - &i.CreatedAt, - &i.SubsonicPassword, - &i.ListenbrainzToken, - &i.ListenbrainzEnabled, - &i.DisplayName, - &i.AutoApproveRequests, - &i.Email, - &i.Timezone, - &i.TimezoneUpdatedAt, - &i.DebugModeEnabled, - &i.ApiTokenHash, - ) - return i, err -} - const updateUserProfile = `-- name: UpdateUserProfile :one UPDATE users SET display_name = $2, diff --git a/internal/db/dbq/wanted.sql.go b/internal/db/dbq/wanted.sql.go deleted file mode 100644 index efe87083..00000000 --- a/internal/db/dbq/wanted.sql.go +++ /dev/null @@ -1,1697 +0,0 @@ -// Code generated by sqlc. DO NOT EDIT. -// versions: -// sqlc v1.31.1 -// source: wanted.sql - -package dbq - -import ( - "context" - - "github.com/jackc/pgx/v5/pgtype" -) - -const clearDefaultReleaseTypeProfile = `-- name: ClearDefaultReleaseTypeProfile :exec -UPDATE release_type_profiles SET is_default = false, updated_at = now() WHERE is_default -` - -func (q *Queries) ClearDefaultReleaseTypeProfile(ctx context.Context) error { - _, err := q.db.Exec(ctx, clearDefaultReleaseTypeProfile) - return err -} - -const clearHeldUpgrades = `-- name: ClearHeldUpgrades :execrows -UPDATE wants SET upgrade = '', held_tier = '', held_quality = '', upgrade_checked_at = now() - WHERE fulfilled_at IS NOT NULL AND upgrade <> '' - AND NOT EXISTS (SELECT 1 FROM tracks t - WHERE t.album_id = wants.album_id AND t.missing_since IS NULL AND t.duration_ms > 0) -` - -// A held want whose album has no track left to judge is not wanted again. -func (q *Queries) ClearHeldUpgrades(ctx context.Context) (int64, error) { - result, err := q.db.Exec(ctx, clearHeldUpgrades) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const countAlbumsToMatch = `-- name: CountAlbumsToMatch :one -SELECT COUNT(*) - FROM albums a - WHERE (a.created_at <= $1::timestamptz - OR EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id AND t.added_at <= $1::timestamptz)) - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id) - AND NOT EXISTS (SELECT 1 FROM library_album_matches m - WHERE m.album_id = a.id - AND (m.outcome IN ('listed', 'dismissed') - OR (m.matcher >= $2::int - AND NOT EXISTS (SELECT 1 FROM tracks t - WHERE t.album_id = a.id AND t.updated_at > m.decided_at)))) -` - -type CountAlbumsToMatchParams struct { - Cutoff pgtype.Timestamptz - Matcher int32 -} - -// Whether a finished listing has albums to go back to: those -// ListAlbumsToMatch would hand it. -func (q *Queries) CountAlbumsToMatch(ctx context.Context, arg CountAlbumsToMatchParams) (int64, error) { - row := q.db.QueryRow(ctx, countAlbumsToMatch, arg.Cutoff, arg.Matcher) - var count int64 - err := row.Scan(&count) - return count, err -} - -const countWants = `-- name: CountWants :one -SELECT COUNT(*) FILTER (WHERE fulfilled_at IS NULL AND reason <> 'missing')::bigint AS wanted, - COUNT(*) FILTER (WHERE fulfilled_at IS NULL AND reason = 'missing')::bigint AS missing, - COUNT(*) FILTER (WHERE fulfilled_at IS NOT NULL)::bigint AS held, - -- Held albums wanted for a better copy. - COUNT(*) FILTER (WHERE fulfilled_at IS NOT NULL AND upgrade <> '')::bigint AS upgrades, - COUNT(DISTINCT artist_mbid)::bigint AS artists, - -- Library albums waiting to be matched by hand. - (SELECT COUNT(*) FROM library_album_matches m - WHERE m.outcome = 'unmatched' - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = m.album_id))::bigint AS unmatched, - (SELECT COUNT(*) FROM follows)::bigint AS following, - -- The Wanted view's other tabs: downloads running, those - -- waiting on a person, and releases set aside. - (SELECT COUNT(*) FROM grabs WHERE ended_at IS NULL)::bigint AS activity, - (SELECT COUNT(*) FROM grabs - WHERE ended_at IS NULL AND import_state IN ('review', 'failed'))::bigint AS attention, - (SELECT COUNT(*) FROM release_blocks b - WHERE b.expires_at IS NULL OR b.expires_at > now())::bigint AS blocked - FROM wants -` - -type CountWantsRow struct { - Wanted int64 - Missing int64 - Held int64 - Upgrades int64 - Artists int64 - Unmatched int64 - Following int64 - Activity int64 - Attention int64 - Blocked int64 -} - -func (q *Queries) CountWants(ctx context.Context) (CountWantsRow, error) { - row := q.db.QueryRow(ctx, countWants) - var i CountWantsRow - err := row.Scan( - &i.Wanted, - &i.Missing, - &i.Held, - &i.Upgrades, - &i.Artists, - &i.Unmatched, - &i.Following, - &i.Activity, - &i.Attention, - &i.Blocked, - ) - return i, err -} - -const createReleaseTypeProfile = `-- name: CreateReleaseTypeProfile :one -INSERT INTO release_type_profiles (name, primary_types, secondary_types) -VALUES ($1, $2, $3) -RETURNING id, name, primary_types, secondary_types, is_default, created_at, updated_at -` - -type CreateReleaseTypeProfileParams struct { - Name string - PrimaryTypes []string - SecondaryTypes []string -} - -func (q *Queries) CreateReleaseTypeProfile(ctx context.Context, arg CreateReleaseTypeProfileParams) (ReleaseTypeProfile, error) { - row := q.db.QueryRow(ctx, createReleaseTypeProfile, arg.Name, arg.PrimaryTypes, arg.SecondaryTypes) - var i ReleaseTypeProfile - err := row.Scan( - &i.ID, - &i.Name, - &i.PrimaryTypes, - &i.SecondaryTypes, - &i.IsDefault, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const deleteFollow = `-- name: DeleteFollow :execrows -DELETE FROM follows WHERE artist_mbid = $1 -` - -func (q *Queries) DeleteFollow(ctx context.Context, artistMbid string) (int64, error) { - result, err := q.db.Exec(ctx, deleteFollow, artistMbid) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const deleteReleaseTypeProfile = `-- name: DeleteReleaseTypeProfile :execrows -DELETE FROM release_type_profiles WHERE id = $1 AND NOT is_default -` - -// The default stays; one in use is refused by the follows foreign key. -func (q *Queries) DeleteReleaseTypeProfile(ctx context.Context, id pgtype.UUID) (int64, error) { - result, err := q.db.Exec(ctx, deleteReleaseTypeProfile, id) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const deleteWant = `-- name: DeleteWant :execrows -DELETE FROM wants WHERE id = $1 -` - -func (q *Queries) DeleteWant(ctx context.Context, id pgtype.UUID) (int64, error) { - result, err := q.db.Exec(ctx, deleteWant, id) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const finishLibraryListing = `-- name: FinishLibraryListing :one -UPDATE acquisition_settings - SET library_listed_at = COALESCE(library_listed_at, now()), - library_unlisted = (SELECT COUNT(*) FROM library_album_matches m - WHERE m.outcome = 'unmatched' - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = m.album_id)) - WHERE id = true -RETURNING library_listed_at, library_unlisted -` - -type FinishLibraryListingRow struct { - LibraryListedAt pgtype.Timestamptz - LibraryUnlisted int32 -} - -// The listing is done, or done again after a newer matcher's retries: -// count the albums no match was found for. The first finish is kept. -func (q *Queries) FinishLibraryListing(ctx context.Context) (FinishLibraryListingRow, error) { - row := q.db.QueryRow(ctx, finishLibraryListing) - var i FinishLibraryListingRow - err := row.Scan(&i.LibraryListedAt, &i.LibraryUnlisted) - return i, err -} - -const fulfillWant = `-- name: FulfillWant :exec -UPDATE wants - SET fulfilled_at = now(), - album_id = $1, - updated_at = now() - WHERE id = $2 AND fulfilled_at IS NULL -` - -type FulfillWantParams struct { - AlbumID pgtype.UUID - ID pgtype.UUID -} - -func (q *Queries) FulfillWant(ctx context.Context, arg FulfillWantParams) error { - _, err := q.db.Exec(ctx, fulfillWant, arg.AlbumID, arg.ID) - return err -} - -const getAlbumToMatch = `-- name: GetAlbumToMatch :one -SELECT a.id, a.title, a.mbid, a.release_group_mbid, ar.name AS artist_name, ar.mbid AS artist_mbid - FROM albums a - JOIN artists ar ON ar.id = a.artist_id - JOIN library_album_matches m ON m.album_id = a.id AND m.outcome IN ('unmatched', 'dismissed') - WHERE a.id = $1 -` - -type GetAlbumToMatchRow struct { - ID pgtype.UUID - Title string - Mbid *string - ReleaseGroupMbid *string - ArtistName string - ArtistMbid *string -} - -// One album a person is matching by hand, as the matcher reads albums. -func (q *Queries) GetAlbumToMatch(ctx context.Context, id pgtype.UUID) (GetAlbumToMatchRow, error) { - row := q.db.QueryRow(ctx, getAlbumToMatch, id) - var i GetAlbumToMatchRow - err := row.Scan( - &i.ID, - &i.Title, - &i.Mbid, - &i.ReleaseGroupMbid, - &i.ArtistName, - &i.ArtistMbid, - ) - return i, err -} - -const getDefaultReleaseTypeProfile = `-- name: GetDefaultReleaseTypeProfile :one -SELECT id, name, primary_types, secondary_types, is_default, created_at, updated_at FROM release_type_profiles WHERE is_default -` - -func (q *Queries) GetDefaultReleaseTypeProfile(ctx context.Context) (ReleaseTypeProfile, error) { - row := q.db.QueryRow(ctx, getDefaultReleaseTypeProfile) - var i ReleaseTypeProfile - err := row.Scan( - &i.ID, - &i.Name, - &i.PrimaryTypes, - &i.SecondaryTypes, - &i.IsDefault, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const getFollow = `-- name: GetFollow :one -SELECT f.artist_mbid, f.artist_name, f.profile_id, f.followed_by, f.followed_at, f.baseline_at, f.want_existing, f.checked_at, f.updated_at, p.name AS profile_name - FROM follows f - JOIN release_type_profiles p ON p.id = f.profile_id - WHERE f.artist_mbid = $1 -` - -type GetFollowRow struct { - ArtistMbid string - ArtistName string - ProfileID pgtype.UUID - FollowedBy pgtype.UUID - FollowedAt pgtype.Timestamptz - BaselineAt pgtype.Timestamptz - WantExisting bool - CheckedAt pgtype.Timestamptz - UpdatedAt pgtype.Timestamptz - ProfileName string -} - -func (q *Queries) GetFollow(ctx context.Context, artistMbid string) (GetFollowRow, error) { - row := q.db.QueryRow(ctx, getFollow, artistMbid) - var i GetFollowRow - err := row.Scan( - &i.ArtistMbid, - &i.ArtistName, - &i.ProfileID, - &i.FollowedBy, - &i.FollowedAt, - &i.BaselineAt, - &i.WantExisting, - &i.CheckedAt, - &i.UpdatedAt, - &i.ProfileName, - ) - return i, err -} - -const getLibraryListing = `-- name: GetLibraryListing :one - -SELECT library_listing_started_at, library_listed_at, library_unlisted - FROM acquisition_settings WHERE id = true -` - -type GetLibraryListingRow struct { - LibraryListingStartedAt pgtype.Timestamptz - LibraryListedAt pgtype.Timestamptz - LibraryUnlisted int32 -} - -// The one-time listing of the library. --------------------------- -func (q *Queries) GetLibraryListing(ctx context.Context) (GetLibraryListingRow, error) { - row := q.db.QueryRow(ctx, getLibraryListing) - var i GetLibraryListingRow - err := row.Scan(&i.LibraryListingStartedAt, &i.LibraryListedAt, &i.LibraryUnlisted) - return i, err -} - -const getReleaseTypeProfile = `-- name: GetReleaseTypeProfile :one -SELECT id, name, primary_types, secondary_types, is_default, created_at, updated_at FROM release_type_profiles WHERE id = $1 -` - -func (q *Queries) GetReleaseTypeProfile(ctx context.Context, id pgtype.UUID) (ReleaseTypeProfile, error) { - row := q.db.QueryRow(ctx, getReleaseTypeProfile, id) - var i ReleaseTypeProfile - err := row.Scan( - &i.ID, - &i.Name, - &i.PrimaryTypes, - &i.SecondaryTypes, - &i.IsDefault, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const getWantAlbum = `-- name: GetWantAlbum :one -SELECT id, release_group_mbid, album_id FROM wants WHERE id = $1 -` - -type GetWantAlbumRow struct { - ID pgtype.UUID - ReleaseGroupMbid string - AlbumID pgtype.UUID -} - -// What the Wanted view's track list needs of a want. -func (q *Queries) GetWantAlbum(ctx context.Context, id pgtype.UUID) (GetWantAlbumRow, error) { - row := q.db.QueryRow(ctx, getWantAlbum, id) - var i GetWantAlbumRow - err := row.Scan(&i.ID, &i.ReleaseGroupMbid, &i.AlbumID) - return i, err -} - -const getWantByReleaseGroup = `-- name: GetWantByReleaseGroup :one -SELECT id, release_group_mbid, artist_mbid, artist_name, title, primary_type, first_release_date, reason, album_id, created_by, wanted_at, fulfilled_at, created_at, updated_at, searched_at, search_outcome, search_considered, best_release, best_verdict, search_error, upgrade, held_tier, held_quality, upgrade_checked_at FROM wants WHERE release_group_mbid = $1 -` - -func (q *Queries) GetWantByReleaseGroup(ctx context.Context, releaseGroupMbid string) (Want, error) { - row := q.db.QueryRow(ctx, getWantByReleaseGroup, releaseGroupMbid) - var i Want - err := row.Scan( - &i.ID, - &i.ReleaseGroupMbid, - &i.ArtistMbid, - &i.ArtistName, - &i.Title, - &i.PrimaryType, - &i.FirstReleaseDate, - &i.Reason, - &i.AlbumID, - &i.CreatedBy, - &i.WantedAt, - &i.FulfilledAt, - &i.CreatedAt, - &i.UpdatedAt, - &i.SearchedAt, - &i.SearchOutcome, - &i.SearchConsidered, - &i.BestRelease, - &i.BestVerdict, - &i.SearchError, - &i.Upgrade, - &i.HeldTier, - &i.HeldQuality, - &i.UpgradeCheckedAt, - ) - return i, err -} - -const insertLibraryWant = `-- name: InsertLibraryWant :execrows -INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, primary_type, first_release_date, - reason, album_id, wanted_at, fulfilled_at) -VALUES ($1, $2, $3, $4, - $5, $6, 'library', $7::uuid, now(), - COALESCE((SELECT MIN(t.missing_since) FROM tracks t WHERE t.album_id = $7::uuid) - - interval '1 second', now())) -ON CONFLICT (release_group_mbid) DO NOTHING -` - -type InsertLibraryWantParams struct { - ReleaseGroupMbid string - ArtistMbid string - ArtistName string - Title string - PrimaryType string - FirstReleaseDate string - AlbumID pgtype.UUID -} - -// Puts one matched album on the list as held, from just before its first -// lost track if it has one. A group already on the list is left as it is. -func (q *Queries) InsertLibraryWant(ctx context.Context, arg InsertLibraryWantParams) (int64, error) { - result, err := q.db.Exec(ctx, insertLibraryWant, - arg.ReleaseGroupMbid, - arg.ArtistMbid, - arg.ArtistName, - arg.Title, - arg.PrimaryType, - arg.FirstReleaseDate, - arg.AlbumID, - ) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const libraryHoldsReleaseGroup = `-- name: LibraryHoldsReleaseGroup :one -SELECT EXISTS ( - SELECT 1 FROM albums a - WHERE (a.release_group_mbid = $1::text - OR a.mbid = $1 - OR a.mbid IN (SELECT r.mbid FROM mb_releases r WHERE r.release_group_mbid = $1) - OR (lower(a.title) = lower($2::text) - AND a.artist_id IN (SELECT ar.id FROM artists ar WHERE ar.mbid = $3::text))) - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id AND t.missing_since IS NULL) -) -` - -type LibraryHoldsReleaseGroupParams struct { - GroupMbid string - Title string - ArtistMbid string -} - -// Whether the library already has the group: an album tagged with it or -// with one of its releases, or failing that an album of the artist's with -// the same title. Only albums with a track on disk count. -func (q *Queries) LibraryHoldsReleaseGroup(ctx context.Context, arg LibraryHoldsReleaseGroupParams) (bool, error) { - row := q.db.QueryRow(ctx, libraryHoldsReleaseGroup, arg.GroupMbid, arg.Title, arg.ArtistMbid) - var exists bool - err := row.Scan(&exists) - return exists, err -} - -const listAlbumTracksToMatch = `-- name: ListAlbumTracksToMatch :many -SELECT title, disc_number, track_number, duration_ms, mbid - FROM tracks - WHERE album_id = $1 - ORDER BY disc_number NULLS FIRST, track_number NULLS LAST, title -` - -type ListAlbumTracksToMatchRow struct { - Title string - DiscNumber *int32 - TrackNumber *int32 - DurationMs int32 - Mbid *string -} - -// What an album's tracks say about themselves, to check a match against. -func (q *Queries) ListAlbumTracksToMatch(ctx context.Context, albumID pgtype.UUID) ([]ListAlbumTracksToMatchRow, error) { - rows, err := q.db.Query(ctx, listAlbumTracksToMatch, albumID) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListAlbumTracksToMatchRow - for rows.Next() { - var i ListAlbumTracksToMatchRow - if err := rows.Scan( - &i.Title, - &i.DiscNumber, - &i.TrackNumber, - &i.DurationMs, - &i.Mbid, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listAlbumsToMatch = `-- name: ListAlbumsToMatch :many -SELECT a.id, a.title, a.mbid, a.release_group_mbid, ar.name AS artist_name, ar.mbid AS artist_mbid - FROM albums a - JOIN artists ar ON ar.id = a.artist_id - WHERE (a.created_at <= $1::timestamptz - OR EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id AND t.added_at <= $1::timestamptz)) - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id) - AND NOT EXISTS (SELECT 1 FROM library_album_matches m - WHERE m.album_id = a.id - AND (m.outcome IN ('listed', 'dismissed') - OR (m.matcher >= $2::int - AND NOT EXISTS (SELECT 1 FROM tracks t - WHERE t.album_id = a.id AND t.updated_at > m.decided_at)))) - ORDER BY a.created_at, a.id - LIMIT $3 -` - -type ListAlbumsToMatchParams struct { - Cutoff pgtype.Timestamptz - Matcher int32 - MaxRows int32 -} - -type ListAlbumsToMatchRow struct { - ID pgtype.UUID - Title string - Mbid *string - ReleaseGroupMbid *string - ArtistName string - ArtistMbid *string -} - -// Library albums the listing has not decided, or that an older matcher -// refused, or whose tracks changed since they were refused: each is matched -// to MusicBrainz. A library album is one from before the cutoff, or -// one whose tracks were: a re-tag files a track under a new album row but -// keeps the track. An album row with no tracks is not an album in -// the library; one a person left off stays off. -func (q *Queries) ListAlbumsToMatch(ctx context.Context, arg ListAlbumsToMatchParams) ([]ListAlbumsToMatchRow, error) { - rows, err := q.db.Query(ctx, listAlbumsToMatch, arg.Cutoff, arg.Matcher, arg.MaxRows) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListAlbumsToMatchRow - for rows.Next() { - var i ListAlbumsToMatchRow - if err := rows.Scan( - &i.ID, - &i.Title, - &i.Mbid, - &i.ReleaseGroupMbid, - &i.ArtistName, - &i.ArtistMbid, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listFollowGroups = `-- name: ListFollowGroups :many -SELECT g.mbid, g.title, g.primary_type, g.secondary_types, g.first_release_date, - EXISTS (SELECT 1 FROM follow_seen_groups s - WHERE s.artist_mbid = $1 AND s.release_group_mbid = g.mbid) AS seen - FROM mb_artist_release_groups ag - JOIN mb_release_groups g ON g.mbid = ag.release_group_mbid - WHERE ag.artist_mbid = $1 - ORDER BY g.first_release_date, g.title -` - -type ListFollowGroupsRow struct { - Mbid string - Title string - PrimaryType string - SecondaryTypes []string - FirstReleaseDate string - Seen bool -} - -// An artist's release groups from the catalogue, each with whether the -// follow has judged it before. -func (q *Queries) ListFollowGroups(ctx context.Context, artistMbid string) ([]ListFollowGroupsRow, error) { - rows, err := q.db.Query(ctx, listFollowGroups, artistMbid) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListFollowGroupsRow - for rows.Next() { - var i ListFollowGroupsRow - if err := rows.Scan( - &i.Mbid, - &i.Title, - &i.PrimaryType, - &i.SecondaryTypes, - &i.FirstReleaseDate, - &i.Seen, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listFollows = `-- name: ListFollows :many -SELECT f.artist_mbid, f.artist_name, f.profile_id, f.followed_at, f.baseline_at, f.checked_at, - p.name AS profile_name, - (SELECT a.id FROM artists a WHERE a.mbid = f.artist_mbid) AS artist_id, - (SELECT COUNT(*) FROM wants w - WHERE w.artist_mbid = f.artist_mbid AND w.fulfilled_at IS NULL)::bigint AS open_wants - FROM follows f - JOIN release_type_profiles p ON p.id = f.profile_id - ORDER BY lower(f.artist_name), f.artist_mbid -` - -type ListFollowsRow struct { - ArtistMbid string - ArtistName string - ProfileID pgtype.UUID - FollowedAt pgtype.Timestamptz - BaselineAt pgtype.Timestamptz - CheckedAt pgtype.Timestamptz - ProfileName string - ArtistID pgtype.UUID - OpenWants int64 -} - -func (q *Queries) ListFollows(ctx context.Context) ([]ListFollowsRow, error) { - rows, err := q.db.Query(ctx, listFollows) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListFollowsRow - for rows.Next() { - var i ListFollowsRow - if err := rows.Scan( - &i.ArtistMbid, - &i.ArtistName, - &i.ProfileID, - &i.FollowedAt, - &i.BaselineAt, - &i.CheckedAt, - &i.ProfileName, - &i.ArtistID, - &i.OpenWants, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listFollowsDue = `-- name: ListFollowsDue :many -SELECT f.artist_mbid, f.artist_name, f.profile_id, f.followed_by, f.followed_at, f.baseline_at, f.want_existing, f.checked_at, f.updated_at - FROM follows f - WHERE f.baseline_at IS NULL OR f.checked_at IS NULL OR f.checked_at < $1 - ORDER BY f.baseline_at NULLS FIRST, f.checked_at NULLS FIRST, f.artist_mbid - LIMIT $2 -` - -type ListFollowsDueParams struct { - Before pgtype.Timestamptz - MaxRows int32 -} - -// Follows whose discography has not been applied since @before, never-read -// first, so a new follow's baseline is not stuck behind daily re-checks. -func (q *Queries) ListFollowsDue(ctx context.Context, arg ListFollowsDueParams) ([]Follow, error) { - rows, err := q.db.Query(ctx, listFollowsDue, arg.Before, arg.MaxRows) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Follow - for rows.Next() { - var i Follow - if err := rows.Scan( - &i.ArtistMbid, - &i.ArtistName, - &i.ProfileID, - &i.FollowedBy, - &i.FollowedAt, - &i.BaselineAt, - &i.WantExisting, - &i.CheckedAt, - &i.UpdatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listHeldWantFormats = `-- name: ListHeldWantFormats :many - -SELECT w.id AS want_id, w.upgrade, w.held_tier, w.held_quality, - lower(t.file_format)::text AS file_format, - SUM(t.file_size)::bigint AS bytes, - SUM(t.duration_ms)::bigint AS duration_ms, - COUNT(*)::int AS tracks, - COUNT(*) FILTER (WHERE t.source_verdict = 'suspect')::int AS rips - FROM wants w - JOIN tracks t ON t.album_id = w.album_id - WHERE w.fulfilled_at IS NOT NULL - AND t.missing_since IS NULL AND t.duration_ms > 0 - GROUP BY w.id, lower(t.file_format) - ORDER BY w.id -` - -type ListHeldWantFormatsRow struct { - WantID pgtype.UUID - Upgrade string - HeldTier string - HeldQuality string - FileFormat string - Bytes int64 - DurationMs int64 - Tracks int32 - Rips int32 -} - -// Upgrades. ------------------------------------------------------- -// What each held album's copy is made of, for judging whether it is worth -// upgrading: its present tracks grouped by format, with their bytes and -// length summed, and how many are held back as video rips. A held want whose -// album has no track with a length yet has no rows. -func (q *Queries) ListHeldWantFormats(ctx context.Context) ([]ListHeldWantFormatsRow, error) { - rows, err := q.db.Query(ctx, listHeldWantFormats) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListHeldWantFormatsRow - for rows.Next() { - var i ListHeldWantFormatsRow - if err := rows.Scan( - &i.WantID, - &i.Upgrade, - &i.HeldTier, - &i.HeldQuality, - &i.FileFormat, - &i.Bytes, - &i.DurationMs, - &i.Tracks, - &i.Rips, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listLibraryAlbumTracks = `-- name: ListLibraryAlbumTracks :many -SELECT title, disc_number, track_number, duration_ms, (missing_since IS NOT NULL)::bool AS missing - FROM tracks - WHERE album_id = $1 - ORDER BY disc_number NULLS FIRST, track_number NULLS LAST, title -` - -type ListLibraryAlbumTracksRow struct { - Title string - DiscNumber *int32 - TrackNumber *int32 - DurationMs int32 - Missing bool -} - -// A held or missing album's own tracks, for the Wanted view's track list; -// missing says its file is gone. -func (q *Queries) ListLibraryAlbumTracks(ctx context.Context, albumID pgtype.UUID) ([]ListLibraryAlbumTracksRow, error) { - rows, err := q.db.Query(ctx, listLibraryAlbumTracks, albumID) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListLibraryAlbumTracksRow - for rows.Next() { - var i ListLibraryAlbumTracksRow - if err := rows.Scan( - &i.Title, - &i.DiscNumber, - &i.TrackNumber, - &i.DurationMs, - &i.Missing, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listLibraryAlbums = `-- name: ListLibraryAlbums :execrows -INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id, wanted_at, fulfilled_at) -SELECT DISTINCT ON (a.release_group_mbid) - a.release_group_mbid, ar.mbid, ar.name, a.title, 'library', a.id, now(), - COALESCE((SELECT MIN(t.missing_since) FROM tracks t WHERE t.album_id = a.id) - interval '1 second', now()) - FROM albums a - JOIN artists ar ON ar.id = a.artist_id - WHERE a.release_group_mbid IS NOT NULL AND a.release_group_mbid <> '' - AND ar.mbid IS NOT NULL AND ar.mbid <> '' - AND a.created_at <= $1::timestamptz - AND ($2::uuid IS NULL OR a.id = $2::uuid) - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id) - ORDER BY a.release_group_mbid, - (SELECT COUNT(*) FROM tracks t WHERE t.album_id = a.id AND t.missing_since IS NULL) DESC, a.id -ON CONFLICT (release_group_mbid) DO NOTHING -` - -type ListLibraryAlbumsParams struct { - Cutoff pgtype.Timestamptz - AlbumID pgtype.UUID -} - -// Puts library albums on the list as held: each release group once, by the -// album holding the most tracks. Only albums the library had at the cutoff, -// and with album_id given only that album. A group already on the list is -// left as it is. An album with tracks already missing is held from just -// before the first loss, so re-acquisition still fetches it back. -func (q *Queries) ListLibraryAlbums(ctx context.Context, arg ListLibraryAlbumsParams) (int64, error) { - result, err := q.db.Exec(ctx, listLibraryAlbums, arg.Cutoff, arg.AlbumID) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const listOpenWants = `-- name: ListOpenWants :many -SELECT id, release_group_mbid, artist_mbid, artist_name, title, primary_type, first_release_date, reason, album_id, created_by, wanted_at, fulfilled_at, created_at, updated_at, searched_at, search_outcome, search_considered, best_release, best_verdict, search_error, upgrade, held_tier, held_quality, upgrade_checked_at FROM wants - WHERE fulfilled_at IS NULL - ORDER BY wanted_at, id - LIMIT $1 -` - -func (q *Queries) ListOpenWants(ctx context.Context, limit int32) ([]Want, error) { - rows, err := q.db.Query(ctx, listOpenWants, limit) - if err != nil { - return nil, err - } - defer rows.Close() - var items []Want - for rows.Next() { - var i Want - if err := rows.Scan( - &i.ID, - &i.ReleaseGroupMbid, - &i.ArtistMbid, - &i.ArtistName, - &i.Title, - &i.PrimaryType, - &i.FirstReleaseDate, - &i.Reason, - &i.AlbumID, - &i.CreatedBy, - &i.WantedAt, - &i.FulfilledAt, - &i.CreatedAt, - &i.UpdatedAt, - &i.SearchedAt, - &i.SearchOutcome, - &i.SearchConsidered, - &i.BestRelease, - &i.BestVerdict, - &i.SearchError, - &i.Upgrade, - &i.HeldTier, - &i.HeldQuality, - &i.UpgradeCheckedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listReleaseTypeProfiles = `-- name: ListReleaseTypeProfiles :many - -SELECT p.id, p.name, p.primary_types, p.secondary_types, p.is_default, p.created_at, p.updated_at, - (SELECT COUNT(*) FROM follows f WHERE f.profile_id = p.id)::bigint AS follow_count - FROM release_type_profiles p - ORDER BY p.is_default DESC, p.name -` - -type ListReleaseTypeProfilesRow struct { - ID pgtype.UUID - Name string - PrimaryTypes []string - SecondaryTypes []string - IsDefault bool - CreatedAt pgtype.Timestamptz - UpdatedAt pgtype.Timestamptz - FollowCount int64 -} - -// The wanted list: release-type profiles, follows and wants. -func (q *Queries) ListReleaseTypeProfiles(ctx context.Context) ([]ListReleaseTypeProfilesRow, error) { - rows, err := q.db.Query(ctx, listReleaseTypeProfiles) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListReleaseTypeProfilesRow - for rows.Next() { - var i ListReleaseTypeProfilesRow - if err := rows.Scan( - &i.ID, - &i.Name, - &i.PrimaryTypes, - &i.SecondaryTypes, - &i.IsDefault, - &i.CreatedAt, - &i.UpdatedAt, - &i.FollowCount, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listUnmatchedLibraryAlbums = `-- name: ListUnmatchedLibraryAlbums :many -SELECT m.album_id, a.title, ar.name AS artist_name, m.detail, m.outcome, m.decided_at, - (SELECT COUNT(*) FROM tracks t WHERE t.album_id = a.id AND t.missing_since IS NULL)::int AS tracks - FROM library_album_matches m - JOIN albums a ON a.id = m.album_id - JOIN artists ar ON ar.id = a.artist_id - WHERE m.outcome IN ('unmatched', 'dismissed') - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = m.album_id) - ORDER BY m.outcome DESC, lower(ar.name), lower(a.title) - LIMIT $1 -` - -type ListUnmatchedLibraryAlbumsRow struct { - AlbumID pgtype.UUID - Title string - ArtistName string - Detail string - Outcome string - DecidedAt pgtype.Timestamptz - Tracks int32 -} - -// The Collection's Unmatched section: the albums the listing could -// not match, and below them the ones a person left off. An album whose -// tracks have all moved to another row is gone, not unmatched. -func (q *Queries) ListUnmatchedLibraryAlbums(ctx context.Context, maxRows int32) ([]ListUnmatchedLibraryAlbumsRow, error) { - rows, err := q.db.Query(ctx, listUnmatchedLibraryAlbums, maxRows) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListUnmatchedLibraryAlbumsRow - for rows.Next() { - var i ListUnmatchedLibraryAlbumsRow - if err := rows.Scan( - &i.AlbumID, - &i.Title, - &i.ArtistName, - &i.Detail, - &i.Outcome, - &i.DecidedAt, - &i.Tracks, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listWantArtists = `-- name: ListWantArtists :many -WITH a AS ( - SELECT w.artist_mbid, - (array_agg(w.artist_name ORDER BY w.wanted_at DESC))[1] AS artist_name, - COUNT(*) FILTER (WHERE w.fulfilled_at IS NOT NULL)::int AS held, - COUNT(*) FILTER (WHERE w.fulfilled_at IS NULL AND w.reason <> 'missing')::int AS wanted, - COUNT(*) FILTER (WHERE w.fulfilled_at IS NULL AND w.reason = 'missing')::int AS missing, - COUNT(*)::int AS albums, - bool_or($1::text <> '' AND strpos(lower(w.title), lower($1::text)) > 0) AS title_match - FROM wants w - WHERE $4::text = '' OR w.artist_mbid = $4::text - GROUP BY w.artist_mbid -) -SELECT a.artist_mbid, a.artist_name::text AS artist_name, a.held, a.wanted, a.missing, a.albums, - COALESCE(NULLIF(la.sort_name, ''), a.artist_name)::text AS sort_name, - la.id AS library_artist_id, - (f.artist_mbid IS NOT NULL)::bool AS following, - (SELECT COUNT(*) FROM grabs g JOIN wants x ON x.id = g.want_id - WHERE x.artist_mbid = a.artist_mbid AND g.ended_at IS NULL)::int AS downloading, - COUNT(*) OVER () AS total - FROM a - LEFT JOIN LATERAL (SELECT ar.id, ar.sort_name FROM artists ar WHERE ar.mbid = a.artist_mbid LIMIT 1) la ON true - LEFT JOIN follows f ON f.artist_mbid = a.artist_mbid - WHERE $1::text = '' - OR strpos(lower(a.artist_name), lower($1::text)) > 0 - OR a.title_match - ORDER BY lower(COALESCE(NULLIF(la.sort_name, ''), a.artist_name)), a.artist_mbid - LIMIT $3 OFFSET $2 -` - -type ListWantArtistsParams struct { - Q string - Skip int32 - MaxRows int32 - ArtistMbid string -} - -type ListWantArtistsRow struct { - ArtistMbid string - ArtistName string - Held int32 - Wanted int32 - Missing int32 - Albums int32 - SortName string - LibraryArtistID pgtype.UUID - Following bool - Downloading int32 - Total int64 -} - -// The Wanted view's main page: every artist on the list, A–Z like -// the library, with how many of their albums are held of how many are on -// the list. q matches the artist's name or any of their albums' titles; -// artist_mbid narrows to one artist, for their page's header. -func (q *Queries) ListWantArtists(ctx context.Context, arg ListWantArtistsParams) ([]ListWantArtistsRow, error) { - rows, err := q.db.Query(ctx, listWantArtists, - arg.Q, - arg.Skip, - arg.MaxRows, - arg.ArtistMbid, - ) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListWantArtistsRow - for rows.Next() { - var i ListWantArtistsRow - if err := rows.Scan( - &i.ArtistMbid, - &i.ArtistName, - &i.Held, - &i.Wanted, - &i.Missing, - &i.Albums, - &i.SortName, - &i.LibraryArtistID, - &i.Following, - &i.Downloading, - &i.Total, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const listWants = `-- name: ListWants :many -SELECT w.id, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title, w.reason, - w.album_id, w.wanted_at, w.fulfilled_at, - -- Why a held album is wanted again, and the copy it has. - w.upgrade, w.held_quality, - -- A request names an album before the catalogue has read it: the - -- catalogue's type and date fill in once it has. - COALESCE(NULLIF(w.primary_type, ''), g.primary_type, '')::text AS primary_type, - COALESCE(g.secondary_types, '{}')::text[] AS secondary_types, - COALESCE(NULLIF(w.first_release_date, ''), g.first_release_date, '')::text AS first_release_date, - u.username AS requested_by, - la.id AS library_artist_id, - COUNT(*) OVER () AS total, - -- The search schedule's last look, and the download running. - w.searched_at, w.search_outcome, w.search_considered, w.best_release, w.best_verdict, w.search_error, - -- Its latest download, running or ended, and what the import made of - -- it. - gr.id AS grab_id, gr.name AS grab_name, gr.grabbed_at, gr.origin AS grab_origin, - gr.quality AS grab_quality, gr.ended_at AS grab_ended_at, gr.end_reason AS grab_end_reason, - gr.progress AS grab_progress, gr.import_state AS grab_import_state, - gr.import_reason AS grab_import_reason, gr.end_detail AS grab_end_detail, gr.imported_at AS grab_imported_at, - gr.imported_files AS grab_imported_files - FROM wants w - LEFT JOIN users u ON u.id = w.created_by - LEFT JOIN mb_release_groups g ON g.mbid = w.release_group_mbid - LEFT JOIN LATERAL (SELECT ar.id, ar.sort_name FROM artists ar WHERE ar.mbid = w.artist_mbid LIMIT 1) la ON true - LEFT JOIN grabs gr ON gr.id = (SELECT x.id FROM grabs x WHERE x.want_id = w.id - ORDER BY x.ended_at IS NULL DESC, x.grabbed_at DESC LIMIT 1) - WHERE CASE $1::text - WHEN 'all' THEN true - WHEN 'held' THEN w.fulfilled_at IS NOT NULL - WHEN 'upgrade' THEN w.fulfilled_at IS NOT NULL AND w.upgrade <> '' - WHEN 'missing' THEN w.fulfilled_at IS NULL AND w.reason = 'missing' - ELSE w.fulfilled_at IS NULL AND w.reason <> 'missing' - END - AND ($2::text = '' OR w.artist_mbid = $2::text) - -- want_ids narrows to given wants: the Missing page's folders. - AND (COALESCE(cardinality($3::uuid[]), 0) = 0 OR w.id = ANY($3::uuid[])) - AND ($4::text = '' - OR strpos(lower(w.artist_name), lower($4::text)) > 0 - OR strpos(lower(w.title), lower($4::text)) > 0) - ORDER BY lower(COALESCE(NULLIF(la.sort_name, ''), w.artist_name)), w.artist_mbid, - NULLIF(COALESCE(NULLIF(w.first_release_date, ''), g.first_release_date, ''), '') NULLS LAST, - lower(w.title), w.id - LIMIT $6 OFFSET $5 -` - -type ListWantsParams struct { - State string - ArtistMbid string - WantIds []pgtype.UUID - Q string - Skip int32 - MaxRows int32 -} - -type ListWantsRow struct { - ID pgtype.UUID - ReleaseGroupMbid string - ArtistMbid string - ArtistName string - Title string - Reason string - AlbumID pgtype.UUID - WantedAt pgtype.Timestamptz - FulfilledAt pgtype.Timestamptz - Upgrade string - HeldQuality string - PrimaryType string - SecondaryTypes []string - FirstReleaseDate string - RequestedBy *string - LibraryArtistID pgtype.UUID - Total int64 - SearchedAt pgtype.Timestamptz - SearchOutcome string - SearchConsidered int32 - BestRelease string - BestVerdict string - SearchError string - GrabID pgtype.UUID - GrabName *string - GrabbedAt pgtype.Timestamptz - GrabOrigin *string - GrabQuality *string - GrabEndedAt pgtype.Timestamptz - GrabEndReason *string - GrabProgress *float32 - GrabImportState *string - GrabImportReason *string - GrabEndDetail *string - GrabImportedAt pgtype.Timestamptz - GrabImportedFiles *int32 -} - -// The Wanted page: open wants (all but missing ones, or only missing ones), -// the held ones, or all of them; artist_mbid narrows to one artist's page, -// want_ids to given wants. Ordered like the library: artists A–Z by the -// library's sort name when it holds the artist, each artist's albums by -// release date, so the page groups them as they arrive. q narrows to an -// artist or album title; total counts every match, past the page. -func (q *Queries) ListWants(ctx context.Context, arg ListWantsParams) ([]ListWantsRow, error) { - rows, err := q.db.Query(ctx, listWants, - arg.State, - arg.ArtistMbid, - arg.WantIds, - arg.Q, - arg.Skip, - arg.MaxRows, - ) - if err != nil { - return nil, err - } - defer rows.Close() - var items []ListWantsRow - for rows.Next() { - var i ListWantsRow - if err := rows.Scan( - &i.ID, - &i.ReleaseGroupMbid, - &i.ArtistMbid, - &i.ArtistName, - &i.Title, - &i.Reason, - &i.AlbumID, - &i.WantedAt, - &i.FulfilledAt, - &i.Upgrade, - &i.HeldQuality, - &i.PrimaryType, - &i.SecondaryTypes, - &i.FirstReleaseDate, - &i.RequestedBy, - &i.LibraryArtistID, - &i.Total, - &i.SearchedAt, - &i.SearchOutcome, - &i.SearchConsidered, - &i.BestRelease, - &i.BestVerdict, - &i.SearchError, - &i.GrabID, - &i.GrabName, - &i.GrabbedAt, - &i.GrabOrigin, - &i.GrabQuality, - &i.GrabEndedAt, - &i.GrabEndReason, - &i.GrabProgress, - &i.GrabImportState, - &i.GrabImportReason, - &i.GrabEndDetail, - &i.GrabImportedAt, - &i.GrabImportedFiles, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const markFollowChecked = `-- name: MarkFollowChecked :exec -UPDATE follows - SET checked_at = now(), - baseline_at = COALESCE(baseline_at, now()), - want_existing = false, - updated_at = now() - WHERE artist_mbid = $1 -` - -func (q *Queries) MarkFollowChecked(ctx context.Context, artistMbid string) error { - _, err := q.db.Exec(ctx, markFollowChecked, artistMbid) - return err -} - -const markGroupSeen = `-- name: MarkGroupSeen :exec -INSERT INTO follow_seen_groups (artist_mbid, release_group_mbid) -VALUES ($1, $2) -ON CONFLICT DO NOTHING -` - -type MarkGroupSeenParams struct { - ArtistMbid string - ReleaseGroupMbid string -} - -func (q *Queries) MarkGroupSeen(ctx context.Context, arg MarkGroupSeenParams) error { - _, err := q.db.Exec(ctx, markGroupSeen, arg.ArtistMbid, arg.ReleaseGroupMbid) - return err -} - -const recordAlbumMatch = `-- name: RecordAlbumMatch :exec -INSERT INTO library_album_matches (album_id, outcome, method, release_group_mbid, detail, matcher) -VALUES ($1, $2, $3, $4, $5, - $6) -ON CONFLICT (album_id) DO UPDATE - SET outcome = EXCLUDED.outcome, method = EXCLUDED.method, release_group_mbid = EXCLUDED.release_group_mbid, - detail = EXCLUDED.detail, matcher = EXCLUDED.matcher, decided_at = now() -` - -type RecordAlbumMatchParams struct { - AlbumID pgtype.UUID - Outcome string - Method string - ReleaseGroupMbid string - Detail string - Matcher int32 -} - -func (q *Queries) RecordAlbumMatch(ctx context.Context, arg RecordAlbumMatchParams) error { - _, err := q.db.Exec(ctx, recordAlbumMatch, - arg.AlbumID, - arg.Outcome, - arg.Method, - arg.ReleaseGroupMbid, - arg.Detail, - arg.Matcher, - ) - return err -} - -const recordListedAlbums = `-- name: RecordListedAlbums :exec -INSERT INTO library_album_matches (album_id, outcome, method, release_group_mbid) -SELECT a.id, 'listed', 'tags', a.release_group_mbid - FROM albums a - JOIN artists ar ON ar.id = a.artist_id - WHERE a.release_group_mbid IS NOT NULL AND a.release_group_mbid <> '' - AND ar.mbid IS NOT NULL AND ar.mbid <> '' - AND a.created_at <= $1::timestamptz - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id) -ON CONFLICT (album_id) DO NOTHING -` - -// The albums ListLibraryAlbums takes are decided as listed by their tags, -// so the matching that follows leaves them alone. -func (q *Queries) RecordListedAlbums(ctx context.Context, cutoff pgtype.Timestamptz) error { - _, err := q.db.Exec(ctx, recordListedAlbums, cutoff) - return err -} - -const recountLibraryUnlisted = `-- name: RecountLibraryUnlisted :exec -UPDATE acquisition_settings - SET library_unlisted = (SELECT COUNT(*) FROM library_album_matches m - WHERE m.outcome = 'unmatched' - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = m.album_id)) - WHERE id = true -` - -// After a person matches or leaves off an album, the count the catalogue -// shows. -func (q *Queries) RecountLibraryUnlisted(ctx context.Context) error { - _, err := q.db.Exec(ctx, recountLibraryUnlisted) - return err -} - -const repointWant = `-- name: RepointWant :exec -UPDATE wants SET release_group_mbid = $2, updated_at = now() WHERE id = $1 -` - -type RepointWantParams struct { - ID pgtype.UUID - ReleaseGroupMbid string -} - -// A want recorded under a release id is moved to its group. -func (q *Queries) RepointWant(ctx context.Context, arg RepointWantParams) error { - _, err := q.db.Exec(ctx, repointWant, arg.ID, arg.ReleaseGroupMbid) - return err -} - -const setAlbumMatchOutcome = `-- name: SetAlbumMatchOutcome :execrows -UPDATE library_album_matches SET outcome = $1, decided_at = now() - WHERE album_id = $2 AND outcome = $3 -` - -type SetAlbumMatchOutcomeParams struct { - Outcome string - AlbumID pgtype.UUID - Was string -} - -// Leave an unmatched album off the list, or put a left-off one back. -func (q *Queries) SetAlbumMatchOutcome(ctx context.Context, arg SetAlbumMatchOutcomeParams) (int64, error) { - result, err := q.db.Exec(ctx, setAlbumMatchOutcome, arg.Outcome, arg.AlbumID, arg.Was) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const setAlbumReleaseGroup = `-- name: SetAlbumReleaseGroup :exec -UPDATE albums SET release_group_mbid = $1, updated_at = now() - WHERE id = $2 AND (release_group_mbid IS NULL OR release_group_mbid = '') -` - -type SetAlbumReleaseGroupParams struct { - ReleaseGroupMbid *string - ID pgtype.UUID -} - -// Names a matched album's release group, filling only what nothing named. -func (q *Queries) SetAlbumReleaseGroup(ctx context.Context, arg SetAlbumReleaseGroupParams) error { - _, err := q.db.Exec(ctx, setAlbumReleaseGroup, arg.ReleaseGroupMbid, arg.ID) - return err -} - -const setDefaultReleaseTypeProfile = `-- name: SetDefaultReleaseTypeProfile :execrows -UPDATE release_type_profiles SET is_default = true, updated_at = now() WHERE id = $1 -` - -func (q *Queries) SetDefaultReleaseTypeProfile(ctx context.Context, id pgtype.UUID) (int64, error) { - result, err := q.db.Exec(ctx, setDefaultReleaseTypeProfile, id) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil -} - -const setWantUpgrades = `-- name: SetWantUpgrades :exec -UPDATE wants w - SET upgrade = v.upgrade, - held_tier = v.held_tier, - held_quality = v.held_quality, - upgrade_checked_at = now(), - updated_at = CASE WHEN w.upgrade <> v.upgrade THEN now() ELSE w.updated_at END - FROM (SELECT unnest($1::uuid[]) AS id, - unnest($2::text[]) AS upgrade, - unnest($3::text[]) AS held_tier, - unnest($4::text[]) AS held_quality) v - WHERE w.id = v.id -` - -type SetWantUpgradesParams struct { - Ids []pgtype.UUID - Upgrades []string - HeldTiers []string - HeldQualities []string -} - -// Writes the upgrade check's verdicts, one row per want. -func (q *Queries) SetWantUpgrades(ctx context.Context, arg SetWantUpgradesParams) error { - _, err := q.db.Exec(ctx, setWantUpgrades, - arg.Ids, - arg.Upgrades, - arg.HeldTiers, - arg.HeldQualities, - ) - return err -} - -const startLibraryListing = `-- name: StartLibraryListing :one -UPDATE acquisition_settings - SET library_listing_started_at = COALESCE(library_listing_started_at, now()) - WHERE id = true -RETURNING library_listing_started_at -` - -// Marks the listing begun, once; its time is the cutoff for which albums -// count as the library's. -func (q *Queries) StartLibraryListing(ctx context.Context) (pgtype.Timestamptz, error) { - row := q.db.QueryRow(ctx, startLibraryListing) - var library_listing_started_at pgtype.Timestamptz - err := row.Scan(&library_listing_started_at) - return library_listing_started_at, err -} - -const updateReleaseTypeProfile = `-- name: UpdateReleaseTypeProfile :one -UPDATE release_type_profiles - SET name = $2, - primary_types = $3, - secondary_types = $4, - updated_at = now() - WHERE id = $1 -RETURNING id, name, primary_types, secondary_types, is_default, created_at, updated_at -` - -type UpdateReleaseTypeProfileParams struct { - ID pgtype.UUID - Name string - PrimaryTypes []string - SecondaryTypes []string -} - -func (q *Queries) UpdateReleaseTypeProfile(ctx context.Context, arg UpdateReleaseTypeProfileParams) (ReleaseTypeProfile, error) { - row := q.db.QueryRow(ctx, updateReleaseTypeProfile, - arg.ID, - arg.Name, - arg.PrimaryTypes, - arg.SecondaryTypes, - ) - var i ReleaseTypeProfile - err := row.Scan( - &i.ID, - &i.Name, - &i.PrimaryTypes, - &i.SecondaryTypes, - &i.IsDefault, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} - -const upsertFollow = `-- name: UpsertFollow :one -INSERT INTO follows (artist_mbid, artist_name, profile_id, followed_by, want_existing) -VALUES ($1, $2, $3, - $4, $5) -ON CONFLICT (artist_mbid) DO UPDATE - SET artist_name = EXCLUDED.artist_name, - profile_id = EXCLUDED.profile_id, - want_existing = follows.want_existing OR EXCLUDED.want_existing, - baseline_at = CASE WHEN EXCLUDED.want_existing THEN NULL ELSE follows.baseline_at END, - updated_at = now() -RETURNING artist_mbid, artist_name, profile_id, followed_by, followed_at, baseline_at, want_existing, checked_at, updated_at -` - -type UpsertFollowParams struct { - ArtistMbid string - ArtistName string - ProfileID pgtype.UUID - FollowedBy pgtype.UUID - WantExisting bool -} - -// Following an artist already followed changes its profile. want_existing -// only ever turns on here; the worker turns it off once applied, and turning -// it on again re-reads the baseline so the existing releases are judged. -func (q *Queries) UpsertFollow(ctx context.Context, arg UpsertFollowParams) (Follow, error) { - row := q.db.QueryRow(ctx, upsertFollow, - arg.ArtistMbid, - arg.ArtistName, - arg.ProfileID, - arg.FollowedBy, - arg.WantExisting, - ) - var i Follow - err := row.Scan( - &i.ArtistMbid, - &i.ArtistName, - &i.ProfileID, - &i.FollowedBy, - &i.FollowedAt, - &i.BaselineAt, - &i.WantExisting, - &i.CheckedAt, - &i.UpdatedAt, - ) - return i, err -} - -const wantHoldings = `-- name: WantHoldings :many -SELECT a.id AS album_id, - COUNT(*) FILTER (WHERE t.missing_since IS NULL)::int AS present, - COALESCE(bool_or(t.missing_since IS NULL AND t.added_at > $1::timestamptz), false)::bool AS arrived_since, - COALESCE(bool_or(t.missing_since IS NOT NULL AND t.missing_since < $1::timestamptz), false)::bool AS missing_before, - -- Missing tracks no track on disk stands in for, by position or - -- title. A missing row whose song is back at a new path is - -- covered; a lost bonus track is not. A track with no disc number is - -- on disc 1, so a missing track 6 with no disc beside a present 1/6 - -- does not keep the album wanted. - COUNT(*) FILTER (WHERE t.missing_since IS NOT NULL AND NOT EXISTS ( - SELECT 1 FROM tracks p - WHERE p.album_id = a.id AND p.missing_since IS NULL - AND ((p.track_number = t.track_number AND COALESCE(p.disc_number, 1) = COALESCE(t.disc_number, 1)) - OR lower(p.title) = lower(t.title))))::int AS uncovered_missing - FROM albums a - JOIN tracks t ON t.album_id = a.id - WHERE a.release_group_mbid = $2::text - OR a.mbid = $2 - OR a.id = $3 - OR a.mbid IN (SELECT r.mbid FROM mb_releases r WHERE r.release_group_mbid = $2) - GROUP BY a.id - ORDER BY present DESC, a.id -` - -type WantHoldingsParams struct { - Since pgtype.Timestamptz - GroupMbid string - AlbumID pgtype.UUID -} - -type WantHoldingsRow struct { - AlbumID pgtype.UUID - Present int32 - ArrivedSince bool - MissingBefore bool - UncoveredMissing int32 -} - -// Each library album that may answer a want, with what it has on disk: -// tracks present, whether one arrived since the want, and whether a track -// missing before the want is still missing. -func (q *Queries) WantHoldings(ctx context.Context, arg WantHoldingsParams) ([]WantHoldingsRow, error) { - rows, err := q.db.Query(ctx, wantHoldings, arg.Since, arg.GroupMbid, arg.AlbumID) - if err != nil { - return nil, err - } - defer rows.Close() - var items []WantHoldingsRow - for rows.Next() { - var i WantHoldingsRow - if err := rows.Scan( - &i.AlbumID, - &i.Present, - &i.ArrivedSince, - &i.MissingBefore, - &i.UncoveredMissing, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - -const wantReleaseGroup = `-- name: WantReleaseGroup :one -INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, primary_type, - first_release_date, reason, created_by) -VALUES ($1, $2, $3, $4, - $5, $6, $7, $8) -ON CONFLICT (release_group_mbid) DO UPDATE SET updated_at = wants.updated_at -RETURNING id, release_group_mbid, artist_mbid, artist_name, title, primary_type, first_release_date, reason, album_id, created_by, wanted_at, fulfilled_at, created_at, updated_at, searched_at, search_outcome, search_considered, best_release, best_verdict, search_error, upgrade, held_tier, held_quality, upgrade_checked_at -` - -type WantReleaseGroupParams struct { - ReleaseGroupMbid string - ArtistMbid string - ArtistName string - Title string - PrimaryType string - FirstReleaseDate string - Reason string - CreatedBy pgtype.UUID -} - -// Wants a release group, or returns the want already recorded for it. A -// held want is not reopened here: the library has it. -func (q *Queries) WantReleaseGroup(ctx context.Context, arg WantReleaseGroupParams) (Want, error) { - row := q.db.QueryRow(ctx, wantReleaseGroup, - arg.ReleaseGroupMbid, - arg.ArtistMbid, - arg.ArtistName, - arg.Title, - arg.PrimaryType, - arg.FirstReleaseDate, - arg.Reason, - arg.CreatedBy, - ) - var i Want - err := row.Scan( - &i.ID, - &i.ReleaseGroupMbid, - &i.ArtistMbid, - &i.ArtistName, - &i.Title, - &i.PrimaryType, - &i.FirstReleaseDate, - &i.Reason, - &i.AlbumID, - &i.CreatedBy, - &i.WantedAt, - &i.FulfilledAt, - &i.CreatedAt, - &i.UpdatedAt, - &i.SearchedAt, - &i.SearchOutcome, - &i.SearchConsidered, - &i.BestRelease, - &i.BestVerdict, - &i.SearchError, - &i.Upgrade, - &i.HeldTier, - &i.HeldQuality, - &i.UpgradeCheckedAt, - ) - return i, err -} diff --git a/internal/db/queries/acquisition.sql b/internal/db/queries/acquisition.sql deleted file mode 100644 index 82e11b15..00000000 --- a/internal/db/queries/acquisition.sql +++ /dev/null @@ -1,76 +0,0 @@ --- Indexers and download clients. - --- name: ListAcquisitionServices :many --- Returns no secret, only whether one is set: this feeds the settings page. -SELECT code, kind, protocol, label, enabled, base_url, - (api_key <> '')::boolean AS has_api_key, - (password <> '')::boolean AS has_password, - username, category, timeout_ms, - serves_search, serves_downloads, serves_enrichment, - local_path, remote_path, searches_per_hour, - created_at, updated_at -FROM acquisition_services -ORDER BY kind, label, code; - --- name: GetAcquisitionService :one --- The full row, credentials included. For building a client, never for a --- response. -SELECT * FROM acquisition_services WHERE code = sqlc.arg(code); - --- name: ListUsableAcquisitionServices :many --- Every enabled, addressed service doing one job. An exchange row does a job --- when its switch for it is on; any other row does the job its kind names. --- Ordered by code so a fan-out is deterministic. -SELECT * FROM acquisition_services -WHERE enabled AND base_url <> '' - AND CASE WHEN protocol = 'exchange' THEN - CASE sqlc.arg(job)::text - WHEN 'search' THEN serves_search - WHEN 'download' THEN serves_downloads - WHEN 'enrichment' THEN serves_enrichment - ELSE false - END - ELSE kind = sqlc.arg(job)::text - END -ORDER BY code; - --- name: CreateAcquisitionService :one --- ON CONFLICT so the caller can try the next code when this one is taken. -INSERT INTO acquisition_services - (code, kind, protocol, label, category, timeout_ms, searches_per_hour) -VALUES - (sqlc.arg(code), sqlc.arg(kind), sqlc.arg(protocol), sqlc.arg(label), - sqlc.arg(category), sqlc.arg(timeout_ms), sqlc.arg(searches_per_hour)) -ON CONFLICT (code) DO NOTHING -RETURNING *; - --- name: UpdateAcquisitionService :one --- Everything on the settings form except the credentials, which have their --- own write-only calls. -UPDATE acquisition_services SET - label = sqlc.arg(label), - enabled = sqlc.arg(enabled), - base_url = sqlc.arg(base_url), - username = sqlc.arg(username), - category = sqlc.arg(category), - timeout_ms = sqlc.arg(timeout_ms), - serves_search = sqlc.arg(serves_search), - serves_downloads = sqlc.arg(serves_downloads), - serves_enrichment = sqlc.arg(serves_enrichment), - local_path = sqlc.arg(local_path), - remote_path = sqlc.arg(remote_path), - searches_per_hour = sqlc.arg(searches_per_hour), - updated_at = now() -WHERE code = sqlc.arg(code) -RETURNING *; - --- name: DeleteAcquisitionService :execrows -DELETE FROM acquisition_services WHERE code = sqlc.arg(code); - --- name: SetAcquisitionServiceAPIKey :execrows -UPDATE acquisition_services SET api_key = sqlc.arg(api_key), updated_at = now() -WHERE code = sqlc.arg(code); - --- name: SetAcquisitionServicePassword :execrows -UPDATE acquisition_services SET password = sqlc.arg(password), updated_at = now() -WHERE code = sqlc.arg(code); diff --git a/internal/db/queries/admin_status.sql b/internal/db/queries/admin_status.sql index e36b2a37..95471f56 100644 --- a/internal/db/queries/admin_status.sql +++ b/internal/db/queries/admin_status.sql @@ -1,5 +1,5 @@ --- The admin Overview's status sections: who is listening, what is --- downloading, what the library holds. Each is one cheap read. +-- The admin Overview's status sections: who is listening and what the +-- library holds. Each is one cheap read. -- name: ListListeningNow :many -- Plays in progress: open play events still inside their track's length, @@ -29,20 +29,6 @@ SELECT COUNT(*)::bigint AS plays, FROM play_events WHERE started_at > now() - INTERVAL '24 hours'; --- name: DownloadsStatus :one --- Downloads at a glance: running, waiting on a person (and since when), and --- the last day's imports and give-ups. -SELECT COUNT(*) FILTER (WHERE ended_at IS NULL AND import_state IN ('', 'downloading', 'waiting', 'importing'))::bigint AS running, - COUNT(*) FILTER (WHERE ended_at IS NULL AND import_state IN ('review', 'failed'))::bigint AS needs_you, - MIN(COALESCE(import_updated_at, grabbed_at)) FILTER (WHERE ended_at IS NULL AND import_state IN ('review', 'failed'))::timestamptz AS needs_you_since, - COUNT(*) FILTER (WHERE imported_at > now() - INTERVAL '24 hours')::bigint AS imported_day, - COUNT(*) FILTER (WHERE ended_at > now() - INTERVAL '24 hours' - AND end_reason IN ('stalled', 'failed', 'gone', 'abandoned'))::bigint AS given_up_day - FROM grabs - WHERE ended_at IS NULL - OR ended_at > now() - INTERVAL '24 hours' - OR imported_at > now() - INTERVAL '24 hours'; - -- name: LibraryStatus :one -- What the library holds on disk: tracks whose files are present, the -- albums and artists they make, and their size. diff --git a/internal/db/queries/admin_summary.sql b/internal/db/queries/admin_summary.sql index 9d4167f7..bb8f9a4b 100644 --- a/internal/db/queries/admin_summary.sql +++ b/internal/db/queries/admin_summary.sql @@ -5,8 +5,6 @@ -- oldest item still waiting: how long the queue has been left, which is what -- tells an admin a count of 2 is fine today and wrong after a month. SELECT - (SELECT count(*) FROM requests WHERE status = 'pending')::bigint AS requests, - (SELECT min(requested_at) FROM requests WHERE status = 'pending')::timestamptz AS requests_since, -- A broken track is one report however it was found: a listener flagged -- it, or it failed to play. Counted by track, as the Reports -- page lists them. diff --git a/internal/db/queries/albums.sql b/internal/db/queries/albums.sql index b74cb1fb..2d72456b 100644 --- a/internal/db/queries/albums.sql +++ b/internal/db/queries/albums.sql @@ -215,22 +215,6 @@ UPDATE albums updated_at = now() WHERE id = $1 AND release_group_mbid IS NULL; --- name: GetReleaseGroupForReleaseMbid :one --- The release group the library already knows for a release id, so a --- request carrying a release id can be repaired without asking MusicBrainz. -SELECT release_group_mbid::text - FROM albums - WHERE mbid = $1 AND release_group_mbid IS NOT NULL - LIMIT 1; - --- name: SetReleaseGroupForReleaseMbidIfNull :exec --- Cache a MusicBrainz-resolved release group onto the album row that --- holds the release, filling only a NULL. -UPDATE albums - SET release_group_mbid = sqlc.arg(release_group_mbid)::text, - updated_at = now() - WHERE mbid = sqlc.arg(release_mbid)::text AND release_group_mbid IS NULL; - -- name: ListAlbumsMissingMbidWithTrack :many -- One-shot MBID backfill: returns each album where mbid IS NULL alongside -- one of its tracks' file_path so the worker can re-read tags. LIMIT diff --git a/internal/db/queries/candidate_artist_tags.sql b/internal/db/queries/candidate_artist_tags.sql deleted file mode 100644 index 4bc94881..00000000 --- a/internal/db/queries/candidate_artist_tags.sql +++ /dev/null @@ -1,103 +0,0 @@ --- Folksonomy tags for out-of-library Discover candidates. Parallel to --- track_tags.sql, but keyed by MBID because the artist has no local row. See --- 0050_candidate_artist_tags.up.sql for why the bookkeeping is its own table. - --- name: ListCandidateArtistsMissingTags :many --- Candidates eligible for tag enrichment: never processed (no state row) or --- settled 'none' under an older provider version. --- --- artist_similarity_unmatched holds one row per (seed, candidate, source), so --- this GROUPs to one row per candidate — enriching the same MBID once per seed --- that pointed at it would multiply the API calls for no gain. --- --- ORDER BY summed similarity DESC is the load-bearing part. The candidate pool --- is O(library artists x neighbours per artist) — thousands — and MusicBrainz --- allows ~1 req/s, so it can NEVER be fully enriched in one pass. Draining in --- strength order means the candidates most likely to actually reach a user's --- deck get tags first, and the long tail fills in over subsequent ticks --- instead of starving behind it. --- --- Already-in-library candidates are skipped: they have an artists row, so --- their tags belong in track_tags, and the suggestion query filters them out --- anyway. $1 = current tag_sources_version, $2 = limit. --- candidate_name is coalesced to the empty string so it lands non-nullable in --- Go: the name is only a Last.fm lookup key, and empty simply means "MBID-keyed --- providers only", which the provider chain already handles. max() is an --- arbitrary-but-deterministic pick when several seeds spell one MBID --- differently. -SELECT u.candidate_mbid, - coalesce(max(u.candidate_name), '')::text AS candidate_name, - sum(u.score)::float8 AS total_score - FROM artist_similarity_unmatched u - LEFT JOIN candidate_artist_tag_state s ON s.candidate_mbid = u.candidate_mbid - WHERE NOT EXISTS (SELECT 1 FROM artists a WHERE a.mbid = u.candidate_mbid) - AND ( - s.candidate_mbid IS NULL - OR (s.tag_source = 'none' AND s.tag_sources_version < $1) - ) - GROUP BY u.candidate_mbid - ORDER BY total_score DESC, u.candidate_mbid - LIMIT $2; - --- name: DeleteCandidateArtistTags :exec --- Clear a candidate's cached tags before rewriting (atomic replace by the --- caller, same shape as DeleteTrackTags). -DELETE FROM candidate_artist_tags WHERE candidate_mbid = $1; - --- name: InsertCandidateArtistTag :exec --- Upsert one (candidate, tag); keep the stronger weight when two providers --- agree on a tag with different folksonomy strengths. -INSERT INTO candidate_artist_tags (candidate_mbid, tag, weight) -VALUES ($1, $2, $3) -ON CONFLICT (candidate_mbid, tag) - DO UPDATE SET weight = GREATEST(candidate_artist_tags.weight, EXCLUDED.weight); - --- name: SetCandidateArtistTagState :exec --- Stamp the enrichment outcome so the drainer skips settled candidates. --- $2 = 'musicbrainz' | 'lastfm' | 'mixed' | 'none', $3 = current version. -INSERT INTO candidate_artist_tag_state (candidate_mbid, tag_source, tag_sources_version) -VALUES ($1, $2, $3) -ON CONFLICT (candidate_mbid) DO UPDATE - SET tag_source = EXCLUDED.tag_source, - tag_sources_version = EXCLUDED.tag_sources_version, - updated_at = now(); - --- name: ListCandidateArtistTagsForMbids :many --- Cached tags for a set of candidates, for the taste-overlap ranking. --- One row per (candidate, tag). -SELECT candidate_mbid, tag, weight - FROM candidate_artist_tags - WHERE candidate_mbid = ANY($1::text[]); - --- name: CountCandidateArtistTagCoverage :one --- Operator-facing coverage: how many distinct candidates have been processed, --- and how many of those actually yielded tags. The gap is the honest ceiling --- from the task — obscure artists with no MBID presence or no upstream tags --- stay thin no matter how long the worker runs, and that is worth being able --- to see rather than inferring from a silent surface. -SELECT count(*)::bigint AS processed, - count(*) FILTER (WHERE tag_source <> 'none')::bigint AS with_tags - FROM candidate_artist_tag_state; - --- name: GcDeleteOrphanedCandidateArtistTags :execrows --- Drops cached tags for candidates that no longer appear in the similarity --- feed, or that have since been added to the library (their tags now live in --- track_tags). The feed is refetched periodically and churns, so without this --- the cache only ever grows. -DELETE FROM candidate_artist_tags t - WHERE NOT EXISTS ( - SELECT 1 FROM artist_similarity_unmatched u - WHERE u.candidate_mbid = t.candidate_mbid - ) - OR EXISTS (SELECT 1 FROM artists a WHERE a.mbid = t.candidate_mbid); - --- name: GcDeleteOrphanedCandidateArtistTagState :execrows --- Same sweep for the bookkeeping rows. Kept as a separate statement rather --- than a cascade: the two tables are independent by design (a 'none' outcome --- has state but no tags), so neither can be the parent of the other. -DELETE FROM candidate_artist_tag_state s - WHERE NOT EXISTS ( - SELECT 1 FROM artist_similarity_unmatched u - WHERE u.candidate_mbid = s.candidate_mbid - ) - OR EXISTS (SELECT 1 FROM artists a WHERE a.mbid = s.candidate_mbid); diff --git a/internal/db/queries/catalogue.sql b/internal/db/queries/catalogue.sql index 6d8d5309..c39d2220 100644 --- a/internal/db/queries/catalogue.sql +++ b/internal/db/queries/catalogue.sql @@ -1,34 +1,8 @@ -- The release catalogue read from MusicBrainz. --- name: UpsertMBArtist :exec -INSERT INTO mb_artists (mbid, name, sort_name, disambiguation, artist_type, country) -VALUES (sqlc.arg(mbid), sqlc.arg(name), sqlc.arg(sort_name), sqlc.arg(disambiguation), - sqlc.arg(artist_type), sqlc.arg(country)) -ON CONFLICT (mbid) DO UPDATE SET - name = EXCLUDED.name, - sort_name = EXCLUDED.sort_name, - disambiguation = EXCLUDED.disambiguation, - artist_type = EXCLUDED.artist_type, - country = EXCLUDED.country, - updated_at = now(); - --- name: GetMBArtist :one -SELECT * FROM mb_artists WHERE mbid = sqlc.arg(mbid); - --- name: MarkMBArtistDiscographyRefreshed :exec -UPDATE mb_artists SET discography_refreshed_at = now() WHERE mbid = sqlc.arg(mbid); - --- name: ListMBArtistsDueForRefresh :many --- Artists whose discography has never been read, or was read before the --- cutoff: never-read first, then oldest first. -SELECT mbid FROM mb_artists -WHERE discography_refreshed_at IS NULL OR discography_refreshed_at < sqlc.arg(before) -ORDER BY discography_refreshed_at NULLS FIRST, mbid -LIMIT sqlc.arg(max_rows); - -- name: UpsertMBReleaseGroup :exec --- Leaves releases_refreshed_at alone: re-reading a discography says nothing --- about whether the group's track lists are current. +-- Leaves releases_refreshed_at alone: only a full read of the group's +-- releases and track lists marks it. INSERT INTO mb_release_groups (mbid, title, primary_type, secondary_types, first_release_date, artist_credit) VALUES (sqlc.arg(mbid), sqlc.arg(title), sqlc.arg(primary_type), sqlc.arg(secondary_types)::text[], sqlc.arg(first_release_date), sqlc.arg(artist_credit)) @@ -43,21 +17,6 @@ ON CONFLICT (mbid) DO UPDATE SET -- name: GetMBReleaseGroup :one SELECT * FROM mb_release_groups WHERE mbid = sqlc.arg(mbid); --- name: DeleteMBArtistReleaseGroups :exec -DELETE FROM mb_artist_release_groups WHERE artist_mbid = sqlc.arg(artist_mbid); - --- name: AddMBArtistReleaseGroup :exec -INSERT INTO mb_artist_release_groups (artist_mbid, release_group_mbid) -VALUES (sqlc.arg(artist_mbid), sqlc.arg(release_group_mbid)) -ON CONFLICT DO NOTHING; - --- name: ListMBArtistReleaseGroups :many --- An artist's discography, oldest first; undated groups last. -SELECT g.* FROM mb_release_groups g -JOIN mb_artist_release_groups ag ON ag.release_group_mbid = g.mbid -WHERE ag.artist_mbid = sqlc.arg(artist_mbid) -ORDER BY g.first_release_date = '', g.first_release_date, g.title, g.mbid; - -- name: MarkMBReleaseGroupReleasesRefreshed :exec UPDATE mb_release_groups SET releases_refreshed_at = now() WHERE mbid = sqlc.arg(mbid); @@ -88,12 +47,6 @@ VALUES (sqlc.arg(release_mbid), sqlc.arg(medium_position), sqlc.arg(position), sqlc.arg(medium_format), sqlc.arg(number), sqlc.arg(title), sqlc.arg(length_ms), sqlc.arg(recording_mbid)); --- name: ListMBReleasesForGroup :many --- Official editions first, then by date; undated last. -SELECT * FROM mb_releases -WHERE release_group_mbid = sqlc.arg(release_group_mbid) -ORDER BY status <> 'Official', release_date = '', release_date, title, mbid; - -- name: ListMBReleaseTracksForGroup :many SELECT t.* FROM mb_release_tracks t JOIN mb_releases r ON r.mbid = t.release_mbid diff --git a/internal/db/queries/imports.sql b/internal/db/queries/imports.sql deleted file mode 100644 index e373303c..00000000 --- a/internal/db/queries/imports.sql +++ /dev/null @@ -1,176 +0,0 @@ --- Import with verification. - --- name: ListLiveGrabs :many --- Every download still running or waiting on its import, oldest first. -SELECT g.*, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title AS want_title, - w.reason AS want_reason, w.album_id AS want_album_id - FROM grabs g - LEFT JOIN wants w ON w.id = g.want_id - WHERE g.ended_at IS NULL - ORDER BY g.grabbed_at, g.id; - --- name: GetGrab :one -SELECT * FROM grabs WHERE id = $1; - --- name: SetGrabProgress :exec --- What the download client says of a download not yet finished, or finished --- and waiting for what the import needs. Leaves a claimed, decided or failed --- import alone. -UPDATE grabs - SET progress = sqlc.arg(progress), - import_state = sqlc.arg(import_state), - import_reason = sqlc.arg(import_reason), - import_updated_at = now() - WHERE id = sqlc.arg(id) AND ended_at IS NULL - AND import_state IN ('', 'downloading', 'waiting'); - --- name: ClaimGrabImport :one --- Takes a finished download for import. The update is the lock: a second --- pass finds nothing to claim. -UPDATE grabs - SET import_state = 'importing', import_attempts = import_attempts + 1, - import_updated_at = now(), progress = 1 - WHERE id = $1 AND ended_at IS NULL AND import_state IN ('', 'downloading', 'waiting') -RETURNING *; - --- name: ReclaimStaleImports :execrows --- An import cut short (a restart mid-copy) is tried again. -UPDATE grabs - SET import_state = 'waiting', - import_reason = 'The last import was cut short, so it is being tried again.' - WHERE ended_at IS NULL AND import_state = 'importing' - AND import_updated_at < now() - make_interval(mins => sqlc.arg(minutes)::int); - --- name: RecordImport :one --- The import's outcome. An end reason ends the grab: imported, refused or --- held. Without one the grab stays live: review and failed wait for a person. -UPDATE grabs - SET import_state = sqlc.arg(import_state), - import_reason = sqlc.arg(import_reason), - import_detail = sqlc.arg(import_detail), - import_updated_at = now(), - import_forced = false, - imported_at = CASE WHEN sqlc.arg(import_state)::text = 'imported' THEN now() ELSE imported_at END, - -- The files were copied, so an imported download goes on sharing - -- until the lifecycle removes it. - seeding = sqlc.arg(import_state)::text = 'imported', - imported_files = sqlc.arg(imported_files), - destination = sqlc.arg(destination), - ended_at = CASE WHEN sqlc.arg(end_reason)::text <> '' THEN now() ELSE ended_at END, - end_reason = sqlc.arg(end_reason), - end_detail = CASE WHEN sqlc.arg(end_reason)::text <> '' THEN sqlc.arg(import_reason)::text ELSE end_detail END - WHERE id = sqlc.arg(id) -RETURNING *; - --- name: ForceGrabImport :one --- "Import anyway": the next pass imports the download whatever the check said. -UPDATE grabs - SET import_state = '', import_forced = true, import_reason = '', import_updated_at = now() - WHERE id = $1 AND ended_at IS NULL AND import_state IN ('review', 'failed') -RETURNING *; - --- name: RetryGrabImport :one -UPDATE grabs - SET import_state = '', import_reason = '', import_updated_at = now() - WHERE id = $1 AND ended_at IS NULL AND import_state = 'failed' -RETURNING *; - --- name: RehomeGrab :one --- A download that turned out to be another wanted album is that want's grab, --- when the want has none running. -UPDATE grabs g - SET want_id = sqlc.arg(want_id) - WHERE g.id = sqlc.arg(id) AND g.ended_at IS NULL - AND NOT EXISTS (SELECT 1 FROM grabs o WHERE o.want_id = sqlc.arg(want_id) AND o.ended_at IS NULL) -RETURNING *; - --- name: OpenWantsForGroups :many --- Open wants for these release groups with no download running. -SELECT w.* FROM wants w - WHERE w.fulfilled_at IS NULL AND w.release_group_mbid = ANY(sqlc.arg(groups)::text[]) - AND NOT EXISTS (SELECT 1 FROM grabs g WHERE g.want_id = w.id AND g.ended_at IS NULL); - --- name: MarkWantSearched :exec --- Imported: nothing to search for until the wanted pass settles the want. -UPDATE wants SET searched_at = now() WHERE id = $1; - --- name: SearchWantAgain :exec --- Refused: search again on the next pass. -UPDATE wants SET searched_at = NULL, search_outcome = '' WHERE id = $1; - --- name: HeldRecordings :many --- Which of these recordings the library holds a file for. -SELECT DISTINCT mbid::text AS mbid FROM tracks - WHERE mbid = ANY(sqlc.arg(recordings)::text[]) AND missing_since IS NULL; - --- name: HeldPositions :many --- The disc and track positions the library holds for an album of this group. -SELECT DISTINCT COALESCE(t.disc_number, 1)::int AS disc, t.track_number::int AS track - FROM tracks t - JOIN albums a ON a.id = t.album_id - WHERE a.release_group_mbid = sqlc.arg(release_group_mbid) - AND t.missing_since IS NULL AND t.track_number IS NOT NULL; - --- name: ListGrabs :many --- The Wanted view's Activity tab: downloads waiting on a person first, then --- the rest still running, then the ended ones newest first. Each carries --- what is wrong with it now, if anything. -SELECT g.id, g.want_id, g.name, g.indexer, g.client, g.quality, g.origin, g.forced, - g.grabbed_at, g.ended_at, g.end_reason, g.end_detail, g.progress, g.import_state, - g.import_reason, g.import_detail, g.import_updated_at, g.imported_at, g.imported_files, - g.destination, g.seeding, g.ratio, g.seed_released_at, - w.title AS want_title, w.artist_name AS want_artist, - di.trigger AS condition, di.detail AS condition_detail, di.remediations AS condition_retries, - di.first_seen AS condition_since - FROM grabs g - LEFT JOIN wants w ON w.id = g.want_id - LEFT JOIN download_interventions di - ON di.id = (SELECT x.id FROM download_interventions x - WHERE x.grab_id = g.id AND x.outcome = 'open' - ORDER BY x.last_seen DESC LIMIT 1) - ORDER BY (g.ended_at IS NULL AND g.import_state IN ('review', 'failed')) DESC, - g.ended_at IS NULL DESC, g.grabbed_at DESC, g.id - LIMIT sqlc.arg(max_rows) OFFSET sqlc.arg(skip); - --- name: ListReleaseBlocksPage :many -SELECT b.info_hash, b.name, b.reason, b.detail, b.blocked_at, b.expires_at, w.title AS want_title, - w.artist_name AS want_artist - FROM release_blocks b - LEFT JOIN wants w ON w.id = b.want_id - WHERE b.expires_at IS NULL OR b.expires_at > now() - ORDER BY b.blocked_at DESC, b.info_hash - LIMIT sqlc.arg(max_rows) OFFSET sqlc.arg(skip); - --- name: ListUpgradeTracks :many --- The tracks an upgrade may replace: the present tracks of the album --- the want holds. -SELECT id, file_path, mbid, COALESCE(disc_number, 1)::int AS disc, track_number, - file_format, file_size, duration_ms, source_verdict - FROM tracks - WHERE album_id = $1 AND missing_since IS NULL; - --- name: FinishUpgrade :exec --- An upgrade was imported: the want holds the album the new tracks are in, --- and is judged again by the next upgrade check. -UPDATE wants - SET album_id = COALESCE(sqlc.narg(album_id), album_id), - upgrade = '', - upgrade_checked_at = NULL, - updated_at = now() - WHERE id = sqlc.arg(id); - --- name: ClearTrackSourceVerdict :exec --- A track whose file was replaced in place is no longer the rip it was. -UPDATE tracks SET source_verdict = NULL, source_verdict_at = NULL - WHERE id = $1 AND source_verdict = 'suspect'; - --- name: ListImportedGrabsHolding :many --- The downloads Minstrel imported into the folder holding path: the releases a --- file there came from, to block when the files proved not to be the album ---. A file imported before Minstrel downloaded anything has none. -SELECT id, info_hash, name, want_id - FROM grabs - WHERE import_state = 'imported' - AND info_hash <> '' - AND destination <> '' - AND starts_with(sqlc.arg(path)::text, rtrim(destination, '/') || '/'); diff --git a/internal/db/queries/library_settings.sql b/internal/db/queries/library_settings.sql index 99d0fd37..cab5dc23 100644 --- a/internal/db/queries/library_settings.sql +++ b/internal/db/queries/library_settings.sql @@ -23,17 +23,6 @@ UPDATE albums SET cover_art_path = sqlc.arg(new_root)::text || substr(cover_art_path, length(sqlc.arg(old_root)::text) + 1) WHERE starts_with(cover_art_path, sqlc.arg(old_root)::text || '/'); --- name: RerootGrabDestinations :execrows -UPDATE grabs - SET destination = sqlc.arg(new_root)::text || substr(destination, length(sqlc.arg(old_root)::text) + 1) - WHERE destination = sqlc.arg(old_root)::text - OR starts_with(destination, sqlc.arg(old_root)::text || '/'); - --- name: RerootImportRoot :execrows -UPDATE acquisition_settings - SET import_root = sqlc.arg(new_root)::text - WHERE import_root = sqlc.arg(old_root)::text; - -- name: SampleTrackPathsUnder :many -- A few present tracks under a folder, to check a move before rewriting it. SELECT file_path FROM tracks diff --git a/internal/db/queries/lifecycle.sql b/internal/db/queries/lifecycle.sql deleted file mode 100644 index 8b5b21c0..00000000 --- a/internal/db/queries/lifecycle.sql +++ /dev/null @@ -1,102 +0,0 @@ --- The download lifecycle: the intervention ledger, giving a --- download up, and seeding after import. - --- name: RaiseIntervention :one --- A condition seen, or seen again: a second pass lands on the open row, so --- a download stuck for a week is one row that says how long. The name only --- improves, and the detail is the latest word. -INSERT INTO download_interventions (client, info_hash, grab_id, name, trigger, detail) -VALUES (sqlc.arg(client), sqlc.arg(info_hash), sqlc.narg(grab_id), sqlc.arg(name), - sqlc.arg(trigger), sqlc.arg(detail)) -ON CONFLICT (client, info_hash, trigger) WHERE outcome = 'open' DO UPDATE SET - observations = download_interventions.observations + 1, - last_seen = now(), - detail = EXCLUDED.detail, - grab_id = COALESCE(EXCLUDED.grab_id, download_interventions.grab_id), - name = CASE WHEN EXCLUDED.name <> '' THEN EXCLUDED.name ELSE download_interventions.name END -RETURNING *; - --- name: SettleIntervention :execrows --- The condition cleared. -UPDATE download_interventions - SET outcome = 'resolved', settled_at = now(), last_seen = now() - WHERE client = sqlc.arg(client) AND info_hash = sqlc.arg(info_hash) - AND trigger = sqlc.arg(trigger) AND outcome = 'open'; - --- name: SettleInterventionsFor :execrows --- Everything open about one download is over: it is gone from the client. -UPDATE download_interventions - SET outcome = 'resolved', settled_at = now(), last_seen = now() - WHERE client = sqlc.arg(client) AND info_hash = sqlc.arg(info_hash) AND outcome = 'open'; - --- name: SettleEndedGrabs :execrows --- What was wrong with a download whose grab has ended (imported, refused, --- given up) is over, except a download left in the client to remove. -UPDATE download_interventions di - SET outcome = 'resolved', settled_at = now(), last_seen = now() - FROM grabs g - WHERE g.id = di.grab_id AND g.ended_at IS NOT NULL - AND di.outcome = 'open' AND di.trigger <> 'not_removed'; - --- name: CountRemediation :one --- Minstrel acted on an open condition. A retry climbs the ladder; giving up --- or removing ends the condition, since what it was about is gone. Written --- before the act, so a retry that happened is never uncounted. -UPDATE download_interventions - SET remediations = remediations + 1, - action = sqlc.arg(action), - last_seen = now(), - outcome = CASE WHEN sqlc.arg(action)::text IN ('given_up', 'removed') THEN 'resolved' ELSE outcome END, - settled_at = CASE WHEN sqlc.arg(action)::text IN ('given_up', 'removed') THEN now() ELSE settled_at END - WHERE client = sqlc.arg(client) AND info_hash = sqlc.arg(info_hash) - AND trigger = sqlc.arg(trigger) AND outcome = 'open' -RETURNING remediations; - --- name: ListLeftovers :many --- Downloads given up on that the client would not remove; each pass asks --- again. -SELECT * FROM download_interventions - WHERE trigger = 'not_removed' AND outcome = 'open' AND info_hash <> '' - ORDER BY first_seen; - --- name: ListClientsDown :many --- Download clients not answering now, for the Activity tab. -SELECT client, detail, first_seen FROM download_interventions - WHERE trigger = 'client_unreachable' AND outcome = 'open' - ORDER BY client; - --- name: LiveGrabOnHash :one -SELECT EXISTS (SELECT 1 FROM grabs WHERE info_hash = sqlc.arg(info_hash) AND ended_at IS NULL)::bool; - --- name: GiveUpGrab :one --- Ends a running grab, given up on. Only from the states named: the update --- is the lock against an import that claimed the download meanwhile. -UPDATE grabs - SET ended_at = now(), end_reason = sqlc.arg(end_reason), end_detail = sqlc.arg(end_detail), - import_updated_at = now() - WHERE id = sqlc.arg(id) AND ended_at IS NULL AND import_state = ANY(sqlc.arg(states)::text[]) -RETURNING *; - --- name: CountGoneGrabs :one --- How many of a release's grabs ended gone since a time: a release that keeps --- leaving its client is held back rather than taken on every pass. -SELECT count(*) FROM grabs - WHERE info_hash = sqlc.arg(info_hash) AND end_reason = 'gone' AND ended_at > sqlc.arg(since); - --- name: ListSeedingGrabs :many --- Imported downloads still sharing, oldest import first. -SELECT id, client, info_hash, name, imported_at FROM grabs - WHERE seeding AND seed_released_at IS NULL - ORDER BY imported_at, id; - --- name: SetGrabRatio :exec -UPDATE grabs SET ratio = sqlc.arg(ratio) WHERE id = sqlc.arg(id); - --- name: ReleaseSeed :exec --- The seeding download was removed, or found gone. -UPDATE grabs SET seed_released_at = now() WHERE id = $1 AND seed_released_at IS NULL; - --- name: DeleteExpiredBlocks :execrows --- A release given up on is held back for a while; after that the search may --- take it again. -DELETE FROM release_blocks WHERE expires_at IS NOT NULL AND expires_at <= now(); diff --git a/internal/db/queries/merge.sql b/internal/db/queries/merge.sql index 4ea5001d..851e66c3 100644 --- a/internal/db/queries/merge.sql +++ b/internal/db/queries/merge.sql @@ -33,10 +33,6 @@ UPDATE contextual_likes SET track_id = sqlc.arg(survivor_id)::uuid WHERE track_i -- name: MergeRepointPlaybackErrors :execrows UPDATE playback_errors SET track_id = sqlc.arg(survivor_id)::uuid WHERE track_id = sqlc.arg(loser_id)::uuid; --- name: MergeRepointRequests :execrows -UPDATE requests SET matched_track_id = sqlc.arg(survivor_id)::uuid - WHERE matched_track_id = sqlc.arg(loser_id)::uuid; - -- name: MergeRepointPlaylistTracks :many -- playlist_tracks is keyed by (playlist_id, position), so repointing keeps every -- entry exactly where it was. A playlist that held both copies simply holds the diff --git a/internal/db/queries/reacquisition.sql b/internal/db/queries/reacquisition.sql deleted file mode 100644 index be665a19..00000000 --- a/internal/db/queries/reacquisition.sql +++ /dev/null @@ -1,93 +0,0 @@ --- Re-acquisition of missing files. --- The unit is the album: an album whose files went missing becomes a want for --- its release group, and the search schedule looks for it. - --- name: GetReacquisitionSettings :one -SELECT * FROM reacquisition_settings WHERE id = true; - --- name: UpdateReacquisitionSettings :one --- Whole-row write from the admin card; the CHECKs in migration 0056 are the --- validation, so a bad value fails loudly rather than being clamped silently. -UPDATE reacquisition_settings - SET enabled = sqlc.arg(enabled), - grace_hours = sqlc.arg(grace_hours), - max_per_pass = sqlc.arg(max_per_pass) - WHERE id = true -RETURNING *; - --- name: ListAlbumsDueReacquisition :many --- The sweeper's selection. An album qualifies when: --- --- * it still has at least one track whose file has been missing longer than --- the grace window. Measured on missing_since, which reconcile never --- re-stamps, so it is a genuine "gone since" clock; --- * MusicBrainz can name it: the artist has an MBID, and the album a --- release MBID or a release group. The release MBID is only ever used to --- find the group, and an album matched to a group may carry no release --- id. Albums failing this are counted separately --- (CountAlbumsMissingWithoutMbid); --- * it is on the wanted list: an album taken off the list, or --- dropped into the library by hand, is not fetched back; --- * no want already covers it: none open, and none held since its files --- went missing. A want held after the loss means the library has a full --- copy despite the missing rows, so asking again would loop. -SELECT albums.id AS album_id, - albums.title AS album_title, - albums.mbid AS album_mbid, - albums.release_group_mbid AS album_release_group_mbid, - artists.name AS artist_name, - artists.mbid AS artist_mbid, - COUNT(tracks.id)::bigint AS missing_track_count, - MAX(tracks.missing_since)::timestamptz AS last_missing_since - FROM albums - JOIN artists ON artists.id = albums.artist_id - JOIN tracks ON tracks.album_id = albums.id - WHERE tracks.missing_since IS NOT NULL - AND tracks.missing_since <= now() - make_interval(hours => sqlc.arg(grace_hours)::int) - AND (albums.mbid IS NOT NULL OR albums.release_group_mbid IS NOT NULL) - AND artists.mbid IS NOT NULL - GROUP BY albums.id, albums.title, albums.mbid, albums.release_group_mbid, - artists.name, artists.mbid -HAVING EXISTS ( - SELECT 1 FROM wants w - WHERE w.album_id = albums.id OR w.release_group_mbid = albums.release_group_mbid) - AND NOT EXISTS ( - SELECT 1 FROM wants w - WHERE (w.album_id = albums.id OR w.release_group_mbid = albums.release_group_mbid) - AND (w.fulfilled_at IS NULL OR w.fulfilled_at > MAX(tracks.missing_since))) - ORDER BY MIN(tracks.missing_since), albums.sort_title - LIMIT sqlc.arg(page_limit); - --- name: CountAlbumsMissingWithoutMbid :one --- Albums with missing files that can never be wanted automatically because --- nothing identifies them to MusicBrainz. Surfaced on the admin card so the --- gap is visible: silently doing nothing for these would read as broken. -SELECT COUNT(DISTINCT albums.id)::bigint - FROM albums - JOIN artists ON artists.id = albums.artist_id - JOIN tracks ON tracks.album_id = albums.id - WHERE tracks.missing_since IS NOT NULL - AND ((albums.mbid IS NULL AND albums.release_group_mbid IS NULL) OR artists.mbid IS NULL); - --- name: WantMissingAlbum :one --- Records an album with missing files as wanted. A held want for the same --- group is reopened; an open one is left as it is, and no row comes back. -INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id) -VALUES (sqlc.arg(release_group_mbid), sqlc.arg(artist_mbid), sqlc.arg(artist_name), - sqlc.arg(title), 'missing', sqlc.arg(album_id)) -ON CONFLICT (release_group_mbid) DO UPDATE - SET reason = 'missing', - album_id = EXCLUDED.album_id, - wanted_at = now(), - fulfilled_at = NULL, - updated_at = now() - WHERE wants.fulfilled_at IS NOT NULL -RETURNING *; - --- name: GetWantsForAlbums :many --- What the wanted list says about each album on the missing-files page. -SELECT a.id AS album_id, w.id AS want_id, w.wanted_at, w.fulfilled_at - FROM albums a - JOIN wants w ON w.album_id = a.id OR w.release_group_mbid = a.release_group_mbid - WHERE a.id = ANY(sqlc.arg(album_ids)::uuid[]) - ORDER BY w.wanted_at DESC; diff --git a/internal/db/queries/recommendation.sql b/internal/db/queries/recommendation.sql index d4c3c805..b92e87a1 100644 --- a/internal/db/queries/recommendation.sql +++ b/internal/db/queries/recommendation.sql @@ -310,109 +310,6 @@ WHERE user_id = $1 ORDER BY started_at DESC LIMIT 1; --- name: SuggestArtistsForUser :many --- Per-user artist suggestions ranked by taste signal x similarity, projected --- through artist_similarity_unmatched (out-of-library candidates only). --- --- Seeds are TIERED so the surface never empties: --- tier 1 - taste_profile_artists.weight: engagement-graded, time-decayed and --- SIGNED by internal/taste, so an artist the user has drifted away --- from stops contributing instead of accumulating forever. --- tier 2 - likes + completed plays, used ONLY when the profile has no rows --- (new account, or before the first daily recompute). --- --- The signal is log-damped: contribution is signal x similarity, and an --- undamped sum would let one heavily-played artist's neighbours take every --- slot -- entrenching harder the MORE the user listened. --- --- Candidates already in the library, already requested and not terminal, or --- snoozed by this user, are excluded. $1=user_id, $2=half_life_days (tier 2 --- decay), $3=limit. -WITH artist_plays AS ( - -- Completed plays only: counting every play_event would make skipping an - -- artist repeatedly INCREASE its signal and push more of its neighbours - -- at the user. - SELECT t.artist_id, count(*)::bigint AS play_count - FROM play_events pe - JOIN tracks t ON t.id = pe.track_id - WHERE pe.user_id = $1 AND pe.was_skipped = false - GROUP BY t.artist_id -), -profile_seeds AS ( - SELECT tpa.artist_id, tpa.weight AS raw_signal - FROM taste_profile_artists tpa - WHERE tpa.user_id = $1 AND tpa.weight > 0 -), -fallback_seeds AS ( - SELECT a.id AS artist_id, - 5.0 * (CASE WHEN gla.artist_id IS NOT NULL THEN 1 ELSE 0 END) - + COALESCE(SUM(EXP(- EXTRACT(epoch FROM now() - pe.started_at) / ($2::float8 * 86400.0))), 0) - AS raw_signal - FROM artists a - LEFT JOIN general_likes_artists gla ON gla.artist_id = a.id AND gla.user_id = $1 - LEFT JOIN tracks t ON t.artist_id = a.id - LEFT JOIN play_events pe - ON pe.track_id = t.id AND pe.user_id = $1 AND pe.was_skipped = false - WHERE (gla.artist_id IS NOT NULL OR pe.id IS NOT NULL) - AND NOT EXISTS (SELECT 1 FROM profile_seeds) - GROUP BY a.id, gla.artist_id -), -seeds AS ( - SELECT s.artist_id, - ln(1.0 + s.raw_signal) AS signal, - (gla.artist_id IS NOT NULL) AS is_liked, - COALESCE(ap.play_count, 0)::bigint AS play_count - FROM ( - SELECT artist_id, raw_signal FROM profile_seeds - UNION ALL - SELECT artist_id, raw_signal FROM fallback_seeds - ) s - LEFT JOIN general_likes_artists gla - ON gla.artist_id = s.artist_id AND gla.user_id = $1 - LEFT JOIN artist_plays ap ON ap.artist_id = s.artist_id - WHERE s.raw_signal > 0 -), -contributions AS ( - SELECT u.candidate_mbid, - u.candidate_name, - seeds.artist_id AS seed_id, - seeds.is_liked, - seeds.play_count, - seeds.signal * u.score AS contribution - FROM artist_similarity_unmatched u - JOIN seeds ON seeds.artist_id = u.seed_artist_id - WHERE NOT EXISTS (SELECT 1 FROM artists WHERE mbid = u.candidate_mbid) - AND NOT EXISTS ( - SELECT 1 FROM requests r - WHERE r.user_id = $1 - AND r.artist_mbid = u.candidate_mbid - AND r.status NOT IN ('rejected', 'failed') - ) - -- Snoozed by this user and not yet expired. Time-boxed and - -- per-user: the candidate returns on its own once snoozed_until - -- passes, and stays visible to everyone else meanwhile. Deliberately - -- filtered at the candidate stage, NOT folded into the score — a - -- snooze carries no opinion about the music, so it must not become a - -- ranking signal. - AND NOT EXISTS ( - SELECT 1 FROM suggestion_snoozes s - WHERE s.user_id = $1 - AND s.candidate_mbid = u.candidate_mbid - AND s.snoozed_until > now() - ) -) -SELECT candidate_mbid, - candidate_name, - SUM(contribution)::float8 AS total_score, - ((array_agg(seed_id ORDER BY contribution DESC))[1:3])::uuid[] AS top_seed_ids, - ((array_agg(contribution ORDER BY contribution DESC))[1:3])::float8[] AS top_contributions, - ((array_agg(is_liked ORDER BY contribution DESC))[1:3])::boolean[] AS top_is_liked, - ((array_agg(play_count ORDER BY contribution DESC))[1:3])::bigint[] AS top_play_counts -FROM contributions -GROUP BY candidate_mbid, candidate_name -ORDER BY total_score DESC -LIMIT $3; - -- name: ListMostPlayedTracksForUser :many -- Top-N tracks by completed-play count for the user. was_skipped -- excludes skips so a user spamming next doesn't fabricate a top track. diff --git a/internal/db/queries/recommendation_tuning.sql b/internal/db/queries/recommendation_tuning.sql index 49c627ed..215c23d9 100644 --- a/internal/db/queries/recommendation_tuning.sql +++ b/internal/db/queries/recommendation_tuning.sql @@ -53,24 +53,6 @@ UPDATE taste_tuning WHERE singleton = true RETURNING *; --- name: UpsertDiscoverTuningDefaults :exec --- Boot reconcile for the Discover scope. Never overwrites --- admin-tuned values, same contract as the other two. -INSERT INTO discover_tuning (singleton, tag_overlap_weight, snooze_days) -VALUES (true, $1, $2) -ON CONFLICT (singleton) DO NOTHING; - --- name: GetDiscoverTuning :one -SELECT * FROM discover_tuning WHERE singleton = true; - --- name: UpdateDiscoverTuning :one -UPDATE discover_tuning - SET tag_overlap_weight = $1, - snooze_days = $2, - updated_at = now() - WHERE singleton = true -RETURNING *; - -- name: InsertTuningAudit :exec -- changes is a jsonb array of {field, old, new} objects. INSERT INTO recommendation_tuning_audit (scope, action, changes) diff --git a/internal/db/queries/requests.sql b/internal/db/queries/requests.sql deleted file mode 100644 index 7eff0d01..00000000 --- a/internal/db/queries/requests.sql +++ /dev/null @@ -1,119 +0,0 @@ --- name: CreateRequest :one -INSERT INTO requests ( - user_id, kind, - artist_mbid, album_mbid, track_mbid, - artist_name, album_title, track_title -) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) -RETURNING *; - --- name: GetRequestByID :one -SELECT * FROM requests WHERE id = $1; - --- name: ListRequestsForUser :many -SELECT * FROM requests -WHERE user_id = $1 -ORDER BY requested_at DESC -LIMIT $2; - --- name: ListRequestsByStatus :many -SELECT * FROM requests -WHERE status = $1 -ORDER BY requested_at DESC -LIMIT $2; - --- name: ListApprovedRequestsForReconcile :many -SELECT * FROM requests -WHERE status = 'approved' -ORDER BY decided_at ASC -LIMIT $1; - --- name: ApproveRequest :one -UPDATE requests - SET status = 'approved', - decided_at = now(), - decided_by = $2, - updated_at = now() - WHERE id = $1 AND status = 'pending' - RETURNING *; - --- name: RejectRequest :one -UPDATE requests - SET status = 'rejected', - notes = $2, - decided_at = now(), - decided_by = $3, - updated_at = now() - WHERE id = $1 AND status = 'pending' - RETURNING *; - --- name: CancelRequest :one --- $2 serves as both the ownership guard (user_id = $2) and the --- decided_by audit field. The generated parameter struct names it --- DecidedBy — callers must pass the cancelling user's UUID, and --- a wrong UUID will silently no-op (zero rows updated) rather than --- erroring at the SQL layer. -UPDATE requests - SET status = 'rejected', - notes = 'cancelled by user', - decided_at = now(), - decided_by = $2, - updated_at = now() - WHERE id = $1 AND user_id = $2 AND status = 'pending' - RETURNING *; - --- name: CompleteRequest :one --- Reconciler transitions an approved request to completed when its --- target track/album/artist has appeared in the library. Callers --- should set ONLY the matched_*_id corresponding to the request's --- kind and pass pgtype.UUID{} (Valid: false) for the others — --- per-kind one-of-three is the contract, NULL for the others --- preserves auditability if the matched row is later evicted. -UPDATE requests - SET status = 'completed', - matched_track_id = $2, - matched_album_id = $3, - matched_artist_id = $4, - completed_at = now(), - updated_at = now() - WHERE id = $1 AND status = 'approved' - RETURNING *; - --- name: HasNonTerminalRequestForMBID :one --- Whether something is already on its way: a pending, approved or completed --- request at the given level, an open want for the release group, or a --- follow of the artist. Sets the `requested` flag on catalogue search. --- Terminal-status (rejected, failed) requests do not count. MBIDs are unique --- per entity type, so checking one id against every column cannot --- false-match. -SELECT (EXISTS ( - SELECT 1 FROM requests r - WHERE r.status IN ('pending', 'approved', 'completed') - AND ((r.kind = 'artist' AND r.artist_mbid = @mbid) - OR (r.kind = 'album' AND r.album_mbid = @mbid) - OR (r.kind = 'track' AND r.track_mbid = @mbid)) -) OR EXISTS ( - SELECT 1 FROM wants w WHERE w.release_group_mbid = @mbid AND w.fulfilled_at IS NULL -) OR EXISTS ( - SELECT 1 FROM follows f WHERE f.artist_mbid = @mbid -))::bool AS requested; - --- name: GetNonTerminalRequestForMBID :one --- Returns the oldest non-terminal (pending/approved/completed) request --- whose own-kind MBID matches @mbid, or pgx.ErrNoRows when none. Create() --- uses this to dedupe: a re-request for something already in flight --- returns the existing row instead of inserting a duplicate. -SELECT * FROM requests - WHERE status IN ('pending', 'approved', 'completed') - AND ((kind = 'artist' AND artist_mbid = @mbid) - OR (kind = 'album' AND album_mbid = @mbid) - OR (kind = 'track' AND track_mbid = @mbid)) - ORDER BY requested_at ASC - LIMIT 1; - --- name: RepointRequestsAlbumMbid :exec --- A want moved from a release id to its group takes the requests --- that name it along, so the reconciler finds it. -UPDATE requests - SET album_mbid = sqlc.arg(new_mbid), - updated_at = now() - WHERE album_mbid = sqlc.arg(old_mbid); diff --git a/internal/db/queries/search.sql b/internal/db/queries/search.sql deleted file mode 100644 index 01c17fa3..00000000 --- a/internal/db/queries/search.sql +++ /dev/null @@ -1,141 +0,0 @@ --- Searching for wants and grabbing releases. - --- name: GetAcquisitionSettings :one -SELECT * FROM acquisition_settings WHERE id = true; - --- name: UpdateAcquisitionSettings :one -UPDATE acquisition_settings - SET auto_grab = sqlc.arg(auto_grab), - search_interval_hours = sqlc.arg(search_interval_hours), - allowed_qualities = sqlc.arg(allowed_qualities), - import_root = sqlc.arg(import_root), - seed_ratio = sqlc.arg(seed_ratio), - seed_days = sqlc.arg(seed_days), - stall_hours = sqlc.arg(stall_hours), - upgrade_until = sqlc.arg(upgrade_until), - updated_at = now() - WHERE id = true -RETURNING *; - --- name: RecordSearchPass :exec -UPDATE acquisition_settings SET last_pass_at = now(), last_pass = sqlc.arg(report) WHERE id = true; - --- name: ListWantsDueForSearch :many --- Wants with no download running that were never searched or not since --- @hours ago: open wants first (never-searched, then requests, then missing --- files, then followed artists' releases), then held albums wanted for an --- upgrade, which are searched at most weekly. -SELECT w.id, w.release_group_mbid, w.artist_name, w.title, w.reason, w.upgrade, w.held_tier, w.held_quality, - COALESCE(NULLIF(w.primary_type, ''), g.primary_type, '')::text AS primary_type, - COALESCE(g.secondary_types, '{}')::text[] AS secondary_types, - COALESCE(NULLIF(w.first_release_date, ''), g.first_release_date, '')::text AS first_release_date - FROM wants w - LEFT JOIN mb_release_groups g ON g.mbid = w.release_group_mbid - WHERE (w.fulfilled_at IS NULL OR w.upgrade <> '') - AND NOT EXISTS (SELECT 1 FROM grabs gr WHERE gr.want_id = w.id AND gr.ended_at IS NULL) - AND (w.searched_at IS NULL - OR w.searched_at < now() - make_interval(hours => CASE WHEN w.fulfilled_at IS NULL - THEN sqlc.arg(hours)::int - ELSE GREATEST(sqlc.arg(hours)::int, 168) END)) - ORDER BY w.fulfilled_at IS NOT NULL, - w.searched_at IS NOT NULL, - CASE w.reason WHEN 'request' THEN 0 WHEN 'missing' THEN 1 ELSE 2 END, - w.searched_at NULLS FIRST, w.wanted_at, w.id - LIMIT sqlc.arg(max_rows); - --- name: GetWantForSearch :one -SELECT w.id, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title, w.reason, w.fulfilled_at, - w.upgrade, w.held_tier, w.held_quality, w.album_id, - COALESCE(NULLIF(w.primary_type, ''), g.primary_type, '')::text AS primary_type, - COALESCE(g.secondary_types, '{}')::text[] AS secondary_types, - COALESCE(NULLIF(w.first_release_date, ''), g.first_release_date, '')::text AS first_release_date - FROM wants w - LEFT JOIN mb_release_groups g ON g.mbid = w.release_group_mbid - WHERE w.id = $1; - --- name: EditionTrackSpan :one --- The fewest tracks an official edition of the group has (any edition when --- none is official), and the most any has. Zero when none is catalogued. -SELECT COALESCE(MIN(track_count) FILTER (WHERE status = 'Official'), MIN(track_count), 0)::int AS min_tracks, - COALESCE(MAX(track_count), 0)::int AS max_tracks - FROM mb_releases - WHERE release_group_mbid = $1 AND track_count > 0; - --- name: RecordWantSearch :exec -UPDATE wants - SET searched_at = now(), - search_outcome = sqlc.arg(outcome), - search_considered = sqlc.arg(considered), - best_release = sqlc.arg(best_release), - best_verdict = sqlc.arg(best_verdict), - search_error = sqlc.arg(search_error), - updated_at = now() - WHERE id = sqlc.arg(id); - --- name: DeleteWantCandidates :exec -DELETE FROM want_candidates WHERE want_id = $1; - --- name: InsertWantCandidate :exec -INSERT INTO want_candidates (want_id, key, name, info_hash, indexer, source, size_bytes, seeders, - files_count, format, quality, year, verdict, reason, reasons, folded_from, rank) -VALUES (sqlc.arg(want_id), sqlc.arg(key), sqlc.arg(name), sqlc.arg(info_hash), sqlc.arg(indexer), - sqlc.arg(source), sqlc.arg(size_bytes), sqlc.arg(seeders), sqlc.arg(files_count), sqlc.arg(format), - sqlc.arg(quality), sqlc.arg(year), sqlc.arg(verdict), sqlc.arg(reason), sqlc.arg(reasons), - sqlc.arg(folded_from), sqlc.arg(rank)) -ON CONFLICT (want_id, key) DO NOTHING; - --- name: ListWantCandidates :many -SELECT * FROM want_candidates WHERE want_id = $1 ORDER BY rank, key; - --- name: GetWantCandidate :one -SELECT * FROM want_candidates WHERE want_id = $1 AND key = $2; - --- name: MarkCandidatesBlocked :exec -UPDATE want_candidates - SET verdict = 'blocked', reason = sqlc.arg(reason), - reasons = jsonb_build_array(jsonb_build_object('kind', 'hard', 'code', 'blocked', 'text', sqlc.arg(reason)::text)) - WHERE info_hash = sqlc.arg(info_hash); - --- name: MarkCandidatesUnblocked :exec -UPDATE want_candidates - SET verdict = 'review', reason = sqlc.arg(reason), - reasons = jsonb_build_array(jsonb_build_object('kind', 'soft', 'code', 'unblocked', 'text', sqlc.arg(reason)::text)) - WHERE info_hash = sqlc.arg(info_hash) AND verdict = 'blocked'; - --- name: GetLiveGrab :one -SELECT * FROM grabs WHERE want_id = $1 AND ended_at IS NULL; - --- name: InsertGrab :one -INSERT INTO grabs (want_id, info_hash, name, key, indexer, source, client, save_path, quality, - origin, forced, grabbed_by) -VALUES (sqlc.narg(want_id), sqlc.arg(info_hash), sqlc.arg(name), sqlc.arg(key), sqlc.arg(indexer), - sqlc.arg(source), sqlc.arg(client), sqlc.arg(save_path), sqlc.arg(quality), - sqlc.arg(origin), sqlc.arg(forced), sqlc.narg(grabbed_by)) -RETURNING *; - --- name: EndGrab :exec -UPDATE grabs - SET ended_at = now(), end_reason = sqlc.arg(end_reason), end_detail = sqlc.arg(end_detail) - WHERE id = sqlc.arg(id) AND ended_at IS NULL; - --- name: ListReleaseBlocks :many --- Every release set aside and still held back: a stalled one lifts by itself --- at expires_at. -SELECT * FROM release_blocks - WHERE expires_at IS NULL OR expires_at > now() - ORDER BY blocked_at DESC; - --- name: BlockRelease :exec --- A release set aside. expires_at NULL is for good. A block for good is never --- shortened by a timed one: only a timed block, or another for good, is --- replaced. -INSERT INTO release_blocks (info_hash, name, reason, detail, want_id, blocked_by, expires_at) -VALUES (sqlc.arg(info_hash), sqlc.arg(name), sqlc.arg(reason), sqlc.arg(detail), - sqlc.narg(want_id), sqlc.narg(blocked_by), sqlc.narg(expires_at)) -ON CONFLICT (info_hash) DO UPDATE - SET reason = EXCLUDED.reason, detail = EXCLUDED.detail, blocked_at = now(), - expires_at = EXCLUDED.expires_at, blocked_by = EXCLUDED.blocked_by - WHERE release_blocks.expires_at IS NOT NULL OR EXCLUDED.expires_at IS NULL; - --- name: UnblockRelease :execrows -DELETE FROM release_blocks WHERE info_hash = $1; diff --git a/internal/db/queries/similarity.sql b/internal/db/queries/similarity.sql index b4beef0d..072c2a5d 100644 --- a/internal/db/queries/similarity.sql +++ b/internal/db/queries/similarity.sql @@ -121,18 +121,6 @@ VALUES ($1, $2, $3, 'listenbrainz', now()) ON CONFLICT (artist_a_id, artist_b_id, source) DO UPDATE SET score = EXCLUDED.score, fetched_at = EXCLUDED.fetched_at; --- name: UpsertArtistSimilarityUnmatched :exec --- Persists an out-of-library similar-artist MBID. Idempotent on --- (seed_artist_id, candidate_mbid, source) — re-fetches refresh the --- name/score and bump fetched_at. -INSERT INTO artist_similarity_unmatched ( - seed_artist_id, candidate_mbid, candidate_name, score, source -) VALUES ($1, $2, $3, $4, $5) -ON CONFLICT (seed_artist_id, candidate_mbid, source) DO UPDATE SET - candidate_name = EXCLUDED.candidate_name, - score = EXCLUDED.score, - fetched_at = now(); - -- name: ListSimilarArtistsForArtist :many -- In-library artists similar to a seed artist, ranked by best similarity -- score across sources (deduped per candidate). cover_album_id + album_count diff --git a/internal/db/queries/suggestion_snoozes.sql b/internal/db/queries/suggestion_snoozes.sql deleted file mode 100644 index 1bd47d0e..00000000 --- a/internal/db/queries/suggestion_snoozes.sql +++ /dev/null @@ -1,39 +0,0 @@ --- Time-boxed "not right now" on a Discover artist suggestion. --- --- See 0049_suggestion_snoozes.up.sql for why this is a snooze and not a --- dismissal: it records no verdict on the music, expires on its own, and --- must never reach the taste profile. Nothing in internal/taste may read --- this table. - --- name: SnoozeSuggestion :exec --- Upsert, so re-snoozing an already-snoozed candidate EXTENDS it instead of --- erroring on the PK. The name is refreshed too — a later suggestion may --- carry a corrected spelling from the similarity feed. --- $1=user_id, $2=candidate_mbid, $3=candidate_name, $4=duration in days. -INSERT INTO suggestion_snoozes (user_id, candidate_mbid, candidate_name, snoozed_until) -VALUES ($1, $2, $3, now() + ($4::float8 * INTERVAL '1 day')) -ON CONFLICT (user_id, candidate_mbid) DO UPDATE - SET snoozed_until = EXCLUDED.snoozed_until, - candidate_name = EXCLUDED.candidate_name; - --- name: UnsnoozeSuggestion :execrows --- Row count is returned so the handler can 404 an MBID that was never --- snoozed rather than reporting success for a no-op. -DELETE FROM suggestion_snoozes - WHERE user_id = $1 AND candidate_mbid = $2; - --- name: ListActiveSuggestionSnoozes :many --- Backs the manage / un-snooze surface. Expired rows are filtered HERE --- rather than left to the sweeper: gc runs on an hourly tick, so a row can --- outlive its expiry by up to a tick and must not read as still-snoozed in --- the meantime. -SELECT candidate_mbid, candidate_name, snoozed_until, created_at - FROM suggestion_snoozes - WHERE user_id = $1 AND snoozed_until > now() - ORDER BY snoozed_until, candidate_mbid; - --- name: GcDeleteExpiredSuggestionSnoozes :execrows --- Keeps the table from growing without bound. Every read already filters on --- snoozed_until > now(), so deleting an expired row changes no behaviour — --- this is purely reclamation. -DELETE FROM suggestion_snoozes WHERE snoozed_until < now(); diff --git a/internal/db/queries/tracks.sql b/internal/db/queries/tracks.sql index 6be1133e..95ca6b26 100644 --- a/internal/db/queries/tracks.sql +++ b/internal/db/queries/tracks.sql @@ -305,18 +305,10 @@ SELECT t.id, artists.id AS artist_id, artists.name AS artist_name, t.source_verdict, - t.source_verdict_at, - -- The album's place on the wanted list: wanted for a clean copy, and - -- when that was last searched for. - COALESCE(w.upgrade, '')::text AS want_upgrade, - w.searched_at AS searched_at + t.source_verdict_at FROM tracks t JOIN albums ON albums.id = t.album_id JOIN artists ON artists.id = t.artist_id - LEFT JOIN LATERAL ( - SELECT x.upgrade, x.searched_at FROM wants x - WHERE x.album_id = albums.id AND x.fulfilled_at IS NOT NULL LIMIT 1 - ) w ON true WHERE t.missing_since IS NULL AND regexp_replace(t.file_path, '^.*/', '') ~* sqlc.arg(pattern)::text ORDER BY directory, t.disc_number NULLS FIRST, t.track_number NULLS FIRST, t.title diff --git a/internal/db/queries/users.sql b/internal/db/queries/users.sql index 71e6400a..711eace2 100644 --- a/internal/db/queries/users.sql +++ b/internal/db/queries/users.sql @@ -44,8 +44,7 @@ UPDATE users SET subsonic_password = $2 WHERE id = $1; SELECT * FROM users WHERE id = $1; -- name: ListUsernamesByIDs :many --- Names a list's people in one round trip, e.g. who made each request on --- /admin/requests. +-- Names a list's people in one round trip. SELECT id, username FROM users WHERE id = ANY($1::uuid[]); -- name: SetListenBrainzToken :exec @@ -115,13 +114,6 @@ UPDATE users SET password_hash = $2 WHERE id = $1; --- name: UpdateUserAutoApprove :one --- Toggle the per-user auto_approve_requests flag. -UPDATE users - SET auto_approve_requests = $2 - WHERE id = $1 -RETURNING *; - -- name: SetDebugMode :one -- Toggle the per-account diagnostics/debug-reporting opt-in. Admin-driven -- from /admin (flip remotely while a bug is live) or self-driven OFF from diff --git a/internal/db/queries/wanted.sql b/internal/db/queries/wanted.sql deleted file mode 100644 index 11b49a98..00000000 --- a/internal/db/queries/wanted.sql +++ /dev/null @@ -1,501 +0,0 @@ --- The wanted list: release-type profiles, follows and wants. - --- name: ListReleaseTypeProfiles :many -SELECT p.*, - (SELECT COUNT(*) FROM follows f WHERE f.profile_id = p.id)::bigint AS follow_count - FROM release_type_profiles p - ORDER BY p.is_default DESC, p.name; - --- name: GetReleaseTypeProfile :one -SELECT * FROM release_type_profiles WHERE id = $1; - --- name: GetDefaultReleaseTypeProfile :one -SELECT * FROM release_type_profiles WHERE is_default; - --- name: CreateReleaseTypeProfile :one -INSERT INTO release_type_profiles (name, primary_types, secondary_types) -VALUES ($1, $2, $3) -RETURNING *; - --- name: UpdateReleaseTypeProfile :one -UPDATE release_type_profiles - SET name = $2, - primary_types = $3, - secondary_types = $4, - updated_at = now() - WHERE id = $1 -RETURNING *; - --- name: ClearDefaultReleaseTypeProfile :exec -UPDATE release_type_profiles SET is_default = false, updated_at = now() WHERE is_default; - --- name: SetDefaultReleaseTypeProfile :execrows -UPDATE release_type_profiles SET is_default = true, updated_at = now() WHERE id = $1; - --- name: DeleteReleaseTypeProfile :execrows --- The default stays; one in use is refused by the follows foreign key. -DELETE FROM release_type_profiles WHERE id = $1 AND NOT is_default; - --- name: UpsertFollow :one --- Following an artist already followed changes its profile. want_existing --- only ever turns on here; the worker turns it off once applied, and turning --- it on again re-reads the baseline so the existing releases are judged. -INSERT INTO follows (artist_mbid, artist_name, profile_id, followed_by, want_existing) -VALUES (sqlc.arg(artist_mbid), sqlc.arg(artist_name), sqlc.arg(profile_id), - sqlc.narg(followed_by), sqlc.arg(want_existing)) -ON CONFLICT (artist_mbid) DO UPDATE - SET artist_name = EXCLUDED.artist_name, - profile_id = EXCLUDED.profile_id, - want_existing = follows.want_existing OR EXCLUDED.want_existing, - baseline_at = CASE WHEN EXCLUDED.want_existing THEN NULL ELSE follows.baseline_at END, - updated_at = now() -RETURNING *; - --- name: GetFollow :one -SELECT f.*, p.name AS profile_name - FROM follows f - JOIN release_type_profiles p ON p.id = f.profile_id - WHERE f.artist_mbid = $1; - --- name: DeleteFollow :execrows -DELETE FROM follows WHERE artist_mbid = $1; - --- name: ListFollows :many -SELECT f.artist_mbid, f.artist_name, f.profile_id, f.followed_at, f.baseline_at, f.checked_at, - p.name AS profile_name, - (SELECT a.id FROM artists a WHERE a.mbid = f.artist_mbid) AS artist_id, - (SELECT COUNT(*) FROM wants w - WHERE w.artist_mbid = f.artist_mbid AND w.fulfilled_at IS NULL)::bigint AS open_wants - FROM follows f - JOIN release_type_profiles p ON p.id = f.profile_id - ORDER BY lower(f.artist_name), f.artist_mbid; - --- name: ListFollowsDue :many --- Follows whose discography has not been applied since @before, never-read --- first, so a new follow's baseline is not stuck behind daily re-checks. -SELECT f.* - FROM follows f - WHERE f.baseline_at IS NULL OR f.checked_at IS NULL OR f.checked_at < sqlc.arg(before) - ORDER BY f.baseline_at NULLS FIRST, f.checked_at NULLS FIRST, f.artist_mbid - LIMIT sqlc.arg(max_rows); - --- name: MarkFollowChecked :exec -UPDATE follows - SET checked_at = now(), - baseline_at = COALESCE(baseline_at, now()), - want_existing = false, - updated_at = now() - WHERE artist_mbid = $1; - --- name: ListFollowGroups :many --- An artist's release groups from the catalogue, each with whether the --- follow has judged it before. -SELECT g.mbid, g.title, g.primary_type, g.secondary_types, g.first_release_date, - EXISTS (SELECT 1 FROM follow_seen_groups s - WHERE s.artist_mbid = sqlc.arg(artist_mbid) AND s.release_group_mbid = g.mbid) AS seen - FROM mb_artist_release_groups ag - JOIN mb_release_groups g ON g.mbid = ag.release_group_mbid - WHERE ag.artist_mbid = sqlc.arg(artist_mbid) - ORDER BY g.first_release_date, g.title; - --- name: MarkGroupSeen :exec -INSERT INTO follow_seen_groups (artist_mbid, release_group_mbid) -VALUES ($1, $2) -ON CONFLICT DO NOTHING; - --- name: LibraryHoldsReleaseGroup :one --- Whether the library already has the group: an album tagged with it or --- with one of its releases, or failing that an album of the artist's with --- the same title. Only albums with a track on disk count. -SELECT EXISTS ( - SELECT 1 FROM albums a - WHERE (a.release_group_mbid = sqlc.arg(group_mbid)::text - OR a.mbid = sqlc.arg(group_mbid) - OR a.mbid IN (SELECT r.mbid FROM mb_releases r WHERE r.release_group_mbid = sqlc.arg(group_mbid)) - OR (lower(a.title) = lower(sqlc.arg(title)::text) - AND a.artist_id IN (SELECT ar.id FROM artists ar WHERE ar.mbid = sqlc.arg(artist_mbid)::text))) - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id AND t.missing_since IS NULL) -); - --- name: WantReleaseGroup :one --- Wants a release group, or returns the want already recorded for it. A --- held want is not reopened here: the library has it. -INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, primary_type, - first_release_date, reason, created_by) -VALUES (sqlc.arg(release_group_mbid), sqlc.arg(artist_mbid), sqlc.arg(artist_name), sqlc.arg(title), - sqlc.arg(primary_type), sqlc.arg(first_release_date), sqlc.arg(reason), sqlc.narg(created_by)) -ON CONFLICT (release_group_mbid) DO UPDATE SET updated_at = wants.updated_at -RETURNING *; - --- name: GetWantByReleaseGroup :one -SELECT * FROM wants WHERE release_group_mbid = $1; - --- name: ListOpenWants :many -SELECT * FROM wants - WHERE fulfilled_at IS NULL - ORDER BY wanted_at, id - LIMIT $1; - --- name: WantHoldings :many --- Each library album that may answer a want, with what it has on disk: --- tracks present, whether one arrived since the want, and whether a track --- missing before the want is still missing. -SELECT a.id AS album_id, - COUNT(*) FILTER (WHERE t.missing_since IS NULL)::int AS present, - COALESCE(bool_or(t.missing_since IS NULL AND t.added_at > sqlc.arg(since)::timestamptz), false)::bool AS arrived_since, - COALESCE(bool_or(t.missing_since IS NOT NULL AND t.missing_since < sqlc.arg(since)::timestamptz), false)::bool AS missing_before, - -- Missing tracks no track on disk stands in for, by position or - -- title. A missing row whose song is back at a new path is - -- covered; a lost bonus track is not. A track with no disc number is - -- on disc 1, so a missing track 6 with no disc beside a present 1/6 - -- does not keep the album wanted. - COUNT(*) FILTER (WHERE t.missing_since IS NOT NULL AND NOT EXISTS ( - SELECT 1 FROM tracks p - WHERE p.album_id = a.id AND p.missing_since IS NULL - AND ((p.track_number = t.track_number AND COALESCE(p.disc_number, 1) = COALESCE(t.disc_number, 1)) - OR lower(p.title) = lower(t.title))))::int AS uncovered_missing - FROM albums a - JOIN tracks t ON t.album_id = a.id - WHERE a.release_group_mbid = sqlc.arg(group_mbid)::text - OR a.mbid = sqlc.arg(group_mbid) - OR a.id = sqlc.narg(album_id) - OR a.mbid IN (SELECT r.mbid FROM mb_releases r WHERE r.release_group_mbid = sqlc.arg(group_mbid)) - GROUP BY a.id - ORDER BY present DESC, a.id; - --- name: FulfillWant :exec -UPDATE wants - SET fulfilled_at = now(), - album_id = sqlc.arg(album_id), - updated_at = now() - WHERE id = sqlc.arg(id) AND fulfilled_at IS NULL; - --- name: RepointWant :exec --- A want recorded under a release id is moved to its group. -UPDATE wants SET release_group_mbid = $2, updated_at = now() WHERE id = $1; - --- name: DeleteWant :execrows -DELETE FROM wants WHERE id = $1; - --- name: ListWants :many --- The Wanted page: open wants (all but missing ones, or only missing ones), --- the held ones, or all of them; artist_mbid narrows to one artist's page, --- want_ids to given wants. Ordered like the library: artists A–Z by the --- library's sort name when it holds the artist, each artist's albums by --- release date, so the page groups them as they arrive. q narrows to an --- artist or album title; total counts every match, past the page. -SELECT w.id, w.release_group_mbid, w.artist_mbid, w.artist_name, w.title, w.reason, - w.album_id, w.wanted_at, w.fulfilled_at, - -- Why a held album is wanted again, and the copy it has. - w.upgrade, w.held_quality, - -- A request names an album before the catalogue has read it: the - -- catalogue's type and date fill in once it has. - COALESCE(NULLIF(w.primary_type, ''), g.primary_type, '')::text AS primary_type, - COALESCE(g.secondary_types, '{}')::text[] AS secondary_types, - COALESCE(NULLIF(w.first_release_date, ''), g.first_release_date, '')::text AS first_release_date, - u.username AS requested_by, - la.id AS library_artist_id, - COUNT(*) OVER () AS total, - -- The search schedule's last look, and the download running. - w.searched_at, w.search_outcome, w.search_considered, w.best_release, w.best_verdict, w.search_error, - -- Its latest download, running or ended, and what the import made of - -- it. - gr.id AS grab_id, gr.name AS grab_name, gr.grabbed_at, gr.origin AS grab_origin, - gr.quality AS grab_quality, gr.ended_at AS grab_ended_at, gr.end_reason AS grab_end_reason, - gr.progress AS grab_progress, gr.import_state AS grab_import_state, - gr.import_reason AS grab_import_reason, gr.end_detail AS grab_end_detail, gr.imported_at AS grab_imported_at, - gr.imported_files AS grab_imported_files - FROM wants w - LEFT JOIN users u ON u.id = w.created_by - LEFT JOIN mb_release_groups g ON g.mbid = w.release_group_mbid - LEFT JOIN LATERAL (SELECT ar.id, ar.sort_name FROM artists ar WHERE ar.mbid = w.artist_mbid LIMIT 1) la ON true - LEFT JOIN grabs gr ON gr.id = (SELECT x.id FROM grabs x WHERE x.want_id = w.id - ORDER BY x.ended_at IS NULL DESC, x.grabbed_at DESC LIMIT 1) - WHERE CASE sqlc.arg(state)::text - WHEN 'all' THEN true - WHEN 'held' THEN w.fulfilled_at IS NOT NULL - WHEN 'upgrade' THEN w.fulfilled_at IS NOT NULL AND w.upgrade <> '' - WHEN 'missing' THEN w.fulfilled_at IS NULL AND w.reason = 'missing' - ELSE w.fulfilled_at IS NULL AND w.reason <> 'missing' - END - AND (sqlc.arg(artist_mbid)::text = '' OR w.artist_mbid = sqlc.arg(artist_mbid)::text) - -- want_ids narrows to given wants: the Missing page's folders. - AND (COALESCE(cardinality(sqlc.arg(want_ids)::uuid[]), 0) = 0 OR w.id = ANY(sqlc.arg(want_ids)::uuid[])) - AND (sqlc.arg(q)::text = '' - OR strpos(lower(w.artist_name), lower(sqlc.arg(q)::text)) > 0 - OR strpos(lower(w.title), lower(sqlc.arg(q)::text)) > 0) - ORDER BY lower(COALESCE(NULLIF(la.sort_name, ''), w.artist_name)), w.artist_mbid, - NULLIF(COALESCE(NULLIF(w.first_release_date, ''), g.first_release_date, ''), '') NULLS LAST, - lower(w.title), w.id - LIMIT sqlc.arg(max_rows) OFFSET sqlc.arg(skip); - --- name: ListWantArtists :many --- The Wanted view's main page: every artist on the list, A–Z like --- the library, with how many of their albums are held of how many are on --- the list. q matches the artist's name or any of their albums' titles; --- artist_mbid narrows to one artist, for their page's header. -WITH a AS ( - SELECT w.artist_mbid, - (array_agg(w.artist_name ORDER BY w.wanted_at DESC))[1] AS artist_name, - COUNT(*) FILTER (WHERE w.fulfilled_at IS NOT NULL)::int AS held, - COUNT(*) FILTER (WHERE w.fulfilled_at IS NULL AND w.reason <> 'missing')::int AS wanted, - COUNT(*) FILTER (WHERE w.fulfilled_at IS NULL AND w.reason = 'missing')::int AS missing, - COUNT(*)::int AS albums, - bool_or(sqlc.arg(q)::text <> '' AND strpos(lower(w.title), lower(sqlc.arg(q)::text)) > 0) AS title_match - FROM wants w - WHERE sqlc.arg(artist_mbid)::text = '' OR w.artist_mbid = sqlc.arg(artist_mbid)::text - GROUP BY w.artist_mbid -) -SELECT a.artist_mbid, a.artist_name::text AS artist_name, a.held, a.wanted, a.missing, a.albums, - COALESCE(NULLIF(la.sort_name, ''), a.artist_name)::text AS sort_name, - la.id AS library_artist_id, - (f.artist_mbid IS NOT NULL)::bool AS following, - (SELECT COUNT(*) FROM grabs g JOIN wants x ON x.id = g.want_id - WHERE x.artist_mbid = a.artist_mbid AND g.ended_at IS NULL)::int AS downloading, - COUNT(*) OVER () AS total - FROM a - LEFT JOIN LATERAL (SELECT ar.id, ar.sort_name FROM artists ar WHERE ar.mbid = a.artist_mbid LIMIT 1) la ON true - LEFT JOIN follows f ON f.artist_mbid = a.artist_mbid - WHERE sqlc.arg(q)::text = '' - OR strpos(lower(a.artist_name), lower(sqlc.arg(q)::text)) > 0 - OR a.title_match - ORDER BY lower(COALESCE(NULLIF(la.sort_name, ''), a.artist_name)), a.artist_mbid - LIMIT sqlc.arg(max_rows) OFFSET sqlc.arg(skip); - --- name: GetWantAlbum :one --- What the Wanted view's track list needs of a want. -SELECT id, release_group_mbid, album_id FROM wants WHERE id = $1; - --- name: ListLibraryAlbumTracks :many --- A held or missing album's own tracks, for the Wanted view's track list; --- missing says its file is gone. -SELECT title, disc_number, track_number, duration_ms, (missing_since IS NOT NULL)::bool AS missing - FROM tracks - WHERE album_id = $1 - ORDER BY disc_number NULLS FIRST, track_number NULLS LAST, title; - --- name: CountWants :one -SELECT COUNT(*) FILTER (WHERE fulfilled_at IS NULL AND reason <> 'missing')::bigint AS wanted, - COUNT(*) FILTER (WHERE fulfilled_at IS NULL AND reason = 'missing')::bigint AS missing, - COUNT(*) FILTER (WHERE fulfilled_at IS NOT NULL)::bigint AS held, - -- Held albums wanted for a better copy. - COUNT(*) FILTER (WHERE fulfilled_at IS NOT NULL AND upgrade <> '')::bigint AS upgrades, - COUNT(DISTINCT artist_mbid)::bigint AS artists, - -- Library albums waiting to be matched by hand. - (SELECT COUNT(*) FROM library_album_matches m - WHERE m.outcome = 'unmatched' - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = m.album_id))::bigint AS unmatched, - (SELECT COUNT(*) FROM follows)::bigint AS following, - -- The Wanted view's other tabs: downloads running, those - -- waiting on a person, and releases set aside. - (SELECT COUNT(*) FROM grabs WHERE ended_at IS NULL)::bigint AS activity, - (SELECT COUNT(*) FROM grabs - WHERE ended_at IS NULL AND import_state IN ('review', 'failed'))::bigint AS attention, - (SELECT COUNT(*) FROM release_blocks b - WHERE b.expires_at IS NULL OR b.expires_at > now())::bigint AS blocked - FROM wants; - --- The one-time listing of the library. --------------------------- - --- name: GetLibraryListing :one -SELECT library_listing_started_at, library_listed_at, library_unlisted - FROM acquisition_settings WHERE id = true; - --- name: StartLibraryListing :one --- Marks the listing begun, once; its time is the cutoff for which albums --- count as the library's. -UPDATE acquisition_settings - SET library_listing_started_at = COALESCE(library_listing_started_at, now()) - WHERE id = true -RETURNING library_listing_started_at; - --- name: ListLibraryAlbums :execrows --- Puts library albums on the list as held: each release group once, by the --- album holding the most tracks. Only albums the library had at the cutoff, --- and with album_id given only that album. A group already on the list is --- left as it is. An album with tracks already missing is held from just --- before the first loss, so re-acquisition still fetches it back. -INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id, wanted_at, fulfilled_at) -SELECT DISTINCT ON (a.release_group_mbid) - a.release_group_mbid, ar.mbid, ar.name, a.title, 'library', a.id, now(), - COALESCE((SELECT MIN(t.missing_since) FROM tracks t WHERE t.album_id = a.id) - interval '1 second', now()) - FROM albums a - JOIN artists ar ON ar.id = a.artist_id - WHERE a.release_group_mbid IS NOT NULL AND a.release_group_mbid <> '' - AND ar.mbid IS NOT NULL AND ar.mbid <> '' - AND a.created_at <= sqlc.arg(cutoff)::timestamptz - AND (sqlc.narg(album_id)::uuid IS NULL OR a.id = sqlc.narg(album_id)::uuid) - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id) - ORDER BY a.release_group_mbid, - (SELECT COUNT(*) FROM tracks t WHERE t.album_id = a.id AND t.missing_since IS NULL) DESC, a.id -ON CONFLICT (release_group_mbid) DO NOTHING; - --- name: RecordListedAlbums :exec --- The albums ListLibraryAlbums takes are decided as listed by their tags, --- so the matching that follows leaves them alone. -INSERT INTO library_album_matches (album_id, outcome, method, release_group_mbid) -SELECT a.id, 'listed', 'tags', a.release_group_mbid - FROM albums a - JOIN artists ar ON ar.id = a.artist_id - WHERE a.release_group_mbid IS NOT NULL AND a.release_group_mbid <> '' - AND ar.mbid IS NOT NULL AND ar.mbid <> '' - AND a.created_at <= sqlc.arg(cutoff)::timestamptz - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id) -ON CONFLICT (album_id) DO NOTHING; - --- name: ListAlbumsToMatch :many --- Library albums the listing has not decided, or that an older matcher --- refused, or whose tracks changed since they were refused: each is matched --- to MusicBrainz. A library album is one from before the cutoff, or --- one whose tracks were: a re-tag files a track under a new album row but --- keeps the track. An album row with no tracks is not an album in --- the library; one a person left off stays off. -SELECT a.id, a.title, a.mbid, a.release_group_mbid, ar.name AS artist_name, ar.mbid AS artist_mbid - FROM albums a - JOIN artists ar ON ar.id = a.artist_id - WHERE (a.created_at <= sqlc.arg(cutoff)::timestamptz - OR EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id AND t.added_at <= sqlc.arg(cutoff)::timestamptz)) - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id) - AND NOT EXISTS (SELECT 1 FROM library_album_matches m - WHERE m.album_id = a.id - AND (m.outcome IN ('listed', 'dismissed') - OR (m.matcher >= sqlc.arg(matcher)::int - AND NOT EXISTS (SELECT 1 FROM tracks t - WHERE t.album_id = a.id AND t.updated_at > m.decided_at)))) - ORDER BY a.created_at, a.id - LIMIT sqlc.arg(max_rows); - --- name: CountAlbumsToMatch :one --- Whether a finished listing has albums to go back to: those --- ListAlbumsToMatch would hand it. -SELECT COUNT(*) - FROM albums a - WHERE (a.created_at <= sqlc.arg(cutoff)::timestamptz - OR EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id AND t.added_at <= sqlc.arg(cutoff)::timestamptz)) - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = a.id) - AND NOT EXISTS (SELECT 1 FROM library_album_matches m - WHERE m.album_id = a.id - AND (m.outcome IN ('listed', 'dismissed') - OR (m.matcher >= sqlc.arg(matcher)::int - AND NOT EXISTS (SELECT 1 FROM tracks t - WHERE t.album_id = a.id AND t.updated_at > m.decided_at)))); - --- name: ListAlbumTracksToMatch :many --- What an album's tracks say about themselves, to check a match against. -SELECT title, disc_number, track_number, duration_ms, mbid - FROM tracks - WHERE album_id = $1 - ORDER BY disc_number NULLS FIRST, track_number NULLS LAST, title; - --- name: SetAlbumReleaseGroup :exec --- Names a matched album's release group, filling only what nothing named. -UPDATE albums SET release_group_mbid = sqlc.arg(release_group_mbid), updated_at = now() - WHERE id = sqlc.arg(id) AND (release_group_mbid IS NULL OR release_group_mbid = ''); - --- name: InsertLibraryWant :execrows --- Puts one matched album on the list as held, from just before its first --- lost track if it has one. A group already on the list is left as it is. -INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, primary_type, first_release_date, - reason, album_id, wanted_at, fulfilled_at) -VALUES (sqlc.arg(release_group_mbid), sqlc.arg(artist_mbid), sqlc.arg(artist_name), sqlc.arg(title), - sqlc.arg(primary_type), sqlc.arg(first_release_date), 'library', sqlc.arg(album_id)::uuid, now(), - COALESCE((SELECT MIN(t.missing_since) FROM tracks t WHERE t.album_id = sqlc.arg(album_id)::uuid) - - interval '1 second', now())) -ON CONFLICT (release_group_mbid) DO NOTHING; - --- name: RecordAlbumMatch :exec -INSERT INTO library_album_matches (album_id, outcome, method, release_group_mbid, detail, matcher) -VALUES (sqlc.arg(album_id), sqlc.arg(outcome), sqlc.arg(method), sqlc.arg(release_group_mbid), sqlc.arg(detail), - sqlc.arg(matcher)) -ON CONFLICT (album_id) DO UPDATE - SET outcome = EXCLUDED.outcome, method = EXCLUDED.method, release_group_mbid = EXCLUDED.release_group_mbid, - detail = EXCLUDED.detail, matcher = EXCLUDED.matcher, decided_at = now(); - --- name: FinishLibraryListing :one --- The listing is done, or done again after a newer matcher's retries: --- count the albums no match was found for. The first finish is kept. -UPDATE acquisition_settings - SET library_listed_at = COALESCE(library_listed_at, now()), - library_unlisted = (SELECT COUNT(*) FROM library_album_matches m - WHERE m.outcome = 'unmatched' - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = m.album_id)) - WHERE id = true -RETURNING library_listed_at, library_unlisted; - --- name: ListUnmatchedLibraryAlbums :many --- The Collection's Unmatched section: the albums the listing could --- not match, and below them the ones a person left off. An album whose --- tracks have all moved to another row is gone, not unmatched. -SELECT m.album_id, a.title, ar.name AS artist_name, m.detail, m.outcome, m.decided_at, - (SELECT COUNT(*) FROM tracks t WHERE t.album_id = a.id AND t.missing_since IS NULL)::int AS tracks - FROM library_album_matches m - JOIN albums a ON a.id = m.album_id - JOIN artists ar ON ar.id = a.artist_id - WHERE m.outcome IN ('unmatched', 'dismissed') - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = m.album_id) - ORDER BY m.outcome DESC, lower(ar.name), lower(a.title) - LIMIT sqlc.arg(max_rows); - --- name: RecountLibraryUnlisted :exec --- After a person matches or leaves off an album, the count the catalogue --- shows. -UPDATE acquisition_settings - SET library_unlisted = (SELECT COUNT(*) FROM library_album_matches m - WHERE m.outcome = 'unmatched' - AND EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = m.album_id)) - WHERE id = true; - --- name: GetAlbumToMatch :one --- One album a person is matching by hand, as the matcher reads albums. -SELECT a.id, a.title, a.mbid, a.release_group_mbid, ar.name AS artist_name, ar.mbid AS artist_mbid - FROM albums a - JOIN artists ar ON ar.id = a.artist_id - JOIN library_album_matches m ON m.album_id = a.id AND m.outcome IN ('unmatched', 'dismissed') - WHERE a.id = $1; - --- name: SetAlbumMatchOutcome :execrows --- Leave an unmatched album off the list, or put a left-off one back. -UPDATE library_album_matches SET outcome = sqlc.arg(outcome), decided_at = now() - WHERE album_id = sqlc.arg(album_id) AND outcome = sqlc.arg(was); - - --- Upgrades. ------------------------------------------------------- - --- name: ListHeldWantFormats :many --- What each held album's copy is made of, for judging whether it is worth --- upgrading: its present tracks grouped by format, with their bytes and --- length summed, and how many are held back as video rips. A held want whose --- album has no track with a length yet has no rows. -SELECT w.id AS want_id, w.upgrade, w.held_tier, w.held_quality, - lower(t.file_format)::text AS file_format, - SUM(t.file_size)::bigint AS bytes, - SUM(t.duration_ms)::bigint AS duration_ms, - COUNT(*)::int AS tracks, - COUNT(*) FILTER (WHERE t.source_verdict = 'suspect')::int AS rips - FROM wants w - JOIN tracks t ON t.album_id = w.album_id - WHERE w.fulfilled_at IS NOT NULL - AND t.missing_since IS NULL AND t.duration_ms > 0 - GROUP BY w.id, lower(t.file_format) - ORDER BY w.id; - --- name: SetWantUpgrades :exec --- Writes the upgrade check's verdicts, one row per want. -UPDATE wants w - SET upgrade = v.upgrade, - held_tier = v.held_tier, - held_quality = v.held_quality, - upgrade_checked_at = now(), - updated_at = CASE WHEN w.upgrade <> v.upgrade THEN now() ELSE w.updated_at END - FROM (SELECT unnest(sqlc.arg(ids)::uuid[]) AS id, - unnest(sqlc.arg(upgrades)::text[]) AS upgrade, - unnest(sqlc.arg(held_tiers)::text[]) AS held_tier, - unnest(sqlc.arg(held_qualities)::text[]) AS held_quality) v - WHERE w.id = v.id; - --- name: ClearHeldUpgrades :execrows --- A held want whose album has no track left to judge is not wanted again. -UPDATE wants SET upgrade = '', held_tier = '', held_quality = '', upgrade_checked_at = now() - WHERE fulfilled_at IS NOT NULL AND upgrade <> '' - AND NOT EXISTS (SELECT 1 FROM tracks t - WHERE t.album_id = wants.album_id AND t.missing_since IS NULL AND t.duration_ms > 0); diff --git a/internal/downloader/downloader_test.go b/internal/downloader/downloader_test.go deleted file mode 100644 index 5af62b57..00000000 --- a/internal/downloader/downloader_test.go +++ /dev/null @@ -1,156 +0,0 @@ -package downloader - -import ( - "context" - "errors" - "io" - "net/http" - "net/http/httptest" - "strings" - "sync/atomic" - "testing" -) - -func TestPathMap(t *testing.T) { - m := PathMap{Local: "/downloads", Remote: "/data/torrents/"} - for in, want := range map[string]string{ - "/downloads": "/data/torrents", - "/downloads/abc/x.mp3": "/data/torrents/abc/x.mp3", - "/downloads2/abc": "/downloads2/abc", - "/music/a": "/music/a", - "": "", - } { - if got := m.ToRemote(in); got != want { - t.Errorf("ToRemote(%q) = %q, want %q", in, got, want) - } - } - if got := m.ToLocal("/data/torrents/abc"); got != "/downloads/abc" { - t.Errorf("ToLocal = %q", got) - } - if (PathMap{Local: "/downloads", Remote: "/downloads/"}).Active() { - t.Error("the same folder under one name is not a mapping") - } -} - -// fakeQbit is a qBittorrent WebUI that wants a login, and that expires the -// session once to make the client log in again. -func fakeQbit(t *testing.T, expireOnce bool) (*httptest.Server, *atomic.Int32) { - t.Helper() - var logins atomic.Int32 - var expired atomic.Bool - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("Referer") == "" { - w.WriteHeader(http.StatusForbidden) - return - } - switch r.URL.Path { - case "/api/v2/auth/login": - _ = r.ParseForm() - if r.Form.Get("password") != "pw" { - _, _ = w.Write([]byte("Fails.")) - return - } - logins.Add(1) - http.SetCookie(w, &http.Cookie{Name: "SID", Value: "s", Path: "/"}) - _, _ = w.Write([]byte("Ok.")) - case "/api/v2/torrents/info": - if _, err := r.Cookie("SID"); err != nil { - w.WriteHeader(http.StatusForbidden) - return - } - if expireOnce && !expired.Swap(true) { - w.WriteHeader(http.StatusForbidden) - return - } - if r.URL.Query().Get("category") != "minstrel" { - t.Errorf("category = %q", r.URL.Query().Get("category")) - } - _, _ = w.Write([]byte(`[{"hash": "ABC", "name": "x", "save_path": "/data/torrents/abc", "progress": 1}]`)) - case "/api/v2/torrents/add": - body, _ := io.ReadAll(r.Body) - if !strings.Contains(string(body), "/data/torrents/new") { - t.Errorf("add body has no translated save path: %s", body) - } - _, _ = w.Write([]byte("Ok.")) - default: - w.WriteHeader(http.StatusNotFound) - } - })) - t.Cleanup(srv.Close) - return srv, &logins -} - -func TestQBittorrent_LogsInAndLogsInAgainWhenTheSessionExpires(t *testing.T) { - srv, logins := fakeQbit(t, true) - d := Mapped(&Client{Config: Config{BaseURL: srv.URL, Username: "u", Password: "pw", Category: "minstrel"}}, - PathMap{Local: "/downloads", Remote: "/data/torrents"}) - ts, err := d.Torrents(context.Background()) - if err != nil { - t.Fatalf("torrents: %v", err) - } - if logins.Load() != 2 { - t.Errorf("logins = %d, want 2 (the second after the 403)", logins.Load()) - } - if len(ts) != 1 || ts[0].Hash != "abc" || ts[0].SavePath != "/downloads/abc" || !ts[0].Done() { - t.Errorf("torrents = %+v, want one, hash lowercased, path in Minstrel's terms", ts) - } - if err := d.AddTorrent(context.Background(), Add{Magnet: "magnet:?xt=urn:btih:abc", SavePath: "/downloads/new"}); err != nil { - t.Errorf("add: %v", err) - } -} - -func TestQBittorrent_AWrongPasswordIsAuth(t *testing.T) { - srv, _ := fakeQbit(t, false) - c := &Client{Config: Config{BaseURL: srv.URL, Password: "nope"}} - if _, err := c.Torrents(context.Background()); !errors.Is(err, ErrAuth) { - t.Fatalf("err = %v, want ErrAuth", err) - } -} - -func TestExchange_MapsDownloadsAndRefusesWhileRestoring(t *testing.T) { - var restoring, sentKey atomic.Bool - restoring.Store(true) - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("Authorization") != "" { - sentKey.Store(true) - } - pending := "0" - if restoring.Load() { - pending = "3" - } - _, _ = io.WriteString(w, `{"enabled": true, "running": false, "held_reason": "disk full", "restore_pending": `+pending+`, "downloads": [ - {"infohash": "ABC", "name": "Album", "save_path": "/dl", "state": "seeding", "progress": 1, "length": 10, "bytes": 10, "category": "minstrel"}, - {"infohash": "DEF", "name": "Other", "save_path": "/dl", "state": "fast", "category": "someone-else"}, - {"infohash": "123", "name": "Broken", "save_path": "/dl", "failed": true, "fail_reason": "tracker gone", "category": "minstrel"} - ]}`) - })) - defer srv.Close() - - c := &Exchange{Config: Config{BaseURL: srv.URL, Category: "minstrel"}} - if _, err := c.Torrents(context.Background()); !errors.Is(err, ErrRestoring) { - t.Fatalf("while restoring: err = %v, want ErrRestoring", err) - } - if sentKey.Load() { - t.Error("an Authorization header was sent with no key set") - } - restoring.Store(false) - ts, err := c.Torrents(context.Background()) - if err != nil { - t.Fatalf("torrents: %v", err) - } - if len(ts) != 2 { - t.Fatalf("torrents = %+v, want the two in Minstrel's category", ts) - } - if ts[0].Hash != "abc" || ts[0].State != "uploading" || ts[0].ContentPath != "/dl/Album" || !strings.Contains(ts[0].EngineDown, "disk full") { - t.Errorf("first = %+v", ts[0]) - } - if !ts[1].Failed() || ts[1].FailReason != "tracker gone" { - t.Errorf("second = %+v, want failed with Exchange's reason", ts[1]) - } -} - -func TestFor_RefusesAnUnknownProtocol(t *testing.T) { - if _, err := For("transmission", Config{}); err == nil { - t.Fatal("want an error for a client Minstrel cannot drive") - } -} diff --git a/internal/downloader/driver.go b/internal/downloader/driver.go deleted file mode 100644 index 2b1d8d99..00000000 --- a/internal/downloader/driver.go +++ /dev/null @@ -1,197 +0,0 @@ -// Package downloader drives the download clients Minstrel hands a grab to: -// qBittorrent's WebUI v2, and FabledExchange's native /api/downloads. -// -// Both answer through one Driver, and Torrent is qBittorrent's shape: the -// Exchange driver maps its own downloads onto it, so the grab, the importer -// and the stall watchdog read one kind of record whichever client holds the -// torrent. -package downloader - -import ( - "context" - "errors" - "fmt" - "time" -) - -// Protocols a download client row may name. -const ( - KindQBittorrent = "qbittorrent" - KindExchange = "exchange" -) - -// ErrUnconfigured is a client with no address. -var ErrUnconfigured = errors.New("download client is not configured") - -// ErrUnreachable is a call that could not be made, or a refusal that is not -// about credentials. -var ErrUnreachable = errors.New("download client unreachable") - -// ErrAuth is credentials the client refused. -var ErrAuth = errors.New("download client refused the credentials") - -// ErrRestoring is an Exchange still bringing its downloads back after a -// restart: its list is incomplete, so nothing may be concluded from it. -var ErrRestoring = errors.New("download client is still restoring its downloads after a restart") - -// ErrDuplicate is a torrent the client already holds. -var ErrDuplicate = errors.New("download client already holds this torrent") - -// ErrUnsupported is a call this client has no equivalent for. -var ErrUnsupported = errors.New("download client cannot do that") - -// UserAgent names Minstrel in the client's logs. -const UserAgent = "Minstrel (self-hosted music server; download client)" - -// DefaultTimeout is what an unset Timeout means: a download client answers -// from its own state. -const DefaultTimeout = 15 * time.Second - -// Config is one download client. -type Config struct { - BaseURL string - // Username and Password are qBittorrent's. Both may be empty for a WebUI - // that bypasses auth for local addresses. - Username string - Password string - // APIKey is Exchange's, sent as a bearer only when set. - APIKey string - // Category files everything Minstrel adds, so its torrents can be told - // from anything else in the client. - Category string - Timeout time.Duration -} - -// Driver is what Minstrel needs from a download client. -type Driver interface { - AddTorrent(ctx context.Context, a Add) error - // Torrents lists this client's torrents in Config.Category, or only the - // given hashes. - Torrents(ctx context.Context, hashes ...string) ([]Torrent, error) - // Connected says whether the client can reach any peer at all. - Connected(ctx context.Context) (bool, error) - Stop(ctx context.Context, hashes ...string) error - Start(ctx context.Context, hashes ...string) error - Reannounce(ctx context.Context, hashes ...string) error - Recheck(ctx context.Context, hashes ...string) error - Delete(ctx context.Context, deleteFiles bool, hashes ...string) error -} - -// FileSkipper sets which files of a torrent are downloaded. -type FileSkipper interface { - SkipFiles(ctx context.Context, hash string, indices []int) error - KeepFiles(ctx context.Context, hash string, indices []int) error -} - -// File is one file inside a torrent. -type File struct { - Index int - Path string - Size int64 -} - -// FileLister lists a torrent's files. -type FileLister interface { - Files(ctx context.Context, hash string) ([]File, error) -} - -// FileState is one file's progress. -type FileState struct { - Path string - Size int64 - Done int64 - Skipped bool -} - -// Unfinished says whether the file is wanted and not yet whole. -func (f FileState) Unfinished() bool { return !f.Skipped && f.Done < f.Size } - -// FileProgress reports per-file progress. -type FileProgress interface { - FileStates(ctx context.Context, hash string) ([]FileState, error) -} - -var ( - _ Driver = (*Client)(nil) - _ Driver = (*Exchange)(nil) - _ FileSkipper = (*Client)(nil) - _ FileSkipper = (*Exchange)(nil) - _ FileSkipper = (*mapped)(nil) - _ FileLister = (*Client)(nil) - _ FileLister = (*Exchange)(nil) - _ FileLister = (*mapped)(nil) - _ FileProgress = (*Client)(nil) - _ FileProgress = (*Exchange)(nil) - _ FileProgress = (*mapped)(nil) -) - -// For builds the driver a protocol names. -func For(protocol string, cfg Config) (Driver, error) { - switch protocol { - case KindQBittorrent: - return &Client{Config: cfg}, nil - case KindExchange: - return &Exchange{Config: cfg}, nil - } - return nil, fmt.Errorf("download client %q is not one Minstrel can drive", protocol) -} - -// Torrent is one torrent in qBittorrent's JSON shape. -type Torrent struct { - Hash string `json:"hash"` - Name string `json:"name"` - Size int64 `json:"size"` - Progress float64 `json:"progress"` - State string `json:"state"` - Category string `json:"category"` - Tags string `json:"tags"` - SavePath string `json:"save_path"` - ContentPath string `json:"content_path"` - AddedOn int64 `json:"added_on"` - NumSeeds int `json:"num_seeds"` - NumLeechs int `json:"num_leechs"` - AmountLeft int64 `json:"amount_left"` - Ratio float64 `json:"ratio"` - DLSpeed int64 `json:"dlspeed"` - ETA int64 `json:"eta"` - // EngineDown is why the client's whole engine is not moving anything, in - // words; empty when it is running. Exchange says so; qBittorrent does not. - EngineDown string `json:"-"` - // FailReason is the client's own reason for a failed torrent. - FailReason string `json:"-"` -} - -// Done says the torrent is whole. -func (t Torrent) Done() bool { return t.Progress >= 1 } - -// Failed says the client gave up on it. -func (t Torrent) Failed() bool { return t.State == "error" || t.State == "missingFiles" } - -// Fetching says it is still fetching metadata. -func (t Torrent) Fetching() bool { return t.State == "metaDL" || t.State == "forcedMetaDL" } - -// Alone says nobody is sharing an unfinished torrent. -func (t Torrent) Alone() bool { return !t.Done() && t.NumSeeds == 0 && t.NumLeechs == 0 } - -// Added is when the client took it. -func (t Torrent) Added() time.Time { - if t.AddedOn <= 0 { - return time.Time{} - } - return time.Unix(t.AddedOn, 0) -} - -// Add is one torrent to hand a client: a magnet or the .torrent bytes. -type Add struct { - Magnet string - Torrent []byte - // SavePath is where it lands, as Minstrel sees it; Mapped translates it. - // Required: a download with no save path lands outside the folder - // Minstrel imports from. - SavePath string - Tags []string - Paused bool - // StopAtMetadata asks qBittorrent to stop once it has the file list, so - // unwanted files can be skipped before any data moves. - StopAtMetadata bool -} diff --git a/internal/downloader/exchange.go b/internal/downloader/exchange.go deleted file mode 100644 index e1b04fd0..00000000 --- a/internal/downloader/exchange.go +++ /dev/null @@ -1,340 +0,0 @@ -package downloader - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "net/url" - "path" - "strings" - "time" -) - -// Exchange drives FabledExchange's native /api/downloads: not its qBittorrent -// facade, so its own states, failure reasons and engine status come through. -type Exchange struct { - Config Config - // HTTP is injectable for tests. Nil takes a plain client. - HTTP *http.Client -} - -type exchangeItem struct { - InfoHash string `json:"infohash"` - Name string `json:"name"` - Bytes int64 `json:"bytes"` - Length int64 `json:"length"` - Progress float64 `json:"progress"` - State string `json:"state"` - Peers int `json:"peers"` - Category string `json:"category"` - Tags []string `json:"tags"` - SavePath string `json:"save_path"` - AddedAt time.Time `json:"added_at"` - Failed bool `json:"failed"` - FailReason string `json:"fail_reason"` - Ratio float64 `json:"ratio"` -} - -type exchangeSnapshot struct { - Enabled bool `json:"enabled"` - Running bool `json:"running"` - HeldReason string `json:"held_reason"` - InboundConns int64 `json:"inbound_conns"` - RestorePending int `json:"restore_pending"` - Downloads []exchangeItem `json:"downloads"` -} - -func (c *Exchange) AddTorrent(ctx context.Context, a Add) error { - if a.Magnet == "" && len(a.Torrent) == 0 { - return fmt.Errorf("add: neither a magnet nor torrent bytes") - } - if a.SavePath == "" { - return fmt.Errorf("add: no save path, which would land the download outside the folder Minstrel imports from") - } - if len(a.Torrent) > 0 { - q := url.Values{"save_path": {a.SavePath}} - if c.Config.Category != "" { - q.Set("category", c.Config.Category) - } - for _, t := range a.Tags { - q.Add("tag", t) - } - if a.Paused { - q.Set("paused", "true") - } - _, err := c.call(ctx, http.MethodPost, "/api/downloads/torrent?"+q.Encode(), "application/x-bittorrent", a.Torrent) - return err - } - body, err := json.Marshal(map[string]any{ - "magnet": a.Magnet, "category": c.Config.Category, "save_path": a.SavePath, - "tags": a.Tags, "paused": a.Paused, - }) - if err != nil { - return fmt.Errorf("add: %w", err) - } - _, err = c.call(ctx, http.MethodPost, "/api/downloads", "application/json", body) - return err -} - -// Torrents maps Exchange's downloads onto qBittorrent's shape. While Exchange -// is still restoring after a restart its list is incomplete, so it answers -// ErrRestoring rather than a list that looks like downloads vanished. -func (c *Exchange) Torrents(ctx context.Context, hashes ...string) ([]Torrent, error) { - snap, err := c.snapshot(ctx) - if err != nil { - return nil, err - } - if snap.RestorePending > 0 { - return nil, fmt.Errorf("%w (%d still to come back)", ErrRestoring, snap.RestorePending) - } - want := map[string]bool{} - for _, h := range hashes { - want[strings.ToLower(strings.TrimSpace(h))] = true - } - down := "" - switch { - case snap.Running: - case !snap.Enabled: - down = "the download engine is switched off in FabledExchange" - case snap.HeldReason != "": - down = "the download engine is held (" + snap.HeldReason + ") and resumes by itself" - default: - down = "the download engine is enabled but not running; FabledExchange is retrying it" - } - out := make([]Torrent, 0, len(snap.Downloads)) - for _, it := range snap.Downloads { - if c.Config.Category != "" && it.Category != c.Config.Category { - continue - } - t := asTorrent(it) - t.EngineDown = down - if len(want) > 0 && !want[t.Hash] { - continue - } - out = append(out, t) - } - return out, nil -} - -func asTorrent(it exchangeItem) Torrent { - content := it.SavePath - if it.Name != "" { - content = path.Join(it.SavePath, it.Name) - } - left := max(it.Length-it.Bytes, 0) - state := exchangeState(it.State, it.Progress) - if it.Failed { - state = "error" - } - var added int64 - if !it.AddedAt.IsZero() { - added = it.AddedAt.Unix() - } - return Torrent{ - Hash: strings.ToLower(it.InfoHash), Name: it.Name, Size: it.Length, - Progress: it.Progress, State: state, Category: it.Category, - Tags: strings.Join(it.Tags, ","), SavePath: it.SavePath, ContentPath: content, - AddedOn: added, NumLeechs: it.Peers, AmountLeft: left, - FailReason: it.FailReason, Ratio: it.Ratio, - } -} - -// exchangeState names Exchange's state in qBittorrent's vocabulary. -func exchangeState(tier string, progress float64) string { - switch tier { - case "fast", "slow": - return "downloading" - case "metadata": - return "metaDL" - case "stalled": - return "stalledDL" - case "queued": - return "queuedDL" - case "seeding": - return "uploading" - case "relocating": - return "moving" - case "paused": - if progress >= 1 { - return "pausedUP" - } - return "pausedDL" - } - return "unknown" -} - -// Connected is a running engine that has had an inbound connection: the -// nearest Exchange comes to qBittorrent's "connected". -func (c *Exchange) Connected(ctx context.Context) (bool, error) { - snap, err := c.snapshot(ctx) - if err != nil { - return false, err - } - return snap.Running && snap.InboundConns > 0, nil -} - -func (c *Exchange) Stop(ctx context.Context, hashes ...string) error { - return c.each(ctx, hashes, func(h string) error { - _, err := c.call(ctx, http.MethodPost, "/api/downloads/"+url.PathEscape(h)+"/pause", "", nil) - return err - }) -} - -func (c *Exchange) Start(ctx context.Context, hashes ...string) error { - return c.each(ctx, hashes, func(h string) error { - _, err := c.call(ctx, http.MethodPost, "/api/downloads/"+url.PathEscape(h)+"/resume", "", nil) - return err - }) -} - -func (c *Exchange) SkipFiles(ctx context.Context, hash string, indices []int) error { - return c.filePriority(ctx, hash, indices, "skip") -} - -func (c *Exchange) KeepFiles(ctx context.Context, hash string, indices []int) error { - return c.filePriority(ctx, hash, indices, "normal") -} - -func (c *Exchange) filePriority(ctx context.Context, hash string, indices []int, priority string) error { - if len(indices) == 0 { - return nil - } - body, err := json.Marshal(map[string]any{"indices": indices, "priority": priority}) - if err != nil { - return err - } - _, err = c.call(ctx, http.MethodPatch, "/api/downloads/"+url.PathEscape(hash)+"/files", "application/json", body) - return err -} - -func (c *Exchange) Files(ctx context.Context, hash string) ([]File, error) { - raw, err := c.files(ctx, hash) - if err != nil { - return nil, err - } - out := make([]File, len(raw)) - for i, f := range raw { - out[i] = File{Index: f.Index, Path: f.Path, Size: f.Length} - } - return out, nil -} - -func (c *Exchange) FileStates(ctx context.Context, hash string) ([]FileState, error) { - raw, err := c.files(ctx, hash) - if err != nil { - return nil, err - } - out := make([]FileState, len(raw)) - for i, f := range raw { - if f.Bytes == nil { - return nil, fmt.Errorf("%w: FabledExchange does not say how much of each file it has", ErrUnsupported) - } - out[i] = FileState{Path: f.Path, Size: f.Length, Done: *f.Bytes, Skipped: f.Priority == "skip"} - } - return out, nil -} - -type exchangeFile struct { - Index int `json:"index"` - Path string `json:"path"` - Length int64 `json:"length"` - Bytes *int64 `json:"bytes"` - Priority string `json:"priority"` -} - -func (c *Exchange) files(ctx context.Context, hash string) ([]exchangeFile, error) { - body, err := c.call(ctx, http.MethodGet, "/api/downloads/"+url.PathEscape(hash), "", nil) - if err != nil { - return nil, err - } - var detail struct { - Files []exchangeFile `json:"files"` - } - if err := json.Unmarshal(body, &detail); err != nil { - return nil, fmt.Errorf("%w: parse download detail: %v", ErrUnreachable, err) - } - return detail.Files, nil -} - -func (c *Exchange) Reannounce(context.Context, ...string) error { - return fmt.Errorf("%w: FabledExchange schedules its own announces", ErrUnsupported) -} - -func (c *Exchange) Recheck(context.Context, ...string) error { - return fmt.Errorf("%w: FabledExchange has no recheck", ErrUnsupported) -} - -func (c *Exchange) Delete(ctx context.Context, deleteFiles bool, hashes ...string) error { - return c.each(ctx, hashes, func(h string) error { - p := "/api/downloads/" + url.PathEscape(h) - if deleteFiles { - p += "?delete_files=true" - } - _, err := c.call(ctx, http.MethodDelete, p, "", nil) - return err - }) -} - -func (c *Exchange) each(ctx context.Context, hashes []string, do func(string) error) error { - for _, h := range hashes { - if ctx.Err() != nil { - return ctx.Err() - } - if err := do(strings.ToLower(strings.TrimSpace(h))); err != nil { - return err - } - } - return nil -} - -func (c *Exchange) snapshot(ctx context.Context) (exchangeSnapshot, error) { - var snap exchangeSnapshot - body, err := c.call(ctx, http.MethodGet, "/api/downloads", "", nil) - if err != nil { - return snap, err - } - if err := json.Unmarshal(body, &snap); err != nil { - return snap, fmt.Errorf("%w: parse download list: %v", ErrUnreachable, err) - } - return snap, nil -} - -func (c *Exchange) call(ctx context.Context, method, p, contentType string, body []byte) ([]byte, error) { - if c.Config.BaseURL == "" { - return nil, ErrUnconfigured - } - httpc := c.HTTP - if httpc == nil { - httpc = &http.Client{} - } - status, out, err := send(ctx, httpc, c.Config.Timeout, method, - strings.TrimRight(c.Config.BaseURL, "/")+p, contentType, body, func(h http.Header) { - h.Set("Accept", "application/json") - if c.Config.APIKey != "" { - h.Set("Authorization", "Bearer "+c.Config.APIKey) - } - }) - if err != nil { - return nil, err - } - if status >= 200 && status < 300 { - return out, nil - } - var e struct { - Error string `json:"error"` - Reason string `json:"reason"` - } - _ = json.Unmarshal(out, &e) - said := strings.TrimSpace(e.Error) - if said == "" { - said = fmt.Sprintf("HTTP %d", status) - } - switch { - case status == http.StatusUnauthorized || status == http.StatusForbidden: - return nil, fmt.Errorf("%w: FabledExchange refused the API key (%s)", ErrAuth, said) - case status == http.StatusConflict && e.Reason == "duplicate": - return nil, fmt.Errorf("%w: %s", ErrDuplicate, said) - } - return nil, fmt.Errorf("%w: %s", ErrUnreachable, said) -} diff --git a/internal/downloader/pathmap.go b/internal/downloader/pathmap.go deleted file mode 100644 index be43772e..00000000 --- a/internal/downloader/pathmap.go +++ /dev/null @@ -1,107 +0,0 @@ -package downloader - -import ( - "context" - "path" - "strings" -) - -// PathMap is one downloads folder under two names: where Minstrel has it -// mounted (Local) and where the client sees it (Remote). The two usually run -// in different containers. Every path crossing between them swaps one prefix -// for the other. -type PathMap struct { - Local string - Remote string -} - -// Active says the two names differ, so there is something to translate. -func (m PathMap) Active() bool { - return m.Remote != "" && m.Local != "" && path.Clean(m.Remote) != path.Clean(m.Local) -} - -// ToRemote is a Minstrel path as the client names it. -func (m PathMap) ToRemote(p string) string { return swap(p, m.Local, m.Remote) } - -// ToLocal is a client's path as Minstrel names it. -func (m PathMap) ToLocal(p string) string { return swap(p, m.Remote, m.Local) } - -// swap replaces the prefix from with to on segment boundaries -// (/downloads2 is not inside /downloads). A path outside from is unchanged. -func swap(p, from, to string) string { - if p == "" || from == "" || to == "" { - return p - } - from, to = path.Clean(from), path.Clean(to) - clean := path.Clean(p) - if clean == from { - return to - } - if from == "/" { - return path.Join(to, clean) - } - if rest, ok := strings.CutPrefix(clean, from+"/"); ok { - return path.Join(to, rest) - } - return p -} - -// Mapped wraps d so every path it is given or answers with is translated. -// File indices and in-torrent paths need no translation. -func Mapped(d Driver, m PathMap) Driver { - if !m.Active() { - return d - } - return &mapped{Driver: d, m: m} -} - -type mapped struct { - Driver - m PathMap -} - -func (c *mapped) AddTorrent(ctx context.Context, a Add) error { - a.SavePath = c.m.ToRemote(a.SavePath) - return c.Driver.AddTorrent(ctx, a) -} - -func (c *mapped) Torrents(ctx context.Context, hashes ...string) ([]Torrent, error) { - ts, err := c.Driver.Torrents(ctx, hashes...) - for i := range ts { - ts[i].SavePath = c.m.ToLocal(ts[i].SavePath) - ts[i].ContentPath = c.m.ToLocal(ts[i].ContentPath) - } - return ts, err -} - -func (c *mapped) SkipFiles(ctx context.Context, hash string, indices []int) error { - fs, ok := c.Driver.(FileSkipper) - if !ok { - return ErrUnsupported - } - return fs.SkipFiles(ctx, hash, indices) -} - -func (c *mapped) KeepFiles(ctx context.Context, hash string, indices []int) error { - fs, ok := c.Driver.(FileSkipper) - if !ok { - return ErrUnsupported - } - return fs.KeepFiles(ctx, hash, indices) -} - -func (c *mapped) Files(ctx context.Context, hash string) ([]File, error) { - fl, ok := c.Driver.(FileLister) - if !ok { - return nil, ErrUnsupported - } - return fl.Files(ctx, hash) -} - -func (c *mapped) FileStates(ctx context.Context, hash string) ([]FileState, error) { - fp, ok := c.Driver.(FileProgress) - if !ok { - return nil, ErrUnsupported - } - return fp.FileStates(ctx, hash) -} diff --git a/internal/downloader/qbittorrent.go b/internal/downloader/qbittorrent.go deleted file mode 100644 index 73d1eb6b..00000000 --- a/internal/downloader/qbittorrent.go +++ /dev/null @@ -1,367 +0,0 @@ -package downloader - -import ( - "bytes" - "context" - "encoding/json" - "errors" - "fmt" - "io" - "mime/multipart" - "net/http" - "net/http/cookiejar" - "net/url" - "strconv" - "strings" - "sync" - "time" -) - -// Client drives one qBittorrent through its WebUI API v2. -type Client struct { - Config Config - - mu sync.Mutex - // http carries the SID cookie once logged in; nil until then. - http *http.Client -} - -// AddTorrent hands qBittorrent a magnet or .torrent bytes, filed under the -// configured category with the save path already set. -func (c *Client) AddTorrent(ctx context.Context, a Add) error { - if a.Magnet == "" && len(a.Torrent) == 0 { - return fmt.Errorf("add: neither a magnet nor torrent bytes") - } - if a.SavePath == "" { - return fmt.Errorf("add: no save path, which would land the download outside the folder Minstrel imports from") - } - body := &bytes.Buffer{} - form := multipart.NewWriter(body) - fields := map[string]string{ - "savepath": a.SavePath, - "category": c.Config.Category, - // Both, because qBittorrent 5 renamed paused to stopped and an older - // one ignores the field it does not know. - "paused": strconv.FormatBool(a.Paused), - "stopped": strconv.FormatBool(a.Paused), - } - if len(a.Tags) > 0 { - fields["tags"] = strings.Join(a.Tags, ",") - } - if a.StopAtMetadata && !a.Paused { - fields["stopCondition"] = "MetadataReceived" - } - if a.Magnet != "" { - fields["urls"] = a.Magnet - } - for k, v := range fields { - if v == "" { - continue - } - if err := form.WriteField(k, v); err != nil { - return fmt.Errorf("add: build form: %w", err) - } - } - if len(a.Torrent) > 0 { - part, err := form.CreateFormFile("torrents", "release.torrent") - if err != nil { - return fmt.Errorf("add: build form: %w", err) - } - if _, err := part.Write(a.Torrent); err != nil { - return fmt.Errorf("add: build form: %w", err) - } - } - if err := form.Close(); err != nil { - return fmt.Errorf("add: build form: %w", err) - } - resp, err := c.post(ctx, "/api/v2/torrents/add", form.FormDataContentType(), body.Bytes()) - if err != nil { - return err - } - // qBittorrent refuses with HTTP 200 and the body "Fails.". - if strings.HasPrefix(strings.TrimSpace(string(resp)), "Fails") { - return fmt.Errorf("%w: the client refused the torrent", ErrUnreachable) - } - return nil -} - -// Torrents lists the configured category's torrents, or the given hashes. -func (c *Client) Torrents(ctx context.Context, hashes ...string) ([]Torrent, error) { - params := url.Values{} - if c.Config.Category != "" { - params.Set("category", c.Config.Category) - } - if len(hashes) > 0 { - params.Set("hashes", strings.Join(hashes, "|")) - } - body, err := c.get(ctx, "/api/v2/torrents/info", params) - if err != nil { - return nil, err - } - var out []Torrent - if err := json.Unmarshal(body, &out); err != nil { - return nil, fmt.Errorf("%w: parse torrent list: %v", ErrUnreachable, err) - } - for i := range out { - out[i].Hash = strings.ToLower(out[i].Hash) - } - return out, nil -} - -// Connected reads qBittorrent's own connection status. -func (c *Client) Connected(ctx context.Context) (bool, error) { - body, err := c.get(ctx, "/api/v2/transfer/info", nil) - if err != nil { - return false, err - } - var info struct { - ConnectionStatus string `json:"connection_status"` - } - if err := json.Unmarshal(body, &info); err != nil { - return false, fmt.Errorf("%w: parse transfer info: %v", ErrUnreachable, err) - } - return info.ConnectionStatus == "connected", nil -} - -// Stop and Start try qBittorrent 5's names first and fall back to 4's. -func (c *Client) Stop(ctx context.Context, hashes ...string) error { - return c.command(ctx, hashes, "/api/v2/torrents/stop", "/api/v2/torrents/pause") -} - -func (c *Client) Start(ctx context.Context, hashes ...string) error { - return c.command(ctx, hashes, "/api/v2/torrents/start", "/api/v2/torrents/resume") -} - -func (c *Client) Reannounce(ctx context.Context, hashes ...string) error { - return c.command(ctx, hashes, "/api/v2/torrents/reannounce", "/api/v2/torrents/reannounce") -} - -func (c *Client) Recheck(ctx context.Context, hashes ...string) error { - return c.command(ctx, hashes, "/api/v2/torrents/recheck", "/api/v2/torrents/recheck") -} - -func (c *Client) Delete(ctx context.Context, deleteFiles bool, hashes ...string) error { - if len(hashes) == 0 { - return nil - } - params := url.Values{ - "hashes": {strings.Join(hashes, "|")}, - "deleteFiles": {strconv.FormatBool(deleteFiles)}, - } - _, err := c.post(ctx, "/api/v2/torrents/delete", "application/x-www-form-urlencoded", []byte(params.Encode())) - return err -} - -func (c *Client) SkipFiles(ctx context.Context, hash string, indices []int) error { - return c.filePriority(ctx, hash, indices, "0") -} - -func (c *Client) KeepFiles(ctx context.Context, hash string, indices []int) error { - return c.filePriority(ctx, hash, indices, "1") -} - -func (c *Client) filePriority(ctx context.Context, hash string, indices []int, priority string) error { - if len(indices) == 0 { - return nil - } - ids := make([]string, len(indices)) - for i, n := range indices { - ids[i] = strconv.Itoa(n) - } - params := url.Values{"hash": {hash}, "id": {strings.Join(ids, "|")}, "priority": {priority}} - _, err := c.post(ctx, "/api/v2/torrents/filePrio", "application/x-www-form-urlencoded", []byte(params.Encode())) - return err -} - -func (c *Client) Files(ctx context.Context, hash string) ([]File, error) { - raw, err := c.files(ctx, hash) - if err != nil { - return nil, err - } - out := make([]File, len(raw)) - for i, f := range raw { - out[i] = File{Index: f.Index, Path: f.Name, Size: f.Size} - } - return out, nil -} - -func (c *Client) FileStates(ctx context.Context, hash string) ([]FileState, error) { - raw, err := c.files(ctx, hash) - if err != nil { - return nil, err - } - out := make([]FileState, len(raw)) - for i, f := range raw { - done := f.Size - if f.Progress < 1 { - // Progress is a float; never round an unfinished file up to whole. - done = min(int64(f.Progress*float64(f.Size)), f.Size-1) - } - out[i] = FileState{Path: f.Name, Size: f.Size, Done: max(done, 0), Skipped: f.Priority == 0} - } - return out, nil -} - -type qbitFile struct { - Index int `json:"index"` - Name string `json:"name"` - Size int64 `json:"size"` - Progress float64 `json:"progress"` - Priority int `json:"priority"` -} - -func (c *Client) files(ctx context.Context, hash string) ([]qbitFile, error) { - body, err := c.get(ctx, "/api/v2/torrents/files", url.Values{"hash": {hash}}) - if err != nil { - return nil, err - } - var raw []qbitFile - if err := json.Unmarshal(body, &raw); err != nil { - return nil, fmt.Errorf("%w: parse file list: %v", ErrUnreachable, err) - } - return raw, nil -} - -func (c *Client) command(ctx context.Context, hashes []string, primary, fallback string) error { - if len(hashes) == 0 { - return nil - } - params := []byte(url.Values{"hashes": {strings.Join(hashes, "|")}}.Encode()) - _, err := c.post(ctx, primary, "application/x-www-form-urlencoded", params) - var status statusError - if errors.As(err, &status) && (status.code == http.StatusNotFound || status.code == http.StatusMethodNotAllowed) { - _, err = c.post(ctx, fallback, "application/x-www-form-urlencoded", params) - } - return err -} - -type statusError struct { - code int - what string -} - -func (e statusError) Error() string { return fmt.Sprintf("%s: HTTP %d", e.what, e.code) } - -func (e statusError) Unwrap() error { return ErrUnreachable } - -func (c *Client) get(ctx context.Context, path string, params url.Values) ([]byte, error) { - endpoint := path - if len(params) > 0 { - endpoint += "?" + params.Encode() - } - return c.do(ctx, http.MethodGet, endpoint, "", nil) -} - -func (c *Client) post(ctx context.Context, path, contentType string, body []byte) ([]byte, error) { - return c.do(ctx, http.MethodPost, path, contentType, body) -} - -// do logs in when there is no session, and once more on a 403, which is how -// qBittorrent says a session expired. -func (c *Client) do(ctx context.Context, method, path, contentType string, body []byte) ([]byte, error) { - if c.Config.BaseURL == "" { - return nil, ErrUnconfigured - } - if err := c.ensureSession(ctx, false); err != nil { - return nil, err - } - out, err := c.call(ctx, method, path, contentType, body) - var status statusError - if errors.As(err, &status) && status.code == http.StatusForbidden { - if err := c.ensureSession(ctx, true); err != nil { - return nil, err - } - return c.call(ctx, method, path, contentType, body) - } - return out, err -} - -func (c *Client) ensureSession(ctx context.Context, force bool) error { - c.mu.Lock() - defer c.mu.Unlock() - if c.http != nil && !force { - return nil - } - jar, err := cookiejar.New(nil) - if err != nil { - return fmt.Errorf("cookie jar: %w", err) - } - httpc := &http.Client{Jar: jar} - params := url.Values{"username": {c.Config.Username}, "password": {c.Config.Password}} - out, err := c.callWith(ctx, httpc, http.MethodPost, "/api/v2/auth/login", - "application/x-www-form-urlencoded", []byte(params.Encode())) - if err != nil { - var status statusError - if errors.As(err, &status) && status.code == http.StatusForbidden { - return fmt.Errorf("%w: too many failed attempts, or this address is banned", ErrAuth) - } - return err - } - if strings.HasPrefix(strings.TrimSpace(string(out)), "Fails") { - return fmt.Errorf("%w: wrong username or password", ErrAuth) - } - c.http = httpc - return nil -} - -func (c *Client) call(ctx context.Context, method, path, contentType string, body []byte) ([]byte, error) { - c.mu.Lock() - httpc := c.http - c.mu.Unlock() - if httpc == nil { - httpc = &http.Client{} - } - return c.callWith(ctx, httpc, method, path, contentType, body) -} - -func (c *Client) callWith(ctx context.Context, httpc *http.Client, method, path, contentType string, body []byte) ([]byte, error) { - base := strings.TrimRight(c.Config.BaseURL, "/") - status, out, err := send(ctx, httpc, c.Config.Timeout, method, base+path, contentType, body, func(h http.Header) { - // qBittorrent's CSRF check refuses a request without a matching - // Referer. - h.Set("Referer", base) - }) - if err != nil { - return nil, err - } - if status < 200 || status >= 300 { - return nil, statusError{code: status, what: path} - } - return out, nil -} - -// send makes one request to a download client under its timeout, lets the -// caller add headers, and reads at most 16 MB. It returns the status for the -// caller to read, because each client's refusals are its own. -func send(ctx context.Context, httpc *http.Client, timeout time.Duration, method, target, contentType string, body []byte, set func(http.Header)) (int, []byte, error) { - if timeout <= 0 { - timeout = DefaultTimeout - } - ctx, cancel := context.WithTimeout(ctx, timeout) - defer cancel() - var reader io.Reader - if body != nil { - reader = bytes.NewReader(body) - } - req, err := http.NewRequestWithContext(ctx, method, target, reader) - if err != nil { - return 0, nil, fmt.Errorf("%w: build request: %v", ErrUnreachable, err) - } - req.Header.Set("User-Agent", UserAgent) - if contentType != "" { - req.Header.Set("Content-Type", contentType) - } - if set != nil { - set(req.Header) - } - resp, err := httpc.Do(req) - if err != nil { - return 0, nil, fmt.Errorf("%w: %v", ErrUnreachable, err) - } - defer func() { _ = resp.Body.Close() }() - out, err := io.ReadAll(io.LimitReader(resp.Body, 16<<20)) - if err != nil { - return 0, nil, fmt.Errorf("%w: read body: %v", ErrUnreachable, err) - } - return resp.StatusCode, out, nil -} diff --git a/internal/eventbus/eventbus.go b/internal/eventbus/eventbus.go index d9b42eda..2b9b01a8 100644 --- a/internal/eventbus/eventbus.go +++ b/internal/eventbus/eventbus.go @@ -24,7 +24,7 @@ import ( ) // Event is a single broadcast message. Kind names follow a "domain.action" -// convention (e.g. "track.liked", "request.status_changed"). UserID, when +// convention (e.g. "track.liked", "playlist.system_rebuilt"). UserID, when // non-empty, scopes the event to a specific user — subscribers receive // only events with UserID == their auth user OR with empty UserID (which // means "broadcast to everyone subscribed"). diff --git a/internal/exchange/exchange.go b/internal/exchange/exchange.go deleted file mode 100644 index 987c90bc..00000000 --- a/internal/exchange/exchange.go +++ /dev/null @@ -1,181 +0,0 @@ -// Package exchange talks to FabledExchange's native API for the two jobs that -// are not downloading: searching its DHT index (/api/search) and describing -// one torrent (/api/torrents/{infohash}). Downloading is internal/downloader's. -// -// The native search rather than Exchange's Torznab feed, because it reports -// whether a swarm was measured and how many files a torrent holds, which the -// feed cannot say. -package exchange - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "io" - "log/slog" - "net/http" - "strings" - "time" - - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -// ErrUnconfigured is an Exchange with no address. -var ErrUnconfigured = errors.New("exchange is not configured") - -// ErrUnreachable is a call that could not be made or was not understood. -var ErrUnreachable = errors.New("exchange unreachable") - -// ErrAuth is an API key Exchange refused, or a secured Exchange asked with -// none. -var ErrAuth = errors.New("exchange refused the API key") - -// ErrUnknown is a torrent Exchange has not crawled: a JSON 404, which is an -// answer and not a fault. -var ErrUnknown = errors.New("exchange has not crawled this torrent") - -// UserAgent names Minstrel in Exchange's logs. -const UserAgent = "Minstrel (self-hosted music server; release search)" - -// DefaultTimeout is what an unset Timeout means. Exchange answers from its -// own database. -const DefaultTimeout = 20 * time.Second - -// Config is one Exchange. -type Config struct { - BaseURL string - // APIKey is optional and sent only when set: an empty bearer makes a - // secured Exchange answer 401. - APIKey string - Timeout time.Duration -} - -// Client asks one Exchange. -type Client struct { - Config Config - // HTTP is injectable for tests. Nil takes http.DefaultClient. - HTTP *http.Client - // Code is the acquisition_services row, stamped onto search results. - Code string - Logger *slog.Logger -} - -func (c *Client) log() *slog.Logger { - if c == nil || c.Logger == nil { - return slog.Default() - } - return c.Logger -} - -// Detail is what Exchange knows about one torrent. -type Detail struct { - InfoHash string `json:"infohash"` - Name string `json:"name"` - Size int64 `json:"size"` - FilesCount int `json:"files_count"` - Seeders int `json:"seeders"` - SwarmKnown bool `json:"swarm_known"` - Leechers int `json:"leechers"` - LeechersKnown bool `json:"leechers_known"` - ContentType string `json:"content_type"` - Files []DetailFile `json:"files"` -} - -// DetailFile is one file inside a torrent. -type DetailFile struct { - Index int `json:"index"` - Path string `json:"path"` - Size int64 `json:"size"` -} - -// Torrent asks Exchange about one v1 infohash. -func (c *Client) Torrent(ctx context.Context, infohash string) (Detail, error) { - if strings.TrimSpace(c.Config.BaseURL) == "" { - return Detail{}, ErrUnconfigured - } - hash := indexer.NormalizeHash(infohash) - if len(hash) != 40 || !indexer.IsInfoHash(hash) { - return Detail{}, fmt.Errorf("%w: %q is not a 40-character infohash", ErrUnreachable, infohash) - } - body, err := c.get(ctx, "/api/torrents/"+hash) - if err != nil { - return Detail{}, err - } - var out Detail - if err := json.Unmarshal(body, &out); err != nil { - return Detail{}, fmt.Errorf("%w: parse torrent detail: %v", ErrUnreachable, err) - } - if out.InfoHash == "" { - return Detail{}, ErrUnknown - } - return out, nil -} - -func (c *Client) get(ctx context.Context, path string) ([]byte, error) { - timeout := c.Config.Timeout - if timeout <= 0 { - timeout = DefaultTimeout - } - ctx, cancel := context.WithTimeout(ctx, timeout) - defer cancel() - - target := strings.TrimRight(c.Config.BaseURL, "/") + path - req, err := http.NewRequestWithContext(ctx, http.MethodGet, target, nil) - if err != nil { - return nil, fmt.Errorf("%w: %v", ErrUnreachable, err) - } - req.Header.Set("User-Agent", UserAgent) - req.Header.Set("Accept", "application/json") - if key := strings.TrimSpace(c.Config.APIKey); key != "" { - req.Header.Set("Authorization", "Bearer "+key) - } - client := c.HTTP - if client == nil { - client = http.DefaultClient - } - res, err := client.Do(req) - if err != nil { - return nil, fmt.Errorf("%w: %v", ErrUnreachable, err) - } - defer func() { _ = res.Body.Close() }() - body, err := io.ReadAll(io.LimitReader(res.Body, 1<<20)) - if err != nil { - return nil, fmt.Errorf("%w: read answer: %v", ErrUnreachable, err) - } - switch { - case res.StatusCode == http.StatusNotFound: - // A 404 that is not JSON came from something else at this address, - // which must not look like a healthy Exchange that lacks a torrent. - if !looksLikeJSON(body) { - return nil, fmt.Errorf("%w: something at this address answered 404, "+ - "but it was not FabledExchange", ErrUnreachable) - } - return nil, ErrUnknown - case res.StatusCode == http.StatusUnauthorized || res.StatusCode == http.StatusForbidden: - return nil, fmt.Errorf("%w: %s", ErrAuth, - message(body, "set this FabledExchange's API key in Minstrel's settings")) - case res.StatusCode == http.StatusServiceUnavailable: - return nil, fmt.Errorf("%w: %s", ErrUnreachable, message(body, "no index is configured")) - case res.StatusCode < 200 || res.StatusCode >= 300: - return nil, fmt.Errorf("%w: %s", ErrUnreachable, - message(body, fmt.Sprintf("returned %d", res.StatusCode))) - } - return body, nil -} - -// message is Exchange's own {"error": "..."} text, or fallback. -func message(body []byte, fallback string) string { - var env struct { - Error string `json:"error"` - } - if err := json.Unmarshal(body, &env); err == nil && strings.TrimSpace(env.Error) != "" { - return strings.TrimSpace(env.Error) - } - return fallback -} - -func looksLikeJSON(body []byte) bool { - t := strings.TrimSpace(string(body)) - return strings.HasPrefix(t, "{") || strings.HasPrefix(t, "[") -} diff --git a/internal/exchange/exchange_test.go b/internal/exchange/exchange_test.go deleted file mode 100644 index bfd5aa92..00000000 --- a/internal/exchange/exchange_test.go +++ /dev/null @@ -1,108 +0,0 @@ -package exchange - -import ( - "context" - "errors" - "fmt" - "net/http" - "net/http/httptest" - "strings" - "testing" - - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -func TestTorrent_SendsTheKeyOnlyWhenSet(t *testing.T) { - auth := make(chan string, 2) - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - auth <- r.Header.Get("Authorization") - _, _ = w.Write([]byte(`{"infohash": "` + strings.Repeat("a", 40) + `", "files_count": 12}`)) - })) - defer srv.Close() - - for _, key := range []string{"", "k"} { - c := &Client{Config: Config{BaseURL: srv.URL, APIKey: key}} - d, err := c.Torrent(context.Background(), strings.Repeat("A", 40)) - if err != nil || d.FilesCount != 12 { - t.Fatalf("key %q: detail %+v err %v", key, d, err) - } - } - if without, with := <-auth, <-auth; without != "" || with != "Bearer k" { - t.Errorf("Authorization = %q then %q, want none without a key and a bearer with one", without, with) - } -} - -func TestTorrent_TellsAnUncrawledTorrentFromAnotherService(t *testing.T) { - for name, tc := range map[string]struct { - status int - body string - want error - }{ - "Exchange has not crawled it": {http.StatusNotFound, `{"error": "unknown torrent"}`, ErrUnknown}, - "not FabledExchange": {http.StatusNotFound, `404`, ErrUnreachable}, - "a refused key": {http.StatusUnauthorized, `{"error": "bad key"}`, ErrAuth}, - } { - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.WriteHeader(tc.status) - _, _ = w.Write([]byte(tc.body)) - })) - _, err := (&Client{Config: Config{BaseURL: srv.URL}}).Torrent(context.Background(), strings.Repeat("0", 40)) - srv.Close() - if !errors.Is(err, tc.want) { - t.Errorf("%s: err = %v, want %v", name, err, tc.want) - } - } -} - -func TestSearch_PagesAndKeepsMeasuredSwarmsOnly(t *testing.T) { - queries := make(chan string, 4) - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - queries <- r.URL.RawQuery - offset := r.URL.Query().Get("offset") - var rows []string - if offset == "0" { - for i := 0; i < SearchLimit; i++ { - rows = append(rows, fmt.Sprintf(`{"infohash": "%040x", "name": "r%d", "content_type": "music", "seeders": 5, "swarm_known": %t}`, i, i, i == 0)) - } - } else { - // A repeat of page one's first hash, which must not be listed twice. - rows = append(rows, fmt.Sprintf(`{"infohash": "%040x", "name": "again"}`, 0)) - } - _, _ = fmt.Fprintf(w, `{"total": %d, "results": [%s]}`, SearchLimit+1, strings.Join(rows, ",")) - })) - defer srv.Close() - - c := &Client{Code: "exchange", Config: Config{BaseURL: srv.URL}} - results, err := c.Search(context.Background(), indexer.Query{Text: "x", Categories: indexer.MusicCategories}) - if err != nil { - t.Fatalf("search: %v", err) - } - close(queries) - var asked []string - for q := range queries { - asked = append(asked, q) - } - if len(asked) != 2 || !strings.Contains(asked[0], "type=music") { - t.Errorf("queries = %v, want two pages asking for music", asked) - } - if len(results) != SearchLimit { - t.Errorf("results = %d, want %d (the repeat dropped)", len(results), SearchLimit) - } - if r := results[0]; !r.SwarmKnown || r.Seeders != 5 || r.Category != 3000 || r.Source != "exchange" || !strings.HasPrefix(r.Magnet, "magnet:") { - t.Errorf("first = %+v", r) - } - if r := results[1]; r.SwarmKnown || r.Seeders != 0 { - t.Errorf("an unmeasured swarm reported seeders: %+v", r) - } -} - -func TestSearch_ARefusedKeyReadsAsAuth(t *testing.T) { - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.WriteHeader(http.StatusForbidden) - })) - defer srv.Close() - _, err := (&Client{Config: Config{BaseURL: srv.URL}}).Search(context.Background(), indexer.Query{Text: "x"}) - if !errors.Is(err, indexer.ErrAuth) { - t.Fatalf("err = %v, want indexer.ErrAuth", err) - } -} diff --git a/internal/exchange/search.go b/internal/exchange/search.go deleted file mode 100644 index d2fc8724..00000000 --- a/internal/exchange/search.go +++ /dev/null @@ -1,169 +0,0 @@ -package exchange - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "net/url" - "strings" - - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -// SearchLimit is one page of /api/search; SearchDepth is the most read across -// pages. A query matching more than SearchDepth is too broad to judge anyway. -const ( - SearchLimit = 100 - SearchDepth = 300 -) - -type searchResult struct { - InfoHash string `json:"infohash"` - Name string `json:"name"` - Size int64 `json:"size"` - FilesCount int `json:"files_count"` - ContentType string `json:"content_type"` - Seeders int `json:"seeders"` - SwarmKnown bool `json:"swarm_known"` - Leechers int `json:"leechers"` - LeechersKnown bool `json:"leechers_known"` -} - -type searchPage struct { - Total int `json:"total"` - Results []searchResult `json:"results"` -} - -// Search asks Exchange's DHT index. Every result is a magnet, so there is no -// .torrent to fetch through it. A later page that fails keeps the earlier -// ones; a first page that fails is the search failing. -func (c *Client) Search(ctx context.Context, q indexer.Query) ([]indexer.Result, error) { - if strings.TrimSpace(c.Config.BaseURL) == "" { - return nil, indexer.ErrUnconfigured - } - params := url.Values{"q": {q.Text}, "limit": {fmt.Sprint(SearchLimit)}} - if t := contentType(q.Categories); t != "" { - params.Set("type", t) - } - var ( - out []indexer.Result - seen = map[string]bool{} - ) - for offset := 0; offset < SearchDepth; offset += SearchLimit { - params.Set("offset", fmt.Sprint(offset)) - page, err := c.searchPage(ctx, params) - if err != nil { - if offset == 0 { - return nil, err - } - c.log().Warn("exchange: a later page of a search could not be read; keeping the pages before it", - "q", q.Text, "offset", offset, "err", err) - break - } - for _, r := range page.Results { - res, ok := c.result(r) - if !ok || seen[res.InfoHash] { - continue - } - seen[res.InfoHash] = true - out = append(out, res) - } - if len(page.Results) < SearchLimit || offset+SearchLimit >= page.Total { - break - } - } - return out, nil -} - -func (c *Client) searchPage(ctx context.Context, params url.Values) (searchPage, error) { - var page searchPage - body, err := c.get(ctx, "/api/search?"+params.Encode()) - if err != nil { - return page, searchError(err) - } - if err := json.Unmarshal(body, &page); err != nil { - return page, fmt.Errorf("%w: parse search results: %v", indexer.ErrUnreachable, err) - } - return page, nil -} - -func (c *Client) result(r searchResult) (indexer.Result, bool) { - hash := indexer.NormalizeHash(r.InfoHash) - if hash == "" || strings.TrimSpace(r.Name) == "" { - return indexer.Result{}, false - } - res := indexer.Result{ - Title: r.Name, - InfoHash: hash, - Magnet: magnet(hash, r.Name), - SizeBytes: r.Size, - Category: category(r.ContentType), - Source: c.Code, - FilesCount: r.FilesCount, - SwarmKnown: r.SwarmKnown, - } - // Seeders only when measured: an unmeasured count is not health. - if r.SwarmKnown { - res.Seeders, res.Peers = r.Seeders, r.Seeders - if r.LeechersKnown { - res.Peers = r.Seeders + r.Leechers - } - } - return res, true -} - -// searchError puts Exchange's errors in the vocabulary every search source -// shares, so a refused key reads the same whichever source refused it. -func searchError(err error) error { - switch { - case errors.Is(err, ErrAuth): - return fmt.Errorf("%w: %v", indexer.ErrAuth, err) - case errors.Is(err, ErrUnknown): - return fmt.Errorf("%w: this FabledExchange has no search API", indexer.ErrUnreachable) - } - return fmt.Errorf("%w: %v", indexer.ErrUnreachable, err) -} - -// contentType narrows Exchange's search to music when the query asks only -// for music categories (3000s, not 3030 audiobooks). -func contentType(cats []int) string { - music := false - for _, c := range cats { - if c < 3000 || c >= 4000 || c == 3030 { - return "" - } - music = true - } - if music { - return "music" - } - return "" -} - -func category(contentType string) int { - switch contentType { - case "music": - return 3000 - case "audiobook": - return 3030 - case "ebook": - return 7020 - case "comic": - return 7030 - case "movie": - return 2000 - case "tv": - return 5000 - } - return 8000 -} - -// magnet is a trackerless magnet for a hash Exchange found on the DHT. -func magnet(hash, name string) string { - m := "magnet:?xt=urn:btih:" + hash - if name != "" { - m += "&dn=" + url.QueryEscape(name) - } - return m -} diff --git a/internal/gc/worker.go b/internal/gc/worker.go index c88ad965..5a0ea9d8 100644 --- a/internal/gc/worker.go +++ b/internal/gc/worker.go @@ -16,9 +16,6 @@ // - GcDeleteExpiredPasswordResets // - GcDeleteExpiredSessions (idle 30d / absolute 1y) // - GcPruneDiagnostics (30d retention) -// - GcDeleteExpiredSuggestionSnoozes (snoozes expire, then go) -// - GcDeleteOrphanedCandidateArtistTags(+State) (the similarity feed -// churns, so cached candidate tags outlive their candidates) package gc import ( @@ -87,14 +84,6 @@ func (w *Worker) tickOnce(ctx context.Context) { w.runSweep(ctx, "delete_expired_password_resets", q.GcDeleteExpiredPasswordResets) w.runSweep(ctx, "delete_expired_sessions", q.GcDeleteExpiredSessions) w.runSweep(ctx, "prune_diagnostics", q.GcPruneDiagnostics) - w.runSweep(ctx, "delete_expired_suggestion_snoozes", q.GcDeleteExpiredSuggestionSnoozes) - // Tags before state: if the process dies between the two, a candidate left - // with a state row and no tags simply reads as "settled, nothing found", - // which is already a valid state. The reverse order could leave tags with - // no state row, which the drainer would treat as never-processed and - // re-fetch on top of rows that are already there. - w.runSweep(ctx, "orphaned_candidate_artist_tags", q.GcDeleteOrphanedCandidateArtistTags) - w.runSweep(ctx, "orphaned_candidate_artist_tag_state", q.GcDeleteOrphanedCandidateArtistTagState) } // runSweep is a small adapter so each sweep call site is a one-liner diff --git a/internal/importer/importer.go b/internal/importer/importer.go deleted file mode 100644 index 024c96c4..00000000 --- a/internal/importer/importer.go +++ /dev/null @@ -1,610 +0,0 @@ -package importer - -import ( - "context" - "encoding/json" - "errors" - "fmt" - "log/slog" - "os" - "path/filepath" - "slices" - "strconv" - "sync" - "time" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -// Import states, as grabs.import_state holds them. -const ( - StateDownloading = "downloading" - StateWaiting = "waiting" - StateImporting = "importing" - StateImported = "imported" - StateRefused = "refused" - StateReview = "review" - StateHeld = "held" - StateFailed = "failed" -) - -const ( - // interval: a finished download is noticed within a minute. One - // Torrents call per download client, so the client barely notices. - interval = time.Minute - // passTimeout bounds a pass: fingerprinting a long album against - // AcoustID's pace takes a minute or two, so half an hour is a hang. - passTimeout = 30 * time.Minute - // staleMinutes: an import still "importing" after this was cut short - // (a restart mid-copy) and is tried again. - staleMinutes = 30 -) - -// Scanner reads new files into the library. -type Scanner interface { - ScanFiles(ctx context.Context, paths []string) ([]pgtype.UUID, error) -} - -// Catalogue lists a release group's editions. -type Catalogue interface { - Releases(ctx context.Context, groupMBID string) (catalogue.Editions, error) -} - -// Importer watches every live grab's download and imports it once finished. -type Importer struct { - pool *pgxpool.Pool - Scanner Scanner - Catalogue Catalogue - Printer Printer - // Roots are the library's folders (the scan paths), read on every use - // because they change in Settings. New music is filed under the - // one the settings name, or the first. - Roots func() []string - // DataDir is where cached art lives, for tidying an artist an upgrade - // leaves with nothing. Empty skips it. - DataDir string - // ClientFor builds the download client a grab went to. Defaults to - // acquire.DownloadClientFor. - ClientFor func(ctx context.Context, code string) (downloader.Driver, error) - logger *slog.Logger - wake chan struct{} - mu sync.Mutex // one pass at a time -} - -// New returns an Importer. printer may be nil: the check then rests on -// lengths and tags. -func New(pool *pgxpool.Pool, scanner Scanner, cat Catalogue, printer Printer, roots func() []string, logger *slog.Logger) *Importer { - if logger == nil { - logger = slog.Default() - } - im := &Importer{pool: pool, Scanner: scanner, Catalogue: cat, Printer: printer, Roots: roots, - logger: logger, wake: make(chan struct{}, 1)} - im.ClientFor = func(ctx context.Context, code string) (downloader.Driver, error) { - return acquire.DownloadClientFor(ctx, pool, code) - } - return im -} - -// Wake asks for a pass now: after a grab, or a person's decision. -func (im *Importer) Wake() { - select { - case im.wake <- struct{}{}: - default: - } -} - -// Run passes every minute, and when woken, until ctx ends. -func (im *Importer) Run(ctx context.Context) { - t := time.NewTicker(interval) - defer t.Stop() - for { - select { - case <-ctx.Done(): - return - case <-t.C: - case <-im.wake: - } - rep := im.Pass(ctx) - if rep.Imported+rep.Refused+rep.Review+rep.Held+rep.Failed > 0 || rep.Error != "" { - im.logger.Info("import: pass", "imported", rep.Imported, "refused", rep.Refused, - "review", rep.Review, "held", rep.Held, "failed", rep.Failed, - "downloading", rep.Downloading, "error", rep.Error) - } - } -} - -// Report is one pass. -type Report struct { - Downloading, Imported, Refused, Review, Held, Failed, Waiting int - Error string -} - -// Pass looks at every live grab once. -func (im *Importer) Pass(ctx context.Context) Report { - im.mu.Lock() - defer im.mu.Unlock() - ctx, cancel := context.WithTimeout(ctx, passTimeout) - defer cancel() - var rep Report - q := dbq.New(im.pool) - if _, err := q.ReclaimStaleImports(ctx, staleMinutes); err != nil { - rep.Error = err.Error() - return rep - } - grabs, err := q.ListLiveGrabs(ctx) - if err != nil { - rep.Error = err.Error() - return rep - } - byClient := map[string][]dbq.ListLiveGrabsRow{} - var order []string - for _, g := range grabs { - if _, ok := byClient[g.Client]; !ok { - order = append(order, g.Client) - } - byClient[g.Client] = append(byClient[g.Client], g) - } - for _, code := range order { - im.passClient(ctx, q, code, byClient[code], &rep) - } - return rep -} - -func (im *Importer) passClient(ctx context.Context, q *dbq.Queries, code string, gs []dbq.ListLiveGrabsRow, rep *Report) { - wait := func(g dbq.ListLiveGrabsRow, why string) { - rep.Waiting++ - _ = q.SetGrabProgress(ctx, dbq.SetGrabProgressParams{ - ID: g.ID, Progress: g.Progress, ImportState: StateWaiting, ImportReason: why}) - } - d, err := im.ClientFor(ctx, code) - if err != nil { - for _, g := range gs { - wait(g, fmt.Sprintf("Can't reach its download client: %v.", err)) - } - return - } - hashes := make([]string, 0, len(gs)) - for _, g := range gs { - hashes = append(hashes, g.InfoHash) - } - ts, err := d.Torrents(ctx, hashes...) - if err != nil { - for _, g := range gs { - wait(g, fmt.Sprintf("Can't reach its download client %s: %s", code, indexer.FirstLine(err.Error()))) - } - return - } - byHash := map[string]downloader.Torrent{} - for _, t := range ts { - byHash[indexer.NormalizeHash(t.Hash)] = t - } - for _, g := range gs { - if ctx.Err() != nil { - return - } - switch g.ImportState { - case StateReview, StateFailed, StateImporting: - continue - } - t, ok := byHash[indexer.NormalizeHash(g.InfoHash)] - if !ok { - continue // gone from the client: the lifecycle settles it - } - if !g.WantID.Valid { - wait(g, "Its album is no longer wanted.") - continue - } - if !finished(ctx, d, t) { - rep.Downloading++ - _ = q.SetGrabProgress(ctx, dbq.SetGrabProgressParams{ - ID: g.ID, Progress: float32(min(t.Progress, 0.999)), ImportState: StateDownloading}) - continue - } - claimed, err := q.ClaimGrabImport(ctx, g.ID) - if err != nil { - continue // claimed by a pass racing this one, or ended - } - switch out := im.importOne(ctx, q, claimed, d, t); out { - case StateImported: - rep.Imported++ - case StateRefused: - rep.Refused++ - case StateReview: - rep.Review++ - case StateHeld: - rep.Held++ - case StateFailed: - rep.Failed++ - case StateWaiting: - rep.Waiting++ - } - } -} - -// upStates are qBittorrent's states for a torrent that has every byte and -// is seeding, paused or queued to seed. Exchange's states arrive in the same -// vocabulary. -var upStates = map[string]bool{ - "uploading": true, "stalledUP": true, "pausedUP": true, "stoppedUP": true, - "queuedUP": true, "forcedUP": true, "checkingUP": true, -} - -// finished: whole, in a seeding state, and no wanted file short of bytes. -func finished(ctx context.Context, d downloader.Driver, t downloader.Torrent) bool { - if !t.Done() || !upStates[t.State] { - return false - } - if fp, ok := d.(downloader.FileProgress); ok { - if states, err := fp.FileStates(ctx, t.Hash); err == nil { - for _, f := range states { - if f.Unfinished() { - return false - } - } - } - } - return true -} - -// Detail is what grabs.import_detail keeps: the verdict and what it rested -// on, the edition chosen, and what happened to the files. -type Detail struct { - Verdict Verdict `json:"verdict"` - Edition string `json:"edition,omitempty"` - EditionMBID string `json:"edition_mbid,omitempty"` - Files []string `json:"files,omitempty"` - Placed int `json:"placed,omitempty"` - Skipped int `json:"skipped,omitempty"` - Untagged int `json:"untagged,omitempty"` - // RehomedFrom: the album it was grabbed for, when it turned out to be - // another wanted album. - RehomedFrom string `json:"rehomed_from,omitempty"` -} - -// importOne checks and files one finished download and records the outcome. -func (im *Importer) importOne(ctx context.Context, q *dbq.Queries, g dbq.Grab, d downloader.Driver, t downloader.Torrent) string { - record := func(state, reason, end string, det Detail, files int, dest string) string { - body, _ := json.Marshal(det) - // The outcome is written even when the pass's context is ending: - // a claimed grab left "importing" waits half an hour to be retried. - wctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 15*time.Second) - defer cancel() - if _, err := q.RecordImport(wctx, dbq.RecordImportParams{ - ID: g.ID, ImportState: state, ImportReason: reason, ImportDetail: body, - ImportedFiles: int32(files), Destination: dest, EndReason: end, - }); err != nil { - im.logger.Warn("import: record outcome", "grab", g.Name, "state", state, "err", err) - } - im.logger.Info("import: "+state, "grab", g.Name, "reason", reason) - return state - } - - w, err := im.want(ctx, q, g.WantID) - if err != nil { - return record(StateWaiting, "Its album could not be read: "+err.Error(), "", Detail{}, 0, "") - } - editions, err := im.editions(ctx, w.GroupMBID) - if err != nil { - return record(StateWaiting, "MusicBrainz could not be reached to check it against the album; trying again shortly.", - "", Detail{}, 0, "") - } - src := t.ContentPath - if src == "" { - src = filepath.Join(g.SavePath, t.Name) - } - files, printed, err := inspect(ctx, src, im.Printer) - if err != nil { - return record(StateFailed, fmt.Sprintf("Couldn't read the download at %s: %v", src, err), "", Detail{}, 0, "") - } - v := Verify(w, editions, files, printed, g.ImportForced) - det := Detail{Verdict: v} - for _, f := range files { - det.Files = append(det.Files, f.Rel) - } - if v.Edition >= 0 { - det.Edition, det.EditionMBID = describe(editions[v.Edition]), editions[v.Edition].MBID - } - - switch v.Outcome { - case Review: - return record(StateReview, v.Reason, "", det, 0, "") - case Refuse: - if v.Code == CodeWrongAlbum { - if out, ok := im.rehome(ctx, q, g, d, w, files, printed, record); ok { - return out - } - } - im.refuse(ctx, q, g, d, w, v.Reason, pgtype.UUID{}) - return record(StateRefused, v.Reason, StateRefused, det, 0, "") - } - return im.place(ctx, q, g, d, w, editions, files, v, det, record) -} - -type recordFunc func(state, reason, end string, det Detail, files int, dest string) string - -// place files an accepted download and records it imported, or held when -// the library already had every track. -func (im *Importer) place(ctx context.Context, q *dbq.Queries, g dbq.Grab, d downloader.Driver, w Want, - editions []Edition, files []File, v Verdict, det Detail, record recordFunc) string { - root, err := im.root(ctx, q) - if err != nil { - return record(StateFailed, "Not imported: "+err.Error()+".", "", det, 0, "") - } - var e *Edition - if v.Edition >= 0 { - e = &editions[v.Edition] - } - plan := Plan(root, w, e, files, v.Pairs) - if w.Upgrade != "" { - return im.placeUpgrade(ctx, q, g, d, w, e, files, plan, v, det, record) - } - heldRec, heldPos, err := im.held(ctx, q, w, plan) - if err != nil { - return record(StateWaiting, "Couldn't read what the library holds: "+err.Error(), "", det, 0, "") - } - - var placed []string - for _, pl := range plan { - if (pl.Recording != "" && heldRec[pl.Recording]) || heldPos[[2]int{pl.Disc, pl.Track}] { - det.Skipped++ - continue - } - tags := Tags{Title: pl.Title, Artist: w.Artist, Album: w.Title, AlbumArtist: w.Artist, Year: w.Year, - Disc: pl.Disc, Discs: pl.Discs, Track: pl.Track, Tracks: pl.Tracks, - GroupMBID: w.GroupMBID, RecordingMBID: pl.Recording, ArtistMBID: w.ArtistMBID} - if e != nil { - tags.ReleaseMBID = e.MBID - } - tagged, err := Place(ctx, files[pl.File].Path, pl.Dest, tags) - if errors.Is(err, ErrOccupied) { - det.Skipped++ - continue - } - if err != nil { - for _, p := range placed { - _ = os.Remove(p) - } - return record(StateFailed, fmt.Sprintf("Copying into the library failed, and nothing was left behind: %v", err), - "", det, 0, "") - } - placed = append(placed, pl.Dest) - if !tagged { - det.Untagged++ - } - } - det.Placed = len(placed) - if len(placed) == 0 { - im.removeFromClient(ctx, d, g) - return record(StateHeld, fmt.Sprintf("The library already holds every track of %s, so nothing was imported.", w.Title), - StateHeld, det, 0, "") - } - - if _, err := im.Scanner.ScanFiles(ctx, placed); err != nil { - // The watcher and the next scan find the files anyway. - im.logger.Warn("import: scan the new files", "grab", g.Name, "err", err) - } - if err := q.MarkWantSearched(ctx, g.WantID); err != nil { - im.logger.Warn("import: mark the want searched", "grab", g.Name, "err", err) - } - dest := filepath.Dir(placed[0]) - if e != nil && e.Media() > 1 { - dest = filepath.Dir(dest) - } - reason := fmt.Sprintf("Imported %s into %s. %s", tracks(len(placed)), dest, v.Reason) - if det.Skipped > 0 { - reason += fmt.Sprintf(" %s already in the library left as they were.", tracks(det.Skipped)) - } - if det.Untagged > 0 { - reason += fmt.Sprintf(" %s copied with their own tags: ffmpeg could not rewrite them.", tracks(det.Untagged)) - } - return record(StateImported, reason, StateImported, det, len(placed), dest) -} - -// rehome imports a download that turned out to be another wanted album, -// named by its tags, for that album, when its own grab is not running. -func (im *Importer) rehome(ctx context.Context, q *dbq.Queries, g dbq.Grab, d downloader.Driver, - from Want, files []File, printed bool, record recordFunc) (string, bool) { - group := majority(files, func(f File) string { return f.GroupMBID }) - if group == "" { - return "", false - } - others, err := q.OpenWantsForGroups(ctx, []string{group}) - if err != nil || len(others) == 0 { - return "", false - } - other := others[0] - w, err := im.want(ctx, q, other.ID) - if err != nil { - return "", false - } - editions, err := im.editions(ctx, w.GroupMBID) - if err != nil { - return "", false - } - v := Verify(w, editions, files, printed, false) - if v.Outcome != Accept { - return "", false - } - moved, err := q.RehomeGrab(ctx, dbq.RehomeGrabParams{ID: g.ID, WantID: other.ID}) - if err != nil { - return "", false - } - if err := q.SearchWantAgain(ctx, g.WantID); err != nil { - im.logger.Warn("import: search the first album again", "want", from.Title, "err", err) - } - det := Detail{Verdict: v, RehomedFrom: from.Title} - for _, f := range files { - det.Files = append(det.Files, f.Rel) - } - if v.Edition >= 0 { - det.Edition, det.EditionMBID = describe(editions[v.Edition]), editions[v.Edition].MBID - } - v.Reason = fmt.Sprintf("Grabbed for %s, it is %s, which is wanted too. %s", from.Title, w.Title, v.Reason) - det.Verdict = v - return im.place(ctx, q, moved, d, w, editions, files, v, det, record), true -} - -// refuse blocks the release, asks for the album to be searched again and -// removes the download with its files: it is not the album. -func (im *Importer) refuse(ctx context.Context, q *dbq.Queries, g dbq.Grab, d downloader.Driver, w Want, why string, by pgtype.UUID) { - if err := acquire.NewMemory(im.pool).Block(ctx, dbq.BlockReleaseParams{ - InfoHash: g.InfoHash, Name: g.Name, Reason: acquire.BlockImport, Detail: why, WantID: g.WantID, BlockedBy: by, - }); err != nil { - im.logger.Warn("import: block the release", "grab", g.Name, "err", err) - } - if err := q.SearchWantAgain(ctx, g.WantID); err != nil { - im.logger.Warn("import: search again", "want", w.Title, "err", err) - } - im.removeFromClient(ctx, d, g) -} - -func (im *Importer) removeFromClient(ctx context.Context, d downloader.Driver, g dbq.Grab) { - if d == nil || g.InfoHash == "" { - return - } - if err := d.Delete(ctx, true, g.InfoHash); err != nil { - im.logger.Warn("import: remove the download", "grab", g.Name, "err", err) - } -} - -// Refuse is a person's "refuse" on a download the check held for them, or -// one that failed: it is refused as the check would have. -func (im *Importer) Refuse(ctx context.Context, grabID, by pgtype.UUID) (dbq.Grab, error) { - q := dbq.New(im.pool) - g, err := q.GetGrab(ctx, grabID) - if err != nil { - return dbq.Grab{}, err - } - if g.EndedAt.Valid || (g.ImportState != StateReview && g.ImportState != StateFailed) { - return dbq.Grab{}, ErrNotDecidable - } - var w Want - if g.WantID.Valid { - w, _ = im.want(ctx, q, g.WantID) - } - d, err := im.ClientFor(ctx, g.Client) - if err != nil { - d = nil - } - why := "A person refused it: it is not the album." - im.refuse(ctx, q, g, d, w, why, by) - out, err := q.RecordImport(ctx, dbq.RecordImportParams{ - ID: g.ID, ImportState: StateRefused, ImportReason: why, ImportDetail: g.ImportDetail, - EndReason: StateRefused, - }) - im.Wake() - return out, err -} - -// ErrNotDecidable: the grab is not waiting on a person. -var ErrNotDecidable = errors.New("importer: that download is not waiting on a decision") - -func (im *Importer) want(ctx context.Context, q *dbq.Queries, id pgtype.UUID) (Want, error) { - row, err := q.GetWantForSearch(ctx, id) - if err != nil { - return Want{}, err - } - w := Want{ - GroupMBID: row.ReleaseGroupMbid, ArtistMBID: row.ArtistMbid, Artist: row.ArtistName, Title: row.Title, - Year: yearOf(row.FirstReleaseDate), PrimaryType: row.PrimaryType, SecondaryTypes: row.SecondaryTypes, - } - if row.FulfilledAt.Valid && row.Upgrade != "" && row.AlbumID.Valid { - w.Upgrade, w.AlbumID = row.Upgrade, row.AlbumID - } - return w, nil -} - -func (im *Importer) editions(ctx context.Context, group string) ([]Edition, error) { - if im.Catalogue == nil { - return nil, errors.New("no catalogue") - } - eds, err := im.Catalogue.Releases(ctx, group) - if err != nil { - return nil, err - } - out := make([]Edition, 0, len(eds.Editions)) - for _, e := range eds.Editions { - ed := Edition{MBID: e.Release.Mbid, Title: e.Release.Title, Status: e.Release.Status, - Date: e.Release.ReleaseDate, Country: e.Release.Country} - for _, t := range e.Tracks { - ed.Tracks = append(ed.Tracks, Track{Medium: int(t.MediumPosition), Format: t.MediumFormat, - Position: int(t.Position), Title: t.Title, RecordingMBID: t.RecordingMbid, LengthMs: int(t.LengthMs)}) - } - slices.SortStableFunc(ed.Tracks, func(a, b Track) int { - if a.Medium != b.Medium { - return a.Medium - b.Medium - } - return a.Position - b.Position - }) - out = append(out, ed) - } - return out, nil -} - -// root is the library folder new music is filed into. -func (im *Importer) root(ctx context.Context, q *dbq.Queries) (string, error) { - roots := im.Roots() - if len(roots) == 0 { - return "", errors.New("there is no library folder to file new music into; add one in Settings → Library") - } - s, err := q.GetAcquisitionSettings(ctx) - if err == nil && s.ImportRoot != "" && slices.Contains(roots, s.ImportRoot) { - return s.ImportRoot, nil - } - return roots[0], nil -} - -// held reads which of the planned tracks the library already has: by -// recording, and by disc and track of an album of the same group. -func (im *Importer) held(ctx context.Context, q *dbq.Queries, w Want, plan []Placement) (map[string]bool, map[[2]int]bool, error) { - var recs []string - for _, pl := range plan { - if pl.Recording != "" { - recs = append(recs, pl.Recording) - } - } - rec := map[string]bool{} - if len(recs) > 0 { - got, err := q.HeldRecordings(ctx, recs) - if err != nil { - return nil, nil, err - } - for _, r := range got { - rec[r] = true - } - } - pos := map[[2]int]bool{} - got, err := q.HeldPositions(ctx, &w.GroupMBID) - if err != nil { - return nil, nil, err - } - for _, p := range got { - pos[[2]int{int(p.Disc), int(p.Track)}] = true - } - return rec, pos, nil -} - -func yearOf(date string) int { - if len(date) < 4 { - return 0 - } - y, err := strconv.Atoi(date[:4]) - if err != nil { - return 0 - } - return y -} - -func tracks(n int) string { - if n == 1 { - return "1 track" - } - return strconv.Itoa(n) + " tracks" -} diff --git a/internal/importer/importer_test.go b/internal/importer/importer_test.go deleted file mode 100644 index dd442f1e..00000000 --- a/internal/importer/importer_test.go +++ /dev/null @@ -1,268 +0,0 @@ -package importer - -import ( - "context" - "io" - "log/slog" - "os" - "path/filepath" - "strings" - "testing" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" -) - -func newPool(t *testing.T) *pgxpool.Pool { - t.Helper() - if testing.Short() { - t.Skip("skipping integration test in -short mode") - } - dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") - if dsn == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { - t.Fatalf("migrate: %v", err) - } - pool, err := pgxpool.New(context.Background(), dsn) - if err != nil { - t.Fatalf("pool: %v", err) - } - t.Cleanup(pool.Close) - dbtest.ResetDB(t, pool) - return pool -} - -const hash = "0123456789abcdef0123456789abcdef01234567" - -// fakeClient is a download client holding one finished torrent. -type fakeClient struct { - downloader.Driver - content string - deleted []string -} - -func (c *fakeClient) Torrents(_ context.Context, _ ...string) ([]downloader.Torrent, error) { - return []downloader.Torrent{{Hash: hash, Name: "Daft Punk - RAM [FLAC]", Progress: 1, State: "uploading", - ContentPath: c.content}}, nil -} - -func (c *fakeClient) Delete(_ context.Context, _ bool, hashes ...string) error { - c.deleted = append(c.deleted, hashes...) - return nil -} - -// fakeCatalogue knows RAM as one three-track edition. -type fakeCatalogue struct{} - -func (fakeCatalogue) Releases(_ context.Context, _ string) (catalogue.Editions, error) { - return catalogue.Editions{Editions: []catalogue.Edition{{ - Release: dbq.MbRelease{Mbid: "rel-ram", Title: "Random Access Memories", Status: "Official", ReleaseDate: "2013-05-17"}, - Tracks: []dbq.MbReleaseTrack{ - {MediumPosition: 1, MediumFormat: "CD", Position: 1, Title: "Give Life Back to Music", RecordingMbid: "rec-01"}, - {MediumPosition: 1, MediumFormat: "CD", Position: 2, Title: "The Game of Love", RecordingMbid: "rec-02"}, - {MediumPosition: 1, MediumFormat: "CD", Position: 3, Title: "Giorgio by Moroder", RecordingMbid: "rec-03"}, - }, - }}}, nil -} - -// fakePrinter names each file's recording by its file name: 01.flac is -// rec-01, unless wrong is set. -type fakePrinter struct{ wrong bool } - -func (p fakePrinter) Recordings(_ context.Context, path string) ([]string, error) { - if p.wrong { - return []string{"rec-other"}, nil - } - return []string{"rec-" + strings.TrimSuffix(filepath.Base(path), ".flac")}, nil -} - -type fakeScanner struct{ scanned []string } - -func (s *fakeScanner) ScanFiles(_ context.Context, paths []string) ([]pgtype.UUID, error) { - s.scanned = append(s.scanned, paths...) - return nil, nil -} - -type stack struct { - pool *pgxpool.Pool - q *dbq.Queries - im *Importer - client *fakeClient - scanner *fakeScanner - library string - want dbq.Want - grab dbq.Grab -} - -// newStack wants RAM, has a finished download of it waiting in a fake -// client, and an importer filing into a temp library. -func newStack(t *testing.T, printer Printer) *stack { - t.Helper() - ctx := context.Background() - pool := newPool(t) - q := dbq.New(pool) - content := t.TempDir() - for _, n := range []string{"01", "02", "03"} { - if err := os.WriteFile(filepath.Join(content, n+".flac"), []byte("not really flac "+n), 0o644); err != nil { - t.Fatal(err) - } - } - w, err := wanted.WantWith(ctx, q, wanted.WantParams{GroupMBID: "rg-ram", ArtistMBID: "mb-dp", ArtistName: "Daft Punk", - Title: "Random Access Memories", PrimaryType: "Album", FirstReleaseDate: "2013-05-17", Reason: wanted.ReasonRequest}) - if err != nil { - t.Fatal(err) - } - g, err := acquire.NewMemory(pool).Grabbed(ctx, dbq.InsertGrabParams{WantID: w.ID, InfoHash: hash, - Name: "Daft Punk - RAM [FLAC]", Key: hash, Client: "qbittorrent", SavePath: content, Origin: acquire.OriginSchedule}, - pgtype.UUID{}, "") - if err != nil { - t.Fatal(err) - } - s := &stack{pool: pool, q: q, client: &fakeClient{content: content}, scanner: &fakeScanner{}, - library: t.TempDir(), want: w, grab: g} - s.im = New(pool, s.scanner, fakeCatalogue{}, printer, func() []string { return []string{s.library} }, slog.New(slog.NewTextHandler(io.Discard, nil))) - s.im.ClientFor = func(context.Context, string) (downloader.Driver, error) { return s.client, nil } - return s -} - -func (s *stack) grabNow(t *testing.T) dbq.Grab { - t.Helper() - g, err := s.q.GetGrab(context.Background(), s.grab.ID) - if err != nil { - t.Fatal(err) - } - return g -} - -func TestImport_AFinishedDownloadOfTheAlbumIsFiled_Integration(t *testing.T) { - s := newStack(t, fakePrinter{}) - rep := s.im.Pass(context.Background()) - if rep.Imported != 1 { - t.Fatalf("pass = %+v", rep) - } - g := s.grabNow(t) - if g.ImportState != StateImported || g.EndReason != StateImported || !g.EndedAt.Valid || g.ImportedFiles != 3 { - t.Fatalf("grab = %s %s ended=%v files=%d: %s", g.ImportState, g.EndReason, g.EndedAt.Valid, g.ImportedFiles, g.ImportReason) - } - dir := filepath.Join(s.library, "Daft Punk", "Random Access Memories (2013)") - if g.Destination != dir { - t.Errorf("destination %q", g.Destination) - } - got, err := os.ReadFile(filepath.Join(dir, "Daft Punk - Random Access Memories - 02 - The Game of Love.flac")) - if err != nil || string(got) != "not really flac 02" { - t.Errorf("filed copy: %q %v", got, err) - } - if _, err := os.Stat(filepath.Join(s.client.content, "02.flac")); err != nil { - t.Errorf("the download's own file went: %v", err) - } - if len(s.scanner.scanned) != 3 || len(s.client.deleted) != 0 { - t.Errorf("scanned %v, deleted %v", s.scanner.scanned, s.client.deleted) - } - w, _ := s.q.GetWantByReleaseGroup(context.Background(), "rg-ram") - if !w.SearchedAt.Valid { - t.Error("an imported want is due for search again") - } - if s.im.Pass(context.Background()).Imported != 0 { - t.Error("imported twice") - } -} - -func TestImport_TheWrongAlbumIsRefusedBlockedAndSearchedAgain_Integration(t *testing.T) { - s := newStack(t, fakePrinter{wrong: true}) - ctx := context.Background() - if err := s.q.MarkWantSearched(ctx, s.want.ID); err != nil { - t.Fatal(err) - } - if rep := s.im.Pass(ctx); rep.Refused != 1 { - t.Fatalf("pass = %+v", rep) - } - g := s.grabNow(t) - if g.ImportState != StateRefused || g.EndReason != StateRefused || !strings.Contains(g.ImportReason, "recordings") { - t.Fatalf("grab = %s %s: %s", g.ImportState, g.EndReason, g.ImportReason) - } - blocks, err := s.q.ListReleaseBlocks(ctx) - if err != nil || len(blocks) != 1 || blocks[0].InfoHash != hash || blocks[0].Reason != acquire.BlockImport { - t.Errorf("blocks = %+v %v", blocks, err) - } - if len(s.client.deleted) != 1 { - t.Errorf("the refused download was not removed: %v", s.client.deleted) - } - w, _ := s.q.GetWantByReleaseGroup(ctx, "rg-ram") - if w.SearchedAt.Valid { - t.Error("a refused want is not due for search") - } - entries, _ := os.ReadDir(s.library) - if len(entries) != 0 { - t.Errorf("the library got %d entries", len(entries)) - } -} - -func TestImport_ADoubtfulDownloadWaitsForAPersonThenImportsAnyway_Integration(t *testing.T) { - s := newStack(t, nil) // no AcoustID; untagged files: nothing but lengths - ctx := context.Background() - if rep := s.im.Pass(ctx); rep.Review != 1 { - t.Fatalf("pass = %+v", rep) - } - g := s.grabNow(t) - if g.ImportState != StateReview || g.EndedAt.Valid || !strings.Contains(g.ImportReason, "A person decides") { - t.Fatalf("grab = %s ended=%v: %s", g.ImportState, g.EndedAt.Valid, g.ImportReason) - } - if rep := s.im.Pass(ctx); rep.Review+rep.Imported != 0 { - t.Errorf("a held download was looked at again: %+v", rep) - } - if _, err := s.q.ForceGrabImport(ctx, g.ID); err != nil { - t.Fatal(err) - } - if rep := s.im.Pass(ctx); rep.Imported != 1 { - t.Fatalf("after import anyway: %+v", rep) - } - if g := s.grabNow(t); g.ImportState != StateImported || !strings.Contains(g.ImportReason, "anyway") { - t.Errorf("grab = %s: %s", g.ImportState, g.ImportReason) - } -} - -func TestImport_APersonRefusesADoubtfulDownload_Integration(t *testing.T) { - s := newStack(t, nil) - ctx := context.Background() - s.im.Pass(ctx) - g, err := s.im.Refuse(ctx, s.grab.ID, pgtype.UUID{}) - if err != nil { - t.Fatal(err) - } - if g.ImportState != StateRefused || !g.EndedAt.Valid || len(s.client.deleted) != 1 { - t.Errorf("grab = %s ended=%v deleted=%v", g.ImportState, g.EndedAt.Valid, s.client.deleted) - } - if _, err := s.im.Refuse(ctx, s.grab.ID, pgtype.UUID{}); err == nil { - t.Error("refused twice") - } -} - -func TestImport_AnUnfinishedDownloadIsOnlyWatched_Integration(t *testing.T) { - s := newStack(t, fakePrinter{}) - s.im.ClientFor = func(context.Context, string) (downloader.Driver, error) { return &halfClient{s.client}, nil } - if rep := s.im.Pass(context.Background()); rep.Downloading != 1 { - t.Fatalf("pass = %+v", rep) - } - g := s.grabNow(t) - if g.ImportState != StateDownloading || g.Progress < 0.49 || g.Progress > 0.51 { - t.Errorf("grab = %s %.2f", g.ImportState, g.Progress) - } -} - -type halfClient struct{ *fakeClient } - -func (c *halfClient) Torrents(ctx context.Context, h ...string) ([]downloader.Torrent, error) { - ts, _ := c.fakeClient.Torrents(ctx, h...) - ts[0].Progress, ts[0].State = 0.5, "downloading" - return ts, nil -} diff --git a/internal/importer/inspect.go b/internal/importer/inspect.go deleted file mode 100644 index e727b4cb..00000000 --- a/internal/importer/inspect.go +++ /dev/null @@ -1,176 +0,0 @@ -package importer - -import ( - "context" - "errors" - "io/fs" - "os" - "path/filepath" - "sort" - "strings" - - "github.com/dhowden/tag" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acoustid" - "git.fabledsword.com/bvandeusen/minstrel/internal/library" -) - -// Printer says which MusicBrainz recordings a file's audio is. ErrNoPrints: -// it cannot say for any file (AcoustID is not set up, or not answering), so -// the check rests on lengths and tags. -type Printer interface { - Recordings(ctx context.Context, path string) ([]string, error) -} - -// ErrNoPrints: fingerprints are not available for this download. -var ErrNoPrints = errors.New("importer: fingerprints are not available") - -// AcoustIDPrinter fingerprints with fpcalc and asks AcoustID, under the -// AcoustID settings (key, minimum score). Each call reads the settings, so -// a change takes effect on the next file. -type AcoustIDPrinter struct { - Settings interface { - Get() library.AcoustIDSettings - } - Client *acoustid.Client -} - -// Recordings is every recording AcoustID gives at or above the minimum score: -// one audio can be several recordings (the album track and the single). -func (p *AcoustIDPrinter) Recordings(ctx context.Context, path string) ([]string, error) { - s := p.Settings.Get() - if !s.Ready() || p.Client == nil { - return nil, ErrNoPrints - } - fp, dur, err := library.LookupFingerprint(ctx, path) - if err != nil { - if library.IsInconclusive(err) { - return nil, ErrNoPrints - } - return nil, nil // fpcalc rejected the file: no evidence either way - } - cands, err := p.Client.Lookup(ctx, s.APIKey, fp, dur) - switch { - case errors.Is(err, acoustid.ErrInvalidKey), errors.Is(err, acoustid.ErrUnavailable): - return nil, ErrNoPrints - case err != nil: - return nil, nil - } - var out []string - for _, c := range cands { - if c.Score >= float64(s.MinScore) && c.ID != "" { - out = append(out, c.ID) - } - } - return out, nil -} - -// formatRank picks which format's files are the album when a download holds -// more than one (a FLAC folder and an MP3 folder): the most files, then the -// better format. -var formatRank = map[string]int{".flac": 6, ".wav": 5, ".m4a": 4, ".opus": 3, ".ogg": 2, ".mp3": 1, ".aac": 0} - -// audioFiles lists the audio files under root (a folder, or one file), in -// the one format that is the album. Dot-named files and folders are skipped: -// they are partial downloads, OS litter or Minstrel's own temp files. -func audioFiles(root string) ([]string, error) { - info, err := os.Stat(root) - if err != nil { - return nil, err - } - if !info.IsDir() { - if library.IsAudioFile(root) { - return []string{root}, nil - } - return nil, nil - } - byExt := map[string][]string{} - err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if strings.HasPrefix(d.Name(), ".") && path != root { - if d.IsDir() { - return filepath.SkipDir - } - return nil - } - if !d.IsDir() && library.IsAudioFile(path) { - ext := strings.ToLower(filepath.Ext(path)) - byExt[ext] = append(byExt[ext], path) - } - return nil - }) - if err != nil { - return nil, err - } - best := "" - for ext, paths := range byExt { - if best == "" || len(paths) > len(byExt[best]) || - (len(paths) == len(byExt[best]) && formatRank[ext] > formatRank[best]) { - best = ext - } - } - out := byExt[best] - sort.Strings(out) - return out, nil -} - -// readFile reads what one file's tags and length say. A file whose tags -// cannot be read still counts, by its length and name. -func readFile(ctx context.Context, root, path string) File { - f := File{Path: path, Rel: rel(root, path), Video: len(library.SourceMarkersFor(path)) > 0} - if ms, err := library.ProbeDurationMs(ctx, path); err == nil { - f.LengthMs = int(ms) - } - h, err := os.Open(path) - if err != nil { - return f - } - defer func() { _ = h.Close() }() - m, err := tag.ReadFrom(h) - if err != nil { - return f - } - f.Title, f.Artist, f.AlbumArtist, f.Album = m.Title(), m.Artist(), m.AlbumArtist(), m.Album() - f.Track, f.Disc = library.ReadTrackAndDisc(m) - ids := library.ReadTagMBIDs(m) - f.GroupMBID, f.ReleaseMBID, f.RecordingMBID = ids.ReleaseGroup, ids.Release, ids.Recording - return f -} - -func rel(root, path string) string { - if r, err := filepath.Rel(root, path); err == nil && r != "." { - return r - } - return filepath.Base(path) -} - -// inspect reads every audio file under root and asks the printer about each. -// fingerprinted reports whether the printer answered at all. -func inspect(ctx context.Context, root string, p Printer) (files []File, fingerprinted bool, err error) { - paths, err := audioFiles(root) - if err != nil { - return nil, false, err - } - fingerprinted = p != nil - for _, path := range paths { - if ctx.Err() != nil { - return nil, false, ctx.Err() - } - f := readFile(ctx, root, path) - if fingerprinted && len(paths) <= maxFiles { - recs, err := p.Recordings(ctx, path) - if errors.Is(err, ErrNoPrints) { - fingerprinted = false - for i := range files { - files[i].Recordings = nil - } - } else { - f.Recordings = recs - } - } - files = append(files, f) - } - return files, fingerprinted, nil -} diff --git a/internal/importer/name.go b/internal/importer/name.go deleted file mode 100644 index b6a15a5b..00000000 --- a/internal/importer/name.go +++ /dev/null @@ -1,109 +0,0 @@ -package importer - -import ( - "fmt" - "path/filepath" - "strings" - "unicode/utf8" -) - -// Placement is where one file goes and the names written into its tags. -type Placement struct { - File int // index into the files - Dest string // absolute - Title string - Disc int - Discs int - Track int - Tracks int // on its disc - // Recording is the MusicBrainz recording the file was matched to. - Recording string -} - -// Plan names every file's destination under root the way the library is -// conventionally named, so new arrivals sit beside what is there: -// -// {Artist}/{Album} ({Year})/{Artist} - {Album} - {track:00} - {Title}.ext -// -// with a "{Medium Format} {medium:00}" folder between when the edition has -// more than one medium. A file the check paired to a track takes the track's -// name and place; one it did not (an "import anyway" with more files than -// tracks) keeps its own tags. -func Plan(root string, w Want, e *Edition, files []File, pairs []Pair) []Placement { - album := w.Title - if w.Year > 0 { - album = fmt.Sprintf("%s (%d)", w.Title, w.Year) - } - dir := filepath.Join(root, Segment(w.Artist), Segment(album)) - media := 1 - if e != nil { - media = max(e.Media(), 1) - } - perDisc := map[int]int{} - if e != nil { - for _, t := range e.Tracks { - perDisc[t.Medium]++ - } - } - paired := map[int]Pair{} - for _, p := range pairs { - paired[p.File] = p - } - - out := make([]Placement, 0, len(files)) - for i, f := range files { - pl := Placement{File: i, Title: f.Title, Disc: max(f.Disc, 1), Track: f.Track, Discs: media} - format := "" - if p, ok := paired[i]; ok && e != nil { - t := e.Tracks[p.Track] - pl.Title, pl.Disc, pl.Track, pl.Recording, format = t.Title, max(t.Medium, 1), t.Position, t.RecordingMBID, t.Format - pl.Tracks = perDisc[t.Medium] - } - if pl.Title == "" { - pl.Title = strings.TrimSuffix(filepath.Base(f.Path), filepath.Ext(f.Path)) - } - d := dir - if media > 1 { - if format == "" { - format = "Disc" - } - d = filepath.Join(dir, Segment(fmt.Sprintf("%s %02d", format, pl.Disc))) - } - name := fmt.Sprintf("%s - %s - %02d - %s", w.Artist, w.Title, pl.Track, pl.Title) - pl.Dest = filepath.Join(d, Segment(name)+strings.ToLower(filepath.Ext(f.Path))) - out = append(out, pl) - } - return out -} - -// segmentMax keeps a folder or file name inside the 255 bytes most -// filesystems allow, with room for an extension and a temp prefix. -const segmentMax = 200 - -var illegal = strings.NewReplacer( - `\`, "+", "/", "+", "<", "", ">", "", "?", "!", "*", "-", "|", "", `"`, "", - // The "smart" colon: "Title: Subtitle" reads "Title - Subtitle". - ": ", " - ", ":", "-", -) - -// Segment makes a name safe as one path segment, the way the *arr programs -// clean file names: their illegal-character mapping, no leading or trailing -// spaces or dots, at most segmentMax bytes, never "" or "..". -func Segment(s string) string { - s = illegal.Replace(s) - s = strings.Map(func(r rune) rune { - if r < 0x20 || r == 0x7f { - return -1 - } - return r - }, s) - s = strings.Trim(s, " .") - for len(s) > segmentMax { - _, size := utf8.DecodeLastRuneInString(s) - s = strings.TrimRight(s[:len(s)-size], " .") - } - if s == "" { - return "_" - } - return s -} diff --git a/internal/importer/place.go b/internal/importer/place.go deleted file mode 100644 index 090091ea..00000000 --- a/internal/importer/place.go +++ /dev/null @@ -1,210 +0,0 @@ -package importer - -import ( - "context" - "crypto/rand" - "encoding/hex" - "errors" - "fmt" - "io" - "os" - "os/exec" - "path/filepath" - "strconv" - "strings" - "time" -) - -// ErrOccupied: the destination already has a file. Nothing is overwritten. -var ErrOccupied = errors.New("importer: destination already holds a file") - -// tagTimeout bounds one ffmpeg run. A remux copies the audio untouched, so a -// whole album track takes well under a second on local disk; two minutes is -// for a slow network mount, not for ffmpeg. -const tagTimeout = 2 * time.Minute - -// Tags are the names written into an imported file. Empty values are left -// out; the file's own tags stay for everything not named here. -type Tags struct { - Title, Artist, Album, AlbumArtist string - Year int - Disc, Discs, Track, Tracks int - // MusicBrainz ids, written the way Picard writes them. - ReleaseMBID, GroupMBID, RecordingMBID, ArtistMBID string -} - -// args turns the tags into ffmpeg -metadata pairs for a file of this -// extension. Vorbis comments (FLAC, Ogg, Opus) take Picard's key names; -// ID3 takes Picard's TXXX descriptions, which ffmpeg writes for any key it -// has no frame for. MP4's freeform atoms ffmpeg cannot write, so an M4A -// gets the plain names only. -// -// A Vorbis track or disc number is the number alone, its count in -// TRACKTOTAL and DISCTOTAL, as Picard writes them: dhowden/tag reads a -// TRACKNUMBER of "3/20" as 0, which would lose the album's order. ID3 and MP4 -// take "3/20", which their readers parse. -func (t Tags) args(ext string) []string { - vorbis := ext == ".flac" || ext == ".ogg" || ext == ".opus" - track, disc := ofN(t.Track, t.Tracks), ofN(t.Disc, t.Discs) - if vorbis { - track, disc = ofN(t.Track, 0), ofN(t.Disc, 0) - } - kv := [][2]string{ - {"title", t.Title}, {"artist", t.Artist}, {"album", t.Album}, {"album_artist", t.AlbumArtist}, - {"track", track}, {"disc", disc}, - } - if t.Year > 0 { - kv = append(kv, [2]string{"date", strconv.Itoa(t.Year)}) - } - switch ext { - case ".flac", ".ogg", ".opus": - if t.Track > 0 && t.Tracks > 0 { - kv = append(kv, [2]string{"TRACKTOTAL", strconv.Itoa(t.Tracks)}) - } - if t.Disc > 0 && t.Discs > 0 { - kv = append(kv, [2]string{"DISCTOTAL", strconv.Itoa(t.Discs)}) - } - kv = append(kv, - [2]string{"MUSICBRAINZ_ALBUMID", t.ReleaseMBID}, - [2]string{"MUSICBRAINZ_RELEASEGROUPID", t.GroupMBID}, - [2]string{"MUSICBRAINZ_TRACKID", t.RecordingMBID}, - [2]string{"MUSICBRAINZ_ARTISTID", t.ArtistMBID}, - [2]string{"MUSICBRAINZ_ALBUMARTISTID", t.ArtistMBID}) - case ".mp3": - kv = append(kv, - [2]string{"MusicBrainz Album Id", t.ReleaseMBID}, - [2]string{"MusicBrainz Release Group Id", t.GroupMBID}, - [2]string{"MusicBrainz Track Id", t.RecordingMBID}, - [2]string{"MusicBrainz Artist Id", t.ArtistMBID}, - [2]string{"MusicBrainz Album Artist Id", t.ArtistMBID}) - } - var out []string - for _, p := range kv { - if p[1] != "" { - out = append(out, "-metadata", p[0]+"="+p[1]) - } - } - return out -} - -func ofN(n, of int) string { - switch { - case n <= 0: - return "" - case of > 0: - return fmt.Sprintf("%d/%d", n, of) - } - return strconv.Itoa(n) -} - -// taggable: the formats ffmpeg can rewrite tags in by remuxing. -var taggable = map[string]bool{".flac": true, ".ogg": true, ".opus": true, ".mp3": true, ".m4a": true} - -// Place copies src to dst with tags written, leaving src as it was (the -// download keeps seeding). The copy is written to a dot-named temp file -// beside dst, which the scanner skips, and renamed into place, so the -// library never sees half a file. Tags are written by an ffmpeg remux that -// copies the audio untouched; when ffmpeg cannot (it is missing, or the -// container will not take the file's streams) the file is copied as it is -// and tagged reports false. An existing dst is never overwritten. -func Place(ctx context.Context, src, dst string, t Tags) (tagged bool, err error) { - return placeFile(ctx, src, dst, t, false) -} - -// Overwrite is Place onto a file an upgrade replaces: the new copy -// takes the old one's path in one rename, so the library never sees the -// path empty, and the track row at that path, history and all, carries on -// with the new file. -func Overwrite(ctx context.Context, src, dst string, t Tags) (tagged bool, err error) { - return placeFile(ctx, src, dst, t, true) -} - -func placeFile(ctx context.Context, src, dst string, t Tags, overwrite bool) (tagged bool, err error) { - if _, err := os.Lstat(dst); err == nil && !overwrite { - return false, fmt.Errorf("%w: %s", ErrOccupied, dst) - } - dir := filepath.Dir(dst) - if err := os.MkdirAll(dir, 0o755); err != nil { - return false, fmt.Errorf("make %s: %w", dir, err) - } - ext := strings.ToLower(filepath.Ext(dst)) - tmp := filepath.Join(dir, ".minstrel-import-"+randomHex()+ext) - defer func() { _ = os.Remove(tmp) }() // gone already once renamed - - if taggable[ext] { - tagged = remux(ctx, src, tmp, t.args(ext)) == nil - } - if !tagged { - if err := copyFile(src, tmp); err != nil { - return false, err - } - } - if err := os.Chmod(tmp, 0o644); err != nil { - return false, fmt.Errorf("chmod %s: %w", tmp, err) - } - if _, err := os.Lstat(dst); err == nil && !overwrite { - return false, fmt.Errorf("%w: %s", ErrOccupied, dst) - } - if err := os.Rename(tmp, dst); err != nil { - return false, fmt.Errorf("move into place %s: %w", dst, err) - } - return tagged, nil -} - -// remux writes src to dst through ffmpeg with every stream copied as it is -// (the cover art too) and the tags set. Its output is checked against the -// source's size: a remux shifts container bytes, never the audio, so a file -// much smaller than its source lost something on the way. -func remux(ctx context.Context, src, dst string, meta []string) error { - ctx, cancel := context.WithTimeout(ctx, tagTimeout) - defer cancel() - args := append([]string{"-nostdin", "-v", "error", "-y", "-i", src, - "-map", "0", "-c", "copy", "-map_metadata", "0"}, meta...) - args = append(args, dst) - cmd := exec.CommandContext(ctx, "ffmpeg", args...) - if out, err := cmd.CombinedOutput(); err != nil { - _ = os.Remove(dst) - return fmt.Errorf("ffmpeg: %w: %s", err, strings.TrimSpace(string(out))) - } - si, err := os.Stat(src) - if err != nil { - return err - } - di, err := os.Stat(dst) - if err != nil { - return err - } - if di.Size() < si.Size()/2 { - _ = os.Remove(dst) - return fmt.Errorf("ffmpeg wrote %d bytes from %d", di.Size(), si.Size()) - } - return nil -} - -// copyFile copies src to dst byte for byte and syncs it. -func copyFile(src, dst string) error { - in, err := os.Open(src) - if err != nil { - return err - } - defer func() { _ = in.Close() }() - out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) - if err != nil { - return err - } - if _, err := io.Copy(out, in); err != nil { - _ = out.Close() - return fmt.Errorf("copy %s: %w", src, err) - } - if err := out.Sync(); err != nil { - _ = out.Close() - return err - } - return out.Close() -} - -func randomHex() string { - b := make([]byte, 6) - _, _ = rand.Read(b) - return hex.EncodeToString(b) -} diff --git a/internal/importer/upgrade.go b/internal/importer/upgrade.go deleted file mode 100644 index d40f9c08..00000000 --- a/internal/importer/upgrade.go +++ /dev/null @@ -1,304 +0,0 @@ -package importer - -import ( - "context" - "errors" - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/library" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// Importing an upgrade: a better copy of an album the library -// holds. Each file is matched to the track it would replace, by recording -// and then by disc and track. It replaces that track when the track is a -// video rip or the new file is a better tier; otherwise the old track stays. -// A file with no track to replace fills a gap, as any import does. -// -// The new copy is filed before the old one goes. On the old file's own path -// it takes the path in one rename and the row carries on; at a new path it -// is scanned in as a track of its own and the old track is folded into it -// (library.ReplaceTrack), so likes, plays and playlist entries carry over. - -// OldTrack is a library track an upgrade may replace. -type OldTrack struct { - ID pgtype.UUID - Path string - Recording string - Disc, Track int - Format string - Bytes int64 - Ms int64 - Rip bool -} - -// Tier is the old track's tier, estimated as the upgrade check estimates -// an album's; ok is false for a format the ladder cannot place. -func (o OldTrack) Tier() (release.Tier, bool) { - return release.TierOfFile(o.Format, release.FileKbps(o.Bytes, o.Ms)) -} - -// What an upgrade does with one placement. -const ( - stepPlace = "place" // no track to replace: fills a gap - stepReplace = "replace" // replaces Old from a new path - stepOverwrite = "overwrite" // replaces Old on its own path - stepKeep = "keep" // Old is as good: left as it is -) - -type upgradeStep struct { - Placement int - Action string - Old int // index into olds, or -1 -} - -// planUpgrade matches each placement to the track it would replace and -// decides what to do with it. newTiers is each placement's tier. -func planUpgrade(plan []Placement, newTiers []release.Tier, olds []OldTrack) []upgradeStep { - used := make([]bool, len(olds)) - match := make([]int, len(plan)) - for i := range match { - match[i] = -1 - } - for i, pl := range plan { - if pl.Recording == "" { - continue - } - for j, o := range olds { - if !used[j] && o.Recording == pl.Recording { - match[i], used[j] = j, true - break - } - } - } - for i, pl := range plan { - if match[i] >= 0 || pl.Track <= 0 { - continue - } - for j, o := range olds { - if !used[j] && o.Track == pl.Track && max(o.Disc, 1) == max(pl.Disc, 1) { - match[i], used[j] = j, true - break - } - } - } - steps := make([]upgradeStep, len(plan)) - for i, pl := range plan { - j := match[i] - steps[i] = upgradeStep{Placement: i, Old: j, Action: stepPlace} - if j < 0 { - continue - } - o := olds[j] - oldTier, _ := o.Tier() - better := release.Rank(newTiers[i]) > release.Rank(oldTier) - switch { - case !o.Rip && !better: - steps[i].Action = stepKeep - case filepath.Clean(pl.Dest) == filepath.Clean(o.Path): - steps[i].Action = stepOverwrite - default: - steps[i].Action = stepReplace - } - } - return steps -} - -// fileTier estimates a downloaded file's tier from its size and length, the -// way the library's are estimated, so the two compare like for like. A file -// that cannot be measured takes the release's tier. -func fileTier(f File, fallback release.Tier) release.Tier { - if info, err := os.Stat(f.Path); err == nil && f.LengthMs > 0 { - if t, ok := release.TierOfFile(filepath.Ext(f.Path), release.FileKbps(info.Size(), int64(f.LengthMs))); ok { - return t - } - } - return fallback -} - -func oldTracks(rows []dbq.ListUpgradeTracksRow) []OldTrack { - out := make([]OldTrack, 0, len(rows)) - for _, r := range rows { - o := OldTrack{ID: r.ID, Path: r.FilePath, Disc: int(r.Disc), Format: r.FileFormat, - Bytes: r.FileSize, Ms: int64(r.DurationMs), Rip: r.SourceVerdict != nil && *r.SourceVerdict == "suspect"} - if r.Mbid != nil { - o.Recording = *r.Mbid - } - if r.TrackNumber != nil { - o.Track = int(*r.TrackNumber) - } - out = append(out, o) - } - return out -} - -// placeUpgrade files an accepted upgrade, replacing the tracks it betters. -func (im *Importer) placeUpgrade(ctx context.Context, q *dbq.Queries, g dbq.Grab, d downloader.Driver, w Want, - e *Edition, files []File, plan []Placement, v Verdict, det Detail, record recordFunc) string { - rows, err := q.ListUpgradeTracks(ctx, w.AlbumID) - if err != nil { - return record(StateWaiting, "Couldn't read the album's tracks: "+err.Error(), "", det, 0, "") - } - olds := oldTracks(rows) - grabTier := release.Tier(g.Quality) - newTiers := make([]release.Tier, len(plan)) - for i, pl := range plan { - newTiers[i] = fileTier(files[pl.File], grabTier) - } - steps := planUpgrade(plan, newTiers, olds) - heldRec, heldPos, err := im.held(ctx, q, w, plan) - if err != nil { - return record(StateWaiting, "Couldn't read what the library holds: "+err.Error(), "", det, 0, "") - } - - type swap struct { - old OldTrack - dest string - } - var ( - placed []string // every file written, for the scan - swaps []swap - inPlace []OldTrack - failed []string - ) - for _, st := range steps { - pl := plan[st.Placement] - if st.Action == stepKeep { - det.Skipped++ - continue - } - if st.Action == stepPlace && ((pl.Recording != "" && heldRec[pl.Recording]) || heldPos[[2]int{pl.Disc, pl.Track}]) { - det.Skipped++ - continue - } - tags := Tags{Title: pl.Title, Artist: w.Artist, Album: w.Title, AlbumArtist: w.Artist, Year: w.Year, - Disc: pl.Disc, Discs: pl.Discs, Track: pl.Track, Tracks: pl.Tracks, - GroupMBID: w.GroupMBID, RecordingMBID: pl.Recording, ArtistMBID: w.ArtistMBID} - if e != nil { - tags.ReleaseMBID = e.MBID - } - src := files[pl.File].Path - var tagged bool - if st.Action == stepOverwrite { - tagged, err = Overwrite(ctx, src, pl.Dest, tags) - } else { - tagged, err = Place(ctx, src, pl.Dest, tags) - } - switch { - case errors.Is(err, ErrOccupied): - det.Skipped++ - continue - case err != nil: - // Each replacement stands alone: the old track stays where a - // copy failed, and the rest go ahead. - failed = append(failed, pl.Title) - im.logger.Warn("import: copy an upgrade into the library", "grab", g.Name, "dest", pl.Dest, "err", err) - continue - } - placed = append(placed, pl.Dest) - if !tagged { - det.Untagged++ - } - switch st.Action { - case stepOverwrite: - inPlace = append(inPlace, olds[st.Old]) - case stepReplace: - swaps = append(swaps, swap{old: olds[st.Old], dest: pl.Dest}) - } - } - det.Placed = len(placed) - - if len(placed) == 0 { - why := fmt.Sprintf("Nothing in it is better than the copy of %s you have, so nothing was imported.", w.Title) - if len(failed) > 0 { - return record(StateFailed, fmt.Sprintf("Copying into the library failed for %s.", strings.Join(failed, ", ")), - "", det, 0, "") - } - // Set aside, so the weekly search doesn't take it again. - if err := acquire.NewMemory(im.pool).Block(ctx, dbq.BlockReleaseParams{ - InfoHash: g.InfoHash, Name: g.Name, Reason: acquire.BlockImport, Detail: why, WantID: g.WantID, - }); err != nil { - im.logger.Warn("import: set the release aside", "grab", g.Name, "err", err) - } - if err := q.MarkWantSearched(ctx, g.WantID); err != nil { - im.logger.Warn("import: mark the want searched", "grab", g.Name, "err", err) - } - im.removeFromClient(ctx, d, g) - return record(StateHeld, why, StateHeld, det, 0, "") - } - - if _, err := im.Scanner.ScanFiles(ctx, placed); err != nil { - im.logger.Warn("import: scan the new files", "grab", g.Name, "err", err) - } - for _, o := range inPlace { - if o.Rip { - if err := q.ClearTrackSourceVerdict(ctx, o.ID); err != nil { - im.logger.Warn("import: release a replaced rip", "path", o.Path, "err", err) - } - } - } - replaced, kept := len(inPlace), 0 - for _, s := range swaps { - nt, err := q.GetTrackByPath(ctx, s.dest) - if err != nil { - kept++ - im.logger.Warn("import: find the new track", "path", s.dest, "err", err) - continue - } - moved, err := library.ReplaceTrack(ctx, im.pool, im.logger, im.DataDir, s.old.ID, nt.ID) - if err != nil { - // The old file stays; the duplicate sweep finds the two copies. - kept++ - im.logger.Warn("import: replace the old track", "old", s.old.Path, "new", s.dest, "err", err) - continue - } - replaced++ - im.logger.Info("import: replaced a track with a better copy", "old", s.old.Path, "new", s.dest, - "play_events", moved.PlayEvents, "likes", moved.Likes, "playlist_entries", moved.PlaylistEntries) - } - - // The want holds the album the new tracks are in, once the old album row - // has none left. - var album pgtype.UUID - if left, err := q.ListUpgradeTracks(ctx, w.AlbumID); err == nil && len(left) == 0 { - if nt, err := q.GetTrackByPath(ctx, placed[0]); err == nil { - album = nt.AlbumID - } - } - if err := q.FinishUpgrade(ctx, dbq.FinishUpgradeParams{ID: g.WantID, AlbumID: album}); err != nil { - im.logger.Warn("import: finish the upgrade", "grab", g.Name, "err", err) - } - if err := q.MarkWantSearched(ctx, g.WantID); err != nil { - im.logger.Warn("import: mark the want searched", "grab", g.Name, "err", err) - } - - dest := filepath.Dir(placed[0]) - if e != nil && e.Media() > 1 { - dest = filepath.Dir(dest) - } - reason := fmt.Sprintf("Replaced %s of %s with a better copy.", tracks(replaced), w.Title) - if added := len(placed) - replaced - kept; added > 0 { - reason += fmt.Sprintf(" Added %s it was missing.", tracks(added)) - } - if det.Skipped > 0 { - reason += fmt.Sprintf(" %s already as good left as they were.", tracks(det.Skipped)) - } - if kept > 0 { - reason += fmt.Sprintf(" %s couldn't be removed, so both copies are in the library.", tracks(kept)) - } - if len(failed) > 0 { - reason += fmt.Sprintf(" Copying failed for %s; those stay as they were.", strings.Join(failed, ", ")) - } - if det.Untagged > 0 { - reason += fmt.Sprintf(" %s copied with their own tags: ffmpeg could not rewrite them.", tracks(det.Untagged)) - } - return record(StateImported, reason+" "+v.Reason, StateImported, det, len(placed), dest) -} diff --git a/internal/importer/upgrade_test.go b/internal/importer/upgrade_test.go deleted file mode 100644 index a415fd45..00000000 --- a/internal/importer/upgrade_test.go +++ /dev/null @@ -1,53 +0,0 @@ -package importer - -import ( - "testing" - - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -func TestPlanUpgrade_ReplacesOnlyWhatItBetters(t *testing.T) { - plan := []Placement{ - {File: 0, Dest: "/music/A/B (2001)/A - B - 01 - One.flac", Disc: 1, Track: 1, Recording: "rec-1"}, - {File: 1, Dest: "/music/A/B (2001)/A - B - 02 - Two.mp3", Disc: 1, Track: 2}, - {File: 2, Dest: "/music/A/B (2001)/A - B - 03 - Three.flac", Disc: 1, Track: 3}, - {File: 3, Dest: "/music/A/B (2001)/A - B - 04 - Four.flac", Disc: 1, Track: 4}, - {File: 4, Dest: "/music/A/B (2001)/A - B - 05 - Five.flac", Disc: 1, Track: 5}, - } - newTiers := []release.Tier{release.TierLossless, release.TierLossyHigh, release.TierLossless, - release.TierLossless, release.TierLossyLow} - olds := []OldTrack{ - // Matched by recording though its number is wrong: a 192 MP3. - {Path: "/music/A/B/01 One.mp3", Recording: "rec-1", Disc: 1, Track: 9, Format: "mp3", Bytes: 4_800_000, Ms: 200_000}, - // Same path as the new file, a 320 MP3: the new 320 is no better. - {Path: "/music/A/B (2001)/A - B - 02 - Two.mp3", Disc: 1, Track: 2, Format: "mp3", Bytes: 8_000_000, Ms: 200_000}, - // Already lossless: kept. - {Path: "/music/A/B/03 Three.flac", Disc: 1, Track: 3, Format: "flac", Bytes: 22_000_000, Ms: 200_000}, - // A video rip, even though lossless: replaced. - {Path: "/music/A/B/04 Four (Official Video).flac", Disc: 0, Track: 4, Format: "flac", Bytes: 22_000_000, Ms: 200_000, Rip: true}, - } - steps := planUpgrade(plan, newTiers, olds) - want := []struct { - action string - old int - }{ - {stepReplace, 0}, {stepKeep, 1}, {stepKeep, 2}, {stepReplace, 3}, {stepPlace, -1}, - } - for i, w := range want { - if steps[i].Action != w.action || steps[i].Old != w.old { - t.Errorf("placement %d: %s old %d, want %s old %d", i, steps[i].Action, steps[i].Old, w.action, w.old) - } - } -} - -func TestPlanUpgrade_OverwritesAWorseFileOnItsOwnPath(t *testing.T) { - dest := "/music/A/B (2001)/A - B - 01 - One.mp3" - steps := planUpgrade( - []Placement{{File: 0, Dest: dest, Disc: 1, Track: 1}}, - []release.Tier{release.TierLossyHigh}, - []OldTrack{{Path: dest, Disc: 1, Track: 1, Format: "mp3", Bytes: 3_200_000, Ms: 200_000}}, // 128 - ) - if steps[0].Action != stepOverwrite || steps[0].Old != 0 { - t.Errorf("got %+v", steps[0]) - } -} diff --git a/internal/importer/verify.go b/internal/importer/verify.go deleted file mode 100644 index 9181d46c..00000000 --- a/internal/importer/verify.go +++ /dev/null @@ -1,437 +0,0 @@ -// Package importer checks a finished download against the album it was -// grabbed for and files it into the library. Nothing reaches -// the library before the check says the files are the album: a wrong album, -// another version of it, a video rip or an incomplete copy is refused, its -// release is blocked and the album is searched for again. -// -// The files are matched to one of the album's MusicBrainz editions by -// AcoustID fingerprints, track lengths, titles and the tags they carry. -package importer - -import ( - "fmt" - "slices" - "sort" - "strings" - - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// Outcome is what the check decided. -type Outcome string - -const ( - Accept Outcome = "accept" - // Review: the files may be the album and the evidence cannot say. A - // person decides. - Review Outcome = "review" - Refuse Outcome = "refuse" -) - -// Codes say why, beside the sentence in Verdict.Reason. -const ( - CodeMatched = "matched" - CodeForced = "forced" - CodeNoAudio = "no_audio" - CodeVideo = "video" - CodeWrongAlbum = "wrong_album" - CodeOtherVersion = release.VetoVersion - CodeWrongTitle = release.VetoTitle - CodeIncomplete = "incomplete" - CodeNoEdition = "no_edition" - CodeWeak = "weak" - CodeTooMany = "too_many" -) - -// maxFiles is the most audio files one album is checked for. More is a box -// set or a discography, which is a person's call, not a fingerprint queue. -const maxFiles = 60 - -// File is what one audio file in a download says about itself. -type File struct { - Path string // absolute, as Minstrel sees it - Rel string // under the download, for the person reading the verdict - // Disc and Track are the tags' positions; 0 when untagged. - Disc, Track int - Title string - Artist string - AlbumArtist string - Album string - // MusicBrainz ids from the tags, when a tagger wrote them. - GroupMBID string - ReleaseMBID string - RecordingMBID string - LengthMs int - // Recordings are the MusicBrainz recordings AcoustID says the audio is, - // at or above the configured minimum score. - Recordings []string - // Video: the file name says it is a music video, a lyric video or the - // like (library.SourceMarkersFor). - Video bool -} - -// Track is one track of an edition. -type Track struct { - Medium int - Format string // the medium's: CD, Digital Media, Vinyl - Position int - Title string - RecordingMBID string - LengthMs int -} - -// Edition is one MusicBrainz release of the wanted release group, its -// tracks in medium and position order. -type Edition struct { - MBID string - Title string - Status string - Date string - Country string - Tracks []Track -} - -// Media is how many media the edition has. -func (e Edition) Media() int { - n := 0 - for _, t := range e.Tracks { - n = max(n, t.Medium) - } - return n -} - -// Want is the album the download was grabbed for. -type Want struct { - GroupMBID string - ArtistMBID string - Artist string - Title string - Year int - PrimaryType string - SecondaryTypes []string - // Upgrade: a held album wanted for a better copy, quality or - // rip; AlbumID is the library album it holds, whose tracks the import - // replaces. - Upgrade string - AlbumID pgtype.UUID -} - -// Pair is a file matched to an edition's track, and what agreed. -type Pair struct { - File int `json:"file"` - Track int `json:"track"` - Recording bool `json:"recording"` - Length bool `json:"length"` - Title bool `json:"title"` -} - -// Evidence is what the check counted, kept with the verdict so a person can -// see what it rested on. -type Evidence struct { - Files int `json:"files"` - Tracks int `json:"tracks"` - // Identified: files with a recording from AcoustID or their tags. - Identified int `json:"identified"` - Recordings int `json:"recordings"` - Lengths int `json:"lengths"` - Titles int `json:"titles"` - // GroupTag: "same" when the tags name this release group, "other" when - // another, "" when they name none. - GroupTag string `json:"group_tag,omitempty"` - AlbumTag string `json:"album_tag,omitempty"` - // Fingerprinted: AcoustID was asked. False when it is not set up or - // could not be reached; the check then rests on lengths and tags. - Fingerprinted bool `json:"fingerprinted"` -} - -// Verdict is the check's answer. -type Verdict struct { - Outcome Outcome `json:"outcome"` - Code string `json:"code"` - Reason string `json:"reason"` - // Edition indexes the editions passed in; -1 when none was chosen. - Edition int `json:"-"` - Pairs []Pair `json:"pairs,omitempty"` - Evidence Evidence `json:"evidence"` -} - -// Verify decides whether files are the wanted album and, if so, which -// edition and which track each file is. force is a person's "import anyway": -// the best edition is taken whatever the evidence, but a download with no -// audio is still refused. -func Verify(w Want, editions []Edition, files []File, fingerprinted, force bool) Verdict { - v := Verdict{Edition: -1, Evidence: Evidence{Files: len(files), Fingerprinted: fingerprinted}} - if len(files) == 0 { - return v.set(Refuse, CodeNoAudio, "The download holds no audio files.") - } - if !force && len(files) > maxFiles { - return v.set(Review, CodeTooMany, fmt.Sprintf( - "The download holds %d audio files, more than one album; a person decides.", len(files))) - } - if video := countIf(files, func(f File) bool { return f.Video }); !force && video*2 > len(files) { - return v.set(Refuse, CodeVideo, fmt.Sprintf( - "%d of its %d audio files are named as music or lyric videos: the sound of a video, not the album.", video, len(files))) - } - - group := majority(files, func(f File) string { return f.GroupMBID }) - switch { - case group == "": - case strings.EqualFold(group, w.GroupMBID): - v.Evidence.GroupTag = "same" - default: - v.Evidence.GroupTag = "other" - } - album := majority(files, func(f File) string { return f.Album }) - v.Evidence.AlbumTag = album - var tagVeto release.Match - albumAgrees := false - if album != "" { - // The want's own artist goes in front: the tags' artist spelling is - // not what this asks. Only the album title and its version words. - m := release.Judge(release.Want{Artist: w.Artist, Title: w.Title, PrimaryType: w.PrimaryType, - SecondaryTypes: w.SecondaryTypes}, release.Parse(w.Artist+" - "+album)) - switch m.Veto { - case release.VetoVersion, release.VetoTitle: - tagVeto = m - case "": - albumAgrees = true - } - } - if !force && v.Evidence.GroupTag == "other" { - return v.set(Refuse, CodeWrongAlbum, fmt.Sprintf( - "Its tags name a different MusicBrainz release group (%s), not %s.", group, w.Title)) - } - - if len(editions) == 0 { - if force { - v.Code, v.Outcome, v.Reason = CodeForced, Accept, "Imported anyway at a person's word; MusicBrainz lists no edition to match it to." - return v - } - return v.set(Review, CodeNoEdition, fmt.Sprintf( - "MusicBrainz lists no edition of %s to check the files against; a person decides.", w.Title)) - } - - best, bestScore := -1, -1 - shortest, longest := len(editions[0].Tracks), 0 - for i, e := range editions { - shortest, longest = min(shortest, len(e.Tracks)), max(longest, len(e.Tracks)) - if len(e.Tracks) != len(files) && !force { - continue - } - pairs := pairUp(files, e.Tracks) - if s := score(pairs); s > bestScore || (s == bestScore && better(e, editions[best])) { - best, bestScore = i, s - } - } - if best < 0 { - if len(files) < shortest { - return v.set(Refuse, CodeIncomplete, fmt.Sprintf( - "It holds %d audio files; the shortest edition of %s has %d tracks.", len(files), w.Title, shortest)) - } - return v.set(Review, CodeNoEdition, fmt.Sprintf( - "It holds %d audio files; no edition of %s has %d tracks (they have %s). A person decides.", - len(files), w.Title, len(files), span(shortest, longest))) - } - - e := editions[best] - v.Edition = best - v.Pairs = pairUp(files, e.Tracks) - n := len(e.Tracks) - ev := &v.Evidence - ev.Tracks = n - for _, p := range v.Pairs { - f := files[p.File] - if f.RecordingMBID != "" || len(f.Recordings) > 0 { - ev.Identified++ - } - ev.Recordings += b2i(p.Recording) - ev.Lengths += b2i(p.Length) - ev.Titles += b2i(p.Title) - } - if force { - v.Outcome, v.Code = Accept, CodeForced - v.Reason = fmt.Sprintf("Imported anyway at a person's word, as %s. %s", describe(e), ev.sentence()) - return v - } - - half := (n + 1) / 2 - strong := ev.GroupTag == "same" || (ev.Identified >= half && ev.Recordings*10 >= ev.Identified*7) - switch { - case ev.Identified >= half && ev.Recordings*10 < ev.Identified*3 && ev.GroupTag != "same": - return v.set(Refuse, CodeWrongAlbum, fmt.Sprintf( - "Only %d of the %d tracks AcoustID knows are recordings on %s.", ev.Recordings, ev.Identified, w.Title)) - case tagVeto.Veto != "" && !strong: - return v.set(Refuse, tagVeto.Veto, fmt.Sprintf("Its tags call it %q. %s", album, tagVeto.Reason)) - case !strong && ev.Identified == 0 && ev.Lengths*2 < n: - return v.set(Refuse, CodeWrongAlbum, fmt.Sprintf( - "Only %d of its %d tracks are the length of %s's, and nothing else says it is the album.", ev.Lengths, n, w.Title)) - case strong && ev.Lengths*10 >= n*6, - !strong && ev.Lengths*10 >= n*8 && (ev.Titles*10 >= n*6 || albumAgrees): - v.Outcome, v.Code = Accept, CodeMatched - v.Reason = fmt.Sprintf("It is %s. %s", describe(e), ev.sentence()) - return v - } - return v.set(Review, CodeWeak, fmt.Sprintf( - "It may be %s, but the evidence is thin: %s A person decides.", describe(e), ev.sentence())) -} - -func (v Verdict) set(o Outcome, code, reason string) Verdict { - v.Outcome, v.Code, v.Reason = o, code, reason - return v -} - -// sentence says what agreed, for the verdict's reason. -func (ev Evidence) sentence() string { - var parts []string - if ev.GroupTag == "same" { - parts = append(parts, "its tags name this album") - } - if ev.Identified > 0 { - parts = append(parts, fmt.Sprintf("%d of %d identified tracks are its recordings", ev.Recordings, ev.Identified)) - } - parts = append(parts, fmt.Sprintf("%d of %d lengths fit", ev.Lengths, ev.Tracks), - fmt.Sprintf("%d of %d titles agree", ev.Titles, ev.Tracks)) - s := strings.Join(parts, ", ") - s = strings.ToUpper(s[:1]) + s[1:] + "." - if !ev.Fingerprinted { - s += " AcoustID was not asked (not set up or unreachable), so lengths and tags decided." - } - return s -} - -// describe names an edition for a person: "the 2013 CD release (13 tracks)". -func describe(e Edition) string { - var b strings.Builder - b.WriteString("the ") - if len(e.Date) >= 4 { - b.WriteString(e.Date[:4] + " ") - } - if f := e.format(); f != "" { - b.WriteString(f + " ") - } - fmt.Fprintf(&b, "edition (%d tracks)", len(e.Tracks)) - return b.String() -} - -func (e Edition) format() string { - if len(e.Tracks) == 0 { - return "" - } - f := e.Tracks[0].Format - for _, t := range e.Tracks { - if t.Format != f { - return "" - } - } - return f -} - -// pairUp matches files to tracks in order: by the tags' disc and track -// numbers when every file has them and no two share a place, by file path -// otherwise. With a person's "import anyway" the counts may differ; pairs -// run to the shorter. -func pairUp(files []File, tracks []Track) []Pair { - order := make([]int, len(files)) - for i := range order { - order[i] = i - } - if numbered(files) { - sort.SliceStable(order, func(a, b int) bool { - fa, fb := files[order[a]], files[order[b]] - if da, db := max(fa.Disc, 1), max(fb.Disc, 1); da != db { - return da < db - } - return fa.Track < fb.Track - }) - } else { - sort.SliceStable(order, func(a, b int) bool { return files[order[a]].Rel < files[order[b]].Rel }) - } - n := min(len(files), len(tracks)) - pairs := make([]Pair, n) - for i := range n { - f, t := files[order[i]], tracks[i] - pairs[i] = Pair{ - File: order[i], - Track: i, - Recording: t.RecordingMBID != "" && (f.RecordingMBID == t.RecordingMBID || slices.Contains(f.Recordings, t.RecordingMBID)), - Length: release.LengthFits(f.LengthMs, t.LengthMs), - Title: release.TitleAgrees(f.Title, t.Title), - } - } - return pairs -} - -// numbered: every file carries a track number and no two share a disc and -// track. -func numbered(files []File) bool { - seen := map[[2]int]bool{} - for _, f := range files { - k := [2]int{max(f.Disc, 1), f.Track} - if f.Track <= 0 || seen[k] { - return false - } - seen[k] = true - } - return true -} - -// score weighs recordings above lengths above titles: a fingerprint is the -// audio itself, a length is the audio's shape, a title is only a name. -func score(pairs []Pair) int { - s := 0 - for _, p := range pairs { - s += 3*b2i(p.Recording) + 2*b2i(p.Length) + b2i(p.Title) - } - return s -} - -// better breaks a tie between equally good editions: an official one, then -// the earlier. -func better(a, b Edition) bool { - if (a.Status == "Official") != (b.Status == "Official") { - return a.Status == "Official" - } - return a.Date != "" && (b.Date == "" || a.Date < b.Date) -} - -func majority(files []File, get func(File) string) string { - counts := map[string]int{} - best, n := "", 0 - for _, f := range files { - v := strings.TrimSpace(get(f)) - if v == "" { - continue - } - counts[strings.ToLower(v)]++ - if c := counts[strings.ToLower(v)]; c > n { - best, n = v, c - } - } - return best -} - -func countIf(files []File, f func(File) bool) int { - n := 0 - for _, x := range files { - if f(x) { - n++ - } - } - return n -} - -func span(a, b int) string { - if a == b { - return fmt.Sprint(a) - } - return fmt.Sprintf("%d to %d", a, b) -} - -func b2i(b bool) int { - if b { - return 1 - } - return 0 -} diff --git a/internal/importer/verify_test.go b/internal/importer/verify_test.go deleted file mode 100644 index e3208fb7..00000000 --- a/internal/importer/verify_test.go +++ /dev/null @@ -1,239 +0,0 @@ -package importer - -import ( - "fmt" - "path/filepath" - "strings" - "testing" -) - -var ram = Want{GroupMBID: "rg-ram", ArtistMBID: "mb-dp", Artist: "Daft Punk", Title: "Random Access Memories", - Year: 2013, PrimaryType: "Album"} - -var ramTitles = []string{"Give Life Back to Music", "The Game of Love", "Giorgio by Moroder", "Within", - "Instant Crush", "Lose Yourself to Dance", "Touch", "Get Lucky", "Beyond", "Motherboard", - "Fragments of Time", "Doin' It Right", "Contact"} - -// edition is RAM's 13-track CD, recordings rec-01.., lengths 4:00 + n seconds. -func edition(mbid string, n int) Edition { - e := Edition{MBID: mbid, Title: "Random Access Memories", Status: "Official", Date: "2013-05-17"} - for i := range n { - e.Tracks = append(e.Tracks, Track{Medium: 1, Format: "CD", Position: i + 1, Title: ramTitles[i%len(ramTitles)], - RecordingMBID: fmt.Sprintf("rec-%02d", i+1), LengthMs: 240000 + i*1000}) - } - return e -} - -// download is n files lined up with the edition: tagged positions, lengths -// within a second, AcoustID naming each file's recording. -func download(n int) []File { - var fs []File - for i := range n { - fs = append(fs, File{Path: fmt.Sprintf("/dl/%02d.flac", i+1), Rel: fmt.Sprintf("%02d.flac", i+1), - Disc: 1, Track: i + 1, Title: ramTitles[i%len(ramTitles)], Album: "Random Access Memories", - LengthMs: 240000 + i*1000 + 400, Recordings: []string{fmt.Sprintf("rec-%02d", i+1)}}) - } - return fs -} - -func TestVerify_TheAlbumAndWhatIsNot(t *testing.T) { - eds := []Edition{edition("rel-cd", 13), edition("rel-deluxe", 15)} - cases := []struct { - name string - files func() []File - printed bool - out Outcome - code string - edition string - }{ - {"the album, fingerprinted", func() []File { return download(13) }, true, Accept, CodeMatched, "rel-cd"}, - {"the deluxe by count", func() []File { return download(15) }, true, Accept, CodeMatched, "rel-deluxe"}, - {"no audio", func() []File { return nil }, true, Refuse, CodeNoAudio, ""}, - {"music videos", func() []File { - fs := download(13) - for i := range fs { - fs[i].Video = i < 10 - } - return fs - }, true, Refuse, CodeVideo, ""}, - {"tags name another group", func() []File { - fs := download(13) - for i := range fs { - fs[i].GroupMBID = "rg-homework" - } - return fs - }, true, Refuse, CodeWrongAlbum, ""}, - {"fingerprints say other recordings", func() []File { - fs := download(13) - for i := range fs { - fs[i].Recordings = []string{"rec-other"} - } - return fs - }, true, Refuse, CodeWrongAlbum, "rel-cd"}, - {"the drumless edition: right lengths, album tag says otherwise", func() []File { - fs := download(13) - for i := range fs { - fs[i].Recordings, fs[i].Album = nil, "Random Access Memories (Drumless Edition)" - } - return fs - }, false, Refuse, CodeOtherVersion, "rel-cd"}, - {"fewer files than any edition", func() []File { return download(9) }, true, Refuse, CodeIncomplete, ""}, - {"no edition this long", func() []File { return download(14) }, true, Review, CodeNoEdition, ""}, - {"no fingerprints, lengths and titles agree", func() []File { - fs := download(13) - for i := range fs { - fs[i].Recordings = nil - } - return fs - }, false, Accept, CodeMatched, "rel-cd"}, - {"no fingerprints, no titles, nothing to go on but lengths", func() []File { - fs := download(13) - for i := range fs { - fs[i].Recordings, fs[i].Title, fs[i].Album = nil, "", "" - } - return fs - }, false, Review, CodeWeak, "rel-cd"}, - {"no fingerprints, lengths all wrong", func() []File { - fs := download(13) - for i := range fs { - fs[i].Recordings, fs[i].LengthMs = nil, 600000 - } - return fs - }, false, Refuse, CodeWrongAlbum, "rel-cd"}, - {"tags name the group: strong even unprinted", func() []File { - fs := download(13) - for i := range fs { - fs[i].Recordings, fs[i].Title, fs[i].GroupMBID = nil, "", "rg-ram" - } - return fs - }, false, Accept, CodeMatched, "rel-cd"}, - } - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - v := Verify(ram, eds, c.files(), c.printed, false) - if v.Outcome != c.out || v.Code != c.code { - t.Fatalf("got %s/%s (%s), want %s/%s", v.Outcome, v.Code, v.Reason, c.out, c.code) - } - got := "" - if v.Edition >= 0 { - got = eds[v.Edition].MBID - } - if got != c.edition { - t.Errorf("edition %q, want %q", got, c.edition) - } - if v.Reason == "" { - t.Error("no reason given") - } - }) - } -} - -func TestVerify_UnnumberedFilesPairByPath(t *testing.T) { - fs := download(13) - for i := range fs { - fs[i].Track, fs[i].Disc = 0, 0 - } - fs[0], fs[5] = fs[5], fs[0] // listed out of order; their names sort right - v := Verify(ram, []Edition{edition("rel-cd", 13)}, fs, true, false) - if v.Outcome != Accept || v.Evidence.Recordings != 13 { - t.Fatalf("got %+v", v) - } -} - -func TestVerify_ImportAnywayTakesTheBestEdition(t *testing.T) { - v := Verify(ram, []Edition{edition("rel-cd", 13)}, download(14), true, true) - if v.Outcome != Accept || v.Code != CodeForced || v.Edition != 0 || len(v.Pairs) != 13 { - t.Fatalf("got %+v", v) - } - if v := Verify(ram, nil, nil, true, true); v.Outcome != Refuse { - t.Errorf("forced with no audio: %+v", v) - } -} - -func TestVerify_WithoutAcoustIDTheReasonSaysSo(t *testing.T) { - fs := download(13) - for i := range fs { - fs[i].Recordings = nil - } - v := Verify(ram, []Edition{edition("rel-cd", 13)}, fs, false, false) - if !strings.Contains(v.Reason, "AcoustID was not asked") { - t.Errorf("reason %q", v.Reason) - } -} - -func TestPlan_LibraryNaming(t *testing.T) { - e := edition("rel-cd", 2) - e.Tracks[1].Title = "Within: Part 2/3?" - pl := Plan("/music", ram, &e, download(2), Verify(ram, []Edition{e}, download(2), true, false).Pairs) - want := []string{ - "/music/Daft Punk/Random Access Memories (2013)/Daft Punk - Random Access Memories - 01 - Give Life Back to Music.flac", - "/music/Daft Punk/Random Access Memories (2013)/Daft Punk - Random Access Memories - 02 - Within - Part 2+3!.flac", - } - for i, p := range pl { - if p.Dest != want[i] { - t.Errorf("dest %d = %q, want %q", i, p.Dest, want[i]) - } - } - if pl[0].Recording != "rec-01" || pl[0].Tracks != 2 { - t.Errorf("placement %+v", pl[0]) - } -} - -func TestPlan_MultiDiscGetsAMediumFolder(t *testing.T) { - e := edition("rel-2cd", 4) - e.Tracks[2].Medium, e.Tracks[2].Position = 2, 1 - e.Tracks[3].Medium, e.Tracks[3].Position = 2, 2 - fs := download(4) - fs[2].Disc, fs[2].Track, fs[3].Disc, fs[3].Track = 2, 1, 2, 2 - pl := Plan("/music", ram, &e, fs, Verify(ram, []Edition{e}, fs, true, false).Pairs) - if got := filepath.Dir(pl[3].Dest); got != "/music/Daft Punk/Random Access Memories (2013)/CD 02" { - t.Errorf("disc folder %q", got) - } - if !strings.HasSuffix(pl[3].Dest, " - 02 - Get Lucky.flac") && !strings.Contains(pl[3].Dest, " - 02 - ") { - t.Errorf("dest %q", pl[3].Dest) - } -} - -func TestSegment(t *testing.T) { - for in, want := range map[string]string{ - "AC/DC": "AC+DC", `What?`: "What!", "Title: Sub": "Title - Sub", "a:b": "a-b", " ..x.. ": "x", - "": "_", "..": "_", `"Quoted" *y* |z|`: "Quoted x -y- z", - } { - if got := Segment(in); got != want { - t.Errorf("Segment(%q) = %q, want %q", in, got, want) - } - } - if long := Segment(strings.Repeat("é", 200)); len(long) > segmentMax { - t.Errorf("long segment is %d bytes", len(long)) - } -} - -func TestTagsArgs(t *testing.T) { - tg := Tags{Title: "Get Lucky", Artist: "Daft Punk", Track: 8, Tracks: 13, Disc: 1, Discs: 1, Year: 2013, - RecordingMBID: "rec-08", GroupMBID: "rg-ram"} - flac := strings.Join(tg.args(".flac"), " ") - for _, s := range []string{ - "title=Get Lucky", "track=8 ", "TRACKTOTAL=13", "disc=1 ", "DISCTOTAL=1", "date=2013", - "MUSICBRAINZ_TRACKID=rec-08", "MUSICBRAINZ_RELEASEGROUPID=rg-ram", - } { - if !strings.Contains(flac, s) { - t.Errorf("flac args lack %q: %s", s, flac) - } - } - // dhowden/tag reads a Vorbis TRACKNUMBER of "8/13" as 0. - if strings.Contains(flac, "/13") { - t.Errorf("flac track number carries its count: %s", flac) - } - if mp3 := strings.Join(tg.args(".mp3"), " "); !strings.Contains(mp3, "track=8/13") { - t.Errorf("mp3 track should be 8/13, which ID3 readers parse: %s", mp3) - } - if strings.Contains(flac, "MUSICBRAINZ_ALBUMID") { - t.Errorf("an empty release id was written: %s", flac) - } - if mp3 := strings.Join(tg.args(".mp3"), " "); !strings.Contains(mp3, "MusicBrainz Track Id=rec-08") { - t.Errorf("mp3 args %s", mp3) - } - if m4a := strings.Join(tg.args(".m4a"), " "); strings.Contains(m4a, "MusicBrainz") { - t.Errorf("m4a args %s", m4a) - } -} diff --git a/internal/indexer/indexer.go b/internal/indexer/indexer.go deleted file mode 100644 index d6a8af17..00000000 --- a/internal/indexer/indexer.go +++ /dev/null @@ -1,156 +0,0 @@ -// Package indexer is what every search source answers with, and the Torznab -// client. -// -// A client here holds ONE endpoint and knows nothing about the others. The -// fan-out across sources, the merge and what a source being down means belong -// to internal/acquire, because they are decisions about a search rather than -// about one source. -// -// Prowlarr is not served from here: it has no aggregate Torznab feed (`all` -// 404s like any id it does not have), so asking it everything at once means -// its native API, which internal/prowlarr speaks. -package indexer - -import ( - "errors" - "fmt" - "io" - "net/http" - "strings" - "time" -) - -// ErrUnconfigured is a source with no address (or, for Prowlarr, no key): not -// a failure, an instance that does not search there. -var ErrUnconfigured = errors.New("indexer is not configured") - -// ErrUnreachable is a search that could not be made: no network, a timeout, a -// malformed answer, a status outside 2xx. -var ErrUnreachable = errors.New("indexer unreachable") - -// ErrAuth is a key the source refused. Separate from ErrUnreachable because -// it needs a different sentence and a different action: a refused key is a -// field in Settings, an outage is something to wait out. -var ErrAuth = errors.New("the API key was refused") - -// UserAgent names Minstrel in Prowlarr's and each indexer's logs. -const UserAgent = "Minstrel (self-hosted music server; release search)" - -// MusicCategories are the Newznab categories a music search asks for: 3000 -// Audio, with its MP3 (3010) and Lossless (3040) children alongside, because -// an indexer that files music under a child answers only for the child. -// 3030 (Audiobook) is deliberately absent. -var MusicCategories = []int{3000, 3010, 3040} - -// Query is one search: the words, and which categories to ask in. Empty -// Categories asks every category. -type Query struct { - Text string - Categories []int -} - -// Result is one search result, as the source described it. -type Result struct { - Title string - // InfoHash is the torrent's identity, lowercased; empty when the source - // gave none. - InfoHash string - // Magnet is a magnet URI when the source published one. - Magnet string - // DownloadURL is a .torrent URL when it did not. It must be fetched - // through the source that offered it (it holds the tracker credentials), - // never handed to the download client. - DownloadURL string - SizeBytes int64 - Category int - // Indexer is the tracker BEHIND the source, as the source named it. - Indexer string - // Source is the acquisition_services code that was asked: where a - // .torrent fetch must be routed back to. - Source string - Published time.Time - // Seeders and Peers as reported. A feed that has not sampled a swarm - // reports a nominal 1, so these are not health unless SwarmKnown. - Seeders int - Peers int - // SwarmKnown says Seeders was measured, which only FabledExchange's own - // search can say. - SwarmKnown bool - // FilesCount is how many files the torrent holds, 0 when not said. - FilesCount int -} - -// Send sends req through c, stopping at a redirect to a magnet (returned as a -// *MagnetLink), and reads at most 8 MB of the answer. A call that never got -// an answer is ErrUnreachable; the status is the caller's to read. -func Send(c *http.Client, req *http.Request) (*http.Response, []byte, error) { - resp, err := StopAtMagnet(c).Do(req) - if m, ok := AsMagnet(err); ok { - return nil, nil, &MagnetLink{URI: m} - } - if err != nil { - return nil, nil, fmt.Errorf("%w: %v", ErrUnreachable, err) - } - defer func() { _ = resp.Body.Close() }() - body, err := io.ReadAll(io.LimitReader(resp.Body, 8<<20)) - if err != nil { - return nil, nil, fmt.Errorf("%w: read body: %v", ErrUnreachable, err) - } - return resp, body, nil -} - -// StatusError is what a non-2xx status means for a search source: a 429 is -// RateLimited naming who, anything else ErrUnreachable. Nil for a 2xx. -func StatusError(resp *http.Response, who string) error { - switch { - case resp.StatusCode == http.StatusTooManyRequests: - return &RateLimited{Who: who, RetryAfter: RetryAfter(resp.Header, time.Now())} - case resp.StatusCode < 200 || resp.StatusCode >= 300: - return fmt.Errorf("%w: HTTP %d", ErrUnreachable, resp.StatusCode) - } - return nil -} - -// NormalizeHash is a torrent's identity in its one canonical spelling. Every -// comparison of infohashes goes through it, so a hash differing only in case -// is never two releases. -func NormalizeHash(s string) string { - return strings.ToLower(strings.TrimSpace(s)) -} - -// IsInfoHash says whether s is shaped like a v1 (40) or v2 (64) hex infohash. -func IsInfoHash(s string) bool { - s = NormalizeHash(s) - if len(s) != 40 && len(s) != 64 { - return false - } - for _, r := range s { - if (r < '0' || r > '9') && (r < 'a' || r > 'f') { - return false - } - } - return true -} - -// MagnetHash pulls the infohash out of a magnet link, or returns "". It does -// not validate it. -func MagnetHash(magnet string) string { - const prefix = "urn:btih:" - at := strings.Index(magnet, prefix) - if at < 0 { - return "" - } - hash := magnet[at+len(prefix):] - if cut := strings.IndexAny(hash, "&/?"); cut >= 0 { - hash = hash[:cut] - } - return strings.ToLower(hash) -} - -// FirstLine is an error's first line, for a sentence on the settings page. -func FirstLine(s string) string { - if i := strings.IndexByte(s, '\n'); i >= 0 { - return s[:i] - } - return s -} diff --git a/internal/indexer/magnet.go b/internal/indexer/magnet.go deleted file mode 100644 index 123e61c2..00000000 --- a/internal/indexer/magnet.go +++ /dev/null @@ -1,58 +0,0 @@ -package indexer - -import ( - "bytes" - "errors" - "net/http" -) - -// MagnetLink is a download link that turned out to be a magnet. Returned as -// an error by a .torrent fetch so the grab hands the magnet to the download -// client instead. Public trackers do this routinely: Prowlarr's download link -// answers with a redirect to `magnet:?xt=…`, which Go's client would otherwise -// fail on as an unsupported scheme. -type MagnetLink struct{ URI string } - -func (m *MagnetLink) Error() string { return "the download link is a magnet link" } - -// AsMagnet says whether err is a magnet link, and which. -func AsMagnet(err error) (string, bool) { - var m *MagnetLink - if errors.As(err, &m) && m.URI != "" { - return m.URI, true - } - return "", false -} - -// StopAtMagnet returns a copy of c that stops at a redirect to a magnet URI -// and reports it as a *MagnetLink. Every other redirect behaves as c would. -func StopAtMagnet(c *http.Client) *http.Client { - if c == nil { - c = http.DefaultClient - } - out := *c - next := c.CheckRedirect - out.CheckRedirect = func(req *http.Request, via []*http.Request) error { - if req.URL != nil && req.URL.Scheme == "magnet" { - return &MagnetLink{URI: req.URL.String()} - } - if next != nil { - return next(req, via) - } - if len(via) >= 10 { - return errors.New("stopped after 10 redirects") - } - return nil - } - return &out -} - -// MagnetBody is a response whose whole body is a magnet URI, which some -// indexers send with a 200 instead of a redirect. Nil otherwise. -func MagnetBody(body []byte) error { - trimmed := bytes.TrimSpace(body) - if bytes.HasPrefix(trimmed, []byte("magnet:")) && !bytes.ContainsAny(trimmed, " \n<") { - return &MagnetLink{URI: string(trimmed)} - } - return nil -} diff --git a/internal/indexer/ratelimit.go b/internal/indexer/ratelimit.go deleted file mode 100644 index 5be3ca9f..00000000 --- a/internal/indexer/ratelimit.go +++ /dev/null @@ -1,50 +0,0 @@ -package indexer - -import ( - "errors" - "fmt" - "net/http" - "strconv" - "time" -) - -// RateLimited is a source answering 429. Named so the search pacing can hold -// that source back for as long as it asked; still an ErrUnreachable to -// everything else, because this search did not reach it. -type RateLimited struct { - // RetryAfter is what the source asked for; zero when it did not say. - RetryAfter time.Duration - // Who names the source in the sentence, e.g. "Prowlarr". - Who string -} - -func (e *RateLimited) Error() string { - return fmt.Sprintf("%v: HTTP 429, %s is rate-limiting this search", ErrUnreachable, e.Who) -} - -func (e *RateLimited) Unwrap() error { return ErrUnreachable } - -// AsRateLimited finds a RateLimited anywhere in err's chain. -func AsRateLimited(err error) (*RateLimited, bool) { - var rl *RateLimited - if errors.As(err, &rl) { - return rl, true - } - return nil, false -} - -// RetryAfter reads a Retry-After header: seconds, or an HTTP date. Zero when -// absent or unreadable. -func RetryAfter(h http.Header, now time.Time) time.Duration { - v := h.Get("Retry-After") - if v == "" { - return 0 - } - if s, err := strconv.Atoi(v); err == nil && s > 0 { - return time.Duration(s) * time.Second - } - if t, err := http.ParseTime(v); err == nil && t.After(now) { - return t.Sub(now) - } - return 0 -} diff --git a/internal/indexer/torznab.go b/internal/indexer/torznab.go deleted file mode 100644 index 8a458b1b..00000000 --- a/internal/indexer/torznab.go +++ /dev/null @@ -1,253 +0,0 @@ -package indexer - -import ( - "context" - "encoding/xml" - "fmt" - "net/http" - "net/url" - "strconv" - "strings" - "time" -) - -// DefaultTimeout is what an unset Timeout means. Longer than a metadata -// call: a feed may itself fan out to several trackers before answering. -const DefaultTimeout = 30 * time.Second - -// TorznabConfig is one Torznab feed. -type TorznabConfig struct { - // Code is the acquisition_services row this client was built from, - // stamped onto every Result. - Code string - // FeedURL is the feed's whole address up to and including /api, as - // Jackett or a Prowlarr indexer shows it — e.g. - // http://prowlarr:9696/12/api. Query parameters are appended to it. - // One field rather than base + path + indexer id: a pasted URL is more - // reliable than Minstrel being right about a third party's routes. - FeedURL string - // APIKey is optional: Torznab makes it so. A feed that needs one says so - // with Newznab error 100, which is ErrAuth. - APIKey string - Timeout time.Duration -} - -// Torznab searches one Torznab feed. -type Torznab struct { - Config TorznabConfig - // HTTP is injectable for tests. Nil takes http.DefaultClient. - HTTP *http.Client -} - -// Search asks the feed for q. `t=search` is free text, which every indexer -// supports; `t=music` takes artist and album separately, and a release name -// does not reliably say which of its parts is which. -func (c *Torznab) Search(ctx context.Context, q Query) ([]Result, error) { - params := url.Values{"t": {"search"}, "q": {q.Text}} - for _, cat := range q.Categories { - params.Add("cat", strconv.Itoa(cat)) - } - body, err := c.get(ctx, c.endpoint(params)) - if err != nil { - return nil, err - } - out, err := parseFeed(body) - if err != nil { - return nil, err - } - for i := range out { - out[i].Source = c.Config.Code - } - return out, nil -} - -// Download fetches a result's .torrent bytes through the feed, which holds -// the tracker's credentials. A result with a magnet needs none of this. -func (c *Torznab) Download(ctx context.Context, r Result) ([]byte, error) { - if r.DownloadURL == "" { - return nil, fmt.Errorf("%w: %q has no download URL", ErrUnreachable, r.Title) - } - body, err := c.get(ctx, r.DownloadURL) - if err != nil { - return nil, err - } - return torrentBody(r.Title, body) -} - -// torrentBody checks a fetched .torrent. A torrent is a bencoded dictionary -// and starts with 'd'; an expired session gets an HTML login page with HTTP -// 200, which the download client would otherwise reject as an unsupported -// file from a component that did nothing wrong. -func torrentBody(title string, body []byte) ([]byte, error) { - if err := MagnetBody(body); err != nil { - return nil, err - } - if len(body) == 0 || body[0] != 'd' { - return nil, fmt.Errorf("%w: %q returned %d bytes that are not a torrent", ErrUnreachable, title, len(body)) - } - return body, nil -} - -// TorrentBody is torrentBody for the other search clients' fetches. -func TorrentBody(title string, body []byte) ([]byte, error) { return torrentBody(title, body) } - -func (c *Torznab) endpoint(params url.Values) string { - if c.Config.APIKey != "" { - params.Set("apikey", c.Config.APIKey) - } - feed := strings.TrimSpace(c.Config.FeedURL) - sep := "?" - if strings.Contains(feed, "?") { - sep = "&" - } - return feed + sep + params.Encode() -} - -func (c *Torznab) get(ctx context.Context, endpoint string) ([]byte, error) { - if strings.TrimSpace(c.Config.FeedURL) == "" { - return nil, ErrUnconfigured - } - timeout := c.Config.Timeout - if timeout <= 0 { - timeout = DefaultTimeout - } - ctx, cancel := context.WithTimeout(ctx, timeout) - defer cancel() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) - if err != nil { - return nil, fmt.Errorf("%w: build request: %v", ErrUnreachable, err) - } - req.Header.Set("User-Agent", UserAgent) - resp, body, err := Send(c.HTTP, req) - if err != nil { - return nil, err - } - if err := StatusError(resp, "the indexer"); err != nil { - return nil, err - } - // Newznab reports errors as HTTP 200 with an document, so a - // client checking only the status reads a refused key as an empty result - // set: search quietly stops finding things. - if err := errorDocument(body); err != nil { - return nil, err - } - return body, nil -} - -// errorDocument reads an answer. Newznab's 1xx -// codes (100 wrong key, 101 suspended, 102 insufficient privileges) are all -// about who is asking, so they are ErrAuth. -func errorDocument(body []byte) error { - head := body - if len(head) > 512 { - head = head[:512] - } - if !strings.Contains(string(head), " - // and repeats it in , either populated by version. - for _, candidate := range []string{it.Link, it.Enclosure.URL} { - if strings.HasPrefix(candidate, "magnet:") && r.Magnet == "" { - r.Magnet = candidate - } else if candidate != "" && r.DownloadURL == "" { - r.DownloadURL = candidate - } - } - // The infohash from the explicit attr, then the magnet, then the GUID - // only when it is shaped like a hash: Prowlarr puts a URL there as - // often as a hash. - r.InfoHash = NormalizeHash(it.attr("infohash")) - if r.InfoHash == "" { - r.InfoHash = MagnetHash(r.Magnet) - } - if r.InfoHash == "" && IsInfoHash(it.GUID) { - r.InfoHash = NormalizeHash(it.GUID) - } - if t, err := time.Parse(time.RFC1123Z, strings.TrimSpace(it.PubDate)); err == nil { - r.Published = t - } else if t, err := time.Parse(time.RFC1123, strings.TrimSpace(it.PubDate)); err == nil { - r.Published = t - } - out = append(out, r) - } - return out, nil -} - -func atoi(s string) int { - n, err := strconv.Atoi(strings.TrimSpace(s)) - if err != nil { - return 0 - } - return n -} diff --git a/internal/indexer/torznab_test.go b/internal/indexer/torznab_test.go deleted file mode 100644 index 2855f0af..00000000 --- a/internal/indexer/torznab_test.go +++ /dev/null @@ -1,188 +0,0 @@ -package indexer - -import ( - "context" - "errors" - "net/http" - "net/http/httptest" - "testing" - "time" -) - -const sampleFeed = ` - - - - Radiohead - OK Computer (1997) [FLAC] - https://tracker.example/details/1 - https://prowlarr.example/1/download?link=abc - 512000000 - Tue, 07 Oct 2026 10:00:00 +0000 - - - - - - - - Radiohead - Kid A (2000) [MP3 320] - 0123456789abcdef0123456789abcdef01234567 - magnet:?xt=urn:btih:FFEEDDCCBBAA99887766554433221100FFEEDDCC&dn=kid+a - - - - - -` - -func TestTorznabSearch_ParsesTheFeedAndStampsTheSource(t *testing.T) { - reqs := make(chan *http.Request, 1) - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - reqs <- r - _, _ = w.Write([]byte(sampleFeed)) - })) - defer srv.Close() - - c := &Torznab{Config: TorznabConfig{Code: "torznab-2", FeedURL: srv.URL + "/12/api", APIKey: "k"}} - results, err := c.Search(context.Background(), Query{Text: "radiohead", Categories: MusicCategories}) - if err != nil { - t.Fatalf("search: %v", err) - } - got := <-reqs - if got.URL.Path != "/12/api" || got.URL.Query().Get("t") != "search" || got.URL.Query().Get("apikey") != "k" { - t.Errorf("request = %s, want the feed URL with t=search and the key", got.URL) - } - if cats := got.URL.Query()["cat"]; len(cats) != 3 || cats[0] != "3000" { - t.Errorf("categories = %v, want the music categories", cats) - } - if len(results) != 2 { - t.Fatalf("results = %d, want 2 (the untitled item dropped)", len(results)) - } - first := results[0] - if first.InfoHash != "aabbccddeeff00112233445566778899aabbccdd" || first.Seeders != 12 || - first.Category != 3040 || first.Indexer != "RED" || first.DownloadURL == "" || first.Source != "torznab-2" { - t.Errorf("first = %+v", first) - } - if first.Published.IsZero() { - t.Error("pubDate was not parsed") - } - // The magnet's hash wins over a GUID shaped like another hash. - if results[1].Magnet == "" || results[1].InfoHash != "ffeeddccbbaa99887766554433221100ffeeddcc" { - t.Errorf("second = %+v, want the magnet's hash", results[1]) - } -} - -func TestTorznabSearch_KeepsAQueryAlreadyInTheFeedURL(t *testing.T) { - reqs := make(chan *http.Request, 1) - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - reqs <- r - _, _ = w.Write([]byte(``)) - })) - defer srv.Close() - - c := &Torznab{Config: TorznabConfig{FeedURL: srv.URL + "/api?passkey=p"}} - if _, err := c.Search(context.Background(), Query{Text: "x"}); err != nil { - t.Fatalf("search: %v", err) - } - got := <-reqs - if got.URL.Query().Get("passkey") != "p" || got.URL.Query().Get("q") != "x" { - t.Errorf("request = %s, want the pasted query kept and ours added", got.URL) - } -} - -// Newznab reports a refused key as HTTP 200 with an document. Read -// as a feed, that is an empty result set: search would quietly find nothing. -func TestTorznabSearch_ReadsAnErrorDocument(t *testing.T) { - for name, tc := range map[string]struct { - doc string - want error - }{ - "a wrong key": {``, ErrAuth}, - "anything else": {``, ErrUnreachable}, - } { - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - _, _ = w.Write([]byte(`` + tc.doc)) - })) - c := &Torznab{Config: TorznabConfig{FeedURL: srv.URL + "/api"}} - _, err := c.Search(context.Background(), Query{Text: "x"}) - srv.Close() - if !errors.Is(err, tc.want) { - t.Errorf("%s: err = %v, want %v", name, err, tc.want) - } - } -} - -func TestTorznabSearch_A429IsRateLimitedWithItsRetryAfter(t *testing.T) { - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.Header().Set("Retry-After", "90") - w.WriteHeader(http.StatusTooManyRequests) - })) - defer srv.Close() - - c := &Torznab{Config: TorznabConfig{FeedURL: srv.URL + "/api"}} - _, err := c.Search(context.Background(), Query{Text: "x"}) - rl, ok := AsRateLimited(err) - if !ok || rl.RetryAfter != 90*time.Second { - t.Fatalf("err = %v, want RateLimited for 90s", err) - } - if !errors.Is(err, ErrUnreachable) { - t.Error("a 429 should still read as unreachable to everything else") - } -} - -func TestTorznabSearch_NoAddressIsUnconfigured(t *testing.T) { - _, err := (&Torznab{}).Search(context.Background(), Query{Text: "x"}) - if !errors.Is(err, ErrUnconfigured) { - t.Fatalf("err = %v, want ErrUnconfigured", err) - } -} - -func TestTorznabDownload_RefusesWhatIsNotATorrent(t *testing.T) { - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch r.URL.Path { - case "/login": - _, _ = w.Write([]byte(`please log in`)) - case "/magnet": - _, _ = w.Write([]byte("magnet:?xt=urn:btih:abc")) - case "/redirect": - http.Redirect(w, r, "magnet:?xt=urn:btih:def", http.StatusFound) - default: - _, _ = w.Write([]byte("d8:announce0:e")) - } - })) - defer srv.Close() - - c := &Torznab{Config: TorznabConfig{FeedURL: srv.URL + "/api"}} - ctx := context.Background() - if _, err := c.Download(ctx, Result{Title: "t", DownloadURL: srv.URL + "/login"}); !errors.Is(err, ErrUnreachable) { - t.Errorf("an HTML page: err = %v, want ErrUnreachable", err) - } - for _, path := range []string{"/magnet", "/redirect"} { - _, err := c.Download(ctx, Result{Title: "t", DownloadURL: srv.URL + path}) - if _, ok := AsMagnet(err); !ok { - t.Errorf("%s: err = %v, want a MagnetLink", path, err) - } - } - body, err := c.Download(ctx, Result{Title: "t", DownloadURL: srv.URL + "/file"}) - if err != nil || string(body) != "d8:announce0:e" { - t.Errorf("a torrent: body %q err %v", body, err) - } -} - -func TestRetryAfter(t *testing.T) { - now := time.Date(2026, 10, 10, 0, 0, 0, 0, time.UTC) - for v, want := range map[string]time.Duration{ - "": 0, - "30": 30 * time.Second, - "soon": 0, - "Sat, 10 Oct 2026 00:02:00 GMT": 2 * time.Minute, - } { - h := http.Header{} - if v != "" { - h.Set("Retry-After", v) - } - if got := RetryAfter(h, now); got != want { - t.Errorf("RetryAfter(%q) = %v, want %v", v, got, want) - } - } -} diff --git a/internal/library/duplicate_merge.go b/internal/library/duplicate_merge.go index 54f4cd40..6bced5fd 100644 --- a/internal/library/duplicate_merge.go +++ b/internal/library/duplicate_merge.go @@ -206,9 +206,6 @@ func foldTrackInto( if _, err := tq.MergeRepointPlaybackErrors(ctx, dbq.MergeRepointPlaybackErrorsParams{SurvivorID: survivorID, LoserID: loserID}); err != nil { return f, fmt.Errorf("move playback errors: %w", err) } - if _, err := tq.MergeRepointRequests(ctx, dbq.MergeRepointRequestsParams{SurvivorID: survivorID, LoserID: loserID}); err != nil { - return f, fmt.Errorf("move request matches: %w", err) - } playlists, err := tq.MergeRepointPlaylistTracks(ctx, dbq.MergeRepointPlaylistTracksParams{SurvivorID: survivorID, LoserID: loserID}) if err != nil { diff --git a/internal/library/duplicate_resolve.go b/internal/library/duplicate_resolve.go index a41c0480..9130618a 100644 --- a/internal/library/duplicate_resolve.go +++ b/internal/library/duplicate_resolve.go @@ -38,7 +38,7 @@ import ( // - One file filling several of an album's track slots: MusicBrainz's track // lengths say which slot it belongs to, or AcoustID which song it is when // each copy was cut to its own slot's length, and the copies in the other -// slots are removed so the album is fetched again (duplicate_settle.go). +// slots are removed, leaving those slots missing (duplicate_settle.go). // - Anything else waits for an admin. const ( diff --git a/internal/library/duplicate_resolve_test.go b/internal/library/duplicate_resolve_test.go index 5cd41a74..afca68cc 100644 --- a/internal/library/duplicate_resolve_test.go +++ b/internal/library/duplicate_resolve_test.go @@ -379,7 +379,7 @@ func seedTrackList(t *testing.T, pool *pgxpool.Pool, album dbq.Album, songs map[ // One recording in two slots of an album: MusicBrainz says Void is as long as // the file and Reach is a minute longer, so the copy in Reach's slot goes. Its -// track stays, marked missing, for the album to be fetched again. +// track stays, marked missing, until a good copy arrives. func TestResolveDuplicates_RemovesACopyFromAnotherSongsSlot_Integration(t *testing.T) { f := newOneAlbumFixture(t, "acoustic", "Void", "Reach") seedTrackList(t, f.pool, f.album, map[string]int{"Void": 220500, "Reach": 281000}) @@ -420,7 +420,7 @@ func TestResolveDuplicates_RemovesACopyFromAnotherSongsSlot_Integration(t *testi // A slot emptied of another song's file folds into a copy of its own song the // album already holds (Savant's VOID DLC "Weeper" beside Void's). A copy under // the title that is not as long as the song is another song: the slot waits for -// a copy fetched again, and folds into that. +// a good copy, and folds into that. func TestResolveDuplicates_EmptiedSlotFoldsOnlyIntoItsOwnSong_Integration(t *testing.T) { for _, tc := range []struct { name string @@ -488,40 +488,21 @@ func TestResolveDuplicates_RemovesEveryCopyWhenNoSlotIsTheirs_Integration(t *tes // seedTrimmedAlbum is newOneAlbumFixture for one recording cut to both its // slots' lengths: identical audio, as Tormentor and as Record Shop, each as // long as its own slot's song, and the track list naming each slot's -// recording. The files came from a download Minstrel imported. +// recording. func seedTrimmedAlbum(t *testing.T) oneAlbumFixture { t.Helper() f := newOneAlbumFixture(t, "exact", "Tormentor", "Record Shop") ctx := context.Background() seedTrackList(t, f.pool, f.album, map[string]int{"Tormentor": 221000, "Record Shop": 219000}) - for _, stmt := range []struct { - sql string - args []any - }{ - {`UPDATE mb_release_tracks SET recording_mbid = 'rec-' || lower(replace(title, ' ', '-'))`, nil}, - {`INSERT INTO grabs (info_hash, name, key, client, save_path, origin, import_state, destination) - VALUES ('abc123', 'The Wombats - Void [WEB]', 'k', 'qbit', '/dl', 'schedule', 'imported', $1)`, - []any{filepath.Dir(f.aPath)}}, - } { - if _, err := f.pool.Exec(ctx, stmt.sql, stmt.args...); err != nil { - t.Fatalf("exec %q: %v", stmt.sql, err) - } + if _, err := f.pool.Exec(ctx, + `UPDATE mb_release_tracks SET recording_mbid = 'rec-' || lower(replace(title, ' ', '-'))`); err != nil { + t.Fatal(err) } return f } -func releaseBlocked(t *testing.T, pool *pgxpool.Pool, hash string) bool { - t.Helper() - var n int - if err := pool.QueryRow(context.Background(), - `SELECT count(*) FROM release_blocks WHERE info_hash = $1 AND reason = 'files'`, hash).Scan(&n); err != nil { - t.Fatal(err) - } - return n == 1 -} - -// AcoustID matched one copy to Tormentor's recording: that copy stays, Record -// Shop's goes, and the download they came from is set aside. +// AcoustID matched one copy to Tormentor's recording: that copy stays and +// Record Shop's goes. func TestResolveDuplicates_KeepsTheCopyAcoustIDNames_Integration(t *testing.T) { f := seedTrimmedAlbum(t) if _, err := f.pool.Exec(context.Background(), @@ -543,9 +524,6 @@ func TestResolveDuplicates_KeepsTheCopyAcoustIDNames_Integration(t *testing.T) { t.Fatalf("result %+v, tormentor exists %v, record shop exists %v; want only Record Shop's copy removed", res, exists(f.aPath), exists(f.bPath)) } - if !releaseBlocked(t, f.pool, "abc123") { - t.Error("the download the files came from was not set aside, so the album would be fetched from it again") - } } // Copies whose own lookups all name Record Shop's recording are that @@ -651,8 +629,8 @@ func TestResolveDuplicates_TrimmedCopiesAcoustIDCannotName_Integration(t *testin if err != nil { t.Fatal(err) } - if res.WrongFiles != 0 || !exists(f.aPath) || !exists(f.bPath) || releaseBlocked(t, f.pool, "abc123") { - t.Fatalf("result %+v; want nothing removed or set aside while AcoustID cannot answer", res) + if res.WrongFiles != 0 || !exists(f.aPath) || !exists(f.bPath) { + t.Fatalf("result %+v; want nothing removed while AcoustID cannot answer", res) } if status, _, _, _ := (resolveFixture{pool: f.pool, groupID: f.groupID}).group(t); status != "pending" { t.Errorf("group = %s, want pending", status) diff --git a/internal/library/duplicate_settle.go b/internal/library/duplicate_settle.go index d44b0c4f..55da3760 100644 --- a/internal/library/duplicate_settle.go +++ b/internal/library/duplicate_settle.go @@ -11,7 +11,6 @@ import ( "github.com/jackc/pgx/v5/pgtype" "github.com/jackc/pgx/v5/pgxpool" - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" "git.fabledsword.com/bvandeusen/minstrel/internal/audit" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" @@ -29,8 +28,8 @@ import ( // three of an album's tracks). The album's MusicBrainz track list // says how long each slot's song is. When exactly one slot's song is as // long as the file and every other slot's song is clearly not, the copies -// in the other slots are removed and their tracks marked missing, so the -// reacquisition sweeper wants the album back and a good copy fills them. +// in the other slots are removed and their tracks marked missing, until a +// good copy fills them. // When every slot's song is clearly not the file, every copy goes. // - named: the lengths cannot say (one recording filed as many tracks, each // cut to its own slot's length), but AcoustID @@ -40,9 +39,6 @@ import ( // when it is in none of their slots. Same audio that AcoustID does not // know at all is no copy's song, and every copy goes. // -// Whenever copies go, the release the files came from is set aside, so the -// album fetched again is not the same files. -// // Anything less certain stays with an admin, who settles it the same way // with their own answer (SettleWrongSlot). @@ -234,11 +230,8 @@ func settleWrongSlots( if !ok { continue } - // Set aside before the files go: a pass that dies between the two - // still does not fetch the same files again. - blocked := blockReleasesHolding(ctx, pool, logger, g) emptied := emptiedSlots(ctx, q, logger, g, losers, tracks) - n, err := removeWrongSlots(ctx, pool, logger, g, keep, losers, emptied, blocked, pgtype.UUID{}) + n, err := removeWrongSlots(ctx, pool, logger, g, keep, losers, emptied, pgtype.UUID{}) removed += n if err != nil { logger.Warn("duplicate resolve: removing wrong-slot copies failed", "group_id", syncpkg.FormatUUID(g.id), "err", err) @@ -261,19 +254,15 @@ var ErrNotWrongSlot = errors.New("library: duplicate group is not one recording type WrongSlotResult struct { Kept *MergedCopy Removed []MergedCopy - // Blocked names the releases set aside. None when the album was imported - // before Minstrel downloaded anything: the same files may come back. - Blocked []string } // SettleWrongSlot settles a one-album mismatch the resolver left for the // admin, with their answer in place of MusicBrainz's lengths and // AcoustID's. keepID is the copy whose title they say the recording is; an // invalid id means they cannot say, and every copy goes. Either way it is the -// resolver's own path: the release the files came from is set aside, the -// copies removed are marked missing, and the album is wanted again for them. -// A merge does none of that, and keeps one copy's title whether or not it is -// the audio's. +// resolver's own path: the copies removed are marked missing, and their slots +// wait for a good copy. A merge does none of that, and keeps one copy's title +// whether or not it is the audio's. // // Copies sharing the kept copy's title are one song held twice and stay, as in // wrongSlots: the next sweep finds the pair and the resolver merges it. @@ -320,9 +309,9 @@ func SettleWrongSlot( } } - res := WrongSlotResult{Blocked: blockReleasesHolding(ctx, pool, logger, g)} + var res WrongSlotResult emptied := emptiedSlots(ctx, q, logger, g, losers, tracks) - n, err := removeWrongSlots(ctx, pool, logger, g, keep, losers, emptied, res.Blocked, actorID) + n, err := removeWrongSlots(ctx, pool, logger, g, keep, losers, emptied, actorID) if keep >= 0 { res.Kept = &MergedCopy{TrackID: g.members[keep].TrackID, FilePath: g.members[keep].FilePath} } @@ -434,40 +423,9 @@ func groupRecordings( return ids, true } -// blockReleasesHolding sets aside the downloads a group's files were imported -// from, and returns their names. An album imported before Minstrel downloaded -// anything has none. -func blockReleasesHolding(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, g *resolveGroup) []string { - q := dbq.New(pool) - seen := map[string]bool{} - var names []string - for _, m := range g.members { - grabs, err := q.ListImportedGrabsHolding(ctx, m.FilePath) - if err != nil { - logger.Warn("duplicate resolve: finding the download a file came from failed", "path", m.FilePath, "err", err) - continue - } - for _, gr := range grabs { - if seen[gr.InfoHash] { - continue - } - seen[gr.InfoHash] = true - if err := acquire.NewMemory(pool).Block(ctx, dbq.BlockReleaseParams{ - InfoHash: gr.InfoHash, Name: gr.Name, Reason: acquire.BlockFiles, - Detail: "one recording fills several of the album's tracks.", WantID: gr.WantID, - }); err != nil { - logger.Warn("duplicate resolve: setting the release aside failed", "release", gr.Name, "err", err) - continue - } - names = append(names, gr.Name) - } - } - return names -} - // emptiedSlots is the losers whose song the album holds no other copy of: // no present track on it under that title as long as the song's slot. Those -// wait for a copy fetched again (migration 0095). The rest are the song held +// wait for a good copy to arrive. The rest are the song held // twice, and missing-pair folding folds them into the copy held: Savant's // VOID DLC "Weeper" beside Void's. A failed read counts every loser as // emptied, which only makes the album wait for a copy it may not need. @@ -515,17 +473,16 @@ func neverRead(ctx context.Context, q *dbq.Queries, rg string) bool { // removeWrongSlots deletes each loser's file, then marks its track missing in // the same transaction that closes the group. The row is kept, with its likes -// and plays: it is the slot, and a good copy fetched again takes it over. Its -// recording MBID is dropped, being the wrong file's, so missing-pair folding -// does not fold the slot into the copy that was kept. The slots in emptied -// are stamped to wait for a copy fetched again (emptiedSlots). blocked names -// the releases set aside, for the record. A file that cannot be removed stops +// and plays: it is the slot, and a good copy that arrives later takes it over. +// Its recording MBID is dropped, being the wrong file's, so missing-pair +// folding does not fold the slot into the copy that was kept. The slots in +// emptied are stamped to wait for a good copy (emptiedSlots). A file that cannot be removed stops // the group; the ones already gone are still marked, and the group waits for // the next pass. actorID is the admin settling it by hand, invalid for the // resolver. func removeWrongSlots( ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger, g *resolveGroup, keep int, losers []int, - emptied []pgtype.UUID, blocked []string, actorID pgtype.UUID, + emptied []pgtype.UUID, actorID pgtype.UUID, ) (int, error) { var gone []pgtype.UUID var removedFiles []map[string]string @@ -579,9 +536,6 @@ func removeWrongSlots( "artist": g.members[0].ArtistName, "removed": removedFiles, } - if len(blocked) > 0 { - details["blocked"] = blocked - } keptPath := "" if keep >= 0 { kept := g.members[keep] diff --git a/internal/library/duplicate_settle_test.go b/internal/library/duplicate_settle_test.go index bf14ffc3..b80a2510 100644 --- a/internal/library/duplicate_settle_test.go +++ b/internal/library/duplicate_settle_test.go @@ -3,7 +3,6 @@ package library import ( "context" "errors" - "path/filepath" "strings" "testing" @@ -15,19 +14,13 @@ import ( ) // undecidedSlot is one recording filed as Void and as Reach, two songs -// MusicBrainz lists at lengths that cannot tell them apart, from a download -// Minstrel imported: the resolver leaves it pending for an admin. +// MusicBrainz lists at lengths that cannot tell them apart: the resolver +// leaves it pending for an admin. func undecidedSlot(t *testing.T) (oneAlbumFixture, dbq.User) { t.Helper() f := newOneAlbumFixture(t, "acoustic", "Void", "Reach") ctx := context.Background() seedTrackList(t, f.pool, f.album, map[string]int{"Void": 220500, "Reach": 221000}) - if _, err := f.pool.Exec(ctx, - `INSERT INTO grabs (info_hash, name, key, client, save_path, origin, import_state, destination) - VALUES ('abc123', 'Savant - Void [WEB]', 'k', 'qbit', '/dl', 'schedule', 'imported', $1)`, - filepath.Dir(f.aPath)); err != nil { - t.Fatal(err) - } if res, err := ResolveDuplicates(ctx, f.pool, nil, "", true); err != nil || res.WrongFiles != 0 { t.Fatalf("resolve = %+v, %v; want the group left for the operator", res, err) } @@ -41,8 +34,8 @@ func undecidedSlot(t *testing.T) (oneAlbumFixture, dbq.User) { return f, u } -// emptiedTrack says whether a copy's track is marked missing and waiting for -// the album to be fetched again. +// emptiedTrack says whether a copy's track is marked missing and its slot +// emptied. func emptiedTrack(t *testing.T, f oneAlbumFixture, id pgtype.UUID) bool { t.Helper() var emptied bool @@ -72,9 +65,8 @@ func settledByHand(t *testing.T, f oneAlbumFixture, actor dbq.User) { } } -// "Get it again": the admin cannot say which song the recording is, so -// every copy goes, the download they came from is set aside, and both slots -// wait for the album fetched again. A merge would have kept one title. +// The admin cannot say which song the recording is, so every copy goes and +// both slots show as missing. A merge would have kept one title. func TestSettleWrongSlot_GetItAgainRemovesEveryCopy_Integration(t *testing.T) { f, op := undecidedSlot(t) ctx := context.Background() @@ -88,12 +80,9 @@ func TestSettleWrongSlot_GetItAgainRemovesEveryCopy_Integration(t *testing.T) { } for _, id := range []pgtype.UUID{f.a.ID, f.b.ID} { if !emptiedTrack(t, f, id) { - t.Errorf("track %s is not waiting to be fetched again", syncpkg.FormatUUID(id)) + t.Errorf("track %s is not marked missing with its slot emptied", syncpkg.FormatUUID(id)) } } - if !releaseBlocked(t, f.pool, "abc123") || len(res.Blocked) != 1 { - t.Errorf("blocked %v; want the download the files came from set aside", res.Blocked) - } settledByHand(t, f, op) if _, err := SettleWrongSlot(ctx, f.pool, nil, f.groupID, pgtype.UUID{}, op.ID); !errors.Is(err, ErrDuplicateGroupNotPending) { @@ -102,7 +91,7 @@ func TestSettleWrongSlot_GetItAgainRemovesEveryCopy_Integration(t *testing.T) { } // "This one is Reach": the copy the admin names stays, and only Void's slot -// is emptied and wanted again. +// is emptied. func TestSettleWrongSlot_KeepsTheCopyTheOperatorNames_Integration(t *testing.T) { f, op := undecidedSlot(t) res, err := SettleWrongSlot(context.Background(), f.pool, nil, f.groupID, f.b.ID, op.ID) @@ -114,10 +103,7 @@ func TestSettleWrongSlot_KeepsTheCopyTheOperatorNames_Integration(t *testing.T) res, exists(f.aPath), exists(f.bPath)) } if !emptiedTrack(t, f, f.a.ID) { - t.Error("Void's track is not waiting to be fetched again") - } - if !releaseBlocked(t, f.pool, "abc123") { - t.Error("the download the files came from is not set aside") + t.Error("Void's track is not marked missing with its slot emptied") } settledByHand(t, f, op) } @@ -141,7 +127,7 @@ func TestSettleWrongSlot_RefusesWhatItCannotSettle_Integration(t *testing.T) { if _, err := SettleWrongSlot(ctx, f.pool, nil, f.groupID, other, op.ID); !errors.Is(err, ErrSurvivorNotInGroup) { t.Errorf("err = %v, want ErrSurvivorNotInGroup", err) } - if !exists(f.aPath) || !exists(f.bPath) || releaseBlocked(t, f.pool, "abc123") { + if !exists(f.aPath) || !exists(f.bPath) { t.Error("something changed") } }) diff --git a/internal/library/library_paths.go b/internal/library/library_paths.go index fe0a9a5f..2e0eda22 100644 --- a/internal/library/library_paths.go +++ b/internal/library/library_paths.go @@ -109,12 +109,6 @@ func (s *Scanner) SetRoots(ctx context.Context, changes []RootChange) (RerootRes if _, err := tq.RerootAlbumCoverPaths(ctx, dbq.RerootAlbumCoverPathsParams{NewRoot: m.Path, OldRoot: m.Was}); err != nil { return fmt.Errorf("reroot covers: %w", err) } - if _, err := tq.RerootGrabDestinations(ctx, dbq.RerootGrabDestinationsParams{NewRoot: m.Path, OldRoot: m.Was}); err != nil { - return fmt.Errorf("reroot downloads: %w", err) - } - if _, err := tq.RerootImportRoot(ctx, dbq.RerootImportRootParams{NewRoot: m.Path, OldRoot: m.Was}); err != nil { - return fmt.Errorf("reroot import folder: %w", err) - } } _, err := tq.UpdateLibraryScanPaths(ctx, roots) return err diff --git a/internal/library/mbids.go b/internal/library/mbids.go index 85bad411..68edae03 100644 --- a/internal/library/mbids.go +++ b/internal/library/mbids.go @@ -13,8 +13,8 @@ type TagMBIDs struct { // Release is musicbrainz_albumid: one edition, what albums.mbid stores. Release string Artist string - // ReleaseGroup is musicbrainz_releasegroupid. Wants name albums by - // release group, so re-acquisition and request completion key on it. + // ReleaseGroup is musicbrainz_releasegroupid. The duplicate resolver + // reads an album's MusicBrainz track lists by it. ReleaseGroup string // Recording is what tracks.mbid stores and the ListenBrainz Labs // similar-recordings API keys on. diff --git a/internal/library/scanner.go b/internal/library/scanner.go index f2cff895..4ed88c71 100644 --- a/internal/library/scanner.go +++ b/internal/library/scanner.go @@ -57,9 +57,9 @@ var audioExtensions = map[string]bool{ // to new files only because taste_profile.sql reads tracks.genre directly, // so a half-repaired library would carry both spellings as separate tags. // 3: the MusicBrainz release-group id is read into albums.release_group_mbid. -// Wants name albums by release group, not by the release id -// albums.mbid holds, so re-acquisition and request completion need it for -// the albums already in the library, not only for files added from now on. +// The duplicate resolver reads an album's track lists by release group, +// not by the release id albums.mbid holds, so it needs it for the albums +// already in the library, not only for files added from now on. // 4: every value of a repeated genre field is read for FLAC, Ogg Vorbis, Opus // and MP4. dhowden/tag kept only the last, and most FLACs that carry // genres declare more than one. diff --git a/internal/lifecycle/lifecycle.go b/internal/lifecycle/lifecycle.go deleted file mode 100644 index 23102f4d..00000000 --- a/internal/lifecycle/lifecycle.go +++ /dev/null @@ -1,682 +0,0 @@ -// Package lifecycle looks after a download from the grab to its removal. -// -// - A download the client has failed is given up at once. -// - One nobody has shared, or still fetching its file list, after the -// stall setting is retried: the trackers asked again and the files -// re-read, three times a pass apart. Then it is given up. -// - Giving up removes the download with its files, holds the release back -// for a week, ends the grab and searches for the album again. There is -// no limit per album: what stops it churning is the hold on each release. -// - A running download the client no longer lists, for half an hour, ends -// and its album is searched again. The release is not held back: nothing -// was wrong with it. -// - An imported download seeds (its files were copied) until its share -// ratio reaches the setting or its days are up, then is removed. -// -// Every condition and every act goes in the intervention ledger, one open -// row per (client, download, condition), so the Activity tab can say what -// is wrong with a download and what was done. -package lifecycle - -import ( - "context" - "errors" - "fmt" - "log/slog" - "strings" - "sync" - "time" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -// Conditions, matching download_interventions_trigger_check. -const ( - NoPeers = "no_peers" - NoMetadata = "no_metadata" - ClientError = "client_error" - ClientUnreachable = "client_unreachable" - Gone = "gone" - NotRemoved = "not_removed" -) - -// Acts, matching download_interventions_action_check. -const ( - actRetried = "retried" - actGivenUp = "given_up" - actRemoved = "removed" -) - -// End reasons a grab is given with, matching grabs_end_reason_check. -const ( - EndStalled = "stalled" - EndFailed = "failed" - EndAbandoned = "abandoned" - EndGone = "gone" -) - -const ( - // interval: three retries a pass apart is half an hour of asking before - // a stalled download is given up. - interval = 10 * time.Minute - passTimeout = 5 * time.Minute - // Retries is how many times one stuck download is nudged first. - Retries = 3 - // StallBlock is how long a release given up on is held back. - StallBlock = 7 * 24 * time.Hour - // goneAfter: a client restarting can answer without a download for a - // while; half an hour missing is gone. - goneAfter = 30 * time.Minute - // goneWindow: a release gone twice within it is held back. - goneWindow = 7 * 24 * time.Hour -) - -// running are the import states of a grab still downloading: the lifecycle -// acts only on these, so an import under way is never pulled from under. -var running = []string{"", "downloading", "waiting"} - -// Settings are the limits, from acquisition_settings. -type Settings struct { - // SeedRatio is the share ratio an imported download seeds to; 0 removes - // it once imported. - SeedRatio float64 - // SeedTime is the longest it seeds; 0 is no limit. - SeedTime time.Duration - // Patience is how long a download may sit with nobody sharing it before - // it is retried, then given up; 0 never gives up for that. - Patience time.Duration -} - -// Defaults are FL's rulings. -var Defaults = Settings{SeedRatio: 2, SeedTime: 14 * 24 * time.Hour, Patience: 6 * time.Hour} - -// Service runs the lifecycle passes. -type Service struct { - pool *pgxpool.Pool - // ClientFor builds a download client by its code. Defaults to - // acquire.DownloadClientFor. - ClientFor func(ctx context.Context, code string) (downloader.Driver, error) - // Now is the clock; nil is time.Now. - Now func() time.Time - logger *slog.Logger - mu sync.Mutex // one pass at a time -} - -// New returns a Service. -func New(pool *pgxpool.Pool, logger *slog.Logger) *Service { - if logger == nil { - logger = slog.Default() - } - return &Service{pool: pool, logger: logger, - ClientFor: func(ctx context.Context, code string) (downloader.Driver, error) { - return acquire.DownloadClientFor(ctx, pool, code) - }} -} - -func (s *Service) now() time.Time { - if s.Now != nil { - return s.Now() - } - return time.Now() -} - -// Run passes a minute after start, then every ten minutes, until ctx ends. -// Each pass is bounded, so a client that never answers costs one pass. -func (s *Service) Run(ctx context.Context) { - first := time.NewTimer(time.Minute) - defer first.Stop() - select { - case <-ctx.Done(): - return - case <-first.C: - } - t := time.NewTicker(interval) - defer t.Stop() - for { - rep := s.Pass(ctx) - if rep.Retried+rep.GivenUp+rep.Gone+rep.Released+rep.Removed > 0 || rep.Error != "" { - s.logger.Info("lifecycle: pass", "retried", rep.Retried, "given_up", rep.GivenUp, "gone", rep.Gone, - "released", rep.Released, "removed", rep.Removed, "raised", rep.Raised, "settled", rep.Settled, - "error", rep.Error) - } - select { - case <-ctx.Done(): - return - case <-t.C: - } - } -} - -// Report is one pass. -type Report struct { - // Raised and Settled count ledger writes; the rest count acts. - Raised, Settled int - Retried, GivenUp, Gone, Released, Removed int - Error string -} - -// Pass looks at every running download, every seeding one and every one -// left behind, once. -func (s *Service) Pass(ctx context.Context) Report { - s.mu.Lock() - defer s.mu.Unlock() - ctx, cancel := context.WithTimeout(ctx, passTimeout) - defer cancel() - var rep Report - q := dbq.New(s.pool) - set := s.settings(ctx, q) - - if _, err := q.DeleteExpiredBlocks(ctx); err != nil { - s.logger.Warn("lifecycle: lift expired holds", "err", err) - } - if n, err := q.SettleEndedGrabs(ctx); err == nil { - rep.Settled += int(n) - } - live, err := q.ListLiveGrabs(ctx) - if err != nil { - rep.Error = err.Error() - return rep - } - seeds, err := q.ListSeedingGrabs(ctx) - if err != nil { - rep.Error = err.Error() - return rep - } - left, err := q.ListLeftovers(ctx) - if err != nil { - rep.Error = err.Error() - return rep - } - - work := map[string]*clientWork{} - var order []string - of := func(code string) *clientWork { - if w, ok := work[code]; ok { - return w - } - order = append(order, code) - work[code] = &clientWork{} - return work[code] - } - for _, g := range live { - if g.InfoHash != "" { - of(g.Client).live = append(of(g.Client).live, g) - } - } - for _, g := range seeds { - if g.InfoHash != "" { - of(g.Client).seeds = append(of(g.Client).seeds, g) - } - } - for _, l := range left { - of(l.Client).left = append(of(l.Client).left, l) - } - for _, code := range order { - if ctx.Err() != nil { - break - } - s.passClient(ctx, q, code, work[code], set, &rep) - } - return rep -} - -type clientWork struct { - live []dbq.ListLiveGrabsRow - seeds []dbq.ListSeedingGrabsRow - left []dbq.DownloadIntervention -} - -func (s *Service) settings(ctx context.Context, q *dbq.Queries) Settings { - row, err := q.GetAcquisitionSettings(ctx) - if err != nil { - // A blip keeps the defaults rather than stopping the pass. - return Defaults - } - return Settings{ - SeedRatio: float64(row.SeedRatio), - SeedTime: time.Duration(row.SeedDays) * 24 * time.Hour, - Patience: time.Duration(row.StallHours) * time.Hour, - } -} - -func (s *Service) passClient(ctx context.Context, q *dbq.Queries, code string, w *clientWork, set Settings, rep *Report) { - d, err := s.ClientFor(ctx, code) - if err != nil { - s.raise(ctx, q, code, "", pgtype.UUID{}, "", ClientUnreachable, firstLine(err.Error()), rep) - return - } - var hashes []string - for _, g := range w.live { - hashes = append(hashes, g.InfoHash) - } - for _, g := range w.seeds { - hashes = append(hashes, g.InfoHash) - } - for _, l := range w.left { - hashes = append(hashes, l.InfoHash) - } - ts, err := d.Torrents(ctx, hashes...) - if err != nil { - // Nothing about any one download is observed through a client that - // cannot answer. - s.raise(ctx, q, code, "", pgtype.UUID{}, "", ClientUnreachable, firstLine(err.Error()), rep) - return - } - for _, t := range ts { - if t.EngineDown != "" { - s.raise(ctx, q, code, "", pgtype.UUID{}, "", ClientUnreachable, t.EngineDown, rep) - return - } - } - s.settle(ctx, q, code, "", ClientUnreachable, rep) - - byHash := make(map[string]downloader.Torrent, len(ts)) - for _, t := range ts { - byHash[indexer.NormalizeHash(t.Hash)] = t - } - // ABSENCE IS ONLY A MEASUREMENT FROM A CLIENT THAT LISTS SOMETHING. An - // engine switched off, or a store that would not read, answers with an - // empty list; then nothing is concluded from what is missing. - listed := len(ts) > 0 - if !listed { - all, err := d.Torrents(ctx) - listed = err == nil && len(all) > 0 - } - - for _, l := range w.left { - t, ok := byHash[indexer.NormalizeHash(l.InfoHash)] - s.removeAgain(ctx, q, d, l, t, ok, listed, rep) - } - for _, g := range w.live { - t, ok := byHash[indexer.NormalizeHash(g.InfoHash)] - s.judge(ctx, q, d, g, t, ok, listed, set, rep) - } - for _, g := range w.seeds { - t, ok := byHash[indexer.NormalizeHash(g.InfoHash)] - s.seed(ctx, q, d, g, t, ok, set, rep) - } -} - -// judge decides what, if anything, is wrong with one running download, and -// answers each condition either way: raised when true, settled when not. -func (s *Service) judge(ctx context.Context, q *dbq.Queries, d downloader.Driver, g dbq.ListLiveGrabsRow, - t downloader.Torrent, ok, listed bool, set Settings, rep *Report) { - if !isRunning(g.ImportState) { - return // the importer's, or a person's - } - if !ok { - if !listed { - return - } - row, raised := s.raise(ctx, q, g.Client, g.InfoHash, g.ID, g.Name, Gone, - "The download client no longer lists it.", rep) - if raised && s.now().Sub(row.FirstSeen.Time) >= goneAfter { - s.endGone(ctx, q, g, rep) - } - return - } - s.settle(ctx, q, g.Client, g.InfoHash, Gone, rep) - - if t.Done() { - // Finished: whatever was wrong while it downloaded is over, and the - // import is the importer's. - for _, c := range []string{ClientError, NoMetadata, NoPeers} { - s.settle(ctx, q, g.Client, g.InfoHash, c, rep) - } - return - } - - // The client's own verdict, given up on at once: it has already made - // the decision a retry would ask about. - if t.Failed() { - why := t.FailReason - if why == "" { - why = fmt.Sprintf("the download client reports it %q", t.State) - } - if _, raised := s.raise(ctx, q, g.Client, g.InfoHash, g.ID, g.Name, ClientError, why, rep); raised { - s.giveUp(ctx, q, d, g, ClientError, EndFailed, why, rep) - } - return - } - s.settle(ctx, q, g.Client, g.InfoHash, ClientError, rep) - - // Below here the stall setting applies, measured from the grab. With it - // off nothing is raised and what was raised clears. - age := s.now().Sub(g.GrabbedAt.Time) - patient := set.Patience > 0 && age > set.Patience - - if patient && t.Fetching() { - row, raised := s.raise(ctx, q, g.Client, g.InfoHash, g.ID, g.Name, NoMetadata, - fmt.Sprintf("Still fetching its file list after %s.", span(age)), rep) - if raised { - s.ladder(ctx, q, d, g, NoMetadata, row.Remediations, age, rep) - } - } else { - s.settle(ctx, q, g.Client, g.InfoHash, NoMetadata, rep) - } - // Not while fetching: a torrent with no file list has no swarm to be - // alone in. - if patient && !t.Fetching() && t.Alone() { - row, raised := s.raise(ctx, q, g.Client, g.InfoHash, g.ID, g.Name, NoPeers, - fmt.Sprintf("Nobody has shared it in %s; %.0f%% downloaded.", span(age), t.Progress*100), rep) - if raised { - s.ladder(ctx, q, d, g, NoPeers, row.Remediations, age, rep) - } - } else { - s.settle(ctx, q, g.Client, g.InfoHash, NoPeers, rep) - } -} - -// ladder retries a stuck download until the retries are spent, then gives -// it up. The rung is the ledger row's, so a restart hands out no more. -func (s *Service) ladder(ctx context.Context, q *dbq.Queries, d downloader.Driver, g dbq.ListLiveGrabsRow, - trigger string, done int32, age time.Duration, rep *Report) { - if int(done) < Retries { - // Counted before it happens: a retry that happened and was not - // counted is a bound that never binds. - n, err := q.CountRemediation(ctx, dbq.CountRemediationParams{ - Client: g.Client, InfoHash: g.InfoHash, Trigger: trigger, Action: actRetried}) - if err != nil { - return - } - // Exchange schedules its own announces and has no recheck: for it a - // retry is the time between passes, and the ladder still climbs. - if err := d.Reannounce(ctx, g.InfoHash); err != nil && !errors.Is(err, downloader.ErrUnsupported) { - s.logger.Warn("lifecycle: reannounce", "grab", g.Name, "err", err) - } - if err := d.Recheck(ctx, g.InfoHash); err != nil && !errors.Is(err, downloader.ErrUnsupported) { - s.logger.Warn("lifecycle: recheck", "grab", g.Name, "err", err) - } - s.logger.Info("lifecycle: retried a stalled download", "grab", g.Name, "trigger", trigger, "attempt", n) - rep.Retried++ - return - } - why := fmt.Sprintf("taken %s ago and never finished: %s", span(age), reasonFor(trigger)) - s.giveUp(ctx, q, d, g, trigger, EndStalled, why, rep) -} - -// giveUp ends the grab, holds the release back for StallBlock, removes the -// download with its files and searches for the album again. -func (s *Service) giveUp(ctx context.Context, q *dbq.Queries, d downloader.Driver, g dbq.ListLiveGrabsRow, - trigger, end, why string, rep *Report) { - until := s.now().Add(StallBlock) - detail := capital(why) + fmt.Sprintf(". Held back until %s; the album is searched for again.", until.Format("2 Jan")) - // The update is the lock: an import that claimed the download meanwhile - // keeps it. - if _, err := q.GiveUpGrab(ctx, dbq.GiveUpGrabParams{ID: g.ID, EndReason: end, EndDetail: detail, States: running}); err != nil { - return - } - // Held back before it is removed: if the pass dies between the two, the - // release is still not taken again. - if err := acquire.NewMemory(s.pool).Block(ctx, dbq.BlockReleaseParams{ - InfoHash: g.InfoHash, Name: g.Name, Reason: acquire.BlockStalled, Detail: capital(why) + ".", - WantID: g.WantID, ExpiresAt: pgtype.Timestamptz{Time: until, Valid: true}, - }); err != nil { - s.logger.Warn("lifecycle: hold the release back", "grab", g.Name, "err", err) - } - removed := s.remove(ctx, q, d, g.Client, g.InfoHash, g.ID, g.Name, rep) - if g.WantID.Valid { - if err := q.SearchWantAgain(ctx, g.WantID); err != nil { - s.logger.Warn("lifecycle: search again", "grab", g.Name, "err", err) - } - } - if _, err := q.CountRemediation(ctx, dbq.CountRemediationParams{ - Client: g.Client, InfoHash: g.InfoHash, Trigger: trigger, Action: actGivenUp}); err != nil { - s.logger.Warn("lifecycle: stamp the ledger", "grab", g.Name, "err", err) - } - if removed { - if n, err := q.SettleInterventionsFor(ctx, dbq.SettleInterventionsForParams{ - Client: g.Client, InfoHash: g.InfoHash}); err == nil { - rep.Settled += int(n) - } - } - s.logger.Info("lifecycle: gave up on a download", "grab", g.Name, "trigger", trigger, "why", why) - rep.GivenUp++ -} - -// endGone ends a grab whose download left the client, and searches again. -// The first time, the release is not held back: nothing was found wrong with -// it. A release that leaves its client again within goneWindow is held back for -// acquire.GoneBlock: whatever keeps the client from listing it would otherwise -// have it taken again on every pass. -func (s *Service) endGone(ctx context.Context, q *dbq.Queries, g dbq.ListLiveGrabsRow, rep *Report) { - detail := "The download client no longer lists it: removed outside Minstrel. The album is searched for again." - before, err := q.CountGoneGrabs(ctx, dbq.CountGoneGrabsParams{ - InfoHash: g.InfoHash, Since: pgtype.Timestamptz{Time: s.now().Add(-goneWindow), Valid: true}}) - if err != nil { - s.logger.Warn("lifecycle: count earlier gone grabs", "grab", g.Name, "err", err) - } - again := before > 0 - until := s.now().Add(acquire.GoneBlock) - if again { - detail = fmt.Sprintf("The download client no longer lists it, again: held back until %s; the album is searched for again.", - until.Format("2 Jan 15:04")) - } - if _, err := q.GiveUpGrab(ctx, dbq.GiveUpGrabParams{ID: g.ID, EndReason: EndGone, EndDetail: detail, States: running}); err != nil { - return - } - if again { - if err := acquire.NewMemory(s.pool).Block(ctx, dbq.BlockReleaseParams{ - InfoHash: g.InfoHash, Name: g.Name, Reason: acquire.BlockGone, - Detail: "It left its download client twice without finishing.", - WantID: g.WantID, ExpiresAt: pgtype.Timestamptz{Time: until, Valid: true}, - }); err != nil { - s.logger.Warn("lifecycle: hold the release back", "grab", g.Name, "err", err) - } - } - if g.WantID.Valid { - if err := q.SearchWantAgain(ctx, g.WantID); err != nil { - s.logger.Warn("lifecycle: search again", "grab", g.Name, "err", err) - } - } - if _, err := q.CountRemediation(ctx, dbq.CountRemediationParams{ - Client: g.Client, InfoHash: g.InfoHash, Trigger: Gone, Action: actGivenUp}); err != nil { - s.logger.Warn("lifecycle: stamp the ledger", "grab", g.Name, "err", err) - } - if n, err := q.SettleInterventionsFor(ctx, dbq.SettleInterventionsForParams{ - Client: g.Client, InfoHash: g.InfoHash}); err == nil { - rep.Settled += int(n) - } - s.logger.Info("lifecycle: a download left its client", "grab", g.Name) - rep.Gone++ -} - -// seed removes an imported download once it has shared enough. -// -// Absence is not a verdict: a restarting client answers with a partial list. -// A seeding download missing from it is left until its time is up, then -// recorded as released; there is nothing to delete. -func (s *Service) seed(ctx context.Context, q *dbq.Queries, d downloader.Driver, g dbq.ListSeedingGrabsRow, - t downloader.Torrent, ok bool, set Settings, rep *Report) { - overdue := set.SeedTime > 0 && g.ImportedAt.Valid && s.now().Sub(g.ImportedAt.Time) >= set.SeedTime - if !ok { - if overdue { - s.release(ctx, q, g.ID, rep) - } - return - } - if err := q.SetGrabRatio(ctx, dbq.SetGrabRatioParams{ID: g.ID, Ratio: float32(t.Ratio)}); err != nil { - s.logger.Warn("lifecycle: record the ratio", "grab", g.Name, "err", err) - } - if t.Ratio < set.SeedRatio && !overdue { - return - } - s.remove(ctx, q, d, g.Client, g.InfoHash, g.ID, g.Name, rep) - s.logger.Info("lifecycle: seeded enough, removed the download", "grab", g.Name, "ratio", t.Ratio, "overdue", overdue) - s.release(ctx, q, g.ID, rep) -} - -func (s *Service) release(ctx context.Context, q *dbq.Queries, id pgtype.UUID, rep *Report) { - if err := q.ReleaseSeed(ctx, id); err != nil { - s.logger.Warn("lifecycle: record the seeding ended", "err", err) - return - } - rep.Released++ -} - -// remove deletes a download and its files. A client that refuses leaves an -// open not_removed row, which every pass tries again. -func (s *Service) remove(ctx context.Context, q *dbq.Queries, d downloader.Driver, code, hash string, - grab pgtype.UUID, name string, rep *Report) bool { - if err := d.Delete(ctx, true, hash); err != nil { - s.logger.Warn("lifecycle: remove the download", "grab", name, "err", err) - s.raise(ctx, q, code, hash, grab, name, NotRemoved, notRemoved(err), rep) - return false - } - return true -} - -// removeAgain asks the client once more to remove a download left behind, -// unless it has been taken again since. -func (s *Service) removeAgain(ctx context.Context, q *dbq.Queries, d downloader.Driver, l dbq.DownloadIntervention, - t downloader.Torrent, ok, listed bool, rep *Report) { - if !ok { - if listed { - s.settle(ctx, q, l.Client, l.InfoHash, NotRemoved, rep) - } - return - } - live, err := q.LiveGrabOnHash(ctx, l.InfoHash) - if err != nil { - return - } - if live { - s.settle(ctx, q, l.Client, l.InfoHash, NotRemoved, rep) - return - } - if err := d.Delete(ctx, true, l.InfoHash); err != nil { - s.raise(ctx, q, l.Client, l.InfoHash, l.GrabID, t.Name, NotRemoved, notRemoved(err), rep) - return - } - if _, err := q.CountRemediation(ctx, dbq.CountRemediationParams{ - Client: l.Client, InfoHash: l.InfoHash, Trigger: NotRemoved, Action: actRemoved}); err != nil { - s.logger.Warn("lifecycle: stamp the ledger", "hash", l.InfoHash, "err", err) - } - s.logger.Info("lifecycle: removed a download left behind", "name", l.Name) - rep.Removed++ -} - -// raise records a condition or agrees with the open row that says it. The -// bool is false when the write failed: then nothing is acted on, since -// without the row's count there is no bound. -func (s *Service) raise(ctx context.Context, q *dbq.Queries, code, hash string, grab pgtype.UUID, name, trigger, - detail string, rep *Report) (dbq.DownloadIntervention, bool) { - row, err := q.RaiseIntervention(ctx, dbq.RaiseInterventionParams{ - Client: code, InfoHash: hash, GrabID: grab, Name: name, Trigger: trigger, Detail: detail}) - if err != nil { - s.logger.Warn("lifecycle: record", "client", code, "hash", hash, "trigger", trigger, "err", err) - return row, false - } - rep.Raised++ - return row, true -} - -func (s *Service) settle(ctx context.Context, q *dbq.Queries, code, hash, trigger string, rep *Report) { - n, err := q.SettleIntervention(ctx, dbq.SettleInterventionParams{Client: code, InfoHash: hash, Trigger: trigger}) - if err != nil { - s.logger.Warn("lifecycle: settle", "client", code, "hash", hash, "trigger", trigger, "err", err) - return - } - rep.Settled += int(n) -} - -// ErrNotAbandonable: the download is not running; an import, or a person's -// decision on one, has it. -var ErrNotAbandonable = errors.New("lifecycle: that download is not running") - -// Abandon is a person giving up on a running download: the release is set -// aside for good (their word about it), the download removed with its files, -// and the album searched for again. -func (s *Service) Abandon(ctx context.Context, grabID, by pgtype.UUID) (dbq.Grab, error) { - q := dbq.New(s.pool) - g, err := q.GetGrab(ctx, grabID) - if err != nil { - return dbq.Grab{}, err - } - if g.EndedAt.Valid || !isRunning(g.ImportState) { - return dbq.Grab{}, ErrNotAbandonable - } - const why = "Given up by you before it finished. The album is searched for again." - ended, err := q.GiveUpGrab(ctx, dbq.GiveUpGrabParams{ID: g.ID, EndReason: EndAbandoned, EndDetail: why, States: running}) - if err != nil { - return dbq.Grab{}, ErrNotAbandonable - } - if g.InfoHash != "" { - if err := acquire.NewMemory(s.pool).Block(ctx, dbq.BlockReleaseParams{ - InfoHash: g.InfoHash, Name: g.Name, Reason: acquire.BlockOperator, Detail: "Given up by you before it finished.", - WantID: g.WantID, BlockedBy: by, - }); err != nil { - s.logger.Warn("lifecycle: set the release aside", "grab", g.Name, "err", err) - } - var rep Report - if d, err := s.ClientFor(ctx, g.Client); err != nil { - s.raise(ctx, q, g.Client, g.InfoHash, g.ID, g.Name, NotRemoved, notRemoved(err), &rep) - } else if s.remove(ctx, q, d, g.Client, g.InfoHash, g.ID, g.Name, &rep) { - _, _ = q.SettleInterventionsFor(ctx, dbq.SettleInterventionsForParams{Client: g.Client, InfoHash: g.InfoHash}) - } - } - if g.WantID.Valid { - if err := q.SearchWantAgain(ctx, g.WantID); err != nil { - s.logger.Warn("lifecycle: search again", "grab", g.Name, "err", err) - } - } - return ended, nil -} - -func isRunning(state string) bool { - for _, r := range running { - if state == r { - return true - } - } - return false -} - -// reasonFor is a condition in a person's words. -func reasonFor(trigger string) string { - switch trigger { - case NoPeers: - return "nobody was sharing it" - case NoMetadata: - return "its file list never arrived" - case ClientError: - return "the download client gave up on it" - } - return "it stalled" -} - -func notRemoved(err error) string { - return "Given up on, but the download client would not remove it: " + firstLine(err.Error()) + - ". Minstrel asks again every ten minutes." -} - -// span is a duration as a person says it. -func span(d time.Duration) string { - switch { - case d < 2*time.Hour: - return fmt.Sprintf("%d minutes", int(d.Minutes())) - case d < 48*time.Hour: - return fmt.Sprintf("%d hours", int(d.Hours())) - } - return fmt.Sprintf("%d days", int(d.Hours()/24)) -} - -func capital(s string) string { - if s == "" { - return s - } - return strings.ToUpper(s[:1]) + s[1:] -} - -func firstLine(s string) string { - if i := strings.IndexByte(s, '\n'); i >= 0 { - return strings.TrimSpace(s[:i]) - } - return strings.TrimSpace(s) -} diff --git a/internal/lifecycle/lifecycle_test.go b/internal/lifecycle/lifecycle_test.go deleted file mode 100644 index b5e9277b..00000000 --- a/internal/lifecycle/lifecycle_test.go +++ /dev/null @@ -1,488 +0,0 @@ -package lifecycle - -import ( - "context" - "errors" - "io" - "log/slog" - "os" - "strings" - "testing" - "time" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/db" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" - "git.fabledsword.com/bvandeusen/minstrel/internal/downloader" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" -) - -func newPool(t *testing.T) *pgxpool.Pool { - t.Helper() - if testing.Short() { - t.Skip("skipping integration test in -short mode") - } - dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") - if dsn == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { - t.Fatalf("migrate: %v", err) - } - pool, err := pgxpool.New(context.Background(), dsn) - if err != nil { - t.Fatalf("pool: %v", err) - } - t.Cleanup(pool.Close) - dbtest.ResetDB(t, pool) - return pool -} - -const ( - hash = "0123456789abcdef0123456789abcdef01234567" - other = "fedcba9876543210fedcba9876543210fedcba98" -) - -// fakeClient lists the torrents it holds and records what it is asked. -type fakeClient struct { - downloader.Driver - held map[string]downloader.Torrent - err error - deleteErr error - deleted []string - reannounce int - recheck int -} - -func (c *fakeClient) Torrents(_ context.Context, hashes ...string) ([]downloader.Torrent, error) { - if c.err != nil { - return nil, c.err - } - var out []downloader.Torrent - if len(hashes) == 0 { - for _, t := range c.held { - out = append(out, t) - } - return out, nil - } - for _, h := range hashes { - if t, ok := c.held[h]; ok { - out = append(out, t) - } - } - return out, nil -} - -func (c *fakeClient) Delete(_ context.Context, _ bool, hashes ...string) error { - if c.deleteErr != nil { - return c.deleteErr - } - c.deleted = append(c.deleted, hashes...) - for _, h := range hashes { - delete(c.held, h) - } - return nil -} - -func (c *fakeClient) Reannounce(context.Context, ...string) error { c.reannounce++; return nil } -func (c *fakeClient) Recheck(context.Context, ...string) error { c.recheck++; return nil } - -type stack struct { - q *dbq.Queries - s *Service - client *fakeClient - want dbq.Want - grab dbq.Grab - clock time.Time -} - -// newStack wants RAM and has a grab of it running in a fake client holding -// t under hash. -func newStack(t *testing.T, tor downloader.Torrent) *stack { - t.Helper() - ctx := context.Background() - pool := newPool(t) - q := dbq.New(pool) - w, err := wanted.WantWith(ctx, q, wanted.WantParams{GroupMBID: "rg-ram", ArtistMBID: "mb-dp", ArtistName: "Daft Punk", - Title: "Random Access Memories", PrimaryType: "Album", FirstReleaseDate: "2013-05-17", Reason: wanted.ReasonRequest}) - if err != nil { - t.Fatal(err) - } - g, err := acquire.NewMemory(pool).Grabbed(ctx, dbq.InsertGrabParams{WantID: w.ID, InfoHash: hash, - Name: "Daft Punk - RAM [FLAC]", Key: hash, Client: "qbittorrent", SavePath: "/downloads", Origin: acquire.OriginSchedule}, - pgtype.UUID{}, "") - if err != nil { - t.Fatal(err) - } - if err := q.MarkWantSearched(ctx, w.ID); err != nil { - t.Fatal(err) - } - tor.Hash = hash - st := &stack{q: q, client: &fakeClient{held: map[string]downloader.Torrent{hash: tor}}, want: w, grab: g, - clock: time.Now()} - st.s = New(pool, slog.New(slog.NewTextHandler(io.Discard, nil))) - st.s.ClientFor = func(context.Context, string) (downloader.Driver, error) { return st.client, nil } - st.s.Now = func() time.Time { return st.clock } - return st -} - -func (st *stack) grabNow(t *testing.T) dbq.Grab { - t.Helper() - g, err := st.q.GetGrab(context.Background(), st.grab.ID) - if err != nil { - t.Fatal(err) - } - return g -} - -func (st *stack) searchedAgain(t *testing.T) bool { - t.Helper() - w, err := st.q.GetWantByReleaseGroup(context.Background(), "rg-ram") - if err != nil { - t.Fatal(err) - } - return !w.SearchedAt.Valid -} - -func (st *stack) ledger(t *testing.T) []dbq.DownloadIntervention { - t.Helper() - rows, err := st.s.pool.Query(context.Background(), `SELECT * FROM download_interventions ORDER BY first_seen, id`) - if err != nil { - t.Fatal(err) - } - out, err := pgx.CollectRows(rows, pgx.RowToStructByPos[dbq.DownloadIntervention]) - if err != nil { - t.Fatal(err) - } - return out -} - -func TestLifecycle_AFailedDownloadIsGivenUpAtOnce_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.4, State: "error", - FailReason: "No space left on device"}) - rep := st.s.Pass(context.Background()) - if rep.GivenUp != 1 { - t.Fatalf("pass = %+v", rep) - } - g := st.grabNow(t) - if !g.EndedAt.Valid || g.EndReason != EndFailed { - t.Fatalf("grab ended=%v reason=%q", g.EndedAt.Valid, g.EndReason) - } - if len(st.client.deleted) != 1 || !st.searchedAgain(t) { - t.Errorf("deleted %v, searched again %v", st.client.deleted, st.searchedAgain(t)) - } - blocks, err := st.q.ListReleaseBlocks(context.Background()) - if err != nil || len(blocks) != 1 || blocks[0].Reason != acquire.BlockStalled || !blocks[0].ExpiresAt.Valid { - t.Fatalf("blocks = %+v %v", blocks, err) - } - if d := blocks[0].ExpiresAt.Time.Sub(st.clock); d < StallBlock-time.Minute || d > StallBlock+time.Minute { - t.Errorf("held back for %s", d) - } - rows := st.ledger(t) - if len(rows) != 1 || rows[0].Trigger != ClientError || rows[0].Action != actGivenUp || rows[0].Outcome != "resolved" || - rows[0].Detail != "No space left on device" { - t.Errorf("ledger = %+v", rows) - } -} - -func TestLifecycle_ADownloadNobodySharesIsRetriedThenGivenUp_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.3, State: "stalledDL"}) - ctx := context.Background() - - // Within the stall setting: nothing is wrong yet. - if rep := st.s.Pass(ctx); rep.Retried+rep.GivenUp != 0 || len(st.ledger(t)) != 0 { - t.Fatalf("a young download was judged: %+v", rep) - } - - st.clock = st.clock.Add(7 * time.Hour) - for i := 1; i <= Retries; i++ { - if rep := st.s.Pass(ctx); rep.Retried != 1 || rep.GivenUp != 0 { - t.Fatalf("pass %d = %+v", i, rep) - } - } - if st.client.reannounce != Retries || st.client.recheck != Retries { - t.Errorf("reannounced %d, rechecked %d", st.client.reannounce, st.client.recheck) - } - if st.grabNow(t).EndedAt.Valid { - t.Fatal("given up before the retries ran out") - } - if rep := st.s.Pass(ctx); rep.GivenUp != 1 { - t.Fatalf("last pass = %+v", rep) - } - g := st.grabNow(t) - if g.EndReason != EndStalled || len(st.client.deleted) != 1 || !st.searchedAgain(t) { - t.Errorf("grab %q: %s; deleted %v", g.EndReason, g.EndDetail, st.client.deleted) - } - rows := st.ledger(t) - if len(rows) != 1 || rows[0].Trigger != NoPeers || rows[0].Remediations != Retries+1 || rows[0].Outcome != "resolved" { - t.Errorf("ledger = %+v", rows) - } -} - -func TestLifecycle_AStalledDownloadThatRecoversIsSettled_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.3, State: "stalledDL"}) - ctx := context.Background() - st.clock = st.clock.Add(7 * time.Hour) - st.s.Pass(ctx) - t2 := st.client.held[hash] - t2.NumSeeds = 3 - st.client.held[hash] = t2 - st.s.Pass(ctx) - rows := st.ledger(t) - if len(rows) != 1 || rows[0].Outcome != "resolved" || rows[0].Action != actRetried { - t.Fatalf("ledger = %+v", rows) - } - if st.grabNow(t).EndedAt.Valid { - t.Error("a download that found peers was given up") - } -} - -func TestLifecycle_TheStallSettingOffNeverGivesUp_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.3, State: "stalledDL"}) - ctx := context.Background() - if _, err := st.s.pool.Exec(ctx, `UPDATE acquisition_settings SET stall_hours = 0`); err != nil { - t.Fatal(err) - } - st.clock = st.clock.Add(30 * 24 * time.Hour) - for range Retries + 2 { - if rep := st.s.Pass(ctx); rep.Retried+rep.GivenUp != 0 { - t.Fatalf("pass = %+v", rep) - } - } -} - -func TestLifecycle_ADownloadGoneFromTheClientEndsAfterHalfAnHour_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.3, State: "downloading", NumSeeds: 4}) - ctx := context.Background() - delete(st.client.held, hash) - st.client.held[other] = downloader.Torrent{Hash: other, Name: "Someone else's", Progress: 1, State: "uploading"} - - if rep := st.s.Pass(ctx); rep.Gone != 0 { - t.Fatalf("ended at first sight: %+v", rep) - } - st.clock = st.clock.Add(31 * time.Minute) - if rep := st.s.Pass(ctx); rep.Gone != 1 { - t.Fatalf("pass = %+v", rep) - } - g := st.grabNow(t) - if g.EndReason != EndGone || !st.searchedAgain(t) { - t.Errorf("grab %q, searched again %v", g.EndReason, st.searchedAgain(t)) - } - if blocks, _ := st.q.ListReleaseBlocks(ctx); len(blocks) != 0 { - t.Errorf("a release nothing was wrong with was held back: %+v", blocks) - } -} - -// A release that leaves its client a second time is held back, whatever the -// cause, so it is not taken again on every pass. -func TestLifecycle_AReleaseGoneTwiceIsHeldBack_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.3, State: "downloading", NumSeeds: 4}) - ctx := context.Background() - delete(st.client.held, hash) - st.client.held[other] = downloader.Torrent{Hash: other, Name: "Someone else's", Progress: 1, State: "uploading"} - - st.s.Pass(ctx) - st.clock = st.clock.Add(31 * time.Minute) - if rep := st.s.Pass(ctx); rep.Gone != 1 { - t.Fatalf("first gone: %+v", rep) - } - if blocks, _ := st.q.ListReleaseBlocks(ctx); len(blocks) != 0 { - t.Fatalf("held back after one gone: %+v", blocks) - } - - again, err := acquire.NewMemory(st.s.pool).Grabbed(ctx, dbq.InsertGrabParams{WantID: st.want.ID, InfoHash: hash, - Name: "Daft Punk - RAM [FLAC]", Key: hash, Client: "qbittorrent", SavePath: "/downloads", Origin: acquire.OriginSchedule}, - pgtype.UUID{}, "") - if err != nil { - t.Fatal(err) - } - // first_seen is the database's clock and the test's runs ahead of it, so - // the second grab may end on either pass; count both. - gone := st.s.Pass(ctx).Gone - st.clock = st.clock.Add(31 * time.Minute) - if gone += st.s.Pass(ctx).Gone; gone != 1 { - t.Fatalf("second grab ended gone %d times, want 1", gone) - } - blocks, err := st.q.ListReleaseBlocks(ctx) - if err != nil { - t.Fatal(err) - } - if len(blocks) != 1 || blocks[0].Reason != acquire.BlockGone || !blocks[0].ExpiresAt.Valid { - t.Fatalf("blocks = %+v, want one timed gone block", blocks) - } - g, err := st.q.GetGrab(ctx, again.ID) - if err != nil { - t.Fatal(err) - } - if g.EndReason != EndGone || !strings.Contains(g.EndDetail, "held back until") { - t.Errorf("second grab %q: %s", g.EndReason, g.EndDetail) - } -} - -func TestLifecycle_AnEmptyListIsNotAMeasurement_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.3, State: "downloading"}) - ctx := context.Background() - delete(st.client.held, hash) - st.clock = st.clock.Add(24 * time.Hour) - for range 3 { - if rep := st.s.Pass(ctx); rep.Gone != 0 { - t.Fatalf("pass = %+v", rep) - } - } - if len(st.ledger(t)) != 0 || st.grabNow(t).EndedAt.Valid { - t.Error("a client listing nothing was taken to have lost the download") - } -} - -func TestLifecycle_AnUnreachableClientIsRecordedAndSettled_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.3, State: "stalledDL"}) - ctx := context.Background() - st.clock = st.clock.Add(7 * time.Hour) - st.client.err = errors.New("dial tcp: connection refused") - st.s.Pass(ctx) - down, err := st.q.ListClientsDown(ctx) - if err != nil || len(down) != 1 || down[0].Client != "qbittorrent" { - t.Fatalf("clients down = %+v %v", down, err) - } - if st.client.reannounce != 0 { - t.Error("a download was judged through a client that did not answer") - } - st.client.err = nil - st.s.Pass(ctx) - if down, _ := st.q.ListClientsDown(ctx); len(down) != 0 { - t.Errorf("still down: %+v", down) - } -} - -func TestLifecycle_AnImportedDownloadSeedsThenIsRemoved_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 1, State: "uploading", Ratio: 0.8}) - ctx := context.Background() - if _, err := st.q.RecordImport(ctx, dbq.RecordImportParams{ID: st.grab.ID, ImportState: "imported", - ImportReason: "Imported.", ImportDetail: []byte("{}"), ImportedFiles: 3, EndReason: "imported"}); err != nil { - t.Fatal(err) - } - if !st.grabNow(t).Seeding { - t.Fatal("an import did not leave its download seeding") - } - if rep := st.s.Pass(ctx); rep.Released != 0 || len(st.client.deleted) != 0 { - t.Fatalf("removed before the ratio: %+v", rep) - } - if g := st.grabNow(t); g.Ratio < 0.79 || g.Ratio > 0.81 { - t.Errorf("ratio recorded %v", g.Ratio) - } - tor := st.client.held[hash] - tor.Ratio = 2.1 - st.client.held[hash] = tor - if rep := st.s.Pass(ctx); rep.Released != 1 || len(st.client.deleted) != 1 { - t.Fatalf("pass = %+v, deleted %v", rep, st.client.deleted) - } - if !st.grabNow(t).SeedReleasedAt.Valid { - t.Error("the release was not recorded") - } -} - -func TestLifecycle_AnImportedDownloadIsRemovedWhenItsDaysAreUp_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 1, State: "uploading", Ratio: 0.1}) - ctx := context.Background() - if _, err := st.q.RecordImport(ctx, dbq.RecordImportParams{ID: st.grab.ID, ImportState: "imported", - ImportReason: "Imported.", ImportDetail: []byte("{}"), ImportedFiles: 3, EndReason: "imported"}); err != nil { - t.Fatal(err) - } - st.clock = st.clock.Add(15 * 24 * time.Hour) - if rep := st.s.Pass(ctx); rep.Released != 1 || len(st.client.deleted) != 1 { - t.Fatalf("pass = %+v", rep) - } -} - -func TestLifecycle_ADownloadTheClientWouldNotRemoveIsAskedAgain_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.4, State: "error"}) - ctx := context.Background() - st.client.deleteErr = errors.New("HTTP 500") - if rep := st.s.Pass(ctx); rep.GivenUp != 1 { - t.Fatalf("pass = %+v", rep) - } - if !st.grabNow(t).EndedAt.Valid { - t.Fatal("a refused removal held the grab") - } - open := 0 - for _, r := range st.ledger(t) { - if r.Trigger == NotRemoved && r.Outcome == "open" { - open++ - } - } - if open != 1 { - t.Fatalf("ledger = %+v", st.ledger(t)) - } - st.client.deleteErr = nil - if rep := st.s.Pass(ctx); rep.Removed != 1 || len(st.client.deleted) != 1 { - t.Fatalf("pass = %+v", rep) - } - for _, r := range st.ledger(t) { - if r.Outcome == "open" { - t.Errorf("still open: %+v", r) - } - } -} - -func TestLifecycle_APersonGivingUpSetsTheReleaseAsideForGood_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "Daft Punk - RAM [FLAC]", Progress: 0.4, State: "downloading"}) - ctx := context.Background() - if _, err := st.s.Abandon(ctx, st.grab.ID, pgtype.UUID{}); err != nil { - t.Fatal(err) - } - g := st.grabNow(t) - if g.EndReason != EndAbandoned || len(st.client.deleted) != 1 || !st.searchedAgain(t) { - t.Errorf("grab %q, deleted %v", g.EndReason, st.client.deleted) - } - blocks, _ := st.q.ListReleaseBlocks(ctx) - if len(blocks) != 1 || blocks[0].Reason != acquire.BlockOperator || blocks[0].ExpiresAt.Valid { - t.Errorf("blocks = %+v", blocks) - } - if _, err := st.s.Abandon(ctx, st.grab.ID, pgtype.UUID{}); !errors.Is(err, ErrNotAbandonable) { - t.Errorf("abandoned twice: %v", err) - } -} - -func TestLifecycle_AHoldExpiresButABlockForGoodIsNeverShortened_Integration(t *testing.T) { - st := newStack(t, downloader.Torrent{Name: "x", Progress: 0.4, State: "downloading"}) - ctx := context.Background() - mem := acquire.NewMemory(st.s.pool) - past := pgtype.Timestamptz{Time: time.Now().Add(-time.Hour), Valid: true} - if err := mem.Block(ctx, dbq.BlockReleaseParams{InfoHash: other, Name: "old", Reason: acquire.BlockStalled, ExpiresAt: past}); err != nil { - t.Fatal(err) - } - if blocks, _ := st.q.ListReleaseBlocks(ctx); len(blocks) != 0 { - t.Errorf("an expired hold still holds: %+v", blocks) - } - if n, err := st.q.DeleteExpiredBlocks(ctx); err != nil || n != 1 { - t.Errorf("lifted %d %v", n, err) - } - - if err := mem.Block(ctx, dbq.BlockReleaseParams{InfoHash: hash, Name: "x", Reason: acquire.BlockOperator}); err != nil { - t.Fatal(err) - } - soon := pgtype.Timestamptz{Time: time.Now().Add(time.Hour), Valid: true} - if err := mem.Block(ctx, dbq.BlockReleaseParams{InfoHash: hash, Name: "x", Reason: acquire.BlockStalled, ExpiresAt: soon}); err != nil { - t.Fatal(err) - } - blocks, _ := st.q.ListReleaseBlocks(ctx) - if len(blocks) != 1 || blocks[0].Reason != acquire.BlockOperator || blocks[0].ExpiresAt.Valid { - t.Errorf("a block for good was shortened: %+v", blocks) - } -} - -func TestSpan(t *testing.T) { - for _, c := range []struct { - d time.Duration - want string - }{{45 * time.Minute, "45 minutes"}, {7 * time.Hour, "7 hours"}, {3 * 24 * time.Hour, "3 days"}} { - if got := span(c.d); got != c.want { - t.Errorf("span(%s) = %q, want %q", c.d, got, c.want) - } - } -} diff --git a/internal/prowlarr/prowlarr.go b/internal/prowlarr/prowlarr.go deleted file mode 100644 index 3ca4975b..00000000 --- a/internal/prowlarr/prowlarr.go +++ /dev/null @@ -1,176 +0,0 @@ -// Package prowlarr searches through Prowlarr's native API. -// -// Native rather than Torznab because Prowlarr has no aggregate Torznab feed: -// `/api/v1/indexer/all/newznab` 404s exactly like an indexer id that does not -// exist. -// `/api/v1/search` is the one place Prowlarr asks every indexer at once. -package prowlarr - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "net/url" - "strings" - "time" - - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -// SearchPath is Prowlarr's aggregate search. -const SearchPath = "/api/v1/search" - -// DefaultTimeout is longer than a Torznab feed's: Prowlarr waits on every -// indexer it has before answering. -const DefaultTimeout = 60 * time.Second - -// Config is one Prowlarr. -type Config struct { - // Code is the acquisition_services row, stamped onto every Result. - Code string - BaseURL string - // APIKey is required: Prowlarr's API refuses every call without one. - APIKey string - Timeout time.Duration -} - -// Client searches one Prowlarr and fetches .torrent files through it. -type Client struct { - Config Config - // HTTP is injectable for tests. Nil takes http.DefaultClient. - HTTP *http.Client -} - -type searchResult struct { - Title string `json:"title"` - InfoHash string `json:"infoHash"` - GUID string `json:"guid"` - MagnetURL string `json:"magnetUrl"` - DownloadURL string `json:"downloadUrl"` - Size int64 `json:"size"` - Seeders int `json:"seeders"` - Leechers int `json:"leechers"` - Indexer string `json:"indexer"` - PublishDate string `json:"publishDate"` - Categories []struct { - ID int `json:"id"` - } `json:"categories"` -} - -// Search asks every indexer in this Prowlarr. `type=search` is free text; -// `type=music` takes artist and album separately, and a release name does not -// reliably say which part is which. -func (c *Client) Search(ctx context.Context, q indexer.Query) ([]indexer.Result, error) { - params := url.Values{"query": {q.Text}, "type": {"search"}} - for _, cat := range q.Categories { - params.Add("categories", fmt.Sprint(cat)) - } - endpoint := strings.TrimRight(c.Config.BaseURL, "/") + SearchPath + "?" + params.Encode() - body, err := c.get(ctx, endpoint) - if err != nil { - return nil, err - } - out, err := parseResults(body) - if err != nil { - return nil, err - } - for i := range out { - out[i].Source = c.Config.Code - } - return out, nil -} - -func parseResults(body []byte) ([]indexer.Result, error) { - var rows []searchResult - if err := json.Unmarshal(body, &rows); err != nil { - return nil, fmt.Errorf("%w: parse search results: %v", indexer.ErrUnreachable, err) - } - out := make([]indexer.Result, 0, len(rows)) - for _, row := range rows { - title := strings.TrimSpace(row.Title) - if title == "" { - continue - } - r := indexer.Result{ - Title: title, - SizeBytes: row.Size, - Seeders: row.Seeders, - Peers: row.Leechers, - Indexer: strings.TrimSpace(row.Indexer), - Magnet: firstMagnet(row.MagnetURL, row.GUID), - DownloadURL: strings.TrimSpace(row.DownloadURL), - } - if len(row.Categories) > 0 { - r.Category = row.Categories[0].ID - } - r.InfoHash = indexer.NormalizeHash(row.InfoHash) - if r.InfoHash == "" { - r.InfoHash = indexer.MagnetHash(r.Magnet) - } - if r.InfoHash == "" && indexer.IsInfoHash(row.GUID) { - r.InfoHash = indexer.NormalizeHash(row.GUID) - } - if t, err := time.Parse(time.RFC3339, strings.TrimSpace(row.PublishDate)); err == nil { - r.Published = t - } - out = append(out, r) - } - return out, nil -} - -func firstMagnet(candidates ...string) string { - for _, c := range candidates { - c = strings.TrimSpace(c) - if strings.HasPrefix(c, "magnet:") { - return c - } - } - return "" -} - -// Download fetches a result's .torrent through Prowlarr, which holds the -// tracker credentials. A public tracker's link often redirects to a magnet, -// which comes back as an *indexer.MagnetLink. -func (c *Client) Download(ctx context.Context, r indexer.Result) ([]byte, error) { - if r.DownloadURL == "" { - return nil, fmt.Errorf("%w: %q has no download URL", indexer.ErrUnreachable, r.Title) - } - body, err := c.get(ctx, r.DownloadURL) - if err != nil { - return nil, err - } - return indexer.TorrentBody(r.Title, body) -} - -func (c *Client) get(ctx context.Context, endpoint string) ([]byte, error) { - if c.Config.BaseURL == "" || c.Config.APIKey == "" { - return nil, indexer.ErrUnconfigured - } - timeout := c.Config.Timeout - if timeout <= 0 { - timeout = DefaultTimeout - } - ctx, cancel := context.WithTimeout(ctx, timeout) - defer cancel() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) - if err != nil { - return nil, fmt.Errorf("%w: build request: %v", indexer.ErrUnreachable, err) - } - req.Header.Set("User-Agent", indexer.UserAgent) - // In a header, not ?apikey=, so the key stays out of URLs, logs and - // error strings. - req.Header.Set("X-Api-Key", c.Config.APIKey) - req.Header.Set("Accept", "application/json") - resp, body, err := indexer.Send(c.HTTP, req) - if err != nil { - return nil, err - } - if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden { - return nil, fmt.Errorf("%w: HTTP %d", indexer.ErrAuth, resp.StatusCode) - } - if err := indexer.StatusError(resp, "Prowlarr"); err != nil { - return nil, err - } - return body, nil -} diff --git a/internal/prowlarr/prowlarr_test.go b/internal/prowlarr/prowlarr_test.go deleted file mode 100644 index 2d4f3011..00000000 --- a/internal/prowlarr/prowlarr_test.go +++ /dev/null @@ -1,78 +0,0 @@ -package prowlarr - -import ( - "context" - "errors" - "net/http" - "net/http/httptest" - "testing" - - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" -) - -const sampleResults = `[ - {"title": "Radiohead - In Rainbows (2007) [FLAC]", "infoHash": "AABBCCDDEEFF00112233445566778899AABBCCDD", - "downloadUrl": "http://prowlarr/1/download?link=x", "size": 400000000, "seeders": 30, "leechers": 4, - "indexer": "RED", "publishDate": "2026-10-01T12:00:00Z", "categories": [{"id": 3040}]}, - {"title": "Radiohead - Amnesiac", "guid": "magnet:?xt=urn:btih:ffeeddccbbaa99887766554433221100ffeeddcc"}, - {"title": ""} -]` - -func TestSearch_AsksTheNativeAPIWithTheKeyInAHeader(t *testing.T) { - reqs := make(chan *http.Request, 1) - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - reqs <- r - _, _ = w.Write([]byte(sampleResults)) - })) - defer srv.Close() - - c := &Client{Config: Config{Code: "prowlarr", BaseURL: srv.URL + "/", APIKey: "secret"}} - results, err := c.Search(context.Background(), indexer.Query{Text: "radiohead", Categories: []int{3000}}) - if err != nil { - t.Fatalf("search: %v", err) - } - got := <-reqs - if got.URL.Path != SearchPath || got.URL.Query().Get("type") != "search" || got.URL.Query().Get("categories") != "3000" { - t.Errorf("request = %s, want %s with type=search and the categories", got.URL, SearchPath) - } - if got.Header.Get("X-Api-Key") != "secret" || got.URL.Query().Get("apikey") != "" { - t.Error("the key belongs in X-Api-Key, never in the URL") - } - if len(results) != 2 { - t.Fatalf("results = %d, want 2", len(results)) - } - if r := results[0]; r.InfoHash != "aabbccddeeff00112233445566778899aabbccdd" || r.Peers != 4 || - r.Category != 3040 || r.Source != "prowlarr" || r.Published.IsZero() { - t.Errorf("first = %+v", r) - } - if r := results[1]; r.Magnet == "" || r.InfoHash != "ffeeddccbbaa99887766554433221100ffeeddcc" { - t.Errorf("second = %+v, want the GUID's magnet and its hash", r) - } -} - -func TestSearch_Refusals(t *testing.T) { - for name, tc := range map[string]struct { - status int - want error - }{ - "a refused key": {http.StatusUnauthorized, indexer.ErrAuth}, - "an outage": {http.StatusBadGateway, indexer.ErrUnreachable}, - } { - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.WriteHeader(tc.status) - })) - c := &Client{Config: Config{BaseURL: srv.URL, APIKey: "k"}} - _, err := c.Search(context.Background(), indexer.Query{Text: "x"}) - srv.Close() - if !errors.Is(err, tc.want) { - t.Errorf("%s: err = %v, want %v", name, err, tc.want) - } - } -} - -func TestSearch_NoKeyIsUnconfigured(t *testing.T) { - c := &Client{Config: Config{BaseURL: "http://prowlarr:9696"}} - if _, err := c.Search(context.Background(), indexer.Query{Text: "x"}); !errors.Is(err, indexer.ErrUnconfigured) { - t.Fatalf("err = %v, want ErrUnconfigured", err) - } -} diff --git a/internal/reacquisition/settings.go b/internal/reacquisition/settings.go deleted file mode 100644 index 29bf05ba..00000000 --- a/internal/reacquisition/settings.go +++ /dev/null @@ -1,116 +0,0 @@ -// Package reacquisition wants an album back when its files go missing, -// without anyone pressing anything. -// -// The unit of work is the ALBUM, not the track. Music is acquired as -// releases; there is no meaningful "fetch me one track", and a recording -// MBID is something plenty of files lack. Grouping also does most of the -// safety work: a reorganised album's forty missing files become one want, -// not forty. -package reacquisition - -import ( - "context" - "errors" - "fmt" - "log/slog" - "sync" - - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -// Settings is the admin-tunable policy, mirroring the columns and CHECK -// ranges in migration 0056. -type Settings struct { - Enabled bool - GraceHours int32 - MaxPerPass int32 -} - -// Defaults mirror migration 0056's column defaults. Duplicated here so a -// database that cannot be read still yields a sane policy rather than a -// zero-valued one — a zero grace window would fire on every transient -// unmount, which is the exact failure the grace period exists to prevent. -var Defaults = Settings{ - Enabled: true, - GraceHours: 24, - MaxPerPass: 20, -} - -// ErrOutOfRange is returned by Set for values migration 0056's CHECKs would -// reject, so the API layer answers 400 instead of surfacing a constraint -// violation. -var ErrOutOfRange = errors.New("reacquisition setting out of range") - -// SettingsService caches the settings and owns their persistence. Cached -// because the sweeper reads them every pass and the admin card reads them on -// every render; neither needs a round-trip. -type SettingsService struct { - pool *pgxpool.Pool - logger *slog.Logger - - mu sync.RWMutex - cur Settings -} - -// NewSettingsService loads once and caches. -// -// Always returns a usable service, even alongside a non-nil error: a -// boot-time database hiccup should leave the sweeper running on defaults -// rather than take it out entirely. The error is returned so the caller can -// log that the cache holds defaults rather than stored state. -func NewSettingsService(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) (*SettingsService, error) { - s := &SettingsService{pool: pool, logger: logger, cur: Defaults} - row, err := dbq.New(pool).GetReacquisitionSettings(ctx) - if err != nil { - return s, fmt.Errorf("reacquisition: load settings: %w", err) - } - s.cur = fromRow(row) - return s, nil -} - -// Get returns the cached settings. -func (s *SettingsService) Get() Settings { - s.mu.RLock() - defer s.mu.RUnlock() - return s.cur -} - -// Set validates, persists and re-caches. -func (s *SettingsService) Set(ctx context.Context, in Settings) (Settings, error) { - if err := validate(in); err != nil { - return Settings{}, err - } - row, err := dbq.New(s.pool).UpdateReacquisitionSettings(ctx, dbq.UpdateReacquisitionSettingsParams{ - Enabled: in.Enabled, - GraceHours: in.GraceHours, - MaxPerPass: in.MaxPerPass, - }) - if err != nil { - return Settings{}, fmt.Errorf("reacquisition: save settings: %w", err) - } - out := fromRow(row) - s.mu.Lock() - s.cur = out - s.mu.Unlock() - return out, nil -} - -func validate(in Settings) error { - switch { - case in.GraceHours < 1 || in.GraceHours > 720: - return fmt.Errorf("%w: grace_hours must be 1-720", ErrOutOfRange) - case in.MaxPerPass < 1 || in.MaxPerPass > 200: - return fmt.Errorf("%w: max_per_pass must be 1-200", ErrOutOfRange) - } - return nil -} - -func fromRow(row dbq.ReacquisitionSetting) Settings { - return Settings{ - Enabled: row.Enabled, - GraceHours: row.GraceHours, - MaxPerPass: row.MaxPerPass, - } -} diff --git a/internal/reacquisition/settings_test.go b/internal/reacquisition/settings_test.go deleted file mode 100644 index 60b599d9..00000000 --- a/internal/reacquisition/settings_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package reacquisition - -import ( - "errors" - "testing" -) - -func TestValidateRejectsWhatTheCheckWouldReject(t *testing.T) { - // Each case mirrors a CHECK in migration 0056. Validating in Go as well - // means the API answers 400 with a readable message instead of surfacing - // a constraint violation. - cases := []struct { - name string - in Settings - }{ - {"zero grace would fire on every transient unmount", - mutate(func(s *Settings) { s.GraceHours = 0 })}, - {"grace beyond a month", mutate(func(s *Settings) { s.GraceHours = 721 })}, - {"zero per pass means never sweep", mutate(func(s *Settings) { s.MaxPerPass = 0 })}, - {"per pass beyond the cap", mutate(func(s *Settings) { s.MaxPerPass = 201 })}, - } - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - if err := validate(c.in); !errors.Is(err, ErrOutOfRange) { - t.Errorf("validate() = %v, want ErrOutOfRange", err) - } - }) - } -} - -func TestValidateAcceptsDefaults(t *testing.T) { - if err := validate(Defaults); err != nil { - t.Fatalf("the shipped defaults must be valid, got %v", err) - } -} - -func mutate(f func(*Settings)) Settings { - s := Defaults - f(&s) - return s -} diff --git a/internal/reacquisition/sweeper.go b/internal/reacquisition/sweeper.go deleted file mode 100644 index b5c4118a..00000000 --- a/internal/reacquisition/sweeper.go +++ /dev/null @@ -1,167 +0,0 @@ -package reacquisition - -import ( - "context" - "errors" - "fmt" - "log/slog" - "time" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/tags" -) - -// Sweeper periodically wants back albums whose files have been missing past -// the grace window. Searching for them is the search schedule's, which -// retries per want. -// -// Deliberately a periodic worker rather than a hook inside the scanner's -// reconcile pass: reconcile runs inside a scan and re-runs often, and a -// filesystem lies transiently (an unmounted volume, a NAS mid-reboot) in ways -// the grace window exists to ride out. -type Sweeper struct { - pool *pgxpool.Pool - settings *SettingsService - logger *slog.Logger - tick time.Duration - // releaseGroup names the MusicBrainz release group of a release id, for an - // album whose tags never carried one. Wants are kept by release - // group. A field so tests can stand in for MusicBrainz. - releaseGroup func(ctx context.Context, releaseMBID string) (string, error) -} - -// NewSweeper constructs a Sweeper. The tick is deliberately coarse: the grace -// window is measured in hours. -func NewSweeper(pool *pgxpool.Pool, settings *SettingsService, logger *slog.Logger) *Sweeper { - return &Sweeper{ - pool: pool, - settings: settings, - logger: logger, - tick: 1 * time.Hour, - releaseGroup: tags.ReleaseGroupForRelease, - } -} - -// Run drives the sweep loop until ctx is cancelled. -func (s *Sweeper) Run(ctx context.Context) { - t := time.NewTicker(s.tick) - defer t.Stop() - for { - select { - case <-ctx.Done(): - return - case <-t.C: - if err := s.SweepOnce(ctx); err != nil { - s.logger.Warn("reacquisition: sweep failed", "err", err) - } - } - } -} - -// PassResult reports what a single sweep did, for logging and tests. -type PassResult struct { - Wanted int // albums newly wanted, or wanted again after being held - Unnameable int64 // albums with missing files but no MBID to want them by - // Unresolved: albums skipped this pass because MusicBrainz could not name - // their release group; the next pass tries again. - Unresolved int -} - -// SweepOnce runs one pass. Exported so tests drive it directly. -func (s *Sweeper) SweepOnce(ctx context.Context) error { - cfg := s.settings.Get() - if !cfg.Enabled { - return nil - } - q := dbq.New(s.pool) - var res PassResult - // Counted, not acted on: an album MusicBrainz cannot name is not a - // failure to retry, it is a permanent gap an admin should see. - if n, cerr := q.CountAlbumsMissingWithoutMbid(ctx); cerr == nil { - res.Unnameable = n - } - due, err := q.ListAlbumsDueReacquisition(ctx, dbq.ListAlbumsDueReacquisitionParams{ - GraceHours: cfg.GraceHours, - PageLimit: cfg.MaxPerPass, - }) - if err != nil { - return fmt.Errorf("list due: %w", err) - } - for _, album := range due { - if err := s.want(ctx, q, album, &res); err != nil { - // One album's failure must not abandon the rest of the pass. - s.logger.Warn("reacquisition: want failed", "album", album.AlbumTitle, "err", err) - } - } - s.logSummary(res) - return nil -} - -// want records one album as wanted under its release group. -func (s *Sweeper) want(ctx context.Context, q *dbq.Queries, album dbq.ListAlbumsDueReacquisitionRow, res *PassResult) error { - // The query already filters these out; belt and braces. - hasGroup := album.AlbumReleaseGroupMbid != nil && *album.AlbumReleaseGroupMbid != "" - if (album.AlbumMbid == nil && !hasGroup) || album.ArtistMbid == nil { - return nil - } - group, err := s.albumReleaseGroup(ctx, q, album) - if err != nil { - if errors.Is(err, tags.ErrNotFound) { - res.Unresolved++ - s.logger.Info("reacquisition: no MusicBrainz release group for album; skipped", - "album", album.AlbumTitle, "release_mbid", *album.AlbumMbid) - return nil - } - return fmt.Errorf("release group: %w", err) - } - _, err = q.WantMissingAlbum(ctx, dbq.WantMissingAlbumParams{ - ReleaseGroupMbid: group, ArtistMbid: *album.ArtistMbid, ArtistName: album.ArtistName, - Title: album.AlbumTitle, AlbumID: album.AlbumID, - }) - if errors.Is(err, pgx.ErrNoRows) { - // Already wanted, open: by a request, a follow, or this sweeper. - return nil - } - if err != nil { - return fmt.Errorf("want: %w", err) - } - res.Wanted++ - return nil -} - -// albumReleaseGroup is the album's release group: the one its tags carried, -// else MusicBrainz's answer for its release id, cached onto the album so the -// next pass need not ask again. Returns tags.ErrNotFound when MusicBrainz -// cannot name it. -func (s *Sweeper) albumReleaseGroup(ctx context.Context, q *dbq.Queries, album dbq.ListAlbumsDueReacquisitionRow) (string, error) { - if album.AlbumReleaseGroupMbid != nil && *album.AlbumReleaseGroupMbid != "" { - return *album.AlbumReleaseGroupMbid, nil - } - group, err := s.releaseGroup(ctx, *album.AlbumMbid) - if err != nil { - return "", err - } - if cerr := q.SetAlbumReleaseGroupMbidIfNull(ctx, dbq.SetAlbumReleaseGroupMbidIfNullParams{ - ID: album.AlbumID, - ReleaseGroupMbid: &group, - }); cerr != nil { - s.logger.Warn("reacquisition: cache release group failed", "album", album.AlbumTitle, "err", cerr) - } - return group, nil -} - -func (s *Sweeper) logSummary(res PassResult) { - // Silence when a pass did nothing at all — this runs hourly forever, and - // an unconditional line would bury the passes that mattered. - if res.Wanted == 0 && res.Unresolved == 0 { - return - } - s.logger.Info("reacquisition: sweep", - "wanted", res.Wanted, - "unnameable", res.Unnameable, - "unresolved", res.Unresolved, - ) -} diff --git a/internal/reacquisition/sweeper_test.go b/internal/reacquisition/sweeper_test.go deleted file mode 100644 index 8d8a0899..00000000 --- a/internal/reacquisition/sweeper_test.go +++ /dev/null @@ -1,199 +0,0 @@ -package reacquisition - -import ( - "context" - "io" - "log/slog" - "os" - "testing" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" - "git.fabledsword.com/bvandeusen/minstrel/internal/tags" -) - -func newSweepPool(t *testing.T) *pgxpool.Pool { - t.Helper() - if testing.Short() { - t.Skip("skipping integration test in -short mode") - } - dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") - if dsn == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { - t.Fatalf("migrate: %v", err) - } - pool, err := pgxpool.New(context.Background(), dsn) - if err != nil { - t.Fatalf("pool: %v", err) - } - t.Cleanup(pool.Close) - dbtest.ResetDB(t, pool) - return pool -} - -// lostAlbum seeds an album whose only track has been missing for two days, -// named by MusicBrainz release id (when release is set) and release group -// (when group is set). -func lostAlbum(t *testing.T, pool *pgxpool.Pool, title, release, group string) pgtype.UUID { - t.Helper() - ctx := context.Background() - q := dbq.New(pool) - artistMBID := "artist-" + title - ar, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: title + " Artist", SortName: title, Mbid: &artistMBID}) - if err != nil { - t.Fatal(err) - } - params := dbq.UpsertAlbumParams{Title: title, SortTitle: title, ArtistID: ar.ID} - if release != "" { - params.Mbid = &release - } - if group != "" { - params.ReleaseGroupMbid = &group - } - al, err := q.UpsertAlbum(ctx, params) - if err != nil { - t.Fatal(err) - } - tr, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ - Title: "Lost", AlbumID: al.ID, ArtistID: ar.ID, DurationMs: 180000, - FilePath: "/music/" + title + "/01.flac", FileSize: 1, FileFormat: "flac", - }) - if err != nil { - t.Fatal(err) - } - if _, err := pool.Exec(ctx, "UPDATE tracks SET missing_since = now() - interval '2 days' WHERE id = $1", tr.ID); err != nil { - t.Fatal(err) - } - return al.ID -} - -// heldWant puts an album on the wanted list, held since ago. -func heldWant(t *testing.T, pool *pgxpool.Pool, group string, album pgtype.UUID, ago string) { - t.Helper() - if _, err := pool.Exec(context.Background(), ` - INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id, fulfilled_at) - VALUES ($1, 'a', 'A', 'T', 'library', $2, now() - $3::interval)`, group, album, ago); err != nil { - t.Fatal(err) - } -} - -// Only an album on the wanted list is fetched back: one taken off it, or -// dropped into the library by hand, is not. An album on the list is wanted -// by its release group, never its release id: a tag-supplied -// group is used as is; otherwise MusicBrainz names it and the answer is -// cached onto the album; an album MusicBrainz cannot name is skipped and -// tried again next pass. -func TestSweep_WantsBackOnlyAlbumsOnTheList_Integration(t *testing.T) { - pool := newSweepPool(t) - ctx := context.Background() - logger := slog.New(slog.NewTextHandler(io.Discard, nil)) - q := dbq.New(pool) - - settings, err := NewSettingsService(ctx, pool, logger) - if err != nil { - t.Fatal(err) - } - tagged := lostAlbum(t, pool, "Tagged", "release-tagged", "group-tagged") - resolved := lostAlbum(t, pool, "Resolved", "release-resolved", "") - unknown := lostAlbum(t, pool, "Unknown", "release-unknown", "") - _ = lostAlbum(t, pool, "Off The List", "release-off", "group-off") - // Matched to a group with no release id: still wanted back. - groupOnly := lostAlbum(t, pool, "Group Only", "", "group-only") - heldWant(t, pool, "group-tagged", tagged, "3 days") - heldWant(t, pool, "group-only", groupOnly, "3 days") - heldWant(t, pool, "group-resolved", resolved, "3 days") - heldWant(t, pool, "group-unknown", unknown, "3 days") - - s := NewSweeper(pool, settings, logger) - asked := map[string]int{} - s.releaseGroup = func(_ context.Context, release string) (string, error) { - asked[release]++ - if release == "release-resolved" { - return "group-resolved", nil - } - return "", tags.ErrNotFound - } - if err := s.SweepOnce(ctx); err != nil { - t.Fatalf("sweep: %v", err) - } - - wantFor := func(group string, album pgtype.UUID) { - t.Helper() - w, err := q.GetWantByReleaseGroup(ctx, group) - if err != nil { - t.Fatalf("no want for %s: %v", group, err) - } - if w.Reason != "missing" || w.AlbumID != album || w.FulfilledAt.Valid { - t.Errorf("want %s = %+v: an open missing want about the album", group, w) - } - } - wantFor("group-tagged", tagged) - wantFor("group-resolved", resolved) - wantFor("group-only", groupOnly) - if w, _ := q.GetWantByReleaseGroup(ctx, "group-unknown"); !w.FulfilledAt.Valid { - t.Error("an album MusicBrainz cannot name was reopened under the wrong group") - } - if _, err := q.GetWantByReleaseGroup(ctx, "group-off"); err == nil { - t.Error("an album not on the list was wanted back") - } - if asked["release-tagged"] != 0 || asked["release-off"] != 0 { - t.Errorf("MusicBrainz asked %v: not about a tagged album, nor one off the list", asked) - } - var cached *string - if err := pool.QueryRow(ctx, "SELECT release_group_mbid FROM albums WHERE id = $1", resolved).Scan(&cached); err != nil { - t.Fatal(err) - } - if cached == nil || *cached != "group-resolved" { - t.Errorf("resolved group not cached on the album: %v", cached) - } - - // The next pass finds both already wanted and asks nothing new. - asked = map[string]int{} - if err := s.SweepOnce(ctx); err != nil { - t.Fatal(err) - } - if asked["release-resolved"] != 0 || asked["release-unknown"] != 1 { - t.Errorf("second pass asked MusicBrainz %v: only the unresolved album again", asked) - } -} - -// A want held after the files went missing means the library has a full copy -// despite the missing rows: the album is not wanted again. One held -// before they went missing is reopened. -func TestSweep_HeldWantsAreReopenedOnlyForANewLoss_Integration(t *testing.T) { - pool := newSweepPool(t) - ctx := context.Background() - logger := slog.New(slog.NewTextHandler(io.Discard, nil)) - settings, err := NewSettingsService(ctx, pool, logger) - if err != nil { - t.Fatal(err) - } - stale := lostAlbum(t, pool, "Stale", "release-stale", "group-stale") - lost := lostAlbum(t, pool, "Lost Again", "release-lost", "group-lost") - for _, c := range []struct { - group, heldAgo string - album pgtype.UUID - }{{"group-stale", "1 hour", stale}, {"group-lost", "3 days", lost}} { - if _, err := pool.Exec(ctx, ` - INSERT INTO wants (release_group_mbid, artist_mbid, artist_name, title, reason, album_id, fulfilled_at) - VALUES ($1, 'a', 'A', 'T', 'request', $2, now() - $3::interval)`, c.group, c.album, c.heldAgo); err != nil { - t.Fatal(err) - } - } - if err := NewSweeper(pool, settings, logger).SweepOnce(ctx); err != nil { - t.Fatal(err) - } - q := dbq.New(pool) - if w, _ := q.GetWantByReleaseGroup(ctx, "group-stale"); !w.FulfilledAt.Valid { - t.Error("a want held since the loss was reopened") - } - if w, _ := q.GetWantByReleaseGroup(ctx, "group-lost"); w.FulfilledAt.Valid || w.Reason != "missing" { - t.Errorf("a want held before the loss should reopen as missing: %+v", w) - } -} diff --git a/internal/recommendation/candidate_tags_integration_test.go b/internal/recommendation/candidate_tags_integration_test.go deleted file mode 100644 index ef24982a..00000000 --- a/internal/recommendation/candidate_tags_integration_test.go +++ /dev/null @@ -1,455 +0,0 @@ -package recommendation - -import ( - "context" - "testing" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -// The candidate-artist tag cache for out-of-library Discover candidates. These -// cover the SQL rather than the provider chain — the eligibility query is the -// piece with real risk in it (a GROUP BY over a many-rows-per-candidate table, -// a LEFT JOIN to bookkeeping, and two exclusion branches), and it feeds this -// package's taste-tag ranking. -// -// Fixtures live here because the harness and seedUnmatched do. - -const ( - tagVersionCurrent = 2 - tagVersionOld = 1 -) - -// listEligible is the query under test, at the current provider version. -func listEligible(t *testing.T, pool *pgxpool.Pool, limit int32) []dbq.ListCandidateArtistsMissingTagsRow { - t.Helper() - rows, err := dbq.New(pool).ListCandidateArtistsMissingTags(context.Background(), - dbq.ListCandidateArtistsMissingTagsParams{ - TagSourcesVersion: tagVersionCurrent, - Limit: limit, - }) - if err != nil { - t.Fatalf("ListCandidateArtistsMissingTags: %v", err) - } - return rows -} - -func setState(t *testing.T, pool *pgxpool.Pool, mbid, source string, version int32) { - t.Helper() - if err := dbq.New(pool).SetCandidateArtistTagState(context.Background(), - dbq.SetCandidateArtistTagStateParams{ - CandidateMbid: mbid, TagSource: source, TagSourcesVersion: version, - }); err != nil { - t.Fatalf("SetCandidateArtistTagState: %v", err) - } -} - -func mbidsOf(rows []dbq.ListCandidateArtistsMissingTagsRow) []string { - out := make([]string, 0, len(rows)) - for _, r := range rows { - out = append(out, r.CandidateMbid) - } - return out -} - -func TestCandidateTags_NeverProcessedIsEligible(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seed := seedArtist(t, pool, "Seed", "") - likeArtist(t, pool, user.ID, seed.ID) - seedUnmatched(t, pool, seed.ID, "cand-1", "Candidate One", 0.9) - - rows := listEligible(t, pool, 10) - if len(rows) != 1 { - t.Fatalf("len = %d, want 1: %v", len(rows), mbidsOf(rows)) - } - if rows[0].CandidateName != "Candidate One" { - t.Errorf("name = %q, want Candidate One", rows[0].CandidateName) - } - if rows[0].TotalScore != 0.9 { - t.Errorf("score = %v, want 0.9", rows[0].TotalScore) - } -} - -// An in-library candidate's tags belong in track_tags, and the suggestion query -// filters it out anyway — enriching it would be wasted API budget. -func TestCandidateTags_InLibraryCandidateIsExcluded(t *testing.T) { - pool := newPool(t) - seed := seedArtist(t, pool, "Seed", "") - seedArtist(t, pool, "Already Here", "cand-in-lib") - seedUnmatched(t, pool, seed.ID, "cand-in-lib", "Already Here", 0.9) - - if rows := listEligible(t, pool, 10); len(rows) != 0 { - t.Errorf("len = %d, want 0: %v", len(rows), mbidsOf(rows)) - } -} - -func TestCandidateTags_SettledWithTagsIsExcluded(t *testing.T) { - pool := newPool(t) - seed := seedArtist(t, pool, "Seed", "") - seedUnmatched(t, pool, seed.ID, "cand-1", "Candidate One", 0.9) - setState(t, pool, "cand-1", "musicbrainz", tagVersionCurrent) - - if rows := listEligible(t, pool, 10); len(rows) != 0 { - t.Errorf("len = %d, want 0 (already enriched): %v", len(rows), mbidsOf(rows)) - } -} - -// A candidate that settled 'none' becomes eligible again when the provider set -// widens (version bump) — that's the whole point of the version column. It must -// NOT be eligible at the current version, or the worker re-fetches it forever. -func TestCandidateTags_SettledNoneReopensOnlyOnVersionBump(t *testing.T) { - pool := newPool(t) - seed := seedArtist(t, pool, "Seed", "") - seedUnmatched(t, pool, seed.ID, "cand-1", "Candidate One", 0.9) - - setState(t, pool, "cand-1", "none", tagVersionCurrent) - if rows := listEligible(t, pool, 10); len(rows) != 0 { - t.Errorf("current version: len = %d, want 0 (settled)", len(rows)) - } - - setState(t, pool, "cand-1", "none", tagVersionOld) - if rows := listEligible(t, pool, 10); len(rows) != 1 { - t.Errorf("older version: len = %d, want 1 (eligible again)", len(rows)) - } -} - -// artist_similarity_unmatched holds one row per (seed, candidate, source). -// Without the GROUP BY, a candidate that five seeds point at would be fetched -// five times — five times the MusicBrainz budget for identical data. -func TestCandidateTags_ManySeedsCollapseToOneRowAndSumScores(t *testing.T) { - pool := newPool(t) - seedA := seedArtist(t, pool, "Seed A", "") - seedB := seedArtist(t, pool, "Seed B", "") - seedUnmatched(t, pool, seedA.ID, "cand-1", "Candidate One", 0.4) - seedUnmatched(t, pool, seedB.ID, "cand-1", "Candidate One", 0.3) - - rows := listEligible(t, pool, 10) - if len(rows) != 1 { - t.Fatalf("len = %d, want 1 (grouped): %v", len(rows), mbidsOf(rows)) - } - if got := rows[0].TotalScore; got < 0.69 || got > 0.71 { - t.Errorf("total_score = %v, want ~0.7 (summed across seeds)", got) - } -} - -// The pool is far larger than one pass can drain at ~1 req/s, so the ordering -// IS the feature: the strongest candidates must be enriched first, or the ones -// that actually reach a user's deck starve behind the long tail. -func TestCandidateTags_StrongestCandidatesComeFirst(t *testing.T) { - pool := newPool(t) - seed := seedArtist(t, pool, "Seed", "") - seedUnmatched(t, pool, seed.ID, "weak", "Weak", 0.1) - seedUnmatched(t, pool, seed.ID, "strong", "Strong", 0.95) - seedUnmatched(t, pool, seed.ID, "middle", "Middle", 0.5) - - rows := listEligible(t, pool, 10) - want := []string{"strong", "middle", "weak"} - got := mbidsOf(rows) - if len(got) != 3 { - t.Fatalf("len = %d, want 3: %v", len(got), got) - } - for i := range want { - if got[i] != want[i] { - t.Fatalf("order = %v, want %v", got, want) - } - } - - // And the limit takes the strongest, not an arbitrary slice. - if top := mbidsOf(listEligible(t, pool, 1)); len(top) != 1 || top[0] != "strong" { - t.Errorf("limit 1 returned %v, want [strong]", top) - } -} - -func TestCandidateTags_InsertKeepsTheStrongerWeight(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - ins := func(w float64) { - if err := q.InsertCandidateArtistTag(ctx, dbq.InsertCandidateArtistTagParams{ - CandidateMbid: "cand-1", Tag: "shoegaze", Weight: w, - }); err != nil { - t.Fatalf("InsertCandidateArtistTag: %v", err) - } - } - ins(0.8) - ins(0.3) // weaker second write must not clobber - - rows, err := q.ListCandidateArtistTagsForMbids(ctx, []string{"cand-1"}) - if err != nil { - t.Fatalf("ListCandidateArtistTagsForMbids: %v", err) - } - if len(rows) != 1 { - t.Fatalf("len = %d, want 1", len(rows)) - } - if rows[0].Weight != 0.8 { - t.Errorf("weight = %v, want 0.8 (GREATEST)", rows[0].Weight) - } -} - -func TestCandidateTags_ListForMbidsIgnoresUnaskedCandidates(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - for _, mbid := range []string{"want-1", "want-2", "other"} { - if err := q.InsertCandidateArtistTag(ctx, dbq.InsertCandidateArtistTagParams{ - CandidateMbid: mbid, Tag: "rock", Weight: 1, - }); err != nil { - t.Fatalf("insert %s: %v", mbid, err) - } - } - rows, err := q.ListCandidateArtistTagsForMbids(ctx, []string{"want-1", "want-2"}) - if err != nil { - t.Fatalf("ListCandidateArtistTagsForMbids: %v", err) - } - if len(rows) != 2 { - t.Errorf("len = %d, want 2", len(rows)) - } - for _, r := range rows { - if r.CandidateMbid == "other" { - t.Error("returned a candidate that wasn't asked for") - } - } -} - -// The similarity feed is refetched and churns, so without the sweep the cache -// only grows. Both halves must survive/die together for the right candidates. -func TestCandidateTags_GcDropsOrphansAndKeepsLiveOnes(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - - seed := seedArtist(t, pool, "Seed", "") - seedUnmatched(t, pool, seed.ID, "live", "Live", 0.9) - // "gone" is cached but no longer in the feed; "adopted" got added to the - // library since, so its tags belong in track_tags now. - seedArtist(t, pool, "Adopted", "adopted") - seedUnmatched(t, pool, seed.ID, "adopted", "Adopted", 0.8) - - for _, mbid := range []string{"live", "gone", "adopted"} { - if err := q.InsertCandidateArtistTag(ctx, dbq.InsertCandidateArtistTagParams{ - CandidateMbid: mbid, Tag: "rock", Weight: 1, - }); err != nil { - t.Fatalf("insert %s: %v", mbid, err) - } - setState(t, pool, mbid, "musicbrainz", tagVersionCurrent) - } - - deleted, err := q.GcDeleteOrphanedCandidateArtistTags(ctx) - if err != nil { - t.Fatalf("GcDeleteOrphanedCandidateArtistTags: %v", err) - } - if deleted != 2 { - t.Errorf("deleted %d tag rows, want 2 (gone + adopted)", deleted) - } - deletedState, err := q.GcDeleteOrphanedCandidateArtistTagState(ctx) - if err != nil { - t.Fatalf("GcDeleteOrphanedCandidateArtistTagState: %v", err) - } - if deletedState != 2 { - t.Errorf("deleted %d state rows, want 2", deletedState) - } - - rows, err := q.ListCandidateArtistTagsForMbids(ctx, []string{"live", "gone", "adopted"}) - if err != nil { - t.Fatalf("ListCandidateArtistTagsForMbids: %v", err) - } - if len(rows) != 1 || rows[0].CandidateMbid != "live" { - t.Errorf("survivors = %v, want [live] only", rows) - } -} - -// Coverage is the admin's window onto the honest ceiling: processed vs -// actually-tagged. A big gap means thin upstream data, not a broken worker. -func TestCandidateTags_CoverageCountsProcessedAndTagged(t *testing.T) { - pool := newPool(t) - setState(t, pool, "has-tags", "musicbrainz", tagVersionCurrent) - setState(t, pool, "mixed-tags", "mixed", tagVersionCurrent) - setState(t, pool, "no-tags", "none", tagVersionCurrent) - - got, err := dbq.New(pool).CountCandidateArtistTagCoverage(context.Background()) - if err != nil { - t.Fatalf("CountCandidateArtistTagCoverage: %v", err) - } - if got.Processed != 3 { - t.Errorf("processed = %d, want 3", got.Processed) - } - if got.WithTags != 2 { - t.Errorf("with_tags = %d, want 2 ('none' excluded)", got.WithTags) - } -} - -// --- The taste-tag blend, end to end --- -// -// The pure tests in tagoverlap_test.go cover the scoring maths. These cover the -// wiring the pure tests cannot reach: that loadTagInputs actually reads both -// sides from the DB and that the blend reaches the returned deck. - -func seedTasteTag(t *testing.T, pool *pgxpool.Pool, userID pgtype.UUID, tag string, weight float64) { - t.Helper() - if _, err := pool.Exec(context.Background(), - `INSERT INTO taste_profile_tags (user_id, tag, weight) VALUES ($1, $2, $3) - ON CONFLICT (user_id, tag) DO UPDATE SET weight = EXCLUDED.weight`, - userID, tag, weight, - ); err != nil { - t.Fatalf("seedTasteTag: %v", err) - } -} - -func seedCandidateTag(t *testing.T, pool *pgxpool.Pool, mbid, tag string, weight float64) { - t.Helper() - if err := dbq.New(pool).InsertCandidateArtistTag(context.Background(), - dbq.InsertCandidateArtistTagParams{CandidateMbid: mbid, Tag: tag, Weight: weight}, - ); err != nil { - t.Fatalf("seedCandidateTag: %v", err) - } -} - -// twoCandidatePool wires a liked seed with two unmatched neighbours: "loud" -// scores higher on similarity, "match" lower. Skewed on purpose — with equal -// similarity the reorder assertion below could not fail. -func twoCandidatePool(t *testing.T, pool *pgxpool.Pool, userID pgtype.UUID) { - t.Helper() - seed := seedArtist(t, pool, "Seed", "") - likeArtist(t, pool, userID, seed.ID) - seedUnmatched(t, pool, seed.ID, "loud", "Loud Neighbour", 0.9) - seedUnmatched(t, pool, seed.ID, "match", "Taste Match", 0.6) -} - -func TestSuggestArtists_TasteTagMatchOvertakesStrongerSimilarity(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - twoCandidatePool(t, pool, user.ID) - - seedTasteTag(t, pool, user.ID, "shoegaze", 10) - seedCandidateTag(t, pool, "match", "shoegaze", 1.0) - seedCandidateTag(t, pool, "loud", "death metal", 1.0) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 1.0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 2 { - t.Fatalf("len = %d, want 2", len(out)) - } - if out[0].MBID != "match" { - t.Errorf("first = %q, want match (0.6×2 beats 0.9×1)", out[0].MBID) - } - // And it explains itself. - if len(out[0].MatchedTags) != 1 || out[0].MatchedTags[0] != "shoegaze" { - t.Errorf("matched tags = %v, want [shoegaze]", out[0].MatchedTags) - } - if out[1].MatchedTags != nil { - t.Errorf("non-matching candidate got matched tags: %v", out[1].MatchedTags) - } -} - -// The same fixture with the knob at 0 must return pure similarity order — the -// off switch, verified against a real DB rather than assumed. -func TestSuggestArtists_ZeroTagWeightKeepsSimilarityOrder(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - twoCandidatePool(t, pool, user.ID) - - seedTasteTag(t, pool, user.ID, "shoegaze", 10) - seedCandidateTag(t, pool, "match", "shoegaze", 1.0) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if out[0].MBID != "loud" { - t.Errorf("first = %q, want loud (tag term disabled)", out[0].MBID) - } -} - -// A user with no taste profile must still get a full deck in similarity order: -// the cold-start path, which is every user's first days. -func TestSuggestArtists_NoTasteTagsStillReturnsSimilarityOrder(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - twoCandidatePool(t, pool, user.ID) - // Candidate tags exist, but the user has no taste tags to match them. - seedCandidateTag(t, pool, "match", "shoegaze", 1.0) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 1.0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 2 { - t.Fatalf("len = %d, want 2 (nothing dropped)", len(out)) - } - if out[0].MBID != "loud" { - t.Errorf("first = %q, want loud", out[0].MBID) - } -} - -// An untagged candidate must never be dropped or sunk just because another -// candidate has tags — permanently-partial coverage must not become a -// permanent ranking penalty. -func TestSuggestArtists_UntaggedCandidateSurvivesAlongsideTagged(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - twoCandidatePool(t, pool, user.ID) - - seedTasteTag(t, pool, user.ID, "shoegaze", 10) - seedCandidateTag(t, pool, "match", "shoegaze", 1.0) - // "loud" is deliberately left with NO cached tags at all. - - // Compared against the same request with the term disabled, rather than - // against a literal. The pool score is signal-weighted by the seed query - // (ln(1+signal) x similarity), so hardcoding a number here would assert - // against the seeding maths — a different layer — and break whenever that - // changes. The property under test is only that the blend leaves an - // untagged candidate alone. - blended, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 1.0) - if err != nil { - t.Fatalf("SuggestArtists(blended): %v", err) - } - baseline, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists(baseline): %v", err) - } - if len(blended) != 2 { - t.Fatalf("len = %d, want 2 — the untagged candidate must still appear", len(blended)) - } - - scoreOf := func(out []ArtistSuggestion, mbid string) (float64, bool) { - for _, s := range out { - if s.MBID == mbid { - return s.Score, true - } - } - return 0, false - } - want, ok := scoreOf(baseline, "loud") - if !ok { - t.Fatal("untagged candidate missing from the baseline deck") - } - got, ok := scoreOf(blended, "loud") - if !ok { - t.Fatal("untagged candidate vanished once the tag term was enabled") - } - if got != want { - t.Errorf("untagged score = %v, want %v (identical to the term-disabled run)", got, want) - } - // Sanity: the tagged candidate DID move, so the comparison above is - // meaningful rather than both runs being trivially identical. - if tagged, _ := scoreOf(blended, "match"); tagged == mustScore(t, baseline, "match") { - t.Error("the tagged candidate's score did not change — the term did nothing") - } -} - -func mustScore(t *testing.T, out []ArtistSuggestion, mbid string) float64 { - t.Helper() - for _, s := range out { - if s.MBID == mbid { - return s.Score - } - } - t.Fatalf("no candidate %q in %v", mbid, out) - return 0 -} diff --git a/internal/recommendation/fixtures_integration_test.go b/internal/recommendation/fixtures_integration_test.go new file mode 100644 index 00000000..49929624 --- /dev/null +++ b/internal/recommendation/fixtures_integration_test.go @@ -0,0 +1,121 @@ +package recommendation + +// Shared fixtures for this package's integration tests. + +import ( + "context" + "io" + "log/slog" + "os" + "testing" + "time" + + "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" + + "git.fabledsword.com/bvandeusen/minstrel/internal/db" + "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" + "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" +) + +func newPool(t *testing.T) *pgxpool.Pool { + t.Helper() + if testing.Short() { + t.Skip("skipping integration test in -short mode") + } + dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") + if dsn == "" { + t.Skip("MINSTREL_TEST_DATABASE_URL not set") + } + if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { + t.Fatalf("migrate: %v", err) + } + pool, err := pgxpool.New(context.Background(), dsn) + if err != nil { + t.Fatalf("pool: %v", err) + } + t.Cleanup(pool.Close) + dbtest.ResetDB(t, pool) + return pool +} + +func seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User { + t.Helper() + u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{ + Username: dbtest.TestUserPrefix + name, PasswordHash: "x", + ApiTokenHash: name + "-token", IsAdmin: false, + }) + if err != nil { + t.Fatalf("seed user: %v", err) + } + return u +} + +func seedArtist(t *testing.T, pool *pgxpool.Pool, name, mbid string) dbq.Artist { + t.Helper() + var mbidPtr *string + if mbid != "" { + mbidPtr = &mbid + } + a, err := dbq.New(pool).UpsertArtist(context.Background(), dbq.UpsertArtistParams{ + Name: name, SortName: name, Mbid: mbidPtr, + }) + if err != nil { + t.Fatalf("seed artist: %v", err) + } + return a +} + +func seedAlbumForArtist(t *testing.T, pool *pgxpool.Pool, artistID pgtype.UUID, title string) dbq.Album { + t.Helper() + a, err := dbq.New(pool).UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{ + Title: title, SortTitle: title, ArtistID: artistID, + }) + if err != nil { + t.Fatalf("seed album: %v", err) + } + return a +} + +func seedTrackOnAlbum(t *testing.T, pool *pgxpool.Pool, albumID, artistID pgtype.UUID, title string) dbq.Track { + t.Helper() + tr, err := dbq.New(pool).UpsertTrack(context.Background(), dbq.UpsertTrackParams{ + Title: title, AlbumID: albumID, ArtistID: artistID, + DurationMs: 1000, FilePath: "/tmp/m5c-" + title + ".mp3", + FileSize: 1, FileFormat: "mp3", + }) + if err != nil { + t.Fatalf("seed track: %v", err) + } + return tr +} + +func insertPlayEvent(t *testing.T, pool *pgxpool.Pool, userID, trackID pgtype.UUID, startedAt time.Time) { + t.Helper() + ctx := context.Background() + var sessionID pgtype.UUID + if err := pool.QueryRow(ctx, + `INSERT INTO play_sessions (user_id, started_at, last_event_at, client_id) + VALUES ($1, $2, $2, 'm5c-test') RETURNING id`, + userID, startedAt, + ).Scan(&sessionID); err != nil { + t.Fatalf("insert play_session: %v", err) + } + if _, err := pool.Exec(ctx, + `INSERT INTO play_events (user_id, track_id, session_id, started_at, was_skipped) + VALUES ($1, $2, $3, $4, false)`, + userID, trackID, sessionID, startedAt, + ); err != nil { + t.Fatalf("insert play_event: %v", err) + } +} + +func likeArtist(t *testing.T, pool *pgxpool.Pool, userID, artistID pgtype.UUID) { + t.Helper() + if _, err := pool.Exec(context.Background(), + `INSERT INTO general_likes_artists (user_id, artist_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`, + userID, artistID, + ); err != nil { + t.Fatalf("like artist: %v", err) + } +} diff --git a/internal/recommendation/suggestions.go b/internal/recommendation/suggestions.go deleted file mode 100644 index b7cb8b4f..00000000 --- a/internal/recommendation/suggestions.go +++ /dev/null @@ -1,322 +0,0 @@ -// suggestions.go is the per-user artist-suggestion service behind the -// Discover request surface. Seeds from the taste profile (falling back to -// likes + completed plays for a user who has none yet), projects those seeds -// through artist_similarity_unmatched via a single CTE, and returns top-N -// out-of-library candidates with top-3 attribution seeds resolved to names. -// -// Seeding from the taste profile rather than raw likes + plays matters: raw -// counts grow without bound and count skips as engagement, so a few -// heavily-played artists would monopolize every slot and entrench harder the -// more the user listened. See internal/db/queries/recommendation.sql for the -// tiering. -package recommendation - -import ( - "context" - "crypto/md5" - "encoding/hex" - "fmt" - "sort" - "time" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -// ArtistSuggestion is one ranked candidate with its top-3 attribution seeds. -type ArtistSuggestion struct { - MBID string - Name string - Score float64 - Attribution []SeedContribution - // MatchedTags are the candidate's own tags that overlap the user's taste - // profile, strongest first (max 3) — the "matches: shoegaze, melancholic" - // explanation. Empty when the candidate has no cached tags, which is - // common and not an error: coverage is permanently partial. - MatchedTags []string - // TagOverlap is the [0,1] share of the candidate's tag mass the user likes. - // Exposed for admin tuning — seeing the term's actual distribution is how - // a weight gets picked rather than guessed. - TagOverlap float64 -} - -// SeedContribution is one of the top-3 contributing seeds for a candidate. -type SeedContribution struct { - ArtistID pgtype.UUID - Name string - Contribution float64 - IsLiked bool - PlayCount int64 -} - -// SuggestArtists returns top-N artist suggestions for the user. limit is -// capped at 50 (default 12 when out of range). -// -// halfLifeDays (default 30) is the recency-decay half-life for the TIER-2 -// seed path only — the likes + completed-plays fallback used while the user -// has no taste-profile rows yet. Once the profile is populated it seeds -// instead, carrying its own decay, so this knob stops applying. -func SuggestArtists( - ctx context.Context, pool *pgxpool.Pool, userID pgtype.UUID, - halfLifeDays float64, limit int, tagOverlapWeight float64, -) ([]ArtistSuggestion, error) { - if limit <= 0 || limit > 50 { - limit = 12 - } - if halfLifeDays <= 0 { - halfLifeDays = 30 - } - q := dbq.New(pool) - // Over-fetch so there is something to rotate through. A deterministic - // top-N over a score ordering shows the same faces every day until one is - // requested away, leaving the tail of the ranking unreachable. Selection - // from this pool happens in selectSuggestions. - rows, err := q.SuggestArtistsForUser(ctx, dbq.SuggestArtistsForUserParams{ - UserID: userID, - Column2: halfLifeDays, - Limit: int32(poolSizeFor(limit)), - }) - if err != nil { - return nil, fmt.Errorf("suggest: query: %w", err) - } - if len(rows) == 0 { - return []ArtistSuggestion{}, nil - } - - // Collect the union of top-3 seed IDs across all rows for one batched - // name lookup. pgtype.UUID is a comparable struct so it works as a map - // key directly. - seedSet := make(map[pgtype.UUID]struct{}, len(rows)*3) - for _, r := range rows { - for _, sid := range r.TopSeedIds { - seedSet[sid] = struct{}{} - } - } - seedIDs := make([]pgtype.UUID, 0, len(seedSet)) - for id := range seedSet { - seedIDs = append(seedIDs, id) - } - artists, err := q.GetArtistsByIDs(ctx, seedIDs) - if err != nil { - return nil, fmt.Errorf("suggest: resolve seeds: %w", err) - } - nameByID := make(map[pgtype.UUID]string, len(artists)) - for _, a := range artists { - nameByID[a.ID] = a.Name - } - - out := make([]ArtistSuggestion, 0, len(rows)) - for _, r := range rows { - attribution := make([]SeedContribution, 0, len(r.TopSeedIds)) - for i, sid := range r.TopSeedIds { - if i >= len(r.TopContributions) || i >= len(r.TopIsLiked) || i >= len(r.TopPlayCounts) { - break - } - attribution = append(attribution, SeedContribution{ - ArtistID: sid, - Name: nameByID[sid], - Contribution: r.TopContributions[i], - IsLiked: r.TopIsLiked[i], - PlayCount: r.TopPlayCounts[i], - }) - } - out = append(out, ArtistSuggestion{ - MBID: r.CandidateMbid, - Name: r.CandidateName, - Score: r.TotalScore, - Attribution: attribution, - }) - } - - // Taste-tag term. Applied to the whole over-fetched pool BEFORE - // selection, so the rotation and diversity rules in selectSuggestions - // operate on taste-blended scores — boosting only the twelve already - // chosen by similarity would leave the actual re-ranking undone. - // - // A query error here is returned, NOT degraded past. Graceful degradation - // is for expected absence — no taste profile yet, no cached tags for a - // candidate — and both of those are handled explicitly as empty inputs - // below. A failing query is neither: swallowing it would hide a broken DB - // behind a subtly worse ranking that nothing reports. - tasteTags, candTags, err := loadTagInputs(ctx, q, userID, out) - if err != nil { - return nil, err - } - out = applyTagOverlap(out, candTags, tasteTags, tagOverlapWeight) - - return selectSuggestions(out, limit, rotationDay(time.Now())), nil -} - -// tasteTagLimit caps how many of the user's taste tags participate. The -// profile's long tail is near-zero weight and contributes nothing after -// normalization, so this bounds the query rather than the meaning. -const tasteTagLimit = 50 - -// loadTagInputs fetches both sides of the overlap comparison: the user's taste -// tags and the cached tags for exactly the candidates in this pool. -func loadTagInputs( - ctx context.Context, q *dbq.Queries, userID pgtype.UUID, pool []ArtistSuggestion, -) (TagWeights, map[string]TagWeights, error) { - tasteRows, err := q.ListTasteProfileTagsForUser(ctx, dbq.ListTasteProfileTagsForUserParams{ - UserID: userID, - Limit: tasteTagLimit, - }) - if err != nil { - return nil, nil, fmt.Errorf("suggest: taste tags: %w", err) - } - // No taste tags is a cold start, not a failure — return early and skip the - // candidate-tag fetch entirely, since nothing could match. - if len(tasteRows) == 0 { - return nil, nil, nil - } - taste := make(TagWeights, len(tasteRows)) - for _, r := range tasteRows { - taste[r.Tag] = r.Weight - } - - mbids := make([]string, 0, len(pool)) - for _, s := range pool { - mbids = append(mbids, s.MBID) - } - tagRows, err := q.ListCandidateArtistTagsForMbids(ctx, mbids) - if err != nil { - return nil, nil, fmt.Errorf("suggest: candidate tags: %w", err) - } - byCandidate := make(map[string]TagWeights, len(pool)) - for _, r := range tagRows { - if byCandidate[r.CandidateMbid] == nil { - byCandidate[r.CandidateMbid] = TagWeights{} - } - byCandidate[r.CandidateMbid][r.Tag] = r.Weight - } - return taste, byCandidate, nil -} - -// Pool multiplier: how many scored candidates to fetch per slot shown, so the -// rotation has somewhere to rotate. 4x keeps a day's deck genuinely different -// from yesterday's without pulling the whole long tail (whose scores are noise) -// into every request. -const suggestionPoolFactor = 4 - -// Hard ceiling on the fetched pool. The scored tail past this is low-signal, so -// paying for it would buy churn rather than better suggestions. -const suggestionPoolMax = 60 - -func poolSizeFor(limit int) int { - n := limit * suggestionPoolFactor - if n > suggestionPoolMax { - return suggestionPoolMax - } - return n -} - -// rotationDay is the bucket the daily rotation hashes against. Server-local -// date, matching the `current_date` the Home rows already rotate on, so the -// whole product turns over at the same moment. -func rotationDay(now time.Time) string { - return now.Format("2006-01-02") -} - -// selectSuggestions turns the scored pool into the response. -// -// Pure by design — no DB, no clock — so the rotation and diversity rules are -// unit-testable without Postgres. Callers pass the day bucket in. -// -// Three rules, in order: -// -// 1. Diversity cap. Walking in score order, a candidate is dropped once its -// dominant seed already owns maxPerSeed slots, so twelve slots cannot all -// be neighbours of one artist and read as a single narrow cluster. -// 2. Head. The best few by score always lead, so the strongest matches are -// never rotated out of sight. Mirrors For You's head/tail shape. -// 3. Tail. The rest of the slots are drawn from the remaining pool ordered by -// md5(mbid + day) — the same daily-stable idiom the Home rows use. Stable -// within a day, different tomorrow, and it needs no stored state. -func selectSuggestions(pool []ArtistSuggestion, limit int, day string) []ArtistSuggestion { - if len(pool) <= limit { - return pool - } - preferred, overflow := partitionPerSeed(pool, maxPerSeedFor(limit)) - - headCount := limit / 3 - if headCount > len(preferred) { - headCount = len(preferred) - } - out := make([]ArtistSuggestion, 0, limit) - out = append(out, preferred[:headCount]...) - - // Hash once per candidate, not once per comparison. - rest := preferred[headCount:] - tail := make([]rotatable, 0, len(rest)) - for _, s := range rest { - tail = append(tail, rotatable{key: rotationKey(s.MBID, day), suggestion: s}) - } - sort.SliceStable(tail, func(i, j int) bool { return tail[i].key < tail[j].key }) - for _, r := range tail { - if len(out) >= limit { - break - } - out = append(out, r.suggestion) - } - // Diversity is a PREFERENCE, not a quota that may starve the deck. A user - // whose whole pool hangs off one or two seeds would otherwise get a - // three-card surface, which is worse than the monoculture it avoids. - // Top up - // in score order from what the cap set aside. - for _, s := range overflow { - if len(out) >= limit { - break - } - out = append(out, s) - } - return out -} - -// rotatable pairs a candidate with its precomputed daily rotation key. -type rotatable struct { - key string - suggestion ArtistSuggestion -} - -// maxPerSeedFor keeps roughly a quarter of the deck attributable to any single -// seed artist — enough for a strong affinity to be well represented, not enough -// for it to BE the deck. Floored at 1 so a small limit still returns something. -func maxPerSeedFor(limit int) int { - n := limit / 4 - if n < 1 { - return 1 - } - return n -} - -// partitionPerSeed splits the pool by whether each candidate's dominant -// (highest-contributing) seed still has allowance left. Input must be in score -// order; both outputs preserve it. -// -// Candidates with no attribution are always preferred — there is no seed to -// attribute them to, so they cannot be the cause of a monoculture. -func partitionPerSeed(pool []ArtistSuggestion, maxPerSeed int) (preferred, overflow []ArtistSuggestion) { - perSeed := make(map[pgtype.UUID]int, len(pool)) - preferred = make([]ArtistSuggestion, 0, len(pool)) - for _, s := range pool { - if len(s.Attribution) == 0 { - preferred = append(preferred, s) - continue - } - dominant := s.Attribution[0].ArtistID - if perSeed[dominant] >= maxPerSeed { - overflow = append(overflow, s) - continue - } - perSeed[dominant]++ - preferred = append(preferred, s) - } - return preferred, overflow -} - -func rotationKey(mbid, day string) string { - sum := md5.Sum([]byte(mbid + day)) - return hex.EncodeToString(sum[:]) -} diff --git a/internal/recommendation/suggestions_integration_test.go b/internal/recommendation/suggestions_integration_test.go deleted file mode 100644 index e8f50bd8..00000000 --- a/internal/recommendation/suggestions_integration_test.go +++ /dev/null @@ -1,687 +0,0 @@ -package recommendation - -import ( - "context" - "fmt" - "io" - "log/slog" - "os" - "testing" - "time" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" -) - -func newPool(t *testing.T) *pgxpool.Pool { - t.Helper() - if testing.Short() { - t.Skip("skipping integration test in -short mode") - } - dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") - if dsn == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { - t.Fatalf("migrate: %v", err) - } - pool, err := pgxpool.New(context.Background(), dsn) - if err != nil { - t.Fatalf("pool: %v", err) - } - t.Cleanup(pool.Close) - dbtest.ResetDB(t, pool) - return pool -} - -func seedUser(t *testing.T, pool *pgxpool.Pool, name string) dbq.User { - t.Helper() - u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{ - Username: dbtest.TestUserPrefix + name, PasswordHash: "x", - ApiTokenHash: name + "-token", IsAdmin: false, - }) - if err != nil { - t.Fatalf("seed user: %v", err) - } - return u -} - -func seedArtist(t *testing.T, pool *pgxpool.Pool, name, mbid string) dbq.Artist { - t.Helper() - var mbidPtr *string - if mbid != "" { - mbidPtr = &mbid - } - a, err := dbq.New(pool).UpsertArtist(context.Background(), dbq.UpsertArtistParams{ - Name: name, SortName: name, Mbid: mbidPtr, - }) - if err != nil { - t.Fatalf("seed artist: %v", err) - } - return a -} - -func seedAlbumForArtist(t *testing.T, pool *pgxpool.Pool, artistID pgtype.UUID, title string) dbq.Album { - t.Helper() - a, err := dbq.New(pool).UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{ - Title: title, SortTitle: title, ArtistID: artistID, - }) - if err != nil { - t.Fatalf("seed album: %v", err) - } - return a -} - -func seedTrackOnAlbum(t *testing.T, pool *pgxpool.Pool, albumID, artistID pgtype.UUID, title string) dbq.Track { - t.Helper() - tr, err := dbq.New(pool).UpsertTrack(context.Background(), dbq.UpsertTrackParams{ - Title: title, AlbumID: albumID, ArtistID: artistID, - DurationMs: 1000, FilePath: "/tmp/m5c-" + title + ".mp3", - FileSize: 1, FileFormat: "mp3", - }) - if err != nil { - t.Fatalf("seed track: %v", err) - } - return tr -} - -func insertPlayEvent(t *testing.T, pool *pgxpool.Pool, userID, trackID pgtype.UUID, startedAt time.Time) { - t.Helper() - ctx := context.Background() - var sessionID pgtype.UUID - if err := pool.QueryRow(ctx, - `INSERT INTO play_sessions (user_id, started_at, last_event_at, client_id) - VALUES ($1, $2, $2, 'm5c-test') RETURNING id`, - userID, startedAt, - ).Scan(&sessionID); err != nil { - t.Fatalf("insert play_session: %v", err) - } - if _, err := pool.Exec(ctx, - `INSERT INTO play_events (user_id, track_id, session_id, started_at, was_skipped) - VALUES ($1, $2, $3, $4, false)`, - userID, trackID, sessionID, startedAt, - ); err != nil { - t.Fatalf("insert play_event: %v", err) - } -} - -func likeArtist(t *testing.T, pool *pgxpool.Pool, userID, artistID pgtype.UUID) { - t.Helper() - if _, err := pool.Exec(context.Background(), - `INSERT INTO general_likes_artists (user_id, artist_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`, - userID, artistID, - ); err != nil { - t.Fatalf("like artist: %v", err) - } -} - -func seedUnmatched(t *testing.T, pool *pgxpool.Pool, seedID pgtype.UUID, candMBID, candName string, score float64) { - t.Helper() - if err := dbq.New(pool).UpsertArtistSimilarityUnmatched(context.Background(), dbq.UpsertArtistSimilarityUnmatchedParams{ - SeedArtistID: seedID, - CandidateMbid: candMBID, - CandidateName: candName, - Score: score, - Source: "listenbrainz", - }); err != nil { - t.Fatalf("seed unmatched: %v", err) - } -} - -func TestSuggestArtists_LikesAndPlaysContributeToScore(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seedA := seedArtist(t, pool, "Seed Liked", "") - seedB := seedArtist(t, pool, "Seed Played", "") - - likeArtist(t, pool, user.ID, seedA.ID) - - seedBAlbum := seedAlbumForArtist(t, pool, seedB.ID, "Album B") - seedBTrack := seedTrackOnAlbum(t, pool, seedBAlbum.ID, seedB.ID, "Track B") - insertPlayEvent(t, pool, user.ID, seedBTrack.ID, time.Now().Add(-1*time.Hour)) - - seedUnmatched(t, pool, seedA.ID, "out-mbid", "Outsider", 0.9) - seedUnmatched(t, pool, seedB.ID, "out-mbid", "Outsider", 0.5) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 1 { - t.Fatalf("len = %d, want 1", len(out)) - } - s := out[0] - if s.MBID != "out-mbid" || s.Name != "Outsider" { - t.Errorf("got = %+v", s) - } - if s.Score <= 0 { - t.Errorf("score = %v, want > 0", s.Score) - } - if len(s.Attribution) != 2 { - t.Errorf("attribution len = %d, want 2", len(s.Attribution)) - } -} - -func TestSuggestArtists_Top12Cap(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seed := seedArtist(t, pool, "Seed", "") - likeArtist(t, pool, user.ID, seed.ID) - for i := 0; i < 30; i++ { - seedUnmatched(t, pool, seed.ID, fmt.Sprintf("mbid-%02d", i), fmt.Sprintf("Artist %02d", i), 0.99-float64(i)*0.01) - } - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 12 { - t.Errorf("len = %d, want 12", len(out)) - } - if out[0].MBID != "mbid-00" { - t.Errorf("first = %s, want mbid-00 (highest score)", out[0].MBID) - } -} - -func TestSuggestArtists_AttributionTopThree(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seeds := make([]dbq.Artist, 5) - for i := 0; i < 5; i++ { - seeds[i] = seedArtist(t, pool, fmt.Sprintf("Seed %d", i), "") - likeArtist(t, pool, user.ID, seeds[i].ID) - seedUnmatched(t, pool, seeds[i].ID, "shared-mbid", "Shared", 0.9-float64(i)*0.1) - } - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 1 { - t.Fatalf("len = %d, want 1 (shared candidate)", len(out)) - } - if got := len(out[0].Attribution); got != 3 { - t.Errorf("attribution len = %d, want 3", got) - } - if out[0].Attribution[0].Name != "Seed 0" { - t.Errorf("top attribution = %q, want Seed 0", out[0].Attribution[0].Name) - } -} - -func TestSuggestArtists_RecencyDecayDownweightsOldPlays(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - recentSeed := seedArtist(t, pool, "Recent", "") - oldSeed := seedArtist(t, pool, "Old", "") - - rAlbum := seedAlbumForArtist(t, pool, recentSeed.ID, "Recent Album") - rTrack := seedTrackOnAlbum(t, pool, rAlbum.ID, recentSeed.ID, "Recent Track") - insertPlayEvent(t, pool, user.ID, rTrack.ID, time.Now().Add(-1*24*time.Hour)) - - oAlbum := seedAlbumForArtist(t, pool, oldSeed.ID, "Old Album") - oTrack := seedTrackOnAlbum(t, pool, oAlbum.ID, oldSeed.ID, "Old Track") - insertPlayEvent(t, pool, user.ID, oTrack.ID, time.Now().Add(-90*24*time.Hour)) - - seedUnmatched(t, pool, recentSeed.ID, "cand", "Cand", 0.5) - seedUnmatched(t, pool, oldSeed.ID, "cand", "Cand", 0.5) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 1 { - t.Fatalf("len = %d, want 1", len(out)) - } - if len(out[0].Attribution) != 2 { - t.Fatalf("attribution len = %d, want 2", len(out[0].Attribution)) - } - if out[0].Attribution[0].Name != "Recent" { - t.Errorf("top attribution = %q, want Recent", out[0].Attribution[0].Name) - } - if out[0].Attribution[0].Contribution <= out[0].Attribution[1].Contribution { - t.Errorf("recent contribution (%v) should exceed old (%v)", - out[0].Attribution[0].Contribution, out[0].Attribution[1].Contribution) - } -} - -func TestSuggestArtists_FiltersInLibraryCandidates(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seed := seedArtist(t, pool, "Seed", "") - likeArtist(t, pool, user.ID, seed.ID) - inLibMBID := "in-lib-mbid" - seedArtist(t, pool, "InLib", inLibMBID) - seedUnmatched(t, pool, seed.ID, inLibMBID, "InLib", 0.9) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 0 { - t.Errorf("len = %d, want 0 (in-library candidate should be filtered)", len(out)) - } -} - -func TestSuggestArtists_FiltersAlreadyRequested(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seed := seedArtist(t, pool, "Seed", "") - likeArtist(t, pool, user.ID, seed.ID) - seedUnmatched(t, pool, seed.ID, "req-mbid", "Pending Request", 0.9) - if _, err := dbq.New(pool).CreateRequest(context.Background(), dbq.CreateRequestParams{ - UserID: user.ID, - Kind: dbq.RequestKindArtist, - ArtistMbid: "req-mbid", - ArtistName: "Pending Request", - }); err != nil { - t.Fatalf("CreateRequest: %v", err) - } - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 0 { - t.Errorf("len = %d, want 0 (pending request should hide candidate)", len(out)) - } -} - -func TestSuggestArtists_RejectedRequestStillShown(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seed := seedArtist(t, pool, "Seed", "") - likeArtist(t, pool, user.ID, seed.ID) - seedUnmatched(t, pool, seed.ID, "rej-mbid", "Rejected Once", 0.9) - req, err := dbq.New(pool).CreateRequest(context.Background(), dbq.CreateRequestParams{ - UserID: user.ID, - Kind: dbq.RequestKindArtist, - ArtistMbid: "rej-mbid", - ArtistName: "Rejected Once", - }) - if err != nil { - t.Fatalf("CreateRequest: %v", err) - } - rejNotes := "wrong artist" - if _, err := dbq.New(pool).RejectRequest(context.Background(), dbq.RejectRequestParams{ - ID: req.ID, Notes: &rejNotes, DecidedBy: user.ID, - }); err != nil { - t.Fatalf("RejectRequest: %v", err) - } - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 1 { - t.Errorf("len = %d, want 1 (rejected requests don't hide the candidate)", len(out)) - } -} - -func TestSuggestArtists_EmptyForNewUser(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "newbie") - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 0 { - t.Errorf("len = %d, want 0 (new user has no signal)", len(out)) - } -} - -// --- Taste-profile seeding, tiering, and skipped plays --- - -func setTasteWeight(t *testing.T, pool *pgxpool.Pool, userID, artistID pgtype.UUID, weight float64) { - t.Helper() - if _, err := pool.Exec(context.Background(), - `INSERT INTO taste_profile_artists (user_id, artist_id, weight) VALUES ($1, $2, $3) - ON CONFLICT (user_id, artist_id) DO UPDATE SET weight = EXCLUDED.weight`, - userID, artistID, weight, - ); err != nil { - t.Fatalf("set taste weight: %v", err) - } -} - -func insertSkippedPlayEvent(t *testing.T, pool *pgxpool.Pool, userID, trackID pgtype.UUID, startedAt time.Time) { - t.Helper() - ctx := context.Background() - var sessionID pgtype.UUID - if err := pool.QueryRow(ctx, - `INSERT INTO play_sessions (user_id, started_at, last_event_at, client_id) - VALUES ($1, $2, $2, 'skip-test') RETURNING id`, - userID, startedAt, - ).Scan(&sessionID); err != nil { - t.Fatalf("insert play_session: %v", err) - } - if _, err := pool.Exec(ctx, - `INSERT INTO play_events (user_id, track_id, session_id, started_at, was_skipped) - VALUES ($1, $2, $3, $4, true)`, - userID, trackID, sessionID, startedAt, - ); err != nil { - t.Fatalf("insert skipped play_event: %v", err) - } -} - -// Tier 1: a taste-profile weight alone seeds a suggestion, with no like and no -// play on the seed artist. -func TestSuggestArtists_TasteProfileWeightSeedsTier1(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seed := seedArtist(t, pool, "Profile Seed", "") - - setTasteWeight(t, pool, user.ID, seed.ID, 4.0) - seedUnmatched(t, pool, seed.ID, "out-mbid", "Outsider", 0.9) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 1 { - t.Fatalf("len = %d, want 1 (taste weight alone should seed)", len(out)) - } - if out[0].MBID != "out-mbid" { - t.Errorf("mbid = %q, want out-mbid", out[0].MBID) - } - if out[0].Score <= 0 { - t.Errorf("score = %v, want > 0", out[0].Score) - } -} - -// Once the profile has any positive row, tier 2 is not consulted — so an artist -// the user played but that the taste engine did not keep does NOT seed. That is -// the point of the rewrite: the taste engine decides what counts as affinity. -func TestSuggestArtists_TasteProfileSupersedesRawPlays(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - kept := seedArtist(t, pool, "Kept By Taste", "") - dropped := seedArtist(t, pool, "Dropped By Taste", "") - - setTasteWeight(t, pool, user.ID, kept.ID, 3.0) - - // `dropped` has real completed plays but no profile row. - album := seedAlbumForArtist(t, pool, dropped.ID, "Album") - track := seedTrackOnAlbum(t, pool, album.ID, dropped.ID, "Track") - insertPlayEvent(t, pool, user.ID, track.ID, time.Now().Add(-1*time.Hour)) - - seedUnmatched(t, pool, kept.ID, "kept-cand", "Kept Candidate", 0.9) - seedUnmatched(t, pool, dropped.ID, "dropped-cand", "Dropped Candidate", 0.9) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 1 { - t.Fatalf("len = %d, want 1 (tier 2 must not run alongside tier 1)", len(out)) - } - if out[0].MBID != "kept-cand" { - t.Errorf("mbid = %q, want kept-cand", out[0].MBID) - } -} - -// A non-positive taste weight is not affinity. Guarded by a second, positive -// row so tier 1 stays active — otherwise an empty tier 1 would fall through to -// tier 2 and the assertion would pass for the wrong reason. -func TestSuggestArtists_NonPositiveTasteWeightDoesNotSeed(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - positive := seedArtist(t, pool, "Still Liked", "") - abandoned := seedArtist(t, pool, "Abandoned", "") - - setTasteWeight(t, pool, user.ID, positive.ID, 2.0) - setTasteWeight(t, pool, user.ID, abandoned.ID, -1.5) - - seedUnmatched(t, pool, positive.ID, "pos-cand", "Positive Candidate", 0.9) - seedUnmatched(t, pool, abandoned.ID, "neg-cand", "Negative Candidate", 0.9) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - for _, s := range out { - if s.MBID == "neg-cand" { - t.Fatalf("negative-weight artist seeded a suggestion: %+v", s) - } - } - if len(out) != 1 || out[0].MBID != "pos-cand" { - t.Errorf("out = %+v, want only pos-cand", out) - } -} - -// Tier 2 counts COMPLETED plays only. Counting every play_event would make -// skipping an artist repeatedly increase its signal and push more of its -// neighbours at the user. -func TestSuggestArtists_SkippedPlaysDoNotSeedTier2(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - skipped := seedArtist(t, pool, "Only Skipped", "") - - album := seedAlbumForArtist(t, pool, skipped.ID, "Album") - track := seedTrackOnAlbum(t, pool, album.ID, skipped.ID, "Track") - for i := 0; i < 5; i++ { - insertSkippedPlayEvent(t, pool, user.ID, track.ID, time.Now().Add(-time.Duration(i+1)*time.Hour)) - } - seedUnmatched(t, pool, skipped.ID, "skip-cand", "Skip Candidate", 0.9) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 0 { - t.Errorf("len = %d, want 0 (skips are not affinity): %+v", len(out), out) - } -} - -// --- Time-boxed suggestion snooze --- -// -// The defining property under test is that a snooze EXPIRES: suggestions have -// no permanent "never show" control. A permanent dismissal would pass most of -// these; only TestSuggestArtists_ExpiredSnooze distinguishes the two. - -// snoozeUntil inserts a snooze row with an explicit absolute expiry, so a -// test can place it in the past without depending on the duration arithmetic -// in SnoozeSuggestion. -func snoozeUntil(t *testing.T, pool *pgxpool.Pool, userID pgtype.UUID, mbid, name string, until time.Time) { - t.Helper() - if _, err := pool.Exec(context.Background(), - `INSERT INTO suggestion_snoozes (user_id, candidate_mbid, candidate_name, snoozed_until) - VALUES ($1, $2, $3, $4) - ON CONFLICT (user_id, candidate_mbid) DO UPDATE SET snoozed_until = EXCLUDED.snoozed_until`, - userID, mbid, name, until, - ); err != nil { - t.Fatalf("snoozeUntil: %v", err) - } -} - -// seedOneCandidate wires the minimum that puts exactly one candidate in the -// deck: a liked seed artist with one unmatched neighbour. -func seedOneCandidate(t *testing.T, pool *pgxpool.Pool, userID pgtype.UUID, mbid, name string) { - t.Helper() - seed := seedArtist(t, pool, "Seed", "") - likeArtist(t, pool, userID, seed.ID) - seedUnmatched(t, pool, seed.ID, mbid, name, 0.9) -} - -func TestSuggestArtists_ActiveSnoozeHidesCandidate(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seedOneCandidate(t, pool, user.ID, "snoozed-mbid", "Parked Artist") - - if err := dbq.New(pool).SnoozeSuggestion(context.Background(), dbq.SnoozeSuggestionParams{ - UserID: user.ID, - CandidateMbid: "snoozed-mbid", - CandidateName: "Parked Artist", - Column4: 90, - }); err != nil { - t.Fatalf("SnoozeSuggestion: %v", err) - } - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 0 { - t.Errorf("len = %d, want 0 (active snooze should hide the candidate): %+v", len(out), out) - } -} - -// The whole point of a snooze over a dismissal: it comes back on its own. -func TestSuggestArtists_ExpiredSnoozeShowsCandidateAgain(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seedOneCandidate(t, pool, user.ID, "expired-mbid", "Returning Artist") - snoozeUntil(t, pool, user.ID, "expired-mbid", "Returning Artist", time.Now().Add(-time.Hour)) - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 1 { - t.Fatalf("len = %d, want 1 (an expired snooze must not hide anything): %+v", len(out), out) - } - if out[0].MBID != "expired-mbid" { - t.Errorf("mbid = %q, want expired-mbid", out[0].MBID) - } -} - -// One household member parking a suggestion must not remove it -// from anyone else's deck. -func TestSuggestArtists_SnoozeIsPerUser(t *testing.T) { - pool := newPool(t) - alice := seedUser(t, pool, "alice") - bob := seedUser(t, pool, "bob") - - seed := seedArtist(t, pool, "Seed", "") - likeArtist(t, pool, alice.ID, seed.ID) - likeArtist(t, pool, bob.ID, seed.ID) - seedUnmatched(t, pool, seed.ID, "shared-mbid", "Shared Candidate", 0.9) - - snoozeUntil(t, pool, alice.ID, "shared-mbid", "Shared Candidate", time.Now().Add(24*time.Hour)) - - aliceOut, err := SuggestArtists(context.Background(), pool, alice.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists(alice): %v", err) - } - if len(aliceOut) != 0 { - t.Errorf("alice len = %d, want 0 (she snoozed it)", len(aliceOut)) - } - bobOut, err := SuggestArtists(context.Background(), pool, bob.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists(bob): %v", err) - } - if len(bobOut) != 1 { - t.Errorf("bob len = %d, want 1 (alice's snooze is not his)", len(bobOut)) - } -} - -func TestUnsnoozeSuggestion_RestoresImmediately(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - seedOneCandidate(t, pool, user.ID, "undo-mbid", "Undo Artist") - snoozeUntil(t, pool, user.ID, "undo-mbid", "Undo Artist", time.Now().Add(90*24*time.Hour)) - - q := dbq.New(pool) - rows, err := q.UnsnoozeSuggestion(context.Background(), dbq.UnsnoozeSuggestionParams{ - UserID: user.ID, CandidateMbid: "undo-mbid", - }) - if err != nil { - t.Fatalf("UnsnoozeSuggestion: %v", err) - } - if rows != 1 { - t.Errorf("rows = %d, want 1", rows) - } - // A second delete affects nothing — the handler turns this into a 404 - // rather than reporting success for a no-op. - rows, err = q.UnsnoozeSuggestion(context.Background(), dbq.UnsnoozeSuggestionParams{ - UserID: user.ID, CandidateMbid: "undo-mbid", - }) - if err != nil { - t.Fatalf("UnsnoozeSuggestion (repeat): %v", err) - } - if rows != 0 { - t.Errorf("repeat rows = %d, want 0", rows) - } - - out, err := SuggestArtists(context.Background(), pool, user.ID, 30, 12, 0) - if err != nil { - t.Fatalf("SuggestArtists: %v", err) - } - if len(out) != 1 { - t.Errorf("len = %d, want 1 (unsnooze restores the candidate)", len(out)) - } -} - -// Re-snoozing must extend, not conflict on the PK. -func TestSnoozeSuggestion_UpsertExtends(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - q := dbq.New(pool) - - snoozeUntil(t, pool, user.ID, "extend-mbid", "Old Name", time.Now().Add(time.Hour)) - if err := q.SnoozeSuggestion(context.Background(), dbq.SnoozeSuggestionParams{ - UserID: user.ID, - CandidateMbid: "extend-mbid", - CandidateName: "New Name", - Column4: 90, - }); err != nil { - t.Fatalf("SnoozeSuggestion (re-snooze): %v", err) - } - - rows, err := q.ListActiveSuggestionSnoozes(context.Background(), user.ID) - if err != nil { - t.Fatalf("ListActiveSuggestionSnoozes: %v", err) - } - if len(rows) != 1 { - t.Fatalf("len = %d, want 1 (upsert, not a second row)", len(rows)) - } - if rows[0].CandidateName != "New Name" { - t.Errorf("name = %q, want New Name (upsert refreshes it)", rows[0].CandidateName) - } - if got := time.Until(rows[0].SnoozedUntil.Time); got < 80*24*time.Hour { - t.Errorf("snoozed_until is %v away, want ~90d (re-snooze should extend)", got) - } -} - -// ListActiveSuggestionSnoozes filters expired rows itself rather than -// trusting the hourly gc sweep to have run. -func TestListActiveSuggestionSnoozes_ExcludesExpired(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - snoozeUntil(t, pool, user.ID, "live-mbid", "Live", time.Now().Add(24*time.Hour)) - snoozeUntil(t, pool, user.ID, "dead-mbid", "Dead", time.Now().Add(-24*time.Hour)) - - rows, err := dbq.New(pool).ListActiveSuggestionSnoozes(context.Background(), user.ID) - if err != nil { - t.Fatalf("ListActiveSuggestionSnoozes: %v", err) - } - if len(rows) != 1 { - t.Fatalf("len = %d, want 1 (expired row must not be listed)", len(rows)) - } - if rows[0].CandidateMbid != "live-mbid" { - t.Errorf("mbid = %q, want live-mbid", rows[0].CandidateMbid) - } -} - -func TestGcDeleteExpiredSuggestionSnoozes_KeepsActive(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "alice") - snoozeUntil(t, pool, user.ID, "live-mbid", "Live", time.Now().Add(24*time.Hour)) - snoozeUntil(t, pool, user.ID, "dead-mbid", "Dead", time.Now().Add(-24*time.Hour)) - - q := dbq.New(pool) - deleted, err := q.GcDeleteExpiredSuggestionSnoozes(context.Background()) - if err != nil { - t.Fatalf("GcDeleteExpiredSuggestionSnoozes: %v", err) - } - if deleted != 1 { - t.Errorf("deleted = %d, want 1 (only the expired row)", deleted) - } - rows, err := q.ListActiveSuggestionSnoozes(context.Background(), user.ID) - if err != nil { - t.Fatalf("ListActiveSuggestionSnoozes: %v", err) - } - if len(rows) != 1 { - t.Errorf("len = %d, want 1 (active snooze survives the sweep)", len(rows)) - } -} diff --git a/internal/recommendation/suggestions_select_test.go b/internal/recommendation/suggestions_select_test.go deleted file mode 100644 index 138a3a73..00000000 --- a/internal/recommendation/suggestions_select_test.go +++ /dev/null @@ -1,169 +0,0 @@ -package recommendation - -import ( - "fmt" - "testing" - - "github.com/jackc/pgx/v5/pgtype" -) - -// selectSuggestions is deliberately pure (no DB, no clock), so the rotation and -// diversity rules are covered here in the fast lane rather -// than behind the integration gate. - -func seedID(n byte) pgtype.UUID { - var u pgtype.UUID - u.Bytes[0] = n - u.Valid = true - return u -} - -// candidate builds a pool entry attributed to the given dominant seed. -func candidate(mbid string, score float64, dominant byte) ArtistSuggestion { - return ArtistSuggestion{ - MBID: mbid, - Name: mbid, - Score: score, - Attribution: []SeedContribution{ - {ArtistID: seedID(dominant), Contribution: score}, - }, - } -} - -// poolOf returns n candidates in descending score order, spread across -// `seeds` distinct dominant seeds. -func poolOf(n int, seeds int) []ArtistSuggestion { - out := make([]ArtistSuggestion, 0, n) - for i := 0; i < n; i++ { - out = append(out, candidate(fmt.Sprintf("mbid-%02d", i), 1.0-float64(i)*0.01, byte(i%seeds))) - } - return out -} - -func mbids(in []ArtistSuggestion) []string { - out := make([]string, 0, len(in)) - for _, s := range in { - out = append(out, s.MBID) - } - return out -} - -func TestSelectSuggestions_ReturnsPoolUnchangedWhenNotOverfetched(t *testing.T) { - pool := poolOf(5, 5) - got := selectSuggestions(pool, 12, "2026-08-01") - if len(got) != 5 { - t.Fatalf("len = %d, want 5 (nothing to rotate)", len(got)) - } - if got[0].MBID != "mbid-00" { - t.Errorf("first = %q, want mbid-00", got[0].MBID) - } -} - -func TestSelectSuggestions_FillsTheRequestedLimit(t *testing.T) { - got := selectSuggestions(poolOf(48, 8), 12, "2026-08-01") - if len(got) != 12 { - t.Fatalf("len = %d, want 12", len(got)) - } -} - -// The reported symptom: the deck must change day to day without the user -// requesting anything. -func TestSelectSuggestions_RotatesAcrossDays(t *testing.T) { - pool := poolOf(48, 8) - day1 := mbids(selectSuggestions(pool, 12, "2026-08-01")) - day2 := mbids(selectSuggestions(pool, 12, "2026-08-02")) - - if fmt.Sprint(day1) == fmt.Sprint(day2) { - t.Fatalf("deck identical across days: %v", day1) - } - // ...but stable WITHIN a day, or the surface would reshuffle on every - // pull-to-refresh, which reads as broken rather than fresh. - again := mbids(selectSuggestions(pool, 12, "2026-08-01")) - if fmt.Sprint(day1) != fmt.Sprint(again) { - t.Errorf("same day differed:\n %v\n %v", day1, again) - } -} - -// The strongest matches should never rotate out of sight. -func TestSelectSuggestions_HeadIsStableTopScorers(t *testing.T) { - pool := poolOf(48, 8) - for _, day := range []string{"2026-08-01", "2026-08-02", "2026-09-15"} { - got := selectSuggestions(pool, 12, day) - if got[0].MBID != "mbid-00" { - t.Errorf("day %s: first = %q, want mbid-00 (top score leads)", day, got[0].MBID) - } - } -} - -// Twelve slots must not all be neighbours of one artist. -// -// The pool is deliberately SKEWED — one seed owns the entire top of the score -// ranking — because that is the only shape where a cap can bite. With scores -// spread evenly across seeds the top-N is already diverse and the cap is -// untestable: an even pool cannot fail this test. -func TestSelectSuggestions_CapsOneDominantSeed(t *testing.T) { - const dominantRun = 20 - pool := make([]ArtistSuggestion, 0, 48) - for i := 0; i < dominantRun; i++ { // seed 0 owns the 20 best scores - pool = append(pool, candidate(fmt.Sprintf("dom-%02d", i), 1.0-float64(i)*0.01, 0)) - } - for i := 0; i < 28; i++ { // seeds 1..9 hold everything below - pool = append(pool, candidate(fmt.Sprintf("oth-%02d", i), 0.80-float64(i)*0.01, byte(1+i%9))) - } - - got := selectSuggestions(pool, 12, "2026-08-01") - if len(got) != 12 { - t.Fatalf("len = %d, want 12", len(got)) - } - - perSeed := map[pgtype.UUID]int{} - for _, s := range got { - perSeed[s.Attribution[0].ArtistID]++ - } - // Uncapped, the top 12 by score would be 12 of seed 0's neighbours. - if n := perSeed[seedID(0)]; n > maxPerSeedFor(12) { - t.Errorf("dominant seed owns %d of 12 slots, want <= %d: %v", - n, maxPerSeedFor(12), mbids(got)) - } - if len(perSeed) < 4 { - t.Errorf("deck spans only %d seeds, want >= 4: %v", len(perSeed), mbids(got)) - } -} - -// Diversity must not starve the deck: a pool hanging off a single -// seed should still fill, not collapse to maxPerSeed entries. -func TestSelectSuggestions_SingleSeedPoolStillFills(t *testing.T) { - got := selectSuggestions(poolOf(30, 1), 12, "2026-08-01") - if len(got) != 12 { - t.Fatalf("len = %d, want 12 (cap must not starve the deck)", len(got)) - } - if got[0].MBID != "mbid-00" { - t.Errorf("first = %q, want mbid-00", got[0].MBID) - } -} - -// A candidate with no attribution has no seed to blame, so it must never be -// held back by the diversity cap. -func TestPartitionPerSeed_UnattributedNeverCapped(t *testing.T) { - pool := []ArtistSuggestion{ - candidate("a", 0.9, 1), - candidate("b", 0.8, 1), - {MBID: "orphan", Score: 0.1}, - } - preferred, overflow := partitionPerSeed(pool, 1) - if len(preferred) != 2 || preferred[1].MBID != "orphan" { - t.Errorf("preferred = %v, want [a orphan]", mbids(preferred)) - } - if len(overflow) != 1 || overflow[0].MBID != "b" { - t.Errorf("overflow = %v, want [b]", mbids(overflow)) - } -} - -func TestPoolSizeFor_OverfetchesButIsBounded(t *testing.T) { - if got := poolSizeFor(12); got != 48 { - t.Errorf("poolSizeFor(12) = %d, want 48", got) - } - if got := poolSizeFor(50); got != suggestionPoolMax { - t.Errorf("poolSizeFor(50) = %d, want the %d ceiling", got, suggestionPoolMax) - } -} diff --git a/internal/recommendation/tagoverlap.go b/internal/recommendation/tagoverlap.go deleted file mode 100644 index afde5eaf..00000000 --- a/internal/recommendation/tagoverlap.go +++ /dev/null @@ -1,152 +0,0 @@ -// tagoverlap.go — the taste-tag term for the Discover request surface. -// -// Similarity alone says "some artist you play is similar to it", a -// graph-adjacency fact that says nothing about whether the music sounds like -// anything you actually like. Here the candidate's own folksonomy tags (from -// the candidate tag cache) are compared against the user's taste-profile -// tags, so the surface can rank on "matches the sound you like" and say WHY. -// -// Pure by design — no DB, no clock — so the scoring rules are unit-testable in -// the fast lane rather than behind the integration gate. -package recommendation - -import "sort" - -// maxMatchedTags caps the "matches: …" explanation. Three is what the existing -// seed attribution shows, and a longer list stops being a reason and becomes a -// tag dump. -const maxMatchedTags = 3 - -// TagWeights is a tag → weight map. Both sides of the comparison use it: -// candidate tags (normalized [0,1] by the enrichment providers) and the user's -// taste-profile tags (accumulated, unbounded — normalized here). -type TagWeights map[string]float64 - -// tagOverlap scores how much of a candidate's tag identity the user actually -// likes, in [0,1], and returns the matched tags ordered by contribution. -// -// The measure is: of this candidate's total tag mass, what share sits on tags -// the user likes — each weighted by how strongly they like it? -// -// overlap = Σ(shared) candWeight × normalizedTasteWeight ÷ Σ(all) candWeight -// -// Normalizing the taste side by the user's STRONGEST tag is what makes this -// comparable across users: taste weights accumulate with listening, so a -// heavy listener's raw numbers dwarf a new user's while meaning the same -// thing — "this is my favourite tag". Dividing by the candidate's own total -// mass makes it comparable across candidates, so a densely-tagged artist -// can't out-score a sparsely-tagged one just by having more tags. -// -// Returns (0, nil) when either side is empty. That is the load-bearing -// degradation path: tag coverage for out-of-library candidates is permanently -// partial, and a cold-start user has no taste tags at all. Both must leave -// the candidate's similarity score untouched rather than sink it. -func tagOverlap(candidate, taste TagWeights) (float64, []string) { - if len(candidate) == 0 || len(taste) == 0 { - return 0, nil - } - - maxTaste := 0.0 - for _, w := range taste { - if w > maxTaste { - maxTaste = w - } - } - // Every taste weight <= 0 carries no preference to match against. Guarding - // here also avoids dividing by zero below. - if maxTaste <= 0 { - return 0, nil - } - - totalMass := 0.0 - for _, w := range candidate { - // Negative or zero candidate weights would let a tag subtract from the - // denominator and inflate the ratio past 1. - if w > 0 { - totalMass += w - } - } - if totalMass <= 0 { - return 0, nil - } - - type contribution struct { - tag string - score float64 - } - var matched []contribution - sum := 0.0 - for tag, candWeight := range candidate { - if candWeight <= 0 { - continue - } - tasteWeight, ok := taste[tag] - if !ok || tasteWeight <= 0 { - continue - } - c := candWeight * (tasteWeight / maxTaste) - sum += c - matched = append(matched, contribution{tag: tag, score: c}) - } - if len(matched) == 0 { - return 0, nil - } - - // Strongest contribution first; tag name breaks ties so the explanation is - // deterministic for a given input rather than map-iteration order. - sort.Slice(matched, func(i, j int) bool { - if matched[i].score != matched[j].score { - return matched[i].score > matched[j].score - } - return matched[i].tag < matched[j].tag - }) - names := make([]string, 0, min(len(matched), maxMatchedTags)) - for i := 0; i < len(matched) && i < maxMatchedTags; i++ { - names = append(names, matched[i].tag) - } - return sum / totalMass, names -} - -// applyTagOverlap re-scores and re-orders a candidate pool by taste-tag -// overlap, stamping the matched tags onto each suggestion for the UI. -// -// The blend is MULTIPLICATIVE — score × (1 + weight × overlap) — not additive, -// and the difference is the whole safety argument: -// -// - A candidate with no tags has overlap 0, so its score is EXACTLY -// unchanged. Partial tag coverage costs a candidate nothing. -// - Nothing can leapfrog on tags alone. An additive term with a large -// weight would let a near-zero-similarity artist outrank a strong match -// just for sharing a popular tag, which reads as noise to the user. -// - weight 0 disables the feature completely and restores pure similarity -// order, so the knob has a real off position. -// -// Callers must pass the pool in similarity order; it is returned in blended -// order. Mutates the elements in place (they're the caller's own slice built -// per request), and re-sorts, because selectSuggestions downstream relies on -// score order for its head/tail split. -func applyTagOverlap( - pool []ArtistSuggestion, candidateTags map[string]TagWeights, taste TagWeights, weight float64, -) []ArtistSuggestion { - // A zero weight turns the feature off. Skip the work AND the re-sort so - // the ordering is bit-for-bit pure similarity order. - if weight == 0 || len(taste) == 0 { - return pool - } - for i := range pool { - overlap, matched := tagOverlap(candidateTags[pool[i].MBID], taste) - pool[i].MatchedTags = matched - pool[i].TagOverlap = overlap - pool[i].Score *= 1 + weight*overlap - } - sort.SliceStable(pool, func(i, j int) bool { - if pool[i].Score != pool[j].Score { - return pool[i].Score > pool[j].Score - } - // Stable tiebreak by MBID. Without it, two candidates on equal scores - // could swap between requests within the same day, which the daily - // rotation exists to prevent. - return pool[i].MBID < pool[j].MBID - }) - return pool -} diff --git a/internal/recommendation/tagoverlap_test.go b/internal/recommendation/tagoverlap_test.go deleted file mode 100644 index e18cec86..00000000 --- a/internal/recommendation/tagoverlap_test.go +++ /dev/null @@ -1,243 +0,0 @@ -package recommendation - -import ( - "fmt" - "testing" -) - -// tagOverlap / applyTagOverlap are pure, so the taste-tag ranking rules are -// covered in the fast lane rather than behind the integration gate. -// -// Fixtures here are deliberately SKEWED — candidates that match the taste -// profile to clearly different degrees. An evenly-matching pool cannot exercise -// a re-ranking at all: every candidate gets the same multiplier and the order is -// unchanged whether the blend works or not. - -func TestTagOverlap_FullMatchScoresOne(t *testing.T) { - // Every unit of the candidate's tag mass sits on the user's single - // strongest tag → the whole mass matches at full strength. - got, matched := tagOverlap( - TagWeights{"shoegaze": 1.0}, - TagWeights{"shoegaze": 5.0}, - ) - if got != 1.0 { - t.Errorf("overlap = %v, want 1.0", got) - } - if len(matched) != 1 || matched[0] != "shoegaze" { - t.Errorf("matched = %v, want [shoegaze]", matched) - } -} - -func TestTagOverlap_NoSharedTagsScoresZero(t *testing.T) { - got, matched := tagOverlap( - TagWeights{"death metal": 1.0}, - TagWeights{"shoegaze": 5.0}, - ) - if got != 0 { - t.Errorf("overlap = %v, want 0", got) - } - if matched != nil { - t.Errorf("matched = %v, want nil", matched) - } -} - -// Half the candidate's mass is on a matching tag, and that tag is the user's -// strongest → 0.5. -func TestTagOverlap_PartialMassMatchIsProportional(t *testing.T) { - got, _ := tagOverlap( - TagWeights{"shoegaze": 1.0, "death metal": 1.0}, - TagWeights{"shoegaze": 5.0}, - ) - if got != 0.5 { - t.Errorf("overlap = %v, want 0.5", got) - } -} - -// Matching a tag the user barely likes must score below matching one they love. -func TestTagOverlap_WeakTasteTagScoresLowerThanStrong(t *testing.T) { - taste := TagWeights{"shoegaze": 10.0, "polka": 1.0} - strong, _ := tagOverlap(TagWeights{"shoegaze": 1.0}, taste) - weak, _ := tagOverlap(TagWeights{"polka": 1.0}, taste) - if !(strong > weak) { - t.Errorf("strong=%v weak=%v — a favourite tag must outscore a marginal one", strong, weak) - } - if weak != 0.1 { // 1.0 * (1/10) / 1.0 - t.Errorf("weak = %v, want 0.1", weak) - } -} - -// THE degradation path. Tag coverage for out-of-library candidates is -// permanently partial and cold-start users have no taste tags, so both must be -// scored 0 — never negative, never dropped. -func TestTagOverlap_EmptyEitherSideIsZeroNotNegative(t *testing.T) { - cases := []struct { - name string - candidate, taste TagWeights - }{ - {"no candidate tags", nil, TagWeights{"shoegaze": 5}}, - {"no taste tags", TagWeights{"shoegaze": 1}, nil}, - {"both empty", nil, nil}, - {"taste weights all zero", TagWeights{"shoegaze": 1}, TagWeights{"shoegaze": 0}}, - {"candidate weights all zero", TagWeights{"shoegaze": 0}, TagWeights{"shoegaze": 5}}, - } - for _, c := range cases { - got, matched := tagOverlap(c.candidate, c.taste) - if got != 0 { - t.Errorf("%s: overlap = %v, want 0", c.name, got) - } - if matched != nil { - t.Errorf("%s: matched = %v, want nil", c.name, matched) - } - } -} - -// A densely-tagged artist must not out-score a focused one merely by having -// more tags — that's what dividing by the candidate's own mass buys. -func TestTagOverlap_IsNotAPopularityContest(t *testing.T) { - taste := TagWeights{"shoegaze": 5.0} - focused, _ := tagOverlap(TagWeights{"shoegaze": 1.0}, taste) - sprawling, _ := tagOverlap(TagWeights{ - "shoegaze": 1.0, "rock": 1.0, "alternative": 1.0, "90s": 1.0, - }, taste) - if !(focused > sprawling) { - t.Errorf("focused=%v sprawling=%v — extra unmatched tags must dilute, not add", - focused, sprawling) - } -} - -// Taste weights accumulate with listening, so raw magnitudes differ wildly -// between a new user and a heavy one while meaning the same thing. Normalizing -// by the user's own strongest tag is what makes the score comparable. -func TestTagOverlap_IsInvariantToTasteMagnitude(t *testing.T) { - candidate := TagWeights{"shoegaze": 1.0, "dream pop": 1.0} - newUser, _ := tagOverlap(candidate, TagWeights{"shoegaze": 2.0, "polka": 1.0}) - heavyUser, _ := tagOverlap(candidate, TagWeights{"shoegaze": 2000.0, "polka": 1000.0}) - if newUser != heavyUser { - t.Errorf("newUser=%v heavyUser=%v — scaling all taste weights must not change the result", - newUser, heavyUser) - } -} - -func TestTagOverlap_MatchedTagsAreOrderedAndCapped(t *testing.T) { - got, matched := tagOverlap( - TagWeights{"a": 0.2, "b": 0.9, "c": 0.5, "d": 0.7}, - TagWeights{"a": 10, "b": 10, "c": 10, "d": 10}, - ) - if got <= 0 { - t.Fatalf("overlap = %v, want > 0", got) - } - // All taste weights equal, so candidate weight decides: b(.9) d(.7) c(.5). - want := []string{"b", "d", "c"} - if fmt.Sprint(matched) != fmt.Sprint(want) { - t.Errorf("matched = %v, want %v (strongest first, capped at %d)", - matched, want, maxMatchedTags) - } -} - -// --- applyTagOverlap --- - -func poolFor(specs ...struct { - mbid string - score float64 -}) []ArtistSuggestion { - out := make([]ArtistSuggestion, 0, len(specs)) - for _, s := range specs { - out = append(out, ArtistSuggestion{MBID: s.mbid, Name: s.mbid, Score: s.score}) - } - return out -} - -type spec = struct { - mbid string - score float64 -} - -// The payoff, and the reason the fixture is skewed: a taste-matching candidate -// that started BELOW another must be able to overtake it. With an evenly -// matching pool this assertion could not fail. -func TestApplyTagOverlap_TasteMatchOvertakesAStrongerNonMatch(t *testing.T) { - pool := poolFor(spec{"loud", 1.0}, spec{"match", 0.7}) - candTags := map[string]TagWeights{ - "loud": {"death metal": 1.0}, - "match": {"shoegaze": 1.0}, - } - taste := TagWeights{"shoegaze": 5.0} - - got := applyTagOverlap(pool, candTags, taste, 1.0) - if got[0].MBID != "match" { - t.Errorf("first = %q, want match (0.7×2 = 1.4 beats 1.0×1)", got[0].MBID) - } - if got[0].MatchedTags == nil { - t.Error("matched tags not stamped onto the winner") - } -} - -// An untagged candidate keeps its score EXACTLY. This is the multiplicative -// blend's whole safety argument: partial coverage costs a candidate nothing. -func TestApplyTagOverlap_UntaggedCandidateScoreIsUnchanged(t *testing.T) { - pool := poolFor(spec{"untagged", 0.9}) - got := applyTagOverlap(pool, map[string]TagWeights{}, TagWeights{"shoegaze": 5}, 1.0) - if got[0].Score != 0.9 { - t.Errorf("score = %v, want 0.9 exactly (no tags must not penalise)", got[0].Score) - } - if got[0].MatchedTags != nil { - t.Errorf("matched = %v, want nil", got[0].MatchedTags) - } -} - -// Weight 0 is the off switch: the ordering must be bit-for-bit pure similarity -// order, not merely similar. -func TestApplyTagOverlap_ZeroWeightIsAnExactNoOp(t *testing.T) { - pool := poolFor(spec{"a", 1.0}, spec{"b", 0.7}) - candTags := map[string]TagWeights{"b": {"shoegaze": 1.0}} - got := applyTagOverlap(pool, candTags, TagWeights{"shoegaze": 5.0}, 0) - if got[0].MBID != "a" || got[0].Score != 1.0 || got[1].Score != 0.7 { - t.Errorf("weight 0 changed the pool: %+v", got) - } - // And it must not stamp matched tags either — the UI would otherwise - // explain a boost that never happened. - if got[1].MatchedTags != nil { - t.Errorf("matched tags stamped while the feature is off: %v", got[1].MatchedTags) - } -} - -// A user with no taste profile is the cold-start case: every candidate scores -// the same multiplier of 1, so similarity order must survive intact. -func TestApplyTagOverlap_NoTasteProfileLeavesOrderIntact(t *testing.T) { - pool := poolFor(spec{"a", 1.0}, spec{"b", 0.7}, spec{"c", 0.4}) - candTags := map[string]TagWeights{"c": {"shoegaze": 1.0}} - got := applyTagOverlap(pool, candTags, nil, 1.0) - for i, want := range []string{"a", "b", "c"} { - if got[i].MBID != want { - t.Fatalf("order = %v..., want a b c", got[i].MBID) - } - } -} - -// Bounded boost: even a perfect match at the maximum weight can only multiply -// by (1 + w), so a candidate cannot be catapulted arbitrarily far. -func TestApplyTagOverlap_BoostIsBoundedByOnePlusWeight(t *testing.T) { - pool := poolFor(spec{"perfect", 1.0}) - got := applyTagOverlap(pool, - map[string]TagWeights{"perfect": {"shoegaze": 1.0}}, - TagWeights{"shoegaze": 5.0}, 2.0) - if got[0].Score != 3.0 { - t.Errorf("score = %v, want 3.0 (1.0 × (1 + 2×1))", got[0].Score) - } - if got[0].TagOverlap != 1.0 { - t.Errorf("TagOverlap = %v, want 1.0", got[0].TagOverlap) - } -} - -// Equal blended scores must resolve deterministically, or two candidates could -// swap between requests inside one day — the exact churn the daily rotation -// exists to prevent. -func TestApplyTagOverlap_TiesBreakDeterministically(t *testing.T) { - first := applyTagOverlap(poolFor(spec{"zzz", 1.0}, spec{"aaa", 1.0}), - map[string]TagWeights{}, TagWeights{"x": 1}, 1.0) - second := applyTagOverlap(poolFor(spec{"aaa", 1.0}, spec{"zzz", 1.0}), - map[string]TagWeights{}, TagWeights{"x": 1}, 1.0) - if first[0].MBID != "aaa" || second[0].MBID != "aaa" { - t.Errorf("tie order not deterministic: %q then %q", first[0].MBID, second[0].MBID) - } -} diff --git a/internal/recsettings/patch.go b/internal/recsettings/patch.go index 179c1e3b..6ae86833 100644 --- a/internal/recsettings/patch.go +++ b/internal/recsettings/patch.go @@ -161,70 +161,6 @@ func applyTastePatch(current TasteTuning, patch map[string]float64) (TasteTuning return next, changes, nil } -// Discover tuning bounds. -const ( - // A tag-overlap weight above this stops being a boost and becomes the - // ranking — at 10, a perfect match multiplies similarity by 11, which lets - // tag agreement swamp the similarity signal entirely. The bound is for - // typos, not to constrain exploration; the multiplicative blend keeps even - // the maximum from reordering an untagged candidate. - tagOverlapWeightMax = 10.0 - // Snooze duration: at least a day (anything less isn't a snooze, it's a - // flicker), at most a year — past that it's a permanent dismissal wearing a - // snooze's clothes, and suggestions have no permanent "never show" control. - snoozeDaysMin = 1.0 - snoozeDaysMax = 365.0 -) - -// applyDiscoverPatch validates and applies a partial Discover update. -func applyDiscoverPatch( - current DiscoverTuning, patch map[string]float64, -) (DiscoverTuning, []fieldChange, error) { - next := current - var changes []fieldChange - for field, v := range patch { - var target *float64 - switch field { - case "tag_overlap_weight": - if v < 0 || v > tagOverlapWeightMax { - return current, nil, fmt.Errorf("%w: %s = %v (must be in [0, %v])", - ErrOutOfRange, field, v, tagOverlapWeightMax) - } - target = &next.TagOverlapWeight - case "snooze_days": - if v < snoozeDaysMin || v > snoozeDaysMax { - return current, nil, fmt.Errorf("%w: %s = %v (must be in [%v, %v])", - ErrOutOfRange, field, v, snoozeDaysMin, snoozeDaysMax) - } - target = &next.SnoozeDays - default: - return current, nil, fmt.Errorf("%w: %q", ErrUnknownField, field) - } - if *target == v { - continue - } - changes = append(changes, fieldChange{Field: field, Old: *target, New: v}) - *target = v - } - return next, changes, nil -} - -// diffDiscover returns per-field changes from a to b (empty when equal). -func diffDiscover(a, b DiscoverTuning) []fieldChange { - var out []fieldChange - if a.TagOverlapWeight != b.TagOverlapWeight { - out = append(out, fieldChange{ - Field: "tag_overlap_weight", Old: a.TagOverlapWeight, New: b.TagOverlapWeight, - }) - } - if a.SnoozeDays != b.SnoozeDays { - out = append(out, fieldChange{ - Field: "snooze_days", Old: a.SnoozeDays, New: b.SnoozeDays, - }) - } - return out -} - // diffWeights returns per-field changes from a to b (empty when equal). func diffWeights(a, b recommendation.ScoringWeights) []fieldChange { var out []fieldChange diff --git a/internal/recsettings/service.go b/internal/recsettings/service.go index e333ff46..cb5f7145 100644 --- a/internal/recsettings/service.go +++ b/internal/recsettings/service.go @@ -1,6 +1,6 @@ // Package recsettings is the DB-backed home of the recommendation // tuning knobs: the scoring-weight profiles (radio, daily_mix, -// songs_like), Discover's settings, and the taste-profile build +// songs_like) and the taste-profile build // settings (engagement half-life + completion curve). It follows the coverart // SettingsService pattern — boot reconcile seeds shipped defaults for // missing rows, values are cached under a RWMutex, and every change @@ -34,11 +34,6 @@ const ( ScopeRadio = "radio" ScopeDailyMix = "daily_mix" ScopeTaste = "taste" - // ScopeDiscover is the Discover request surface. Its own scope rather - // than columns on taste: SnoozeDays lives here, and a snooze must never - // be read as taste signal — filing it under taste would put it one - // careless join from that leak. - ScopeDiscover = "discover" // ScopeSongsLike is the "Songs like {X}" surface. It is separate from // daily_mix because the two want opposite things: For-You answers "what // will they enjoy today" and is supposed to roam; Songs-like answers @@ -162,32 +157,6 @@ func shippedWeightsFor(scope string) (recommendation.ScoringWeights, bool) { return recommendation.ScoringWeights{}, false } -// DiscoverTuning is the tunable set for the Discover request surface. -type DiscoverTuning struct { - // TagOverlapWeight scales the taste-tag term: score × (1 + w × overlap). - // 0 disables it and restores pure similarity ranking. - TagOverlapWeight float64 - // SnoozeDays is the default "not right now" duration. - SnoozeDays float64 -} - -// ShippedDiscoverTuning are the shipped Discover defaults. -// -// TagOverlapWeight 1.0 lets a perfect tag match at most double a candidate's -// similarity score — enough to reorder the deck meaningfully, not enough for a -// popular-tag coincidence to beat a genuinely strong similarity match. It is a -// starting point, not a tuned value: the way to pick it is the metrics trend -// view after some real use. -// -// SnoozeDays 90: long enough that a parked suggestion stops nagging, short -// enough that a taste shift brings it back on its own. -func ShippedDiscoverTuning() DiscoverTuning { - return DiscoverTuning{ - TagOverlapWeight: 1.0, - SnoozeDays: 90, - } -} - // ShippedTasteTuning mirrors taste.DefaultConfig's tunable subset. func ShippedTasteTuning() TasteTuning { d := taste.DefaultConfig() @@ -211,7 +180,6 @@ type Service struct { mu sync.RWMutex profiles map[string]recommendation.ScoringWeights taste TasteTuning - discover DiscoverTuning } // New boots the service: seeds shipped defaults for missing rows, @@ -254,13 +222,6 @@ func (s *Service) reconcile(ctx context.Context) error { }); err != nil { return fmt.Errorf("seed taste tuning: %w", err) } - sd := ShippedDiscoverTuning() - if err := q.UpsertDiscoverTuningDefaults(ctx, dbq.UpsertDiscoverTuningDefaultsParams{ - TagOverlapWeight: sd.TagOverlapWeight, - SnoozeDays: sd.SnoozeDays, - }); err != nil { - return fmt.Errorf("seed discover tuning: %w", err) - } rows, err := q.ListWeightProfiles(ctx) if err != nil { @@ -270,10 +231,6 @@ func (s *Service) reconcile(ctx context.Context) error { if err != nil { return fmt.Errorf("get taste tuning: %w", err) } - dt, err := q.GetDiscoverTuning(ctx) - if err != nil { - return fmt.Errorf("get discover tuning: %w", err) - } s.mu.Lock() s.profiles = map[string]recommendation.ScoringWeights{} @@ -289,10 +246,6 @@ func (s *Service) reconcile(ctx context.Context) error { EraScale: tt.EraScale, MoodScale: tt.MoodScale, } - s.discover = DiscoverTuning{ - TagOverlapWeight: dt.TagOverlapWeight, - SnoozeDays: dt.SnoozeDays, - } s.mu.Unlock() s.push() @@ -327,15 +280,6 @@ func (s *Service) Taste() TasteTuning { return s.taste } -// Discover returns the cached Discover-tuning values. Read per request by the -// suggestions handler, so an admin change takes effect on the next refresh -// with no restart. -func (s *Service) Discover() DiscoverTuning { - s.mu.RLock() - defer s.mu.RUnlock() - return s.discover -} - // TasteConfig assembles the full taste.Config the profile builder // consumes: shipped non-tunable knobs (like bonuses, floors, caps) // plus the tuned half-life and curve. WindowDays scales with the @@ -395,19 +339,6 @@ func (s *Service) UpdateTaste(ctx context.Context, patch map[string]float64) err return s.persistTaste(ctx, next, "update", changes) } -// UpdateDiscover applies a partial update to the Discover tuning singleton. -func (s *Service) UpdateDiscover(ctx context.Context, patch map[string]float64) error { - current := s.Discover() - next, changes, err := applyDiscoverPatch(current, patch) - if err != nil { - return err - } - if len(changes) == 0 { - return nil - } - return s.persistDiscover(ctx, next, "update", changes) -} - // Reset restores a scope to its shipped defaults, with one audit row // carrying the full diff. A scope already at defaults is a no-op. func (s *Service) Reset(ctx context.Context, scope string) error { @@ -430,13 +361,6 @@ func (s *Service) Reset(ctx context.Context, scope string) error { return nil } return s.persistTaste(ctx, shipped, "reset", changes) - case ScopeDiscover: - shipped := ShippedDiscoverTuning() - changes := diffDiscover(s.Discover(), shipped) - if len(changes) == 0 { - return nil - } - return s.persistDiscover(ctx, shipped, "reset", changes) default: return fmt.Errorf("%w: %q", ErrUnknownScope, scope) } @@ -487,28 +411,6 @@ func (s *Service) persistTaste( return nil } -// persistDiscover writes the discover row + audit entry and refreshes the -// cache. No push(): unlike taste and daily_mix, nothing precomputes from these -// — the suggestions handler reads Discover() per request. -func (s *Service) persistDiscover( - ctx context.Context, d DiscoverTuning, action string, changes []fieldChange, -) error { - q := dbq.New(s.pool) - if _, err := q.UpdateDiscoverTuning(ctx, dbq.UpdateDiscoverTuningParams{ - TagOverlapWeight: d.TagOverlapWeight, - SnoozeDays: d.SnoozeDays, - }); err != nil { - return fmt.Errorf("update discover tuning: %w", err) - } - if err := s.audit(ctx, q, ScopeDiscover, action, changes); err != nil { - return err - } - s.mu.Lock() - s.discover = d - s.mu.Unlock() - return nil -} - // audit writes one recommendation_tuning_audit row. Changes are // sorted by field so rows are deterministic and diff-friendly. func (s *Service) audit( diff --git a/internal/recsettings/service_test.go b/internal/recsettings/service_test.go index 37bc3958..cf96f8a3 100644 --- a/internal/recsettings/service_test.go +++ b/internal/recsettings/service_test.go @@ -281,115 +281,3 @@ func TestUpdate_NoOpWritesNoAudit(t *testing.T) { t.Errorf("no-op update wrote %d audit rows, want 0", len(rows)) } } - -// --- Discover scope --- - -func TestNew_SeedsDiscoverDefaults(t *testing.T) { - pool := newPool(t) - s := newService(t, pool) - got := s.Discover() - want := ShippedDiscoverTuning() - if got != want { - t.Errorf("Discover() = %+v, want shipped %+v", got, want) - } -} - -func TestUpdateDiscover_PersistsAndAudits(t *testing.T) { - pool := newPool(t) - s := newService(t, pool) - if err := s.UpdateDiscover(context.Background(), map[string]float64{ - "tag_overlap_weight": 2.5, - "snooze_days": 30, - }); err != nil { - t.Fatalf("UpdateDiscover: %v", err) - } - if got := s.Discover().TagOverlapWeight; got != 2.5 { - t.Errorf("TagOverlapWeight = %v, want 2.5", got) - } - if got := s.Discover().SnoozeDays; got != 30 { - t.Errorf("SnoozeDays = %v, want 30", got) - } - - // The audit row must land under the new scope: a scope missing from - // recommendation_tuning_audit's CHECK fails this INSERT at runtime. - rows := auditRows(t, pool) - if len(rows) != 1 { - t.Fatalf("audit rows = %d, want 1", len(rows)) - } - if rows[0].Scope != ScopeDiscover { - t.Errorf("audit scope = %q, want %q", rows[0].Scope, ScopeDiscover) - } - if len(rows[0].Changes) != 2 { - t.Errorf("audit changes = %+v, want both fields", rows[0].Changes) - } - - // Reload from the DB to prove it persisted rather than only caching. - s2 := newService(t, pool) - if got := s2.Discover().TagOverlapWeight; got != 2.5 { - t.Errorf("after reload TagOverlapWeight = %v, want 2.5 (not re-seeded to shipped)", got) - } -} - -func TestUpdateDiscover_Validation(t *testing.T) { - pool := newPool(t) - s := newService(t, pool) - cases := []struct { - name string - patch map[string]float64 - }{ - {"unknown field", map[string]float64{"nope": 1}}, - {"negative weight", map[string]float64{"tag_overlap_weight": -1}}, - {"weight past the typo bound", map[string]float64{"tag_overlap_weight": 100}}, - // A sub-day snooze isn't a snooze, it's a flicker. - {"snooze under a day", map[string]float64{"snooze_days": 0.5}}, - // Past a year it's a permanent dismissal wearing a snooze's clothes. - {"snooze past a year", map[string]float64{"snooze_days": 400}}, - } - for _, c := range cases { - if err := s.UpdateDiscover(context.Background(), c.patch); err == nil { - t.Errorf("%s: expected rejection, got nil", c.name) - } - } - if got := s.Discover(); got != ShippedDiscoverTuning() { - t.Errorf("a rejected patch mutated state: %+v", got) - } - if rows := auditRows(t, pool); len(rows) != 0 { - t.Errorf("rejected patches wrote %d audit rows, want 0", len(rows)) - } -} - -// Weight 0 must be accepted: it's the off switch for the whole tag term, so -// a "must be positive" bound would leave no way to disable it. -func TestUpdateDiscover_ZeroWeightIsAllowed(t *testing.T) { - pool := newPool(t) - s := newService(t, pool) - if err := s.UpdateDiscover(context.Background(), - map[string]float64{"tag_overlap_weight": 0}); err != nil { - t.Fatalf("UpdateDiscover(0): %v", err) - } - if got := s.Discover().TagOverlapWeight; got != 0 { - t.Errorf("TagOverlapWeight = %v, want 0", got) - } -} - -func TestResetDiscover_RestoresShippedDefaults(t *testing.T) { - pool := newPool(t) - s := newService(t, pool) - if err := s.UpdateDiscover(context.Background(), - map[string]float64{"tag_overlap_weight": 4}); err != nil { - t.Fatalf("UpdateDiscover: %v", err) - } - if err := s.Reset(context.Background(), ScopeDiscover); err != nil { - t.Fatalf("Reset: %v", err) - } - if got := s.Discover(); got != ShippedDiscoverTuning() { - t.Errorf("after reset = %+v, want shipped %+v", got, ShippedDiscoverTuning()) - } - // Already-at-defaults is a no-op: update + reset = 2 rows, not 3. - if err := s.Reset(context.Background(), ScopeDiscover); err != nil { - t.Fatalf("second Reset: %v", err) - } - if rows := auditRows(t, pool); len(rows) != 2 { - t.Errorf("audit rows = %d, want 2 (the no-op reset must not audit)", len(rows)) - } -} diff --git a/internal/recsettings/songs_like_weights_test.go b/internal/recsettings/songs_like_weights_test.go index c4830d49..5c11766c 100644 --- a/internal/recsettings/songs_like_weights_test.go +++ b/internal/recsettings/songs_like_weights_test.go @@ -94,7 +94,7 @@ func TestShippedWeightsFor_CoversEveryWeightProfile(t *testing.T) { } // Non-weight scopes must NOT resolve here, or Reset would treat the taste // singleton as a weight profile and write nonsense. - for _, scope := range []string{ScopeTaste, ScopeDiscover, "nonsense"} { + for _, scope := range []string{ScopeTaste, "nonsense"} { if _, ok := shippedWeightsFor(scope); ok { t.Errorf("scope %q resolved as a weight profile and is not one", scope) } diff --git a/internal/release/agree.go b/internal/release/agree.go deleted file mode 100644 index d220408e..00000000 --- a/internal/release/agree.go +++ /dev/null @@ -1,40 +0,0 @@ -package release - -import "slices" - -// Comparing a file against a MusicBrainz track: shared by the importer's -// check of a download and the library listing's match of an album. - -// LengthFits allows five seconds or 3%, whichever is more: remasters and -// rips differ by a second or two, a different recording by far more. A -// track MusicBrainz has no length for says nothing against the file. -func LengthFits(fileMs, trackMs int) bool { - if trackMs <= 0 { - return true - } - if fileMs <= 0 { - return false - } - tol := max(5000, trackMs*3/100) - d := fileMs - trackMs - return d <= tol && -d <= tol -} - -// TitleAgrees: the same words, or one title holding all of the other's -// ("Get Lucky (Radio Edit)" holds "Get Lucky"). -func TitleAgrees(a, b string) bool { - ta, tb := Tokens(a), Tokens(b) - if len(ta) == 0 || len(tb) == 0 { - return false - } - return subset(ta, tb) || subset(tb, ta) -} - -func subset(small, big []string) bool { - for _, w := range small { - if !slices.Contains(big, w) { - return false - } - } - return true -} diff --git a/internal/release/agree_test.go b/internal/release/agree_test.go deleted file mode 100644 index 50910fe7..00000000 --- a/internal/release/agree_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package release - -import "testing" - -func TestLengthFits(t *testing.T) { - for _, c := range []struct { - file, track int - ok bool - }{ - // Short tracks get the 5s floor; longer ones 3% of the track. - {120000, 125000, true}, {120000, 126000, false}, - {240000, 246000, true}, {240000, 248000, false}, {600000, 615000, true}, {600000, 620000, false}, - {0, 240000, false}, {240000, 0, true}, - } { - if got := LengthFits(c.file, c.track); got != c.ok { - t.Errorf("LengthFits(%d, %d) = %v", c.file, c.track, got) - } - } -} diff --git a/internal/release/files.go b/internal/release/files.go deleted file mode 100644 index 2bfeca9f..00000000 --- a/internal/release/files.go +++ /dev/null @@ -1,76 +0,0 @@ -package release - -import ( - "path" - "strconv" - "strings" -) - -// File is one file inside a torrent. -type File struct { - Index int - Path string - Size int64 -} - -// Contents is what a torrent's file list holds. -type Contents struct { - Audio int - Video int - // Format is the format most of the audio files are in, "" when none. - Format string -} - -var videoExt = map[string]bool{ - "mkv": true, "mp4": true, "avi": true, "m2ts": true, "vob": true, - "mov": true, "webm": true, "wmv": true, "m4v": true, "ts": true, -} - -// Inspect counts a file list's audio and video files. -func Inspect(files []File) Contents { - var c Contents - formats := map[string]int{} - for _, f := range files { - ext := strings.TrimPrefix(strings.ToLower(path.Ext(f.Path)), ".") - if fm := formatOf(ext); fm != "" { - c.Audio++ - formats[fm]++ - } else if videoExt[ext] { - c.Video++ - } - } - best := 0 - for fm, n := range formats { - if n > best || (n == best && formatRank[fm] > formatRank[c.Format]) { - c.Format, best = fm, n - } - } - return c -} - -// JudgeFiles says what a torrent's files say about whether it is the album: -// a veto (code and sentence), or a doubt for a person, or nothing. -func JudgeFiles(w Want, files []File) (veto, reason, doubt string) { - if len(files) == 0 { - return "", "", "" - } - c := Inspect(files) - switch { - case c.Audio == 0 && c.Video > 0: - return VetoVideo, "It holds video files and no audio.", "" - case c.Audio == 0: - return VetoNoAudio, "It holds no audio files Minstrel can read.", "" - case w.MinTracks > 0 && c.Audio < w.MinTracks: - return VetoIncomplete, "It holds " + tracks(c.Audio) + "; the shortest edition has " + tracks(w.MinTracks) + ".", "" - case w.MaxTracks > 0 && c.Audio > 2*w.MaxTracks+4: - return "", "", "It holds " + tracks(c.Audio) + ", more than any edition of the album has: probably more than one album." - } - return "", "", "" -} - -func tracks(n int) string { - if n == 1 { - return "1 track" - } - return strconv.Itoa(n) + " tracks" -} diff --git a/internal/release/match.go b/internal/release/match.go deleted file mode 100644 index 3fb25006..00000000 --- a/internal/release/match.go +++ /dev/null @@ -1,210 +0,0 @@ -package release - -import "strings" - -// Want is the album a search is for, as the catalogue describes it. -type Want struct { - Artist string - Title string - Year int - PrimaryType string // Album, EP, Single, Broadcast, Other - SecondaryTypes []string - // MinTracks and MaxTracks span the group's editions' track counts; zero - // when the catalogue has not read them. - MinTracks int - MaxTracks int -} - -// Veto codes: why a name is not the album. Kept on a candidate as its -// reason's code. -const ( - VetoArtist = "wrong_artist" - VetoTitle = "wrong_title" - VetoVideo = "video" - VetoDiscography = "discography" - VetoVersion = "other_version" - VetoType = "wrong_type" - VetoNoAudio = "no_audio" - VetoIncomplete = "incomplete" -) - -// Match is what a name says about whether it is the album wanted. -type Match struct { - // Veto is a code from above when the name is not the album, with Reason - // saying why in a sentence. - Veto string - Reason string - // Doubt is set when the name may be the album and cannot say for sure: a - // person decides. It is a sentence. - Doubt string - // YearAgrees: the name states the album's year, give or take one. - YearAgrees bool -} - -// versions are words that make a release a different recording of the -// album, with what to call it. A word the wanted title or artist holds does -// not count ("Live Through This", "Instrumental Core"). -var versions = []struct { - words []string - what string - // typ is the MusicBrainz secondary type that wants this version. - typ string -}{ - {[]string{"instrumental", "instrumentals"}, "an instrumental version", ""}, - {[]string{"drumless"}, "a drumless edition", ""}, - {[]string{"karaoke"}, "a karaoke version", ""}, - {[]string{"acapella", "acappella", "accapella", "acapellas"}, "an a cappella version", ""}, - {[]string{"remix", "remixes", "remixed"}, "a remix release", "Remix"}, - {[]string{"live", "bootleg", "bbc", "sessions", "unplugged"}, "a live recording", "Live"}, - {[]string{"demo", "demos"}, "demo recordings", "Demo"}, - // Not "covers": in a release name that is usually the artwork scans. - {[]string{"tribute", "lullaby", "lullabies"}, "a tribute release", ""}, - {[]string{"nightcore", "slowed", "sped"}, "a slowed or sped-up edit", ""}, - {[]string{"commentary", "interview", "interviews"}, "commentary or interviews", "Interview"}, -} - -// collections are words naming more than one album. -var collections = []string{"collection", "anthology", "albums", "boxset", "discography"} - -// Judge says whether a release name is the album wanted. It looks only at -// the name; JudgeFiles looks at what a torrent holds. -func Judge(w Want, r Release) Match { - var m Match - m.YearAgrees = r.Year != 0 && w.Year != 0 && abs(r.Year-w.Year) <= 1 - if r.Video != "" { - m.Veto, m.Reason = VetoVideo, "A video, not audio (“"+r.Video+"”)." - return m - } - artist := Tokens(w.Artist) - title := Tokens(w.Title) - if len(artist) > 0 && !contains(r.Tokens, artist) && !contains(r.Tokens, withoutThe(artist)) { - m.Veto, m.Reason = VetoArtist, "The name doesn’t mention "+w.Artist+"." - return m - } - if len(title) > 0 && !contains(r.Tokens, title) && !contains(r.Tokens, bare(w.Title)) { - m.Veto, m.Reason = VetoTitle, "The name doesn’t contain the title “"+w.Title+"”." - return m - } - own := map[string]bool{} - for _, t := range append(append([]string{}, title...), artist...) { - own[t] = true - } - if r.Discography != "" && !own[r.Discography] { - m.Veto, m.Reason = VetoDiscography, "A discography or collection ("+r.Discography+"), and one album is wanted." - return m - } - for _, c := range collections { - if !own[c] && has(r.Tokens, c) { - m.Veto, m.Reason = VetoDiscography, "A collection of albums (“"+c+"”), and one album is wanted." - return m - } - } - for _, v := range versions { - if v.typ != "" && hasType(w.SecondaryTypes, v.typ) { - continue - } - for _, word := range v.words { - if !own[word] && has(r.Tokens, word) { - m.Veto, m.Reason = VetoVersion, "This is "+v.what+" (“"+word+"”)." - return m - } - } - } - if w.PrimaryType == "Album" || w.PrimaryType == "EP" { - if !own["single"] && has(r.Tokens, "single") { - m.Veto, m.Reason = VetoType, "A single, and the "+strings.ToLower(w.PrimaryType)+" is wanted." - return m - } - } - if w.PrimaryType == "Album" && !own["ep"] && has(r.Tokens, "ep") { - m.Veto, m.Reason = VetoType, "An EP, and the album is wanted." - return m - } - // A self-titled album: every release of the artist names it once. Two - // mentions, or its year, say it is this one. - if len(title) > 0 && equal(title, artist) && count(r.Tokens, title) < 2 && !m.YearAgrees { - m.Doubt = "The album shares the artist’s name, and nothing else in the name says it is this album." - } - return m -} - -func abs(n int) int { - if n < 0 { - return -n - } - return n -} - -func withoutThe(t []string) []string { - if len(t) > 1 && t[0] == "the" { - return t[1:] - } - return t -} - -// bare is a title without a trailing bracketed part ("Album (Remastered)"), -// for names that leave it off. -func bare(title string) []string { - if i := strings.IndexAny(title, "(["); i > 0 { - return Tokens(title[:i]) - } - return nil -} - -// contains says whether want's words appear in name in order and together, -// or, for two or more words, run together ("ACDC" for AC/DC). A single word -// is never looked for run together: "muse" is inside "museum". -func contains(name, want []string) bool { - if len(want) == 0 { - return false - } - if count(name, want) > 0 { - return true - } - joined := strings.Join(want, "") - if len(want) < 2 || len(joined) < 4 { - return false - } - return strings.Contains(strings.Join(name, ""), joined) -} - -// count is how often want's words appear in name in order and together. -func count(name, want []string) int { - n := 0 - for i := 0; i+len(want) <= len(name); i++ { - if equal(name[i:i+len(want)], want) { - n++ - } - } - return n -} - -func equal(a, b []string) bool { - if len(a) != len(b) { - return false - } - for i := range a { - if a[i] != b[i] { - return false - } - } - return true -} - -func has(tokens []string, word string) bool { - for _, t := range tokens { - if t == word { - return true - } - } - return false -} - -func hasType(types []string, t string) bool { - for _, x := range types { - if strings.EqualFold(x, t) { - return true - } - } - return false -} diff --git a/internal/release/parse.go b/internal/release/parse.go deleted file mode 100644 index 9acce161..00000000 --- a/internal/release/parse.go +++ /dev/null @@ -1,289 +0,0 @@ -// Package release reads music release names and judges them against a -// wanted album: what a name says it is (format, bitrate, bit -// depth, source, year, edition), whether it is the album wanted, and which -// quality tier it sits in. Nothing here touches the network or the database, -// so every rule is testable on a string. -package release - -import ( - "regexp" - "strconv" - "strings" -) - -// Formats a name can state. -const ( - FormatFLAC = "FLAC" - FormatALAC = "ALAC" - FormatAPE = "APE" - FormatWV = "WavPack" - FormatWAV = "WAV" - FormatAIFF = "AIFF" - FormatDSD = "DSD" - FormatMP3 = "MP3" - FormatAAC = "AAC" - FormatVorbis = "Vorbis" - FormatOpus = "Opus" -) - -// Sources a name can state. -const ( - SourceWEB = "WEB" - SourceCD = "CD" - SourceVinyl = "Vinyl" - SourceSACD = "SACD" - SourceCassette = "Cassette" -) - -// Release is what a release name says about itself. Zero values mean the -// name does not say. -type Release struct { - Name string - Year int - Format string - // Bitrate is a constant bitrate in kbps; VBR is a LAME preset (V0, V1, V2). - Bitrate int - VBR string - // BitDepth (16, 24) and SampleRate (kHz) for lossless. - BitDepth int - SampleRate float64 - Source string - // Video is the marker that says this is a video ("x264", "1080p"), or "". - Video string - // Discography: a year range or a word naming many albums. - Discography string - // Tokens are the name's words, normalised for matching. - Tokens []string -} - -var ( - formatRe = regexp.MustCompile(`\b(flac|alac|ape|wavpack|wv|wav|aiff|dsd|dsf|dff|mp3|aac|m4a|ogg|vorbis|opus)\b`) - // "24-96", "24/192", "16-44 1" (44.1 after dots become spaces). - depthRateRe = regexp.MustCompile(`\b(16|24|32) ?[-/ ] ?(44 1|44|48|88 2|88|96|176 4|176|192|352 8|384)\b`) - depthRe = regexp.MustCompile(`\b(16|24|32) ?-?bits?\b`) - rateRe = regexp.MustCompile(`\b(44 1|48|88 2|96|176 4|192) ?khz\b`) - hiResRe = regexp.MustCompile(`\bhi ?-?res\b`) - codecRateRe = regexp.MustCompile(`\b(?:mp3|aac|m4a|cbr|ogg|opus)[ \-@]*(320|256|224|192|160|128|112|96|64)\b`) - kbpsRe = regexp.MustCompile(`\b(320|256|224|192|160|128|112|96|64) ?(?:kbps|kbit|kb/s|k)\b`) - vbrRe = regexp.MustCompile(`\bv([0-2])\b`) - webRe = regexp.MustCompile(`\bweb(?: ?-?dl)?\b`) - cdRe = regexp.MustCompile(`\bcd ?(?:rip)?\b`) - vinylRe = regexp.MustCompile(`\b(?:vinyl|vinylrip|lp ?rip|needle ?drop)\b`) - sacdRe = regexp.MustCompile(`\bsacd\b`) - cassetteRe = regexp.MustCompile(`\b(?:cassette|tape ?rip)\b`) - yearRe = regexp.MustCompile(`\b(19[3-9]\d|20[0-4]\d)\b`) - bracketYear = regexp.MustCompile(`[(\[{] ?(19[3-9]\d|20[0-4]\d) ?[)\]}]`) - yearRangeRe = regexp.MustCompile(`\b(19[3-9]\d|20[0-4]\d) ?(?:-|–|to) ?(19[3-9]\d|20[0-4]\d)\b`) - videoRe = regexp.MustCompile(`\b(x264|x265|h ?264|h ?265|hevc|xvid|divx|1080p|1080i|720p|2160p|bluray|blu ?ray|bdrip|brrip|bdremux|dvdrip|dvd9|dvd5|webrip|hdtv|mkv|avi|m2ts)\b`) - discogRe = regexp.MustCompile(`\b(discography|discografia|discographie|diskografie|discografie)\b`) -) - -// Parse reads what a release name says. -func Parse(name string) Release { - r := Release{Name: name} - s := spaced(name) - - r.Format = bestFormat(formatRe.FindAllStringSubmatch(s, -1)) - - if m := depthRateRe.FindStringSubmatch(s); m != nil { - r.BitDepth, _ = strconv.Atoi(m[1]) - r.SampleRate = rate(m[2]) - } - if r.BitDepth == 0 { - if m := depthRe.FindStringSubmatch(s); m != nil { - r.BitDepth, _ = strconv.Atoi(m[1]) - } - } - if r.SampleRate == 0 { - if m := rateRe.FindStringSubmatch(s); m != nil { - r.SampleRate = rate(m[1]) - } - } - if r.BitDepth == 0 && hiResRe.MatchString(s) { - r.BitDepth = 24 - } - - if m := codecRateRe.FindStringSubmatch(s); m != nil { - r.Bitrate, _ = strconv.Atoi(m[1]) - } else if m := kbpsRe.FindStringSubmatch(s); m != nil { - r.Bitrate, _ = strconv.Atoi(m[1]) - } - // A bare "V0" is a LAME preset only beside MP3 or VBR; elsewhere it can - // be part of a title. - if m := vbrRe.FindStringSubmatch(s); m != nil && (r.Format == FormatMP3 || strings.Contains(s, "vbr")) { - r.VBR = "V" + m[1] - } - if r.Format == "" && (r.Bitrate > 0 || r.VBR != "") { - r.Format = FormatMP3 - } - - switch { - case sacdRe.MatchString(s): - r.Source = SourceSACD - case vinylRe.MatchString(s): - r.Source = SourceVinyl - case webRe.MatchString(s): - r.Source = SourceWEB - case cassetteRe.MatchString(s): - r.Source = SourceCassette - case cdRe.MatchString(s): - r.Source = SourceCD - } - - if m := videoRe.FindStringSubmatch(s); m != nil { - r.Video = m[1] - } - if m := discogRe.FindStringSubmatch(s); m != nil { - r.Discography = m[1] - } else if m := yearRangeRe.FindStringSubmatch(s); m != nil && m[2] > m[1] { - r.Discography = m[1] + "–" + m[2] - } - r.Year = year(s) - r.Tokens = Tokens(name) - return r -} - -// spaced lowercases a name and turns its separators into spaces, keeping -// the slash in "24/96" and "kb/s". -func spaced(name string) string { - var b strings.Builder - for _, c := range strings.ToLower(name) { - switch c { - case '_', '.', ',': - b.WriteByte(' ') - default: - b.WriteRune(c) - } - } - return b.String() -} - -// rate reads a sample rate in kHz; "44", "88" and "176" are the short -// spellings of 44.1, 88.2 and 176.4. -func rate(s string) float64 { - switch s { - case "44": - return 44.1 - case "88": - return 88.2 - case "176": - return 176.4 - } - f, _ := strconv.ParseFloat(strings.ReplaceAll(s, " ", "."), 64) - return f -} - -// formatRank orders formats a name might state together ("FLAC + MP3"): -// the best is the one it is described by. -var formatRank = map[string]int{ - FormatDSD: 11, FormatFLAC: 10, FormatALAC: 9, FormatWV: 8, FormatAPE: 7, FormatWAV: 6, FormatAIFF: 5, - FormatOpus: 4, FormatAAC: 3, FormatVorbis: 2, FormatMP3: 1, -} - -func bestFormat(found [][]string) string { - best := "" - for _, m := range found { - f := formatOf(m[1]) - if formatRank[f] > formatRank[best] { - best = f - } - } - return best -} - -// formatOf names the format a word or file extension stands for. -func formatOf(word string) string { - switch word { - case "flac": - return FormatFLAC - case "alac": - return FormatALAC - case "ape": - return FormatAPE - case "wavpack", "wv": - return FormatWV - case "wav": - return FormatWAV - case "aiff", "aif": - return FormatAIFF - case "dsd", "dsf", "dff": - return FormatDSD - case "mp3": - return FormatMP3 - case "aac", "m4a": - return FormatAAC - case "ogg", "vorbis": - return FormatVorbis - case "opus": - return FormatOpus - } - return "" -} - -// year is the release year a name states: one in brackets when there is -// one (so the title "1989" is not read as its year), else the last. -func year(s string) int { - if m := bracketYear.FindStringSubmatch(s); m != nil { - y, _ := strconv.Atoi(m[1]) - return y - } - all := yearRe.FindAllString(s, -1) - if len(all) == 0 { - return 0 - } - y, _ := strconv.Atoi(all[len(all)-1]) - return y -} - -// fold maps the accented letters music names carry to plain ones. -var fold = map[rune]string{ - 'à': "a", 'á': "a", 'â': "a", 'ã': "a", 'ä': "a", 'å': "a", 'ā': "a", 'ą': "a", - 'æ': "ae", 'ç': "c", 'ć': "c", 'č': "c", - 'è': "e", 'é': "e", 'ê': "e", 'ë': "e", 'ē': "e", 'ė': "e", 'ę': "e", 'ě': "e", - 'ì': "i", 'í': "i", 'î': "i", 'ï': "i", 'ī': "i", 'ı': "i", - 'ñ': "n", 'ń': "n", 'ň': "n", - 'ò': "o", 'ó': "o", 'ô': "o", 'õ': "o", 'ö': "o", 'ø': "o", 'ō': "o", 'ő': "o", - 'œ': "oe", 'ù': "u", 'ú': "u", 'û': "u", 'ü': "u", 'ū': "u", 'ů': "u", 'ű': "u", - 'ý': "y", 'ÿ': "y", 'ß': "ss", 'š': "s", 'ś': "s", 'ş': "s", - 'ž': "z", 'ź': "z", 'ż': "z", 'ł': "l", 'đ': "d", 'ð': "d", 'þ': "th", 'ř': "r", -} - -// abbreviations are spelled out on both sides before comparing. -var abbreviations = map[string]string{"vol": "volume", "pt": "part", "n": "and"} - -// Tokens are a name's words for matching: lowercase, accents folded, "&" -// and "+" read as "and", apostrophes dropped (so "Don't" is "dont"), every -// other mark a word break. -func Tokens(s string) []string { - var b strings.Builder - for _, c := range strings.ToLower(s) { - switch { - case c >= 'a' && c <= 'z', c >= '0' && c <= '9': - b.WriteRune(c) - case c == '\'' || c == '’' || c == '`' || c == '´': - case c == '&' || c == '+': - b.WriteString(" and ") - default: - if f, ok := fold[c]; ok { - b.WriteString(f) - } else if c > 127 && isLetter(c) { - b.WriteRune(c) - } else { - b.WriteByte(' ') - } - } - } - words := strings.Fields(b.String()) - for i, w := range words { - if full, ok := abbreviations[w]; ok { - words[i] = full - } - } - return words -} - -// isLetter keeps letters of scripts the fold table does not cover (Cyrillic, -// CJK) as letters rather than breaks. -func isLetter(c rune) bool { - return (c >= 0x0400 && c <= 0x04FF) || (c >= 0x0370 && c <= 0x03FF) || c >= 0x3040 -} diff --git a/internal/release/quality.go b/internal/release/quality.go deleted file mode 100644 index 456a3950..00000000 --- a/internal/release/quality.go +++ /dev/null @@ -1,271 +0,0 @@ -package release - -import ( - "fmt" - "strconv" - "strings" -) - -// Tier is a rung of the quality ladder. -type Tier string - -// The ladder, best first. -const ( - TierLosslessHiRes Tier = "lossless_hires" - TierLossless Tier = "lossless" - TierLossyHigh Tier = "lossy_high" - TierLossyStandard Tier = "lossy_standard" - TierLossyLow Tier = "lossy_low" - TierLossyPoor Tier = "lossy_poor" - TierLossyTrash Tier = "lossy_trash" - TierUnknown Tier = "unknown" -) - -// losslessCategory is Newznab's Audio/Lossless. -const losslessCategory = 3040 - -// TierInfo is a rung as the settings page shows it. -type TierInfo struct { - Tier Tier `json:"tier"` - Label string `json:"label"` - What string `json:"what"` -} - -// Tiers is the ladder, best first. -var Tiers = []TierInfo{ - {TierLosslessHiRes, "Lossless, hi-res", "24-bit or above 48 kHz: FLAC, ALAC, DSD"}, - {TierLossless, "Lossless", "FLAC, ALAC, APE, WavPack, WAV at CD quality"}, - {TierLossyHigh, "High quality lossy", "MP3 320 or V0, AAC 256 and up"}, - {TierLossyStandard, "Standard quality lossy", "MP3 224–256 or V1/V2, AAC 192, Vorbis, Opus, or a lossy format with no bitrate stated"}, - {TierLossyLow, "Low quality lossy", "160–192 kbps"}, - {TierLossyPoor, "Poor quality lossy", "128 kbps"}, - {TierLossyTrash, "Trash quality lossy", "Under 128 kbps"}, - {TierUnknown, "Unknown", "Nothing says what format it is"}, -} - -// DefaultTiers is what a new install allows: every format from trash lossy -// to lossless. Unknown is left out; a -// release that names no format has its .torrent read to find out. -var DefaultTiers = []Tier{ - TierLosslessHiRes, TierLossless, TierLossyHigh, TierLossyStandard, - TierLossyLow, TierLossyPoor, TierLossyTrash, -} - -// KnownTier says whether t is a rung of the ladder. -func KnownTier(t string) bool { - for _, info := range Tiers { - if string(info.Tier) == t { - return true - } - } - return false -} - -// Rank orders tiers: higher is better, Unknown lowest. -func Rank(t Tier) int { - for i, info := range Tiers { - if info.Tier == t { - return len(Tiers) - i - } - } - return 0 -} - -// Label is a tier's name for a person. -func Label(t Tier) string { - for _, info := range Tiers { - if info.Tier == t { - return info.Label - } - } - return string(t) -} - -// TierOf places a release on the ladder. category is the indexer's; 3040 -// (Lossless) stands in for a name that states no format. -func TierOf(r Release, category int) Tier { - switch r.Format { - case FormatDSD: - return TierLosslessHiRes - case FormatFLAC, FormatALAC, FormatAPE, FormatWV, FormatWAV, FormatAIFF: - if r.BitDepth >= 24 || r.SampleRate > 48 { - return TierLosslessHiRes - } - return TierLossless - case FormatMP3: - switch r.VBR { - case "V0": - return TierLossyHigh - case "V1", "V2": - return TierLossyStandard - } - return byBitrate(r.Bitrate, 320) - case FormatAAC, FormatVorbis, FormatOpus: - return byBitrate(r.Bitrate, 256) - } - if category == losslessCategory { - return TierLossless - } - return TierUnknown -} - -// byBitrate places a lossy bitrate; high is the bitrate from which the codec -// counts as high quality. An unstated bitrate is standard. -func byBitrate(kbps, high int) Tier { - switch { - case kbps == 0: - return TierLossyStandard - case kbps >= high: - return TierLossyHigh - case kbps >= 224 || (high == 256 && kbps >= 192): - return TierLossyStandard - case kbps >= 160: - return TierLossyLow - case kbps >= 128: - return TierLossyPoor - } - return TierLossyTrash -} - -// Describe is a release's format in a few words: "FLAC 24-bit/96 kHz", -// "MP3 V0", "AAC 256". Empty when the name states none. -func Describe(r Release) string { - if r.Format == "" { - return "" - } - parts := []string{r.Format} - switch { - case r.BitDepth > 0 && r.SampleRate > 0: - parts = append(parts, fmt.Sprintf("%d-bit/%s kHz", r.BitDepth, khz(r.SampleRate))) - case r.BitDepth > 0: - parts = append(parts, fmt.Sprintf("%d-bit", r.BitDepth)) - case r.VBR != "": - parts = append(parts, r.VBR) - case r.Bitrate > 0: - parts = append(parts, strconv.Itoa(r.Bitrate)) - } - return strings.Join(parts, " ") -} - -func khz(f float64) string { - return strings.TrimSuffix(strconv.FormatFloat(f, 'f', 1, 64), ".0") -} - -// Profile is which tiers an install takes. -type Profile struct { - Allowed map[Tier]bool -} - -// NewProfile builds a profile from the stored tier list. -func NewProfile(tiers []string) Profile { - p := Profile{Allowed: map[Tier]bool{}} - for _, t := range tiers { - p.Allowed[Tier(t)] = true - } - return p -} - -// Allows says whether the profile takes a tier. -func (p Profile) Allows(t Tier) bool { return p.Allowed[t] } - -// Held is what the library's copy of an album is: its tier, estimated from -// what the scanner records, and the same in words. -type Held struct { - Tier Tier - Words string -} - -// HeldFormat is an album's present tracks of one format, summed. -type HeldFormat struct { - Format string - Bytes, Ms int64 -} - -// HeldCopy judges an album's copy from its tracks grouped by format: the -// lowest tier among them: an album is as good as its worst file. -// Summing a format's tracks first keeps a near-silent VBR interlude from -// dragging the album down. ok is false when no format can be placed. -func HeldCopy(formats []HeldFormat) (Held, bool) { - var out Held - found := false - for _, f := range formats { - kbps := FileKbps(f.Bytes, f.Ms) - t, ok := TierOfFile(f.Format, kbps) - if !ok { - continue - } - if !found || Rank(t) < Rank(out.Tier) { - out = Held{Tier: t, Words: HeldWords(f.Format, t, kbps)} - found = true - } - } - return out, found -} - -// FileKbps is the average bitrate of files totalling bytes over ms of audio: -// the scanner records no bitrate (tracks.bitrate is never filled), so size -// per second is the measure there is. Cover art and tags count too, which -// only ever reads high. -func FileKbps(bytes, ms int64) int { - if bytes <= 0 || ms <= 0 { - return 0 - } - return int(bytes * 8 / ms) -} - -// mp3High is the average bitrate from which an MP3 counts as high quality -// when only its size is known. A V0 encode averages 220 to 260 kbps, so the -// line sits below it: an estimate errs toward the copy held, and an album is -// never fetched again on a guess. -const mp3High = 220 - -// TierOfFile places files of one format on the ladder from their average -// bitrate (FileKbps). format is the scanner's extension, without the dot. -// ok is false for a format the ladder cannot place. -func TierOfFile(format string, kbps int) (Tier, bool) { - switch strings.ToLower(strings.TrimPrefix(format, ".")) { - case "flac", "wav", "aiff", "aif", "ape", "wv", "alac": - // 16-bit/44.1 kHz stereo is 1,411 kbps before compression; past - // 1,600 on average is more bits or a higher rate than CD. - if kbps > 1600 { - return TierLosslessHiRes, true - } - return TierLossless, true - case "dsf", "dff": - return TierLosslessHiRes, true - case "m4a", "mp4": - // ALAC or AAC: the container says nothing, the rate does. No AAC - // encode runs near 500 kbps. - if kbps > 500 { - return TierLossless, true - } - return byBitrate(kbps, 256), kbps > 0 - case "mp3": - if kbps >= mp3High { - return TierLossyHigh, true - } - return byBitrate(kbps, 320), kbps > 0 - case "aac", "ogg", "oga", "opus": - return byBitrate(kbps, 256), kbps > 0 - } - return TierUnknown, false -} - -// HeldWords says what a held copy is: "FLAC", "MP3, about 192 kbps". -func HeldWords(format string, tier Tier, kbps int) string { - f := strings.ToUpper(strings.TrimPrefix(format, ".")) - switch tier { - case TierLosslessHiRes: - return f + ", hi-res" - case TierLossless: - return f - } - if kbps <= 0 { - return f - } - return fmt.Sprintf("%s, about %d kbps", f, roundKbps(kbps)) -} - -// roundKbps rounds an average bitrate to the nearest 8, so a 191 reads as -// the 192 it was encoded at. -func roundKbps(k int) int { return (k + 4) / 8 * 8 } diff --git a/internal/release/release_test.go b/internal/release/release_test.go deleted file mode 100644 index 46ab4456..00000000 --- a/internal/release/release_test.go +++ /dev/null @@ -1,311 +0,0 @@ -package release - -import ( - "crypto/sha1" - "encoding/hex" - "fmt" - "strings" - "testing" -) - -func TestParse_ReadsWhatTheNameSays(t *testing.T) { - for _, c := range []struct { - name string - format, vbr, src string - bitrate, depth, yr int - rate float64 - tier Tier - }{ - // A bracketed edition with its own year: not the original album. - {"Daft Punk Random Access Memories (Drumless Edition 2023) [Flac 24-88]", FormatFLAC, "", "", 0, 24, 2023, 88.2, TierLosslessHiRes}, - {"Gorillaz - Humanz - (Japanese Edition) - [CD-FLAC] 2017", FormatFLAC, "", SourceCD, 0, 0, 2017, 0, TierLossless}, - {"Radiohead - OK Computer (1997) [MP3 320]", FormatMP3, "", "", 320, 0, 1997, 0, TierLossyHigh}, - {"Artist - Album 2019 MP3 V0", FormatMP3, "V0", "", 0, 0, 2019, 0, TierLossyHigh}, - {"Artist - Album (2020) [WEB] [AAC 256]", FormatAAC, "", SourceWEB, 256, 0, 2020, 0, TierLossyHigh}, - {"Artist_-_Album-(2004)-192kbps", FormatMP3, "", "", 192, 0, 2004, 0, TierLossyLow}, - {"Artist - Album [24bit-96kHz] FLAC Vinyl", FormatFLAC, "", SourceVinyl, 0, 24, 0, 96, TierLosslessHiRes}, - {"Taylor Swift - 1989 (2014) [FLAC]", FormatFLAC, "", "", 0, 0, 2014, 0, TierLossless}, - {"Artist - Album", "", "", "", 0, 0, 0, 0, TierUnknown}, - } { - r := Parse(c.name) - got := fmt.Sprint(r.Format, r.VBR, r.Source, r.Bitrate, r.BitDepth, r.Year, r.SampleRate, TierOf(r, 0)) - want := fmt.Sprint(c.format, c.vbr, c.src, c.bitrate, c.depth, c.yr, c.rate, c.tier) - if got != want { - t.Errorf("Parse(%q)\n got %s\nwant %s", c.name, got, want) - } - } -} - -func TestParse_VideoAndDiscographies(t *testing.T) { - if v := Parse("Artist.Album.2018.1080p.BluRay.x264").Video; v != "1080p" { - t.Errorf("video = %q", v) - } - if d := Parse("Artist Discography 1994-2010 FLAC").Discography; d != "discography" { - t.Errorf("discography = %q", d) - } - if d := Parse("Artist - Studio Albums 1994 - 2010 [MP3]").Discography; d != "1994–2010" { - t.Errorf("year range = %q", d) - } - if d := Parse("Artist - Album (2010) [FLAC]").Discography; d != "" { - t.Errorf("one year read as a range: %q", d) - } -} - -func judge(w Want, name string) Match { return Judge(w, Parse(name)) } - -func TestJudge_TheAlbumAndWhatIsNot(t *testing.T) { - ram := Want{Artist: "Daft Punk", Title: "Random Access Memories", Year: 2013, PrimaryType: "Album"} - for name, veto := range map[string]string{ - "Daft Punk - Random Access Memories (2013) [FLAC]": "", - "Daft_Punk-Random_Access_Memories-WEB-2013": "", - "Daft Punk Random Access Memories (Drumless Edition 2023) [Flac 24-88]": VetoVersion, - "Daft Punk - Random Access Memories (Instrumentals) [MP3]": VetoVersion, - "Daft Punk - Random Access Memories Remixes": VetoVersion, - "Daft Punk - Random Access Memories (Live) FLAC": VetoVersion, - "Daft Punk - Random Access Memories (BBC Sessions) FLAC": VetoVersion, - "Daft Punk - Random Access Memories Unplugged [MP3]": VetoVersion, - "Daft Punk - Discovery (2001) [FLAC]": VetoTitle, - "Random Access Memories (2013) [FLAC]": VetoArtist, - "Daft Punk - Discography 1997-2013 FLAC": VetoTitle, - "Daft Punk - Random Access Memories + Discovery + Homework Collection": VetoDiscography, - "Daft Punk - Random Access Memories (2013) 1080p BluRay": VetoVideo, - "Daft Punk - Random Access Memories (Single) 2013": VetoType, - "Daft Punk - Random Access Memories (Deluxe 10th Anniversary Edition) 2023": "", - } { - if got := judge(ram, name).Veto; got != veto { - t.Errorf("%q: veto %q, want %q", name, got, veto) - } - } - if !judge(ram, "Daft Punk - Random Access Memories (2013) [FLAC]").YearAgrees { - t.Error("the year did not agree") - } -} - -func TestJudge_WordsTheWantedTitleHoldsDoNotVeto(t *testing.T) { - hole := Want{Artist: "Hole", Title: "Live Through This", PrimaryType: "Album"} - if m := judge(hole, "Hole - Live Through This (1994) [FLAC]"); m.Veto != "" { - t.Errorf("Live Through This: %s", m.Reason) - } - alive := Want{Artist: "Daft Punk", Title: "Alive 2007", PrimaryType: "Album", SecondaryTypes: []string{"Live"}} - if m := judge(alive, "Daft Punk - Alive 2007 (Live) [FLAC]"); m.Veto != "" { - t.Errorf("a wanted live album: %s", m.Reason) - } - alive.SecondaryTypes = nil - if m := judge(alive, "Daft Punk - Alive 2007 (Live) [FLAC]"); m.Veto != VetoVersion { - t.Errorf("live without the Live type: veto %q", m.Veto) - } -} - -func TestJudge_SpellingsOfNames(t *testing.T) { - for _, c := range []struct { - w Want - name string - }{ - {Want{Artist: "AC/DC", Title: "Back in Black"}, "ACDC - Back In Black (1980) FLAC"}, - {Want{Artist: "AC/DC", Title: "Back in Black"}, "AC-DC - Back in Black [MP3 320]"}, - {Want{Artist: "Sigur Rós", Title: "Ágætis byrjun"}, "Sigur Ros - Agaetis Byrjun (1999) [FLAC]"}, - {Want{Artist: "Simon & Garfunkel", Title: "Bookends"}, "Simon and Garfunkel - Bookends"}, - {Want{Artist: "The Beatles", Title: "Abbey Road"}, "Beatles - Abbey Road (Remastered 2009)"}, - {Want{Artist: "Florence + the Machine", Title: "Lungs"}, "Florence and The Machine - Lungs (2009)"}, - {Want{Artist: "Guns N' Roses", Title: "Appetite for Destruction"}, "Guns N Roses - Appetite For Destruction"}, - } { - if m := judge(c.w, c.name); m.Veto != "" { - t.Errorf("%q for %s / %s: %s", c.name, c.w.Artist, c.w.Title, m.Reason) - } - } - // One word is never matched run together: Muse is not inside Museum. - if m := judge(Want{Artist: "Muse", Title: "Absolution"}, "Museum - Absolution"); m.Veto != VetoArtist { - t.Errorf("Museum read as Muse: %q", m.Veto) - } -} - -func TestJudge_ASelfTitledAlbumNeedsMoreThanTheName(t *testing.T) { - weezer := Want{Artist: "Weezer", Title: "Weezer", Year: 1994, PrimaryType: "Album"} - if m := judge(weezer, "Weezer - Weezer (Blue Album) [FLAC]"); m.Doubt != "" || m.Veto != "" { - t.Errorf("named twice: %+v", m) - } - if m := judge(weezer, "Weezer (1994) [FLAC]"); m.Doubt != "" { - t.Errorf("the year agrees: %+v", m) - } - if m := judge(weezer, "Weezer (Green Album) FLAC"); m.Doubt == "" { - t.Error("a self-titled name with nothing else was taken as sure") - } -} - -func TestTierOf_TheLadder(t *testing.T) { - for _, c := range []struct { - r Release - cat int - want Tier - }{ - {Release{Format: FormatMP3, Bitrate: 256}, 0, TierLossyStandard}, - {Release{Format: FormatMP3, Bitrate: 128}, 0, TierLossyPoor}, - {Release{Format: FormatMP3, Bitrate: 96}, 0, TierLossyTrash}, - {Release{Format: FormatMP3, VBR: "V2"}, 0, TierLossyStandard}, - {Release{Format: FormatMP3}, 0, TierLossyStandard}, - {Release{Format: FormatAAC, Bitrate: 192}, 0, TierLossyStandard}, - {Release{Format: FormatALAC, SampleRate: 96}, 0, TierLosslessHiRes}, - {Release{Format: FormatDSD}, 0, TierLosslessHiRes}, - {Release{}, 3040, TierLossless}, - {Release{}, 3010, TierUnknown}, - } { - if got := TierOf(c.r, c.cat); got != c.want { - t.Errorf("TierOf(%+v, %d) = %s, want %s", c.r, c.cat, got, c.want) - } - } - if Rank(TierLossless) <= Rank(TierLossyHigh) || Rank(TierUnknown) >= Rank(TierLossyTrash) { - t.Error("the ladder is out of order") - } - if !NewProfile([]string{"lossless"}).Allows(TierLossless) || NewProfile([]string{"lossless"}).Allows(TierUnknown) { - t.Error("profile") - } -} - -func files(paths ...string) []File { - out := make([]File, len(paths)) - for i, p := range paths { - out[i] = File{Index: i, Path: p, Size: 1 << 20} - } - return out -} - -func numbered(n int, ext string) []string { - var out []string - for i := 1; i <= n; i++ { - out = append(out, fmt.Sprintf("Album/%02d - Track.%s", i, ext)) - } - return out -} - -func TestJudgeFiles_WhatTheTorrentHolds(t *testing.T) { - w := Want{MinTracks: 10, MaxTracks: 13} - if veto, _, _ := JudgeFiles(w, files(numbered(13, "flac")...)); veto != "" { - t.Errorf("a whole album: %s", veto) - } - if veto, reason, _ := JudgeFiles(w, files(numbered(3, "flac")...)); veto != VetoIncomplete || !strings.Contains(reason, "3 tracks") { - t.Errorf("three tracks: %s %s", veto, reason) - } - if veto, _, _ := JudgeFiles(w, files("Concert/Concert.mkv", "Concert/cover.jpg")); veto != VetoVideo { - t.Errorf("a video: %s", veto) - } - if veto, _, _ := JudgeFiles(w, files("Album.rar", "Album.r00")); veto != VetoNoAudio { - t.Errorf("an archive: %s", veto) - } - if _, _, doubt := JudgeFiles(w, files(numbered(60, "mp3")...)); doubt == "" { - t.Error("sixty tracks for a thirteen-track album raised no doubt") - } - if c := Inspect(files(append(numbered(12, "flac"), "Album/cover.jpg", "Album/album.cue")...)); c.Format != FormatFLAC || c.Audio != 12 { - t.Errorf("inspect = %+v", c) - } -} - -// bencode writes the few shapes a test torrent needs. -func bencode(v any) string { - switch x := v.(type) { - case string: - return fmt.Sprintf("%d:%s", len(x), x) - case int: - return fmt.Sprintf("i%de", x) - case []any: - var b strings.Builder - b.WriteString("l") - for _, e := range x { - b.WriteString(bencode(e)) - } - return b.String() + "e" - case [][2]any: // an ordered dictionary - var b strings.Builder - b.WriteString("d") - for _, kv := range x { - b.WriteString(bencode(kv[0]) + bencode(kv[1])) - } - return b.String() + "e" - } - panic(fmt.Sprintf("bencode %T", v)) -} - -func TestParseTorrent_FilesAndInfohash(t *testing.T) { - info := bencode([][2]any{ - {"files", []any{ - [][2]any{{"length", 300}, {"path", []any{"01 - One.flac"}}}, - [][2]any{{"attr", "p"}, {"length", 12}, {"path", []any{".pad", "12"}}}, - [][2]any{{"length", 200}, {"path", []any{"Scans", "front.jpg"}}}, - }}, - {"name", "Artist - Album"}, - {"piece length", 16384}, - {"pieces", strings.Repeat("x", 20)}, - }) - body := "d" + bencode("announce") + bencode("http://tracker/") + bencode("info") + info + "e" - meta, err := ParseTorrent([]byte(body)) - if err != nil { - t.Fatal(err) - } - sum := sha1.Sum([]byte(info)) - if meta.InfoHash != hex.EncodeToString(sum[:]) { - t.Errorf("infohash = %s", meta.InfoHash) - } - if len(meta.Files) != 2 || meta.Files[0].Path != "Artist - Album/01 - One.flac" || meta.Files[1].Path != "Artist - Album/Scans/front.jpg" { - t.Errorf("files = %+v", meta.Files) - } - if _, err := ParseTorrent([]byte("not a torrent")); err == nil { - t.Error("HTML parsed as a torrent") - } -} - -func TestTierOfFile(t *testing.T) { - cases := []struct { - format string - kbps int - want Tier - ok bool - }{ - {"flac", 900, TierLossless, true}, - {"FLAC", 2800, TierLosslessHiRes, true}, - {"m4a", 750, TierLossless, true}, - {"m4a", 256, TierLossyHigh, true}, - {"mp3", 320, TierLossyHigh, true}, - {"mp3", 245, TierLossyHigh, true}, // a V0, by size - {"mp3", 192, TierLossyLow, true}, - {"mp3", 128, TierLossyPoor, true}, - {"mp3", 96, TierLossyTrash, true}, - {"opus", 160, TierLossyLow, true}, - {"wma", 192, TierUnknown, false}, - {"mp3", 0, TierLossyStandard, false}, - } - for _, c := range cases { - got, ok := TierOfFile(c.format, c.kbps) - if got != c.want || ok != c.ok { - t.Errorf("TierOfFile(%q, %d) = %s, %v; want %s, %v", c.format, c.kbps, got, ok, c.want, c.ok) - } - } -} - -func TestFileKbpsAndWords(t *testing.T) { - // 5 MB over 3:30 of audio is about 190 kbps. - k := FileKbps(5_000_000, 210_000) - if k != 190 { - t.Fatalf("FileKbps = %d, want 190", k) - } - if got := HeldWords("mp3", TierLossyLow, 191); got != "MP3, about 192 kbps" { - t.Errorf("HeldWords = %q", got) - } - if got := HeldWords("flac", TierLossless, 900); got != "FLAC" { - t.Errorf("HeldWords = %q", got) - } - if FileKbps(0, 1000) != 0 || FileKbps(1000, 0) != 0 { - t.Error("FileKbps with nothing to measure is 0") - } -} - -func TestHeldCopy_TakesTheWorstFormat(t *testing.T) { - h, ok := HeldCopy([]HeldFormat{ - {Format: "flac", Bytes: 300_000_000, Ms: 2_400_000}, - {Format: "mp3", Bytes: 4_800_000, Ms: 200_000}, // 192 kbps - }) - if !ok || h.Tier != TierLossyLow || h.Words != "MP3, about 192 kbps" { - t.Fatalf("HeldCopy = %+v, %v", h, ok) - } - if _, ok := HeldCopy([]HeldFormat{{Format: "wma", Bytes: 1, Ms: 1}}); ok { - t.Error("a format the ladder can't place says nothing") - } -} diff --git a/internal/release/torrent.go b/internal/release/torrent.go deleted file mode 100644 index 15c844d4..00000000 --- a/internal/release/torrent.go +++ /dev/null @@ -1,178 +0,0 @@ -package release - -import ( - "bytes" - "crypto/sha1" - "encoding/hex" - "errors" - "fmt" - "strconv" - "strings" -) - -// Reading a .torrent file. A .torrent -// is the metadata: its info dictionary names every file, and its SHA-1 is the -// infohash. So a release offered as a .torrent link can be judged on what it -// holds before any of it is downloaded. Bencode is four shapes; walking them -// here costs less than a dependency. - -// TorrentMeta is what a .torrent says about itself. -type TorrentMeta struct { - // InfoHash is the v1 infohash: the SHA-1 of the info dictionary's own - // bytes, lowercase hex. - InfoHash string - Name string - Files []File -} - -// ErrNotTorrent is bytes that are not a .torrent. -var ErrNotTorrent = errors.New("not a .torrent file") - -// maxTorrentBytes bounds what is read, as Exchange bounds what it accepts. -const maxTorrentBytes = 10 << 20 - -// ParseTorrent reads a .torrent's info dictionary. -func ParseTorrent(b []byte) (TorrentMeta, error) { - if len(b) == 0 || len(b) > maxTorrentBytes || b[0] != 'd' { - return TorrentMeta{}, ErrNotTorrent - } - d := &bdecoder{b: b} - root, err := d.value(0) - if err != nil { - return TorrentMeta{}, fmt.Errorf("%w: %v", ErrNotTorrent, err) - } - top, ok := root.(map[string]any) - if !ok || d.infoEnd == 0 { - return TorrentMeta{}, fmt.Errorf("%w: no info dictionary", ErrNotTorrent) - } - info, ok := top["info"].(map[string]any) - if !ok { - return TorrentMeta{}, fmt.Errorf("%w: no info dictionary", ErrNotTorrent) - } - sum := sha1.Sum(b[d.infoStart:d.infoEnd]) - out := TorrentMeta{InfoHash: hex.EncodeToString(sum[:])} - out.Name, _ = info["name"].(string) - - files, multi := info["files"].([]any) - if !multi { - // A single-file torrent: the name IS the file. - length, _ := info["length"].(int64) - out.Files = []File{{Path: out.Name, Size: length}} - return out, nil - } - for index, f := range files { - entry, ok := f.(map[string]any) - if !ok { - continue - } - parts, _ := entry["path"].([]any) - segs := []string{out.Name} - for _, p := range parts { - if s, ok := p.(string); ok { - segs = append(segs, s) - } - } - length, _ := entry["length"].(int64) - // BEP 47 padding files carry no content. - if attr, _ := entry["attr"].(string); strings.Contains(attr, "p") { - continue - } - out.Files = append(out.Files, File{Index: index, Path: strings.Join(segs, "/"), Size: length}) - } - return out, nil -} - -// bdecoder walks bencode, and remembers where the top-level info dictionary -// sits so its raw bytes can be hashed exactly as published. -type bdecoder struct { - b []byte - i int - infoStart, infoEnd int -} - -// maxDepth: a real torrent nests four deep. Anything past this is hostile. -const maxDepth = 32 - -func (d *bdecoder) value(depth int) (any, error) { - if depth > maxDepth { - return nil, errors.New("nested too deep") - } - if d.i >= len(d.b) { - return nil, errors.New("truncated") - } - switch c := d.b[d.i]; { - case c == 'i': - end := bytes.IndexByte(d.b[d.i:], 'e') - if end < 0 { - return nil, errors.New("unterminated integer") - } - n, err := strconv.ParseInt(string(d.b[d.i+1:d.i+end]), 10, 64) - if err != nil { - return nil, err - } - d.i += end + 1 - return n, nil - case c == 'l': - d.i++ - var out []any - for d.i < len(d.b) && d.b[d.i] != 'e' { - v, err := d.value(depth + 1) - if err != nil { - return nil, err - } - out = append(out, v) - } - if d.i >= len(d.b) { - return nil, errors.New("unterminated list") - } - d.i++ - return out, nil - case c == 'd': - d.i++ - out := map[string]any{} - for d.i < len(d.b) && d.b[d.i] != 'e' { - k, err := d.str() - if err != nil { - return nil, err - } - start := d.i - v, err := d.value(depth + 1) - if err != nil { - return nil, err - } - if depth == 0 && k == "info" { - d.infoStart, d.infoEnd = start, d.i - } - // Pieces are 20 bytes a piece and never read: not kept. - if k != "pieces" { - out[k] = v - } - } - if d.i >= len(d.b) { - return nil, errors.New("unterminated dictionary") - } - d.i++ - return out, nil - case c >= '0' && c <= '9': - return d.str() - default: - return nil, fmt.Errorf("unexpected %q", c) - } -} - -func (d *bdecoder) str() (string, error) { - colon := bytes.IndexByte(d.b[d.i:], ':') - if colon < 0 { - return "", errors.New("unterminated string length") - } - n, err := strconv.Atoi(string(d.b[d.i : d.i+colon])) - if err != nil || n < 0 { - return "", errors.New("bad string length") - } - start := d.i + colon + 1 - if start+n > len(d.b) { - return "", errors.New("string runs past the end") - } - d.i = start + n - return string(d.b[start : start+n]), nil -} diff --git a/internal/release/version.go b/internal/release/version.go deleted file mode 100644 index 6a849fe2..00000000 --- a/internal/release/version.go +++ /dev/null @@ -1,79 +0,0 @@ -package release - -import "strings" - -// TitleVersion is the version of a recording a release group's title names -// in its qualifier: the words in brackets, or after a dash. MusicBrainz -// does not always type such a group: My Chemical Romance's "Common People -// (Recorded For BBC Radio 1)" is a plain Single. Typ is the -// MusicBrainz secondary type the version is, or "" for one no type names -// (an acoustic or instrumental version); Word is the word that said so. -type TitleVersion struct { - Word string - What string - Typ string -} - -// titleVersions are the qualifier words that name a version, in the order -// they are looked for: "rough mix" is a demo before "mix" is a remix. -var titleVersions = []struct { - words []string - what string - typ string -}{ - {[]string{"demo", "demos", "outtake", "outtakes", "rehearsal", "rough mix"}, "demo recordings", "Demo"}, - {[]string{"live", "bootleg", "bbc", "session", "sessions", "unplugged", "recorded for", "in concert"}, "a live recording", "Live"}, - {[]string{"remix", "remixes", "remixed", "rmx", "mix", "mixes"}, "a remix", "Remix"}, - {[]string{"instrumental", "instrumentals", "acoustic", "karaoke", "acapella", "acappella", "a cappella", - "drumless", "nightcore", "slowed", "sped up"}, "another version", ""}, - {[]string{"commentary", "interview", "interviews"}, "commentary or interviews", "Interview"}, -} - -// VersionInTitle reads a release group's title for the version it names, -// if any. Only the qualifier counts, so "Live Through This" and "Live -// Forever" name no version. -func VersionInTitle(title string) (TitleVersion, bool) { - for _, q := range qualifiers(title) { - tokens := Tokens(q) - for _, v := range titleVersions { - for _, w := range v.words { - if contains(tokens, strings.Fields(w)) { - return TitleVersion{Word: w, What: v.what, Typ: v.typ}, true - } - } - } - } - return TitleVersion{}, false -} - -// qualifiers are the parts of a title in brackets, and what follows a -// spaced dash. -func qualifiers(title string) []string { - var out []string - depth, start := 0, 0 - for i, c := range title { - switch c { - case '(', '[', '{': - if depth == 0 { - start = i + 1 - } - depth++ - case ')', ']', '}': - if depth > 0 { - depth-- - if depth == 0 { - out = append(out, title[start:i]) - } - } - } - } - if depth > 0 { - out = append(out, title[start:]) - } - for _, dash := range []string{" - ", " – ", " — "} { - if i := strings.Index(title, dash); i >= 0 { - out = append(out, title[i+len(dash):]) - } - } - return out -} diff --git a/internal/release/version_test.go b/internal/release/version_test.go deleted file mode 100644 index 7f9b4fc3..00000000 --- a/internal/release/version_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package release - -import "testing" - -func TestVersionInTitle(t *testing.T) { - for title, want := range map[string]string{ - // Groups MusicBrainz types as plain Singles, named by their title. - "Common People (Recorded For BBC Radio 1)": "Live", - "Welcome to the Black Parade (Steve Aoki remix)": "Remix", - "The Five of Us Are Dying (Rough mix)": "Demo", - "I'm Not Okay (I Promise) [outtake version 2]": "Demo", - "Na Na Na (live from Sessions@AOL)": "Live", - "Song - Live at Wembley": "Live", - "Song – Club Mix": "Remix", - "Helena (Acoustic)": "", - "Teenagers (a cappella)": "", - "Hybrid Theory (20th Anniversary Edition)": "none", - "Live Through This": "none", - "Live Forever": "none", - "The Black Parade": "none", - "Warped Tour ’05: Bootleg Series (live at Warped Tour": "Live", // unclosed - } { - v, ok := VersionInTitle(title) - got := v.Typ - if !ok { - got = "none" - } - if got != want { - t.Errorf("VersionInTitle(%q) = %q (%q), want %q", title, got, v.Word, want) - } - } -} diff --git a/internal/requests/reconciler.go b/internal/requests/reconciler.go deleted file mode 100644 index 06593805..00000000 --- a/internal/requests/reconciler.go +++ /dev/null @@ -1,198 +0,0 @@ -package requests - -import ( - "context" - "errors" - "log/slog" - "time" - - "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" - "git.fabledsword.com/bvandeusen/minstrel/internal/eventbus" - "git.fabledsword.com/bvandeusen/minstrel/internal/notifications" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" -) - -// Reconciler is a background worker that completes approved requests once -// the library holds what they asked for: an artist request when the artist -// is in the library, an album or track request when the want it recorded is -// held (internal/wanted settles wants). Errors per row are logged at WARN and -// do not abort the tick. -type Reconciler struct { - pool *pgxpool.Pool - logger *slog.Logger - bus *eventbus.Bus - notifier *notifications.Notifier // nil: no inbox notifications - tick time.Duration - batch int32 -} - -// NewReconciler constructs a Reconciler with production defaults: 5-minute -// tick, batch size 50. Pass nil for bus when no SSE publishing is desired. -func NewReconciler(pool *pgxpool.Pool, logger *slog.Logger, bus *eventbus.Bus) *Reconciler { - return &Reconciler{pool: pool, logger: logger, bus: bus, tick: 5 * time.Minute, batch: 50} -} - -// SetNotifier makes completions land in the requester's notifications inbox. -// Without one, completions are only broadcast on the bus. -func (r *Reconciler) SetNotifier(n *notifications.Notifier) { r.notifier = n } - -// publishCompleted tells the requester their request arrived: a -// request.status_changed event for open screens, and a notification that -// outlives the connection. -func (r *Reconciler) publishCompleted(ctx context.Context, row dbq.Request) { - p := notifications.Payload{ - RequestID: formatUUIDForBus(row.ID), - RequestKind: string(row.Kind), - Name: DisplayName(row), - Artist: row.ArtistName, - Title: requestedTitle(row), - } - if row.MatchedAlbumID.Valid { - p.AlbumID = formatUUIDForBus(row.MatchedAlbumID) - } else if row.MatchedArtistID.Valid { - p.ArtistID = formatUUIDForBus(row.MatchedArtistID) - } - r.notifier.NotifyLogged(ctx, notifications.KindRequestCompleted, notifications.ToUser(row.UserID), p.Map()) - - if r.bus == nil { - return - } - r.bus.Publish(eventbus.Event{ - Kind: "request.status_changed", - UserID: formatUUIDForBus(row.UserID), - Data: map[string]any{ - "request_id": formatUUIDForBus(row.ID), - "status": "completed", - "kind": string(row.Kind), - }, - }) -} - -// formatUUIDForBus renders a pgtype.UUID as the canonical 8-4-4-4-12 hex -// string. Mirrors helpers in internal/api/convert.go and other packages; -// inlined here to avoid a back-edge dependency from requests onto -// the api layer. -func formatUUIDForBus(u pgtype.UUID) string { - if !u.Valid { - return "" - } - const hex = "0123456789abcdef" - out := make([]byte, 36) - pos := 0 - for i, b := range u.Bytes { - if i == 4 || i == 6 || i == 8 || i == 10 { - out[pos] = '-' - pos++ - } - out[pos] = hex[b>>4] - out[pos+1] = hex[b&0xf] - pos += 2 - } - return string(out) -} - -// Run blocks until ctx is cancelled, ticking every r.tick. Errors from -// tickOnce are logged at WARN and never propagated. -func (r *Reconciler) Run(ctx context.Context) { - t := time.NewTicker(r.tick) - defer t.Stop() - for { - select { - case <-ctx.Done(): - return - case <-t.C: - if err := r.tickOnce(ctx); err != nil { - r.logger.Warn("requests: reconciler tick failed", "err", err) - } - } - } -} - -// tickOnce loads approved requests (oldest first, up to r.batch) and -// completes those the library now holds. -func (r *Reconciler) tickOnce(ctx context.Context) error { - q := dbq.New(r.pool) - rows, err := q.ListApprovedRequestsForReconcile(ctx, r.batch) - if err != nil { - return err - } - for _, row := range rows { - if err := r.reconcileRow(ctx, q, row); err != nil { - r.logger.Warn("requests: reconciler row failed", - "request_id", row.ID, "kind", row.Kind, "err", err) - } - } - return nil -} - -// reconcileRow completes one request if the library holds it. A request -// not yet held is not an error. -func (r *Reconciler) reconcileRow(ctx context.Context, q *dbq.Queries, row dbq.Request) error { - if row.Kind == dbq.RequestKindArtist { - return r.reconcileArtist(ctx, q, row) - } - return r.reconcileAlbum(ctx, q, row) -} - -func (r *Reconciler) reconcileArtist(ctx context.Context, q *dbq.Queries, row dbq.Request) error { - var artistID pgtype.UUID - err := r.pool.QueryRow(ctx, "SELECT id FROM artists WHERE mbid = $1", row.ArtistMbid).Scan(&artistID) - if errors.Is(err, pgx.ErrNoRows) { - return nil // not in library yet - } - if err != nil { - return err - } - completed, err := q.CompleteRequest(ctx, dbq.CompleteRequestParams{ID: row.ID, MatchedArtistID: artistID}) - if err != nil { - return err - } - r.publishCompleted(ctx, completed) - return nil -} - -// reconcileAlbum completes an album or track request once its want is held, -// matched to the album that answered it. A track request is matched to the -// track it named when that recording arrived, else the newest arrival. -func (r *Reconciler) reconcileAlbum(ctx context.Context, q *dbq.Queries, row dbq.Request) error { - if row.AlbumMbid == nil || *row.AlbumMbid == "" { - return nil - } - w, err := q.GetWantByReleaseGroup(ctx, *row.AlbumMbid) - if errors.Is(err, pgx.ErrNoRows) { - // Removed from the wanted list while the request waited. Wanting it - // again keeps the approval meaningful; the admin who removed it can - // reject a request instead. - _, err = wanted.WantWith(ctx, q, wantFor(row)) - return err - } - if err != nil { - return err - } - if !w.FulfilledAt.Valid || !w.AlbumID.Valid { - return nil - } - params := dbq.CompleteRequestParams{ID: row.ID, MatchedAlbumID: w.AlbumID} - if row.Kind == dbq.RequestKindTrack { - var trackID pgtype.UUID - err := r.pool.QueryRow(ctx, ` - SELECT id FROM tracks - WHERE album_id = $1 AND missing_since IS NULL - ORDER BY (mbid IS NOT DISTINCT FROM $2) DESC, added_at DESC, id - LIMIT 1`, w.AlbumID, row.TrackMbid).Scan(&trackID) - if err != nil && !errors.Is(err, pgx.ErrNoRows) { - return err - } - params.MatchedTrackID = trackID - } - completed, err := q.CompleteRequest(ctx, params) - if err != nil { - return err - } - r.publishCompleted(ctx, completed) - return nil -} diff --git a/internal/requests/requests_test.go b/internal/requests/requests_test.go deleted file mode 100644 index 5b1b9004..00000000 --- a/internal/requests/requests_test.go +++ /dev/null @@ -1,310 +0,0 @@ -package requests - -import ( - "context" - "encoding/json" - "errors" - "io" - "log/slog" - "os" - "testing" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" - "git.fabledsword.com/bvandeusen/minstrel/internal/notifications" -) - -func newPool(t *testing.T) *pgxpool.Pool { - t.Helper() - if testing.Short() { - t.Skip("skipping integration test in -short mode") - } - dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") - if dsn == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { - t.Fatalf("migrate: %v", err) - } - pool, err := pgxpool.New(context.Background(), dsn) - if err != nil { - t.Fatalf("pool: %v", err) - } - t.Cleanup(pool.Close) - dbtest.ResetDB(t, pool) - return pool -} - -func seedUser(t *testing.T, pool *pgxpool.Pool, name string) pgtype.UUID { - t.Helper() - u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{ - Username: dbtest.TestUserPrefix + name, PasswordHash: "x", ApiTokenHash: "x-" + name, - }) - if err != nil { - t.Fatalf("seed user: %v", err) - } - return u.ID -} - -func seedAlbum(t *testing.T, q *dbq.Queries, artistName, artistMBID, title, group string) (dbq.Artist, dbq.Album) { - t.Helper() - ctx := context.Background() - artist, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: artistName, SortName: artistName, Mbid: &artistMBID}) - if err != nil { - t.Fatal(err) - } - release := group + "-release" - album, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: title, SortTitle: title, ArtistID: artist.ID, Mbid: &release}) - if err != nil { - t.Fatal(err) - } - if err := q.SetAlbumReleaseGroupMbidIfNull(ctx, dbq.SetAlbumReleaseGroupMbidIfNullParams{ID: album.ID, ReleaseGroupMbid: &group}); err != nil { - t.Fatal(err) - } - return artist, album -} - -func seedTrack(t *testing.T, q *dbq.Queries, album dbq.Album, artist dbq.Artist, title, path string) dbq.Track { - t.Helper() - tr, err := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{ - Title: title, AlbumID: album.ID, ArtistID: artist.ID, - DurationMs: 180000, FilePath: path, FileSize: 1024, FileFormat: "flac", - }) - if err != nil { - t.Fatal(err) - } - return tr -} - -func quietLogger() *slog.Logger { return slog.New(slog.NewTextHandler(io.Discard, nil)) } - -func TestCreate_ValidatesEachKind(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "creator") - svc := NewService(pool) - ctx := context.Background() - - if r, err := svc.Create(ctx, user, CreateParams{Kind: "artist", ArtistMBID: "a-mbid", ArtistName: "X"}); err != nil || - r.Status != dbq.RequestStatusPending { - t.Fatalf("artist: %+v, %v", r, err) - } - for name, p := range map[string]CreateParams{ - "album without its group": {Kind: "album", ArtistMBID: "a", ArtistName: "X"}, - "track without its album": {Kind: "track", ArtistMBID: "a", ArtistName: "X", TrackMBID: "t", TrackTitle: "Y"}, - "unknown kind": {Kind: "playlist", ArtistMBID: "a", ArtistName: "X"}, - } { - if _, err := svc.Create(ctx, user, p); !errors.Is(err, ErrInvalidKindFields) { - t.Errorf("%s: err = %v, want ErrInvalidKindFields", name, err) - } - } - again, created, err := svc.CreateTracked(ctx, user, CreateParams{Kind: "artist", ArtistMBID: "a-mbid", ArtistName: "X"}) - if err != nil || created || again.Kind != dbq.RequestKindArtist { - t.Errorf("a repeat request dedupes: created=%v, %v", created, err) - } -} - -func TestApprove_AlbumAndTrackRequestsWantTheirReleaseGroup(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - user := seedUser(t, pool, "wanter") - admin := seedUser(t, pool, "approver") - svc := NewService(pool) - ctx := context.Background() - - album, _ := svc.Create(ctx, user, CreateParams{ - Kind: "album", ArtistMBID: "artist-1", ArtistName: "Gorillaz", AlbumMBID: "group-demon-days", AlbumTitle: "Demon Days", - }) - track, _ := svc.Create(ctx, user, CreateParams{ - Kind: "track", ArtistMBID: "artist-1", ArtistName: "Gorillaz", AlbumMBID: "group-humanz", AlbumTitle: "Humanz", - TrackMBID: "rec-1", TrackTitle: "Andromeda", - }) - for _, r := range []dbq.Request{album, track} { - got, err := svc.Approve(ctx, r.ID, admin, pgtype.UUID{}) - if err != nil || got.Status != dbq.RequestStatusApproved || got.DecidedBy != admin { - t.Fatalf("approve %s: %+v, %v", r.Kind, got, err) - } - } - for group, title := range map[string]string{"group-demon-days": "Demon Days", "group-humanz": "Humanz"} { - w, err := q.GetWantByReleaseGroup(ctx, group) - if err != nil { - t.Fatalf("want %s: %v", group, err) - } - if w.Title != title || w.Reason != "request" || w.CreatedBy != user || w.FulfilledAt.Valid { - t.Errorf("want %s = %+v: the requester's open want", group, w) - } - } - if _, err := svc.Approve(ctx, album.ID, admin, pgtype.UUID{}); !errors.Is(err, ErrNotPending) { - t.Errorf("approving twice: err = %v, want ErrNotPending", err) - } - if _, err := svc.Approve(ctx, pgtype.UUID{Bytes: [16]byte{1}, Valid: true}, admin, pgtype.UUID{}); !errors.Is(err, ErrNotFound) { - t.Errorf("no such request: err = %v", err) - } -} - -func TestApprove_ArtistRequestFollowsAndWantsWhatExists(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - user := seedUser(t, pool, "follower") - ctx := context.Background() - svc := NewService(pool) - - albumsOnly, err := q.CreateReleaseTypeProfile(ctx, dbq.CreateReleaseTypeProfileParams{ - Name: "Albums only", PrimaryTypes: []string{"Album"}, SecondaryTypes: []string{}, - }) - if err != nil { - t.Fatal(err) - } - r, _ := svc.Create(ctx, user, CreateParams{Kind: "artist", ArtistMBID: "artist-2", ArtistName: "Moe Shop"}) - if _, err := svc.Approve(ctx, r.ID, user, albumsOnly.ID); err != nil { - t.Fatal(err) - } - f, err := q.GetFollow(ctx, "artist-2") - if err != nil { - t.Fatal(err) - } - if f.ProfileID != albumsOnly.ID || !f.WantExisting || f.BaselineAt.Valid { - t.Errorf("follow = %+v: on the chosen profile, wanting what exists, baseline still to read", f) - } - - r2, _ := svc.Create(ctx, user, CreateParams{Kind: "artist", ArtistMBID: "artist-3", ArtistName: "Kero Kero Bonito"}) - if _, err := svc.Approve(ctx, r2.ID, user, pgtype.UUID{}); err != nil { - t.Fatal(err) - } - if f, _ := q.GetFollow(ctx, "artist-3"); f.ProfileName != "Standard" { - t.Errorf("no profile chosen: on %q, want the default", f.ProfileName) - } -} - -func TestRejectAndCancel(t *testing.T) { - pool := newPool(t) - user := seedUser(t, pool, "rejected") - svc := NewService(pool) - ctx := context.Background() - - r, _ := svc.Create(ctx, user, CreateParams{Kind: "artist", ArtistMBID: "a-1", ArtistName: "X"}) - if got, err := svc.Reject(ctx, r.ID, user, "low quality"); err != nil || got.Status != dbq.RequestStatusRejected { - t.Fatalf("reject: %+v, %v", got, err) - } - if _, err := svc.Reject(ctx, r.ID, user, "again"); !errors.Is(err, ErrNotPending) { - t.Errorf("reject twice: err = %v", err) - } - c, _ := svc.Create(ctx, user, CreateParams{Kind: "artist", ArtistMBID: "a-2", ArtistName: "Y"}) - if _, err := svc.Cancel(ctx, c.ID, user); err != nil { - t.Fatal(err) - } - if _, err := svc.Cancel(ctx, c.ID, user); !errors.Is(err, ErrNotPending) { - t.Errorf("cancel twice: err = %v", err) - } -} - -func TestReconciler_CompletesWhenTheWantIsHeld(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - user := seedUser(t, pool, "reconciled") - svc := NewService(pool) - ctx := context.Background() - - req, _ := svc.Create(ctx, user, CreateParams{ - Kind: "track", ArtistMBID: "artist-4", ArtistName: "Notify Artist", AlbumMBID: "group-notify", AlbumTitle: "Notify Album", - TrackMBID: "rec-wanted", TrackTitle: "Wanted Track", - }) - if _, err := svc.Approve(ctx, req.ID, user, pgtype.UUID{}); err != nil { - t.Fatal(err) - } - rec := NewReconciler(pool, quietLogger(), nil) - rec.SetNotifier(notifications.New(pool, nil, nil)) - if err := rec.tickOnce(ctx); err != nil { - t.Fatal(err) - } - if got, _ := q.GetRequestByID(ctx, req.ID); got.Status != dbq.RequestStatusApproved { - t.Fatalf("status %v before the want is held, want approved", got.Status) - } - - artist, album := seedAlbum(t, q, "Notify Artist", "artist-4", "Notify Album", "group-notify") - _ = seedTrack(t, q, album, artist, "Other Track", "/music/notify/01.flac") - wantedTrack := seedTrack(t, q, album, artist, "Wanted Track", "/music/notify/02.flac") - if _, err := pool.Exec(ctx, "UPDATE tracks SET mbid = 'rec-wanted', mbid_source = 'tag', added_at = added_at - interval '1 hour' WHERE id = $1", wantedTrack.ID); err != nil { - t.Fatal(err) - } - if _, err := pool.Exec(ctx, "UPDATE wants SET fulfilled_at = now(), album_id = $1 WHERE release_group_mbid = 'group-notify'", album.ID); err != nil { - t.Fatal(err) - } - for range 2 { // the second tick finds nothing left to complete - if err := rec.tickOnce(ctx); err != nil { - t.Fatal(err) - } - } - got, _ := q.GetRequestByID(ctx, req.ID) - if got.Status != dbq.RequestStatusCompleted || got.MatchedAlbumID != album.ID || got.MatchedTrackID != wantedTrack.ID { - t.Errorf("request = %+v: completed, matched to the album and the track it named", got) - } - inbox, err := q.ListNotifications(ctx, dbq.ListNotificationsParams{UserID: user, PageLimit: 10}) - if err != nil { - t.Fatal(err) - } - if len(inbox) != 1 || inbox[0].Kind != string(notifications.KindRequestCompleted) { - t.Fatalf("inbox = %+v, want one request_completed", inbox) - } - var p notifications.Payload - if err := json.Unmarshal(inbox[0].Payload, &p); err != nil { - t.Fatal(err) - } - if p.Name != "Notify Artist – Wanted Track" || p.AlbumID != formatUUIDForBus(album.ID) { - t.Errorf("payload = %+v", p) - } -} - -func TestReconciler_ArtistRequestCompletesOnceTheArtistIsHere(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - user := seedUser(t, pool, "artistreq") - svc := NewService(pool) - ctx := context.Background() - - req, _ := svc.Create(ctx, user, CreateParams{Kind: "artist", ArtistMBID: "artist-5", ArtistName: "Arrives"}) - if _, err := svc.Approve(ctx, req.ID, user, pgtype.UUID{}); err != nil { - t.Fatal(err) - } - rec := NewReconciler(pool, quietLogger(), nil) - if err := rec.tickOnce(ctx); err != nil { - t.Fatal(err) - } - if got, _ := q.GetRequestByID(ctx, req.ID); got.Status != dbq.RequestStatusApproved { - t.Fatalf("status %v with nothing here", got.Status) - } - artist, _ := seedAlbum(t, q, "Arrives", "artist-5", "First", "group-first") - if err := rec.tickOnce(ctx); err != nil { - t.Fatal(err) - } - if got, _ := q.GetRequestByID(ctx, req.ID); got.Status != dbq.RequestStatusCompleted || got.MatchedArtistID != artist.ID { - t.Errorf("request = %+v", got) - } -} - -func TestReconciler_RewantsWhatWasRemovedWhileTheRequestWaited(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - user := seedUser(t, pool, "rewant") - svc := NewService(pool) - ctx := context.Background() - - req, _ := svc.Create(ctx, user, CreateParams{ - Kind: "album", ArtistMBID: "artist-6", ArtistName: "Gone", AlbumMBID: "group-gone", AlbumTitle: "Gone", - }) - if _, err := svc.Approve(ctx, req.ID, user, pgtype.UUID{}); err != nil { - t.Fatal(err) - } - if _, err := pool.Exec(ctx, "DELETE FROM wants"); err != nil { - t.Fatal(err) - } - if err := NewReconciler(pool, quietLogger(), nil).tickOnce(ctx); err != nil { - t.Fatal(err) - } - if _, err := q.GetWantByReleaseGroup(ctx, "group-gone"); err != nil { - t.Errorf("an approved request's want should be recorded again: %v", err) - } -} diff --git a/internal/requests/service.go b/internal/requests/service.go deleted file mode 100644 index 203a7f53..00000000 --- a/internal/requests/service.go +++ /dev/null @@ -1,263 +0,0 @@ -// Package requests owns the lifecycle of what users ask to have added. The -// synchronous Service handles Create/List/Approve/Reject/Cancel; approving -// records the request on the wanted list: an album or a track -// wants its album's release group, an artist is followed. The async -// Reconciler (separate file) completes approved requests once the library -// holds what they asked for. -package requests - -import ( - "context" - "errors" - "fmt" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/apierror" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/wanted" -) - -// Public errors. Handlers map these to API error codes. -var ( - ErrInvalidKindFields = errors.New("requests: missing required fields for kind") - ErrNotPending = errors.New("requests: request is not pending") - // ErrNotFound aliases apierror.ErrNotFound so handlers can errors.Is - // against the shared sentinel. - ErrNotFound = apierror.ErrNotFound -) - -// CreateParams is the input for a new request from a user. AlbumMBID is the -// album's MusicBrainz release group. -type CreateParams struct { - Kind string // "artist", "album", or "track" - ArtistMBID string - AlbumMBID string // required for kind=album/track - TrackMBID string // required for kind=track - ArtistName string - AlbumTitle string // required for kind=album/track - TrackTitle string // required for kind=track -} - -// Service owns the request lifecycle. -type Service struct { - pool *pgxpool.Pool -} - -// NewService constructs a Service. -func NewService(pool *pgxpool.Pool) *Service { - return &Service{pool: pool} -} - -// Create validates the kind→required-fields invariant and inserts a -// pending row. -func (s *Service) Create(ctx context.Context, userID pgtype.UUID, p CreateParams) (dbq.Request, error) { - row, _, err := s.CreateTracked(ctx, userID, p) - return row, err -} - -// CreateTracked is Create, also reporting whether a row was inserted. A -// request that dedupes into one already in flight is not new, so callers -// that announce new requests say nothing for it. -func (s *Service) CreateTracked(ctx context.Context, userID pgtype.UUID, p CreateParams) (dbq.Request, bool, error) { - if err := validateKindFields(p); err != nil { - return dbq.Request{}, false, err - } - q := dbq.New(s.pool) - // Dedup: if a non-terminal request for this MBID already exists, - // return it instead of inserting a duplicate. The Discover/search UI - // already hides requested candidates; this backstops races and direct - // API callers. Match on the new request's own-kind MBID. - dedupMBID := p.ArtistMBID - switch p.Kind { - case "album": - dedupMBID = p.AlbumMBID - case "track": - dedupMBID = p.TrackMBID - } - if existing, derr := q.GetNonTerminalRequestForMBID(ctx, dedupMBID); derr == nil { - return existing, false, nil - } else if !errors.Is(derr, pgx.ErrNoRows) { - return dbq.Request{}, false, fmt.Errorf("create: dedup check: %w", derr) - } - row, err := q.CreateRequest(ctx, dbq.CreateRequestParams{ - UserID: userID, - Kind: dbq.RequestKind(p.Kind), - ArtistMbid: p.ArtistMBID, - AlbumMbid: strPtr(p.AlbumMBID), - TrackMbid: strPtr(p.TrackMBID), - ArtistName: p.ArtistName, - AlbumTitle: strPtr(p.AlbumTitle), - TrackTitle: strPtr(p.TrackTitle), - }) - if err != nil { - return dbq.Request{}, false, fmt.Errorf("create: %w", err) - } - return row, true, nil -} - -// DisplayName is a request as a person would say it: "Moe Shop" for an -// artist, "Moe Shop – WWW" for an album or a track. -func DisplayName(row dbq.Request) string { - if t := requestedTitle(row); t != "" { - return row.ArtistName + " – " + t - } - return row.ArtistName -} - -// requestedTitle is the album or track a request names, or "" for an -// artist request. DisplayName is the artist and this, joined. -func requestedTitle(row dbq.Request) string { - switch { - case row.Kind == dbq.RequestKindTrack && row.TrackTitle != nil: - return *row.TrackTitle - case row.Kind == dbq.RequestKindAlbum && row.AlbumTitle != nil: - return *row.AlbumTitle - } - return "" -} - -func validateKindFields(p CreateParams) error { - if p.ArtistMBID == "" || p.ArtistName == "" { - return fmt.Errorf("%w: artist_mbid and artist_name are always required", ErrInvalidKindFields) - } - switch p.Kind { - case "artist": - // fine - case "album": - if p.AlbumMBID == "" || p.AlbumTitle == "" { - return fmt.Errorf("%w: album kind requires album_mbid and album_title", ErrInvalidKindFields) - } - case "track": - if p.AlbumMBID == "" || p.AlbumTitle == "" { - return fmt.Errorf("%w: track kind requires album_mbid and album_title (its album is what is wanted)", ErrInvalidKindFields) - } - if p.TrackMBID == "" || p.TrackTitle == "" { - return fmt.Errorf("%w: track kind requires track_mbid and track_title", ErrInvalidKindFields) - } - default: - return fmt.Errorf("%w: unknown kind %q", ErrInvalidKindFields, p.Kind) - } - return nil -} - -// ListByStatus returns requests with the given status, ordered by requested_at DESC. -func (s *Service) ListByStatus(ctx context.Context, status string, limit int32) ([]dbq.Request, error) { - return dbq.New(s.pool).ListRequestsByStatus(ctx, dbq.ListRequestsByStatusParams{ - Status: dbq.RequestStatus(status), Limit: limit, - }) -} - -// ListForUser returns all requests by a user, ordered by requested_at DESC. -func (s *Service) ListForUser(ctx context.Context, userID pgtype.UUID, limit int32) ([]dbq.Request, error) { - return dbq.New(s.pool).ListRequestsForUser(ctx, dbq.ListRequestsForUserParams{ - UserID: userID, Limit: limit, - }) -} - -// Approve puts a pending request on the wanted list and marks it approved, -// in one transaction. An album or track request wants the album's release -// group, recorded as the requester's. An artist request follows the artist -// on profileID (the zero id means the default profile) and also wants what -// they have already released that the profile passes: someone asking for an -// artist wants their music, not only what comes next. -func (s *Service) Approve(ctx context.Context, requestID, approverID, profileID pgtype.UUID) (dbq.Request, error) { - var approved dbq.Request - err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { - q := dbq.New(tx) - row, err := q.GetRequestByID(ctx, requestID) - if errors.Is(err, pgx.ErrNoRows) { - return ErrNotFound - } - if err != nil { - return fmt.Errorf("approve: get: %w", err) - } - if row.Status != dbq.RequestStatusPending { - return ErrNotPending - } - switch row.Kind { - case dbq.RequestKindArtist: - if _, err := wanted.FollowWith(ctx, q, wanted.FollowParams{ - ArtistMBID: row.ArtistMbid, ArtistName: row.ArtistName, - ProfileID: profileID, By: approverID, WantExisting: true, - }); err != nil { - return err - } - default: - if _, err := wanted.WantWith(ctx, q, wantFor(row)); err != nil { - return err - } - } - approved, err = q.ApproveRequest(ctx, dbq.ApproveRequestParams{ID: requestID, DecidedBy: approverID}) - if err != nil { - return fmt.Errorf("approve: persist: %w", err) - } - return nil - }) - return approved, err -} - -// wantFor is the want an approved album or track request records. -func wantFor(row dbq.Request) wanted.WantParams { - p := wanted.WantParams{ - ArtistMBID: row.ArtistMbid, ArtistName: row.ArtistName, - Reason: wanted.ReasonRequest, CreatedBy: row.UserID, - } - if row.AlbumMbid != nil { - p.GroupMBID = *row.AlbumMbid - } - if row.AlbumTitle != nil { - p.Title = *row.AlbumTitle - } - return p -} - -// Reject transitions a pending request to rejected, recording notes and -// who decided. Returns ErrNotPending if the request is not pending, -// ErrNotFound if no such request exists. -func (s *Service) Reject(ctx context.Context, requestID pgtype.UUID, adminID pgtype.UUID, notes string) (dbq.Request, error) { - row, err := dbq.New(s.pool).RejectRequest(ctx, dbq.RejectRequestParams{ - ID: requestID, Notes: strPtr(notes), DecidedBy: adminID, - }) - if err != nil { - if errors.Is(err, pgx.ErrNoRows) { - // Either not found OR not pending — check which. - cur, gerr := dbq.New(s.pool).GetRequestByID(ctx, requestID) - if gerr != nil { - return dbq.Request{}, ErrNotFound - } - if cur.Status != dbq.RequestStatusPending { - return dbq.Request{}, ErrNotPending - } - return dbq.Request{}, ErrNotFound - } - return dbq.Request{}, fmt.Errorf("reject: %w", err) - } - return row, nil -} - -// Cancel lets a user withdraw their own pending request. The SQL WHERE -// clause enforces both ownership (user_id = $2) and pending status -// (status = 'pending'). A zero-rows result always means ErrNotPending -// from the caller's perspective. -func (s *Service) Cancel(ctx context.Context, requestID pgtype.UUID, userID pgtype.UUID) (dbq.Request, error) { - row, err := dbq.New(s.pool).CancelRequest(ctx, dbq.CancelRequestParams{ - ID: requestID, DecidedBy: userID, - }) - if err != nil { - if errors.Is(err, pgx.ErrNoRows) { - return dbq.Request{}, ErrNotPending - } - return dbq.Request{}, fmt.Errorf("cancel: %w", err) - } - return row, nil -} - -func strPtr(s string) *string { - if s == "" { - return nil - } - return &s -} diff --git a/internal/secret/secret_test.go b/internal/secret/secret_test.go index 70982e2d..24d3aabe 100644 --- a/internal/secret/secret_test.go +++ b/internal/secret/secret_test.go @@ -104,7 +104,7 @@ func TestEveryColumnHasADistinctPurpose(t *testing.T) { } seen[c.Purpose()] = true } - if !seen[AcquisitionAPIKey.Purpose()] || !seen[SubsonicPassword.Purpose()] { + if !seen[AcoustIDAPIKey.Purpose()] || !seen[SubsonicPassword.Purpose()] { t.Error("Columns is missing a declared column") } } diff --git a/internal/secret/stored.go b/internal/secret/stored.go index 3fa5c73d..a8253f59 100644 --- a/internal/secret/stored.go +++ b/internal/secret/stored.go @@ -27,23 +27,20 @@ func (c Column) Purpose() string { return c.Table + "." + c.Column } // credential Minstrel stores for an outside system, or that must be read back // in the clear, is listed here. var ( - AcquisitionAPIKey = Column{"acquisition_services", "api_key", "code", false} - AcquisitionPassword = Column{"acquisition_services", "password", "code", false} - AcoustIDAPIKey = Column{"acoustid_settings", "api_key", "id", true} - SMTPPassword = Column{"smtp_config", "password", "id", true} - ListenBrainzToken = Column{"users", "listenbrainz_token", "id", false} - SubsonicPassword = Column{"users", "subsonic_password", "id", false} - CoverArtAPIKey = Column{"cover_art_provider_settings", "api_key", "provider_id", false} - TagProviderAPIKey = Column{"tag_provider_settings", "api_key", "provider_id", false} - BackupSecretKey = Column{"backup_settings", "secret_key", "id", true} + AcoustIDAPIKey = Column{"acoustid_settings", "api_key", "id", true} + SMTPPassword = Column{"smtp_config", "password", "id", true} + ListenBrainzToken = Column{"users", "listenbrainz_token", "id", false} + SubsonicPassword = Column{"users", "subsonic_password", "id", false} + CoverArtAPIKey = Column{"cover_art_provider_settings", "api_key", "provider_id", false} + TagProviderAPIKey = Column{"tag_provider_settings", "api_key", "provider_id", false} + BackupSecretKey = Column{"backup_settings", "secret_key", "id", true} ) // Columns is every sealed column, for SealStored and for anything that has to // know which columns hold credentials (a backup leaves them out). var Columns = []Column{ - AcquisitionAPIKey, AcquisitionPassword, AcoustIDAPIKey, SMTPPassword, - ListenBrainzToken, SubsonicPassword, CoverArtAPIKey, TagProviderAPIKey, - BackupSecretKey, + AcoustIDAPIKey, SMTPPassword, ListenBrainzToken, SubsonicPassword, + CoverArtAPIKey, TagProviderAPIKey, BackupSecretKey, } // Owner is the owner string for a row whose key is key. A single-row table's diff --git a/internal/secret/stored_test.go b/internal/secret/stored_test.go index 71e6b2d1..853172b0 100644 --- a/internal/secret/stored_test.go +++ b/internal/secret/stored_test.go @@ -67,14 +67,7 @@ func TestSealStoredSealsWhatIsInTheClear_Integration(t *testing.T) { if err != nil { t.Fatalf("user: %v", err) } - if _, err := q.CreateAcquisitionService(ctx, dbq.CreateAcquisitionServiceParams{ - Code: "seal-test", Kind: "search", Protocol: "prowlarr", Label: "Seal test", - Category: "", TimeoutMs: 1000, SearchesPerHour: 0, - }); err != nil { - t.Fatalf("acquisition row: %v", err) - } seed := []string{ - `UPDATE acquisition_services SET api_key = 'acq-key', password = 'acq-pw' WHERE code = 'seal-test'`, `UPDATE acoustid_settings SET api_key = 'acoustid-key' WHERE id = true`, `UPDATE smtp_config SET password = 'smtp-pw' WHERE id = true`, fmt.Sprintf(`UPDATE users SET listenbrainz_token = 'lb-token', subsonic_password = 'sub-pw' WHERE id = '%s'`, u.ID.String()), @@ -95,7 +88,6 @@ func TestSealStoredSealsWhatIsInTheClear_Integration(t *testing.T) { for _, s := range []string{ `DELETE FROM cover_art_provider_settings WHERE provider_id = 'seal-test'`, `DELETE FROM tag_provider_settings WHERE provider_id = 'seal-test'`, - `DELETE FROM acquisition_services WHERE code = 'seal-test'`, `UPDATE acoustid_settings SET api_key = NULL WHERE id = true`, `UPDATE smtp_config SET password = '' WHERE id = true`, `UPDATE backup_settings SET secret_key = '' WHERE id = true`, @@ -109,8 +101,6 @@ func TestSealStoredSealsWhatIsInTheClear_Integration(t *testing.T) { key string plain string }{ - {secret.AcquisitionAPIKey, "seal-test", "acq-key"}, - {secret.AcquisitionPassword, "seal-test", "acq-pw"}, {secret.AcoustIDAPIKey, "true", "acoustid-key"}, {secret.SMTPPassword, "true", "smtp-pw"}, {secret.ListenBrainzToken, u.ID.String(), "lb-token"}, diff --git a/internal/server/server.go b/internal/server/server.go index c9d66873..6a26d44e 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -26,9 +26,7 @@ import ( "git.fabledsword.com/bvandeusen/minstrel/internal/playevents" "git.fabledsword.com/bvandeusen/minstrel/internal/playlists" "git.fabledsword.com/bvandeusen/minstrel/internal/quarantine" - "git.fabledsword.com/bvandeusen/minstrel/internal/reacquisition" "git.fabledsword.com/bvandeusen/minstrel/internal/recsettings" - "git.fabledsword.com/bvandeusen/minstrel/internal/requests" "git.fabledsword.com/bvandeusen/minstrel/internal/subsonic" "git.fabledsword.com/bvandeusen/minstrel/internal/tags" "git.fabledsword.com/bvandeusen/minstrel/internal/tracks" @@ -90,11 +88,6 @@ type Server struct { // status and "look up now" the admin card drives. Nil in test contexts; // one is built (and never run) so the admin routes still answer. AcoustIDLookup *library.AcoustIDLookupWorker - // ReacqSettings is the DB-backed missing-file re-acquisition policy — the - // same instance the sweeper in cmd/minstrel/main.go reads, so a save from - // the admin card reaches it without a restart. Router() constructs a - // fallback when nil (tests). - ReacqSettings *reacquisition.SettingsService // Backups is the server's own backup, the instance whose loop runs the // nightly schedule. Nil builds one that runs only when an admin asks. Backups *backup.Service @@ -143,26 +136,12 @@ func (s *Server) Router() http.Handler { s.EventsCfg.SkipMaxCompletionRatio, s.EventsCfg.SkipMaxDurationPlayedMs, ) - requestSvc := requests.NewService(s.Pool) - reacqSettings := s.ReacqSettings - if reacqSettings == nil { - // Test contexts construct Server without main.go's boot wiring. - // Always usable even when the load fails — it falls back to the - // shipped defaults rather than leaving the admin card unable to - // render (same posture as netsettings above). - var raErr error - reacqSettings, raErr = reacquisition.NewSettingsService( - context.Background(), s.Pool, s.Logger) - if raErr != nil { - s.Logger.Warn("reacquisition settings unavailable; serving defaults", "err", raErr) - } - } quarantineSvc := quarantine.NewService(s.Pool, s.DataDir) tracksSvc := tracks.NewService(s.Pool, s.Logger, s.DataDir) playlistsSvc := playlists.NewService(s.Pool, s.Logger, s.DataDir) smtpSender := mailer.NewSMTPSender(s.Pool, s.Logger.With("component", "mailer")) // Live-event bus for SSE subscribers, one per process: producers in - // playevents / requests / scanner publish into the same instance. + // playevents / scanner publish into the same instance. // Background workers are built in cmd/minstrel/main.go before Router() // runs and share s.Bus. With no bus set (tests), a fresh local one is // used. @@ -211,7 +190,7 @@ func (s *Server) Router() http.Handler { if backups == nil { backups = backup.NewService(s.Pool, s.Logger, buildinfo.Version) } - api.Mount(r, s.Pool, s.Logger, writer, s.RecommendationCfg, recSettings, requestSvc, quarantineSvc, tracksSvc, playlistsSvc, s.CoverEnricher, s.CoverSettings, s.TagSettings, s.LibraryScanner, s.ScanCfg, s.DataDir, smtpSender, bus, s.PlaylistScheduler, s.StreamSecret, netSettings, reacqSettings, fpSettings, loudSettings, acoustIDLookup, backups, s.Restart) + api.Mount(r, s.Pool, s.Logger, writer, s.RecommendationCfg, recSettings, quarantineSvc, tracksSvc, playlistsSvc, s.CoverEnricher, s.CoverSettings, s.TagSettings, s.LibraryScanner, s.ScanCfg, s.DataDir, smtpSender, bus, s.PlaylistScheduler, s.StreamSecret, netSettings, fpSettings, loudSettings, acoustIDLookup, backups, s.Restart) // /api/admin/scan is the only admin route owned by the server package // (it needs the Scanner). Register it as a single inline-middleware // route — using r.Route("/api/admin", ...) here would create a second diff --git a/internal/server/server_test.go b/internal/server/server_test.go index 41900cad..07677cab 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -1,7 +1,6 @@ package server import ( - "bytes" "context" "encoding/json" "io" @@ -21,7 +20,6 @@ import ( "git.fabledsword.com/bvandeusen/minstrel/internal/db" "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" "git.fabledsword.com/bvandeusen/minstrel/internal/library" - "git.fabledsword.com/bvandeusen/minstrel/internal/reacquisition" "git.fabledsword.com/bvandeusen/minstrel/internal/subsonic" ) @@ -242,104 +240,6 @@ func TestRouter_AdminSubtreeNotShadowed(t *testing.T) { } } -// TestRouter_ReacquisitionSettingsSavedThroughTheAPIReachTheSweeper: a -// Router() that built a reacquisition.SettingsService of its own would refresh -// THAT instance's cache on a save, while the sweeper in cmd/minstrel/main.go -// kept serving what it loaded at boot. The card would show the new policy and -// the feature run the old one until a restart. -// -// The assertion is made against the instance main.go hands the sweeper: save -// through the router, then read that instance. A second service leaves it stale. -func TestRouter_ReacquisitionSettingsSavedThroughTheAPIReachTheSweeper(t *testing.T) { - dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") - if dsn == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - logger := slog.New(slog.NewTextHandler(io.Discard, nil)) - if err := db.Migrate(dsn, logger); err != nil { - t.Fatalf("migrate: %v", err) - } - pool, err := pgxpool.New(context.Background(), dsn) - if err != nil { - t.Fatalf("pool: %v", err) - } - t.Cleanup(pool.Close) - - ctx := context.Background() - q := dbq.New(pool) - _, _ = pool.Exec(ctx, "DELETE FROM sessions WHERE user_agent = 'reacq-settings-test'") - _, _ = pool.Exec(ctx, "DELETE FROM users WHERE username = 'test-reacq-settings-admin'") - user, err := q.CreateUser(ctx, dbq.CreateUserParams{ - Username: "test-reacq-settings-admin", - PasswordHash: "x", - ApiTokenHash: "test-reacq-settings-token", - IsAdmin: true, - }) - if err != nil { - t.Fatalf("CreateUser: %v", err) - } - t.Cleanup(func() { _, _ = pool.Exec(ctx, "DELETE FROM users WHERE id = $1", user.ID) }) - token := "reacq-settings-test-" + time.Now().Format("20060102150405.000000") - tokenHash := auth.HashSessionToken(token) - if _, err := pool.Exec(ctx, - "INSERT INTO sessions (user_id, token_hash, user_agent) VALUES ($1, $2, 'reacq-settings-test')", - user.ID, tokenHash[:], - ); err != nil { - t.Fatalf("insert session: %v", err) - } - - // The sweeper's service. Nothing else in the process may write to the - // settings for the assertion below to mean what it says. - sweeperSettings, err := reacquisition.NewSettingsService(ctx, pool, logger) - if err != nil { - t.Fatalf("reacquisition settings: %v", err) - } - before := sweeperSettings.Get() - t.Cleanup(func() { - if _, err := sweeperSettings.Set(context.Background(), before); err != nil { - t.Errorf("restore reacquisition settings: %v", err) - } - }) - wantGrace := before.GraceHours + 1 - if wantGrace > 720 { - wantGrace = before.GraceHours - 1 - } - - s := New(logger, pool, stubScanner{}, subsonic.Config{}, config.EventsConfig{}, - config.RecommendationConfig{}, "", config.BrandingConfig{}, nil, nil, nil, library.RunScanConfig{}) - s.ReacqSettings = sweeperSettings - ts := httptest.NewServer(s.Router()) - defer ts.Close() - - body, err := json.Marshal(map[string]any{ - "enabled": before.Enabled, - "grace_hours": wantGrace, - "max_per_pass": before.MaxPerPass, - }) - if err != nil { - t.Fatalf("marshal: %v", err) - } - req, err := http.NewRequest(http.MethodPut, ts.URL+"/api/admin/library/reacquisition", bytes.NewReader(body)) - if err != nil { - t.Fatalf("build request: %v", err) - } - req.Header.Set("Authorization", "Bearer "+token) - req.Header.Set("Content-Type", "application/json") - resp, err := http.DefaultClient.Do(req) - if err != nil { - t.Fatalf("PUT reacquisition settings: %v", err) - } - defer func() { _ = resp.Body.Close() }() - if resp.StatusCode != http.StatusOK { - t.Fatalf("PUT reacquisition settings: status = %d, want 200", resp.StatusCode) - } - - if got := sweeperSettings.Get().GraceHours; got != wantGrace { - t.Fatalf("the sweeper's settings hold grace_hours = %d after the save, want %d — "+ - "the API wrote through a different service instance", got, wantGrace) - } -} - // stubScanner is a no-op ScanTrigger used only to make Server.Router() // register /api/admin/scan. Its Scan method must never be called by the // route-presence assertions in this file. diff --git a/internal/similarity/worker.go b/internal/similarity/worker.go index 461a4c79..29db25e4 100644 --- a/internal/similarity/worker.go +++ b/internal/similarity/worker.go @@ -8,7 +8,7 @@ // in the library at the time, so a recording that reaches the library later // (an import, or an MBID the AcoustID lookup fills in) gains its edges without // asking LB again. For artists it stores the in-library matches in -// artist_similarity and the rest in artist_similarity_unmatched. +// artist_similarity. // // Every answer is recorded as a fetch, an empty one included, and the queue // reads those records rather than the edges: a seed that matched nothing is @@ -39,7 +39,7 @@ type Worker struct { logger *slog.Logger tick time.Duration batch int32 - topK int // per artist, for each of the matched and unmatched stores + topK int // per artist } // NewWorker constructs a worker with production defaults: 1h tick, @@ -200,8 +200,8 @@ func (w *Worker) upsertArtistSimilar(ctx context.Context, q *dbq.Queries, artist } sort.Slice(results, func(i, j int) bool { return results[i].Score > results[j].Score }) // Scores are stored relative to the seed's closest artist, as track edges - // are: LB's raw scores are unbounded counts, and both the candidate pool - // and the suggestions read 0 to 1. + // are: LB's raw scores are unbounded counts, and the candidate pool reads + // 0 to 1. best := results[0].Score mbids := make([]string, 0, len(results)) @@ -220,7 +220,7 @@ func (w *Worker) upsertArtistSimilar(ctx context.Context, q *dbq.Queries, artist } } - // Matched: in-library similars → artist_similarity (existing path). + // In-library similars → artist_similarity. takenMatched := 0 for _, r := range results { if takenMatched >= w.topK { @@ -242,33 +242,6 @@ func (w *Worker) upsertArtistSimilar(ctx context.Context, q *dbq.Queries, artist takenMatched++ } - // Unmatched: out-of-library similars → artist_similarity_unmatched (M5c). - // Same top-K cap as the matched path. Skip rows missing a name — we can't - // render a suggestion card without one. - takenUnmatched := 0 - for _, r := range results { - if takenUnmatched >= w.topK { - break - } - if _, inLib := idByMBID[r.MBID]; inLib { - continue - } - if r.Name == "" { - w.logger.Debug("similarity: skipping unmatched similar with empty name", "mbid", r.MBID) - continue - } - if uerr := q.UpsertArtistSimilarityUnmatched(ctx, dbq.UpsertArtistSimilarityUnmatchedParams{ - SeedArtistID: artistAID, - CandidateMbid: r.MBID, - CandidateName: r.Name, - Score: relativeScore(r.Score, best), - Source: "listenbrainz", - }); uerr != nil { - w.logger.Warn("similarity: UpsertArtistSimilarityUnmatched", "err", uerr) - continue - } - takenUnmatched++ - } } // relativeScore is score over the seed's best, in [0, 1]; 0 when the seed has diff --git a/internal/tags/enricher.go b/internal/tags/enricher.go index a17ef139..b42ddb7a 100644 --- a/internal/tags/enricher.go +++ b/internal/tags/enricher.go @@ -91,9 +91,8 @@ func (e *Enricher) EnrichTrack(ctx context.Context, trackID pgtype.UUID, ref Tra return outcomeNone, nil } -// tagFetch pairs a provider ID with a bound fetch call, so the merge-and- -// classify loop below is shared between the track chain and the candidate- -// artist chain instead of being written twice with one word changed. +// tagFetch pairs a provider ID with a bound fetch call for the merge-and- +// classify loop below. type tagFetch struct { providerID string fetch func(context.Context) ([]Tag, error) @@ -116,8 +115,7 @@ type chainResult struct { // eligible for a retry rather than wrongly settling it as "nothing exists" — // the distinction between those two is the whole point of the return value. // -// logKey/logVal identify the subject in warnings (a track id or a candidate -// MBID), since this is shared across entity types. +// logKey/logVal identify the subject in warnings. func (e *Enricher) runChain(ctx context.Context, calls []tagFetch, logKey, logVal string) chainResult { merged := map[string]float64{} contributors := map[string]bool{} @@ -240,146 +238,6 @@ func (e *Enricher) EnrichTrackBatch(ctx context.Context, limit int, return processed, enriched, settledNone + leftNull + errored, nil } -// EnrichCandidateArtist runs the artist-tag chain for one out-of-library -// Discover candidate and caches the merged result. -// -// Mirrors EnrichTrack, with one deliberate difference in the transient case: -// there is no row to "leave NULL", because eligibility is the ABSENCE of a -// candidate_artist_tag_state row. So a transient failure writes nothing at all, -// which leaves the candidate eligible for the next tick. Writing a state row -// here would settle a candidate whose tags we simply failed to fetch. -func (e *Enricher) EnrichCandidateArtist(ctx context.Context, mbid, name string) (outcome, error) { - providers := e.settings.EnabledArtistTagProviders() - ref := ArtistRef{MBID: mbid, Name: name} - calls := make([]tagFetch, 0, len(providers)) - for _, provider := range providers { - calls = append(calls, tagFetch{ - providerID: provider.ID(), - fetch: func(c context.Context) ([]Tag, error) { return provider.FetchArtistTags(c, ref) }, - }) - } - res := e.runChain(ctx, calls, "candidate_mbid", mbid) - version := e.settings.CurrentVersion() - - if len(res.merged) > 0 { - top := topKByWeight(res.merged, e.topK) - if err := e.writeCandidateTags(ctx, mbid, top, sourceLabel(res.contributors), version); err != nil { - return outcomeLeftNull, err - } - return outcomeEnriched, nil - } - if res.anyTransient { - return outcomeLeftNull, nil - } - if err := e.writeCandidateTags(ctx, mbid, nil, sourceNone, version); err != nil { - return outcomeNone, err - } - return outcomeNone, nil -} - -// writeCandidateTags atomically replaces a candidate's cached tags and stamps -// its state. tags may be empty (the 'none' settle path), which clears any prior -// tags and records that the providers had nothing. -func (e *Enricher) writeCandidateTags( - ctx context.Context, mbid string, tags []Tag, source string, version int32, -) error { - tx, err := e.pool.Begin(ctx) - if err != nil { - return fmt.Errorf("begin tx: %w", err) - } - defer func() { _ = tx.Rollback(ctx) }() - q := dbq.New(tx) - if err := q.DeleteCandidateArtistTags(ctx, mbid); err != nil { - return fmt.Errorf("delete candidate artist tags: %w", err) - } - for _, t := range tags { - if err := q.InsertCandidateArtistTag(ctx, dbq.InsertCandidateArtistTagParams{ - CandidateMbid: mbid, Tag: t.Name, Weight: t.Weight, - }); err != nil { - return fmt.Errorf("insert candidate artist tag: %w", err) - } - } - if err := q.SetCandidateArtistTagState(ctx, dbq.SetCandidateArtistTagStateParams{ - CandidateMbid: mbid, - TagSource: source, - TagSourcesVersion: version, - }); err != nil { - return fmt.Errorf("set candidate artist tag state: %w", err) - } - if err := tx.Commit(ctx); err != nil { - return fmt.Errorf("commit: %w", err) - } - return nil -} - -// EnrichCandidateArtistBatch drains up to limit out-of-library candidates and -// enriches each serially. Same limit semantics as EnrichTrackBatch: 0 = -// disabled, >0 = bounded, <0 = unbounded. -// -// The candidate pool is far larger than the track pool (every library artist's -// neighbours) and can never be drained in one pass at MusicBrainz's ~1 req/s, -// so the query hands them back in descending similarity order — see -// ListCandidateArtistsMissingTags. A bounded batch here is therefore normal -// operation, not a degraded mode. -func (e *Enricher) EnrichCandidateArtistBatch(ctx context.Context, limit int) ( - processed, succeeded, failed int, err error, -) { - if limit == 0 { - return 0, 0, 0, nil - } - queryLimit := int32(limit) - if limit < 0 { - queryLimit = 1<<31 - 1 - } - q := dbq.New(e.pool) - rows, qerr := q.ListCandidateArtistsMissingTags(ctx, dbq.ListCandidateArtistsMissingTagsParams{ - TagSourcesVersion: e.settings.CurrentVersion(), - Limit: queryLimit, - }) - if qerr != nil { - return 0, 0, 0, fmt.Errorf("list candidate artists missing tags: %w", qerr) - } - - var enriched, settledNone, leftNull, errored int - for _, r := range rows { - if ctx.Err() != nil { - e.logCandidateBatchSummary(len(rows), processed, enriched, settledNone, leftNull, errored) - return processed, enriched, settledNone + leftNull + errored, ctx.Err() - } - processed++ - oc, eerr := e.EnrichCandidateArtist(ctx, r.CandidateMbid, r.CandidateName) - if eerr != nil { - e.logger.Warn("tags: candidate batch entry failed", - "candidate_mbid", r.CandidateMbid, "err", eerr) - errored++ - continue - } - switch oc { - case outcomeEnriched: - enriched++ - case outcomeNone: - settledNone++ - case outcomeLeftNull: - leftNull++ - } - } - e.logCandidateBatchSummary(len(rows), processed, enriched, settledNone, leftNull, errored) - return processed, enriched, settledNone + leftNull + errored, nil -} - -// logCandidateBatchSummary mirrors logBatchSummary. `settled_none` is the -// honest-ceiling counter for this surface: candidates whose MBID has no -// upstream tags at all, which no amount of retrying will fix. -func (e *Enricher) logCandidateBatchSummary(eligible, processed, enriched, settledNone, leftNull, errored int) { - e.logger.Info("tags: candidate-artist enrichment batch complete", - "eligible", eligible, - "processed", processed, - "enriched", enriched, - "settled_none", settledNone, - "left_null", leftNull, - "errored", errored) -} - // logBatchSummary emits one Info line with the category breakdown — the // enriched/settled/left-null split is the diagnostic for a // "0 enriched" symptom the collapsed tally can't explain. diff --git a/internal/tags/provider_musicbrainz.go b/internal/tags/provider_musicbrainz.go index d477494d..a3cc6912 100644 --- a/internal/tags/provider_musicbrainz.go +++ b/internal/tags/provider_musicbrainz.go @@ -3,7 +3,6 @@ package tags import ( "context" "errors" - "fmt" "net/http" "net/url" "sync/atomic" @@ -158,62 +157,6 @@ func (p *musicbrainzProvider) TestConnection(ctx context.Context) error { return err } -// ReleaseGroupForRelease names the MusicBrainz release group a release -// belongs to. Minstrel's albums.mbid is the release id from the tags, -// while wants know albums only by release group, so re-acquiring a lost -// album needs this translation when its files (and their release-group tag) -// are already gone. -// -// It goes through the registered MusicBrainz provider, so the lookup shares -// that provider's client and 1 req/s limiter with tag enrichment rather than -// doubling Minstrel's request rate, and it respects the provider's on/off -// switch. ErrNotFound: MusicBrainz is switched off, or has no release under -// that id (an id that is already a release group lands here too). -// ErrTransient: retry later. -func ReleaseGroupForRelease(ctx context.Context, releaseMBID string) (string, error) { - p, err := ProviderByID("musicbrainz") - if err != nil { - return "", ErrSwitchedOff - } - mb, ok := p.(*musicbrainzProvider) - if !ok { - return "", ErrSwitchedOff - } - return mb.releaseGroupForRelease(ctx, releaseMBID) -} - -// ErrSwitchedOff is ReleaseGroupForRelease's answer while MusicBrainz is -// switched off. It is an ErrNotFound, so callers that only skip still do; a -// caller that would record "MusicBrainz has none" can tell it apart. -var ErrSwitchedOff = fmt.Errorf("%w: MusicBrainz is switched off", ErrNotFound) - -// mbReleaseResponse models the subset of /ws/2/release/{mbid}?inc=release-groups -// we read. -type mbReleaseResponse struct { - ReleaseGroup struct { - ID string `json:"id"` - } `json:"release-group"` -} - -func (p *musicbrainzProvider) releaseGroupForRelease(ctx context.Context, releaseMBID string) (string, error) { - if !p.enabled.Load() { - return "", ErrSwitchedOff - } - if releaseMBID == "" { - return "", ErrNotFound - } - q := url.Values{"inc": {"release-groups"}, "fmt": {"json"}} - full := mbBaseURL + "/release/" + url.PathEscape(releaseMBID) + "?" + q.Encode() - var resp mbReleaseResponse - if err := p.client.getJSON(ctx, full, &resp); err != nil { - return "", err - } - if resp.ReleaseGroup.ID == "" { - return "", ErrNotFound - } - return resp.ReleaseGroup.ID, nil -} - // normalizeMBTags drops non-positive-vote tags (net-downvoted or zero) and // scales the remaining vote counts into [0,1] relative to the strongest tag // on this recording, so one recording's raw counts don't dominate another's. diff --git a/internal/tags/provider_musicbrainz_test.go b/internal/tags/provider_musicbrainz_test.go index 042887a1..8af8774c 100644 --- a/internal/tags/provider_musicbrainz_test.go +++ b/internal/tags/provider_musicbrainz_test.go @@ -176,37 +176,3 @@ func TestMusicBrainzFetch_EmptyTags(t *testing.T) { t.Errorf("empty tags: err = %v, want ErrNotFound", err) } } - -// A release id resolves to its release group through -// /release/?inc=release-groups; an unknown id (or one that is already a -// group) and a switched-off source both say ErrNotFound. -func TestMusicBrainzReleaseGroupForRelease(t *testing.T) { - const release, group = "rel-1", "group-1" - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/release/"+release { - w.WriteHeader(http.StatusNotFound) - _, _ = w.Write([]byte(`{"error":"Not Found"}`)) - return - } - if got := r.URL.Query().Get("inc"); got != "release-groups" { - t.Errorf("inc = %q, want release-groups", got) - } - _, _ = w.Write([]byte(`{"id":"` + release + `","title":"X","release-group":{"id":"` + group + `","title":"X"}}`)) - })) - defer srv.Close() - old := mbBaseURL - mbBaseURL = srv.URL - defer func() { mbBaseURL = old }() - - ctx := context.Background() - got, err := newMBProvider(true).releaseGroupForRelease(ctx, release) - if err != nil || got != group { - t.Fatalf("resolve = (%q, %v), want (%q, nil)", got, err, group) - } - if _, err := newMBProvider(true).releaseGroupForRelease(ctx, "group-1"); !errors.Is(err, ErrNotFound) { - t.Errorf("unknown release: err = %v, want ErrNotFound", err) - } - if _, err := newMBProvider(false).releaseGroupForRelease(ctx, release); !errors.Is(err, ErrNotFound) { - t.Errorf("switched off: err = %v, want ErrNotFound", err) - } -} diff --git a/internal/tags/worker.go b/internal/tags/worker.go index 0706a7ff..701a08f3 100644 --- a/internal/tags/worker.go +++ b/internal/tags/worker.go @@ -14,32 +14,22 @@ import ( // providers' httpClients, so a tick just drains a bounded batch and the // external APIs pace themselves. type Worker struct { - enricher *Enricher - logger *slog.Logger - tick time.Duration - batch int - candidateBatch int + enricher *Enricher + logger *slog.Logger + tick time.Duration + batch int } // NewWorker constructs a worker with production defaults: an initial drain // shortly after boot, then every 30 minutes, up to 200 tracks per tick. // MusicBrainz's 1 req/s ceiling is the real throttle, so the batch size // mainly bounds how long one tick runs, not the request rate. -// -// candidateBatch is smaller than the track batch on purpose. Library tracks are -// a finite set that drains to completion and then costs nothing; out-of-library -// candidates are effectively unbounded — every library artist's -// neighbours — so this arm would otherwise monopolise every tick forever and -// starve the track arm. 50/tick at ~1 req/s is roughly a minute of work, and -// the query hands back the highest-similarity candidates first so the ones that -// can actually reach a user's deck are enriched first. func NewWorker(enricher *Enricher, logger *slog.Logger) *Worker { return &Worker{ - enricher: enricher, - logger: logger, - tick: 30 * time.Minute, - batch: 200, - candidateBatch: 50, + enricher: enricher, + logger: logger, + tick: 30 * time.Minute, + batch: 200, } } @@ -58,27 +48,12 @@ func (w *Worker) Run(ctx context.Context) { } } -// tickOnce drains one bounded batch of each kind. Both Enrich*Batch methods -// already log a category breakdown, so this only surfaces a fatal batch error. -// -// Tracks first: they back the taste profile the whole app reads from, whereas -// candidate tags only affect the Discover request surface. On a fresh install -// both are cold, and getting the profile warm matters more. +// tickOnce drains one bounded batch. EnrichTrackBatch already logs a +// category breakdown, so this only surfaces a fatal batch error. func (w *Worker) tickOnce(ctx context.Context) { if _, _, _, err := w.enricher.EnrichTrackBatch(ctx, w.batch, nil); err != nil { if ctx.Err() == nil { w.logger.Error("tags: enrichment tick failed", "err", err) } } - // Not gated on the track arm's success: the two drains share nothing but a - // provider chain, and a track-side failure says nothing about whether - // candidate lookups will work. - if ctx.Err() != nil { - return - } - if _, _, _, err := w.enricher.EnrichCandidateArtistBatch(ctx, w.candidateBatch); err != nil { - if ctx.Err() == nil { - w.logger.Error("tags: candidate-artist enrichment tick failed", "err", err) - } - } } diff --git a/internal/wanted/library.go b/internal/wanted/library.go deleted file mode 100644 index ad397b25..00000000 --- a/internal/wanted/library.go +++ /dev/null @@ -1,174 +0,0 @@ -package wanted - -import ( - "context" - "errors" - "fmt" - "time" - - "github.com/jackc/pgx/v5" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -// ReasonLibrary: on the list because the library held it when the list -// began. -const ReasonLibrary = "library" - -// libraryMatchBudget bounds the time one pass spends matching albums to -// MusicBrainz, at its one request a second, so the pass still settles its -// wants; the rest wait for the next pass. -const libraryMatchBudget = 2 * time.Minute - -// libraryMatchBatch is how many albums one query hands the matching. -const libraryMatchBatch = 25 - -// libraryMatcher is the matching's version. Raise it when the matching -// changes in a way that could match an album it refused: albums an older -// version refused are matched again, even after the listing is done. -// -// 1 tags, release, discography and search, checked by title and length -// 2 titles alone under the album's own name; a refusal names the track -// 3 a second copy of a track counts once; joining words count for nothing -// 4 an album holding every track of an edition, and more, is that group -const libraryMatcher = 4 - -// listLibrary puts the library's albums on the wanted list as held, once, -// so that from then on the list is the record of what the library keeps: an -// album taken off it stays off, and files dropped in by hand are not listed. -// It runs a step each pass until done: -// -// - the first step stamps when the listing began, which is its cutoff: -// an album the library gains afterwards, dropped in by hand, is never -// listed; -// - every album whose tags name its release group and artist goes on the -// list at once; -// - every other album is matched to MusicBrainz, within a time -// budget a pass, and listed as it is matched; -// - once none is left to match, the listing is stamped done, with a count -// of the albums no match was found for. -// -// Each album decided is recorded in library_album_matches, so an album taken -// off the list stays off. Once done the listing runs again only for albums -// an older matcher refused (libraryMatcher), refused albums whose tracks -// have changed since, and library tracks a re-tag filed under a new album. -// A person matches the rest by hand, or leaves them off, in the -// Collection's Unmatched section (MatchAlbumTo, LeaveAlbumOff). -func (s *Service) listLibrary(ctx context.Context, res *PassResult) error { - q := dbq.New(s.pool) - state, err := q.GetLibraryListing(ctx) - if err != nil { - return fmt.Errorf("wanted: read the library listing: %w", err) - } - if state.LibraryListedAt.Valid { - stale, err := q.CountAlbumsToMatch(ctx, dbq.CountAlbumsToMatchParams{ - Cutoff: state.LibraryListingStartedAt, Matcher: libraryMatcher}) - if err != nil { - return fmt.Errorf("wanted: count albums to match again: %w", err) - } - if stale == 0 { - return nil - } - } - cutoff := state.LibraryListingStartedAt - if !cutoff.Valid { - if cutoff, err = q.StartLibraryListing(ctx); err != nil { - return fmt.Errorf("wanted: start the library listing: %w", err) - } - if err := q.RecordListedAlbums(ctx, cutoff); err != nil { - return fmt.Errorf("wanted: record the library: %w", err) - } - n, err := q.ListLibraryAlbums(ctx, dbq.ListLibraryAlbumsParams{Cutoff: cutoff}) - if err != nil { - return fmt.Errorf("wanted: list the library: %w", err) - } - res.Listed += int(n) - s.logger.Info("wanted: listing the library's albums as held", "listed", n) - } - - deadline := s.now().Add(libraryMatchBudget) - for { - albums, err := q.ListAlbumsToMatch(ctx, dbq.ListAlbumsToMatchParams{ - Cutoff: cutoff, Matcher: libraryMatcher, MaxRows: libraryMatchBatch}) - if err != nil { - return fmt.Errorf("wanted: albums to match: %w", err) - } - if len(albums) == 0 { - break - } - for _, a := range albums { - if ctx.Err() != nil { - return ctx.Err() - } - if s.now().After(deadline) { - return nil // the rest next pass - } - n, err := s.listAlbum(ctx, q, a) - if errors.Is(err, errMusicBrainzAway) { - s.logger.Info("wanted: matching the library's albums paused", "err", err) - return nil - } - if err != nil { - return err - } - res.Listed += n - } - } - return s.finishLibrary(ctx, q, res) -} - -// listAlbum matches one album and lists it, or records why it could not. -func (s *Service) listAlbum(ctx context.Context, q *dbq.Queries, a dbq.ListAlbumsToMatchRow) (int, error) { - rows, err := q.ListAlbumTracksToMatch(ctx, a.ID) - if err != nil { - return 0, fmt.Errorf("wanted: read %s's tracks: %w", a.Title, err) - } - m, err := s.matchAlbum(ctx, a, tracksToMatch(rows)) - if err != nil { - return 0, err - } - if !m.OK { - s.logger.Info("wanted: no MusicBrainz match for a library album", "album", a.Title, "artist", a.ArtistName, "why", m.Detail) - return 0, q.RecordAlbumMatch(ctx, dbq.RecordAlbumMatchParams{ - AlbumID: a.ID, Outcome: "unmatched", Detail: m.Detail, Matcher: libraryMatcher}) - } - var listed int64 - err = pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { - tq := dbq.New(tx) - if err := tq.SetAlbumReleaseGroup(ctx, dbq.SetAlbumReleaseGroupParams{ID: a.ID, ReleaseGroupMbid: &m.Group}); err != nil { - return err - } - n, err := tq.InsertLibraryWant(ctx, dbq.InsertLibraryWantParams{ - ReleaseGroupMbid: m.Group, ArtistMbid: m.ArtistMBID, ArtistName: m.ArtistName, Title: m.Title, - PrimaryType: m.PrimaryType, FirstReleaseDate: m.FirstReleaseDate, AlbumID: a.ID, - }) - if err != nil { - return err - } - listed = n - return tq.RecordAlbumMatch(ctx, dbq.RecordAlbumMatchParams{ - AlbumID: a.ID, Outcome: "listed", Method: m.Method, ReleaseGroupMbid: m.Group, Matcher: libraryMatcher, - }) - }) - if err != nil { - return 0, fmt.Errorf("wanted: list album %s: %w", a.Title, err) - } - return int(listed), nil -} - -func (s *Service) finishLibrary(ctx context.Context, q *dbq.Queries, res *PassResult) error { - done, err := q.FinishLibraryListing(ctx) - if err != nil { - return fmt.Errorf("wanted: finish the library listing: %w", err) - } - s.logger.Info("wanted: the library's albums are on the list", "listed_this_pass", res.Listed, - "unmatched", done.LibraryUnlisted) - return nil -} - -func deref(p *string) string { - if p == nil { - return "" - } - return *p -} diff --git a/internal/wanted/library_test.go b/internal/wanted/library_test.go deleted file mode 100644 index b153497e..00000000 --- a/internal/wanted/library_test.go +++ /dev/null @@ -1,321 +0,0 @@ -package wanted - -import ( - "context" - "fmt" - "strings" - "testing" - "time" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/tags" -) - -// startListing clears the reset's "already listed", as a fresh install has it. -func startListing(t *testing.T, pool *pgxpool.Pool) { - t.Helper() - if _, err := pool.Exec(context.Background(), ` - UPDATE acquisition_settings SET library_listing_started_at = NULL, library_listed_at = NULL, library_unlisted = 0`); err != nil { - t.Fatal(err) - } -} - -// seedAlbum puts an album in the library whose tracks are titled titles, -// numbered by numbers (from 1 when none are given). An artistMBID or -// release of "" leaves that id out, as untagged files do. -func seedAlbum(t *testing.T, q *dbq.Queries, artistMBID, title, rel string, titles []string, numbers ...int32) dbq.Album { - t.Helper() - ctx := context.Background() - ap := dbq.UpsertArtistParams{Name: "Blur", SortName: "Blur"} - if artistMBID != "" { - ap.Mbid = &artistMBID - } - artist, err := q.UpsertArtist(ctx, ap) - if err != nil { - t.Fatal(err) - } - params := dbq.UpsertAlbumParams{Title: title, SortTitle: title, ArtistID: artist.ID} - if rel != "" { - params.Mbid = &rel - } - album, err := q.UpsertAlbum(ctx, params) - if err != nil { - t.Fatal(err) - } - for i, name := range titles { - num := int32(i + 1) - if i < len(numbers) { - num = numbers[i] - } - if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ - Title: name, AlbumID: album.ID, ArtistID: artist.ID, TrackNumber: &num, DurationMs: 180000, - FilePath: fmt.Sprintf("/music/%s/%02d.flac", strings.ReplaceAll(title, " ", "_"), num), FileSize: 1, FileFormat: "flac", - }); err != nil { - t.Fatal(err) - } - } - return album -} - -func matchOf(t *testing.T, pool *pgxpool.Pool, album pgtype.UUID) (outcome, method, group, detail string) { - t.Helper() - if err := pool.QueryRow(context.Background(), ` - SELECT outcome, method, release_group_mbid, detail FROM library_album_matches WHERE album_id = $1`, album, - ).Scan(&outcome, &method, &group, &detail); err != nil { - t.Fatalf("no match recorded: %v", err) - } - return -} - -// The library's albums go on the list as held, once. Tags that name -// the group list it at once; a release id is named through MusicBrainz; -// what nothing matches is recorded with why. Taken off the list, an album -// stays off, and one added to the library afterwards is never listed. -func TestListLibrary_OnceAndThenTheListIsTheTruth_Integration(t *testing.T) { - pool := newPool(t) - ctx := context.Background() - q := dbq.New(pool) - s := newService(t, pool, newFakeMB(t)) - s.releaseGroup = func(_ context.Context, release string) (string, error) { - if release == "rel-ballad" { - return theBallad, nil - } - return "", tags.ErrNotFound - } - blurAlbum := seedHeld(t, q, "Blur", "rel-blur", "rg-blur", 3) - ballad := seedAlbum(t, q, blur, "The Ballad of Darren", "rel-ballad", []string{"The Ballad", "St. Charles Square"}) - bootleg := seedAlbum(t, q, blur, "Bootleg", "rel-bootleg", []string{"Intro"}) - // An album with a track gone: held from before the loss, so - // re-acquisition still fetches it back. - lost := seedHeld(t, q, "13", "rel-13", "rg-13", 2) - if _, err := pool.Exec(ctx, `UPDATE tracks SET missing_since = now() - interval '1 day' - WHERE album_id = $1 AND track_number = 1`, lost.ID); err != nil { - t.Fatal(err) - } - // An album row with no tracks is not an album in the library. - if _, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Empty", SortTitle: "Empty", ArtistID: blurAlbum.ArtistID}); err != nil { - t.Fatal(err) - } - startListing(t, pool) - - if err := s.listLibrary(ctx, &PassResult{}); err != nil { - t.Fatal(err) - } - for group, album := range map[string]pgtype.UUID{"rg-blur": blurAlbum.ID, theBallad: ballad.ID, "rg-13": lost.ID} { - w, err := q.GetWantByReleaseGroup(ctx, group) - if err != nil { - t.Fatalf("%s not listed: %v", group, err) - } - if w.Reason != ReasonLibrary || !w.FulfilledAt.Valid || w.AlbumID != album || w.ArtistMbid != blur { - t.Errorf("%s = %+v: a held library want about the album", group, w) - } - } - if w, _ := q.GetWantByReleaseGroup(ctx, "rg-13"); !w.FulfilledAt.Time.Before(time.Now().Add(-23 * time.Hour)) { - t.Errorf("an album missing a track is held from %v, not from before the loss", w.FulfilledAt.Time) - } - if outcome, method, _, _ := matchOf(t, pool, ballad.ID); outcome != "listed" || method != matchRelease { - t.Errorf("ballad decided %s by %s", outcome, method) - } - if outcome, _, _, detail := matchOf(t, pool, bootleg.ID); outcome != "unmatched" || !strings.Contains(detail, `no album called "Bootleg"`) { - t.Errorf("bootleg decided %s: %q", outcome, detail) - } - state, err := q.GetLibraryListing(ctx) - if err != nil || !state.LibraryListedAt.Valid || state.LibraryUnlisted != 1 { - t.Fatalf("listing = %+v %v: done, with only the bootleg unmatched", state, err) - } - - // Off the list stays off; an album added later is never listed. - w, _ := q.GetWantByReleaseGroup(ctx, "rg-blur") - if _, err := s.RemoveWant(ctx, w.ID); err != nil { - t.Fatal(err) - } - _ = seedHeld(t, q, "Think Tank", "rel-think", "rg-think", 2) - if _, err := s.Pass(ctx); err != nil { - t.Fatal(err) - } - for _, group := range []string{"rg-blur", "rg-think"} { - if _, err := q.GetWantByReleaseGroup(ctx, group); err == nil { - t.Errorf("%s is on the list", group) - } - } -} - -// An album whose tags name nothing is matched by its title among the -// artist's release groups, and taken only when its tracks agree: a partial -// copy agrees on the tracks it has. One whose title matches but whose -// tracks do not is left unmatched, with why. -func TestListLibrary_MatchesByDiscographyWhenTheTracksAgree_Integration(t *testing.T) { - pool := newPool(t) - ctx := context.Background() - q := dbq.New(pool) - s := newService(t, pool, newFakeMB(t)) - // MusicBrainz switched off for tags does not stop matching by name. - s.releaseGroup = func(context.Context, string) (string, error) { return "", tags.ErrSwitchedOff } - partial := seedAlbum(t, q, blur, "The Ballad of Darren (Deluxe)", "rel-ballad", []string{"St Charles Square"}, 2) - wrong := seedAlbum(t, q, blur, "Parklife", "", []string{"Girls & Boys", "Tracy Jacks"}) - startListing(t, pool) - - if err := s.listLibrary(ctx, &PassResult{}); err != nil { - t.Fatal(err) - } - if outcome, method, group, _ := matchOf(t, pool, partial.ID); outcome != "listed" || method != matchDiscography || group != theBallad { - t.Errorf("partial copy decided %s by %s as %s", outcome, method, group) - } - var named *string - if err := pool.QueryRow(ctx, `SELECT release_group_mbid FROM albums WHERE id = $1`, partial.ID).Scan(&named); err != nil || named == nil || *named != theBallad { - t.Errorf("the album's release group = %v %v", named, err) - } - if outcome, _, _, detail := matchOf(t, pool, wrong.ID); outcome != "unmatched" || !strings.Contains(detail, "none has its tracks") { - t.Errorf("Parklife without Parklife's tracks decided %s: %q", outcome, detail) - } - if _, err := q.GetWantByReleaseGroup(ctx, parklife); err == nil { - t.Error("Parklife listed on its title alone") - } -} - -// An artist the tags give no id is found by MusicBrainz's search on artist -// and title, and the want is filed under the artist MusicBrainz credits. -func TestListLibrary_MatchesBySearchWhenTheArtistHasNoID_Integration(t *testing.T) { - pool := newPool(t) - ctx := context.Background() - q := dbq.New(pool) - s := newService(t, pool, newFakeMB(t)) - album := seedAlbum(t, q, "", "The Ballad of Darren", "", []string{"The Ballad", "St. Charles Square"}) - startListing(t, pool) - - if err := s.listLibrary(ctx, &PassResult{}); err != nil { - t.Fatal(err) - } - if outcome, method, _, _ := matchOf(t, pool, album.ID); outcome != "listed" || method != matchSearch { - t.Errorf("decided %s by %s", outcome, method) - } - w, err := q.GetWantByReleaseGroup(ctx, theBallad) - if err != nil || w.ArtistMbid != blur || w.Reason != ReasonLibrary || !w.FulfilledAt.Valid { - t.Errorf("want = %+v %v", w, err) - } -} - -// A MusicBrainz that does not answer pauses the matching; the album is not -// decided on half the evidence, and the next pass goes on. -func TestListLibrary_UnreachablePausesTheMatching_Integration(t *testing.T) { - pool := newPool(t) - ctx := context.Background() - q := dbq.New(pool) - f := newFakeMB(t) - s := newService(t, pool, f) - album := seedAlbum(t, q, blur, "The Ballad of Darren", "", []string{"The Ballad", "St. Charles Square"}) - startListing(t, pool) - - f.down.Store(true) - if err := s.listLibrary(ctx, &PassResult{}); err != nil { - t.Fatal(err) - } - if state, _ := q.GetLibraryListing(ctx); state.LibraryListedAt.Valid { - t.Fatal("finished while an album was still to be matched") - } - var n int - if err := pool.QueryRow(ctx, `SELECT count(*) FROM library_album_matches WHERE album_id = $1`, album.ID).Scan(&n); err != nil || n != 0 { - t.Fatalf("decided while MusicBrainz was away: %d %v", n, err) - } - f.down.Store(false) - if err := s.listLibrary(ctx, &PassResult{}); err != nil { - t.Fatal(err) - } - if _, err := q.GetWantByReleaseGroup(ctx, theBallad); err != nil { - t.Errorf("not listed once MusicBrainz answered: %v", err) - } - if state, _ := q.GetLibraryListing(ctx); !state.LibraryListedAt.Valid { - t.Error("not finished") - } -} - -func TestTrackAgreement(t *testing.T) { - edition := []editionTrack{ - {Medium: 1, Position: 1, Title: "Intro", LengthMs: 60000, Recording: "rec-1"}, - {Medium: 1, Position: 2, Title: "Song", LengthMs: 200000}, - {Medium: 2, Position: 1, Title: "Encore", LengthMs: 300000}, - } - for _, c := range []struct { - name string - tracks []libraryTrack - want int - }{ - {"all, by position", []libraryTrack{ - {Disc: 1, Number: 1, Title: "Intro", LengthMs: 60000}, {Number: 2, Title: "Song", LengthMs: 201000}, - {Disc: 2, Number: 1, Title: "Encore", LengthMs: 300000}}, 3}, - {"a partial copy", []libraryTrack{{Disc: 2, Number: 1, Title: "Encore", LengthMs: 300500}}, 1}, - {"unnumbered, by title", []libraryTrack{{Title: "Song", LengthMs: 200000}, {Title: "Encore", LengthMs: 299000}}, 2}, - {"the recording, whatever its title", []libraryTrack{{Number: 1, Title: "Untitled", LengthMs: 1, Recording: "REC-1"}}, 1}, - {"the right title at the wrong length", []libraryTrack{{Number: 2, Title: "Song", LengthMs: 400000}}, 0}, - {"a track counted once", []libraryTrack{{Title: "Song", LengthMs: 200000}, {Title: "Song", LengthMs: 200000}}, 1}, - } { - if got, _ := trackAgreement(c.tracks, edition, false); got != c.want { - t.Errorf("%s: %d agree, want %d", c.name, got, c.want) - } - } - // Under the album's own name the titles are enough; the reason a track - // did not agree names both sides. - edit := []libraryTrack{{Number: 2, Title: "Song (Radio Edit)", LengthMs: 150000}} - if n, miss := trackAgreement(edit, edition, false); n != 0 || miss != `"Song (Radio Edit)" runs 2:30 here; MusicBrainz's track 2 is "Song", 3:20` { - t.Errorf("by length: %d, %q", n, miss) - } - if n, _ := trackAgreement(edit, edition, true); n != 1 { - t.Errorf("by title alone: %d agree", n) - } - if _, miss := trackAgreement([]libraryTrack{{Title: "Other", LengthMs: 61000}}, edition, true); miss != `no track of its edition is like "Other" (1:01)` { - t.Errorf("unplaced: %q", miss) - } - if !sameTitle("Brain Stew + Jaded", "Brain Stew / Jaded") || sameTitle("+++", "Grind & Hustle") || sameTitle("Blur", "The Best of Blur") { - t.Error("sameTitle: joining words count for nothing, and a title of none is no title") - } - n := int32(1) - copies := tracksToMatch([]dbq.ListAlbumTracksToMatchRow{ - {Title: "Song", TrackNumber: &n, DurationMs: 1}, {Title: "song", TrackNumber: &n, DurationMs: 2}, {Title: "Other", TrackNumber: &n}}) - if len(copies) != 2 { - t.Errorf("a second copy of a track is read once: %+v", copies) - } - if agrees(3, 4, 0) || !agrees(4, 5, 0) || agrees(0, 0, 0) { - t.Error("agrees: 80% of the album's tracks, and an album has tracks") - } - if !agrees(23, 43, 23) || agrees(2, 10, 2) || agrees(22, 43, 23) { - t.Error("agrees: a whole edition of three or more tracks, with more besides") - } -} - -// A refusal by an older matcher is matched again once the matching improves, -// even after the listing is done; a refusal by this one is not. -func TestListLibrary_RetriesAnOlderMatchersRefusals_Integration(t *testing.T) { - pool := newPool(t) - ctx := context.Background() - q := dbq.New(pool) - s := newService(t, pool, newFakeMB(t)) - ballad := seedAlbum(t, q, blur, "The Ballad of Darren", "", []string{"The Ballad", "St. Charles Square"}) - bootleg := seedAlbum(t, q, blur, "Bootleg", "", []string{"Intro"}) - for _, a := range []pgtype.UUID{ballad.ID, bootleg.ID} { - if err := q.RecordAlbumMatch(ctx, dbq.RecordAlbumMatchParams{AlbumID: a, Outcome: "unmatched", Detail: "old", Matcher: 1}); err != nil { - t.Fatal(err) - } - } - if _, err := pool.Exec(ctx, `UPDATE library_album_matches SET matcher = $1 WHERE album_id = $2`, libraryMatcher, bootleg.ID); err != nil { - t.Fatal(err) - } - // A listing done, with these albums from before its cutoff. - if _, err := pool.Exec(ctx, `UPDATE acquisition_settings SET library_listing_started_at = now(), library_listed_at = now()`); err != nil { - t.Fatal(err) - } - if err := s.listLibrary(ctx, &PassResult{}); err != nil { - t.Fatal(err) - } - if outcome, method, _, _ := matchOf(t, pool, ballad.ID); outcome != "listed" || method != matchDiscography { - t.Errorf("the older refusal was decided %s by %s", outcome, method) - } - if _, _, _, detail := matchOf(t, pool, bootleg.ID); detail != "old" { - t.Errorf("this matcher's own refusal was retried: %q", detail) - } - if state, _ := q.GetLibraryListing(ctx); state.LibraryUnlisted != 1 { - t.Errorf("unlisted = %d, want the bootleg alone", state.LibraryUnlisted) - } -} diff --git a/internal/wanted/manual.go b/internal/wanted/manual.go deleted file mode 100644 index a58e8d45..00000000 --- a/internal/wanted/manual.go +++ /dev/null @@ -1,170 +0,0 @@ -package wanted - -import ( - "context" - "errors" - "fmt" - "regexp" - "strings" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" - "git.fabledsword.com/bvandeusen/minstrel/internal/tags" -) - -// Matching a library album by hand: the Collection's Unmatched section. A -// person names the MusicBrainz album an album is, by a link or a search, and -// it goes on the list as held; or leaves it off for good, as a local band's -// album MusicBrainz will never have. - -const matchManual = "manual" - -// Manual-match errors, which the API answers as 400, 404 or 409. -var ( - ErrMatchRef = errors.New("wanted: not a MusicBrainz album link or id") - ErrMatchNoGroup = errors.New("wanted: MusicBrainz has no such album") - ErrAlbumNotUnmatched = errors.New("wanted: no such unmatched album") -) - -// ManualMatch is what matching an album by hand did: the group it is now, -// and how many of its tracks agree with the group's closest edition, so the -// person can see whether the link was the right one. -type ManualMatch struct { - Group string - Title string - Artist string - Agree int - Tracks int -} - -var mbidPattern = regexp.MustCompile(`(?i)(?:(release-group|release)/)?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})`) - -// ParseMusicBrainzRef reads a MusicBrainz link (a release group's or a -// release's page) or a bare id. Kind is "release-group", "release", or "" -// for a bare id, which is tried as a group first. -func ParseMusicBrainzRef(ref string) (kind, id string, ok bool) { - m := mbidPattern.FindStringSubmatch(strings.TrimSpace(ref)) - if m == nil { - return "", "", false - } - return strings.ToLower(m[1]), strings.ToLower(m[2]), true -} - -// MatchAlbumTo puts an unmatched (or left-off) library album on the list as -// held, as the MusicBrainz album ref names. The person's word is taken: the -// tracks are compared only to say how well they agree. -func (s *Service) MatchAlbumTo(ctx context.Context, albumID pgtype.UUID, ref string) (ManualMatch, error) { - kind, id, ok := ParseMusicBrainzRef(ref) - if !ok { - return ManualMatch{}, ErrMatchRef - } - q := dbq.New(s.pool) - a, err := q.GetAlbumToMatch(ctx, albumID) - if errors.Is(err, pgx.ErrNoRows) { - return ManualMatch{}, ErrAlbumNotUnmatched - } - if err != nil { - return ManualMatch{}, fmt.Errorf("wanted: read album: %w", err) - } - group, err := s.groupOf(ctx, kind, id) - if err != nil { - return ManualMatch{}, err - } - g, err := s.cat.MusicBrainz().ReleaseGroup(ctx, group) - if errors.Is(err, musicbrainz.ErrNotFound) { - return ManualMatch{}, ErrMatchNoGroup - } - if err != nil { - return ManualMatch{}, err - } - artist := creditedArtist(g.ArtistMBIDs, deref(a.ArtistMbid)) - if artist == "" { - return ManualMatch{}, fmt.Errorf("%w: it credits no artist", ErrMatchNoGroup) - } - rows, err := q.ListAlbumTracksToMatch(ctx, a.ID) - if err != nil { - return ManualMatch{}, fmt.Errorf("wanted: read %s's tracks: %w", a.Title, err) - } - tracks := tracksToMatch(rows) - n, _, _, err := s.agreementWith(ctx, group, tracks, func(string) bool { return true }) - if err != nil { - return ManualMatch{}, err - } - err = pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { - tq := dbq.New(tx) - if err := tq.SetAlbumReleaseGroup(ctx, dbq.SetAlbumReleaseGroupParams{ID: a.ID, ReleaseGroupMbid: &group}); err != nil { - return err - } - if _, err := tq.InsertLibraryWant(ctx, dbq.InsertLibraryWantParams{ - ReleaseGroupMbid: group, ArtistMbid: artist, ArtistName: g.ArtistCredit, Title: g.Title, - PrimaryType: g.PrimaryType, FirstReleaseDate: g.FirstReleaseDate, AlbumID: a.ID, - }); err != nil { - return err - } - if err := tq.RecordAlbumMatch(ctx, dbq.RecordAlbumMatchParams{ - AlbumID: a.ID, Outcome: "listed", Method: matchManual, ReleaseGroupMbid: group, Matcher: libraryMatcher, - }); err != nil { - return err - } - return tq.RecountLibraryUnlisted(ctx) - }) - if err != nil { - return ManualMatch{}, fmt.Errorf("wanted: match %s by hand: %w", a.Title, err) - } - s.logger.Info("wanted: a library album matched by hand", "album", a.Title, "group", group, "agree", max(n, 0), "tracks", len(tracks)) - return ManualMatch{Group: group, Title: g.Title, Artist: g.ArtistCredit, Agree: max(n, 0), Tracks: len(tracks)}, nil -} - -// groupOf names the release group a MusicBrainz id is, or belongs to. -func (s *Service) groupOf(ctx context.Context, kind, id string) (string, error) { - if kind != "release" { - _, err := s.cat.MusicBrainz().ReleaseGroup(ctx, id) - if err == nil { - return id, nil - } - if !errors.Is(err, musicbrainz.ErrNotFound) { - return "", err - } - if kind == "release-group" { - return "", ErrMatchNoGroup - } - } - group, err := s.releaseGroup(ctx, id) - switch { - case errors.Is(err, tags.ErrNotFound): - return "", ErrMatchNoGroup - case err != nil: - return "", err - case group == "": - return "", ErrMatchNoGroup - } - return group, nil -} - -// LeaveAlbumOff leaves an unmatched library album off the list for good: -// matching never tries it again, and it is no longer counted as unmatched. -func (s *Service) LeaveAlbumOff(ctx context.Context, albumID pgtype.UUID) error { - return s.setMatchOutcome(ctx, albumID, "unmatched", "dismissed") -} - -// PutAlbumBack undoes LeaveAlbumOff: the album is unmatched again. -func (s *Service) PutAlbumBack(ctx context.Context, albumID pgtype.UUID) error { - return s.setMatchOutcome(ctx, albumID, "dismissed", "unmatched") -} - -func (s *Service) setMatchOutcome(ctx context.Context, albumID pgtype.UUID, was, outcome string) error { - return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { - tq := dbq.New(tx) - n, err := tq.SetAlbumMatchOutcome(ctx, dbq.SetAlbumMatchOutcomeParams{AlbumID: albumID, Was: was, Outcome: outcome}) - if err != nil { - return err - } - if n == 0 { - return ErrAlbumNotUnmatched - } - return tq.RecountLibraryUnlisted(ctx) - }) -} diff --git a/internal/wanted/manual_test.go b/internal/wanted/manual_test.go deleted file mode 100644 index c76fe0d1..00000000 --- a/internal/wanted/manual_test.go +++ /dev/null @@ -1,166 +0,0 @@ -package wanted - -import ( - "context" - "errors" - "testing" - - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/tags" -) - -func TestParseMusicBrainzRef(t *testing.T) { - const id = "11111111-0000-4000-8000-000000000003" - for ref, want := range map[string]string{ - "https://musicbrainz.org/release-group/" + id: "release-group " + id, - "https://musicbrainz.org/release/" + id + "?tab=x": "release " + id, - " " + id + " ": " " + id, - "https://MusicBrainz.org/Release-Group/" + "11111111-0000-4000-8000-00000000000A": "release-group 11111111-0000-4000-8000-00000000000a", - } { - kind, got, ok := ParseMusicBrainzRef(ref) - if !ok || kind+" "+got != want { - t.Errorf("ParseMusicBrainzRef(%q) = %q %q %v, want %q", ref, kind, got, ok, want) - } - } - for _, bad := range []string{"", "Dial Tone", "https://musicbrainz.org/artist/nope"} { - if _, _, ok := ParseMusicBrainzRef(bad); ok { - t.Errorf("ParseMusicBrainzRef(%q) read an id", bad) - } - } -} - -// A person names the MusicBrainz album an unmatched album is, and it goes -// on the list as held; or leaves it off, and matching never tries it again. -func TestMatchAlbumTo_ByLinkOrLeaveItOff_Integration(t *testing.T) { - pool := newPool(t) - ctx := context.Background() - q := dbq.New(pool) - s := newService(t, pool, newFakeMB(t)) - s.releaseGroup = func(context.Context, string) (string, error) { return "", tags.ErrNotFound } - split := seedAlbum(t, q, blur, "Blur + Friends", "", []string{"The Ballad", "St. Charles Square"}) - local := seedAlbum(t, q, blur, "Dial Tone", "", []string{"Intro"}) - for _, a := range []pgtype.UUID{split.ID, local.ID} { - if err := q.RecordAlbumMatch(ctx, dbq.RecordAlbumMatchParams{ - AlbumID: a, Outcome: "unmatched", Detail: "no match", Matcher: libraryMatcher}); err != nil { - t.Fatal(err) - } - } - if _, err := pool.Exec(ctx, `UPDATE acquisition_settings - SET library_listing_started_at = now(), library_listed_at = now(), library_unlisted = 2`); err != nil { - t.Fatal(err) - } - - for ref, want := range map[string]error{ - "not a link": ErrMatchRef, - "https://musicbrainz.org/release-group/99999999-0000-4000-8000-000000000000": ErrMatchNoGroup, - "99999999-0000-4000-8000-000000000000": ErrMatchNoGroup, - } { - if _, err := s.MatchAlbumTo(ctx, split.ID, ref); !errors.Is(err, want) { - t.Errorf("MatchAlbumTo(%q) = %v, want %v", ref, err, want) - } - } - - m, err := s.MatchAlbumTo(ctx, split.ID, "https://musicbrainz.org/release-group/"+theBallad) - if err != nil { - t.Fatal(err) - } - if m.Group != theBallad || m.Title != "The Ballad of Darren" || m.Agree != 2 || m.Tracks != 2 { - t.Errorf("match = %+v", m) - } - w, err := q.GetWantByReleaseGroup(ctx, theBallad) - if err != nil || w.Reason != ReasonLibrary || !w.FulfilledAt.Valid || w.AlbumID != split.ID || w.ArtistMbid != blur { - t.Fatalf("want = %+v, %v: held, about the album", w, err) - } - if outcome, method, group, _ := matchOf(t, pool, split.ID); outcome != "listed" || method != matchManual || group != theBallad { - t.Errorf("decided %s by %s as %s", outcome, method, group) - } - if state, _ := q.GetLibraryListing(ctx); state.LibraryUnlisted != 1 { - t.Errorf("unlisted = %d, want the local album alone", state.LibraryUnlisted) - } - - // The local band's album: left off, it is not counted, and a newer - // matcher does not try it again. - if err := s.LeaveAlbumOff(ctx, local.ID); err != nil { - t.Fatal(err) - } - if outcome, _, _, _ := matchOf(t, pool, local.ID); outcome != "dismissed" { - t.Errorf("left off = %s", outcome) - } - if state, _ := q.GetLibraryListing(ctx); state.LibraryUnlisted != 0 { - t.Errorf("unlisted = %d after leaving it off", state.LibraryUnlisted) - } - if _, err := pool.Exec(ctx, `UPDATE library_album_matches SET matcher = 0 WHERE album_id = $1`, local.ID); err != nil { - t.Fatal(err) - } - if err := s.listLibrary(ctx, &PassResult{}); err != nil { - t.Fatal(err) - } - if outcome, _, _, _ := matchOf(t, pool, local.ID); outcome != "dismissed" { - t.Errorf("an album left off was matched again: %s", outcome) - } - rows, err := q.ListUnmatchedLibraryAlbums(ctx, 10) - if err != nil || len(rows) != 1 || rows[0].Outcome != "dismissed" || rows[0].Tracks != 1 { - t.Errorf("unmatched section = %+v, %v: the left-off album alone", rows, err) - } - - // Put back, it is unmatched again; putting it back twice is not. - if err := s.PutAlbumBack(ctx, local.ID); err != nil { - t.Fatal(err) - } - if outcome, _, _, _ := matchOf(t, pool, local.ID); outcome != "unmatched" { - t.Errorf("put back = %s", outcome) - } - if err := s.PutAlbumBack(ctx, local.ID); !errors.Is(err, ErrAlbumNotUnmatched) { - t.Errorf("put back twice: %v", err) - } -} - -// A re-tag files a library track under a new album row. That album is the -// library's, not one dropped in by hand, so the listing matches it; the -// old row, its tracks gone, is no longer unmatched. -func TestListLibrary_MatchesARetaggedAlbum_Integration(t *testing.T) { - pool := newPool(t) - ctx := context.Background() - q := dbq.New(pool) - s := newService(t, pool, newFakeMB(t)) - untagged := seedAlbum(t, q, blur, "Unknown Album", "", []string{"The Ballad", "St. Charles Square"}) - if err := q.RecordAlbumMatch(ctx, dbq.RecordAlbumMatchParams{ - AlbumID: untagged.ID, Outcome: "unmatched", Detail: "no match", Matcher: libraryMatcher}); err != nil { - t.Fatal(err) - } - for _, stmt := range []string{ - `UPDATE albums SET created_at = now() - interval '2 hours' WHERE id = $1`, - `UPDATE tracks SET added_at = now() - interval '2 hours' WHERE album_id = $1`, - } { - if _, err := pool.Exec(ctx, stmt, untagged.ID); err != nil { - t.Fatal(err) - } - } - if _, err := pool.Exec(ctx, `UPDATE acquisition_settings SET library_listing_started_at = now() - interval '1 hour', - library_listed_at = now() - interval '1 hour', library_unlisted = 1`); err != nil { - t.Fatal(err) - } - // The re-tag: the same tracks, under the album their new tags name. - retagged, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "The Ballad of Darren", SortTitle: "The Ballad of Darren", ArtistID: untagged.ArtistID}) - if err != nil { - t.Fatal(err) - } - if _, err := pool.Exec(ctx, `UPDATE tracks SET album_id = $1, updated_at = now() WHERE album_id = $2`, retagged.ID, untagged.ID); err != nil { - t.Fatal(err) - } - - if err := s.listLibrary(ctx, &PassResult{}); err != nil { - t.Fatal(err) - } - if outcome, method, group, _ := matchOf(t, pool, retagged.ID); outcome != "listed" || method != matchDiscography || group != theBallad { - t.Errorf("the re-tagged album was decided %s by %s as %s", outcome, method, group) - } - if rows, _ := q.ListUnmatchedLibraryAlbums(ctx, 10); len(rows) != 0 { - t.Errorf("unmatched = %+v: the old row has no tracks left", rows) - } - if state, _ := q.GetLibraryListing(ctx); state.LibraryUnlisted != 0 { - t.Errorf("unlisted = %d", state.LibraryUnlisted) - } -} diff --git a/internal/wanted/match.go b/internal/wanted/match.go deleted file mode 100644 index c1fe3fa8..00000000 --- a/internal/wanted/match.go +++ /dev/null @@ -1,404 +0,0 @@ -package wanted - -import ( - "context" - "errors" - "fmt" - "slices" - "strings" - "time" - - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" - "git.fabledsword.com/bvandeusen/minstrel/internal/tags" - "git.fabledsword.com/bvandeusen/minstrel/internal/words" -) - -// Matching a library album to its MusicBrainz release group. -// -// Music from any mainstream source was matched against MusicBrainz on its way -// in, so nearly every album has a group. The ids its tags carry are taken as -// they are. Without them, the groups called what the album is called, from the -// artist's discography and then from a search, are checked against the album's -// own tracks, and the one its tracks agree with is taken. - -// Match methods, as library_album_matches records them. -const ( - matchTags = "tags" - matchRelease = "release" - matchDiscography = "discography" - matchSearch = "search" -) - -// matchAgreement is the share of an album's tracks, in tenths, an edition -// must agree with: most, not all, since a rip's track may carry a typo or a -// hidden track's length. -const matchAgreement = 8 - -// matchCandidates bounds how many groups one album is checked against: each -// costs a MusicBrainz read the first time. -const matchCandidates = 8 - -// matchDiscographyAge is how old a held discography may be and still be -// read for matching: a name does not change, and a new release is not in -// the library. -const matchDiscographyAge = 90 * 24 * time.Hour - -// errMusicBrainzAway: MusicBrainz did not answer, so the album is left for -// the next pass rather than decided on half the evidence. -var errMusicBrainzAway = errors.New("wanted: MusicBrainz did not answer") - -// libraryTrack is what one of an album's tracks says about itself. -type libraryTrack struct { - Disc, Number int - Title string - LengthMs int - Recording string -} - -// editionTrack is one track of a MusicBrainz edition. -type editionTrack struct { - Medium, Position int - Title string - LengthMs int - Recording string -} - -// candidate is a release group an album may be. -type candidate struct { - Group string - Title string - PrimaryType string - FirstReleaseDate string - ArtistMBID string - ArtistName string - Method string -} - -// albumMatch is what matching decided: a group, or why there is none. -type albumMatch struct { - candidate - OK bool - Detail string -} - -// tracksToMatch reads an album's tracks, each once: a second copy of a -// track (the same disc, number and title, as a duplicate file in another -// format is) would otherwise count against the match as a track no edition -// has. -func tracksToMatch(rows []dbq.ListAlbumTracksToMatchRow) []libraryTrack { - out := make([]libraryTrack, 0, len(rows)) - seen := make(map[string]bool, len(rows)) - for _, r := range rows { - t := libraryTrack{Title: r.Title, LengthMs: int(r.DurationMs), Recording: deref(r.Mbid)} - if r.DiscNumber != nil { - t.Disc = int(*r.DiscNumber) - } - if r.TrackNumber != nil { - t.Number = int(*r.TrackNumber) - } - key := fmt.Sprintf("%d/%d/%s", max(t.Disc, 1), t.Number, strings.Join(release.Tokens(t.Title), " ")) - if seen[key] { - continue - } - seen[key] = true - out = append(out, t) - } - return out -} - -// trackAgreement counts the album's tracks an edition agrees with: the same -// recording, or a title that agrees at a length that fits. When the group is -// called just what the album is called, the titles alone are enough: the -// lengths are there to tell apart albums whose names only overlap, and a -// rip's single may be the edit MusicBrainz lists at another length. A track -// is paired by its disc and number first, then by title, so an album missing -// some tracks still pairs the ones it has. miss says how the first track -// that did not agree differs, for the reason a match was refused. -func trackAgreement(tracks []libraryTrack, edition []editionTrack, titlesEnough bool) (n int, miss string) { - byPos := make(map[[2]int]int, len(edition)) - for i, e := range edition { - byPos[[2]int{max(e.Medium, 1), e.Position}] = i - } - agree := func(t libraryTrack, e editionTrack) bool { - if t.Recording != "" && strings.EqualFold(t.Recording, e.Recording) { - return true - } - return release.TitleAgrees(t.Title, e.Title) && (titlesEnough || release.LengthFits(t.LengthMs, e.LengthMs)) - } - used := make([]bool, len(edition)) - for _, t := range tracks { - at, placed := -1, false - if t.Number > 0 { - if i, ok := byPos[[2]int{max(t.Disc, 1), t.Number}]; ok { - at = i - placed = !used[i] && agree(t, edition[i]) - if placed { - used[i] = true - } - } - } - for i, e := range edition { - if !placed && !used[i] && agree(t, e) { - used[i], placed = true, true - } - } - switch { - case placed: - n++ - case miss != "": - case at >= 0: - e := edition[at] - miss = fmt.Sprintf("%q runs %s here; MusicBrainz's track %d is %q, %s", - t.Title, minutes(t.LengthMs), e.Position, e.Title, minutes(e.LengthMs)) - default: - miss = fmt.Sprintf("no track of its edition is like %q (%s)", t.Title, minutes(t.LengthMs)) - } - } - return n, miss -} - -// minutes writes a length as m:ss, or "no length". -func minutes(ms int) string { - if ms <= 0 { - return "no length" - } - sec := (ms + 500) / 1000 - return fmt.Sprintf("%d:%02d", sec/60, sec%60) -} - -// agrees: enough of the album's tracks agree for the edition to be it, or -// the album holds every track of the edition and more besides (a folder -// with the deluxe edition and its demos). A single or a two-track release -// is not taken on containment: too many albums hold its songs. -func agrees(n, tracks, edition int) bool { - return tracks > 0 && (n*10 >= tracks*matchAgreement || (edition >= 3 && n == edition)) -} - -// sameTitle: the same words, not merely one holding the other. "And", -// "&" and "+" join two titles however a release spells it ("Brain Stew + -// Jaded", "Brain Stew / Jaded"), so they count for nothing. -func sameTitle(a, b string) bool { - words := func(s string) []string { - return slices.DeleteFunc(release.Tokens(s), func(w string) bool { return w == "and" }) - } - wa, wb := words(a), words(b) - return len(wa) > 0 && slices.Equal(wa, wb) -} - -// matchAlbum finds the release group a library album is. A MusicBrainz -// that does not answer is errMusicBrainzAway; anything else MusicBrainz -// says is evidence, and an album with none is an albumMatch that is not OK. -func (s *Service) matchAlbum(ctx context.Context, a dbq.ListAlbumsToMatchRow, tracks []libraryTrack) (albumMatch, error) { - // The ids the tags carry name it. - if group := deref(a.ReleaseGroupMbid); group != "" { - if c, err := s.groupCandidate(ctx, group, matchTags, a); err != nil || c.Group != "" { - return albumMatch{candidate: c, OK: err == nil}, err - } - } - if rel := deref(a.Mbid); rel != "" { - group, err := s.releaseGroup(ctx, rel) - switch { - case errors.Is(err, tags.ErrNotFound): // includes MusicBrainz switched off for tags - case err != nil: - return albumMatch{}, fmt.Errorf("%w: %v", errMusicBrainzAway, err) - default: - if c, err := s.groupCandidate(ctx, group, matchRelease, a); err != nil || c.Group != "" { - return albumMatch{candidate: c, OK: err == nil}, err - } - } - } - - // Otherwise its name, checked against its tracks: the artist's own - // discography first, then a search. - tried := map[string]bool{} - var best albumMatch - bestN, bestEdition, considered, bestMiss := -1, 0, 0, "" - check := func(cands []candidate) error { - for _, c := range cands { - if tried[c.Group] || len(tried) >= matchCandidates { - continue - } - tried[c.Group] = true - n, size, miss, err := s.agreementWith(ctx, c.Group, tracks, func(edition string) bool { - return sameTitle(c.Title, a.Title) || sameTitle(edition, a.Title) - }) - if err != nil { - return err - } - considered++ - // More tracks agreeing wins; then the same title over a longer - // one; then the edition nearest the album's length. - st, bt := sameTitle(c.Title, a.Title), sameTitle(best.Title, a.Title) - if n > bestN || (n == bestN && ((st && !bt) || (st == bt && abs(size-len(tracks)) < abs(bestEdition-len(tracks))))) { - best, bestN, bestEdition, bestMiss = albumMatch{candidate: c}, n, size, miss - } - } - return nil - } - discography, err := s.discographyCandidates(ctx, a) - if err != nil { - return albumMatch{}, err - } - if err := check(discography); err != nil { - return albumMatch{}, err - } - if !agrees(bestN, len(tracks), bestEdition) { - found, err := s.searchCandidates(ctx, a) - if err != nil { - return albumMatch{}, err - } - if err := check(found); err != nil { - return albumMatch{}, err - } - } - if agrees(bestN, len(tracks), bestEdition) { - best.OK = true - return best, nil - } - if considered == 0 { - return albumMatch{Detail: fmt.Sprintf("MusicBrainz has no album called %q by %s.", a.Title, a.ArtistName)}, nil - } - detail := fmt.Sprintf("MusicBrainz has %s called %q by %s, but none has its tracks: the closest, %q, agrees on %d of its %d", - words.Count(considered, "album", "albums"), a.Title, a.ArtistName, best.Title, max(bestN, 0), len(tracks)) - switch { - case bestN < 0: - detail += " (MusicBrainz lists no edition of it)" - case bestMiss != "": - detail += ": " + bestMiss - } - return albumMatch{Detail: detail + "."}, nil -} - -// groupCandidate reads a group the tags name. A group MusicBrainz does not -// have is a zero candidate, so the album is matched by name instead. -func (s *Service) groupCandidate(ctx context.Context, group, method string, a dbq.ListAlbumsToMatchRow) (candidate, error) { - g, err := s.cat.MusicBrainz().ReleaseGroup(ctx, group) - switch { - case errors.Is(err, musicbrainz.ErrNotFound): - return candidate{}, nil - case err != nil: - return candidate{}, fmt.Errorf("%w: %v", errMusicBrainzAway, err) - } - artist := creditedArtist(g.ArtistMBIDs, deref(a.ArtistMbid)) - if artist == "" { - return candidate{}, nil - } - return candidate{Group: g.MBID, Title: g.Title, PrimaryType: g.PrimaryType, FirstReleaseDate: g.FirstReleaseDate, - ArtistMBID: artist, ArtistName: g.ArtistCredit, Method: method}, nil -} - -// discographyCandidates are the artist's release groups called what the -// album is called. -func (s *Service) discographyCandidates(ctx context.Context, a dbq.ListAlbumsToMatchRow) ([]candidate, error) { - artist := deref(a.ArtistMbid) - if !Followable(artist, a.ArtistName) { - return nil, nil // no artist, or Various Artists: thousands of groups - } - d, err := s.cat.DiscographyWithin(ctx, artist, matchDiscographyAge) - switch { - case errors.Is(err, catalogue.ErrNotFound): - return nil, nil - case err != nil: - return nil, fmt.Errorf("%w: %v", errMusicBrainzAway, err) - } - var out []candidate - for _, g := range d.Groups { - if release.TitleAgrees(g.Title, a.Title) { - out = append(out, candidate{Group: g.Mbid, Title: g.Title, PrimaryType: g.PrimaryType, - FirstReleaseDate: g.FirstReleaseDate, ArtistMBID: artist, ArtistName: d.Artist.Name, Method: matchDiscography}) - } - } - sortSameTitleFirst(out, a.Title) - return out, nil -} - -// searchCandidates are the groups MusicBrainz's search finds by the album's -// artist and title, whose title and artist both agree. -func (s *Service) searchCandidates(ctx context.Context, a dbq.ListAlbumsToMatchRow) ([]candidate, error) { - found, err := s.cat.MusicBrainz().SearchReleaseGroupsBy(ctx, a.ArtistName, a.Title, 10) - switch { - case errors.Is(err, musicbrainz.ErrNotFound): - return nil, nil - case errors.Is(err, musicbrainz.ErrBadResponse): - // A query MusicBrainz will not read is not a reason to wait. - s.logger.Warn("wanted: MusicBrainz refused a search", "album", a.Title, "artist", a.ArtistName, "err", err) - return nil, nil - case err != nil: - return nil, fmt.Errorf("%w: %v", errMusicBrainzAway, err) - } - var out []candidate - for _, g := range found { - artist := creditedArtist(g.ArtistMBIDs, deref(a.ArtistMbid)) - if artist == "" || !release.TitleAgrees(g.Title, a.Title) { - continue - } - if !slices.Contains(g.ArtistMBIDs, deref(a.ArtistMbid)) && !release.TitleAgrees(g.ArtistCredit, a.ArtistName) { - continue - } - out = append(out, candidate{Group: g.MBID, Title: g.Title, PrimaryType: g.PrimaryType, - FirstReleaseDate: g.FirstReleaseDate, ArtistMBID: artist, ArtistName: g.ArtistCredit, Method: matchSearch}) - } - sortSameTitleFirst(out, a.Title) - return out, nil -} - -// agreementWith is how many of the album's tracks the group's best edition -// agrees with, that edition's length, and how it differs. A group -// MusicBrainz no longer has, or lists no edition of, agrees with none (-1). -// ownName says whether an edition, by its title, is called what the album -// is, so that its track titles are enough. -func (s *Service) agreementWith(ctx context.Context, group string, tracks []libraryTrack, ownName func(edition string) bool) (n, size int, miss string, err error) { - eds, err := s.cat.Releases(ctx, group) - switch { - case errors.Is(err, catalogue.ErrNotFound): - return -1, 0, "", nil - case err != nil: - return 0, 0, "", fmt.Errorf("%w: %v", errMusicBrainzAway, err) - } - n = -1 - for _, e := range eds.Editions { - edition := make([]editionTrack, 0, len(e.Tracks)) - for _, t := range e.Tracks { - edition = append(edition, editionTrack{Medium: int(t.MediumPosition), Position: int(t.Position), - Title: t.Title, LengthMs: int(t.LengthMs), Recording: t.RecordingMbid}) - } - if got, m := trackAgreement(tracks, edition, ownName(e.Release.Title)); got > n || (got == n && abs(len(edition)-len(tracks)) < abs(size-len(tracks))) { - n, size, miss = got, len(edition), m - } - } - return n, size, miss, nil -} - -// creditedArtist is the artist a want is filed under: the library's own -// artist when MusicBrainz credits them, else the first credited. -func creditedArtist(credited []string, own string) string { - if own != "" && (len(credited) == 0 || slices.Contains(credited, own)) { - return own - } - if len(credited) > 0 { - return credited[0] - } - return "" -} - -func sortSameTitleFirst(cs []candidate, title string) { - slices.SortStableFunc(cs, func(x, y candidate) int { - switch sx, sy := sameTitle(x.Title, title), sameTitle(y.Title, title); { - case sx && !sy: - return -1 - case sy && !sx: - return 1 - } - return 0 - }) -} - -func abs(n int) int { - if n < 0 { - return -n - } - return n -} diff --git a/internal/wanted/profiles.go b/internal/wanted/profiles.go deleted file mode 100644 index d1e86764..00000000 --- a/internal/wanted/profiles.go +++ /dev/null @@ -1,228 +0,0 @@ -package wanted - -import ( - "context" - "errors" - "fmt" - "slices" - "strings" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgconn" - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// PrimaryTypes and SecondaryTypes are MusicBrainz's release-group types, in -// the order the profile editor lists them. -var ( - PrimaryTypes = []string{"Album", "EP", "Single", "Broadcast", "Other"} - SecondaryTypes = []string{ - "Compilation", "Live", "Remix", "Soundtrack", "DJ-mix", "Mixtape/Street", - "Demo", "Spokenword", "Interview", "Audiobook", "Audio drama", "Field recording", - } -) - -// Profile errors, which the API answers as 400 or 409. -var ( - ErrProfileInvalid = errors.New("wanted: profile is not valid") - ErrProfileNotFound = errors.New("wanted: no such profile") - ErrProfileInUse = errors.New("wanted: profile is in use") - ErrProfileDefault = errors.New("wanted: the default profile cannot be deleted") - ErrProfileNameUsed = errors.New("wanted: a profile already has that name") -) - -// Passes reports whether a release group of the given types is one a follow -// on profile p wants: its primary type is listed, and every secondary type -// it carries is allowed. A group with no secondary type is a studio release, -// which every profile passes on its primary type alone. -// -// A version its title names counts as a secondary type MusicBrainz left -// off: "Common People (Recorded For BBC Radio 1)" is live whatever -// its types say, and a version no type names, an acoustic one, no profile -// passes. -func Passes(p dbq.ReleaseTypeProfile, title, primary string, secondary []string) bool { - if !containsFold(p.PrimaryTypes, primary) { - return false - } - for _, s := range secondary { - if !containsFold(p.SecondaryTypes, s) { - return false - } - } - if v, ok := release.VersionInTitle(title); ok { - return v.Typ != "" && containsFold(p.SecondaryTypes, v.Typ) - } - return true -} - -func containsFold(list []string, s string) bool { - return slices.ContainsFunc(list, func(v string) bool { return strings.EqualFold(v, s) }) -} - -// ProfileInput is a profile as the editor sends it. -type ProfileInput struct { - Name string - PrimaryTypes []string - SecondaryTypes []string - IsDefault bool -} - -// normalize checks the input against MusicBrainz's types and returns it with -// each type in MusicBrainz's spelling and the editor's order. -func (in ProfileInput) normalize() (ProfileInput, error) { - in.Name = strings.TrimSpace(in.Name) - if in.Name == "" || len(in.Name) > 60 { - return in, fmt.Errorf("%w: a name of 1 to 60 characters is required", ErrProfileInvalid) - } - primary, err := pick(PrimaryTypes, in.PrimaryTypes) - if err != nil { - return in, err - } - if len(primary) == 0 { - return in, fmt.Errorf("%w: choose at least one kind of release", ErrProfileInvalid) - } - secondary, err := pick(SecondaryTypes, in.SecondaryTypes) - if err != nil { - return in, err - } - in.PrimaryTypes, in.SecondaryTypes = primary, secondary - return in, nil -} - -func pick(known, chosen []string) ([]string, error) { - for _, c := range chosen { - if !containsFold(known, c) { - return nil, fmt.Errorf("%w: %q is not a MusicBrainz release type", ErrProfileInvalid, c) - } - } - out := []string{} - for _, k := range known { - if containsFold(chosen, k) { - out = append(out, k) - } - } - return out, nil -} - -// CreateProfile adds a profile, making it the default when asked. -func (s *Service) CreateProfile(ctx context.Context, in ProfileInput) (dbq.ReleaseTypeProfile, error) { - in, err := in.normalize() - if err != nil { - return dbq.ReleaseTypeProfile{}, err - } - var out dbq.ReleaseTypeProfile - err = pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { - q := dbq.New(tx) - p, err := q.CreateReleaseTypeProfile(ctx, dbq.CreateReleaseTypeProfileParams{ - Name: in.Name, PrimaryTypes: in.PrimaryTypes, SecondaryTypes: in.SecondaryTypes, - }) - if err != nil { - return nameErr(err) - } - if in.IsDefault { - if err := makeDefault(ctx, q, p.ID); err != nil { - return err - } - p.IsDefault = true - } - out = p - return nil - }) - return out, err -} - -// UpdateProfile replaces a profile's name and types. IsDefault only ever -// moves the default here; to unset it, make another profile the default. -func (s *Service) UpdateProfile(ctx context.Context, id pgtype.UUID, in ProfileInput) (dbq.ReleaseTypeProfile, error) { - in, err := in.normalize() - if err != nil { - return dbq.ReleaseTypeProfile{}, err - } - var out dbq.ReleaseTypeProfile - err = pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { - q := dbq.New(tx) - p, err := q.UpdateReleaseTypeProfile(ctx, dbq.UpdateReleaseTypeProfileParams{ - ID: id, Name: in.Name, PrimaryTypes: in.PrimaryTypes, SecondaryTypes: in.SecondaryTypes, - }) - if errors.Is(err, pgx.ErrNoRows) { - return ErrProfileNotFound - } - if err != nil { - return nameErr(err) - } - if in.IsDefault && !p.IsDefault { - if err := makeDefault(ctx, q, p.ID); err != nil { - return err - } - p.IsDefault = true - } - out = p - return nil - }) - return out, err -} - -func makeDefault(ctx context.Context, q *dbq.Queries, id pgtype.UUID) error { - if err := q.ClearDefaultReleaseTypeProfile(ctx); err != nil { - return fmt.Errorf("wanted: clear default: %w", err) - } - if _, err := q.SetDefaultReleaseTypeProfile(ctx, id); err != nil { - return fmt.Errorf("wanted: set default: %w", err) - } - return nil -} - -// DeleteProfile removes a profile no follow uses. The default stays. -func (s *Service) DeleteProfile(ctx context.Context, id pgtype.UUID) error { - q := dbq.New(s.pool) - p, err := q.GetReleaseTypeProfile(ctx, id) - if errors.Is(err, pgx.ErrNoRows) { - return ErrProfileNotFound - } - if err != nil { - return fmt.Errorf("wanted: read profile: %w", err) - } - if p.IsDefault { - return ErrProfileDefault - } - if _, err := q.DeleteReleaseTypeProfile(ctx, id); err != nil { - var pgErr *pgconn.PgError - if errors.As(err, &pgErr) && pgErr.Code == "23503" { - return ErrProfileInUse - } - return fmt.Errorf("wanted: delete profile: %w", err) - } - return nil -} - -// profileOrDefault reads the profile a follow names; the zero id means the -// default. -func profileOrDefault(ctx context.Context, q *dbq.Queries, id pgtype.UUID) (dbq.ReleaseTypeProfile, error) { - var ( - p dbq.ReleaseTypeProfile - err error - ) - if id.Valid { - p, err = q.GetReleaseTypeProfile(ctx, id) - } else { - p, err = q.GetDefaultReleaseTypeProfile(ctx) - } - if errors.Is(err, pgx.ErrNoRows) { - return p, ErrProfileNotFound - } - if err != nil { - return p, fmt.Errorf("wanted: read profile: %w", err) - } - return p, nil -} - -func nameErr(err error) error { - var pgErr *pgconn.PgError - if errors.As(err, &pgErr) && pgErr.Code == "23505" { - return ErrProfileNameUsed - } - return fmt.Errorf("wanted: save profile: %w", err) -} diff --git a/internal/wanted/search.go b/internal/wanted/search.go deleted file mode 100644 index 5c8f0102..00000000 --- a/internal/wanted/search.go +++ /dev/null @@ -1,360 +0,0 @@ -package wanted - -import ( - "context" - "encoding/json" - "errors" - "log/slog" - "strconv" - "time" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/indexer" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// Searching for wants on a schedule. The schedule decides which wants and when; -// internal/acquire judges what a search finds and grabs. - -// Search outcomes, matching wants_search_outcome_check. -const ( - OutcomeGrabbed = "grabbed" - OutcomeWaiting = "waiting" - OutcomeNothing = "nothing" - OutcomeError = "error" -) - -const ( - // searchTick is how often the schedule looks for wants due; the - // search interval setting is how long a want waits between searches. - searchTick = 10 * time.Minute - // searchWindow is how long one pass searches: most of a tick, so a pass - // ends before the next begins. - searchWindow = 9 * time.Minute - // duePage bounds what one pass reads. The sources' pace is the real - // limit; this caps a pass whose sources are all unpaced. - duePage = 200 - // grabTries is how many grabbable releases one search tries in turn - // when the first is refused for its files. - grabTries = 3 - // passTimeout backs up the window for a call none of the clients' - // own timeouts cover, such as a wedged database read. - passTimeout = 15 * time.Minute -) - -// ErrWantHeld: the want is already answered by the library, and is not -// wanted for a better copy. -var ErrWantHeld = errors.New("wanted: the library already holds this album") - -// Searcher searches for wants and grabs what they need. -type Searcher struct { - pool *pgxpool.Pool - cat *catalogue.Service - logger *slog.Logger -} - -// NewSearcher returns a Searcher reading editions through cat (nil reads -// only what the catalogue already holds). -func NewSearcher(pool *pgxpool.Pool, cat *catalogue.Service, logger *slog.Logger) *Searcher { - if logger == nil { - logger = slog.Default() - } - return &Searcher{pool: pool, cat: cat, logger: logger} -} - -// Report is one pass. -type Report struct { - Looked int `json:"looked"` - Grabbed int `json:"grabbed"` - Waiting int `json:"waiting"` - Nothing int `json:"nothing"` - Failed int `json:"failed"` - // Paced: the pass stopped because the next search would wait past its - // window for a source's pace. - Paced bool `json:"paced"` - // Off: the schedule is switched off. Unconfigured: nothing to search - // with. Both are states, not faults. - Off bool `json:"off"` - Unconfigured bool `json:"unconfigured"` - AutoGrab bool `json:"auto_grab"` - Error string `json:"error,omitempty"` -} - -// Run searches every searchTick until ctx ends. -func (s *Searcher) Run(ctx context.Context) { - ticker := time.NewTicker(searchTick) - defer ticker.Stop() - said := "" - for { - select { - case <-ctx.Done(): - return - case <-ticker.C: - } - rep := s.Pass(ctx) - // A pass that searched is always logged; an idle one only when the - // schedule's state changes, so the log says once why it is quiet. - if state := rep.state(); rep.Looked > 0 || rep.Error != "" || state != said { - s.logger.Info("wanted: search pass", "state", state, "looked", rep.Looked, - "grabbed", rep.Grabbed, "waiting", rep.Waiting, "nothing", rep.Nothing, - "failed", rep.Failed, "paced", rep.Paced, "error", rep.Error) - said = state - } - } -} - -// state names why a pass did what it did: off, unconfigured, failed, -// idle (nothing was due) or searched. -func (r Report) state() string { - switch { - case r.Off: - return "off" - case r.Unconfigured: - return "unconfigured" - case r.Error != "": - return "failed" - case r.Looked == 0: - return "idle" - } - return "searched" -} - -// Pass searches for the wants due, paced by the sources, until the window -// closes, and records what it did. -func (s *Searcher) Pass(ctx context.Context) Report { - rep := s.pass(ctx) - if body, err := json.Marshal(rep); err == nil { - if err := dbq.New(s.pool).RecordSearchPass(ctx, body); err != nil && ctx.Err() == nil { - s.logger.Warn("wanted: record search pass", "err", err) - } - } - return rep -} - -func (s *Searcher) pass(ctx context.Context) Report { - var rep Report - ctx, cancel := context.WithTimeout(ctx, passTimeout) - defer cancel() - svc, settings, err := acquire.Build(ctx, s.pool) - if err != nil { - rep.Error = indexer.FirstLine(err.Error()) - return rep - } - rep.AutoGrab = settings.AutoGrab - switch { - case settings.SearchIntervalHours <= 0: - rep.Off = true - return rep - case svc.Search == nil: - rep.Unconfigured = true - return rep - } - deadline := time.Now().Add(searchWindow) - due, err := dbq.New(s.pool).ListWantsDueForSearch(ctx, dbq.ListWantsDueForSearchParams{ - Hours: settings.SearchIntervalHours, MaxRows: duePage, - }) - if err != nil { - rep.Error = indexer.FirstLine(err.Error()) - return rep - } - for _, row := range due { - if !svc.Ready(ctx, deadline) { - rep.Paced = ctx.Err() == nil - return rep - } - want := s.wantFor(ctx, searchRow{id: row.ID, artist: row.ArtistName, title: row.Title, - primary: row.PrimaryType, secondary: row.SecondaryTypes, date: row.FirstReleaseDate, - group: row.ReleaseGroupMbid, upgrade: row.Upgrade, heldTier: row.HeldTier, held: row.HeldQuality}) - out := s.one(ctx, svc, want, settings.AutoGrab) - rep.Looked++ - switch out.Outcome { - case OutcomeGrabbed: - rep.Grabbed++ - case OutcomeWaiting: - rep.Waiting++ - case OutcomeNothing: - rep.Nothing++ - default: - rep.Failed++ - s.logger.Warn("wanted: search failed", "want", want.Artist+" - "+want.Title, "err", out.Error) - } - } - return rep -} - -// searchRow is what a search needs of a wants row. -type searchRow struct { - id pgtype.UUID - artist, title, primary, date, group string - secondary []string - // upgrade, heldTier and held: a held album wanted for a better copy - // and the copy it has. - upgrade, heldTier, held string -} - -// wantFor describes a want for the matcher, with its editions' track counts -// from the catalogue (read from MusicBrainz the first time when it can be). -// A held album wanted for better quality takes its copy's tier as a floor. -func (s *Searcher) wantFor(ctx context.Context, r searchRow) acquire.Want { - w := acquire.Want{ID: r.id, Want: release.Want{ - Artist: r.artist, Title: r.title, Year: yearOf(r.date), PrimaryType: r.primary, SecondaryTypes: r.secondary, - }} - if r.upgrade == UpgradeQuality && release.KnownTier(r.heldTier) { - w.Floor, w.Held = release.Tier(r.heldTier), r.held - } - q := dbq.New(s.pool) - span, err := q.EditionTrackSpan(ctx, r.group) - if err == nil && span.MinTracks == 0 && s.cat != nil { - if _, rerr := s.cat.Releases(ctx, r.group); rerr == nil { - span, err = q.EditionTrackSpan(ctx, r.group) - } - } - if err == nil { - w.MinTracks, w.MaxTracks = int(span.MinTracks), int(span.MaxTracks) - } - return w -} - -func yearOf(date string) int { - if len(date) < 4 { - return 0 - } - y, _ := strconv.Atoi(date[:4]) - return y -} - -// Searched is what one want's search came to. -type Searched struct { - Outcome string `json:"outcome"` - Considered int `json:"considered"` - Best string `json:"best_release"` - Verdict string `json:"best_verdict"` - Grabbed string `json:"grabbed,omitempty"` - Narrowed []acquire.Narrowing `json:"narrowed"` - Error string `json:"error,omitempty"` -} - -// one searches for a want, takes the best release when auto-grab is on, and -// records what came of it on the want. -func (s *Searcher) one(ctx context.Context, svc *acquire.Service, want acquire.Want, autoGrab bool) Searched { - found := svc.For(ctx, want) - if found.KeepError != "" { - s.logger.Warn("wanted: searched, but could not keep the candidates", "want", want.Title, "err", found.KeepError) - } - out := Searched{Considered: found.Considered, Narrowed: found.Narrowed} - if out.Narrowed == nil { - out.Narrowed = []acquire.Narrowing{} - } - if len(found.Candidates) > 0 { - out.Best, out.Verdict = found.Candidates[0].Name, string(found.Candidates[0].Verdict) - } - switch { - case found.Unconfigured: - out.Outcome, out.Error = OutcomeError, "No indexer is set up, so there is nothing to search with." - case found.Error != "": - out.Outcome, out.Error = OutcomeError, found.Error - case !found.ForAPerson(): - out.Outcome = OutcomeNothing - case len(found.Grabbable()) == 0 || !autoGrab: - out.Outcome = OutcomeWaiting - default: - out.Outcome = OutcomeWaiting - s.grabBest(ctx, svc, want, found, &out) - } - s.record(ctx, want.ID, out) - return out -} - -// grabBest takes the best grabbable release, trying the next when one is -// refused for what its files hold. -func (s *Searcher) grabBest(ctx context.Context, svc *acquire.Service, want acquire.Want, found acquire.Result, out *Searched) { - for i, c := range found.Grabbable() { - if i == grabTries { - return - } - took := svc.Grab(ctx, want, &c, found.Candidates, false, acquire.Origin{By: acquire.OriginSchedule}) - switch { - case took.Added: - out.Outcome, out.Grabbed, out.Best, out.Verdict = OutcomeGrabbed, c.Name, c.Name, string(acquire.Grab) - if took.Error != "" { - s.logger.Warn("wanted: took a release but could not record it", "want", want.Title, "err", took.Error) - } - s.logger.Info("wanted: took a release", "want", want.Title, "artist", want.Artist, - "release", c.Name, "quality", c.Tier, "save_path", took.SavePath) - return - case took.Refused: - continue - case took.Unconfigured: - out.Outcome, out.Error = OutcomeError, "No download client is set up, so nothing was taken." - return - case took.Contest != nil: - return - case took.Error != "": - out.Outcome, out.Error = OutcomeError, took.Error - return - } - } -} - -func (s *Searcher) record(ctx context.Context, id pgtype.UUID, out Searched) { - if err := dbq.New(s.pool).RecordWantSearch(ctx, dbq.RecordWantSearchParams{ - ID: id, Outcome: out.Outcome, Considered: int32(out.Considered), - BestRelease: out.Best, BestVerdict: out.Verdict, SearchError: out.Error, - }); err != nil && ctx.Err() == nil { - s.logger.Warn("wanted: record search", "err", err) - } -} - -// SearchNow searches for one want at once, as the schedule would, for a -// person pressing Search: it spends the sources' pace but never waits on it. -func (s *Searcher) SearchNow(ctx context.Context, id pgtype.UUID) (Searched, error) { - want, err := s.load(ctx, id) - if err != nil { - return Searched{}, err - } - svc, settings, err := acquire.Build(ctx, s.pool) - if err != nil { - return Searched{}, err - } - return s.one(ctx, svc, want, settings.AutoGrab), nil -} - -// Take grabs the release named by key for a person, searching again first. -func (s *Searcher) Take(ctx context.Context, id pgtype.UUID, key string, force bool, by pgtype.UUID) (acquire.Taken, error) { - want, err := s.load(ctx, id) - if err != nil { - return acquire.Taken{}, err - } - svc, _, err := acquire.Build(ctx, s.pool) - if err != nil { - return acquire.Taken{}, err - } - took := svc.Take(ctx, want, key, force, acquire.Origin{By: acquire.OriginOperator, User: by}) - if took.Added && took.Chosen != nil { - s.record(ctx, id, Searched{ - Outcome: OutcomeGrabbed, Best: took.Chosen.Name, Verdict: string(took.Chosen.Verdict), - Considered: 0, Grabbed: took.Chosen.Name, - }) - } - return took, nil -} - -// load reads a want for a search: an open one, or a held album wanted for a -// better copy. -func (s *Searcher) load(ctx context.Context, id pgtype.UUID) (acquire.Want, error) { - row, err := dbq.New(s.pool).GetWantForSearch(ctx, id) - if err != nil { - return acquire.Want{}, err - } - if row.FulfilledAt.Valid && row.Upgrade == "" { - return acquire.Want{}, ErrWantHeld - } - return s.wantFor(ctx, searchRow{id: row.ID, artist: row.ArtistName, title: row.Title, - primary: row.PrimaryType, secondary: row.SecondaryTypes, date: row.FirstReleaseDate, - group: row.ReleaseGroupMbid, upgrade: row.Upgrade, heldTier: row.HeldTier, held: row.HeldQuality}), nil -} diff --git a/internal/wanted/search_test.go b/internal/wanted/search_test.go deleted file mode 100644 index 407dbd22..00000000 --- a/internal/wanted/search_test.go +++ /dev/null @@ -1,213 +0,0 @@ -package wanted - -import ( - "context" - "encoding/json" - "io" - "log/slog" - "net/http" - "net/http/httptest" - "strings" - "sync/atomic" - "testing" - - "git.fabledsword.com/bvandeusen/minstrel/internal/acquire" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" -) - -const ramHash = "0123456789abcdef0123456789abcdef01234567" - -const ramFeed = ` - -Daft Punk - Random Access Memories (2013) [FLAC] - magnet:?xt=urn:btih:` + ramHash + `400000000 - -Daft Punk Random Access Memories (Drumless Edition 2023) [Flac 24-88] - magnet:?xt=urn:btih:fedcba9876543210fedcba9876543210fedcba98900000000 - -` - -// searchStack is a fake indexer feed and a fake FabledExchange download -// client, configured as an admin would in Admin › Acquisition. -func searchStack(t *testing.T, ctx context.Context, store *acquire.Store) *atomic.Int32 { - t.Helper() - var added atomic.Int32 - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - switch { - case r.URL.Path == "/feed": - if q := r.URL.Query().Get("q"); q != "Daft Punk Random Access Memories" { - t.Errorf("searched for %q", q) - } - _, _ = io.WriteString(w, ramFeed) - case r.URL.Path == "/api/downloads" && r.Method == http.MethodPost: - var body struct { - Magnet string `json:"magnet"` - SavePath string `json:"save_path"` - } - _ = json.NewDecoder(r.Body).Decode(&body) - if !strings.Contains(body.Magnet, ramHash) || body.SavePath != "/downloads/"+ramHash { - t.Errorf("added %+v", body) - } - added.Add(1) - _, _ = io.WriteString(w, "{}") - default: - http.NotFound(w, r) - } - })) - t.Cleanup(srv.Close) - - feed, err := store.Create(ctx, acquire.ProtocolTorznab) - if err != nil { - t.Fatal(err) - } - if _, err := store.Save(ctx, feed.Code, acquire.Update{ - Label: "Feed", Enabled: true, BaseURL: srv.URL + "/feed", TimeoutMs: 5000, LocalPath: "/downloads", - }); err != nil { - t.Fatal(err) - } - ex, err := store.Create(ctx, acquire.ProtocolExchange) - if err != nil { - t.Fatal(err) - } - if _, err := store.Save(ctx, ex.Code, acquire.Update{ - Label: "Exchange", Enabled: true, BaseURL: srv.URL, TimeoutMs: 5000, ServesDownloads: true, LocalPath: "/downloads", - }); err != nil { - t.Fatal(err) - } - return &added -} - -func TestSearcher_FindsAndTakesAWant_Integration(t *testing.T) { - ctx := context.Background() - pool := newPool(t) - q := dbq.New(pool) - added := searchStack(t, ctx, acquire.NewStore(pool)) - w, err := WantWith(ctx, q, WantParams{ - GroupMBID: "rg-ram", ArtistMBID: "mb-daft-punk", ArtistName: "Daft Punk", Title: "Random Access Memories", - PrimaryType: "Album", FirstReleaseDate: "2013-05-17", Reason: ReasonRequest, - }) - if err != nil { - t.Fatal(err) - } - s := NewSearcher(pool, nil, slog.New(slog.NewTextHandler(io.Discard, nil))) - - rep := s.Pass(ctx) - if rep.Looked != 1 || rep.Grabbed != 1 || !rep.AutoGrab || added.Load() != 1 { - t.Fatalf("pass = %+v, added %d", rep, added.Load()) - } - got, err := q.GetWantByReleaseGroup(ctx, "rg-ram") - if err != nil { - t.Fatal(err) - } - if got.SearchOutcome != OutcomeGrabbed || got.SearchConsidered != 2 || !strings.Contains(got.BestRelease, "(2013) [FLAC]") { - t.Errorf("want after the pass: %s %d %q", got.SearchOutcome, got.SearchConsidered, got.BestRelease) - } - cands, err := q.ListWantCandidates(ctx, w.ID) - if err != nil { - t.Fatal(err) - } - if len(cands) != 2 || cands[0].Verdict != "grab" || cands[1].Verdict != "rejected" || !strings.Contains(cands[1].Reason, "drumless") { - t.Errorf("candidates = %+v", cands) - } - g, err := q.GetLiveGrab(ctx, w.ID) - if err != nil { - t.Fatal(err) - } - if g.InfoHash != ramHash || g.Client != "exchange" || g.Origin != acquire.OriginSchedule || g.Quality != "lossless" { - t.Errorf("grab = %+v", g) - } - settings, _ := q.GetAcquisitionSettings(ctx) - var last Report - if err := json.Unmarshal(settings.LastPass, &last); err != nil || !settings.LastPassAt.Valid || last.Grabbed != 1 { - t.Errorf("last pass = %s (%v)", settings.LastPass, err) - } - - // A want with a download running is not searched again. - if rep := s.Pass(ctx); rep.Looked != 0 { - t.Errorf("searched a want already downloading: %+v", rep) - } - - // Setting the drumless edition aside marks it on every kept candidate; - // allowing it again leaves it for a person until the next search. - mem := acquire.NewMemory(pool) - if err := mem.Block(ctx, dbq.BlockReleaseParams{ - InfoHash: cands[1].InfoHash, Name: cands[1].Name, Reason: acquire.BlockOperator, WantID: w.ID, - }); err != nil { - t.Fatal(err) - } - c, _ := q.GetWantCandidate(ctx, dbq.GetWantCandidateParams{WantID: w.ID, Key: cands[1].Key}) - if c.Verdict != "blocked" { - t.Errorf("blocked candidate = %s", c.Verdict) - } - if ok, err := mem.Unblock(ctx, cands[1].InfoHash); err != nil || !ok { - t.Fatalf("unblock: %v %v", ok, err) - } - c, _ = q.GetWantCandidate(ctx, dbq.GetWantCandidateParams{WantID: w.ID, Key: cands[1].Key}) - if c.Verdict != "review" { - t.Errorf("unblocked candidate = %s", c.Verdict) - } -} - -func TestSearcher_OffAndUnconfigured_Integration(t *testing.T) { - ctx := context.Background() - pool := newPool(t) - s := NewSearcher(pool, nil, slog.New(slog.NewTextHandler(io.Discard, nil))) - if rep := s.Pass(ctx); !rep.Unconfigured { - t.Errorf("no indexer: %+v", rep) - } - if _, err := dbq.New(pool).UpdateAcquisitionSettings(ctx, dbq.UpdateAcquisitionSettingsParams{ - AutoGrab: true, SearchIntervalHours: 0, AllowedQualities: []string{"lossless"}, - }); err != nil { - t.Fatal(err) - } - if rep := s.Pass(ctx); !rep.Off { - t.Errorf("interval 0: %+v", rep) - } -} - -func TestGrabs_OneLivePerWant_Integration(t *testing.T) { - ctx := context.Background() - pool := newPool(t) - q := dbq.New(pool) - w, err := WantWith(ctx, q, WantParams{GroupMBID: "rg-x", ArtistMBID: "mb-x", ArtistName: "X", Title: "Y", Reason: ReasonFollow}) - if err != nil { - t.Fatal(err) - } - mem := acquire.NewMemory(pool) - grab := func(hash string) dbq.InsertGrabParams { - return dbq.InsertGrabParams{WantID: w.ID, InfoHash: hash, Name: hash, Key: hash, Client: "qbittorrent", - SavePath: "/downloads/" + hash, Origin: acquire.OriginSchedule} - } - first, err := mem.Grabbed(ctx, grab("aaaa"), dbq.Grab{}.ID, "") - if err != nil { - t.Fatal(err) - } - if _, err := mem.Grabbed(ctx, grab("bbbb"), dbq.Grab{}.ID, ""); err == nil { - t.Error("a second live grab for one want was recorded") - } - second, err := mem.Grabbed(ctx, grab("bbbb"), first.ID, "Replaced by bbbb.") - if err != nil { - t.Fatalf("replacing: %v", err) - } - live, ok, err := mem.LiveGrab(ctx, w.ID) - if err != nil || !ok || live.ID != second.ID { - t.Errorf("live = %+v %v %v", live, ok, err) - } -} - -func TestReport_StateNamesWhyAPassWasQuiet(t *testing.T) { - for _, c := range []struct { - rep Report - want string - }{ - {Report{Off: true}, "off"}, - {Report{Unconfigured: true}, "unconfigured"}, - {Report{Error: "boom"}, "failed"}, - {Report{}, "idle"}, - {Report{Looked: 2, Grabbed: 1}, "searched"}, - } { - if got := c.rep.state(); got != c.want { - t.Errorf("%+v: state = %q, want %q", c.rep, got, c.want) - } - } -} diff --git a/internal/wanted/upgrade.go b/internal/wanted/upgrade.go deleted file mode 100644 index 5b620286..00000000 --- a/internal/wanted/upgrade.go +++ /dev/null @@ -1,125 +0,0 @@ -package wanted - -import ( - "context" - "fmt" - "time" - - "github.com/jackc/pgx/v5/pgtype" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -// Upgrades: a held album stays wanted when the copy the library has is below -// the quality the install settles for (acquisition_settings. -// upgrade_until), or when one of its tracks is a video rip. The search -// schedule then looks for a better release weekly, and the import replaces -// the old tracks with the new ones, carrying their history across. - -// Why a held album is wanted again, matching wants_upgrade_check. -const ( - UpgradeQuality = "quality" - UpgradeRip = "rip" -) - -// upgradeCheckEvery is how often held albums are judged. Their files change -// only by an import or a scan, and the check reads every held album. -const upgradeCheckEvery = time.Hour - -// UpgradeFor says why a held album is wanted again, if it is: a video rip -// among its tracks always (a rip is no copy of the album, whatever the -// setting), else a copy below until. until "" switches quality upgrades off. -func UpgradeFor(held release.Held, judged bool, rips int, until release.Tier) string { - switch { - case rips > 0: - return UpgradeRip - case until == "" || !judged: - return "" - case release.Rank(held.Tier) < release.Rank(until): - return UpgradeQuality - } - return "" -} - -// heldVerdict is one held want as the check judged it. -type heldVerdict struct { - id pgtype.UUID - upgrade, tier, words string - oldUpgrade, oldTier, old string -} - -func (v heldVerdict) changed() bool { - return v.upgrade != v.oldUpgrade || v.tier != v.oldTier || v.words != v.old -} - -// checkUpgrades judges every held album, at most once an hour, and records -// which are wanted for a better copy. -func (s *Service) checkUpgrades(ctx context.Context, res *PassResult) error { - if !s.upgradesChecked.IsZero() && s.now().Sub(s.upgradesChecked) < upgradeCheckEvery { - return nil - } - q := dbq.New(s.pool) - settings, err := q.GetAcquisitionSettings(ctx) - if err != nil { - return fmt.Errorf("read settings: %w", err) - } - rows, err := q.ListHeldWantFormats(ctx) - if err != nil { - return fmt.Errorf("read held albums: %w", err) - } - until := release.Tier(settings.UpgradeUntil) - var out []heldVerdict - flush := func(id pgtype.UUID, group []dbq.ListHeldWantFormatsRow) { - if len(group) == 0 { - return - } - formats := make([]release.HeldFormat, 0, len(group)) - rips := 0 - for _, r := range group { - formats = append(formats, release.HeldFormat{Format: r.FileFormat, Bytes: r.Bytes, Ms: r.DurationMs}) - rips += int(r.Rips) - } - held, judged := release.HeldCopy(formats) - v := heldVerdict{id: id, upgrade: UpgradeFor(held, judged, rips, until), - oldUpgrade: group[0].Upgrade, oldTier: group[0].HeldTier, old: group[0].HeldQuality} - if judged { - v.tier, v.words = string(held.Tier), held.Words - } - if v.upgrade != "" { - res.Upgrades++ - } - if v.changed() { - out = append(out, v) - } - } - var group []dbq.ListHeldWantFormatsRow - for _, r := range rows { - if len(group) > 0 && group[0].WantID != r.WantID { - flush(group[0].WantID, group) - group = group[:0] - } - group = append(group, r) - } - if len(group) > 0 { - flush(group[0].WantID, group) - } - - if len(out) > 0 { - p := dbq.SetWantUpgradesParams{} - for _, v := range out { - p.Ids = append(p.Ids, v.id) - p.Upgrades = append(p.Upgrades, v.upgrade) - p.HeldTiers = append(p.HeldTiers, v.tier) - p.HeldQualities = append(p.HeldQualities, v.words) - } - if err := q.SetWantUpgrades(ctx, p); err != nil { - return fmt.Errorf("record upgrades: %w", err) - } - } - if _, err := q.ClearHeldUpgrades(ctx); err != nil { - return fmt.Errorf("clear upgrades: %w", err) - } - s.upgradesChecked = s.now() - return nil -} diff --git a/internal/wanted/upgrade_test.go b/internal/wanted/upgrade_test.go deleted file mode 100644 index d9837bba..00000000 --- a/internal/wanted/upgrade_test.go +++ /dev/null @@ -1,173 +0,0 @@ -package wanted - -import ( - "context" - "fmt" - "testing" - "time" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" -) - -func TestUpgradeFor(t *testing.T) { - low := release.Held{Tier: release.TierLossyLow} - flac := release.Held{Tier: release.TierLossless} - high := release.Held{Tier: release.TierLossyHigh} - cases := []struct { - name string - held release.Held - judged bool - rips int - until release.Tier - want string - }{ - {"below the cutoff", low, true, 0, release.TierLossyHigh, UpgradeQuality}, - {"at the cutoff", high, true, 0, release.TierLossyHigh, ""}, - {"above the cutoff", flac, true, 0, release.TierLossyHigh, ""}, - {"upgrades off", low, true, 0, "", ""}, - {"a rip, upgrades off", flac, true, 1, "", UpgradeRip}, - {"nothing to judge", release.Held{}, false, 0, release.TierLossless, ""}, - } - for _, c := range cases { - if got := UpgradeFor(c.held, c.judged, c.rips, c.until); got != c.want { - t.Errorf("%s: %q, want %q", c.name, got, c.want) - } - } -} - -// seedMP3s puts Blur's The Ballad of Darren in the library as MP3s at about -// kbps, two tracks, the first held back as a video rip when rip is set. -func seedMP3s(t *testing.T, pool *pgxpool.Pool, kbps int64, rip bool) dbq.Album { - t.Helper() - ctx := context.Background() - q := dbq.New(pool) - mbid, group, rel := blur, theBallad, "r-ballad-mp3" - artist, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Blur", SortName: "Blur", Mbid: &mbid}) - if err != nil { - t.Fatal(err) - } - album, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "The Ballad of Darren", SortTitle: "Ballad of Darren", - ArtistID: artist.ID, Mbid: &rel, ReleaseGroupMbid: &group}) - if err != nil { - t.Fatal(err) - } - for i := range 2 { - num := int32(i + 1) - tr, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ - Title: fmt.Sprintf("Track %d", i+1), AlbumID: album.ID, ArtistID: artist.ID, TrackNumber: &num, - DurationMs: 200000, FilePath: fmt.Sprintf("/music/ballad/%02d.mp3", i+1), - FileSize: kbps * 200000 / 8, FileFormat: "mp3", - }) - if err != nil { - t.Fatal(err) - } - if rip && i == 0 { - if _, err := pool.Exec(ctx, `UPDATE tracks SET source_verdict = 'suspect' WHERE id = $1`, tr.ID); err != nil { - t.Fatal(err) - } - } - } - return album -} - -func TestCheckUpgrades_AHeldAlbumBelowTheCutoffIsWantedAgain_Integration(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - s := newService(t, pool, newFakeMB(t)) - - if _, err := WantWith(ctx, q, WantParams{ - GroupMBID: theBallad, ArtistMBID: blur, ArtistName: "Blur", Title: "The Ballad of Darren", Reason: ReasonRequest, - }); err != nil { - t.Fatal(err) - } - seedMP3s(t, pool, 192, false) - res, err := s.Pass(ctx) - if err != nil || res.Held != 1 || res.Upgrades != 1 { - t.Fatalf("pass: %+v, %v", res, err) - } - w, _ := q.GetWantByReleaseGroup(ctx, theBallad) - if w.Upgrade != UpgradeQuality || w.HeldTier != string(release.TierLossyLow) || w.HeldQuality != "MP3, about 192 kbps" { - t.Fatalf("want = upgrade %q, tier %q, %q", w.Upgrade, w.HeldTier, w.HeldQuality) - } - due, err := q.ListWantsDueForSearch(ctx, dbq.ListWantsDueForSearchParams{Hours: 24, MaxRows: 10}) - if err != nil || len(due) != 1 || due[0].Upgrade != UpgradeQuality { - t.Fatalf("due for search: %+v, %v", due, err) - } - - // Upgrades switched off: the album is not wanted again. - if _, err := pool.Exec(ctx, `UPDATE acquisition_settings SET upgrade_until = ''`); err != nil { - t.Fatal(err) - } - s.upgradesChecked = time.Time{} - if _, err := s.Pass(ctx); err != nil { - t.Fatal(err) - } - w, _ = q.GetWantByReleaseGroup(ctx, theBallad) - if w.Upgrade != "" { - t.Errorf("upgrades off: upgrade %q", w.Upgrade) - } -} - -func TestCheckUpgrades_ARipIsReplacedWhateverTheSetting_Integration(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - s := newService(t, pool, newFakeMB(t)) - - if _, err := WantWith(ctx, q, WantParams{ - GroupMBID: theBallad, ArtistMBID: blur, ArtistName: "Blur", Title: "The Ballad of Darren", Reason: ReasonRequest, - }); err != nil { - t.Fatal(err) - } - seedMP3s(t, pool, 320, true) - if _, err := pool.Exec(ctx, `UPDATE acquisition_settings SET upgrade_until = ''`); err != nil { - t.Fatal(err) - } - if _, err := s.Pass(ctx); err != nil { - t.Fatal(err) - } - w, _ := q.GetWantByReleaseGroup(ctx, theBallad) - if w.Upgrade != UpgradeRip { - t.Errorf("a rip: upgrade %q", w.Upgrade) - } -} - -// A missing track with no disc number is on disc 1, so a present 1/2 stands in -// for a missing "2", and the album is not downloaded again for nothing. A -// missing track no present track covers still counts. -func TestWantHoldings_AMissingTrackWithNoDiscIsOnDiscOne_Integration(t *testing.T) { - pool := newPool(t) - ctx := context.Background() - album := seedMP3s(t, pool, 320, false) - if _, err := pool.Exec(ctx, `UPDATE tracks SET disc_number = 1 WHERE album_id = $1`, album.ID); err != nil { - t.Fatal(err) - } - q := dbq.New(pool) - for _, n := range []int32{2, 3} { - num := n - tr, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ - Title: fmt.Sprintf("Stale %d", n), AlbumID: album.ID, ArtistID: album.ArtistID, TrackNumber: &num, - DurationMs: 200000, FilePath: fmt.Sprintf("/music/ballad/stale-%d.mp3", n), FileSize: 1, FileFormat: "mp3", - }) - if err != nil { - t.Fatal(err) - } - if _, err := pool.Exec(ctx, `UPDATE tracks SET missing_since = now() - interval '2 days' WHERE id = $1`, tr.ID); err != nil { - t.Fatal(err) - } - } - rows, err := q.WantHoldings(ctx, dbq.WantHoldingsParams{ - Since: pgtype.Timestamptz{Time: time.Now(), Valid: true}, GroupMbid: theBallad, AlbumID: album.ID, - }) - if err != nil { - t.Fatal(err) - } - if len(rows) != 1 || rows[0].UncoveredMissing != 1 { - t.Fatalf("holdings = %+v, want one album with 1 uncovered missing track (track 3 only)", rows) - } -} diff --git a/internal/wanted/wanted.go b/internal/wanted/wanted.go deleted file mode 100644 index afbbec71..00000000 --- a/internal/wanted/wanted.go +++ /dev/null @@ -1,542 +0,0 @@ -// Package wanted is what Minstrel is looking for: followed artists, each on -// a release-type profile, and wanted release groups. A want comes from an -// approved request, a followed artist's new release, or an album whose files -// went missing; it is held once the library has a full copy. The library's -// own albums are put on the list as held once, so the list is the record of -// what the library keeps. Searching for open wants is the Searcher's. -package wanted - -import ( - "context" - "errors" - "fmt" - "log/slog" - "time" - - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" - "git.fabledsword.com/bvandeusen/minstrel/internal/release" - "git.fabledsword.com/bvandeusen/minstrel/internal/tags" -) - -// Reasons a release group is wanted, matching wants_reason_check. -const ( - ReasonRequest = "request" - ReasonFollow = "follow" - ReasonMissing = "missing" -) - -// variousArtists is MusicBrainz's Various Artists. Its "discography" is every -// compilation ever catalogued; nobody follows it. -const variousArtists = "89ad4ac3-39f7-470e-963a-56509c546377" - -// ErrNotFollowable: an artist nobody follows (Various Artists, or one of -// MusicBrainz's bracketed placeholders such as [unknown]). -var ErrNotFollowable = errors.New("wanted: not an artist anyone follows") - -// Service owns follows, wants and the worker that keeps them current. -type Service struct { - pool *pgxpool.Pool - cat *catalogue.Service - logger *slog.Logger - now func() time.Time - // releaseGroup names the group of a MusicBrainz release id, to move a - // want recorded under a release id to its group. A field so tests - // stand in for MusicBrainz. - releaseGroup func(ctx context.Context, releaseMBID string) (string, error) - // upgradesChecked is when held albums were last judged for an upgrade - // the worker alone touches it. - upgradesChecked time.Time -} - -// New makes a Service reading discographies and editions through cat. -func New(pool *pgxpool.Pool, cat *catalogue.Service, logger *slog.Logger) *Service { - if logger == nil { - logger = slog.Default() - } - return &Service{pool: pool, cat: cat, logger: logger, now: time.Now, releaseGroup: tags.ReleaseGroupForRelease} -} - -// Followable reports whether an artist is one anyone would follow. -func Followable(mbid, name string) bool { - if mbid == "" || mbid == variousArtists { - return false - } - return len(name) < 2 || name[0] != '[' || name[len(name)-1] != ']' -} - -// FollowParams is a follow as the caller asks for it. A zero ProfileID means -// the default profile. WantExisting also wants what the artist has already -// released that the profile passes and the library lacks. -type FollowParams struct { - ArtistMBID string - ArtistName string - ProfileID pgtype.UUID - By pgtype.UUID - WantExisting bool -} - -// Follow starts or changes a follow. The worker reads the discography. -func (s *Service) Follow(ctx context.Context, p FollowParams) (dbq.Follow, error) { - return FollowWith(ctx, dbq.New(s.pool), p) -} - -// FollowWith is Follow inside the caller's transaction (request approval). -func FollowWith(ctx context.Context, q *dbq.Queries, p FollowParams) (dbq.Follow, error) { - if !Followable(p.ArtistMBID, p.ArtistName) { - return dbq.Follow{}, ErrNotFollowable - } - profile, err := profileOrDefault(ctx, q, p.ProfileID) - if err != nil { - return dbq.Follow{}, err - } - f, err := q.UpsertFollow(ctx, dbq.UpsertFollowParams{ - ArtistMbid: p.ArtistMBID, ArtistName: p.ArtistName, ProfileID: profile.ID, - FollowedBy: p.By, WantExisting: p.WantExisting, - }) - if err != nil { - return dbq.Follow{}, fmt.Errorf("wanted: follow: %w", err) - } - return f, nil -} - -// Unfollow stops following. Wants already recorded stay. -func (s *Service) Unfollow(ctx context.Context, artistMBID string) (bool, error) { - n, err := dbq.New(s.pool).DeleteFollow(ctx, artistMBID) - if err != nil { - return false, fmt.Errorf("wanted: unfollow: %w", err) - } - return n > 0, nil -} - -// WantParams is a release group to want. -type WantParams struct { - GroupMBID string - ArtistMBID string - ArtistName string - Title string - PrimaryType string - FirstReleaseDate string - Reason string - CreatedBy pgtype.UUID -} - -// WantWith records a want inside the caller's transaction, or returns the -// want already recorded for the group. -func WantWith(ctx context.Context, q *dbq.Queries, p WantParams) (dbq.Want, error) { - if p.GroupMBID == "" || p.ArtistMBID == "" { - return dbq.Want{}, errors.New("wanted: a release group and its artist are required") - } - w, err := q.WantReleaseGroup(ctx, dbq.WantReleaseGroupParams{ - ReleaseGroupMbid: p.GroupMBID, ArtistMbid: p.ArtistMBID, ArtistName: p.ArtistName, - Title: p.Title, PrimaryType: p.PrimaryType, FirstReleaseDate: p.FirstReleaseDate, - Reason: p.Reason, CreatedBy: p.CreatedBy, - }) - if err != nil { - return dbq.Want{}, fmt.Errorf("wanted: want: %w", err) - } - return w, nil -} - -// RemoveWant drops a want, open or held. -func (s *Service) RemoveWant(ctx context.Context, id pgtype.UUID) (bool, error) { - n, err := dbq.New(s.pool).DeleteWant(ctx, id) - if err != nil { - return false, fmt.Errorf("wanted: remove want: %w", err) - } - return n > 0, nil -} - -const ( - // FollowRefreshAge is how often a followed artist's discography is read - // for new releases: daily. - FollowRefreshAge = 24 * time.Hour - // NewReleaseGrace: a group first seen after the baseline is wanted only - // if it came out no earlier than this before the follow began. MusicBrainz - // gains old releases all the time; a single from 1998 catalogued today is - // not a new release. - NewReleaseGrace = 30 * 24 * time.Hour - // Interval is how often the worker looks for work; followsPerPass and - // wantsPerPass bound one pass so neither starves the other. - Interval = 5 * time.Minute - followsPerPass = 60 - wantsPerPass = 500 -) - -// Run applies follows and settles wants every Interval until ctx ends. -func (s *Service) Run(ctx context.Context) { - ticker := time.NewTicker(Interval) - defer ticker.Stop() - for { - res, err := s.Pass(ctx) - switch { - case err != nil && ctx.Err() == nil: - s.logger.Warn("wanted: pass stopped", "err", err, "follows", res.Follows, "wanted", res.Wanted, "held", res.Held) - case res.Wanted > 0 || res.Held > 0 || res.Baselined > 0 || res.Listed > 0 || res.Upgrades > 0: - s.logger.Info("wanted: pass complete", "follows", res.Follows, "baselined", res.Baselined, - "wanted", res.Wanted, "held", res.Held, "listed", res.Listed, "upgrades", res.Upgrades) - } - select { - case <-ctx.Done(): - return - case <-ticker.C: - } - } -} - -// PassResult is what one pass did. -type PassResult struct { - Follows int // follows whose discography was applied - Baselined int // of those, first reads - Wanted int // wants recorded from follows - Held int // wants the library now answers - Listed int // library albums put on the list as held - // Upgrades: held albums wanted for a better copy, when the hourly check - // ran this pass. - Upgrades int -} - -// Pass applies the follows due and settles open wants. An unreachable -// MusicBrainz stops the follows (they need it) but not the settling, which -// falls back to what is held. -func (s *Service) Pass(ctx context.Context) (PassResult, error) { - var res PassResult - ferr := s.applyFollows(ctx, &res) - if ctx.Err() != nil { - return res, ctx.Err() - } - // The one-time listing of the library; a failure waits for the - // next pass and does not hold back settling. - if err := s.listLibrary(ctx, &res); err != nil && ctx.Err() == nil { - s.logger.Warn("wanted: list the library", "err", err) - } - if err := s.settleWants(ctx, &res); err != nil { - return res, err - } - if err := s.checkUpgrades(ctx, &res); err != nil && ctx.Err() == nil { - s.logger.Warn("wanted: judge held albums for upgrades", "err", err) - } - return res, ferr -} - -func (s *Service) applyFollows(ctx context.Context, res *PassResult) error { - q := dbq.New(s.pool) - due, err := q.ListFollowsDue(ctx, dbq.ListFollowsDueParams{ - Before: pgtype.Timestamptz{Time: s.now().Add(-FollowRefreshAge), Valid: true}, - MaxRows: followsPerPass, - }) - if err != nil { - return fmt.Errorf("wanted: list follows due: %w", err) - } - for _, f := range due { - n, err := s.applyFollow(ctx, q, f) - if err != nil { - if errors.Is(err, musicbrainz.ErrUnreachable) || ctx.Err() != nil { - return err - } - s.logger.Warn("wanted: apply follow", "artist", f.ArtistName, "mbid", f.ArtistMbid, "err", err) - continue - } - res.Follows++ - res.Wanted += n - if !f.BaselineAt.Valid { - res.Baselined++ - } - } - return nil -} - -// applyFollow reads the artist's discography and judges each release group -// the follow has not judged before. On the first read (the baseline) they -// are only marked seen, unless the follow asked to want what already exists. -// After it, a group is wanted when the profile passes it, it is recent, and -// the library does not hold it. -func (s *Service) applyFollow(ctx context.Context, q *dbq.Queries, f dbq.Follow) (int, error) { - d, err := s.cat.DiscographyWithin(ctx, f.ArtistMbid, FollowRefreshAge) - if err == nil && d.Stale { - // The held copy, served because MusicBrainz could not be asked: - // applying it would mark the follow checked without having looked. - return 0, musicbrainz.ErrUnreachable - } - if err != nil { - if !errors.Is(err, catalogue.ErrNotFound) { - return 0, err - } - // Merged away or removed from MusicBrainz: nothing to read. Marked - // checked so it is asked again tomorrow, not every pass. - s.logger.Info("wanted: followed artist not in MusicBrainz", "artist", f.ArtistName, "mbid", f.ArtistMbid) - return 0, q.MarkFollowChecked(ctx, f.ArtistMbid) - } - profile, err := q.GetReleaseTypeProfile(ctx, f.ProfileID) - if err != nil { - return 0, fmt.Errorf("read profile: %w", err) - } - groups, err := q.ListFollowGroups(ctx, f.ArtistMbid) - if err != nil { - return 0, fmt.Errorf("read discography: %w", err) - } - baseline := !f.BaselineAt.Valid - wanted := 0 - for _, g := range groups { - judge := (baseline && f.WantExisting) || (!baseline && !g.Seen && s.recent(g.FirstReleaseDate, f.BaselineAt.Time)) - passes := Passes(profile, g.Title, g.PrimaryType, g.SecondaryTypes) - if !passes { - if err := s.dropRefusedVersion(ctx, q, g.Mbid, g.Title); err != nil { - return wanted, err - } - } - if judge && passes { - held, err := q.LibraryHoldsReleaseGroup(ctx, dbq.LibraryHoldsReleaseGroupParams{ - GroupMbid: g.Mbid, Title: g.Title, ArtistMbid: f.ArtistMbid, - }) - if err != nil { - return wanted, fmt.Errorf("library check: %w", err) - } - if !held { - w, err := WantWith(ctx, q, WantParams{ - GroupMBID: g.Mbid, ArtistMBID: f.ArtistMbid, ArtistName: f.ArtistName, Title: g.Title, - PrimaryType: g.PrimaryType, FirstReleaseDate: g.FirstReleaseDate, - Reason: ReasonFollow, CreatedBy: f.FollowedBy, - }) - if err != nil { - return wanted, err - } - if !w.FulfilledAt.Valid { - wanted++ - } - } - } - if !g.Seen { - if err := q.MarkGroupSeen(ctx, dbq.MarkGroupSeenParams{ArtistMbid: f.ArtistMbid, ReleaseGroupMbid: g.Mbid}); err != nil { - return wanted, fmt.Errorf("mark seen: %w", err) - } - } - } - return wanted, q.MarkFollowChecked(ctx, f.ArtistMbid) -} - -// dropRefusedVersion takes off the list a group a follow wants that Passes -// now refuses for the version its title names, while it is still only -// wanted: one the library holds, or wanted for another reason, stays. -func (s *Service) dropRefusedVersion(ctx context.Context, q *dbq.Queries, group, title string) error { - v, ok := release.VersionInTitle(title) - if !ok { - return nil - } - w, err := q.GetWantByReleaseGroup(ctx, group) - if errors.Is(err, pgx.ErrNoRows) { - return nil - } - if err != nil { - return fmt.Errorf("read want %s: %w", title, err) - } - if w.Reason != ReasonFollow || w.FulfilledAt.Valid { - return nil - } - if _, err := q.DeleteWant(ctx, w.ID); err != nil { - return fmt.Errorf("drop want %s: %w", title, err) - } - s.logger.Info("wanted: a followed artist's release is another version, taken off the list", - "want", title, "version", v.What, "word", v.Word) - return nil -} - -// recent reports whether a release dated date (MusicBrainz's partial date: -// "2026", "2026-10", "2026-10-09", or empty when not yet known) came out no -// earlier than NewReleaseGrace before the follow's baseline. A partial date -// is read as the last day it could mean, so a year-only date is not -// dismissed for being "January". -func (s *Service) recent(date string, baseline time.Time) bool { - t, ok := latestDate(date) - if !ok { - return true - } - return !t.Before(baseline.Add(-NewReleaseGrace)) -} - -func latestDate(date string) (time.Time, bool) { - if t, err := time.Parse("2006-01-02", date); err == nil { - return t, true - } - if t, err := time.Parse("2006-01", date); err == nil { - return t.AddDate(0, 1, -1), true - } - if t, err := time.Parse("2006", date); err == nil { - return t.AddDate(1, 0, -1), true - } - return time.Time{}, false -} - -// settleWants marks open wants held when the library answers them. Each -// want's editions come from the catalogue, read from MusicBrainz the first -// time; once MusicBrainz is unreachable the rest use only what is held. -func (s *Service) settleWants(ctx context.Context, res *PassResult) error { - q := dbq.New(s.pool) - open, err := q.ListOpenWants(ctx, wantsPerPass) - if err != nil { - return fmt.Errorf("wanted: list open wants: %w", err) - } - mbDown := false - for _, w := range open { - if ctx.Err() != nil { - return ctx.Err() - } - shortest, moved, err := s.shortestEdition(ctx, q, w, &mbDown) - if err != nil { - s.logger.Warn("wanted: read editions", "want", w.Title, "group", w.ReleaseGroupMbid, "err", err) - } - if moved { - continue // settled next pass, under its group - } - album, ok, err := s.answeredBy(ctx, q, w, shortest) - if err != nil { - return err - } - if !ok { - continue - } - if err := q.FulfillWant(ctx, dbq.FulfillWantParams{ID: w.ID, AlbumID: album}); err != nil { - return fmt.Errorf("wanted: fulfil: %w", err) - } - res.Held++ - } - return nil -} - -// shortestEdition is the track count of the group's shortest official -// edition (any edition when none is official), or 0 when the catalogue -// cannot say. moved reports that the want was recorded under a release id -// and has been moved to its group. -func (s *Service) shortestEdition(ctx context.Context, q *dbq.Queries, w dbq.Want, mbDown *bool) (int, bool, error) { - var releases []dbq.MbRelease - if *mbDown { - held, err := q.ListMBReleasesForGroup(ctx, w.ReleaseGroupMbid) - if err != nil { - return 0, false, err - } - releases = held - } else { - ed, err := s.cat.Releases(ctx, w.ReleaseGroupMbid) - switch { - case err == nil: - for _, e := range ed.Editions { - releases = append(releases, e.Release) - } - case errors.Is(err, catalogue.ErrNotFound): - moved, merr := s.moveToGroup(ctx, w) - return 0, moved, merr - case errors.Is(err, musicbrainz.ErrUnreachable): - *mbDown = true - return s.shortestEdition(ctx, q, w, mbDown) - default: - return 0, false, err - } - } - return Shortest(releases), false, nil -} - -// Shortest is the track count of the shortest official release with tracks, -// or of the shortest release with tracks when none is official, or 0. -func Shortest(releases []dbq.MbRelease) int { - best, bestAny := 0, 0 - for _, r := range releases { - n := int(r.TrackCount) - if n <= 0 { - continue - } - if bestAny == 0 || n < bestAny { - bestAny = n - } - if r.Status == "Official" && (best == 0 || n < best) { - best = n - } - } - if best > 0 { - return best - } - return bestAny -} - -// moveToGroup handles a want MusicBrainz has no release group under: one -// recorded under a release id. It is moved to the release's -// group, with the requests that name it; when the group is already wanted, -// this copy is dropped. -func (s *Service) moveToGroup(ctx context.Context, w dbq.Want) (bool, error) { - group, err := s.releaseGroup(ctx, w.ReleaseGroupMbid) - if err != nil { - if errors.Is(err, tags.ErrNotFound) { - return false, nil - } - return false, fmt.Errorf("release group of %s: %w", w.ReleaseGroupMbid, err) - } - if group == "" || group == w.ReleaseGroupMbid { - return false, nil - } - err = pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error { - tq := dbq.New(tx) - if _, gerr := tq.GetWantByReleaseGroup(ctx, group); gerr == nil { - if _, derr := tq.DeleteWant(ctx, w.ID); derr != nil { - return derr - } - } else if errors.Is(gerr, pgx.ErrNoRows) { - if rerr := tq.RepointWant(ctx, dbq.RepointWantParams{ID: w.ID, ReleaseGroupMbid: group}); rerr != nil { - return rerr - } - } else { - return gerr - } - return tq.RepointRequestsAlbumMbid(ctx, dbq.RepointRequestsAlbumMbidParams{ - NewMbid: &group, OldMbid: &w.ReleaseGroupMbid, - }) - }) - if err != nil { - return false, fmt.Errorf("move want to its group: %w", err) - } - s.logger.Info("wanted: want moved from a release id to its group", - "want", w.Title, "release_mbid", w.ReleaseGroupMbid, "release_group_mbid", group) - return true, nil -} - -// answeredBy finds the library album that answers a want, if any: -// -// - for a want about an album whose files went missing, that album once no -// missing track is left without a track on disk in its place; -// - otherwise an album of the group with at least as many tracks on disk as -// the shortest official edition; -// - and when the catalogue cannot say how long the group is: a track -// arrived since the want, or nothing missing before it is still missing. -// -// Only albums with a track on disk count. -func (s *Service) answeredBy(ctx context.Context, q *dbq.Queries, w dbq.Want, shortest int) (pgtype.UUID, bool, error) { - holdings, err := q.WantHoldings(ctx, dbq.WantHoldingsParams{ - Since: w.WantedAt, GroupMbid: w.ReleaseGroupMbid, AlbumID: w.AlbumID, - }) - if err != nil { - return pgtype.UUID{}, false, fmt.Errorf("wanted: read holdings: %w", err) - } - for _, h := range holdings { - if Answers(w, h, shortest) { - return h.AlbumID, true, nil - } - } - return pgtype.UUID{}, false, nil -} - -// Answers is answeredBy's verdict on one album. -func Answers(w dbq.Want, h dbq.WantHoldingsRow, shortest int) bool { - if h.Present == 0 { - return false - } - if w.Reason == ReasonMissing && w.AlbumID.Valid && h.AlbumID == w.AlbumID { - return h.UncoveredMissing == 0 - } - if shortest > 0 { - return int(h.Present) >= shortest - } - return h.ArrivedSince || !h.MissingBefore -} diff --git a/internal/wanted/wanted_test.go b/internal/wanted/wanted_test.go deleted file mode 100644 index e4512c31..00000000 --- a/internal/wanted/wanted_test.go +++ /dev/null @@ -1,481 +0,0 @@ -package wanted - -import ( - "context" - "errors" - "fmt" - "io" - "log/slog" - "net/http" - "net/http/httptest" - "os" - "strings" - "sync/atomic" - "testing" - "time" - - "github.com/jackc/pgx/v5/pgtype" - "github.com/jackc/pgx/v5/pgxpool" - - "git.fabledsword.com/bvandeusen/minstrel/internal/catalogue" - "git.fabledsword.com/bvandeusen/minstrel/internal/db" - "git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq" - "git.fabledsword.com/bvandeusen/minstrel/internal/dbtest" - "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" - "git.fabledsword.com/bvandeusen/minstrel/internal/tags" -) - -func TestPasses_PrimaryListedAndEverySecondaryAllowed(t *testing.T) { - standard := dbq.ReleaseTypeProfile{PrimaryTypes: []string{"Album", "EP", "Single"}, SecondaryTypes: []string{}} - withLive := dbq.ReleaseTypeProfile{PrimaryTypes: []string{"Album"}, SecondaryTypes: []string{"Live"}} - for _, c := range []struct { - profile dbq.ReleaseTypeProfile - title string - primary string - secondary []string - want bool - }{ - {standard, "Revenge", "Album", nil, true}, - {standard, "Helena", "single", nil, true}, // MusicBrainz's case is not relied on - {standard, "Live at Koko", "Album", []string{"Live"}, false}, - {standard, "Radio Show", "Broadcast", nil, false}, - {withLive, "Live at Koko", "Album", []string{"Live"}, true}, - {withLive, "Live & Rare", "Album", []string{"Live", "Compilation"}, false}, - {withLive, "Like Phantoms, Forever", "EP", nil, false}, - // MusicBrainz types these plain Singles; their titles say otherwise. - {standard, "Common People (Recorded For BBC Radio 1)", "Single", nil, false}, - {standard, "Welcome to the Black Parade (Steve Aoki remix)", "Single", nil, false}, - {standard, "The Five of Us Are Dying (Rough mix)", "Single", nil, false}, - {withLive, "Desolation Row (live at BBC Maida Vale)", "Album", nil, true}, - {withLive, "Helena (acoustic)", "Album", nil, false}, - {standard, "Live Through This", "Album", nil, true}, - } { - if got := Passes(c.profile, c.title, c.primary, c.secondary); got != c.want { - t.Errorf("Passes(%v/%v, %q %s %v) = %v, want %v", c.profile.PrimaryTypes, c.profile.SecondaryTypes, - c.title, c.primary, c.secondary, got, c.want) - } - } -} - -func TestProfileInput_NormalizesToMusicBrainzTypes(t *testing.T) { - in, err := ProfileInput{Name: " Live too ", PrimaryTypes: []string{"single", "Album"}, SecondaryTypes: []string{"live"}}.normalize() - if err != nil { - t.Fatal(err) - } - if in.Name != "Live too" || fmt.Sprint(in.PrimaryTypes) != "[Album Single]" || fmt.Sprint(in.SecondaryTypes) != "[Live]" { - t.Errorf("normalized = %+v", in) - } - for name, bad := range map[string]ProfileInput{ - "no name": {PrimaryTypes: []string{"Album"}}, - "no primary type": {Name: "x"}, - "unknown type": {Name: "x", PrimaryTypes: []string{"Mixtape"}}, - } { - if _, err := bad.normalize(); !errors.Is(err, ErrProfileInvalid) { - t.Errorf("%s: err = %v", name, err) - } - } -} - -func TestRecent_ReadsAPartialDateAsItsLastDay(t *testing.T) { - s := &Service{} - baseline := time.Date(2026, 10, 10, 0, 0, 0, 0, time.UTC) - for date, want := range map[string]bool{ - "": true, // not yet known: an announced release - "2026-11-01": true, - "2026-09-15": true, // inside the grace before the follow - "2026-08-01": false, - "2026": true, // could be December - "2025": false, - "2026-09": true, - "1998-04-01": false, - } { - if got := s.recent(date, baseline); got != want { - t.Errorf("recent(%q) = %v, want %v", date, got, want) - } - } -} - -func TestShortest_PrefersOfficialEditions(t *testing.T) { - rel := func(status string, n int32) dbq.MbRelease { return dbq.MbRelease{Status: status, TrackCount: n} } - for _, c := range []struct { - releases []dbq.MbRelease - want int - }{ - {nil, 0}, - {[]dbq.MbRelease{rel("Official", 14), rel("Official", 12), rel("Promotion", 3)}, 12}, - {[]dbq.MbRelease{rel("Bootleg", 9), rel("", 11)}, 9}, - {[]dbq.MbRelease{rel("Official", 0), rel("Bootleg", 9)}, 9}, - } { - if got := Shortest(c.releases); got != c.want { - t.Errorf("Shortest(%v) = %d, want %d", c.releases, got, c.want) - } - } -} - -func TestAnswers(t *testing.T) { - album := pgtype.UUID{Bytes: [16]byte{1}, Valid: true} - other := pgtype.UUID{Bytes: [16]byte{2}, Valid: true} - missing := dbq.Want{Reason: ReasonMissing, AlbumID: album} - request := dbq.Want{Reason: ReasonRequest} - for name, c := range map[string]struct { - w dbq.Want - h dbq.WantHoldingsRow - shortest int - want bool - }{ - "nothing on disk": {request, dbq.WantHoldingsRow{AlbumID: other, ArrivedSince: true}, 10, false}, - "a full edition": {request, dbq.WantHoldingsRow{AlbumID: other, Present: 10}, 10, true}, - "short of every edition": {request, dbq.WantHoldingsRow{AlbumID: other, Present: 9, ArrivedSince: true}, 10, false}, - "no editions known, an arrival": {request, dbq.WantHoldingsRow{AlbumID: other, Present: 1, ArrivedSince: true, MissingBefore: true}, 0, true}, - "no editions known, still missing": {request, dbq.WantHoldingsRow{AlbumID: other, Present: 5, MissingBefore: true}, 0, false}, - "the lost album, a gap remains": {missing, dbq.WantHoldingsRow{AlbumID: album, Present: 12, UncoveredMissing: 1}, 10, false}, - "the lost album, every gap filled": {missing, dbq.WantHoldingsRow{AlbumID: album, Present: 9, UncoveredMissing: 0}, 10, true}, - "another copy of the lost album": {missing, dbq.WantHoldingsRow{AlbumID: other, Present: 10}, 10, true}, - } { - if got := Answers(c.w, c.h, c.shortest); got != c.want { - t.Errorf("%s: Answers = %v, want %v", name, got, c.want) - } - } -} - -func TestFollowable(t *testing.T) { - for _, c := range []struct { - mbid, name string - want bool - }{ - {"a", "Gorillaz", true}, - {variousArtists, "Various Artists", false}, - {"b", "[unknown]", false}, - {"", "Nameless", false}, - {"c", "[ ]Bracketed Band", true}, - } { - if got := Followable(c.mbid, c.name); got != c.want { - t.Errorf("Followable(%q, %q) = %v", c.mbid, c.name, got) - } - } -} - -// --- integration --- - -func newPool(t *testing.T) *pgxpool.Pool { - t.Helper() - if testing.Short() { - t.Skip("skipping integration test in -short mode") - } - dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL") - if dsn == "" { - t.Skip("MINSTREL_TEST_DATABASE_URL not set") - } - if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { - t.Fatalf("migrate: %v", err) - } - pool, err := pgxpool.New(context.Background(), dsn) - if err != nil { - t.Fatalf("pool: %v", err) - } - t.Cleanup(pool.Close) - dbtest.ResetDB(t, pool) - return pool -} - -const ( - blur = "ba853904-ae25-4ebb-89d6-c44cfbd71bd2" - parklife = "11111111-0000-4000-8000-000000000001" // 1994 album, held - liveAtWemb = "11111111-0000-4000-8000-000000000002" // 2009 live album - theBallad = "11111111-0000-4000-8000-000000000003" // 2023 album, not held - newSingle = "11111111-0000-4000-8000-000000000004" // appears after the baseline - oldSingle = "11111111-0000-4000-8000-000000000005" // 1997 single, catalogued late - bbcSingle = "11111111-0000-4000-8000-000000000006" // live, typed a plain Single - parkRelease = "22222222-0000-4000-8000-000000000001" -) - -// fakeMB is a MusicBrainz holding Blur's discography; added groups appear in -// it after the baseline, and down makes every request a 503. -type fakeMB struct { - srv *httptest.Server - added atomic.Bool - down atomic.Bool -} - -func newFakeMB(t *testing.T) *fakeMB { - t.Helper() - f := &fakeMB{} - group := func(id, title, primary, secondary, date string) string { - return fmt.Sprintf(`{"id": %q, "title": %q, "primary-type": %q, "secondary-types": [%s], "first-release-date": %q, - "artist-credit": [{"name": "Blur", "artist": {"id": %q}}]}`, id, title, primary, secondary, date, blur) - } - f.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if f.down.Load() { - w.WriteHeader(http.StatusServiceUnavailable) - return - } - switch { - case r.URL.Path == "/artist/"+blur: - _, _ = fmt.Fprintf(w, `{"id": %q, "name": "Blur", "type": "Group", "country": "GB"}`, blur) - case r.URL.Path == "/release-group" && r.URL.Query().Get("artist") == blur: - groups := group(parklife, "Parklife", "Album", "", "1994-04-25") + "," + - group(liveAtWemb, "Live at Wembley Stadium", "Album", `"Live"`, "2009-07-02") + "," + - group(theBallad, "The Ballad of Darren", "Album", "", "2023-07-21") + "," + - group(bbcSingle, "Song 2 (Recorded For BBC Radio 1)", "Single", "", "2023-08-01") - n := 4 - if f.added.Load() { - groups += "," + group(newSingle, "New Single", "Single", "", "") + - "," + group(oldSingle, "Old Single", "Single", "", "1997-02-03") - n = 6 - } - _, _ = fmt.Fprintf(w, `{"release-group-count": %d, "release-groups": [%s]}`, n, groups) - case r.URL.Path == "/release-group/"+theBallad: - _, _ = fmt.Fprint(w, group(theBallad, "The Ballad of Darren", "Album", "", "2023-07-21")) - case r.URL.Path == "/release" && r.URL.Query().Get("release-group") == theBallad: - _, _ = fmt.Fprint(w, `{"release-count": 2, "releases": [ - {"id": "r-ballad-1", "title": "The Ballad of Darren", "status": "Official", "media": [{"position": 1, - "tracks": [{"position": 1, "number": "1", "title": "The Ballad", "recording": {"id": "rec-1"}}, - {"position": 2, "number": "2", "title": "St. Charles Square", "recording": {"id": "rec-2"}}]}]}, - {"id": "r-ballad-2", "title": "The Ballad of Darren", "status": "Official", "media": [{"position": 1, - "tracks": [{"position": 1, "number": "1", "title": "The Ballad", "recording": {"id": "rec-1"}}, - {"position": 2, "number": "2", "title": "St. Charles Square", "recording": {"id": "rec-2"}}, - {"position": 3, "number": "3", "title": "Bonus", "recording": {"id": "rec-3"}}]}]}]}`) - case r.URL.Path == "/release": - _, _ = fmt.Fprint(w, `{"release-count": 0, "releases": []}`) - case r.URL.Path == "/release-group" && strings.Contains(r.URL.Query().Get("query"), "releasegroup:"): - // The library listing's search by artist and title. - found := "" - if strings.Contains(r.URL.Query().Get("query"), "Ballad") { - found = group(theBallad, "The Ballad of Darren", "Album", "", "2023-07-21") - } - _, _ = fmt.Fprintf(w, `{"release-groups": [%s]}`, found) - default: - w.WriteHeader(http.StatusNotFound) - } - })) - t.Cleanup(f.srv.Close) - return f -} - -func newService(t *testing.T, pool *pgxpool.Pool, f *fakeMB) *Service { - t.Helper() - mb := &musicbrainz.Client{BaseURL: f.srv.URL, Pace: &musicbrainz.Pacer{}, Retries: -1} - s := New(pool, catalogue.New(pool, mb, nil), slog.New(slog.NewTextHandler(io.Discard, nil))) - // Never the real MusicBrainz from a test. - s.releaseGroup = func(context.Context, string) (string, error) { return "", tags.ErrNotFound } - return s -} - -// seedHeld puts an album of Blur's in the library with n tracks on disk. -func seedHeld(t *testing.T, q *dbq.Queries, title, release, group string, n int) dbq.Album { - t.Helper() - ctx := context.Background() - mbid := blur - artist, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Blur", SortName: "Blur", Mbid: &mbid}) - if err != nil { - t.Fatal(err) - } - params := dbq.UpsertAlbumParams{Title: title, SortTitle: title, ArtistID: artist.ID, Mbid: &release} - if group != "" { - params.ReleaseGroupMbid = &group - } - album, err := q.UpsertAlbum(ctx, params) - if err != nil { - t.Fatal(err) - } - for i := range n { - num := int32(i + 1) - if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{ - Title: fmt.Sprintf("Track %d", i+1), AlbumID: album.ID, ArtistID: artist.ID, TrackNumber: &num, - DurationMs: 180000, FilePath: fmt.Sprintf("/music/%s/%02d.flac", release, i+1), FileSize: 1, FileFormat: "flac", - }); err != nil { - t.Fatal(err) - } - } - return album -} - -func TestFollow_BaselineThenOnlyNewReleasesThatPass_Integration(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - f := newFakeMB(t) - s := newService(t, pool, f) - _ = seedHeld(t, q, "Parklife", parkRelease, parklife, 2) - - if _, err := s.Follow(ctx, FollowParams{ArtistMBID: blur, ArtistName: "Blur"}); err != nil { - t.Fatal(err) - } - res, err := s.Pass(ctx) - if err != nil { - t.Fatal(err) - } - if res.Baselined != 1 || res.Wanted != 0 { - t.Fatalf("baseline pass = %+v: a follow wants nothing it already lists", res) - } - - // MusicBrainz gains two singles: one new, one from 1997. A day later the - // follow reads the discography again. - f.added.Store(true) - if _, err := pool.Exec(ctx, ` - UPDATE mb_artists SET discography_refreshed_at = now() - interval '2 days'; - UPDATE follows SET checked_at = now() - interval '2 days'`); err != nil { - t.Fatal(err) - } - if res, err = s.Pass(ctx); err != nil { - t.Fatal(err) - } - if res.Wanted != 1 { - t.Errorf("second pass wanted %d, want 1 (the new single)", res.Wanted) - } - if w, err := q.GetWantByReleaseGroup(ctx, newSingle); err != nil || w.Reason != ReasonFollow || w.PrimaryType != "Single" { - t.Errorf("new single: %+v, %v", w, err) - } - if _, err := q.GetWantByReleaseGroup(ctx, oldSingle); err == nil { - t.Error("a 1997 single catalogued late is not a new release") - } -} - -func TestFollow_WantExistingWantsWhatPassesAndIsNotHeld_Integration(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - f := newFakeMB(t) - s := newService(t, pool, f) - _ = seedHeld(t, q, "Parklife", parkRelease, parklife, 2) - // Wanted before the follow refused the version its title names. - if _, err := WantWith(ctx, q, WantParams{GroupMBID: bbcSingle, ArtistMBID: blur, ArtistName: "Blur", - Title: "Song 2 (Recorded For BBC Radio 1)", PrimaryType: "Single", Reason: ReasonFollow}); err != nil { - t.Fatal(err) - } - - if _, err := s.Follow(ctx, FollowParams{ArtistMBID: blur, ArtistName: "Blur", WantExisting: true}); err != nil { - t.Fatal(err) - } - if _, err := s.Pass(ctx); err != nil { - t.Fatal(err) - } - if _, err := q.GetWantByReleaseGroup(ctx, theBallad); err != nil { - t.Errorf("the studio album not held should be wanted: %v", err) - } - for group, why := range map[string]string{parklife: "held", liveAtWemb: "live, and Standard is studio only", - bbcSingle: "recorded for the BBC, which its title says, and Standard is studio only"} { - if _, err := q.GetWantByReleaseGroup(ctx, group); err == nil { - t.Errorf("%s wanted, but it is %s", group, why) - } - } - if fol, _ := q.GetFollow(ctx, blur); fol.WantExisting || !fol.BaselineAt.Valid { - t.Errorf("follow = %+v: want_existing applied once, then cleared", fol) - } - - // A profile that allows live releases, applied with "want what exists". - live, err := s.CreateProfile(ctx, ProfileInput{Name: "With live", PrimaryTypes: []string{"Album"}, SecondaryTypes: []string{"Live"}}) - if err != nil { - t.Fatal(err) - } - if _, err := s.Follow(ctx, FollowParams{ArtistMBID: blur, ArtistName: "Blur", ProfileID: live.ID, WantExisting: true}); err != nil { - t.Fatal(err) - } - if _, err := s.Pass(ctx); err != nil { - t.Fatal(err) - } - if _, err := q.GetWantByReleaseGroup(ctx, liveAtWemb); err != nil { - t.Errorf("with live allowed, the live album is wanted: %v", err) - } - if err := s.DeleteProfile(ctx, live.ID); !errors.Is(err, ErrProfileInUse) { - t.Errorf("deleting a profile in use: err = %v", err) - } -} - -func TestSettle_HeldOnceAFullEditionIsOnDisk_Integration(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - s := newService(t, pool, newFakeMB(t)) - - if _, err := WantWith(ctx, q, WantParams{ - GroupMBID: theBallad, ArtistMBID: blur, ArtistName: "Blur", Title: "The Ballad of Darren", Reason: ReasonRequest, - }); err != nil { - t.Fatal(err) - } - album := seedHeld(t, q, "The Ballad of Darren", "r-ballad-1", theBallad, 1) - if res, err := s.Pass(ctx); err != nil || res.Held != 0 { - t.Fatalf("one track of a two-track edition: %+v, %v", res, err) - } - _ = seedHeld(t, q, "The Ballad of Darren", "r-ballad-1", theBallad, 2) - if res, err := s.Pass(ctx); err != nil || res.Held != 1 { - t.Fatalf("the shortest edition complete: %+v, %v", res, err) - } - w, _ := q.GetWantByReleaseGroup(ctx, theBallad) - if !w.FulfilledAt.Valid || w.AlbumID != album.ID { - t.Errorf("want = %+v: held by the album", w) - } -} - -func TestSettle_MovesAWantNamedByReleaseIdToItsGroup_Integration(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - s := newService(t, pool, newFakeMB(t)) - s.releaseGroup = func(_ context.Context, release string) (string, error) { - if release == "r-ballad-1" { - return theBallad, nil - } - return "", tags.ErrNotFound - } - if _, err := WantWith(ctx, q, WantParams{ - GroupMBID: "r-ballad-1", ArtistMBID: blur, ArtistName: "Blur", Title: "The Ballad of Darren", Reason: ReasonRequest, - }); err != nil { - t.Fatal(err) - } - group := "r-ballad-1" - if _, err := q.CreateRequest(ctx, dbq.CreateRequestParams{ - UserID: seedUser(t, pool), Kind: dbq.RequestKindAlbum, ArtistMbid: blur, AlbumMbid: &group, ArtistName: "Blur", - }); err != nil { - t.Fatal(err) - } - if _, err := s.Pass(ctx); err != nil { - t.Fatal(err) - } - if _, err := q.GetWantByReleaseGroup(ctx, theBallad); err != nil { - t.Errorf("the want should be under its group now: %v", err) - } - var moved string - if err := pool.QueryRow(ctx, "SELECT album_mbid FROM requests").Scan(&moved); err != nil || moved != theBallad { - t.Errorf("the request should follow its want: %q, %v", moved, err) - } -} - -func TestPass_MusicBrainzDownStopsFollowsButStillSettles_Integration(t *testing.T) { - pool := newPool(t) - q := dbq.New(pool) - ctx := context.Background() - f := newFakeMB(t) - s := newService(t, pool, f) - if _, err := s.Follow(ctx, FollowParams{ArtistMBID: blur, ArtistName: "Blur"}); err != nil { - t.Fatal(err) - } - if _, err := WantWith(ctx, q, WantParams{ - GroupMBID: "some-group", ArtistMBID: blur, ArtistName: "Blur", Title: "Some", Reason: ReasonRequest, - }); err != nil { - t.Fatal(err) - } - _ = seedHeld(t, q, "Some", "some-release", "some-group", 3) - f.down.Store(true) - res, err := s.Pass(ctx) - if !errors.Is(err, musicbrainz.ErrUnreachable) { - t.Errorf("err = %v, want ErrUnreachable", err) - } - if res.Held != 1 { - t.Errorf("held = %d: with no editions known, an album that arrived since the want answers it", res.Held) - } - if fol, _ := q.GetFollow(ctx, blur); fol.BaselineAt.Valid { - t.Error("a follow is not baselined without reading its discography") - } -} - -func seedUser(t *testing.T, pool *pgxpool.Pool) pgtype.UUID { - t.Helper() - u, err := dbq.New(pool).CreateUser(context.Background(), dbq.CreateUserParams{ - Username: dbtest.TestUserPrefix + "wanter", PasswordHash: "x", ApiTokenHash: "x", - }) - if err != nil { - t.Fatal(err) - } - return u.ID -} diff --git a/web/src/lib/admin/queues.ts b/web/src/lib/admin/queues.ts index 7232b02d..c1163e93 100644 --- a/web/src/lib/admin/queues.ts +++ b/web/src/lib/admin/queues.ts @@ -3,22 +3,21 @@ import type { AdminQueue, AdminSummary } from '#lib/api/types.js'; // The queues that wait on an admin, in the order the Overview lists // them. The rails and the Overview read the same list, so a queue that -// needs you is counted the same in every place. Requests live under Admin; -// the library's health queues, Missing files among them, under Collection. +// needs you is counted the same in every place. Every one of them is a +// library health queue, worked under Collection. export type QueueKey = keyof AdminSummary; export type QueueDef = { key: QueueKey; href: string; - // How the Overview names a count of them: "2 requests to decide". + // How the Overview names a count of them: "2 missing files". one: string; many: string; - // How the Overview's healthy line names an empty one: "no requests". + // How the Overview's healthy line names an empty one: "no missing files". none: string; }; export const QUEUES: QueueDef[] = [ - { key: 'requests', href: '/admin/requests', one: 'request to decide', many: 'requests to decide', none: 'requests' }, { key: 'reports', href: '/collection/reports', one: 'broken track reported', many: 'broken tracks reported', none: 'reports' }, { key: 'duplicates', href: '/collection/duplicates', one: 'set of duplicates to review', many: 'sets of duplicates to review', none: 'duplicates' }, { key: 'suspect', href: '/collection/suspect-sources', one: 'track from a suspect source', many: 'tracks from suspect sources', none: 'suspect sources' }, diff --git a/web/src/lib/api/admin.suspect-sources.test.ts b/web/src/lib/api/admin.suspect-sources.test.ts index 9f8624c3..e4ea3eac 100644 --- a/web/src/lib/api/admin.suspect-sources.test.ts +++ b/web/src/lib/api/admin.suspect-sources.test.ts @@ -17,8 +17,6 @@ function track(id: string): AdminSuspectTrack { markers: [], verdict: null, verdict_at: null, - replacing: false, - searched_at: null }; } diff --git a/web/src/lib/api/admin.test.ts b/web/src/lib/api/admin.test.ts index fffad4fe..2e10a7b2 100644 --- a/web/src/lib/api/admin.test.ts +++ b/web/src/lib/api/admin.test.ts @@ -10,9 +10,6 @@ vi.mock('./client', () => ({ })); import { - listAdminRequests, - approveRequest, - rejectRequest, listAdminQuarantine, resolveQuarantine, deleteQuarantineFile, @@ -23,111 +20,13 @@ import { qk } from './queries'; import type { ActionResult, AdminQuarantineRow, - QuarantineActionRow, - MusicRequest + QuarantineActionRow } from './types'; -const baseRow: MusicRequest = { - id: 'r1', - user_id: 'u1', - status: 'pending', - kind: 'album', - artist_mbid: 'art-mbid', - album_mbid: 'alb-mbid', - track_mbid: null, - artist_name: 'Aphex Twin', - album_title: 'Drukqs', - track_title: null, - decided_at: null, - decided_by: null, - notes: null, - completed_at: null, - matched_track_id: null, - matched_album_id: null, - matched_artist_id: null, - requested_at: '2026-01-01T00:00:00Z', - updated_at: '2026-01-01T00:00:00Z', - imported_album_count: 0, - imported_track_count: 0 -}; - beforeEach(() => { vi.clearAllMocks(); }); -describe('listAdminRequests', () => { - test('no args -> /api/admin/requests with no query string', async () => { - (api.get as ReturnType).mockResolvedValueOnce([baseRow]); - await listAdminRequests(); - expect(api.get).toHaveBeenCalledWith('/api/admin/requests'); - }); - - test('status only -> ?status=', async () => { - (api.get as ReturnType).mockResolvedValueOnce([baseRow]); - await listAdminRequests('approved'); - expect(api.get).toHaveBeenCalledWith('/api/admin/requests?status=approved'); - }); - - test('status + limit -> ?status=&limit=', async () => { - (api.get as ReturnType).mockResolvedValueOnce([baseRow]); - await listAdminRequests('pending', 25); - expect(api.get).toHaveBeenCalledWith( - '/api/admin/requests?status=pending&limit=25' - ); - }); - - test('limit only -> ?limit=', async () => { - (api.get as ReturnType).mockResolvedValueOnce([baseRow]); - await listAdminRequests(undefined, 10); - expect(api.get).toHaveBeenCalledWith('/api/admin/requests?limit=10'); - }); -}); - -describe('approveRequest', () => { - test('POSTs to /api/admin/requests/:id/approve with empty body by default', async () => { - (api.post as ReturnType).mockResolvedValueOnce(baseRow); - await approveRequest('r1'); - expect(api.post).toHaveBeenCalledWith('/api/admin/requests/r1/approve', {}); - }); - - test('sends the chosen release-type profile', async () => { - (api.post as ReturnType).mockResolvedValueOnce(baseRow); - await approveRequest('r1', 'p1'); - expect(api.post).toHaveBeenCalledWith('/api/admin/requests/r1/approve', { profile_id: 'p1' }); - }); -}); - -describe('rejectRequest', () => { - test('POSTs to /api/admin/requests/:id/reject with empty body when no notes', async () => { - (api.post as ReturnType).mockResolvedValueOnce(baseRow); - await rejectRequest('r1'); - expect(api.post).toHaveBeenCalledWith('/api/admin/requests/r1/reject', {}); - }); - - test('includes notes when provided', async () => { - (api.post as ReturnType).mockResolvedValueOnce(baseRow); - await rejectRequest('r1', 'duplicate of r0'); - expect(api.post).toHaveBeenCalledWith('/api/admin/requests/r1/reject', { - notes: 'duplicate of r0' - }); - }); -}); - -describe('qk admin keys', () => { - test('adminRequests key uses "all" when status omitted', () => { - // listAdminRequests sends no status param when called without one (returns - // every status), so the cache key must distinguish that from any specific - // status — otherwise an unfiltered call would falsely cache as `pending`. - expect(qk.adminRequests()).toEqual(['adminRequests', { status: 'all' }]); - }); - test('adminRequests key includes the status filter', () => { - expect(qk.adminRequests('approved')).toEqual([ - 'adminRequests', - { status: 'approved' } - ]); - }); -}); - const baseQuarantineRow: AdminQuarantineRow = { track_id: 't1', track_title: 'Avril 14th', diff --git a/web/src/lib/api/admin.ts b/web/src/lib/api/admin.ts index fed79375..fe811c09 100644 --- a/web/src/lib/api/admin.ts +++ b/web/src/lib/api/admin.ts @@ -16,8 +16,6 @@ import type { AdminStatus, AdminSummary, QuarantineActionRow, - MusicRequest, - RequestStatus, PlaybackErrorResolution } from './types'; @@ -38,7 +36,7 @@ export function createAdminSummaryQuery() { }); } -// Admin status: who is listening, downloads, the library and disk, +// Admin status: who is listening, the library and disk, // and the server, for the Overview below its verdict. Polled while open. export async function getAdminStatus(): Promise { @@ -53,59 +51,6 @@ export function createAdminStatusQuery() { }); } -// Admin request queue ----------------------------------------------------- - -export async function listAdminRequests( - status?: RequestStatus, - limit?: number -): Promise { - const params = new URLSearchParams(); - if (status) params.set('status', status); - if (limit !== undefined) params.set('limit', String(limit)); - const qs = params.toString(); - return api.get( - qs ? `/api/admin/requests?${qs}` : '/api/admin/requests' - ); -} - -// Approving puts the request on the wanted list. An artist -// request is followed on profileId, or the default profile when omitted. -export async function approveRequest(id: string, profileId?: string): Promise { - return api.post( - `/api/admin/requests/${id}/approve`, - profileId ? { profile_id: profileId } : {} - ); -} - -export async function rejectRequest( - id: string, - notes?: string -): Promise { - const body = notes !== undefined ? { notes } : {}; - return api.post(`/api/admin/requests/${id}/reject`, body); -} - -// Query factories --------------------------------------------------------- - -export function createAdminRequestsQuery(status?: RequestStatus) { - return createQuery({ - queryKey: qk.adminRequests(status), - queryFn: () => listAdminRequests(status), - // Only the 'approved' tab needs polling — that's where in-flight - // ingests live. Other tabs (pending/rejected/completed) are static - // until an admin acts on them. - refetchInterval: (query) => { - if (status !== 'approved') return false; - const rows = query.state.data as MusicRequest[] | undefined; - return hasInFlightRequest(rows) ? 12_000 : false; - } - }); -} - -function hasInFlightRequest(rows: readonly MusicRequest[] | undefined): boolean { - return rows?.some((r) => r.status === 'approved') ?? false; -} - // Admin quarantine -------------------------------------------------------- export async function listAdminQuarantine(): Promise { @@ -594,7 +539,6 @@ export type AdminUser = { username: string; display_name: string | null; is_admin: boolean; - auto_approve_requests: boolean; debug_mode_enabled: boolean; created_at: string; }; @@ -652,10 +596,6 @@ export async function resetUserPassword(id: string, password: string): Promise { - return api.put(`/api/admin/users/${id}/auto-approve`, { auto_approve: autoApprove }); -} - // Flip an account's diagnostics/debug-reporting opt-in. Admin-set; // the client obeys it (with a local per-device OFF switch). export async function updateUserDebugMode(id: string, enabled: boolean): Promise { @@ -970,28 +910,6 @@ export async function setSuspectVerdict(trackId: string, verdict: SuspectVerdict await api.put(`/api/admin/library/suspect-sources/${encodeURIComponent(trackId)}`, { verdict }); } -// Missing-file re-acquisition ---------------------- - -export type ReacquisitionSettings = { - enabled: boolean; - grace_hours: number; - max_per_pass: number; - // Albums with missing files that can never be wanted automatically because - // neither they nor their artist carries an MBID. Read-only; the server - // computes it, and the card states it so the gap isn't a mystery. - unnameable_albums: number; -}; - -export async function getReacquisitionSettings(): Promise { - return api.get('/api/admin/library/reacquisition'); -} - -export async function updateReacquisitionSettings( - s: ReacquisitionSettings -): Promise { - return api.put('/api/admin/library/reacquisition', s); -} - // Notification emails: when the grouped emails go out. export type NotificationEmailSettings = { summary_hour: number; @@ -1008,85 +926,6 @@ export async function updateNotificationEmailSettings( return api.put('/api/admin/notification-email', s); } -// Indexers and download clients ------------------------------- - -export type AcquisitionProtocol = 'prowlarr' | 'torznab' | 'qbittorrent' | 'exchange'; - -/** One indexer or download client. Secrets never come back, only whether set. */ -export type AcquisitionService = { - code: string; - kind: 'search' | 'download'; - protocol: AcquisitionProtocol; - label: string; - enabled: boolean; - base_url: string; - has_api_key: boolean; - has_password: boolean; - username: string; - category: string; - timeout_ms: number; - /** The three switches mean something on an exchange row only. */ - serves_search: boolean; - serves_downloads: boolean; - serves_enrichment: boolean; - local_path: string; - remote_path: string; - searches_per_hour: number; - /** Switched on and addressed. */ - usable: boolean; -}; - -export type AcquisitionUpdate = Omit< - AcquisitionService, - 'code' | 'kind' | 'protocol' | 'has_api_key' | 'has_password' | 'usable' ->; - -export type AcquisitionTestResult = { ok: boolean; detail: string }; - -const acquisitionBase = '/api/admin/acquisition/services'; - -export async function listAcquisitionServices(): Promise { - const res = await api.get<{ services: AcquisitionService[] }>(acquisitionBase); - return res.services; -} - -export async function addAcquisitionService( - protocol: AcquisitionProtocol -): Promise { - return api.post(acquisitionBase, { protocol }); -} - -export async function updateAcquisitionService( - code: string, - u: AcquisitionUpdate -): Promise { - return api.put(`${acquisitionBase}/${encodeURIComponent(code)}`, u); -} - -export async function removeAcquisitionService(code: string): Promise { - await api.del(`${acquisitionBase}/${encodeURIComponent(code)}`); -} - -export async function testAcquisitionService(code: string): Promise { - return api.post(`${acquisitionBase}/${encodeURIComponent(code)}/test`, {}); -} - -/** Stores a key or password. Write-only: nothing comes back. */ -export async function setAcquisitionSecret( - code: string, - which: 'api-key' | 'password', - value: string -): Promise { - await api.put(`${acquisitionBase}/${encodeURIComponent(code)}/${which}`, { value }); -} - -export async function clearAcquisitionSecret( - code: string, - which: 'api-key' | 'password' -): Promise { - await api.del(`${acquisitionBase}/${encodeURIComponent(code)}/${which}`); -} - // The server's own backup --------------------------------------------------- export type BackupDestination = 'folder' | 's3'; diff --git a/web/src/lib/api/catalogue.test.ts b/web/src/lib/api/catalogue.test.ts deleted file mode 100644 index 56fd5623..00000000 --- a/web/src/lib/api/catalogue.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { describe, expect, test, vi, beforeEach } from 'vitest'; - -vi.mock('./client', () => ({ - api: { - get: vi.fn(), - post: vi.fn(), - put: vi.fn(), - del: vi.fn() - } -})); - -import { searchCatalogue } from './catalogue'; -import { api } from './client'; -import { qk } from './queries'; -import type { CatalogueSearchResult } from './types'; - -beforeEach(() => { - vi.clearAllMocks(); -}); - -describe('searchCatalogue', () => { - test('builds URL with q and kind query params', async () => { - (api.get as ReturnType).mockResolvedValueOnce([]); - await searchCatalogue('aphex twin', 'artist'); - expect(api.get).toHaveBeenCalledWith('/api/catalogue/search?q=aphex+twin&kind=artist'); - }); - - test('encodes special characters in q (quotes, ampersands, plus)', async () => { - (api.get as ReturnType).mockResolvedValueOnce([]); - await searchCatalogue('boards of canada & friends', 'album'); - const calledWith = (api.get as ReturnType).mock.calls[0][0] as string; - // URLSearchParams encodes space as '+' and '&' as '%26'. - expect(calledWith).toBe( - '/api/catalogue/search?q=boards+of+canada+%26+friends&kind=album' - ); - }); - - test('passes through results unchanged', async () => { - const fixture: CatalogueSearchResult[] = [ - { - mbid: 'mbid-1', - name: 'Aphex Twin', - secondary_text: 'Electronic', - image_url: '/api/catalogue/art/artist/mbid-1?name=Aphex+Twin', - artist_mbid: '', - album_mbid: '', - in_library: false, - requested: false - } - ]; - (api.get as ReturnType).mockResolvedValueOnce(fixture); - const out = await searchCatalogue('aphex', 'artist'); - expect(out).toBe(fixture); - }); - - test('propagates errors from api.get', async () => { - const err = { code: 'musicbrainz_unreachable', message: 'down', status: 503 }; - (api.get as ReturnType).mockRejectedValueOnce(err); - await expect(searchCatalogue('x', 'track')).rejects.toMatchObject({ - code: 'musicbrainz_unreachable', - status: 503 - }); - }); -}); - -describe('qk.catalogueSearch', () => { - test('keys include q + kind', () => { - expect(qk.catalogueSearch('foo', 'artist')).toEqual([ - 'catalogueSearch', - { q: 'foo', kind: 'artist' } - ]); - }); -}); diff --git a/web/src/lib/api/catalogue.ts b/web/src/lib/api/catalogue.ts deleted file mode 100644 index 09d66f43..00000000 --- a/web/src/lib/api/catalogue.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { createQuery } from '@tanstack/svelte-query'; -import { api } from './client'; -import { qk } from './queries'; -import type { CatalogueSearchResult, RequestKind } from './types'; - -// searchCatalogue asks MusicBrainz through Minstrel's release catalogue. -// URLSearchParams handles encoding (spaces, quotes, -// ampersands) so callers can pass q raw. -export async function searchCatalogue( - q: string, - kind: RequestKind -): Promise { - const params = new URLSearchParams({ q, kind }); - return api.get(`/api/catalogue/search?${params.toString()}`); -} - -// MusicBrainz allows a request a second, so a repeated search within a minute -// is served from the client's cache rather than asked again. -export function createCatalogueSearchQuery(q: string, kind: RequestKind) { - return createQuery({ - queryKey: qk.catalogueSearch(q, kind), - queryFn: () => searchCatalogue(q, kind), - enabled: q.length > 0, - staleTime: 60_000 - }); -} diff --git a/web/src/lib/api/queries.ts b/web/src/lib/api/queries.ts index 06a49eb4..4270b315 100644 --- a/web/src/lib/api/queries.ts +++ b/web/src/lib/api/queries.ts @@ -29,29 +29,6 @@ export const qk = { likedAlbums: () => ['likedAlbums'] as const, likedArtists: () => ['likedArtists'] as const, history: () => ['history'] as const, - // Requests / admin. - catalogueSearch: (q: string, kind: string) => - ['catalogueSearch', { q, kind }] as const, - myRequests: () => ['myRequests'] as const, - adminRequests: (status?: string) => - ['adminRequests', { status: status ?? 'all' }] as const, - // Wanted list. Wants sit under ['adminWants'] so one prefix - // invalidation refreshes every tab and its counts. - adminWants: (state: string, q: string = '', artist: string = '') => - ['adminWants', { state, q, artist }] as const, - wantArtists: (q: string = '') => ['adminWants', 'artists', { q }] as const, - wantTracks: (id: string) => ['wantTracks', id] as const, - // The Collection's Unmatched section. - unmatched: () => ['adminWants', 'unmatched'] as const, - adminFollows: () => ['adminFollows'] as const, - adminFollow: (artistMbid: string) => ['adminFollow', artistMbid] as const, - releaseProfiles: () => ['releaseProfiles'] as const, - // Search and grab. - wantCandidates: (wantId: string) => ['wantCandidates', wantId] as const, - searchSettings: () => ['searchSettings'] as const, - // The Wanted view's Activity and Blocked tabs. - downloads: () => ['adminWants', 'downloads'] as const, - releaseBlocks: () => ['adminWants', 'releaseBlocks'] as const, myQuarantine: () => ['myQuarantine'] as const, adminQuarantine: () => ['adminQuarantine'] as const, adminQuarantineActions: (limit?: number) => @@ -83,9 +60,6 @@ export const qk = { notifications: () => ['notifications', 'list'] as const, notificationsUnread: () => ['notifications', 'unread'] as const, notificationSettings: () => ['notificationSettings'] as const, - suggestions: (limit?: number) => - ['suggestions', { limit: limit ?? 12 }] as const, - suggestionSnoozes: () => ['suggestionSnoozes'] as const, home: () => ['home'] as const, albumsAlpha: () => ['albumsAlpha'] as const, artistTracks: (artistId: string) => ['artistTracks', artistId] as const, diff --git a/web/src/lib/api/requests.test.ts b/web/src/lib/api/requests.test.ts deleted file mode 100644 index fb646533..00000000 --- a/web/src/lib/api/requests.test.ts +++ /dev/null @@ -1,183 +0,0 @@ -import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest'; - -vi.mock('./client', () => ({ - api: { - get: vi.fn(), - post: vi.fn(), - put: vi.fn(), - del: vi.fn() - }, - apiFetch: vi.fn() -})); - -import { - createRequest, - listMyRequests, - getRequest, - cancelRequest, - hasInFlightRequest -} from './requests'; -import { api, apiFetch } from './client'; -import { qk } from './queries'; -import type { MusicRequest } from './types'; - -const mockRow: MusicRequest = { - id: 'r1', - user_id: 'u1', - status: 'pending', - kind: 'album', - artist_mbid: 'art-mbid', - album_mbid: 'alb-mbid', - track_mbid: null, - artist_name: 'Aphex Twin', - album_title: 'Selected Ambient Works', - track_title: null, - decided_at: null, - decided_by: null, - notes: null, - completed_at: null, - matched_track_id: null, - matched_album_id: null, - matched_artist_id: null, - requested_at: '2026-01-01T00:00:00Z', - updated_at: '2026-01-01T00:00:00Z', - imported_album_count: 0, - imported_track_count: 0 -}; - -beforeEach(() => { - vi.clearAllMocks(); -}); - -afterEach(() => { - vi.clearAllMocks(); -}); - -describe('createRequest', () => { - test('POSTs /api/requests with full body for an album request', async () => { - (api.post as ReturnType).mockResolvedValueOnce(mockRow); - const out = await createRequest({ - kind: 'album', - artist_mbid: 'art-mbid', - album_mbid: 'alb-mbid', - artist_name: 'Aphex Twin', - album_title: 'Selected Ambient Works' - }); - expect(api.post).toHaveBeenCalledWith('/api/requests', { - kind: 'album', - artist_mbid: 'art-mbid', - artist_name: 'Aphex Twin', - album_mbid: 'alb-mbid', - album_title: 'Selected Ambient Works' - }); - expect(out).toBe(mockRow); - }); - - test('omits empty / undefined optional MBID + title fields from wire body', async () => { - (api.post as ReturnType).mockResolvedValueOnce(mockRow); - await createRequest({ - kind: 'artist', - artist_mbid: 'art-mbid', - artist_name: 'Aphex Twin', - album_mbid: '', - track_mbid: '' - }); - const body = (api.post as ReturnType).mock.calls[0][1] as Record< - string, - unknown - >; - expect(body).toEqual({ - kind: 'artist', - artist_mbid: 'art-mbid', - artist_name: 'Aphex Twin' - }); - expect(body).not.toHaveProperty('album_mbid'); - expect(body).not.toHaveProperty('track_mbid'); - expect(body).not.toHaveProperty('album_title'); - expect(body).not.toHaveProperty('track_title'); - }); - - test('includes track fields for a track-kind request', async () => { - (api.post as ReturnType).mockResolvedValueOnce(mockRow); - await createRequest({ - kind: 'track', - artist_mbid: 'art-mbid', - album_mbid: 'alb-mbid', - track_mbid: 'trk-mbid', - artist_name: 'Aphex Twin', - album_title: 'Drukqs', - track_title: 'Avril 14th' - }); - expect(api.post).toHaveBeenCalledWith('/api/requests', { - kind: 'track', - artist_mbid: 'art-mbid', - artist_name: 'Aphex Twin', - album_mbid: 'alb-mbid', - track_mbid: 'trk-mbid', - album_title: 'Drukqs', - track_title: 'Avril 14th' - }); - }); -}); - -describe('listMyRequests', () => { - test('GETs /api/requests', async () => { - (api.get as ReturnType).mockResolvedValueOnce([mockRow]); - const out = await listMyRequests(); - expect(api.get).toHaveBeenCalledWith('/api/requests'); - expect(out).toEqual([mockRow]); - }); -}); - -describe('getRequest', () => { - test('GETs /api/requests/:id', async () => { - (api.get as ReturnType).mockResolvedValueOnce(mockRow); - const out = await getRequest('r1'); - expect(api.get).toHaveBeenCalledWith('/api/requests/r1'); - expect(out).toBe(mockRow); - }); -}); - -describe('cancelRequest', () => { - test('DELETEs /api/requests/:id and returns the cancelled row', async () => { - const cancelled = { ...mockRow, status: 'rejected' as const }; - (apiFetch as ReturnType).mockResolvedValueOnce(cancelled); - const out = await cancelRequest('r1'); - expect(apiFetch).toHaveBeenCalledWith('/api/requests/r1', { method: 'DELETE' }); - expect(out).toBe(cancelled); - expect(out.status).toBe('rejected'); - }); -}); - -describe('qk.myRequests', () => { - test('returns the expected key tuple', () => { - expect(qk.myRequests()).toEqual(['myRequests']); - }); -}); - -describe('hasInFlightRequest', () => { - test('false for undefined / empty', () => { - expect(hasInFlightRequest(undefined)).toBe(false); - expect(hasInFlightRequest([])).toBe(false); - }); - - test('false when no row is approved', () => { - expect( - hasInFlightRequest([ - { ...mockRow, status: 'pending' }, - { ...mockRow, status: 'completed' }, - { ...mockRow, status: 'rejected' } - ]) - ).toBe(false); - }); - - test('true when at least one row is approved (mid-ingest)', () => { - expect( - hasInFlightRequest([ - { ...mockRow, status: 'pending' }, - { ...mockRow, status: 'approved' }, - { ...mockRow, status: 'completed' } - ]) - ).toBe(true); - }); -}); diff --git a/web/src/lib/api/requests.ts b/web/src/lib/api/requests.ts deleted file mode 100644 index 233914e0..00000000 --- a/web/src/lib/api/requests.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { createQuery } from '@tanstack/svelte-query'; -import { api, apiFetch } from './client'; -import { qk } from './queries'; -import type { MusicRequest, RequestKind } from './types'; - -/** Cadence for in-flight request polling: fast enough to feel live, slow - enough not to hammer the server. */ -const POLL_INTERVAL_MS = 12_000; - -/** True if any row is mid-ingest (status === 'approved'). The status flips - to 'completed' / 'rejected' when the music arrives, so this predicate - lets the page poll exactly while there's something interesting to watch. */ -export function hasInFlightRequest(rows: readonly MusicRequest[] | undefined): boolean { - return rows?.some((r) => r.status === 'approved') ?? false; -} - -// CreateRequestParams is the shape callers pass; unused MBID fields are -// optional, and we omit them from the wire body rather than sending empty -// strings (the backend tolerates either, but this keeps test expectations -// crisp and matches the server's wire shape). -export type CreateRequestParams = { - kind: RequestKind; - artist_mbid: string; - album_mbid?: string; - track_mbid?: string; - artist_name: string; - album_title?: string; - track_title?: string; -}; - -type CreateRequestBody = { - kind: RequestKind; - artist_mbid: string; - artist_name: string; - album_mbid?: string; - track_mbid?: string; - album_title?: string; - track_title?: string; -}; - -function buildCreateBody(params: CreateRequestParams): CreateRequestBody { - const body: CreateRequestBody = { - kind: params.kind, - artist_mbid: params.artist_mbid, - artist_name: params.artist_name - }; - if (params.album_mbid) body.album_mbid = params.album_mbid; - if (params.track_mbid) body.track_mbid = params.track_mbid; - if (params.album_title) body.album_title = params.album_title; - if (params.track_title) body.track_title = params.track_title; - return body; -} - -export async function createRequest(params: CreateRequestParams): Promise { - return api.post('/api/requests', buildCreateBody(params)); -} - -export async function listMyRequests(): Promise { - return api.get('/api/requests'); -} - -export async function getRequest(id: string): Promise { - return api.get(`/api/requests/${id}`); -} - -// Server returns the cancelled row body (not 204) so callers can patch the -// cache without a refetch. api.del's return type is fixed to null, so we -// drop down to apiFetch here to keep typing honest. -export async function cancelRequest(id: string): Promise { - return apiFetch(`/api/requests/${id}`, { method: 'DELETE' }) as Promise; -} - -export function createMyRequestsQuery() { - return createQuery({ - queryKey: qk.myRequests(), - queryFn: listMyRequests, - staleTime: 60_000, - // Auto-poll while at least one user request is mid-ingest. Stops - // automatically when all rows are pending/completed/rejected. - // refetchIntervalInBackground defaults to false → polling pauses - // while the tab is hidden and resumes on focus. - refetchInterval: (query) => - hasInFlightRequest(query.state.data as MusicRequest[] | undefined) - ? POLL_INTERVAL_MS - : false - }); -} diff --git a/web/src/lib/api/suggestions.test.ts b/web/src/lib/api/suggestions.test.ts deleted file mode 100644 index 86b2c861..00000000 --- a/web/src/lib/api/suggestions.test.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; - -vi.mock('./client', () => ({ - api: { get: vi.fn(), post: vi.fn(), del: vi.fn() } -})); - -import { - listSuggestions, - listSnoozes, - snoozeSuggestion, - unsnoozeSuggestion -} from './suggestions'; -import { qk } from './queries'; -import { api } from './client'; -import type { ArtistSuggestion, SuggestionSnooze } from './types'; - -afterEach(() => vi.clearAllMocks()); - -describe('suggestions client', () => { - test('listSuggestions hits the right URL with default limit', async () => { - const fixture: ArtistSuggestion[] = [ - { - mbid: 'm1', - name: 'Outsider', - score: 1.5, - attribution: [ - { artist_id: 'a1', name: 'Seed', contribution: 0.9, is_liked: true, play_count: 0 } - ] - } - ]; - (api.get as ReturnType).mockResolvedValueOnce(fixture); - const got = await listSuggestions(); - expect(api.get).toHaveBeenCalledWith('/api/discover/suggestions?limit=12'); - expect(got).toEqual(fixture); - }); - - test('listSuggestions honors a custom limit', async () => { - (api.get as ReturnType).mockResolvedValueOnce([]); - await listSuggestions(20); - expect(api.get).toHaveBeenCalledWith('/api/discover/suggestions?limit=20'); - }); - - test('qk.suggestions key shape', () => { - expect(qk.suggestions()).toEqual(['suggestions', { limit: 12 }]); - expect(qk.suggestions(20)).toEqual(['suggestions', { limit: 20 }]); - }); -}); - -describe('suggestion snoozes (#2375)', () => { - test('snoozeSuggestion sends the name — the server 400s without it', async () => { - (api.post as ReturnType).mockResolvedValueOnce(null); - await snoozeSuggestion('mb-1', 'Parked Artist'); - expect(api.post).toHaveBeenCalledWith('/api/discover/suggestions/mb-1/snooze', { - name: 'Parked Artist' - }); - }); - - test('snoozeSuggestion sends no days, leaving the default to the server', async () => { - (api.post as ReturnType).mockResolvedValueOnce(null); - await snoozeSuggestion('mb-1', 'Parked Artist'); - const body = (api.post as ReturnType).mock.calls[0][1] as Record; - expect(body).not.toHaveProperty('days'); - }); - - // MBIDs are UUIDs today, but the column is free-text and the value comes - // from an external similarity feed, so it goes through encodeURIComponent. - test('the mbid is URL-encoded into the path', async () => { - (api.post as ReturnType).mockResolvedValueOnce(null); - await snoozeSuggestion('weird/id?x', 'Odd'); - expect(api.post).toHaveBeenCalledWith( - '/api/discover/suggestions/weird%2Fid%3Fx/snooze', - { name: 'Odd' } - ); - }); - - test('unsnoozeSuggestion DELETEs the same path', async () => { - (api.del as ReturnType).mockResolvedValueOnce(null); - await unsnoozeSuggestion('mb-1'); - expect(api.del).toHaveBeenCalledWith('/api/discover/suggestions/mb-1/snooze'); - }); - - test('listSnoozes hits the snoozes collection', async () => { - const fixture: SuggestionSnooze[] = [ - { - mbid: 'mb-1', - name: 'Parked Artist', - snoozed_until: '2026-11-01T00:00:00Z', - created_at: '2026-08-03T00:00:00Z' - } - ]; - (api.get as ReturnType).mockResolvedValueOnce(fixture); - const got = await listSnoozes(); - expect(api.get).toHaveBeenCalledWith('/api/discover/snoozes'); - expect(got).toEqual(fixture); - }); - - test('qk.suggestionSnoozes key shape', () => { - expect(qk.suggestionSnoozes()).toEqual(['suggestionSnoozes']); - }); -}); diff --git a/web/src/lib/api/suggestions.ts b/web/src/lib/api/suggestions.ts deleted file mode 100644 index e782e5fe..00000000 --- a/web/src/lib/api/suggestions.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { createQuery } from '@tanstack/svelte-query'; -import { api } from './client'; -import { qk } from './queries'; -import type { ArtistSuggestion, SuggestionSnooze } from './types'; - -export async function listSuggestions(limit = 12): Promise { - return api.get(`/api/discover/suggestions?limit=${limit}`); -} - -export function createSuggestionsQuery(limit = 12) { - return createQuery({ - queryKey: qk.suggestions(limit), - queryFn: () => listSuggestions(limit), - staleTime: 5 * 60_000 // 5 minutes - }); -} - -// Parks a suggestion for the server's default period (90 days). `name` is -// REQUIRED by the server and is not optional bookkeeping: candidates are -// out-of-library, so there is no artists row to resolve a display name from -// and the snooze list would have nothing to render. Omitting it is a 400. -// -// No `days` is sent: the default is an admin setting on the server (Discover -// tuning), and hardcoding a value here would pin it to the client instead of -// the server that owns it. -export async function snoozeSuggestion(mbid: string, name: string): Promise { - await api.post(`/api/discover/suggestions/${encodeURIComponent(mbid)}/snooze`, { name }); -} - -// Brings a parked suggestion back immediately. The server 404s an MBID that -// was never snoozed; callers treat that as already-unsnoozed rather than as a -// failure, since the end state the user asked for is the one they get. -export async function unsnoozeSuggestion(mbid: string): Promise { - await api.del(`/api/discover/suggestions/${encodeURIComponent(mbid)}/snooze`); -} - -export async function listSnoozes(): Promise { - return api.get('/api/discover/snoozes'); -} - -export function createSnoozesQuery() { - return createQuery({ - queryKey: qk.suggestionSnoozes(), - queryFn: listSnoozes - // No staleTime, unlike the suggestions query: this list is the only route - // back to an un-snooze, so it must reflect a snooze made seconds ago - // rather than a cached view of the world. - }); -} diff --git a/web/src/lib/api/tuning.ts b/web/src/lib/api/tuning.ts index 13f0840b..a0d4c9fd 100644 --- a/web/src/lib/api/tuning.ts +++ b/web/src/lib/api/tuning.ts @@ -26,15 +26,7 @@ export type TasteTuning = { mood_scale: number; }; -// Discover request-surface knobs. Its own scope rather than part of -// taste: snooze_days belongs here, and a snooze must never be read as taste -// signal. -export type DiscoverTuning = { - tag_overlap_weight: number; - snooze_days: number; -}; - -export type TuningScope = 'radio' | 'daily_mix' | 'songs_like' | 'taste' | 'discover'; +export type TuningScope = 'radio' | 'daily_mix' | 'songs_like' | 'taste'; /** The weight-profile scopes, as distinct from the singleton-settings scopes. */ export type WeightProfileScope = 'radio' | 'daily_mix' | 'songs_like'; @@ -42,11 +34,9 @@ export type WeightProfileScope = 'radio' | 'daily_mix' | 'songs_like'; export type TuningSnapshot = { profiles: Record; taste: TasteTuning; - discover: DiscoverTuning; shipped: { profiles: Record; taste: TasteTuning; - discover: DiscoverTuning; }; }; diff --git a/web/src/lib/api/types.ts b/web/src/lib/api/types.ts index 051371a4..324f6f6a 100644 --- a/web/src/lib/api/types.ts +++ b/web/src/lib/api/types.ts @@ -133,66 +133,6 @@ export type EventRequest = export type PlayStartedResponse = { play_event_id: string; session_id: string }; export type EventOkResponse = { ok: true }; -// Request shared enums + shapes. -// Status + Kind are used across catalogue.ts, requests.ts, and admin.ts so -// they live here. Per-module helpers (CreateRequestParams, etc.) stay in their -// respective modules. -export type RequestStatus = - | 'pending' - | 'approved' - | 'rejected' - | 'completed' - | 'failed'; - -export type RequestKind = 'artist' | 'album' | 'track'; - -// One row of the request search (GET /api/catalogue/search). -// An album is a MusicBrainz release group; image_url is Minstrel's own art -// endpoint, which answers 404 when no source has an image. -export type CatalogueSearchResult = { - mbid: string; - name: string; - secondary_text: string; - image_url: string; - artist_mbid: string; - album_mbid: string; - in_library: boolean; - requested: boolean; -}; - -// pgtype.UUID and pgtype.Timestamptz JSON-marshal as a native string when -// Valid, and as `null` (or omitted, with omitempty) otherwise. We unify both -// "absent" cases as `string | null` so callers branch on a single check. -export type MusicRequest = { - id: string; - user_id: string; - // The requester's username. Only the admin list sends it. - username?: string; - status: RequestStatus; - kind: RequestKind; - artist_mbid: string; - // The album's MusicBrainz release group. - album_mbid?: string | null; - track_mbid?: string | null; - artist_name: string; - album_title?: string | null; - track_title?: string | null; - decided_at?: string | null; - decided_by?: string | null; - notes?: string | null; - completed_at?: string | null; - matched_track_id?: string | null; - matched_album_id?: string | null; - matched_artist_id?: string | null; - requested_at: string; - updated_at: string; - // Live ingest counters. Server fills these from the matched entity's - // children. Useful for in-flight requests so an admin can see - // "5 albums · 47 tracks" while the rest is still being fetched. - imported_album_count: number; - imported_track_count: number; -}; - // Quarantine ------------------------------------------------------------ export type QuarantineReason = @@ -295,36 +235,6 @@ export type ActionResult = { affected_users: number; }; -export type SeedContribution = { - artist_id: string; - name: string; - contribution: number; - is_liked: boolean; - play_count: number; -}; - -export type ArtistSuggestion = { - mbid: string; - name: string; - score: number; - attribution: SeedContribution[]; // up to 3 entries, ordered by contribution DESC - image_url?: string; // absent → card placeholder - // Tags this candidate shares with your taste profile, strongest first (max - // 3). Absent for most candidates — tag coverage for artists you don't own - // is partial by nature — so the card falls back to seed attribution. - matched_tags?: string[]; -}; - -// One parked suggestion — "not right now", not a dislike. The server only -// ever returns rows whose snoozed_until is still in the future, so the client -// never has to compare against the clock to decide what to show. -export type SuggestionSnooze = { - mbid: string; - name: string; - snoozed_until: string; // RFC3339 - created_at: string; -}; - // Mirrors internal/api/types.go HomePayload. All slices are non-null // per the server contract — empty sections render as []. export type HomePayload = { @@ -357,17 +267,6 @@ export type AdminMissingTrack = { last_played_at: string | null; }; -// What the wanted list says about this group's album: -// when it was wanted back, and when the library held it again. null while the -// album is inside the grace window, or when MusicBrainz can't name it. -export type AdminReacquisitionState = { - wanted_at: string | null; - held_at: string | null; - // The want itself, as the wanted list sends it, so the Missing page - // shows its search and download and acts on it. - want: Want | null; -}; - // A directory's worth of missing tracks. The server groups because the unit an // operator reasons about is a folder: three reorganised albums are three // decisions, not forty. @@ -375,7 +274,6 @@ export type AdminMissingGroup = { directory: string; missing_since: string; tracks: AdminMissingTrack[]; - reacquisition: AdminReacquisitionState | null; }; export type AdminMissingResponse = { @@ -405,10 +303,6 @@ export type AdminSuspectTrack = { // back. null: the resolver hasn't looked yet. verdict: SuspectVerdict | null; verdict_at: string | null; - // The album is on the wanted list for a clean copy, and when one was last - // searched for. - replacing: boolean; - searched_at: string | null; }; export type SuspectVerdict = 'suspect' | 'fine'; @@ -486,12 +380,10 @@ export type MergeDuplicateResult = { }; // What settling one recording in several slots did. kept_track_id is -// null when every copy went; blocked names the releases set aside, empty when -// the album came from before Minstrel downloaded anything. +// null when every copy went. export type RefetchDuplicateResult = { kept_track_id: string | null; removed_paths: string[]; - blocked: string[]; }; export type AdminDuplicatesResponse = { @@ -540,357 +432,10 @@ export type ResolvedDuplicatesResponse = { items: ResolvedDuplicateActivity[]; }; -// Wanted list ------------------------------------------------ - -// library: in the library when the list began. -export type WantReason = 'request' | 'follow' | 'missing' | 'library'; -// all: one artist's page, every album of theirs on the list. -// upgrade: held albums wanted for a better copy. -export type WantState = 'wanted' | 'missing' | 'upgrade' | 'held' | 'all'; -// Why a held album is wanted again: its copy is below the quality settled -// for, or one of its tracks is a video rip. -export type WantUpgrade = 'quality' | 'rip'; - -// One release group on the wanted list. held_at is set once the library -// holds it; artist_id and album_id link into the library when it has them. -export type Want = { - id: string; - release_group_mbid: string; - title: string; - artist_mbid: string; - artist_name: string; - artist_id?: string; - primary_type: string; - secondary_types: string[]; - year: string; - reason: WantReason; - requested_by?: string; - album_id?: string; - wanted_at: string; - held_at: string | null; - image_url: string; - // A held album wanted for a better copy, and what the copy it has is - // ("MP3, about 192 kbps"). - upgrade?: WantUpgrade; - held_quality?: string; - // The search schedule's last look, and the download running. - search?: WantSearch | null; - grab?: WantGrab | null; -}; - -export type SearchOutcome = 'grabbed' | 'waiting' | 'nothing' | 'error'; - -export type WantSearch = { - searched_at: string; - outcome: SearchOutcome; - considered: number; - best_release: string; - best_verdict: string; - error: string; -}; - -// What an import made of a finished download. -export type ImportState = - | '' - | 'downloading' - | 'waiting' - | 'importing' - | 'imported' - | 'refused' - | 'review' - | 'held' - | 'failed'; - -// A want's latest download, running or ended. -export type WantGrab = { - id: string; - name: string; - grabbed_at: string; - origin: 'schedule' | 'operator'; - quality: string; - live: boolean; - end_reason: string; - progress: number; - import_state: ImportState; - import_reason: string; - // Why an ended download ended. - end_detail: string; - imported_at: string | null; - imported_files: number; -}; - -// Search and grab ---------------------------------------------- - -export type CandidateVerdict = 'grab' | 'review' | 'refused' | 'rejected' | 'blocked'; - -// One release a want's last search found, with what was decided about it. -export type Candidate = { - key: string; - name: string; - info_hash: string; - indexer: string; - size_bytes: number; - seeders: number; - files_count: number; - format: string; - quality: string; - year: number; - verdict: CandidateVerdict; - reason: string; - reasons: { kind: 'hard' | 'soft'; code: string; text: string }[]; - folded_from: string[]; - searched_at: string; - downloading: boolean; -}; - -export type Narrowing = { source: string; label: string; reason: string }; - -export type SearchResult = { - outcome: SearchOutcome; - considered: number; - best_release: string; - best_verdict: string; - grabbed?: string; - narrowed: Narrowing[]; - error?: string; -}; - -export type GrabResult = { - added: boolean; - refused: boolean; - gone: boolean; - unconfigured: boolean; - missing?: string; - kept: boolean; - replaced: boolean; - verdict?: CandidateVerdict; - reason?: string; - error?: string; -}; - -export type QualityTier = { tier: string; label: string; what: string }; - -// The last scheduled pass; empty before the first. -export type SearchPass = { - looked?: number; - grabbed?: number; - waiting?: number; - nothing?: number; - failed?: number; - paced?: boolean; - off?: boolean; - unconfigured?: boolean; - auto_grab?: boolean; - error?: string; -}; - -export type SearchSettings = { - auto_grab: boolean; - search_interval_hours: number; - allowed_qualities: string[]; - tiers: QualityTier[]; - last_pass_at: string | null; - last_pass: SearchPass; - // The library folder new music is filed into ("" = the first of - // scan_paths), and the library's folders. - import_root: string; - scan_paths: string[]; - // The download lifecycle: an imported download seeds to this - // ratio or for this many days (0: removed once imported; no time limit), - // and one nobody shares is given up after this many hours (0: never). - seed_ratio: number; - seed_days: number; - stall_hours: number; - // The tier a held album is upgraded to; "" is no quality upgrades. - upgrade_until: string; -}; - -export type WantCounts = { - // Artists in the Collection's catalogue. - artists: number; - wanted: number; - missing: number; - held: number; - // Held albums wanted for a better copy. - upgrades: number; - following: number; - // Downloads running, those of them waiting on a person, releases set aside. - activity: number; - attention: number; - blocked: number; - // Library albums no MusicBrainz album matched, waiting to be matched by - // hand or left off. - unmatched: number; -}; - -// What an import's check counted. -export type ImportEvidence = { - files: number; - tracks: number; - identified: number; - recordings: number; - lengths: number; - titles: number; - group_tag?: string; - album_tag?: string; - fingerprinted: boolean; -}; - -// One download on the Activity tab. -export type Download = { - id: string; - want_id: string | null; - want_title: string; - want_artist: string; - name: string; - indexer: string; - client: string; - quality: string; - origin: 'schedule' | 'operator'; - grabbed_at: string; - ended_at: string | null; - end_reason: string; - progress: number; - import_state: ImportState; - import_reason: string; - imported_at: string | null; - imported_files: number; - destination: string; - edition?: string; - evidence?: ImportEvidence; - files: string[]; - skipped: number; - rehomed_from?: string; - // Why an ended download ended; for an imported one, its seeding. - end_detail: string; - seeding: boolean; - ratio: number; - seed_released_at: string | null; - // What is wrong with it now, if anything. - condition: DownloadCondition | null; -}; - -// An open condition on a download, from the lifecycle's ledger. -export type DownloadCondition = { - trigger: 'no_peers' | 'no_metadata' | 'client_error' | 'client_unreachable' | 'gone' | 'not_removed'; - detail: string; - retries: number; - since: string; -}; - -// A download client not answering now. -export type ClientDown = { client: string; detail: string; since: string }; - -export type DownloadsPage = { - items: Download[]; - limit: number; - offset: number; - clients_down: ClientDown[]; -}; - -// A release set aside: by a person, by its files, or by an import's check. -export type ReleaseBlock = { - info_hash: string; - name: string; - reason: 'operator' | 'files' | 'import' | 'stalled' | 'gone'; - detail: string; - blocked_at: string; - // When a release given up on may be taken again; null is for good. - expires_at: string | null; - want_title: string; - want_artist: string; -}; - -export type ReleaseBlocksPage = { items: ReleaseBlock[]; limit: number; offset: number }; - -// The one-time listing of the library's albums as held: when it -// finished (null while it runs), and how many albums nothing names to -// MusicBrainz, so they could not be listed. -// One album in the Collection's Unmatched section: why no -// MusicBrainz album matched it, or that a person left it off. -export type UnmatchedAlbum = { - album_id: string; - title: string; - artist: string; - detail: string; - tracks: number; - left_off: boolean; - decided_at: string; -}; - -// What matching an album by hand did: the MusicBrainz album it is now, and -// how many of its tracks agree with that album's closest edition. -export type ManualMatch = { release_group_mbid: string; title: string; artist: string; agree: number; tracks: number }; - -export type LibraryListing = { listed_at: string | null; unlisted: number }; - -// artist heads the list when it is narrowed to one artist's page. -export type WantsPage = Page & { counts: WantCounts; library: LibraryListing; artist?: WantArtist }; - -// One artist in the Collection's catalogue: how many of their albums -// on the list are held, of how many. -export type WantArtist = { - artist_mbid: string; - artist_name: string; - sort_name: string; - artist_id?: string; - image_url: string; - albums: number; - held: number; - wanted: number; - missing: number; - downloading: number; - following: boolean; -}; - -export type WantArtistsPage = Page & { counts: WantCounts; library: LibraryListing }; - -// An album's tracks in the Wanted view: the library's own for an -// album it holds (a lost file marked missing), else MusicBrainz's standard -// edition, named by edition; none when MusicBrainz lists no edition. -export type WantTrack = { disc: number; number: number; title: string; length_ms: number; missing: boolean }; - -export type WantTracks = { source: 'library' | 'musicbrainz' | 'none'; edition?: string; tracks: WantTrack[] }; - -export type Follow = { - artist_mbid: string; - artist_name: string; - artist_id?: string; - profile_id: string; - profile_name: string; - followed_at: string; - checked_at: string | null; - open_wants: number; - image_url: string; -}; - -export type FollowState = { following: boolean; profile_id?: string; profile_name?: string }; - -// A release-type profile: which kinds of release a follow wants. A group -// passes when its primary type is listed and every secondary type is too; -// no secondary types means studio releases only. -export type ReleaseProfile = { - id: string; - name: string; - primary_types: string[]; - secondary_types: string[]; - is_default: boolean; - follow_count: number; -}; - -export type ReleaseProfileInput = Omit; - -export type ReleaseProfilesResponse = { - profiles: ReleaseProfile[]; - primary_types: string[]; - secondary_types: string[]; -}; - // What GET /api/admin/summary returns: each queue that waits on an // admin, with how many and since when the oldest has waited (null when empty). export type AdminQueue = { count: number; since: string | null }; export type AdminSummary = { - requests: AdminQueue; // Broken tracks: flagged by a listener or failed to play, counted by track. reports: AdminQueue; duplicates: AdminQueue; @@ -921,15 +466,6 @@ export type AdminStatus = { listeners_day: number; last_play_at: string | null; }; - downloads: { - running: number; - needs_you: number; - needs_you_since: string | null; - imported_day: number; - given_up_day: number; - live: Download[]; - clients_down: { client: string; detail: string; since: string }[]; - }; library: { tracks: number; albums: number; artists: number; bytes: number; disks: AdminDisk[] }; server: { version: string; diff --git a/web/src/lib/api/wanted.ts b/web/src/lib/api/wanted.ts deleted file mode 100644 index a98200e4..00000000 --- a/web/src/lib/api/wanted.ts +++ /dev/null @@ -1,308 +0,0 @@ -import { createInfiniteQuery, createQuery, keepPreviousData } from '@tanstack/svelte-query'; -import { api } from './client'; -import { qk } from './queries'; -import type { - Candidate, - DownloadsPage, - Follow, - ReleaseBlocksPage, - FollowState, - ReleaseProfile, - ReleaseProfileInput, - ReleaseProfilesResponse, - GrabResult, - ManualMatch, - SearchResult, - SearchSettings, - UnmatchedAlbum, - WantArtistsPage, - WantState, - WantTracks, - WantsPage -} from './types'; - -// The wanted list: release groups Minstrel is to get, the -// artists it follows for new releases, and the release-type profiles that -// say which kinds of release a follow wants. - -// q narrows the list to an artist or album whose name holds it; artist to -// one artist's page, which the response then heads. -export async function listWants( - state: WantState, - offset: number = 0, - q: string = '', - artist: string = '' -): Promise { - const params = new URLSearchParams({ state, offset: String(offset) }); - if (q) params.set('q', q); - if (artist) params.set('artist', artist); - return api.get(`/api/admin/wanted?${params}`); -} - -export function wantsNextOffset(last: WantsPage): number | undefined { - const loaded = last.offset + last.items.length; - return last.items.length === 0 || loaded >= last.total ? undefined : loaded; -} - -// Loads as the page scrolls, one tab at a time. -export function createWantsQuery(state: WantState, q: string = '', artist: string = '') { - return createInfiniteQuery({ - queryKey: qk.adminWants(state, q, artist), - queryFn: ({ pageParam }) => listWants(state, pageParam as number, q, artist), - initialPageParam: 0, - getNextPageParam: wantsNextOffset, - // A new filter keeps the list it narrows on screen until its answer comes. - placeholderData: keepPreviousData, - staleTime: 30_000 - }); -} - -// The Collection's catalogue: every artist on the list, A–Z, with -// how many of their albums are held. Its first page carries every section's -// count. -export async function listWantArtists(offset: number = 0, q: string = ''): Promise { - const params = new URLSearchParams({ offset: String(offset) }); - if (q) params.set('q', q); - return api.get(`/api/admin/wanted/artists?${params}`); -} - -export function createWantArtistsQuery(q: string = '') { - return createInfiniteQuery({ - queryKey: qk.wantArtists(q), - queryFn: ({ pageParam }) => listWantArtists(pageParam as number, q), - initialPageParam: 0, - getNextPageParam: (last: WantArtistsPage) => { - const loaded = last.offset + last.items.length; - return last.items.length === 0 || loaded >= last.total ? undefined : loaded; - }, - placeholderData: keepPreviousData, - staleTime: 30_000 - }); -} - -// An album's tracks: the library's own for an album it holds, -// otherwise MusicBrainz's standard edition. -export async function getWantTracks(id: string): Promise { - return api.get(`/api/admin/wanted/${id}/tracks`); -} - -export function createWantTracksQuery(id: string) { - return createQuery({ - queryKey: qk.wantTracks(id), - queryFn: () => getWantTracks(id), - staleTime: 5 * 60_000 - }); -} - -// The Collection's Unmatched section: library albums no MusicBrainz -// album matched, and after them the ones a person left off. -export async function listUnmatched(): Promise { - const out = await api.get<{ items: UnmatchedAlbum[] }>('/api/admin/wanted/unmatched'); - return out.items; -} - -export function createUnmatchedQuery() { - return createQuery({ - queryKey: qk.unmatched(), - queryFn: listUnmatched, - staleTime: 30_000 - }); -} - -// Puts an unmatched album on the list as held, as the MusicBrainz album ref -// names: a release group's or a release's link, or a bare id. -export async function matchUnmatched(albumId: string, ref: string): Promise { - return api.post(`/api/admin/wanted/unmatched/${albumId}/match`, { ref }); -} - -// Leaves an album off for good (a local band's, which MusicBrainz will -// never have), or puts one left off back. -export async function leaveUnmatchedOff(albumId: string): Promise { - await api.post(`/api/admin/wanted/unmatched/${albumId}/leave-off`, {}); -} - -export async function putUnmatchedBack(albumId: string): Promise { - await api.post(`/api/admin/wanted/unmatched/${albumId}/put-back`, {}); -} - -export async function removeWant(id: string): Promise { - await api.del(`/api/admin/wanted/${id}`); -} - -export async function listFollows(): Promise { - return api.get('/api/admin/follows'); -} - -export function createFollowsQuery() { - return createQuery({ - queryKey: qk.adminFollows(), - queryFn: listFollows, - staleTime: 30_000 - }); -} - -export async function getFollow(artistMbid: string): Promise { - return api.get(`/api/admin/follows/${artistMbid}`); -} - -export function createFollowQuery(artistMbid: string) { - return createQuery({ - queryKey: qk.adminFollow(artistMbid), - queryFn: () => getFollow(artistMbid), - staleTime: 30_000 - }); -} - -// Follows an artist, or moves a follow to another profile. wantExisting also -// wants what the artist has already released that the profile passes and the -// library doesn't hold. An omitted profile is the default. -export async function putFollow( - artistMbid: string, - body: { artist_name: string; profile_id?: string; want_existing?: boolean } -): Promise { - return api.put(`/api/admin/follows/${artistMbid}`, body); -} - -export async function deleteFollow(artistMbid: string): Promise { - await api.del(`/api/admin/follows/${artistMbid}`); -} - -export async function listReleaseProfiles(): Promise { - return api.get('/api/admin/release-profiles'); -} - -export function createReleaseProfilesQuery() { - return createQuery({ - queryKey: qk.releaseProfiles(), - queryFn: listReleaseProfiles, - staleTime: 60_000 - }); -} - -export async function createReleaseProfile(body: ReleaseProfileInput): Promise { - return api.post('/api/admin/release-profiles', body); -} - -export async function updateReleaseProfile(id: string, body: ReleaseProfileInput): Promise { - return api.put(`/api/admin/release-profiles/${id}`, body); -} - -export async function deleteReleaseProfile(id: string): Promise { - await api.del(`/api/admin/release-profiles/${id}`); -} - -// Search and grab: each want's last search, a person's Search, -// Grab and "never this one", and the schedule's settings. - -export async function listCandidates(wantId: string): Promise { - const res = await api.get<{ items: Candidate[] }>(`/api/admin/wanted/${wantId}/candidates`); - return res.items; -} - -export function createCandidatesQuery(wantId: string) { - return createQuery({ - queryKey: qk.wantCandidates(wantId), - queryFn: () => listCandidates(wantId), - staleTime: 30_000 - }); -} - -// Searches now, as the schedule would: takes the best release when -// auto-grab is on. Can take most of a minute. -export async function searchWant(wantId: string): Promise { - return api.post(`/api/admin/wanted/${wantId}/search`, {}); -} - -// Takes the release named by key; force takes what the verdict would not. -export async function grabCandidate(wantId: string, key: string, force: boolean): Promise { - return api.post(`/api/admin/wanted/${wantId}/grab`, { key, force }); -} - -// Never takes this release again, for any want. -export async function blockCandidate(wantId: string, key: string): Promise { - await api.post(`/api/admin/wanted/${wantId}/block`, { key }); -} - -export async function unblockRelease(infoHash: string): Promise { - await api.del(`/api/admin/release-blocks/${infoHash}`); -} - -export async function getSearchSettings(): Promise { - return api.get('/api/admin/acquisition/settings'); -} - -export function createSearchSettingsQuery() { - return createQuery({ - queryKey: qk.searchSettings(), - queryFn: getSearchSettings, - staleTime: 30_000 - }); -} - -export async function putSearchSettings(body: { - auto_grab: boolean; - search_interval_hours: number; - allowed_qualities: string[]; - import_root: string; - seed_ratio: number; - seed_days: number; - stall_hours: number; - upgrade_until: string; -}): Promise { - return api.put('/api/admin/acquisition/settings', body); -} - -// Downloads and their imports ---------------------------------- - -// A page holding fewer than it asked for is the last. -function nextOffset(last: { items: unknown[]; limit: number; offset: number }): number | undefined { - return last.items.length < last.limit ? undefined : last.offset + last.items.length; -} - -export async function listDownloads(offset: number = 0): Promise { - return api.get(`/api/admin/downloads?offset=${offset}`); -} - -// Running downloads first; refreshed every few seconds while one runs. -export function createDownloadsQuery() { - return createInfiniteQuery({ - queryKey: qk.downloads(), - queryFn: ({ pageParam }) => listDownloads(pageParam as number), - initialPageParam: 0, - getNextPageParam: nextOffset, - refetchInterval: (q) => - q.state.data?.pages[0]?.items.some((d) => !d.ended_at) ? 5_000 : false - }); -} - -export async function importAnyway(id: string): Promise { - await api.post(`/api/admin/downloads/${id}/import`, {}); -} - -export async function retryImport(id: string): Promise { - await api.post(`/api/admin/downloads/${id}/retry`, {}); -} - -export async function refuseDownload(id: string): Promise { - await api.post(`/api/admin/downloads/${id}/refuse`, {}); -} - -// A running download given up: the release is set aside for good, the -// download removed and the album searched for again. -export async function abandonDownload(id: string): Promise { - await api.post(`/api/admin/downloads/${id}/abandon`, {}); -} - -export async function listReleaseBlocks(offset: number = 0): Promise { - return api.get(`/api/admin/release-blocks?offset=${offset}`); -} - -export function createReleaseBlocksQuery() { - return createInfiniteQuery({ - queryKey: qk.releaseBlocks(), - queryFn: ({ pageParam }) => listReleaseBlocks(pageParam as number), - initialPageParam: 0, - getNextPageParam: nextOffset, - staleTime: 30_000 - }); -} diff --git a/web/src/lib/components/AcquisitionCard.svelte b/web/src/lib/components/AcquisitionCard.svelte deleted file mode 100644 index bb1b4557..00000000 --- a/web/src/lib/components/AcquisitionCard.svelte +++ /dev/null @@ -1,166 +0,0 @@ - - -{#snippet addMenu(kind: 'search' | 'download', label: string)} -
- {#if adding === kind} -
- {#each choices[kind] as choice (choice.protocol)} - - {/each} - -
- {:else} - - {/if} -
-{/snippet} - -
- {#if loadError} -

- Couldn't load indexers and download clients. - -

- {:else if services === null} -

Loading indexers and download clients…

- {:else} - -

- {#if !canFetch} - No download client is switched on, so nothing can be downloaded. - {:else if !canSearch} - No indexer is switched on, so nothing new can be found. - {:else} - Music can be searched for and downloaded. - {/if} -

- -
- - {#each indexers as service (service.code)} - - {:else} -

No indexer yet. Prowlarr is the usual choice.

- {/each} - {#if searchingExchanges.length > 0} -

- Also searched: {searchingExchanges.map((s) => s.label).join(', ')}, under download clients. -

- {/if} - {@render addMenu('search', 'Add indexer')} -
-
- -
- - {#each clients as service (service.code)} - - {:else} -

No download client yet.

- {/each} - {@render addMenu('download', 'Add download client')} -
-
- {/if} -
diff --git a/web/src/lib/components/AcquisitionCard.test.ts b/web/src/lib/components/AcquisitionCard.test.ts deleted file mode 100644 index 3db6448f..00000000 --- a/web/src/lib/components/AcquisitionCard.test.ts +++ /dev/null @@ -1,168 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { render, screen, fireEvent, waitFor, within } from '@testing-library/svelte'; -import type { AcquisitionService } from '#lib/api/admin.js'; - -vi.mock('#lib/api/admin.js', () => ({ - listAcquisitionServices: vi.fn(), - addAcquisitionService: vi.fn(), - updateAcquisitionService: vi.fn(), - removeAcquisitionService: vi.fn(), - testAcquisitionService: vi.fn(), - setAcquisitionSecret: vi.fn(), - clearAcquisitionSecret: vi.fn() -})); - -vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); - -import AcquisitionCard from './AcquisitionCard.svelte'; -import { - addAcquisitionService, - listAcquisitionServices, - setAcquisitionSecret, - testAcquisitionService, - updateAcquisitionService -} from '#lib/api/admin.js'; - -function service(over: Partial = {}): AcquisitionService { - return { - code: 'prowlarr', - kind: 'search', - protocol: 'prowlarr', - label: 'Prowlarr', - enabled: false, - base_url: '', - has_api_key: false, - has_password: false, - username: '', - category: '', - timeout_ms: 60000, - serves_search: true, - serves_downloads: true, - serves_enrichment: true, - local_path: '/downloads', - remote_path: '', - searches_per_hour: 120, - usable: false, - ...over - }; -} - -const exchange = (over: Partial = {}) => - service({ - code: 'exchange', - kind: 'download', - protocol: 'exchange', - label: 'FabledExchange', - category: 'minstrel', - searches_per_hour: 0, - ...over - }); - -afterEach(() => vi.clearAllMocks()); - -async function renderCard(services: AcquisitionService[]) { - vi.mocked(listAcquisitionServices).mockResolvedValue(services); - render(AcquisitionCard); - await screen.findByTestId('acquisition-health'); -} - -describe('AcquisitionCard', () => { - test('starts empty: nothing is seeded', async () => { - await renderCard([]); - expect(screen.getByText(/no indexer yet/i)).toBeInTheDocument(); - expect(screen.getByText(/no download client yet/i)).toBeInTheDocument(); - expect(screen.getByTestId('acquisition-health')).toHaveTextContent(/nothing can be downloaded/i); - }); - - test('adding offers the types each list takes', async () => { - await renderCard([]); - await fireEvent.click(screen.getByRole('button', { name: 'Add indexer' })); - const indexerChoices = within(screen.getByTestId('add-search')); - expect(indexerChoices.getByRole('button', { name: /prowlarr/i })).toBeInTheDocument(); - expect(indexerChoices.getByRole('button', { name: /torznab feed/i })).toBeInTheDocument(); - expect(indexerChoices.queryByRole('button', { name: /qbittorrent/i })).toBeNull(); - - vi.mocked(addAcquisitionService).mockResolvedValue(service()); - await fireEvent.click(indexerChoices.getByRole('button', { name: /prowlarr/i })); - expect(addAcquisitionService).toHaveBeenCalledWith('prowlarr'); - - await fireEvent.click(screen.getByRole('button', { name: 'Add download client' })); - const clientChoices = within(screen.getByTestId('add-download')); - expect(clientChoices.getByRole('button', { name: /qbittorrent/i })).toBeInTheDocument(); - expect(clientChoices.getByRole('button', { name: /fabledexchange/i })).toBeInTheDocument(); - }); - - test('an exchange sits under download clients with a switch per job; others have none', async () => { - await renderCard([ - service({ code: 'qbittorrent', kind: 'download', protocol: 'qbittorrent', label: 'qBittorrent' }), - exchange() - ]); - const clients = within(screen.getByTestId('download-clients')); - expect(clients.getAllByTestId('acquisition-service')).toHaveLength(2); - // Only the exchange carries the job switches. - expect(clients.getAllByTestId('exchange-jobs')).toHaveLength(1); - expect(clients.getByRole('checkbox', { name: /downloads what is grabbed/i })).toBeChecked(); - expect(clients.getByRole('checkbox', { name: /searches its dht index/i })).toBeChecked(); - expect(clients.getByRole('checkbox', { name: /checks a release/i })).toBeChecked(); - // qBittorrent asks for a username and a password; the exchange for an optional key. - expect(clients.getByText('Username')).toBeInTheDocument(); - expect(clients.getByText(/save password/i)).toBeInTheDocument(); - expect(clients.getByText(/save key/i)).toBeInTheDocument(); - }); - - test('an exchange searching is named under indexers too', async () => { - await renderCard([exchange({ usable: true, enabled: true, base_url: 'http://ex' })]); - expect(within(screen.getByTestId('indexers')).getByText(/also searched: fabledexchange/i)).toBeInTheDocument(); - expect(screen.getByTestId('acquisition-health')).toHaveTextContent(/searched for and downloaded/i); - }); - - test('switching a job off and saving sends the switches', async () => { - const ex = exchange({ base_url: 'http://ex' }); - await renderCard([ex]); - await fireEvent.click(screen.getByRole('checkbox', { name: /checks a release/i })); - vi.mocked(updateAcquisitionService).mockResolvedValue({ ...ex, serves_enrichment: false }); - await fireEvent.click(screen.getByRole('button', { name: /^save$/i })); - await waitFor(() => expect(updateAcquisitionService).toHaveBeenCalled()); - const [code, sent] = vi.mocked(updateAcquisitionService).mock.calls[0]; - expect(code).toBe('exchange'); - expect(sent).toMatchObject({ serves_enrichment: false, serves_search: true, serves_downloads: true }); - }); - - test('a refused setting is shown beside the form in the server’s words', async () => { - await renderCard([service({ base_url: 'http://p' })]); - await fireEvent.input(screen.getByRole('textbox', { name: /^name$/i }), { - target: { value: 'Mine' } - }); - vi.mocked(updateAcquisitionService).mockRejectedValue({ - code: 'invalid_setting', - message: 'Address: start with http:// or https://.', - status: 400 - }); - await fireEvent.click(screen.getByRole('button', { name: /^save$/i })); - expect(await screen.findByTestId('acquisition-problem')).toHaveTextContent( - 'Address: start with http:// or https://.' - ); - }); - - test('Test shows what the server found, and needs an address', async () => { - await renderCard([service({ code: 'a' }), service({ code: 'b', label: 'Second', base_url: 'http://p' })]); - const [noAddress, addressed] = screen.getAllByRole('button', { name: 'Test' }); - expect(noAddress).toBeDisabled(); - vi.mocked(testAcquisitionService).mockResolvedValue({ - ok: false, - detail: 'the API key was refused: HTTP 401. Check it on the service’s own settings page.' - }); - await fireEvent.click(addressed); - expect(testAcquisitionService).toHaveBeenCalledWith('b'); - expect(await screen.findByTestId('acquisition-test-result')).toHaveTextContent(/key was refused/i); - }); - - test('a key is sent write-only and never shown back', async () => { - await renderCard([service({ base_url: 'http://p', has_api_key: true })]); - expect(screen.getByLabelText(/replace the api key/i)).toHaveValue(''); - await fireEvent.input(screen.getByLabelText(/replace the api key/i), { target: { value: 'k2' } }); - vi.mocked(setAcquisitionSecret).mockResolvedValue(undefined); - await fireEvent.click(screen.getByRole('button', { name: /save key/i })); - await waitFor(() => expect(setAcquisitionSecret).toHaveBeenCalledWith('prowlarr', 'api-key', 'k2')); - }); -}); diff --git a/web/src/lib/components/AcquisitionServiceRow.svelte b/web/src/lib/components/AcquisitionServiceRow.svelte deleted file mode 100644 index 48f7df91..00000000 --- a/web/src/lib/components/AcquisitionServiceRow.svelte +++ /dev/null @@ -1,394 +0,0 @@ - - -
-
- -
- {#each jobs as job (job)} - {job} - {/each} - {#if service.usable} - On - {:else if service.enabled} - On, but no address - {:else} - Off - {/if} -
-
- -

{blurb[service.protocol]}

- -
- Connection -
- - - - - {#if isExchange} -
- What it does here - - - -
- {/if} - - {#if isQbit} - - {/if} - - {#if fetches} - - - - {/if} - - {#if searches} - - {/if} - - -
- -
- -
- - - {#if secretSet} - - {/if} -
- - {#if service.protocol === 'prowlarr'} - Prowlarr shows it under Settings → General. - {:else if isExchange} - Optional: only if your FabledExchange asks for one. Give Minstrel a key of its own there, - so it can be revoked on its own. - {:else if service.protocol === 'torznab'} - Optional: only if the feed asks for one. - {:else} - Empty if your WebUI does not ask for one. - {/if} - {secretSet ? 'One is saved.' : ''} Never shown again once saved. - -
-
- - {#if problem} -

{problem}

- {/if} - {#if result} -

- {result.detail} -

- {/if} - -
- {#if confirmRemove} - - Remove {service.label}? Minstrel forgets its address and credentials. - Nothing on the service itself changes. - - - - {:else} - - - - {/if} -
-
diff --git a/web/src/lib/components/CollectionRail.svelte b/web/src/lib/components/CollectionRail.svelte new file mode 100644 index 00000000..8cb40464 --- /dev/null +++ b/web/src/lib/components/CollectionRail.svelte @@ -0,0 +1,21 @@ + + + diff --git a/web/src/lib/components/DiscoverResultCard.svelte b/web/src/lib/components/DiscoverResultCard.svelte deleted file mode 100644 index 5458efb5..00000000 --- a/web/src/lib/components/DiscoverResultCard.svelte +++ /dev/null @@ -1,202 +0,0 @@ - - - - -
-
- {#if imageUrl && !imageFailed} - (imageFailed = true)} - /> - {:else} - - {/if} -
- -
-
{title}
- {#if subtitle} -
{subtitle}
- {/if} - {#if attribution} -
- {attribution} -
- {/if} -
- {#if cardState === 'kept'} - Kept - {:else if cardState === 'snoozed'} - Not right now - {/if} -
-
- -
- {#if cardState === 'snoozed'} - - {:else if cardState === 'requestable'} -
- - {#if onSnooze} - - - {/if} -
- {:else if cardState === 'kept'} - - {:else} - - {/if} -
-
- - diff --git a/web/src/lib/components/DiscoverResultCard.test.ts b/web/src/lib/components/DiscoverResultCard.test.ts deleted file mode 100644 index e22ad06f..00000000 --- a/web/src/lib/components/DiscoverResultCard.test.ts +++ /dev/null @@ -1,188 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { render, screen, fireEvent } from '@testing-library/svelte'; -import DiscoverResultCard from './DiscoverResultCard.svelte'; - -afterEach(() => vi.clearAllMocks()); - -describe('DiscoverResultCard', () => { - test('requestable state renders Request button and calls onRequest on click', async () => { - const onRequest = vi.fn(); - render(DiscoverResultCard, { - props: { - kind: 'artist', - title: 'Boards of Canada', - state: 'requestable', - onRequest, - }, - }); - // aria-label includes the title, so the accessible name is - // "Request Boards of Canada". - const btn = screen.getByRole('button', { - name: /request boards of canada/i, - }); - expect(btn).not.toBeDisabled(); - await fireEvent.click(btn); - expect(onRequest).toHaveBeenCalledOnce(); - }); - - test('kept state renders disabled "In library" button + Kept pill with role=status', () => { - render(DiscoverResultCard, { - props: { - kind: 'album', - title: 'Music Has The Right To Children', - state: 'kept', - }, - }); - expect(screen.getByRole('button', { name: /in library/i })).toBeDisabled(); - // Kept pill is exposed as a status region so SR users hear it when it - // appears. - const status = screen.getByRole('status'); - expect(status).toBeInTheDocument(); - expect(status.textContent).toMatch(/kept/i); - }); - - test('requested state renders disabled "Requested" button', () => { - render(DiscoverResultCard, { - props: { - kind: 'track', - title: 'Roygbiv', - state: 'requested', - }, - }); - expect(screen.getByRole('button', { name: /requested/i })).toBeDisabled(); - }); - - test('does not call onRequest when state is not requestable', async () => { - const onRequest = vi.fn(); - render(DiscoverResultCard, { - props: { - kind: 'album', - title: 'Geogaddi', - state: 'kept', - onRequest, - }, - }); - // Button is disabled — the test still verifies onRequest isn't called even - // if a click slips through (jsdom does not enforce :disabled at fireEvent - // level). - const btn = screen.getByRole('button'); - await fireEvent.click(btn); - expect(onRequest).not.toHaveBeenCalled(); - }); - - // The layout discipline (reserved badge slot + bottom-anchored actions) is - // not asserted here: vitest's @testing-library/svelte renderer does not - // inject the component's scoped diff --git a/web/src/lib/components/StatusPill.test.ts b/web/src/lib/components/StatusPill.test.ts deleted file mode 100644 index 7f4d7632..00000000 --- a/web/src/lib/components/StatusPill.test.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { describe, expect, test } from 'vitest'; -import { render, screen } from '@testing-library/svelte'; -import StatusPill from './StatusPill.svelte'; - -// Labels in Minstrel's voice, and their semantic tones. -// Each row is the contract: input status -> rendered label + data-tone attr. -const CASES = [ - { status: 'pending', label: 'Awaiting review', tone: 'warning' }, - { status: 'approved', label: 'Approved · downloading', tone: 'info' }, - { status: 'completed', label: 'Kept', tone: 'success' }, - { status: 'rejected', label: 'Set aside', tone: 'error' }, - { status: 'failed', label: "Couldn't add", tone: 'error' }, -] as const; - -describe('StatusPill', () => { - for (const { status, label, tone } of CASES) { - test(`${status} renders "${label}" with tone=${tone}`, () => { - render(StatusPill, { props: { status } }); - const pill = screen.getByText(label); - expect(pill).toBeInTheDocument(); - expect(pill.getAttribute('data-tone')).toBe(tone); - expect(pill.getAttribute('data-status')).toBe(status); - }); - } -}); diff --git a/web/src/lib/components/SuggestionFeed.svelte b/web/src/lib/components/SuggestionFeed.svelte deleted file mode 100644 index 5f00ee18..00000000 --- a/web/src/lib/components/SuggestionFeed.svelte +++ /dev/null @@ -1,212 +0,0 @@ - - -
-
-

Suggested for you

-

Out-of-library artists drawn from what you've liked and played.

-
- - {#if !query.isPending && suggestions.length === 0} - -

- {snoozes.length === 0 - ? 'Listen to something or like an artist to start getting suggestions.' - : "Nothing new right now — the artists you've parked are below."} -

- {:else if suggestions.length > 0} -
- {#each suggestions.filter(visible) as s (s.mbid)} - onRequest(s)} - onSnooze={() => onSnooze(s)} - onUnsnooze={() => onUnsnooze(s.mbid, s.name)} - /> - {/each} -
- {/if} - - - {#if snoozes.length > 0} -
-

- Not right now -

-

- These come back on their own. Nothing here counts against your taste profile. -

-
    - {#each snoozes as snoozed (snoozed.mbid)} -
  • -
    -
    {snoozed.name}
    -
    - Back {returnsIn(snoozed.snoozed_until)} -
    -
    - -
  • - {/each} -
-
- {/if} -
diff --git a/web/src/lib/components/SuggestionFeed.test.ts b/web/src/lib/components/SuggestionFeed.test.ts deleted file mode 100644 index f22e6c4f..00000000 --- a/web/src/lib/components/SuggestionFeed.test.ts +++ /dev/null @@ -1,292 +0,0 @@ -import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; -import { render, screen, fireEvent } from '@testing-library/svelte'; -import { mockQuery } from '../../test-utils/query'; - -const invalidateMock = vi.fn(); -vi.mock('@tanstack/svelte-query', async (orig) => { - const actual = (await orig()) as Record; - return { ...actual, useQueryClient: () => ({ invalidateQueries: invalidateMock }) }; -}); - -vi.mock('#lib/api/suggestions.js', () => ({ - createSuggestionsQuery: vi.fn(), - createSnoozesQuery: vi.fn(), - snoozeSuggestion: vi.fn().mockResolvedValue(undefined), - unsnoozeSuggestion: vi.fn().mockResolvedValue(undefined) -})); - -vi.mock('#lib/api/requests.js', () => ({ - createRequest: vi.fn().mockResolvedValue({}) -})); - -const pushToastMock = vi.fn(); -vi.mock('#lib/stores/toast.svelte.js', () => ({ - pushToast: (...args: unknown[]) => pushToastMock(...args) -})); - -import SuggestionFeed from './SuggestionFeed.svelte'; -import { - createSuggestionsQuery, - createSnoozesQuery, - snoozeSuggestion, - unsnoozeSuggestion -} from '#lib/api/suggestions.js'; -import { createRequest } from '#lib/api/requests.js'; -import type { ArtistSuggestion, SuggestionSnooze } from '#lib/api/types.js'; - -const oneSeed: ArtistSuggestion = { - mbid: 'mb1', - name: 'Outsider', - score: 1.0, - attribution: [ - { artist_id: 'a1', name: 'Seed', contribution: 0.9, is_liked: true, play_count: 0 } - ] -}; - -const twoSeeds: ArtistSuggestion = { - mbid: 'mb2', - name: 'Outsider Two', - score: 2.0, - attribution: [ - { artist_id: 'a1', name: 'A', contribution: 0.8, is_liked: true, play_count: 0 }, - { artist_id: 'a2', name: 'B', contribution: 0.5, is_liked: false, play_count: 3 } - ] -}; - -const threeSeeds: ArtistSuggestion = { - mbid: 'mb3', - name: 'Outsider Three', - score: 3.0, - attribution: [ - { artist_id: 'a1', name: 'X', contribution: 0.9, is_liked: true, play_count: 0 }, - { artist_id: 'a2', name: 'Y', contribution: 0.6, is_liked: false, play_count: 5 }, - { artist_id: 'a3', name: 'Z', contribution: 0.3, is_liked: false, play_count: 1 } - ] -}; - -/** Days from now as an RFC3339 string, for snooze fixtures. */ -function inDays(n: number): string { - return new Date(Date.now() + n * 86_400_000).toISOString(); -} - -function setSuggestions(data: ArtistSuggestion[]) { - (createSuggestionsQuery as ReturnType).mockReturnValue(mockQuery({ data })); -} - -function setSnoozes(data: SuggestionSnooze[]) { - (createSnoozesQuery as ReturnType).mockReturnValue(mockQuery({ data })); -} - -beforeEach(() => setSnoozes([])); -afterEach(() => vi.clearAllMocks()); - -describe('SuggestionFeed', () => { - test('renders one card per suggestion', () => { - setSuggestions([oneSeed, twoSeeds]); - render(SuggestionFeed); - expect(screen.getByText('Outsider')).toBeInTheDocument(); - expect(screen.getByText('Outsider Two')).toBeInTheDocument(); - }); - - test('attribution copy: 1 seed → "Because you liked X."', () => { - setSuggestions([oneSeed]); - render(SuggestionFeed); - expect(screen.getByText(/because you liked seed\./i)).toBeInTheDocument(); - }); - - test('attribution copy: 2 seeds → "Because you liked A and played B."', () => { - setSuggestions([twoSeeds]); - render(SuggestionFeed); - expect(screen.getByText(/because you liked a and played b\./i)).toBeInTheDocument(); - }); - - test('attribution copy: 3 seeds → Oxford comma', () => { - setSuggestions([threeSeeds]); - render(SuggestionFeed); - expect(screen.getByText(/because you liked x, played y, and played z\./i)).toBeInTheDocument(); - }); - - test('Request button calls createRequest with artist-kind body', async () => { - setSuggestions([oneSeed]); - render(SuggestionFeed); - await fireEvent.click(screen.getByRole('button', { name: /request outsider/i })); - expect(createRequest).toHaveBeenCalledWith({ - kind: 'artist', - artist_mbid: 'mb1', - artist_name: 'Outsider' - }); - expect(invalidateMock).toHaveBeenCalled(); - }); - - test('empty state when data is []', () => { - setSuggestions([]); - render(SuggestionFeed); - expect(screen.getByText(/listen to something or like an artist/i)).toBeInTheDocument(); - }); -}); - -describe('SuggestionFeed snooze (#2375)', () => { - test('snooze sends BOTH mbid and name — the server 400s without the name', async () => { - setSuggestions([oneSeed]); - render(SuggestionFeed); - await fireEvent.click(screen.getByRole('button', { name: /not right now/i })); - expect(snoozeSuggestion).toHaveBeenCalledWith('mb1', 'Outsider'); - }); - - test('the card stays in place showing Undo, rather than vanishing', async () => { - setSuggestions([oneSeed]); - render(SuggestionFeed); - await fireEvent.click(screen.getByRole('button', { name: /not right now/i })); - // Still on screen — the disappearance happens on refetch, not under the - // cursor. - expect(screen.getByText('Outsider')).toBeInTheDocument(); - expect(screen.getByRole('button', { name: /bring outsider back/i })).toBeInTheDocument(); - expect(screen.getByRole('status')).toHaveTextContent('Not right now'); - }); - - test('a failed snooze reverts the card and says so', async () => { - (snoozeSuggestion as ReturnType).mockRejectedValueOnce(new Error('offline')); - setSuggestions([oneSeed]); - render(SuggestionFeed); - await fireEvent.click(screen.getByRole('button', { name: /not right now/i })); - // Back to requestable — a snooze that silently did nothing would leave - // the user tapping it again. - expect(screen.getByRole('button', { name: /request outsider/i })).toBeInTheDocument(); - expect(pushToastMock).toHaveBeenCalledWith("Couldn't hide Outsider", 'error'); - }); - - test('undo on the card calls unsnoozeSuggestion', async () => { - setSuggestions([oneSeed]); - render(SuggestionFeed); - await fireEvent.click(screen.getByRole('button', { name: /not right now/i })); - await fireEvent.click(screen.getByRole('button', { name: /bring outsider back/i })); - expect(unsnoozeSuggestion).toHaveBeenCalledWith('mb1'); - }); - - test('the snoozed list is the way back once the card is gone', async () => { - // Deck empty, one parked artist: exactly the state after a refetch. - setSuggestions([]); - setSnoozes([ - { mbid: 'mbX', name: 'Parked', snoozed_until: inDays(90), created_at: inDays(0) } - ]); - render(SuggestionFeed); - expect(screen.getByRole('heading', { name: /not right now/i })).toBeInTheDocument(); - expect(screen.getByText('Parked')).toBeInTheDocument(); - await fireEvent.click(screen.getByRole('button', { name: /bring parked back now/i })); - expect(unsnoozeSuggestion).toHaveBeenCalledWith('mbX'); - }); - - test('a 404 from unsnooze is not surfaced as an error', async () => { - (unsnoozeSuggestion as ReturnType).mockRejectedValueOnce({ status: 404 }); - setSuggestions([]); - setSnoozes([ - { mbid: 'mbX', name: 'Parked', snoozed_until: inDays(90), created_at: inDays(0) } - ]); - render(SuggestionFeed); - await fireEvent.click(screen.getByRole('button', { name: /bring parked back now/i })); - // Already-unsnoozed IS the end state the user asked for. - expect(pushToastMock).not.toHaveBeenCalled(); - }); - - test('a non-404 unsnooze failure does surface', async () => { - (unsnoozeSuggestion as ReturnType).mockRejectedValueOnce({ status: 500 }); - setSuggestions([]); - setSnoozes([ - { mbid: 'mbX', name: 'Parked', snoozed_until: inDays(90), created_at: inDays(0) } - ]); - render(SuggestionFeed); - await fireEvent.click(screen.getByRole('button', { name: /bring parked back now/i })); - expect(pushToastMock).toHaveBeenCalledWith("Couldn't bring Parked back", 'error'); - }); - - test('return time reads as a relative phrase, not a calendar date', () => { - setSuggestions([]); - setSnoozes([ - { mbid: 'a', name: 'Quarter', snoozed_until: inDays(90), created_at: inDays(0) }, - { mbid: 'b', name: 'Fortnight', snoozed_until: inDays(14), created_at: inDays(0) } - ]); - render(SuggestionFeed); - expect(screen.getByText(/back in about 3 months/i)).toBeInTheDocument(); - expect(screen.getByText(/back in 14 days/i)).toBeInTheDocument(); - }); - - // Pins the days→months seam: with a threshold above the divisor (45 vs 30), - // no day count could round to one month without hitting the days branch - // first, and the singular branch would be dead code. Kept in lockstep with - // SuggestionSnoozeRefTest on Android. - test('the days-to-months boundary sits at 30 days, so "a month" is reachable', () => { - setSuggestions([]); - setSnoozes([ - { mbid: 'a', name: 'JustUnder', snoozed_until: inDays(29), created_at: inDays(0) }, - { mbid: 'b', name: 'JustOver', snoozed_until: inDays(30), created_at: inDays(0) } - ]); - render(SuggestionFeed); - expect(screen.getByText(/back in 29 days/i)).toBeInTheDocument(); - expect(screen.getByText(/back in about a month/i)).toBeInTheDocument(); - }); - - test('no snoozed section when nothing is parked', () => { - setSuggestions([oneSeed]); - setSnoozes([]); - render(SuggestionFeed); - expect(screen.queryByRole('heading', { name: /not right now/i })).not.toBeInTheDocument(); - }); - - // An empty deck has two causes now, and the advice differs. Telling someone - // who parked everything to go listen to music would be wrong. - test('empty-deck copy distinguishes "no signal" from "you parked them all"', () => { - setSuggestions([]); - setSnoozes([ - { mbid: 'a', name: 'Parked', snoozed_until: inDays(90), created_at: inDays(0) } - ]); - render(SuggestionFeed); - expect(screen.getByText(/nothing new right now/i)).toBeInTheDocument(); - expect(screen.queryByText(/listen to something or like an artist/i)).not.toBeInTheDocument(); - }); -}); - -describe('SuggestionFeed taste-tag reason (#2377)', () => { - const tagged = (over: Partial = {}): ArtistSuggestion => ({ - ...oneSeed, - mbid: 'mbT', - name: 'Tag Match', - ...over - }); - - test('matched tags replace seed attribution as the reason line', () => { - setSuggestions([tagged({ matched_tags: ['shoegaze', 'dream pop'] })]); - render(SuggestionFeed); - expect( - screen.getByText(/matches your taste in shoegaze and dream pop\./i) - ).toBeInTheDocument(); - // The graph-adjacency line is superseded — describing the music beats - // describing the graph when we can do both. - expect(screen.queryByText(/because you liked/i)).not.toBeInTheDocument(); - }); - - test('one matched tag reads in the singular', () => { - setSuggestions([tagged({ matched_tags: ['shoegaze'] })]); - render(SuggestionFeed); - expect(screen.getByText(/matches your taste in shoegaze\./i)).toBeInTheDocument(); - }); - - test('three matched tags use an Oxford comma, matching the seed copy', () => { - setSuggestions([tagged({ matched_tags: ['a', 'b', 'c'] })]); - render(SuggestionFeed); - expect(screen.getByText(/matches your taste in a, b, and c\./i)).toBeInTheDocument(); - }); - - // The common case: most candidates have no cached tags, and the card must - // still explain itself rather than going blank. - test('no matched tags falls back to seed attribution', () => { - setSuggestions([oneSeed]); - render(SuggestionFeed); - expect(screen.getByText(/because you liked seed\./i)).toBeInTheDocument(); - }); - - test('an empty matched_tags array also falls back, not to an empty line', () => { - setSuggestions([tagged({ matched_tags: [] })]); - render(SuggestionFeed); - expect(screen.getByText(/because you liked seed\./i)).toBeInTheDocument(); - }); -}); diff --git a/web/src/lib/components/WantCandidates.svelte b/web/src/lib/components/WantCandidates.svelte deleted file mode 100644 index 751638fd..00000000 --- a/web/src/lib/components/WantCandidates.svelte +++ /dev/null @@ -1,181 +0,0 @@ - - -
- {#if query.isPending} -

Loading what the last search found…

- {:else if query.isError} -

Couldn't load the releases.

- {:else if items.length === 0} -

- Nothing kept from a search yet. Search now, or wait for the schedule. -

- {:else} -

- {plural(items.length, 'release', 'releases')} found {relativeTime(items[0].searched_at)}, best first. -

-
    - {#each items as c (c.key)} -
  • -
    -
    {c.name}
    -
    {meta(c)}
    -
    - - {c.downloading ? 'Downloading' : verdictLabel[c.verdict]} - - {c.reason} -
    -
    -
    - {#if c.verdict === 'blocked'} - - {:else if !c.downloading} - - {#if c.info_hash} - - {/if} - {/if} -
    -
  • - {/each} -
- {/if} -
diff --git a/web/src/lib/components/WantCandidates.test.ts b/web/src/lib/components/WantCandidates.test.ts deleted file mode 100644 index 3c7fcecf..00000000 --- a/web/src/lib/components/WantCandidates.test.ts +++ /dev/null @@ -1,90 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; -import { mockQuery } from '../../test-utils/query'; -import type { Candidate } from '#lib/api/types.js'; - -vi.mock('#lib/api/wanted.js', () => ({ - createCandidatesQuery: vi.fn(), - grabCandidate: vi.fn().mockResolvedValue({ added: true, refused: false, gone: false, unconfigured: false, kept: false, replaced: false }), - blockCandidate: vi.fn().mockResolvedValue(undefined), - unblockRelease: vi.fn().mockResolvedValue(undefined) -})); -vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); - -import WantCandidates from './WantCandidates.svelte'; -import { blockCandidate, createCandidatesQuery, grabCandidate, unblockRelease } from '#lib/api/wanted.js'; -import { pushToast } from '#lib/stores/toast.svelte.js'; - -function cand(key: string, extra: Partial = {}): Candidate { - return { - key, - name: `Daft Punk - Random Access Memories (2013) [${key}]`, - info_hash: key, - indexer: 'Knaben', - size_bytes: 450_000_000, - seeders: 12, - files_count: 13, - format: 'FLAC', - quality: 'lossless', - year: 2013, - verdict: 'grab', - reason: 'The album, FLAC.', - reasons: [], - folded_from: [], - searched_at: new Date().toISOString(), - downloading: false, - ...extra - }; -} - -afterEach(() => vi.clearAllMocks()); - -function setup(items: Candidate[]) { - vi.mocked(createCandidatesQuery).mockReturnValue( - mockQuery({ data: items }) as unknown as ReturnType - ); - return render(WantCandidates, { wantId: 'w1', title: 'Random Access Memories' }); -} - -describe('WantCandidates', () => { - test('every release shows its verdict and why', () => { - setup([ - cand('aaaa'), - cand('bbbb', { verdict: 'rejected', reason: 'This is a drumless edition (“drumless”).' }) - ]); - expect(screen.getAllByTestId('candidate-row')).toHaveLength(2); - expect(screen.getByText('Would take')).toBeTruthy(); - expect(screen.getByText('Not the album')).toBeTruthy(); - expect(screen.getByText(/drumless edition/)).toBeTruthy(); - expect(screen.getAllByText(/FLAC · 450 MB · 12 seeders · Knaben/)).toHaveLength(2); - }); - - test('a release the verdict would take is grabbed; any other is grabbed anyway', async () => { - setup([cand('aaaa'), cand('bbbb', { verdict: 'review', reason: 'Nobody is sharing it right now.' })]); - await fireEvent.click(screen.getByRole('button', { name: /^grab daft punk.*\[aaaa\]/i })); - await waitFor(() => expect(grabCandidate).toHaveBeenCalledWith('w1', 'aaaa', false)); - await waitFor(() => expect(pushToast).toHaveBeenCalledWith('Taken for Random Access Memories.', 'info')); - await fireEvent.click(screen.getByRole('button', { name: /^grab anyway daft punk.*\[bbbb\]/i })); - await waitFor(() => expect(grabCandidate).toHaveBeenCalledWith('w1', 'bbbb', true)); - }); - - test('a release can be set aside, and allowed again', async () => { - setup([cand('aaaa'), cand('bbbb', { verdict: 'blocked', reason: 'You set this release aside.' })]); - await fireEvent.click(screen.getByRole('button', { name: /never take .*\[aaaa\]/i })); - await waitFor(() => expect(blockCandidate).toHaveBeenCalledWith('w1', 'aaaa')); - await fireEvent.click(screen.getByRole('button', { name: /allow .*\[bbbb\] again/i })); - await waitFor(() => expect(unblockRelease).toHaveBeenCalledWith('bbbb')); - expect(screen.queryByRole('button', { name: /grab.*\[bbbb\]/i })).toBeNull(); - }); - - test('the release downloading says so and offers nothing', () => { - setup([cand('aaaa', { downloading: true })]); - expect(screen.getByText('Downloading')).toBeTruthy(); - expect(screen.queryByRole('button', { name: /grab/i })).toBeNull(); - }); - - test('before any search, it says how to get one', () => { - setup([]); - expect(screen.getByText(/nothing kept from a search yet/i)).toBeTruthy(); - }); -}); diff --git a/web/src/lib/components/wanted/BlockList.svelte b/web/src/lib/components/wanted/BlockList.svelte deleted file mode 100644 index c30a43a4..00000000 --- a/web/src/lib/components/wanted/BlockList.svelte +++ /dev/null @@ -1,95 +0,0 @@ - - -{#if blocks.isPending} -

Reading the blocked releases…

-{:else if blocks.isError && !blocks.isFetchNextPageError} -

Couldn't load the blocked releases.

-{:else if items.length === 0} -
- -

No releases are blocked.

-

- A release you set aside, or one an import found wasn't the album, lands here so it's never - taken again. -

-
-{:else} -
    - {#each items as b (b.info_hash)} -
  • -
    -
    {b.name || b.info_hash}
    - {#if b.want_title} -
    For {b.want_artist} – {b.want_title}
    - {/if} -
    {by(b)} {relativeTime(b.blocked_at)}{until(b)}
    - {#if b.detail} -

    {b.detail}

    - {/if} -
    - -
  • - {/each} -
- - blocks.fetchNextPage()} - /> -{/if} diff --git a/web/src/lib/components/wanted/BlockList.test.ts b/web/src/lib/components/wanted/BlockList.test.ts deleted file mode 100644 index ae4817e0..00000000 --- a/web/src/lib/components/wanted/BlockList.test.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; -import { mockInfiniteQuery } from '../../../test-utils/query'; -import type { ReleaseBlock } from '#lib/api/types.js'; - -vi.mock('#lib/api/wanted.js', () => ({ - createReleaseBlocksQuery: vi.fn(), - unblockRelease: vi.fn().mockResolvedValue(undefined) -})); -vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); - -import BlockList from './BlockList.svelte'; -import { createReleaseBlocksQuery, unblockRelease } from '#lib/api/wanted.js'; - -const block: ReleaseBlock = { - info_hash: 'abc123', - name: 'Daft Punk - Random Access Memories (Drumless Edition)', - reason: 'import', - detail: 'The album tag says another version.', - blocked_at: new Date(Date.now() - 3_600_000).toISOString(), - expires_at: null, - want_title: 'Random Access Memories', - want_artist: 'Daft Punk' -}; - -afterEach(() => vi.clearAllMocks()); - -function setup(items: ReleaseBlock[]) { - vi.mocked(createReleaseBlocksQuery).mockReturnValue( - mockInfiniteQuery({ pages: [{ items, total: items.length, limit: 50, offset: 0 }] }) as unknown as ReturnType< - typeof createReleaseBlocksQuery - > - ); - return render(BlockList); -} - -describe('BlockList', () => { - test('a blocked release says why and can be allowed again', async () => { - setup([block]); - expect(screen.getByText(/refused on import/i)).toBeTruthy(); - expect(screen.getByText(/another version/)).toBeTruthy(); - expect(screen.getByText(/for daft punk – random access memories/i)).toBeTruthy(); - await fireEvent.click(screen.getByRole('button', { name: /allow .* again/i })); - await waitFor(() => expect(unblockRelease).toHaveBeenCalledWith('abc123')); - }); - - test('a release given up on says until when it is held back', () => { - setup([{ ...block, reason: 'stalled', detail: 'Nobody was sharing it.', expires_at: '2026-10-17T12:00:00Z' }]); - const line = screen.getByText(/given up on: it would not finish/i).textContent ?? ''; - expect(line).toMatch(/held back until/); - }); - - test('with nothing blocked it says so', () => { - setup([]); - expect(screen.getByText(/no releases are blocked/i)).toBeTruthy(); - }); -}); diff --git a/web/src/lib/components/wanted/CatalogueArtistCard.svelte b/web/src/lib/components/wanted/CatalogueArtistCard.svelte deleted file mode 100644 index 9384c0d0..00000000 --- a/web/src/lib/components/wanted/CatalogueArtistCard.svelte +++ /dev/null @@ -1,46 +0,0 @@ - - - -
- -
-
-
-
-
-
{a.artist_name}
-
{a.held} / {a.albums} held
- {#if notes.length} -
{notes.join(' · ')}
- {/if} -
-
diff --git a/web/src/lib/components/wanted/CatalogueGrid.svelte b/web/src/lib/components/wanted/CatalogueGrid.svelte deleted file mode 100644 index e95e09b6..00000000 --- a/web/src/lib/components/wanted/CatalogueGrid.svelte +++ /dev/null @@ -1,72 +0,0 @@ - - -{#if library && !library.listed_at} -

- Your library's albums are being added to the catalogue. Albums whose tags don't name them are - matched on MusicBrainz first, by their artist, title and tracks, about one a second. -

-{/if} - -{#if !artists.isPending && (items.length > 0 || q)} -
- -
-{/if} - -{#if artists.isPending} -

Reading the catalogue…

-{:else if artists.isError && !artists.isFetchNextPageError} -

Couldn't load the catalogue.

-{:else if items.length === 0 && q} -

- No artist in the catalogue, or album of theirs, is named like '{q}'. -

-{:else if items.length === 0} -
- -

The catalogue is empty.

-

- Your library's albums land here once it's been scanned, as do the albums users request and - the releases of artists Minstrel follows. -

-
-{:else} - a.sort_name || a.artist_name} - hasMore={!!artists.hasNextPage} - onLoadMore={() => artists.fetchNextPage()} - > - {#snippet item(a: WantArtist)} - - {/snippet} - - - artists.fetchNextPage()} - /> -{/if} diff --git a/web/src/lib/components/wanted/CatalogueGrid.test.ts b/web/src/lib/components/wanted/CatalogueGrid.test.ts deleted file mode 100644 index 5c49cf87..00000000 --- a/web/src/lib/components/wanted/CatalogueGrid.test.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; -import { mockInfiniteQuery } from '../../../test-utils/query'; -import type { LibraryListing, WantArtist, WantArtistsPage } from '#lib/api/types.js'; - -vi.mock('#lib/api/wanted.js', () => ({ createWantArtistsQuery: vi.fn() })); - -import CatalogueGrid from './CatalogueGrid.svelte'; -import { createWantArtistsQuery } from '#lib/api/wanted.js'; - -const DAY = 24 * 3_600_000; -const ago = (ms: number) => new Date(Date.now() - ms).toISOString(); - -function artist(mbid: string, name: string, extra: Partial = {}): WantArtist { - return { - artist_mbid: mbid, - artist_name: name, - sort_name: name, - image_url: `/api/catalogue/art/artist/${mbid}`, - albums: 4, - held: 3, - wanted: 1, - missing: 0, - downloading: 0, - following: false, - ...extra - }; -} - -function setup( - items: WantArtist[] = [ - artist('blur', 'Blur', { following: true, downloading: 1 }), - artist('beatles', 'The Beatles', { sort_name: 'Beatles, The', albums: 13, held: 13, wanted: 0 }) - ], - library: LibraryListing = { listed_at: ago(DAY), unlisted: 0 } -) { - const page: WantArtistsPage = { - items, - total: items.length, - limit: 96, - offset: 0, - counts: { artists: items.length, wanted: 1, missing: 0, held: 16, upgrades: 0, following: 1, activity: 1, attention: 0, blocked: 0, unmatched: 0 }, - library - }; - vi.mocked(createWantArtistsQuery).mockReturnValue( - mockInfiniteQuery({ pages: [page] }) as unknown as ReturnType - ); - return render(CatalogueGrid); -} - -afterEach(() => vi.clearAllMocks()); - -describe('CatalogueGrid', () => { - test('each artist is a card: how many albums are held, and whether they are followed', () => { - setup(); - const cards = screen.getAllByTestId('catalogue-artist'); - expect(cards.map((c) => c.getAttribute('href'))).toEqual(['/collection/artist/blur', '/collection/artist/beatles']); - expect(cards[0].textContent?.replace(/\s+/g, ' ')).toMatch(/Blur 3 \/ 4 held Following · 1 downloading/); - const bar = screen.getByRole('progressbar', { name: '13 of 13 albums held' }); - expect(bar.getAttribute('aria-valuenow')).toBe('13'); - expect((bar.firstElementChild as HTMLElement).style.width).toBe('100%'); - }); - - test('the alphabet rail files an artist under their sort name', () => { - setup(); - expect(document.getElementById('alpha-B')?.textContent).toMatch(/Blur/); - }); - - test('the filter asks the server for artists by name or album', async () => { - setup(); - await fireEvent.input(screen.getByRole('searchbox', { name: /filter artists and albums/i }), { - target: { value: 'parklife' } - }); - await waitFor(() => expect(createWantArtistsQuery).toHaveBeenLastCalledWith('parklife')); - }); - - test('an empty catalogue says what lands there', () => { - setup([]); - expect(screen.getByText(/the catalogue is empty/i)).toBeTruthy(); - }); - - test('says while the library is being listed', () => { - setup([], { listed_at: null, unlisted: 0 }); - expect(screen.getByTestId('library-listing').textContent).toMatch(/being added to the catalogue/i); - }); -}); diff --git a/web/src/lib/components/wanted/CollectionRail.svelte b/web/src/lib/components/wanted/CollectionRail.svelte deleted file mode 100644 index 56a12be5..00000000 --- a/web/src/lib/components/wanted/CollectionRail.svelte +++ /dev/null @@ -1,33 +0,0 @@ - - - diff --git a/web/src/lib/components/wanted/DownloadList.svelte b/web/src/lib/components/wanted/DownloadList.svelte deleted file mode 100644 index ee3222d8..00000000 --- a/web/src/lib/components/wanted/DownloadList.svelte +++ /dev/null @@ -1,264 +0,0 @@ - - -{#each clientsDown as c (c.client)} -

- Can't reach the download client {c.client} since {relativeTime(c.since)}: {c.detail}. Its - downloads are left as they are until it answers. -

-{/each} - -{#if downloads.isPending} -

Reading the downloads…

-{:else if downloads.isError && !downloads.isFetchNextPageError} -

Couldn't load the downloads.

-{:else if items.length === 0} -
- -

Nothing has been downloaded yet.

-

- A release the search takes, or one you take from a want's Releases, shows here while it - downloads and once it's imported. -

-
-{:else} -
    - {#each items as d (d.id)} -
  • -
    -
    -
    - {d.want_artist ? `${d.want_artist} – ${d.want_title}` : d.name} -
    -
    - {d.name}{#if d.quality} · {d.quality}{/if} -
    -
    - - {label(d)} - -
    - - {#if !d.ended_at && (d.import_state === 'downloading' || d.import_state === '')} -
    -
    -
    - {/if} - - {#if running(d) && d.condition} -

    {condition(d.condition)}

    - {/if} - {#if ending(d)} -

    {ending(d)}

    - {/if} - {#if d.import_reason} -

    - {d.import_reason} -

    - {/if} - {#if d.evidence} -

    {counted(d.evidence)}

    - {/if} - {#if filed(d)} -

    {filed(d)}

    - {/if} - {#if seeded(d)} -

    {seeded(d)}

    - {/if} - {#if d.rehomed_from} -

    Taken for {d.rehomed_from}; it turned out to be this album.

    - {/if} -

    {when(d)}

    - - {#if running(d)} -
    - -
    - {:else if needsYou(d)} -
    - {#if d.import_state === 'review'} - - {:else} - - {/if} - -
    - {/if} -
  • - {/each} -
- - downloads.fetchNextPage()} - /> -{/if} diff --git a/web/src/lib/components/wanted/DownloadList.test.ts b/web/src/lib/components/wanted/DownloadList.test.ts deleted file mode 100644 index 605b42e2..00000000 --- a/web/src/lib/components/wanted/DownloadList.test.ts +++ /dev/null @@ -1,160 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; -import { mockInfiniteQuery } from '../../../test-utils/query'; -import type { ClientDown, Download } from '#lib/api/types.js'; - -vi.mock('#lib/api/wanted.js', () => ({ - createDownloadsQuery: vi.fn(), - importAnyway: vi.fn().mockResolvedValue(undefined), - retryImport: vi.fn().mockResolvedValue(undefined), - refuseDownload: vi.fn().mockResolvedValue(undefined), - abandonDownload: vi.fn().mockResolvedValue(undefined) -})); -vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); - -import DownloadList from './DownloadList.svelte'; -import { abandonDownload, createDownloadsQuery, importAnyway, refuseDownload, retryImport } from '#lib/api/wanted.js'; - -const ago = (ms: number) => new Date(Date.now() - ms).toISOString(); - -function download(id: string, extra: Partial = {}): Download { - return { - id, - want_id: 'w1', - want_title: 'Random Access Memories', - want_artist: 'Daft Punk', - name: 'Daft Punk - Random Access Memories (2013) [FLAC]', - indexer: 'Prowlarr', - client: 'qbittorrent', - quality: 'lossless', - origin: 'schedule', - grabbed_at: ago(3_600_000), - ended_at: null, - end_reason: '', - progress: 0, - import_state: 'downloading', - import_reason: '', - imported_at: null, - imported_files: 0, - destination: '', - files: [], - skipped: 0, - end_detail: '', - seeding: false, - ratio: 0, - seed_released_at: null, - condition: null, - ...extra - }; -} - -afterEach(() => vi.clearAllMocks()); - -function setup(items: Download[], clientsDown: ClientDown[] = []) { - // The Activity page carries the clients not answering beside its items. - const page = { items, total: items.length, limit: 50, offset: 0, clients_down: clientsDown }; - vi.mocked(createDownloadsQuery).mockReturnValue( - mockInfiniteQuery({ pages: [page] }) as unknown as ReturnType< - typeof createDownloadsQuery - > - ); - return render(DownloadList); -} - -describe('DownloadList', () => { - test('a running download shows how far it has got, and can be given up', async () => { - setup([download('d1', { progress: 0.375 })]); - expect(screen.getByTestId('download-state').textContent).toMatch(/downloading/i); - expect(screen.getByRole('progressbar', { name: /downloaded/i }).getAttribute('aria-valuenow')).toBe('37'); - expect(screen.queryByTestId('download-condition')).toBeNull(); - await fireEvent.click(screen.getByRole('button', { name: /give up/i })); - await waitFor(() => expect(abandonDownload).toHaveBeenCalledWith('d1')); - }); - - test('a stalled download says what is wrong and how often it was asked for again', () => { - setup([ - download('d1', { - progress: 0.4, - condition: { trigger: 'no_peers', detail: 'Nobody has shared it in 7 hours; 40% downloaded.', retries: 2, since: ago(1_200_000) } - }) - ]); - expect(screen.getByTestId('download-condition').textContent).toMatch( - /nobody has shared it in 7 hours; 40% downloaded\. asked for again 2 of 3 times/i - ); - }); - - test('a download given up on says why it ended', () => { - setup([ - download('d1', { - ended_at: ago(60_000), - end_reason: 'stalled', - end_detail: 'Taken 7 hours ago and never finished: nobody was sharing it. Held back until 17 Oct.' - }) - ]); - expect(screen.getByTestId('download-state').textContent).toMatch(/given up/i); - expect(screen.getByTestId('download-ending').textContent).toMatch(/nobody was sharing it/); - expect(screen.queryByRole('button')).toBeNull(); - }); - - test('a download client that does not answer is named above the list', () => { - setup([download('d1')], [{ client: 'qbittorrent', detail: 'connection refused', since: ago(600_000) }]); - expect(screen.getByTestId('client-down').textContent).toMatch(/can't reach the download client qbittorrent .*connection refused/i); - }); - - test('a doubtful download says why and can be imported anyway or refused', async () => { - setup([ - download('d1', { - import_state: 'review', - import_reason: 'Only the lengths agree. A person decides.', - evidence: { files: 13, tracks: 13, identified: 0, recordings: 0, lengths: 13, titles: 0, fingerprinted: false } - }) - ]); - expect(screen.getByTestId('download-state').textContent).toMatch(/needs you/i); - expect(screen.getByTestId('download-reason').textContent).toMatch(/a person decides/i); - expect(screen.getByText(/13 files against 13 tracks · no AcoustID · 13 lengths agree/)).toBeTruthy(); - await fireEvent.click(screen.getByRole('button', { name: /import anyway/i })); - await waitFor(() => expect(importAnyway).toHaveBeenCalledWith('d1')); - await fireEvent.click(screen.getByRole('button', { name: /refuse/i })); - await waitFor(() => expect(refuseDownload).toHaveBeenCalledWith('d1')); - }); - - test('a failed import can be tried again', async () => { - setup([download('d1', { import_state: 'failed', import_reason: 'The library folder is not writable.' })]); - expect(screen.queryByRole('button', { name: /import anyway/i })).toBeNull(); - await fireEvent.click(screen.getByRole('button', { name: /try again/i })); - await waitFor(() => expect(retryImport).toHaveBeenCalledWith('d1')); - }); - - test('an imported download says what was filed where', () => { - setup([ - download('d1', { - ended_at: ago(60_000), - end_reason: 'imported', - import_state: 'imported', - imported_at: ago(60_000), - imported_files: 12, - skipped: 1, - destination: '/music/Daft Punk/Random Access Memories (2013)' - }) - ]); - expect(screen.getByTestId('download-state').textContent).toMatch(/imported/i); - expect(screen.getByText(/12 tracks filed, 1 already held into \/music\/Daft Punk/)).toBeTruthy(); - expect(screen.queryByRole('button')).toBeNull(); - }); - - test('an imported download says how its seeding goes', () => { - const imported = { ended_at: ago(60_000), end_reason: 'imported', import_state: 'imported' as const }; - setup([ - download('d1', { ...imported, seeding: true, ratio: 0.8 }), - download('d2', { ...imported, seeding: true, ratio: 2.1, seed_released_at: ago(60_000) }) - ]); - const lines = screen.getAllByTestId('download-seeding').map((e) => e.textContent); - expect(lines[0]).toMatch(/seeding: share ratio 0\.80/i); - expect(lines[1]).toMatch(/seeded, then removed/i); - }); - - test('with nothing downloaded it says what lands here', () => { - setup([]); - expect(screen.getByText(/nothing has been downloaded yet/i)).toBeTruthy(); - }); -}); diff --git a/web/src/lib/components/wanted/FollowList.svelte b/web/src/lib/components/wanted/FollowList.svelte deleted file mode 100644 index f5702487..00000000 --- a/web/src/lib/components/wanted/FollowList.svelte +++ /dev/null @@ -1,118 +0,0 @@ - - -{#if follows.isPending} -

Reading who's followed…

-{:else if follows.isError} -

Couldn't load the followed artists.

-{:else if (follows.data ?? []).length === 0} -

No artists are followed. Follow one from its artist page.

-{:else} -
    - {#each follows.data ?? [] as f (f.artist_mbid)} -
  • - -
    -
    - {#if f.artist_id} - {f.artist_name} - {:else} - {f.artist_name} - {/if} -
    -
    - {plural(f.open_wants, 'album wanted', 'albums wanted')} · - {f.checked_at ? `checked ${relativeTime(f.checked_at)}` : 'not checked yet'} -
    -
    - - -
  • - {/each} -
-{/if} diff --git a/web/src/lib/components/wanted/FollowList.test.ts b/web/src/lib/components/wanted/FollowList.test.ts deleted file mode 100644 index 7ae23c05..00000000 --- a/web/src/lib/components/wanted/FollowList.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; -import { mockQuery } from '../../../test-utils/query'; -import type { Follow, ReleaseProfilesResponse } from '#lib/api/types.js'; - -vi.mock('#lib/api/wanted.js', () => ({ - createFollowsQuery: vi.fn(), - createReleaseProfilesQuery: vi.fn(), - putFollow: vi.fn().mockResolvedValue({ following: true }), - deleteFollow: vi.fn().mockResolvedValue(undefined) -})); -vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); - -import FollowList from './FollowList.svelte'; -import { createFollowsQuery, createReleaseProfilesQuery, deleteFollow, putFollow } from '#lib/api/wanted.js'; - -const ago = (ms: number) => new Date(Date.now() - ms).toISOString(); - -const follow: Follow = { - artist_mbid: 'blur', - artist_name: 'Blur', - artist_id: 'art-1', - profile_id: 'p-std', - profile_name: 'Standard', - followed_at: ago(10 * 86_400_000), - checked_at: ago(3 * 3_600_000), - open_wants: 2, - image_url: '' -}; - -const profiles: ReleaseProfilesResponse = { - profiles: [ - { id: 'p-std', name: 'Standard', primary_types: ['Album'], secondary_types: [], is_default: true, follow_count: 1 }, - { id: 'p-live', name: 'With live', primary_types: ['Album'], secondary_types: ['Live'], is_default: false, follow_count: 0 } - ], - primary_types: ['Album'], - secondary_types: ['Live'] -}; - -afterEach(() => vi.clearAllMocks()); - -function setup(follows: Follow[] = [follow]) { - vi.mocked(createFollowsQuery).mockReturnValue( - mockQuery({ data: follows }) as unknown as ReturnType - ); - vi.mocked(createReleaseProfilesQuery).mockReturnValue( - mockQuery({ data: profiles }) as unknown as ReturnType - ); - return render(FollowList); -} - -describe('FollowList', () => { - test('lists artists; the profile can change and the artist be unfollowed', async () => { - setup(); - expect(screen.getAllByTestId('follow-row')).toHaveLength(1); - expect(screen.getByText(/2 albums wanted/)).toBeTruthy(); - - const select = screen.getByLabelText(/profile for blur/i) as HTMLSelectElement; - expect(select.value).toBe('p-std'); - await fireEvent.change(select, { target: { value: 'p-live' } }); - await waitFor(() => - expect(putFollow).toHaveBeenCalledWith('blur', { artist_name: 'Blur', profile_id: 'p-live' }) - ); - - await fireEvent.click(screen.getByRole('button', { name: /unfollow blur/i })); - await waitFor(() => expect(deleteFollow).toHaveBeenCalledWith('blur')); - }); - - test('with no follows it says where to follow from', () => { - setup([]); - expect(screen.getByText(/follow one from its artist page/i)).toBeTruthy(); - }); -}); diff --git a/web/src/lib/components/wanted/UnmatchedList.svelte b/web/src/lib/components/wanted/UnmatchedList.svelte deleted file mode 100644 index aaaeb93d..00000000 --- a/web/src/lib/components/wanted/UnmatchedList.svelte +++ /dev/null @@ -1,150 +0,0 @@ - - -{#if unmatched.isPending} -

Reading the unmatched albums…

-{:else if unmatched.isError} -

Couldn't load the unmatched albums.

-{:else} - {#if toMatch.length === 0} -
- -

Every album in your library is matched.

-

- An album no MusicBrainz album matches by its artist, title and tracks lands here, to be - matched by hand or left off. -

-
- {:else} -

- No MusicBrainz album matched {toMatch.length === 1 ? 'this album' : `these ${toMatch.length} albums`} - by artist, title and tracks, so {toMatch.length === 1 ? "it isn't" : "they aren't"} fetched back if - {toMatch.length === 1 ? 'its' : 'their'} files go missing. Match each to its MusicBrainz album, or - leave it off if MusicBrainz doesn't have it. -

-
    - {#each toMatch as u (u.album_id)} -
  • -
    -
    - {u.title} -
    {u.artist} · {plural(u.tracks, 'track', 'tracks')}
    -

    {u.detail}

    -
    -
    - - -
    -
    - {#if open === u.album_id} - onMatch(u, ref)} /> - {/if} -
  • - {/each} -
- {/if} - - {#if leftOff.length > 0} -
-

Left off

-

- Kept in the library, never matched again, and not fetched back if their files go missing. -

-
    - {#each leftOff as u (u.album_id)} -
  • -
    - {u.title} -
    - {u.artist} · {plural(u.tracks, 'track', 'tracks')} · left off {relativeTime(u.decided_at)} -
    -
    - -
  • - {/each} -
-
- {/if} -{/if} diff --git a/web/src/lib/components/wanted/UnmatchedList.test.ts b/web/src/lib/components/wanted/UnmatchedList.test.ts deleted file mode 100644 index 2245246c..00000000 --- a/web/src/lib/components/wanted/UnmatchedList.test.ts +++ /dev/null @@ -1,109 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; -import type { CatalogueSearchResult, UnmatchedAlbum } from '#lib/api/types.js'; - -vi.mock('#lib/api/wanted.js', async () => { - const { mockQuery } = await import('../../../test-utils/query'); - return { - createUnmatchedQuery: vi.fn(() => mockQuery({ data: [] })), - matchUnmatched: vi.fn().mockResolvedValue({ - release_group_mbid: 'rg-1', - title: 'Songs for the Deaf', - artist: 'Queens of the Stone Age', - agree: 9, - tracks: 10 - }), - leaveUnmatchedOff: vi.fn().mockResolvedValue(undefined), - putUnmatchedBack: vi.fn().mockResolvedValue(undefined) - }; -}); -vi.mock('#lib/api/catalogue.js', () => ({ createCatalogueSearchQuery: vi.fn() })); -vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); - -import UnmatchedList from './UnmatchedList.svelte'; -import { mockQuery } from '../../../test-utils/query'; -import { createUnmatchedQuery, leaveUnmatchedOff, matchUnmatched, putUnmatchedBack } from '#lib/api/wanted.js'; -import { createCatalogueSearchQuery } from '#lib/api/catalogue.js'; -import { pushToast } from '#lib/stores/toast.svelte.js'; - -const DAY = 24 * 3_600_000; - -function album(id: string, title: string, artist: string, extra: Partial = {}): UnmatchedAlbum { - return { - album_id: id, - title, - artist, - detail: `MusicBrainz has no album called "${title}" by ${artist}.`, - tracks: 10, - left_off: false, - decided_at: new Date(Date.now() - 2 * DAY).toISOString(), - ...extra - }; -} - -const result: CatalogueSearchResult = { - mbid: 'rg-1', - name: 'Songs for the Deaf', - secondary_text: 'Queens of the Stone Age · 2002 · Album', - image_url: '', - artist_mbid: 'qotsa', - album_mbid: '', - in_library: false, - requested: false -}; - -afterEach(() => vi.clearAllMocks()); - -function setup(items: UnmatchedAlbum[], results: CatalogueSearchResult[] = [result]) { - vi.mocked(createUnmatchedQuery).mockReturnValue( - mockQuery({ data: items }) as unknown as ReturnType - ); - vi.mocked(createCatalogueSearchQuery).mockReturnValue( - mockQuery({ data: results }) as unknown as ReturnType - ); - return render(UnmatchedList); -} - -describe('UnmatchedList', () => { - test('each unmatched album says why, with its tracks, and links to the album', () => { - setup([album('al-1', 'Dial Tone', 'Annadale', { tracks: 1 })]); - const row = screen.getByTestId('unmatched-row'); - expect(row.textContent).toMatch(/Annadale · 1 track/); - expect(row.textContent).toMatch(/no album called "Dial Tone"/); - expect(screen.getByRole('link', { name: 'Dial Tone' }).getAttribute('href')).toBe('/albums/al-1'); - }); - - test('Match searches MusicBrainz for the album by its own title and artist, and matches a result', async () => { - setup([album('al-2', 'Songs for the Deaf', 'Queens of the Stone Age')]); - await fireEvent.click(screen.getByRole('button', { name: /match…/i })); - expect(createCatalogueSearchQuery).toHaveBeenLastCalledWith('Songs for the Deaf Queens of the Stone Age', 'album'); - await fireEvent.click(screen.getByRole('button', { name: /match to songs for the deaf/i })); - await waitFor(() => expect(matchUnmatched).toHaveBeenCalledWith('al-2', 'rg-1')); - await waitFor(() => expect(pushToast).toHaveBeenCalledWith(expect.stringMatching(/9 of 10 tracks agree/))); - }); - - test('a pasted MusicBrainz link matches the album', async () => { - setup([album('al-3', '+++', 'DROELOE')]); - await fireEvent.click(screen.getByRole('button', { name: /match…/i })); - const link = 'https://musicbrainz.org/release/11111111-0000-4000-8000-000000000003'; - await fireEvent.input(screen.getByRole('textbox', { name: /musicbrainz album link/i }), { target: { value: link } }); - await fireEvent.click(screen.getByRole('button', { name: /^match$/i })); - await waitFor(() => expect(matchUnmatched).toHaveBeenCalledWith('al-3', link)); - }); - - test("a local band's album is left off, and one left off can be put back", async () => { - setup([album('al-1', 'Dial Tone', 'Annadale'), album('al-4', 'Demo', 'Annadale', { left_off: true })]); - await fireEvent.click(screen.getByRole('button', { name: /leave it off/i })); - await waitFor(() => expect(leaveUnmatchedOff).toHaveBeenCalledWith('al-1')); - const leftOff = screen.getByTestId('left-off-row'); - expect(leftOff.textContent).toMatch(/left off 2d ago/); - await fireEvent.click(screen.getByRole('button', { name: /put back/i })); - await waitFor(() => expect(putUnmatchedBack).toHaveBeenCalledWith('al-4')); - }); - - test('with every album matched it says so, and still lists those left off', () => { - setup([album('al-4', 'Demo', 'Annadale', { left_off: true })]); - expect(screen.getByText(/every album in your library is matched/i)).toBeTruthy(); - expect(screen.getByTestId('left-off')).toBeTruthy(); - }); -}); diff --git a/web/src/lib/components/wanted/UnmatchedMatch.svelte b/web/src/lib/components/wanted/UnmatchedMatch.svelte deleted file mode 100644 index b09059fc..00000000 --- a/web/src/lib/components/wanted/UnmatchedMatch.svelte +++ /dev/null @@ -1,106 +0,0 @@ - - -
-
{ - e.preventDefault(); - asked = text.trim(); - }} - > - - -
- - {#if search.isPending && asked} -

Asking MusicBrainz…

- {:else if search.isError} -

Couldn't reach MusicBrainz. A pasted link works the same.

- {:else if asked && results.length === 0} -

MusicBrainz has no album like that.

- {:else if results.length > 0} -
    - {#each results as r (r.mbid)} -
  • -
    -
    {r.name}
    -
    {r.secondary_text}
    -
    - MusicBrainz - -
  • - {/each} -
- {/if} - -
{ - e.preventDefault(); - if (link.trim()) onMatch(link.trim()); - }} - > - - -
-
diff --git a/web/src/lib/components/wanted/WantList.svelte b/web/src/lib/components/wanted/WantList.svelte deleted file mode 100644 index 65838a73..00000000 --- a/web/src/lib/components/wanted/WantList.svelte +++ /dev/null @@ -1,308 +0,0 @@ - - -{#if artist && head} -
-
- -
-
- ← Catalogue -

{head.artist_name}

-

- {head.held} of {plural(head.albums, 'album', 'albums')} held{head.wanted ? ` · ${head.wanted} wanted` : ''}{head.missing - ? ` · ${head.missing} with files missing` - : ''}{head.downloading ? ` · ${head.downloading} downloading` : ''} -

-
- - {#if head.artist_id} - Open in the library - {/if} -
-
-
-{/if} - -{#if !artist && !wants.isPending && (items.length > 0 || q)} -
- -
-{/if} - -{#if wants.isPending} -

Reading the wanted list…

-{:else if wants.isError && !wants.isFetchNextPageError} -

Couldn't load {artist ? "this artist's albums" : 'the wanted list'}.

-{:else if items.length === 0 && q} -

- Nothing on this list has an artist or album named like '{q}'. -

-{:else if items.length === 0} -
- -

- {#if artist}None of this artist's albums are in the catalogue. - {:else if list === 'upgrade'}No album is wanted for a better copy. - {:else}Nothing is wanted.{/if} -

-

- {#if list === 'upgrade'} - An album you have below the quality set under - Acquisition, or with a video - rip among its tracks, lands here. - {:else} - A request you approve, a new release from a followed artist, or an album whose files went - missing lands here. - {/if} -

-
-{:else} -
- {#each sections as a, i (a.key + ':' + i)} -
- {#if !artist}

- - {#if a.mbid} - {a.name} - {:else} - {a.name} - {/if} - - {plural(a.wants.length, 'album', 'albums')} -

{/if} -
    - {#each a.wants as w (w.id)} -
  • -
    - -
    -
    - {#if w.album_id && w.held_at} - {w.title} - {:else} - {w.title} - {/if} - {#if kind(w)} · {kind(w)}{/if} -
    -
    - {why(w)} · {w.held_at ? `held ${relativeTime(w.held_at)}` : `wanted ${relativeTime(w.wanted_at)}`} -
    - {#if w.held_at && w.upgrade} -
    {upgradeLine(w)}
    - {/if} - {#if searchable(w)} -
    - {wantStatus(w)} - {#if needsYou(w)} - Decide on Activity - {/if} -
    - {:else if arrived(w)} -
    {arrived(w)}
    - {/if} -
    -
    - - {#if searchable(w)} - - - {/if} - -
    -
    - {#if shown === w.id} -
    - -
    - {/if} - {#if open === w.id && searchable(w)} -
    - -
    - {/if} -
  • - {/each} -
-
- {/each} -
- - wants.fetchNextPage()} - /> -{/if} diff --git a/web/src/lib/components/wanted/WantList.test.ts b/web/src/lib/components/wanted/WantList.test.ts deleted file mode 100644 index 662f2ed0..00000000 --- a/web/src/lib/components/wanted/WantList.test.ts +++ /dev/null @@ -1,345 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; -import { mockInfiniteQuery, mockQuery } from '../../../test-utils/query'; -import type { LibraryListing, Want, WantGrab, WantsPage } from '#lib/api/types.js'; - -vi.mock('#lib/api/wanted.js', async () => { - const { mockQuery } = await import('../../../test-utils/query'); - return { - // The artist page's FollowControl. - createFollowQuery: vi.fn(() => mockQuery({ data: { following: true } })), - createReleaseProfilesQuery: vi.fn(() => mockQuery({ data: { profiles: [], primary_types: [], secondary_types: [] } })), - putFollow: vi.fn(), - deleteFollow: vi.fn(), - createWantsQuery: vi.fn(), - removeWant: vi.fn().mockResolvedValue(undefined), - searchWant: vi.fn().mockResolvedValue({ - outcome: 'nothing', considered: 0, best_release: '', best_verdict: '', narrowed: [] - }), - createCandidatesQuery: vi.fn(), - createWantTracksQuery: vi.fn(), - grabCandidate: vi.fn(), - blockCandidate: vi.fn(), - unblockRelease: vi.fn() - }; -}); -vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); - -import WantList from './WantList.svelte'; -import { pushToast } from '#lib/stores/toast.svelte.js'; -import { createCandidatesQuery, createWantTracksQuery, createWantsQuery, removeWant, searchWant } from '#lib/api/wanted.js'; -import type { WantState, WantTracks } from '#lib/api/types.js'; - -const DAY = 24 * 3_600_000; -const ago = (ms: number) => new Date(Date.now() - ms).toISOString(); - -function want(id: string, extra: Partial = {}): Want { - return { - id, - release_group_mbid: `rg-${id}`, - title: 'The Ballad of Darren', - artist_mbid: 'blur', - artist_name: 'Blur', - artist_id: 'art-1', - primary_type: 'Album', - secondary_types: [], - year: '2023', - reason: 'follow', - wanted_at: ago(2 * DAY), - held_at: null, - image_url: '', - ...extra - }; -} - -function page( - items: Want[], - library: LibraryListing = { listed_at: ago(DAY), unlisted: 0 }, - artist?: WantsPage['artist'] -): WantsPage { - return { - artist, - items, - total: items.length, - limit: 50, - offset: 0, - counts: { artists: 2, wanted: 2, missing: 1, held: 4, upgrades: 0, following: 1, activity: 1, attention: 0, blocked: 0, unmatched: 0 }, - library - }; -} - -function grab(extra: Partial = {}): WantGrab { - return { - id: 'g1', - name: 'Blur - Blur (1997) [FLAC]', - grabbed_at: ago(60_000), - origin: 'schedule', - quality: 'lossless', - live: true, - end_reason: '', - progress: 0, - import_state: '', - import_reason: '', - end_detail: '', - imported_at: null, - imported_files: 0, - ...extra - }; -} - -afterEach(() => vi.clearAllMocks()); - -function setup( - items: Want[] = [want('w1'), want('w2', { reason: 'request', requested_by: 'sam', title: 'Parklife' })], - state: WantState = 'wanted', - library?: LibraryListing, - tracks: WantTracks = { - source: 'musicbrainz', - edition: '2023 · CD · GB', - tracks: [ - { disc: 1, number: 1, title: 'The Ballad', length_ms: 214000, missing: false }, - { disc: 1, number: 2, title: 'St. Charles Square', length_ms: 3_725_000, missing: false } - ] - } -) { - vi.mocked(createWantsQuery).mockReturnValue( - mockInfiniteQuery({ pages: [page(items, library)] }) as unknown as ReturnType - ); - vi.mocked(createCandidatesQuery).mockReturnValue( - mockQuery({ data: [] }) as unknown as ReturnType - ); - vi.mocked(createWantTracksQuery).mockReturnValue( - mockQuery({ data: tracks }) as unknown as ReturnType - ); - return render(WantList, { list: state }); -} - -describe('WantList', () => { - test('asks for its own state', () => { - setup([], 'upgrade'); - expect(createWantsQuery).toHaveBeenCalledWith('upgrade', ''); - expect(screen.getByText(/no album is wanted for a better copy/i)).toBeTruthy(); - }); - - test('a want says why it is wanted', () => { - setup(); - const why = screen.getAllByTestId('want-why').map((e) => e.textContent ?? ''); - expect(why[0]).toMatch(/new from a followed artist/i); - expect(why[1]).toMatch(/requested by sam/i); - }); - - test('remove takes a want off the list', async () => { - setup(); - await fireEvent.click(screen.getByRole('button', { name: /stop wanting parklife/i })); - await waitFor(() => expect(removeWant).toHaveBeenCalledWith('w2')); - }); - - test('a held album can be taken off the list, and is not searched for', async () => { - setup([want('w1', { held_at: ago(DAY), album_id: 'al-1', reason: 'library' })], 'held'); - expect(screen.queryByRole('button', { name: /stop wanting|search for/i })).toBeNull(); - expect(screen.getByRole('link', { name: /the ballad of darren/i }).getAttribute('href')).toBe('/albums/al-1'); - expect(screen.getByTestId('want-why').textContent).toMatch(/in your library when the list began/i); - await fireEvent.click(screen.getByRole('button', { name: /take the ballad of darren off the list/i })); - await waitFor(() => expect(removeWant).toHaveBeenCalledWith('w1')); - }); - - test('an empty list explains what lands there', () => { - setup([]); - expect(screen.getByText(/nothing is wanted/i)).toBeTruthy(); - }); - - test('each open want says what its last search found', () => { - setup([ - want('w1'), - want('w2', { - title: 'Parklife', - search: { searched_at: ago(3_600_000), outcome: 'nothing', considered: 4, best_release: 'x', best_verdict: 'rejected', error: '' } - }), - want('w3', { title: 'Blur', grab: grab() }), - want('w4', { title: '13', grab: grab({ import_state: 'downloading', progress: 0.42 }) }) - ]); - const lines = screen.getAllByTestId('want-search').map((e) => e.textContent ?? ''); - expect(lines[0]).toMatch(/not searched yet/i); - expect(lines[1]).toMatch(/4 found, none of them this album/i); - expect(lines[2]).toMatch(/downloading blur - blur \(1997\) \[flac\], taken by the schedule/i); - expect(lines[3]).toMatch(/downloading 42%/i); - }); - - test('a download waiting on a person says why and points to Activity', () => { - setup([ - want('w1', { grab: grab({ import_state: 'review', import_reason: 'Only the lengths agree. A person decides.' }) }) - ]); - const line = screen.getByTestId('want-search'); - expect(line.textContent).toMatch(/needs you: only the lengths agree/i); - expect(screen.getByRole('link', { name: /decide on activity/i }).getAttribute('href')).toBe('/collection/activity'); - // A live download can't be searched for again. - expect((screen.getByRole('button', { name: /search for the ballad of darren now/i }) as HTMLButtonElement).disabled).toBe(true); - }); - - test('a refused download is named beside the search that follows it', () => { - setup([ - want('w1', { - search: { searched_at: ago(3_600_000), outcome: 'nothing', considered: 0, best_release: '', best_verdict: '', error: '' }, - grab: grab({ live: false, end_reason: 'refused', import_state: 'refused', import_reason: 'Another album.' }) - }) - ]); - expect(screen.getByTestId('want-search').textContent).toMatch(/last download refused: another album/i); - }); - - test('a download given up on is named beside the search that follows it', () => { - setup([ - want('w1', { - search: { searched_at: ago(3_600_000), outcome: 'waiting', considered: 4, best_release: '', best_verdict: '', error: '' }, - grab: grab({ live: false, end_reason: 'stalled', import_state: 'downloading', end_detail: 'Nobody was sharing it.' }) - }) - ]); - expect(screen.getByTestId('want-search').textContent).toMatch(/last download given up: nobody was sharing it/i); - }); - - test('a held album says how it arrived', () => { - setup( - [ - want('w1', { - held_at: ago(DAY), - album_id: 'al-1', - grab: grab({ live: false, end_reason: 'imported', import_state: 'imported', imported_at: ago(DAY), imported_files: 11 }) - }) - ], - 'held' - ); - expect(screen.getByTestId('want-arrived').textContent).toMatch(/imported 11 tracks .* from blur - blur/i); - }); - - test('Search runs one now and says what came of it', async () => { - setup(); - await fireEvent.click(screen.getByRole('button', { name: /search for parklife now/i })); - await waitFor(() => expect(searchWant).toHaveBeenCalledWith('w2')); - await waitFor(() => expect(pushToast).toHaveBeenCalledWith('Nothing found for Parklife.', 'info')); - }); - - test('Releases opens what the last search found', async () => { - setup(); - expect(screen.queryByTestId('want-candidates')).toBeNull(); - const toggle = screen.getByRole('button', { name: /releases found for parklife/i }); - await fireEvent.click(toggle); - expect(toggle.getAttribute('aria-expanded')).toBe('true'); - expect(screen.getByTestId('want-candidates')).toBeTruthy(); - expect(createCandidatesQuery).toHaveBeenCalledWith('w2'); - }); - - test('albums sit under their artist, as in the library', () => { - setup([ - want('w1', { artist_mbid: 'blur', artist_name: 'Blur', artist_id: 'art-1', title: 'Parklife' }), - want('w2', { artist_mbid: 'blur', artist_name: 'Blur', artist_id: 'art-1', title: '13' }), - want('w3', { artist_mbid: 'camel', artist_name: 'Camel', artist_id: undefined, title: 'Moonmadness' }) - ]); - const sections = screen.getAllByTestId('want-artist'); - expect(sections.map((s) => s.getAttribute('aria-label'))).toEqual(['Blur', 'Camel']); - expect(sections[0].querySelectorAll('[data-testid="want-row"]').length).toBe(2); - expect(sections[0].textContent).toMatch(/2 albums/); - expect(sections[1].textContent).toMatch(/1 album/); - // Each artist opens on their page in the catalogue. - expect(screen.getByRole('link', { name: 'Blur' }).getAttribute('href')).toBe('/collection/artist/blur'); - expect(screen.getByRole('link', { name: 'Camel' }).getAttribute('href')).toBe('/collection/artist/camel'); - }); - - test('Tracks shows the album\'s tracks, with the edition they come from', async () => { - setup(); - expect(screen.queryByTestId('want-tracks')).toBeNull(); - const toggle = screen.getByRole('button', { name: /tracks of parklife/i }); - await fireEvent.click(toggle); - expect(toggle.getAttribute('aria-expanded')).toBe('true'); - expect(createWantTracksQuery).toHaveBeenCalledWith('w2'); - const panel = screen.getByTestId('want-tracks'); - expect(panel.textContent).toMatch(/MusicBrainz's standard edition\s+·\s+2023 · CD · GB/); - const rows = screen.getAllByTestId('want-track').map((e) => e.textContent?.replace(/\s+/g, ' ').trim()); - expect(rows).toEqual(['1 The Ballad 3:34', '2 St. Charles Square 1:02:05']); - }); - - test('a held album\'s tracks are its own, a lost file marked', async () => { - setup([want('w1', { held_at: ago(DAY), album_id: 'al-1' })], 'held', undefined, { - source: 'library', - tracks: [ - { disc: 1, number: 1, title: 'The Ballad', length_ms: 214000, missing: true }, - { disc: 2, number: 1, title: 'Bonus', length_ms: 0, missing: false } - ] - }); - await fireEvent.click(screen.getByRole('button', { name: /tracks of the ballad of darren/i })); - const panel = screen.getByTestId('want-tracks'); - expect(panel.textContent).not.toMatch(/MusicBrainz/); - const rows = screen.getAllByTestId('want-track').map((e) => e.textContent?.replace(/\s+/g, ' ').trim()); - expect(rows).toEqual(['1-1 The Ballad file missing 3:34', '2-1 Bonus']); - }); - - test('the filter asks the server for the artists and albums it names', async () => { - setup(); - await fireEvent.input(screen.getByRole('searchbox', { name: /filter artists and albums/i }), { - target: { value: ' park ' } - }); - await waitFor(() => expect(createWantsQuery).toHaveBeenLastCalledWith('wanted', 'park')); - }); - - test("an artist's page lists every album of theirs under a header", () => { - vi.mocked(createWantsQuery).mockReturnValue( - mockInfiniteQuery({ - pages: [ - page([want('w1', { held_at: ago(DAY), album_id: 'al-1', title: 'Parklife' }), want('w2')], undefined, { - artist_mbid: 'blur', - artist_name: 'Blur', - sort_name: 'Blur', - artist_id: 'art-1', - image_url: '/art/blur', - albums: 2, - held: 1, - wanted: 1, - missing: 0, - downloading: 0, - following: true - }) - ] - }) as unknown as ReturnType - ); - vi.mocked(createCandidatesQuery).mockReturnValue( - mockQuery({ data: [] }) as unknown as ReturnType - ); - render(WantList, { artist: 'blur' }); - expect(createWantsQuery).toHaveBeenCalledWith('all', '', 'blur'); - const head = screen.getByTestId('catalogue-artist-head'); - expect(head.textContent).toMatch(/Blur/); - expect(screen.getByTestId('catalogue-artist-counts').textContent).toMatch(/1 of 2 albums held\s+·\s+1 wanted/); - expect(screen.getByRole('link', { name: /open in the library/i }).getAttribute('href')).toBe('/artists/art-1'); - expect(screen.getByRole('link', { name: /catalogue/i }).getAttribute('href')).toBe('/collection'); - // One artist: no filter, no artist headings, both albums. - expect(screen.queryByRole('searchbox')).toBeNull(); - expect(screen.getAllByTestId('want-row')).toHaveLength(2); - expect(screen.queryByRole('link', { name: 'Blur' })).toBeNull(); - }); - - test('an upgrade is a held album searched for a better copy', async () => { - setup( - [ - want('u1', { held_at: ago(DAY), reason: 'library', upgrade: 'quality', held_quality: 'MP3, about 192 kbps' }), - want('u2', { held_at: ago(DAY), reason: 'library', upgrade: 'rip', title: 'Humanz' }) - ], - 'upgrade' - ); - expect(createWantsQuery).toHaveBeenCalledWith('upgrade', ''); - const lines = screen.getAllByTestId('want-upgrade').map((e) => e.textContent ?? ''); - expect(lines[0]).toMatch(/you have mp3, about 192 kbps; looking for better/i); - expect(lines[1]).toMatch(/video rip; looking for a clean copy/i); - expect(screen.getAllByTestId('want-search')[0].textContent).toMatch(/not searched yet/i); - await fireEvent.click(screen.getByRole('button', { name: /search for humanz now/i })); - await waitFor(() => expect(searchWant).toHaveBeenCalledWith('u2')); - }); - - test('a held album not wanted for a better copy has no search', () => { - setup([want('h1', { held_at: ago(DAY), reason: 'library' })], 'held'); - expect(screen.queryByTestId('want-upgrade')).toBeNull(); - expect(screen.queryByRole('button', { name: /search for/i })).toBeNull(); - }); - - test('with no upgrades it says what would land here', () => { - setup([], 'upgrade'); - expect(screen.getByText(/no album is wanted for a better copy/i)).toBeTruthy(); - }); -}); diff --git a/web/src/lib/components/wanted/WantTracks.svelte b/web/src/lib/components/wanted/WantTracks.svelte deleted file mode 100644 index f57595cb..00000000 --- a/web/src/lib/components/wanted/WantTracks.svelte +++ /dev/null @@ -1,52 +0,0 @@ - - -
- {#if tracks.isPending} -

Reading the tracks…

- {:else if tracks.isError} -

- {away ? "MusicBrainz can't be reached, so the tracks can't be shown." : "Couldn't load the tracks."} -

- {:else if tracks.data.source === 'none' || list.length === 0} -

MusicBrainz lists no edition of this album, so there are no tracks to show.

- {:else} - {#if tracks.data.source === 'musicbrainz'} -

- MusicBrainz's standard edition{tracks.data.edition ? ` · ${tracks.data.edition}` : ''} -

- {/if} -
    - {#each list as t, i (i)} -
  1. - {position(t)} - - {t.title} - - {#if t.missing}file missing{/if} - {#if t.length_ms > 0} - {formatDuration(t.length_ms / 1000)} - {/if} -
  2. - {/each} -
- {/if} -
diff --git a/web/src/lib/wanted/downloadState.test.ts b/web/src/lib/wanted/downloadState.test.ts deleted file mode 100644 index 30f409bd..00000000 --- a/web/src/lib/wanted/downloadState.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { describe, expect, test } from 'vitest'; -import { liveDownloadLine } from './downloadState.js'; - -describe('liveDownloadLine', () => { - const g = (import_state: Parameters[0]['import_state'], import_reason = '') => ({ - import_state, - progress: 0.428, - import_reason - }); - - test('says each state of a running download', () => { - expect(liveDownloadLine(g('downloading'))).toBe('Downloading 42%'); - expect(liveDownloadLine(g('importing'))).toBe('Downloaded; checking it is the album and filing it'); - expect(liveDownloadLine(g('waiting'))).toBe('Downloaded; waiting to be imported'); - expect(liveDownloadLine(g('waiting', 'the library is busy'))).toBe('Downloaded; the library is busy'); - expect(liveDownloadLine(g('review', '9 files against 12 tracks'))).toBe('Needs you: 9 files against 12 tracks'); - expect(liveDownloadLine(g('failed', 'disk full'))).toBe('Import failed: disk full'); - }); - - test('has nothing to say before the client reports', () => { - expect(liveDownloadLine(g(''))).toBeNull(); - }); -}); diff --git a/web/src/lib/wanted/downloadState.ts b/web/src/lib/wanted/downloadState.ts deleted file mode 100644 index 72d19b7a..00000000 --- a/web/src/lib/wanted/downloadState.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { ImportState } from '#lib/api/types.js'; - -// A running download's state in a line: how far it has got, or what its -// import is doing or waiting on. The wanted list and the admin Overview say -// it the same way. Null before the download client has reported on it. -export function liveDownloadLine(g: { import_state: ImportState; progress: number; import_reason: string }): string | null { - switch (g.import_state) { - case 'downloading': - return `Downloading ${Math.floor(g.progress * 100)}%`; - case 'importing': - return 'Downloaded; checking it is the album and filing it'; - case 'waiting': - return `Downloaded; ${g.import_reason || 'waiting to be imported'}`; - case 'review': - return `Needs you: ${g.import_reason}`; - case 'failed': - return `Import failed: ${g.import_reason}`; - } - return null; -} diff --git a/web/src/lib/wanted/wantStatus.ts b/web/src/lib/wanted/wantStatus.ts deleted file mode 100644 index 363d825e..00000000 --- a/web/src/lib/wanted/wantStatus.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { relativeTime } from '#lib/utils/relativeTime.js'; -import type { SearchResult, Want } from '#lib/api/types.js'; -import { liveDownloadLine } from './downloadState.js'; - -// How a want reads in a line, wherever it is shown: the Collection's lists -// and the Missing page's folders. - -// What a Search an admin ran found, for its toast. -export function searched(w: Want, r: SearchResult): string { - let said: string; - switch (r.outcome) { - case 'grabbed': - said = `Took ${r.grabbed ?? r.best_release}.`; - break; - case 'waiting': - said = `Found ${r.considered}; nothing taken. Releases shows why.`; - break; - case 'nothing': - said = r.considered ? `Found ${r.considered}, none of them ${w.title}.` : `Nothing found for ${w.title}.`; - break; - default: - said = r.error ?? 'The search failed.'; - } - if (r.narrowed.length) said += ` ${r.narrowed.map((n) => n.label).join(', ')} didn't answer.`; - return said; -} - -// A download still running, in a line: how far it has got, or what its -// import is doing or waiting on. -export function downloading(w: Want): string | null { - const g = w.grab; - if (!g?.live) return null; - const line = liveDownloadLine(g); - // While it downloads or files, the line names the release it took. - if (line) return g.import_state === 'downloading' || g.import_state === 'importing' ? `${line} · ${g.name}` : line; - const who = g.origin === 'operator' ? 'taken by you' : 'taken by the schedule'; - return `Downloading ${g.name}, ${who} ${relativeTime(g.grabbed_at)}`; -} - -// A download waiting on a person: an import to review, or one that failed. -export const needsYou = (w: Want) => - !!w.grab?.live && (w.grab.import_state === 'review' || w.grab.import_state === 'failed'); - -// What the schedule last did about a want, in a line. -export function wantStatus(w: Want): string { - const live = downloading(w); - if (live) return live; - const s = w.search; - let said = 'Not searched yet'; - if (s) { - const when = `Searched ${relativeTime(s.searched_at)}`; - switch (s.outcome) { - case 'nothing': - said = s.considered ? `${when}: ${s.considered} found, none of them this album` : `${when}: nothing found`; - break; - case 'waiting': - said = `${when}: ${s.considered} found, nothing taken`; - break; - case 'grabbed': - said = `${when}: took ${s.best_release}`; - break; - default: - said = `${when}: ${s.error}`; - } - } - const g = w.grab; - if (g?.import_state === 'refused') said += `. Last download refused: ${g.import_reason}`; - else if (g && !g.live && ['stalled', 'failed', 'gone'].includes(g.end_reason) && g.end_detail) - said += `. Last download given up: ${g.end_detail}`; - return said; -} diff --git a/web/src/routes/admin/+layout.svelte b/web/src/routes/admin/+layout.svelte index 056db69f..ef749932 100644 --- a/web/src/routes/admin/+layout.svelte +++ b/web/src/routes/admin/+layout.svelte @@ -1,43 +1,21 @@
diff --git a/web/src/routes/admin/+page.svelte b/web/src/routes/admin/+page.svelte index 4e2c8a89..4a4430a7 100644 --- a/web/src/routes/admin/+page.svelte +++ b/web/src/routes/admin/+page.svelte @@ -5,19 +5,16 @@ import { relativeTime } from '#lib/utils/relativeTime.js'; import { formatBytes } from '#lib/utils/bytes.js'; import { QUEUES, countOf, queueLabel } from '#lib/admin/queues.js'; - import { liveDownloadLine } from '#lib/wanted/downloadState.js'; - import type { Download } from '#lib/api/types.js'; // The admin Overview answers "does anything need me?" first: a // verdict, then each queue that waits on you with how long its oldest has // waited, then the empty ones on one line. The queues themselves // are worked on their own pages; the Overview does not repeat them. // - // Below the verdict, the server's state: who is listening, what is - // downloading, what the library holds and how much room is left, and the - // server's version, uptime and recent errors. A download waiting on you, a - // download client not answering, or a library folder unreadable or nearly - // full also count in the verdict. + // Below the verdict, the server's state: who is listening, what the + // library holds and how much room is left, and the server's version, + // uptime and recent errors. A library folder unreadable or nearly full also + // counts in the verdict. const summaryStore = createAdminSummaryQuery(); const summaryQ = $derived($summaryStore); @@ -44,18 +41,6 @@ const statusProblems = $derived.by((): Problem[] => { if (!status) return []; const out: Problem[] = []; - const dl = status.downloads; - if (dl.needs_you > 0) { - out.push({ - key: 'downloads', - text: plural(dl.needs_you, 'download needs you', 'downloads need you'), - since: dl.needs_you_since, - href: '/collection/activity' - }); - } - for (const c of dl.clients_down) { - out.push({ key: `client:${c.client}`, text: `${c.client} isn't answering: ${c.detail}`, since: c.since, href: '/admin/acquisition' }); - } if (status.server.dirty) { out.push({ key: 'migration', text: `The database migration to version ${status.server.migration} stopped part way`, href: '/admin' }); } @@ -67,10 +52,6 @@ return out; }); - - // Worded as the wanted list words it; before the client reports, it is - // simply downloading, as the Downloads list says. - const downloadLine = (d: Download) => liveDownloadLine(d) ?? 'Downloading'; {pageTitle('Admin · Overview')} @@ -148,7 +129,6 @@

{:else if status} {@const l = status.listening} - {@const dl = status.downloads} {@const lib = status.library} {@const srv = status.server}
@@ -180,29 +160,6 @@

-
-

Downloads

-

- {dl.running === 0 ? 'Nothing downloading' : `${dl.running} downloading`}. - In the last day, {dl.imported_day} imported{dl.given_up_day ? ` and ${dl.given_up_day} given up` : ''}. - Activity -

- {#if dl.live.length > 0} -
    - {#each dl.live as d (d.id)} -
  • - - {d.want_title ? `${d.want_title} · ${d.want_artist}` : d.name} - - {downloadLine(d)} · {relativeTime(d.grabbed_at)} -
  • - {/each} -
- {/if} -
-

Library

diff --git a/web/src/routes/admin/acquisition/+page.svelte b/web/src/routes/admin/acquisition/+page.svelte deleted file mode 100644 index 1f306a69..00000000 --- a/web/src/routes/admin/acquisition/+page.svelte +++ /dev/null @@ -1,25 +0,0 @@ - - -{pageTitle('Admin · Acquisition')} - -

Acquisition

- - - {#snippet main()} - - - {/snippet} - {#snippet side()} -
- {/snippet} -
diff --git a/web/src/routes/admin/admin.test.ts b/web/src/routes/admin/admin.test.ts index a858a04f..b65de352 100644 --- a/web/src/routes/admin/admin.test.ts +++ b/web/src/routes/admin/admin.test.ts @@ -67,7 +67,7 @@ describe('admin/+layout.ts load gate', () => { // Importing at module load means the mocks attach to the same handles the // page binds against. import { createAdminStatusQuery, createAdminSummaryQuery, createScanStatusQuery } from '#lib/api/admin.js'; -import type { AdminStatus, AdminSummary, Download } from '#lib/api/types.js'; +import type { AdminStatus, AdminSummary } from '#lib/api/types.js'; import OverviewPage from './+page.svelte'; import AdminLayout from './+layout.svelte'; import { createRawSnippet } from 'svelte'; @@ -78,7 +78,6 @@ const empty = { count: 0, since: null }; function summary(over: Partial = {}): AdminSummary { return { - requests: empty, reports: empty, duplicates: empty, suspect: empty, @@ -103,9 +102,6 @@ const minutesAgo = (m: number) => new Date(Date.now() - m * 60_000).toISOString( function status(over: Partial = {}): AdminStatus { return { listening: { now: [], plays_day: 0, listeners_day: 0, last_play_at: null }, - downloads: { - running: 0, needs_you: 0, needs_you_since: null, imported_day: 0, given_up_day: 0, live: [], clients_down: [] - }, library: { tracks: 41_000, albums: 3_200, artists: 900, bytes: 1_400_000_000_000, disks: [{ path: '/music', total_bytes: 4_000_000_000_000, free_bytes: 2_000_000_000_000 }] @@ -127,24 +123,24 @@ describe('admin Overview page', () => { render(OverviewPage); expect(screen.getByTestId('verdict').textContent).toMatch(/nothing needs you/i); expect(screen.queryAllByTestId('needs-you')).toHaveLength(0); - expect(screen.getByTestId('all-clear').textContent).toMatch(/no requests, reports, duplicates, suspect sources, missing files/i); + expect(screen.getByTestId('all-clear').textContent).toMatch(/no reports, duplicates, suspect sources, missing files/i); }); test('each queue that waits on you is listed with how long its oldest has waited, and opens its page', () => { setupOverview(summary({ - requests: { count: 2, since: ago(3) }, + reports: { count: 2, since: ago(3) }, missing: { count: 1, since: ago(10) } })); render(OverviewPage); expect(screen.getByTestId('verdict').textContent).toMatch(/2 things need you/); const rows = screen.getAllByTestId('needs-you').map((r) => r.textContent?.replace(/\s+/g, ' ').trim()); - expect(rows).toEqual(['2 requests to decide oldest 3d ago Open', '1 missing file oldest 10d ago Open']); - expect(screen.getByRole('link', { name: /2 requests to decide/i }).getAttribute('href')).toBe('/admin/requests'); - expect(screen.getByTestId('all-clear').textContent).not.toMatch(/requests/); + expect(rows).toEqual(['2 broken tracks reported oldest 3d ago Open', '1 missing file oldest 10d ago Open']); + expect(screen.getByRole('link', { name: /2 broken tracks reported/i }).getAttribute('href')).toBe('/collection/reports'); + expect(screen.getByTestId('all-clear').textContent).not.toMatch(/reports/); }); - // The library's health queues live under Collection; the Overview - // still lists them, linking there. + // The health queues are worked under Collection; the Overview lists + // them, linking there. test('a library queue that waits on you opens its Collection page', () => { setupOverview(summary({ duplicates: { count: 5, since: ago(4) } })); render(OverviewPage); @@ -226,55 +222,22 @@ describe('admin Overview status', () => { expect(screen.getAllByTestId('server-error')[0].textContent).toMatch(/admin: summary[\s\S]*err=boom/); }); - test('a download waiting on you, a client not answering and a nearly full disk count in the verdict', () => { + test('a nearly full disk counts in the verdict', () => { setupOverview(summary(), vi.fn(), status({ - downloads: { - running: 1, needs_you: 2, needs_you_since: minutesAgo(90), imported_day: 4, given_up_day: 0, live: [], - clients_down: [{ client: 'qbittorrent', detail: 'connection refused', since: minutesAgo(30) }] - }, library: { tracks: 1, albums: 1, artists: 1, bytes: 1, disks: [{ path: '/music', total_bytes: 1_000, free_bytes: 10 }] } })); render(OverviewPage); - expect(screen.getByTestId('verdict').textContent).toMatch(/3 things need you/); + expect(screen.getByTestId('verdict').textContent).toMatch(/1 thing needs you/); const needs = screen.getAllByTestId('needs-you').map((li) => li.textContent?.replace(/\s+/g, ' ').trim()); - expect(needs).toEqual([ - '2 downloads need you since 1h ago Open', - "qbittorrent isn't answering: connection refused since 30m ago Open", - '/music is nearly full: 10 B free Open' - ]); - expect(textOf('downloads-line')).toBe('1 downloading. In the last day, 4 imported. Activity'); - }); - - // Worded as the wanted list words a download. - test('each live download says what it is doing, as the wanted list says it', () => { - const d = (id: string, import_state: Download['import_state'], import_reason = ''): Download => ({ - id, want_id: null, want_title: `Album ${id}`, want_artist: 'Blur', name: id, indexer: '', client: 'qbittorrent', - quality: 'FLAC', origin: 'schedule', grabbed_at: minutesAgo(5), ended_at: null, end_reason: '', progress: 0.42, - import_state, import_reason, imported_at: null, imported_files: 0, destination: '', files: [], skipped: 0, - end_detail: '', seeding: false, ratio: 0, seed_released_at: null, condition: null - }); - setupOverview(summary(), vi.fn(), status({ - downloads: { - running: 3, needs_you: 1, needs_you_since: minutesAgo(5), imported_day: 0, given_up_day: 0, clients_down: [], - live: [d('a', 'review', '9 files against 12 tracks'), d('b', 'downloading'), d('c', '')] - } - })); - render(OverviewPage); - const rows = screen.getAllByTestId('download-row').map((li) => li.textContent?.replace(/\s+/g, ' ').trim()); - expect(rows).toEqual([ - 'Album a · Blur Needs you: 9 files against 12 tracks · 5m ago', - 'Album b · Blur Downloading 42% · 5m ago', - 'Album c · Blur Downloading · 5m ago' - ]); + expect(needs).toEqual(['/music is nearly full: 10 B free Open']); }); }); describe('admin layout', () => { - test('the sections run down a rail, each queue carrying how many wait on you', () => { - setupOverview(summary({ requests: { count: 2, since: ago(1) }, duplicates: { count: 5, since: ago(4) } })); + test('the sections run down a rail', () => { const children = createRawSnippet(() => ({ render: () => '

body

' })); // eslint-disable-next-line @typescript-eslint/no-explicit-any render(AdminLayout, { children } as any); @@ -285,9 +248,7 @@ describe('admin layout', () => { ]); expect(links).toEqual([ ['/admin', 'Overview'], - ['/admin/requests', 'Requests 2'], ['/admin/library', 'Library'], - ['/admin/acquisition', 'Acquisition'], ['/admin/metadata', 'Metadata sources'], ['/admin/email', 'Email & network'], ['/admin/users', 'Users'], diff --git a/web/src/routes/admin/library/+page.svelte b/web/src/routes/admin/library/+page.svelte index 657e0dc8..e7b26da1 100644 --- a/web/src/routes/admin/library/+page.svelte +++ b/web/src/routes/admin/library/+page.svelte @@ -147,13 +147,7 @@ {#snippet main()} - - { - client.invalidateQueries({ queryKey: qk.scanStatus() }); - client.invalidateQueries({ queryKey: qk.searchSettings() }); - }} - /> + client.invalidateQueries({ queryKey: qk.scanStatus() })} /> -

- {:else if rows.length === 0} -

Nothing here.

- {:else} -
    - {#each rows as r (r.id)} - - {#snippet details()} - {#if r.kind === 'track' && r.album_title} -
    - Approving wants the album {r.album_title}. -
    - {/if} - {#if r.notes} -
    {r.notes}
    - {/if} - {/snippet} - {#snippet actions()} - {#if r.status === 'pending'} - {@const primary: RowAction = { - icon: Check, - label: 'Approve', - ariaLabel: `Approve ${requestName(r)}`, - onclick: () => onApprove(r) - }} - {@const secondary: RowAction[] = [ - ...(r.kind === 'artist' - ? [ - { - icon: SlidersHorizontal, - label: 'Approve on a profile', - ariaLabel: `Approve ${requestName(r)} on a profile`, - onclick: () => openProfile(r) - } - ] - : []), - { - icon: X, - label: 'Reject', - ariaLabel: `Reject ${requestName(r)}`, - onclick: () => openReject(r), - danger: true - } - ]} - - {/if} - {/snippet} -
    - {/each} -
- {/if} -
- - -

- The profile says which kinds of release following - {profileRow?.artist_name ?? 'this artist'} wants. Profiles are edited under Collection › Following. -

- - - -
- - -
-
- - -

- Notes are shown to the requester. -

- -
- - -
-
diff --git a/web/src/routes/admin/requests/requests.test.ts b/web/src/routes/admin/requests/requests.test.ts deleted file mode 100644 index a748ce10..00000000 --- a/web/src/routes/admin/requests/requests.test.ts +++ /dev/null @@ -1,139 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { render, screen, fireEvent, waitFor } from '@testing-library/svelte'; -import { mockQuery } from '../../../test-utils/query'; -import type { MusicRequest } from '#lib/api/types.js'; - -// Wrap useQueryClient so the page can call invalidateQueries() without a -// real QueryClient context. Everything else from svelte-query passes through. -vi.mock('#lib/api/admin.js', () => ({ - createAdminRequestsQuery: vi.fn(), - approveRequest: vi.fn(), - rejectRequest: vi.fn() -})); -vi.mock('#lib/api/wanted.js', () => ({ - createReleaseProfilesQuery: vi.fn() -})); - -import AdminRequestsPage from './+page.svelte'; -import { createAdminRequestsQuery, approveRequest, rejectRequest } from '#lib/api/admin.js'; -import { createReleaseProfilesQuery } from '#lib/api/wanted.js'; - -const baseRow: MusicRequest = { - id: 'r1', - user_id: 'u1234567-aaaa-bbbb-cccc-deadbeef0001', - status: 'pending', - kind: 'album', - artist_mbid: 'a-mbid', - album_mbid: 'al-mbid', - artist_name: 'Boards of Canada', - album_title: 'Geogaddi', - requested_at: '2026-04-29T10:00:00Z', - updated_at: '2026-04-29T10:00:00Z', - imported_album_count: 0, - imported_track_count: 0 -}; - -afterEach(() => vi.clearAllMocks()); - -function setup(rows: MusicRequest[] = [baseRow]) { - (createAdminRequestsQuery as ReturnType).mockReturnValue( - mockQuery({ data: rows }) - ); - (createReleaseProfilesQuery as ReturnType).mockReturnValue( - mockQuery({ - data: { - profiles: [ - { id: 'p-std', name: 'Standard', primary_types: ['Album'], secondary_types: [], is_default: true, follow_count: 0 }, - { id: 'p-live', name: 'With live', primary_types: ['Album'], secondary_types: ['Live'], is_default: false, follow_count: 0 } - ], - primary_types: ['Album'], - secondary_types: ['Live'] - } - }) - ); - return render(AdminRequestsPage); -} - -describe('/admin/requests', () => { - test('Tab switch refetches with new status filter', async () => { - setup(); - await fireEvent.click(screen.getByRole('tab', { name: /approved/i })); - expect(createAdminRequestsQuery).toHaveBeenCalledWith('approved'); - }); - - test('Approve sends no profile', async () => { - setup(); - (approveRequest as ReturnType).mockResolvedValueOnce(baseRow); - await fireEvent.click(screen.getByRole('button', { name: /approve geogaddi/i })); - expect(approveRequest).toHaveBeenCalledWith('r1', undefined); - }); - - test('An album request has no profile choice', () => { - setup(); - expect(screen.queryByRole('button', { name: /on a profile/i })).toBeNull(); - }); - - test('An artist request is approved on the chosen profile', async () => { - setup([{ ...baseRow, kind: 'artist', album_mbid: undefined, album_title: undefined }]); - await fireEvent.click( - screen.getByRole('button', { name: /approve boards of canada on a profile/i }) - ); - const select = screen.getByLabelText(/release-type profile/i) as HTMLSelectElement; - expect(select.value).toBe('p-std'); - await fireEvent.change(select, { target: { value: 'p-live' } }); - (approveRequest as ReturnType).mockResolvedValueOnce(baseRow); - await fireEvent.click(screen.getByRole('button', { name: /approve and follow/i })); - expect(approveRequest).toHaveBeenCalledWith('r1', 'p-live'); - }); - - test('Reject opens notes textarea; confirm sends notes', async () => { - setup(); - await fireEvent.click(screen.getByRole('button', { name: /reject geogaddi/i })); - const textarea = screen.getByLabelText(/notes/i) as HTMLTextAreaElement; - await fireEvent.input(textarea, { target: { value: 'duplicate' } }); - (rejectRequest as ReturnType).mockResolvedValueOnce({ - ...baseRow, - status: 'rejected' - }); - await fireEvent.click( - screen.getByRole('button', { name: /confirm reject/i }) - ); - expect(rejectRequest).toHaveBeenCalledWith('r1', 'duplicate'); - }); - - test('An approve error shows a toast', async () => { - setup(); - (approveRequest as ReturnType).mockRejectedValueOnce({ - code: 'request_not_pending', - message: 'not pending', - status: 409 - }); - await fireEvent.click(screen.getByRole('button', { name: /approve geogaddi/i })); - await waitFor(() => - expect(screen.getByText(/no longer pending/i)).toBeInTheDocument() - ); - }); - - test('Track-kind row renders disclosure copy about adding the album', () => { - setup([ - { - ...baseRow, - kind: 'track', - track_title: 'Roygbiv', - album_title: 'Geogaddi' - } - ]); - expect(screen.getByText(/wants the album/i)).toBeInTheDocument(); - }); - - test('A row names who asked for it', () => { - setup([{ ...baseRow, username: 'alice' }]); - expect(screen.getByText(/^by alice · /)).toBeInTheDocument(); - expect(screen.queryByText(/u1234567/)).not.toBeInTheDocument(); - }); - - test('A row without a name falls back to the user id', () => { - setup(); - expect(screen.getByText(/^by user u1234567 · /)).toBeInTheDocument(); - }); -}); diff --git a/web/src/routes/admin/tuning/+page.svelte b/web/src/routes/admin/tuning/+page.svelte index 64c6cc8c..a5368258 100644 --- a/web/src/routes/admin/tuning/+page.svelte +++ b/web/src/routes/admin/tuning/+page.svelte @@ -10,7 +10,6 @@ type TuningSnapshot, type WeightProfile, type TasteTuning, - type DiscoverTuning, type TrendsResponse, type TrendSeries, type TrendMarker @@ -47,11 +46,6 @@ { key: 'mood_scale', label: 'Mood weight', hint: 'How much the mood counts. 0 ignores it.' } ]; - const discoverFields: { key: keyof DiscoverTuning; label: string; hint: string }[] = [ - { key: 'tag_overlap_weight', label: 'Taste-tag weight', hint: 'Boost for a suggestion whose tags fit your taste. 0 ranks on likeness alone.' }, - { key: 'snooze_days', label: 'Snooze length (days)', hint: 'How long "not right now" hides a suggestion.' } - ]; - const profileScopes: { scope: WeightProfileScope; label: string; blurb: string }[] = [ { scope: 'radio', label: 'Radio', blurb: 'Listening that starts from a seed.' }, { scope: 'daily_mix', label: 'Daily mixes', blurb: 'For You, the discovery mixes and "You might like".' }, @@ -73,14 +67,12 @@ radio: {}, daily_mix: {}, songs_like: {}, - taste: {}, - discover: {} + taste: {} }; for (const p of profileScopes.map((s) => s.scope)) { for (const { key } of weightFields) f[p][key] = String(snap.profiles[p][key]); } for (const { key } of tasteFields) f.taste[key] = String(snap.taste[key]); - for (const { key } of discoverFields) f.discover[key] = String(snap.discover[key]); form = f; } @@ -104,12 +96,6 @@ if (scope === 'taste') { return snapshot.taste[key as keyof TasteTuning] !== snapshot.shipped.taste[key as keyof TasteTuning]; } - if (scope === 'discover') { - return ( - snapshot.discover[key as keyof DiscoverTuning] !== - snapshot.shipped.discover[key as keyof DiscoverTuning] - ); - } return ( snapshot.profiles[scope][key as keyof WeightProfile] !== snapshot.shipped.profiles[scope][key as keyof WeightProfile] @@ -119,7 +105,6 @@ function currentValue(scope: TuningScope, key: string): number { if (!snapshot) return 0; if (scope === 'taste') return snapshot.taste[key as keyof TasteTuning]; - if (scope === 'discover') return snapshot.discover[key as keyof DiscoverTuning]; return snapshot.profiles[scope][key as keyof WeightProfile]; } @@ -277,7 +262,6 @@ id="{scope}-{f.key}" type="number" step={step(f.key)} - min={scope === 'discover' ? '0' : undefined} bind:value={form[scope][f.key]} class="w-24 rounded-md border border-border bg-background px-2 py-1.5 text-right text-sm tabular-nums" /> @@ -327,14 +311,6 @@ (k) => (k === 'half_life_days' ? '1' : '0.05'), (k) => snap.shipped.taste[k as keyof TasteTuning] )} - {@render knobs( - 'discover', - 'Discover requests', - "How Discover ranks artists you don't have.", - discoverFields, - (k) => (k === 'snooze_days' ? '1' : '0.1'), - (k) => snap.shipped.discover[k as keyof DiscoverTuning] - )} {/if} diff --git a/web/src/routes/admin/tuning/tuning.test.ts b/web/src/routes/admin/tuning/tuning.test.ts index f4ca8908..63f70b8f 100644 --- a/web/src/routes/admin/tuning/tuning.test.ts +++ b/web/src/routes/admin/tuning/tuning.test.ts @@ -43,12 +43,6 @@ const taste = (over: Partial> = {}) => ({ ...over }); -const discover = (over: Partial> = {}) => ({ - tag_overlap_weight: 1, - snooze_days: 90, - ...over -}); - // NOTE the `as TuningSnapshot` cast below: it silences the type error when the // server snapshot grows a field, so a missing key here surfaces as every test // on this page throwing inside fillForm rather than as a compile failure. Add @@ -61,15 +55,13 @@ function snapshot(over: Partial = {}): TuningSnapshot { songs_like: weights({ similarity_weight: 4, like_boost: 0.5, taste_weight: 0.25 }) }, taste: taste(), - discover: discover(), shipped: { profiles: { radio: weights({ taste_weight: 1 }), daily_mix: weights(), songs_like: weights({ similarity_weight: 4, like_boost: 0.5, taste_weight: 0.25 }) }, - taste: taste(), - discover: discover() + taste: taste() }, ...over } as TuningSnapshot; @@ -262,62 +254,4 @@ describe('Admin tuning page', () => { expect(screen.getByText(/trends appear once listening accumulates/i)).toBeInTheDocument() ); }); - // --- Discover scope --- - - test('renders the Discover card with its two knobs', async () => { - (getTuning as ReturnType).mockResolvedValue(snapshot()); - render(TuningPage); - await waitFor(() => expect(screen.getByText('Discover requests')).toBeInTheDocument()); - expect(screen.getByLabelText(/taste-tag weight/i)).toBeInTheDocument(); - expect(screen.getByLabelText(/snooze length/i)).toBeInTheDocument(); - }); - - test('saving Discover patches only the changed field under the discover scope', async () => { - (getTuning as ReturnType).mockResolvedValue(snapshot()); - (patchTuning as ReturnType).mockResolvedValue(snapshot()); - render(TuningPage); - await waitFor(() => expect(screen.getByText('Discover requests')).toBeInTheDocument()); - await fireEvent.input(screen.getByLabelText(/taste-tag weight/i), { - target: { value: '2.5' } - }); - await fireEvent.click(screen.getByRole('button', { name: /save discover/i })); - await waitFor(() => - expect(patchTuning).toHaveBeenCalledWith('discover', { tag_overlap_weight: 2.5 }) - ); - }); - - // 0 is an admin's off switch for the whole tag term, so the form must be - // able to send it — a falsy-value bug here would silently make the feature - // impossible to disable. - test('a Discover weight of 0 is sent, not dropped as falsy', async () => { - (getTuning as ReturnType).mockResolvedValue(snapshot()); - (patchTuning as ReturnType).mockResolvedValue(snapshot()); - render(TuningPage); - await waitFor(() => expect(screen.getByText('Discover requests')).toBeInTheDocument()); - await fireEvent.input(screen.getByLabelText(/taste-tag weight/i), { target: { value: '0' } }); - await fireEvent.click(screen.getByRole('button', { name: /save discover/i })); - await waitFor(() => - expect(patchTuning).toHaveBeenCalledWith('discover', { tag_overlap_weight: 0 }) - ); - }); - - test('reset targets the discover scope', async () => { - (getTuning as ReturnType).mockResolvedValue(snapshot()); - (resetTuning as ReturnType).mockResolvedValue(snapshot()); - render(TuningPage); - await waitFor(() => expect(screen.getByText('Discover requests')).toBeInTheDocument()); - await fireEvent.click( - screen.getByRole('button', { name: /reset discover requests to defaults/i }) - ); - await waitFor(() => expect(resetTuning).toHaveBeenCalledWith('discover')); - }); - - test('a deviating Discover knob is dotted', async () => { - const snap = snapshot(); - snap.discover = discover({ tag_overlap_weight: 3 }); - (getTuning as ReturnType).mockResolvedValue(snap); - render(TuningPage); - await waitFor(() => expect(screen.getByText('Discover requests')).toBeInTheDocument()); - expect(screen.getByTitle(/deviates from the shipped default \(1\)/i)).toBeInTheDocument(); - }); }); diff --git a/web/src/routes/admin/users/+page.svelte b/web/src/routes/admin/users/+page.svelte index 59f0b213..c7793f86 100644 --- a/web/src/routes/admin/users/+page.svelte +++ b/web/src/routes/admin/users/+page.svelte @@ -10,7 +10,6 @@ createUser, deleteUser, resetUserPassword, - updateUserAutoApprove, updateUserDebugMode, createAdminUsersQuery, createAdminInvitesQuery, @@ -22,7 +21,7 @@ import { pushToast } from '#lib/stores/toast.svelte.js'; import Modal from '#lib/components/Modal.svelte'; import RowActionsMenu, { type RowAction } from '#lib/components/RowActionsMenu.svelte'; - import { Shield, ShieldOff, CheckCircle2, XCircle, KeyRound, Trash2, Copy, Bug, BugOff } from 'lucide-svelte'; + import { Shield, ShieldOff, KeyRound, Trash2, Copy, Bug, BugOff } from 'lucide-svelte'; const client = useQueryClient(); @@ -150,21 +149,6 @@ } } - async function onToggleAutoApprove(u: AdminUser) { - saving = true; - try { - await updateUserAutoApprove(u.id, !u.auto_approve_requests); - await client.invalidateQueries({ queryKey: qk.adminUsers() }); - pushToast(u.auto_approve_requests - ? `Auto-approve disabled for ${u.username}.` - : `Auto-approve enabled for ${u.username}.`); - } catch (e: unknown) { - pushToast(`Toggle failed: ${errCode(e)}`, 'error'); - } finally { - saving = false; - } - } - // Debug mode makes the account's devices report diagnostics: a // row action here, with the badge linking to that account's events. async function onToggleDebug(u: AdminUser) { @@ -267,15 +251,6 @@ disabled: saving }} {@const secondary: RowAction[] = [ - { - icon: u.auto_approve_requests ? XCircle : CheckCircle2, - label: u.auto_approve_requests ? 'Disable auto-approve' : 'Enable auto-approve', - ariaLabel: u.auto_approve_requests - ? `Disable auto-approve for ${u.username}` - : `Enable auto-approve for ${u.username}`, - onclick: () => onToggleAutoApprove(u), - disabled: saving - }, { icon: u.debug_mode_enabled ? BugOff : Bug, label: u.debug_mode_enabled ? 'Turn off debug mode' : 'Turn on debug mode', @@ -318,12 +293,6 @@ data-testid="debug-badge" >debug on {/if} - {#if u.auto_approve_requests} - auto-approve - {/if}
{#if u.display_name}{u.display_name} · {/if}created {formatDate(u.created_at)} diff --git a/web/src/routes/admin/users/users.test.ts b/web/src/routes/admin/users/users.test.ts index a6d6f578..f59ac7a6 100644 --- a/web/src/routes/admin/users/users.test.ts +++ b/web/src/routes/admin/users/users.test.ts @@ -16,7 +16,6 @@ vi.mock('#lib/api/admin.js', () => ({ createUser: vi.fn(), deleteUser: vi.fn(), resetUserPassword: vi.fn(), - updateUserAutoApprove: vi.fn(), updateUserDebugMode: vi.fn() })); @@ -30,7 +29,6 @@ import { createUser, deleteUser, resetUserPassword, - updateUserAutoApprove, updateUserDebugMode } from '#lib/api/admin.js'; @@ -39,7 +37,6 @@ const alice: AdminUser = { username: 'alice', display_name: null, is_admin: true, - auto_approve_requests: false, debug_mode_enabled: false, created_at: '2026-05-01T00:00:00Z' }; @@ -49,7 +46,6 @@ const bob: AdminUser = { username: 'bob', display_name: 'Bob B', is_admin: false, - auto_approve_requests: false, debug_mode_enabled: false, created_at: '2026-05-02T00:00:00Z' }; @@ -201,15 +197,6 @@ describe('/admin/users', () => { expect(resetUserPassword).toHaveBeenCalledWith(expect.any(String), 'abcd1234'); }); - test('toggle-auto-approve calls updateUserAutoApprove', async () => { - (updateUserAutoApprove as ReturnType).mockResolvedValue({}); - setup(); - await waitFor(() => screen.getByText('alice')); - - await fireEvent.click(screen.getAllByRole('button', { name: /Enable auto-approve/i })[0]); - expect(updateUserAutoApprove).toHaveBeenCalledWith(expect.any(String), true); - }); - test('debug mode is a row action, and an account with it on links to its events', async () => { (updateUserDebugMode as ReturnType).mockResolvedValue({}); setup([alice, { ...bob, debug_mode_enabled: true }]); diff --git a/web/src/routes/artists/[id]/+page.svelte b/web/src/routes/artists/[id]/+page.svelte index 803ec4a0..768dd299 100644 --- a/web/src/routes/artists/[id]/+page.svelte +++ b/web/src/routes/artists/[id]/+page.svelte @@ -14,8 +14,6 @@ import CompactTrackCard from '#lib/components/CompactTrackCard.svelte'; import HorizontalScrollRow from '#lib/components/HorizontalScrollRow.svelte'; import LikeButton from '#lib/components/LikeButton.svelte'; - import FollowControl from '#lib/components/FollowControl.svelte'; - import { user } from '#lib/auth/store.svelte.js'; import LibrarySkeleton from '#lib/components/LibrarySkeleton.svelte'; import ApiErrorBanner from '#lib/components/ApiErrorBanner.svelte'; import { useDelayed } from '#lib/utils/useDelayed.svelte.js'; @@ -25,7 +23,6 @@ import type { ArtistRef, TrackRef } from '#lib/api/types.js'; const id = $derived(page.params.id ?? ''); - const isAdmin = $derived(user.value?.is_admin === true); const queryStore = $derived(createArtistQuery(id)); const query = $derived($queryStore); const showSkeleton = useDelayed(() => query.isPending); @@ -114,11 +111,6 @@ {onPlay} {onShuffle} /> - - {#if isAdmin && detail.mbid} - - {/if}
diff --git a/web/src/routes/artists/[id]/artist.test.ts b/web/src/routes/artists/[id]/artist.test.ts index e19fd300..aa34aa7c 100644 --- a/web/src/routes/artists/[id]/artist.test.ts +++ b/web/src/routes/artists/[id]/artist.test.ts @@ -25,24 +25,6 @@ vi.mock('#lib/api/queries.js', () => ({ vi.mock('#lib/api/likes.js', () => emptyLikesMock()); -const userState = vi.hoisted(() => ({ - current: null as { id: string; username: string; is_admin: boolean } | null -})); - -vi.mock('#lib/auth/store.svelte.js', () => ({ - user: { get value() { return userState.current; } } -})); - -vi.mock('#lib/api/wanted.js', async () => { - const { mockQuery } = await import('../../../test-utils/query'); - return { - createFollowQuery: vi.fn(() => mockQuery({ data: { following: false } })), - createReleaseProfilesQuery: vi.fn(() => mockQuery({ data: { profiles: [], primary_types: [], secondary_types: [] } })), - putFollow: vi.fn(), - deleteFollow: vi.fn() - }; -}); - vi.mock('#lib/api/client.js', () => apiClientMock()); vi.mock('#lib/player/store.svelte.js', () => ({ @@ -77,7 +59,6 @@ function album(id: string, title: string, year?: number): AlbumRef { afterEach(() => { vi.clearAllMocks(); state.pageParams = { id: 'abc' }; - userState.current = null; }); beforeEach(() => { @@ -194,34 +175,4 @@ describe('artist detail page', () => { expect(queued.map((t: { id: string }) => t.id).sort()).toEqual(tracks.map((t) => t.id).sort()); }); }); - - // Following is for the wanted list: an admin control, keyed by - // the artist's MusicBrainz id. - describe('Follow', () => { - const detail = (mbid: string): ArtistDetail => ({ - id: 'abc', name: 'Alice', sort_name: 'Alice', album_count: 1, cover_url: '', - albums: [album('a1', 'First', 2020)], genres: [], mbid - }); - - test('an admin can follow an artist with a MusicBrainz id', () => { - userState.current = { id: 'u1', username: 'admin', is_admin: true }; - (createArtistQuery as ReturnType).mockReturnValue(mockQuery({ data: detail('mb-alice') })); - render(ArtistPage); - expect(screen.getByRole('button', { name: /^follow$/i })).toBeInTheDocument(); - }); - - test('no follow control without a MusicBrainz id', () => { - userState.current = { id: 'u1', username: 'admin', is_admin: true }; - (createArtistQuery as ReturnType).mockReturnValue(mockQuery({ data: detail('') })); - render(ArtistPage); - expect(screen.queryByRole('button', { name: /^follow$/i })).toBeNull(); - }); - - test('no follow control for a non-admin', () => { - userState.current = { id: 'u2', username: 'sam', is_admin: false }; - (createArtistQuery as ReturnType).mockReturnValue(mockQuery({ data: detail('mb-alice') })); - render(ArtistPage); - expect(screen.queryByRole('button', { name: /^follow$/i })).toBeNull(); - }); - }); }); diff --git a/web/src/routes/collection/+layout.svelte b/web/src/routes/collection/+layout.svelte index 33461529..d8947d22 100644 --- a/web/src/routes/collection/+layout.svelte +++ b/web/src/routes/collection/+layout.svelte @@ -1,82 +1,44 @@

Collection

- Every artist and album in your collection: what the library holds, and what Minstrel is - getting for it, from user requests, new releases of the artists it follows, and albums whose - files went missing. It searches your indexers on a schedule, checks each download against - MusicBrainz, and files what's right into the library. Searching and quality are set under - Acquisition. + What in the library needs looking at: tracks people reported, songs held twice, files from + doubtful sources, and files that went missing.

- {#if verdict} -

- {verdict} - {#if counts?.attention} - See Activity - {:else if counts?.unmatched} - See Unmatched - {/if} -

- {/if} - {#if health.length > 0} -

- To look at: - {#each health as d, i (d.key)}{queueLabel(d, countOf(summary, d.key))}{i < health.length - 1 ? ', ' : '.'} - {/each} -

+ {#if summary} + {#if waiting.length > 0} +

+ To look at: + {#each waiting as d, i (d.key)}{queueLabel(d, countOf(summary, d.key))}{i < waiting.length - 1 ? ', ' : '.'} + {/each} +

+ {:else} +

Nothing needs you.

+ {/if} {/if}
- +
diff --git a/web/src/routes/collection/+page.svelte b/web/src/routes/collection/+page.svelte index 5a07e2e2..1aab1ce0 100644 --- a/web/src/routes/collection/+page.svelte +++ b/web/src/routes/collection/+page.svelte @@ -1,8 +1,49 @@ {pageTitle('Collection')} - +
+ {#if summaryQ.isPending} +

Reading the queues…

+ {:else if summaryQ.isError} +

+ Couldn't read the queues. + +

+ {:else} + {#if waiting.length > 0} + + {/if} + {#if clear.length > 0} +

+ No {clear.map((d) => d.none).join(', ')}. +

+ {/if} + {/if} +
diff --git a/web/src/routes/collection/activity/+page.svelte b/web/src/routes/collection/activity/+page.svelte deleted file mode 100644 index d36f1008..00000000 --- a/web/src/routes/collection/activity/+page.svelte +++ /dev/null @@ -1,8 +0,0 @@ - - -{pageTitle('Collection · Activity')} - - diff --git a/web/src/routes/collection/artist/[mbid]/+page.svelte b/web/src/routes/collection/artist/[mbid]/+page.svelte deleted file mode 100644 index 53b4ada0..00000000 --- a/web/src/routes/collection/artist/[mbid]/+page.svelte +++ /dev/null @@ -1,15 +0,0 @@ - - -{pageTitle('Collection · Artist')} - -{#key mbid} - -{/key} diff --git a/web/src/routes/collection/blocked/+page.svelte b/web/src/routes/collection/blocked/+page.svelte deleted file mode 100644 index 07c7b02d..00000000 --- a/web/src/routes/collection/blocked/+page.svelte +++ /dev/null @@ -1,8 +0,0 @@ - - -{pageTitle('Collection · Blocked')} - - diff --git a/web/src/routes/collection/collection.test.ts b/web/src/routes/collection/collection.test.ts index 19132d70..01827c14 100644 --- a/web/src/routes/collection/collection.test.ts +++ b/web/src/routes/collection/collection.test.ts @@ -1,97 +1,57 @@ import { afterEach, describe, expect, test, vi } from 'vitest'; import { render, screen } from '@testing-library/svelte'; import { createRawSnippet } from 'svelte'; -import { mockInfiniteQuery } from '../../test-utils/query'; import { pageUrlModule } from '#test-utils/mocks/appState.js'; import { mockQuery } from '../../test-utils/query'; -import type { AdminSummary, WantArtistsPage, WantCounts } from '#lib/api/types.js'; +import type { AdminSummary } from '#lib/api/types.js'; const state = vi.hoisted(() => ({ pageUrl: new URL('http://localhost/collection') })); vi.mock('$app/state', () => pageUrlModule(state)); -vi.mock('#lib/api/wanted.js', () => ({ createWantArtistsQuery: vi.fn() })); vi.mock('#lib/api/admin.js', () => ({ createAdminSummaryQuery: vi.fn() })); -import WantedLayout from './+layout.svelte'; -import { createWantArtistsQuery } from '#lib/api/wanted.js'; +import CollectionLayout from './+layout.svelte'; +import CollectionPage from './+page.svelte'; import { createAdminSummaryQuery } from '#lib/api/admin.js'; -const counts: WantCounts = { artists: 7, wanted: 5, missing: 1, held: 9, upgrades: 6, following: 2, activity: 3, attention: 0, blocked: 4, unmatched: 3 }; const children = createRawSnippet(() => ({ render: () => `

body

` })); afterEach(() => vi.clearAllMocks()); const none = { count: 0, since: null }; -const clear: AdminSummary = { requests: none, reports: none, duplicates: none, suspect: none, missing: none }; +const clear: AdminSummary = { reports: none, duplicates: none, suspect: none, missing: none }; -function setup(c: WantCounts = counts, path = '/collection', summary: AdminSummary = clear) { - state.pageUrl = new URL(`http://localhost${path}`); +function mockSummary(summary: AdminSummary) { vi.mocked(createAdminSummaryQuery).mockReturnValue( mockQuery({ data: summary }) as unknown as ReturnType ); - const page: WantArtistsPage = { - items: [], - total: 0, - limit: 50, - offset: 0, - counts: c, - library: { listed_at: null, unlisted: 0 } - }; - vi.mocked(createWantArtistsQuery).mockReturnValue( - mockInfiniteQuery({ pages: [page] }) as unknown as ReturnType - ); +} + +function setup(path = '/collection', summary: AdminSummary = clear) { + state.pageUrl = new URL(`http://localhost${path}`); + mockSummary(summary); // eslint-disable-next-line @typescript-eslint/no-explicit-any - return render(WantedLayout, { children } as any); + return render(CollectionLayout, { children } as any); } describe('/collection layout', () => { - test('the sections down the left carry their counts, the current one marked', () => { - setup(counts, '/collection/activity'); + test('the sections down the left are the overview and the health queues, the current one marked', () => { + setup('/collection/missing'); const nav = screen.getByRole('navigation', { name: /collection sections/i }); const links = Array.from(nav.querySelectorAll('a')).map((a) => [a.getAttribute('href'), a.textContent?.replace(/\s+/g, ' ').trim()]); expect(links).toEqual([ - ['/collection', 'Catalogue 7'], - ['/collection/wanted', 'Wanted 5'], - ['/collection/upgrades', 'Upgrades 6'], - ['/collection/activity', 'Activity 3'], - ['/collection/following', 'Following 2'], - ['/collection/blocked', 'Blocked 4'], - ['/collection/unmatched', 'Unmatched 3'], + ['/collection', 'Overview'], ['/collection/reports', 'Reports'], ['/collection/duplicates', 'Duplicates'], ['/collection/suspect-sources', 'Suspect sources'], ['/collection/missing', 'Missing files'] ]); - expect(screen.getByRole('link', { name: /activity/i })).toHaveAttribute('aria-current', 'page'); - expect(screen.getByRole('link', { name: /^catalogue/i })).not.toHaveAttribute('aria-current'); + expect(screen.getByRole('link', { name: /missing files/i })).toHaveAttribute('aria-current', 'page'); + expect(screen.getByRole('link', { name: /^overview/i })).not.toHaveAttribute('aria-current'); expect(screen.getByTestId('tab-body')).toBeTruthy(); }); - test('when nothing needs a person the verdict says so', () => { - setup({ ...counts, unmatched: 0 }); - expect(screen.getByTestId('collection-verdict').textContent).toMatch(/5 albums wanted, 3 downloading\. Nothing needs you\./); - expect(screen.queryByTestId('tab-attention')).toBeNull(); - }); - - test('downloads waiting on a person lead the page and mark Activity', () => { - setup({ ...counts, attention: 2 }); - const verdict = screen.getByTestId('collection-verdict'); - expect(verdict.textContent).toMatch(/2 downloads need you/); - expect(screen.getByRole('link', { name: /see activity/i }).getAttribute('href')).toBe('/collection/activity'); - expect(screen.getByTestId('tab-attention').textContent).toBe('2'); - }); - - test('library albums waiting to be matched need a person, and link to Unmatched', () => { - setup(); - expect(screen.getByTestId('collection-verdict').textContent).toMatch( - /5 albums wanted, 3 downloading\. 3 albums in your library need matching\./ - ); - expect(screen.getByRole('link', { name: /see unmatched/i }).getAttribute('href')).toBe('/collection/unmatched'); - }); - - // The library's health queues moved here from Admin: their counts - // come from the admin summary, accented, and a line names the ones waiting. - test('the library health queues carry what waits on you, and a line links to each', () => { - setup(counts, '/collection', { + test('the health queues carry what waits on you, and a line links to each', () => { + setup('/collection', { ...clear, reports: { count: 2, since: null }, duplicates: { count: 5, since: null }, @@ -111,13 +71,20 @@ describe('/collection layout', () => { ]); }); - test('with nothing waiting there is no health line', () => { + test('with nothing waiting the line says nothing needs you', () => { setup(); - expect(screen.queryByTestId('collection-health')).toBeNull(); + expect(screen.getByTestId('collection-health').textContent?.trim()).toBe('Nothing needs you.'); }); +}); - test("an artist's page sits under Catalogue", () => { - setup(counts, '/collection/artist/blur'); - expect(screen.getByRole('link', { name: /^catalogue/i })).toHaveAttribute('aria-current', 'page'); +describe('/collection overview', () => { + test('lists each queue with something in it, then the empty ones on one line', () => { + mockSummary({ ...clear, missing: { count: 3, since: new Date().toISOString() } }); + render(CollectionPage); + const rows = screen.getAllByTestId('collection-queue'); + expect(rows).toHaveLength(1); + expect(rows[0].textContent).toMatch(/3 missing files/); + expect(rows[0].querySelector('a')?.getAttribute('href')).toBe('/collection/missing'); + expect(screen.getByTestId('collection-clear').textContent?.trim()).toBe('No reports, duplicates, suspect sources.'); }); }); diff --git a/web/src/routes/collection/duplicates/+page.svelte b/web/src/routes/collection/duplicates/+page.svelte index 6a3484cb..39250e4b 100644 --- a/web/src/routes/collection/duplicates/+page.svelte +++ b/web/src/routes/collection/duplicates/+page.svelte @@ -42,11 +42,10 @@ // // Except for one recording filling several of an album's slots (class // mismatch), where a merge is the wrong answer: it keeps one copy's - // title whether or not it is the audio's, and fetches nothing. There an - // admin says which song the recording is, or that they can't tell, and the - // group settles as the resolver would have: copies in other songs' slots are - // removed, the release they came from is set aside, and the album is fetched - // again. + // title whether or not it is the audio's. There an admin says which song the + // recording is, or that they can't tell, and the group settles as the + // resolver would have: copies in other songs' slots are removed, and those + // slots show as missing until a good copy is in the library folder. type Tab = DuplicatesView | 'resolved'; const fromUrl = page.url.searchParams.get('view'); @@ -172,11 +171,7 @@ try { const result = await refetchDuplicateGroup(group.id, { keep_track_id: slotPick[group.id] ?? '' }); const removed = `Removed ${plural(result.removed_paths.length, 'file', 'files')}`; - pushToast( - result.blocked.length > 0 - ? `${removed} and set aside the release they came from. The album will be fetched again.` - : `${removed}. The album will be fetched again, but Minstrel didn't download these files, so the same ones may come back.` - ); + pushToast(`${removed}. Their slots show as missing until a good copy is in the library.`); query.refetch(); } catch (e: unknown) { pushToast(errMessage(e), 'error'); @@ -283,8 +278,8 @@

Tracks that hold the same recording more than once. Most settle on their own: a song held twice on one album is merged, copies on different releases count as one song, two versions of a song - are left alone, and a file filling another song's track slot is removed so the album is fetched - again. What's left here needs you. + are left alone, and a file filling another song's track slot is removed, leaving that slot + missing. What's left here needs you.

@@ -344,12 +339,12 @@ {:else if item.action === 'duplicate_wrong_file' && !item.details.kept_title}

{item.details.artist}: one recording was filed as {removed.map((r) => r.title).join(' and ')}, - and it is none of them. Removed; the album will be fetched again. + and it is none of them. Removed; those slots show as missing.

{:else if item.action === 'duplicate_wrong_file'}

{item.details.artist}: the recording of {item.details.kept_title} was also filed as - {removed.map((r) => r.title).join(', ')}. Removed from there; the album will be fetched again. + {removed.map((r) => r.title).join(', ')}. Removed from there; those slots show as missing.

{:else}

@@ -455,11 +450,11 @@ {#if refetching === group.id} Removing… {:else if confirmingRefetch === group.id} - Remove {plural(refetchRemoves(group).length, 'file', 'files')} and get it again + Remove {plural(refetchRemoves(group).length, 'file', 'files')} {:else if picked} Keep “{picked.title}”… {:else} - Get it again… + Remove the copies… {/if} {:else} @@ -527,8 +522,8 @@ {:else} All {plural(group.members.length, 'copy', 'copies')} will be deleted from disk. {/if} - Their tracks stay, marked missing, with their likes and plays. The release the files came - from is set aside, and the album is fetched again from another one. + Their tracks stay, marked missing, with their likes and plays, until a good copy is in the + library folder.

Tracks whose audio file the last scan couldn't find. They keep their play - history and likes, and stop being offered anywhere until the file returns. + history and likes, and stop being offered anywhere until a file is back in the library + folder.

-

- {#if policy} - {policy.enabled - ? `Albums missing for ${policy.grace_hours} hours go back on the wanted list by themselves.` - : 'Missing albums are not wanted back automatically.'} - {/if} - - {policy ? 'Change' : 'Set what happens to missing files'} - -

- {#if query.isPending}

Checking what's missing…

{:else if query.isError} @@ -158,72 +84,6 @@
- - {#if group.reacquisition} - {@const r = group.reacquisition} - {@const w = r.want} -
-
-
-

- {#if r.held_at} - Held again {relativeTime(r.held_at)}. The rows here clear on the next scan. - {:else if r.wanted_at} - Wanted back since {relativeTime(r.wanted_at)}. - {/if} -

- {#if w && !r.held_at} -

- {wantStatus(w)} - {#if needsYou(w)} - Decide on Activity - {/if} -

- {/if} -
- {#if w && !r.held_at} -
- - - -
- {/if} -
- {#if w && open === w.id} - - {/if} -
- {/if} -
    {#each group.tracks as t (t.track_id)}
  • diff --git a/web/src/routes/collection/missing/missing.test.ts b/web/src/routes/collection/missing/missing.test.ts index 7494a2b1..89b00a4f 100644 --- a/web/src/routes/collection/missing/missing.test.ts +++ b/web/src/routes/collection/missing/missing.test.ts @@ -1,36 +1,12 @@ import { afterEach, describe, expect, test, vi } from 'vitest'; -import { fireEvent, render, screen, waitFor } from '@testing-library/svelte'; +import { render, screen } from '@testing-library/svelte'; import { mockQuery } from '../../../test-utils/query'; -import type { AdminMissingResponse, Want } from '#lib/api/types.js'; +import type { AdminMissingResponse } from '#lib/api/types.js'; -// The page reads the re-acquisition policy to say what happens to these; -// the settings themselves are on Admin › Acquisition. -vi.mock('#lib/api/admin.js', () => ({ - createMissingFilesQuery: vi.fn(), - getReacquisitionSettings: vi.fn().mockResolvedValue({ - enabled: true, - grace_hours: 24, - max_per_pass: 20, - unnameable_albums: 0 - }) -})); -// A folder's want is acted on here as on the wanted list. -vi.mock('#lib/api/wanted.js', () => ({ - removeWant: vi.fn().mockResolvedValue(undefined), - searchWant: vi.fn().mockResolvedValue({ - outcome: 'nothing', considered: 0, best_release: '', best_verdict: '', narrowed: [] - }), - createCandidatesQuery: vi.fn(), - grabCandidate: vi.fn(), - blockCandidate: vi.fn(), - unblockRelease: vi.fn() -})); -vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); +vi.mock('#lib/api/admin.js', () => ({ createMissingFilesQuery: vi.fn() })); import MissingPage from './+page.svelte'; import { createMissingFilesQuery } from '#lib/api/admin.js'; -import { removeWant, searchWant } from '#lib/api/wanted.js'; -import { pushToast } from '#lib/stores/toast.svelte.js'; const DAY = 24 * 3_600_000; @@ -57,7 +33,6 @@ const response: AdminMissingResponse = { { directory: '/music/Linkin Park/Minutes to Midnight', missing_since: new Date(Date.now() - 3 * DAY).toISOString(), - reacquisition: null, tracks: [ track('Given Up', { lastPlayed: new Date(Date.now() - 2 * DAY).toISOString() }), track('Bleed It Out') @@ -66,7 +41,6 @@ const response: AdminMissingResponse = { { directory: '/music/Boards of Canada/Geogaddi', missing_since: new Date(Date.now() - 9 * DAY).toISOString(), - reacquisition: null, tracks: [track('1969')] } ] @@ -74,13 +48,6 @@ const response: AdminMissingResponse = { afterEach(() => vi.clearAllMocks()); -// Whitespace-normalised text of a testid'd element. Svelte templates wrap -// mid-sentence, so raw textContent has newlines where the rendered page has -// single spaces — asserting on it directly makes tests fail on reflow. -function text(testId: string): string { - return (screen.getByTestId(testId).textContent ?? '').replace(/\s+/g, ' ').trim(); -} - function renderWith(data: AdminMissingResponse | undefined, extra = {}) { vi.mocked(createMissingFilesQuery).mockReturnValue( mockQuery({ data, ...extra }) as ReturnType @@ -88,46 +55,7 @@ function renderWith(data: AdminMissingResponse | undefined, extra = {}) { return render(MissingPage); } -function want(extra: Partial = {}): Want { - return { - id: 'w-1', - release_group_mbid: 'rg-1', - title: 'Minutes to Midnight', - artist_mbid: 'lp', - artist_name: 'Linkin Park', - primary_type: 'Album', - secondary_types: [], - year: '2007', - reason: 'missing', - wanted_at: new Date(Date.now() - 2 * DAY).toISOString(), - held_at: null, - image_url: '', - search: { - searched_at: new Date(Date.now() - 3_600_000).toISOString(), - outcome: 'nothing', - considered: 0, - best_release: '', - best_verdict: '', - error: '' - }, - ...extra - }; -} - -function wantedBack(w: Want | null = want()): AdminMissingResponse { - const withState = structuredClone(response); - withState.groups[0].reacquisition = { wanted_at: new Date(Date.now() - 2 * DAY).toISOString(), held_at: null, want: w }; - return withState; -} - describe('/collection/missing', () => { - test('says what happens to missing files, and links to where it is set', async () => { - renderWith(response); - const line = screen.getByTestId('reacquisition-policy'); - await screen.findByText(/missing for 24 hours go back on the wanted list/i); - expect(line.querySelector('a')?.getAttribute('href')).toBe('/admin/acquisition#reacquisition'); - }); - test('groups rows under their directory', () => { renderWith(response); expect(screen.getByText('/music/Linkin Park/Minutes to Midnight')).toBeTruthy(); @@ -185,63 +113,6 @@ describe('/collection/missing', () => { expect(screen.getByText(/couldn't load the missing files/i)).toBeTruthy(); }); - // Not wanted yet is the common case for a folder that just went missing; - // a line saying so would be noise on every row. - test('no re-acquisition line before the album is wanted', () => { - renderWith(response); - expect(screen.queryByTestId('reacquisition-state')).toBeNull(); - }); - - test('an album wanted back says since when, and what its last search found', () => { - renderWith(wantedBack()); - expect(text('reacquisition-state')).toMatch(/wanted back since 2d ago/i); - expect(text('missing-want-status')).toBe('Searched 1h ago: nothing found'); - }); - - test('its download, when one is running, is the line', () => { - renderWith( - wantedBack( - want({ - grab: { - id: 'g', name: 'LP - MtM [FLAC]', grabbed_at: new Date().toISOString(), origin: 'schedule', quality: 'FLAC', - live: true, end_reason: '', progress: 0.42, import_state: 'downloading', import_reason: '', end_detail: '', - imported_at: null, imported_files: 0 - } - }) - ) - ); - expect(text('missing-want-status')).toBe('Downloading 42% · LP - MtM [FLAC]'); - expect(screen.getByRole('button', { name: /search for minutes to midnight now/i })).toHaveProperty('disabled', true); - }); - - test('Search runs one now and says what it found', async () => { - renderWith(wantedBack()); - await fireEvent.click(screen.getByRole('button', { name: /search for minutes to midnight now/i })); - await waitFor(() => expect(searchWant).toHaveBeenCalledWith('w-1')); - await waitFor(() => expect(pushToast).toHaveBeenCalledWith('Nothing found for Minutes to Midnight.', 'info')); - }); - - test('Remove takes it off the wanted list', async () => { - renderWith(wantedBack()); - await fireEvent.click(screen.getByRole('button', { name: /stop wanting minutes to midnight/i })); - await waitFor(() => expect(removeWant).toHaveBeenCalledWith('w-1')); - expect(pushToast).toHaveBeenCalledWith("Minutes to Midnight is off the wanted list, and won't be fetched back."); - }); - - test('an album held again says so', () => { - const withState = structuredClone(response); - withState.groups[0].reacquisition = { - wanted_at: new Date(Date.now() - 5 * DAY).toISOString(), - held_at: new Date(Date.now() - DAY).toISOString(), - want: want({ held_at: new Date(Date.now() - DAY).toISOString() }) - }; - renderWith(withState); - expect(text('reacquisition-state')).toMatch(/held again 1d ago/i); - // Nothing left to search for. - expect(screen.queryByTestId('missing-want-status')).toBeNull(); - expect(screen.queryByRole('button', { name: /search for/i })).toBeNull(); - }); - // Paging only appears when it can do something: a single page of results // should not render dead Previous/Next buttons. test('no pager when everything fits on one page', () => { diff --git a/web/src/routes/collection/suspect-sources/+page.svelte b/web/src/routes/collection/suspect-sources/+page.svelte index b01b0e6d..8959c477 100644 --- a/web/src/routes/collection/suspect-sources/+page.svelte +++ b/web/src/routes/collection/suspect-sources/+page.svelte @@ -17,9 +17,7 @@ // The duplicate resolver holds each one back from radio and the mixes, and // merges it away where the library has a clean copy. A marker // is a reason to look, not proof, so each row says what was done and offers - // the undo: "This one is fine" lets the track back for good. A rip's album - // stays wanted for a clean copy, searched for weekly and filed over the rip - // when it comes. + // the undo: "This one is fine" lets the track back for good. const queryStore = createSuspectSourcesQuery(); const query = $derived($queryStore); @@ -99,9 +97,7 @@ a video site, and can be the wrong audio: a live take, a music-video edit, or something else entirely. Each one is kept out of radio and the mixes, though it still plays when you choose it, and it is merged away where the library has a clean copy of the - same song. Where it is the only copy, its album is searched for a clean copy once a week, - under Upgrades. If one - is fine, say so and it goes back in. + same song. If one is fine, say so and it goes back in.

    @@ -181,13 +177,6 @@ Held back{#if t.verdict_at && !changed[t.track_id]} {relativeTime(t.verdict_at)}{/if} - {#if t.replacing} - - {t.searched_at - ? `Looked for a clean copy ${relativeTime(t.searched_at)}` - : 'A clean copy is wanted'} - - {/if} -
  • - {/each} -
- - -
- {#if query.isError} - - {:else if query.isPending} -

Searching…

- {:else if results.length === 0} -

Nothing to add for that search yet.

- {:else} -
- {#each results as r (r.mbid)} - handleRequestClick(r)} - /> - {/each} -
- {/if} -
- {/if} -
- - - {#if modalResult} -

- Requesting {modalResult.name} - will add {modalResult.secondary_text}. - Continue? -

-
- - -
- {/if} -
diff --git a/web/src/routes/discover/discover.test.ts b/web/src/routes/discover/discover.test.ts deleted file mode 100644 index 23b8aa77..00000000 --- a/web/src/routes/discover/discover.test.ts +++ /dev/null @@ -1,319 +0,0 @@ -import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; -import { render, screen, fireEvent, waitFor } from '@testing-library/svelte'; -import { mockQuery } from '../../test-utils/query'; -import { apiClientMock } from '../../test-utils/mocks/client'; -import type { CatalogueSearchResult } from '#lib/api/types.js'; -import { pageUrlModule } from '../../test-utils/mocks/appState'; - -// $app/state / $app/navigation must be mocked or rendering the page -// pulls in SvelteKit's client runtime (`notifiable_store is not a -// function`) at module load. Same pattern as the other route tests. -const pageState = vi.hoisted(() => ({ - pageUrl: new URL('http://localhost/discover') -})); -vi.mock('$app/state', () => pageUrlModule(pageState)); -vi.mock('$app/navigation', () => ({ goto: vi.fn() })); - -// The catalogue search query factory and createRequest are mocked at the module -// level so each test can shape what the page sees without standing up a -// real QueryClient + network. -vi.mock('#lib/api/catalogue.js', () => ({ - createCatalogueSearchQuery: vi.fn() -})); - -// SuggestionFeed reaches for the snooze surface too. These are -// stubbed here even though this page-level suite asserts nothing about -// snoozing: a factory-shaped module mock must export everything the -// component tree imports, or rendering the feed throws before any -// assertion runs. -vi.mock('#lib/api/suggestions.js', () => ({ - createSuggestionsQuery: vi.fn(), - createSnoozesQuery: vi.fn(), - snoozeSuggestion: vi.fn().mockResolvedValue(undefined), - unsnoozeSuggestion: vi.fn().mockResolvedValue(undefined) -})); - -vi.mock('#lib/api/requests.js', () => ({ - createRequest: vi.fn().mockResolvedValue({ id: 'r1' }) -})); - -vi.mock('#lib/api/client.js', () => apiClientMock()); - -// The page calls useQueryClient() to invalidate qk.myRequests() after a -// submit; stub it so rendering doesn't require a real QueryClient and -// we can assert the invalidation fires. -const queryClientMock = vi.hoisted(() => ({ invalidateQueries: vi.fn() })); -vi.mock('@tanstack/svelte-query', async (importOriginal) => { - const actual = await importOriginal(); - return { ...actual, useQueryClient: () => queryClientMock }; -}); - -import DiscoverPage from './+page.svelte'; -import { createCatalogueSearchQuery } from '#lib/api/catalogue.js'; -import { createSuggestionsQuery, createSnoozesQuery } from '#lib/api/suggestions.js'; -import { createRequest } from '#lib/api/requests.js'; - -const mockedCreateQuery = createCatalogueSearchQuery as ReturnType; -const mockedCreateSuggestionsQuery = createSuggestionsQuery as ReturnType; -const mockedCreateSnoozesQuery = createSnoozesQuery as ReturnType; -const mockedCreateRequest = createRequest as ReturnType; - -function result(over: Partial = {}): CatalogueSearchResult { - return { - mbid: 'mbid-1', - name: 'Boards of Canada', - secondary_text: 'Electronic', - image_url: '', - artist_mbid: 'art-1', - album_mbid: '', - in_library: false, - requested: false, - ...over - }; -} - -beforeEach(() => { - // Default: empty results, non-pending. Tests override per-case. - mockedCreateQuery.mockReturnValue(mockQuery({ data: [] })); - // Default: empty suggestion feed so its empty-state copy renders without - // interfering with search-mode tests. - mockedCreateSuggestionsQuery.mockReturnValue(mockQuery({ data: [] })); - // Nothing parked, which keeps the feed's empty-state copy on the - // "no signal yet" branch that this suite's assertions expect. - mockedCreateSnoozesQuery.mockReturnValue(mockQuery({ data: [] })); -}); - -afterEach(() => { - vi.clearAllMocks(); -}); - -describe('Discover page', () => { - test('initial state (no query) shows the suggestion feed', () => { - render(DiscoverPage); - expect(screen.getByText(/suggested for you/i)).toBeInTheDocument(); - }); - - test('empty input shows the suggestion feed', () => { - render(DiscoverPage); - expect(screen.getByText(/suggested for you/i)).toBeInTheDocument(); - // Kind tabs should NOT be visible when input is empty. - expect( - screen.queryByRole('button', { name: 'Artists' }) - ).not.toBeInTheDocument(); - }); - - test('typing replaces feed with search', async () => { - vi.useFakeTimers(); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'miles' } }); - await vi.advanceTimersByTimeAsync(250); - expect(screen.queryByText(/suggested for you/i)).not.toBeInTheDocument(); - expect(screen.getByText(/add music to the library/i)).toBeInTheDocument(); - // Kind tabs visible when searching. - expect(screen.getByRole('button', { name: 'Artists' })).toBeInTheDocument(); - vi.useRealTimers(); - }); - - test('debounced input fires query factory with typed value after 250ms', async () => { - vi.useFakeTimers(); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'boards' } }); - // Before the timer elapses, the factory was called only with the - // initial empty string — never with 'boards'. - expect(mockedCreateQuery).not.toHaveBeenCalledWith('boards', 'artist'); - await vi.advanceTimersByTimeAsync(250); - expect(mockedCreateQuery).toHaveBeenLastCalledWith('boards', 'artist'); - vi.useRealTimers(); - }); - - test('switching tabs refetches with the new kind', async () => { - vi.useFakeTimers(); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'boards' } }); - await vi.advanceTimersByTimeAsync(250); - // Switch to Albums. - await fireEvent.click(screen.getByRole('button', { name: 'Albums' })); - expect(mockedCreateQuery).toHaveBeenLastCalledWith('boards', 'album'); - // Then to Tracks. - await fireEvent.click(screen.getByRole('button', { name: 'Tracks' })); - expect(mockedCreateQuery).toHaveBeenLastCalledWith('boards', 'track'); - vi.useRealTimers(); - }); - - test('empty results state renders the voice-rule copy', async () => { - vi.useFakeTimers(); - mockedCreateQuery.mockReturnValue(mockQuery({ data: [] })); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'zzz' } }); - await vi.advanceTimersByTimeAsync(250); - await waitFor(() => - expect( - screen.getByText(/nothing to add for that search yet\./i) - ).toBeInTheDocument() - ); - vi.useRealTimers(); - }); - - test('artist-kind Request click calls createRequest immediately (no modal)', async () => { - vi.useFakeTimers(); - const r = result({ - mbid: 'art-mbid', - artist_mbid: 'art-mbid', - name: 'Boards of Canada' - }); - mockedCreateQuery.mockReturnValue(mockQuery({ data: [r] })); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'boards' } }); - await vi.advanceTimersByTimeAsync(250); - vi.useRealTimers(); - - const requestBtn = await screen.findByRole('button', { - name: /request boards of canada/i - }); - await fireEvent.click(requestBtn); - expect(mockedCreateRequest).toHaveBeenCalledTimes(1); - expect(mockedCreateRequest).toHaveBeenCalledWith( - expect.objectContaining({ kind: 'artist' }) - ); - // Modal must not be present for non-track kinds. - expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); - }); - - test('requestable card flips to "Requested" after a successful request', async () => { - vi.useFakeTimers(); - const r = result({ - mbid: 'art-mbid', - artist_mbid: 'art-mbid', - name: 'Boards of Canada' - }); - mockedCreateQuery.mockReturnValue(mockQuery({ data: [r] })); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'boards' } }); - await vi.advanceTimersByTimeAsync(250); - vi.useRealTimers(); - - const requestBtn = await screen.findByRole('button', { - name: /request boards of canada/i - }); - await fireEvent.click(requestBtn); - await waitFor(() => { - const flipped = screen.getByRole('button', { name: /already requested/i }); - expect(flipped).toBeDisabled(); - }); - }); - - test('submitting a request invalidates myRequests so /requests updates (#369)', async () => { - vi.useFakeTimers(); - const r = result({ mbid: 'art-mbid', artist_mbid: 'art-mbid', name: 'Boards of Canada' }); - mockedCreateQuery.mockReturnValue(mockQuery({ data: [r] })); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'boards' } }); - await vi.advanceTimersByTimeAsync(250); - vi.useRealTimers(); - - const requestBtn = await screen.findByRole('button', { - name: /request boards of canada/i - }); - await fireEvent.click(requestBtn); - await waitFor(() => { - expect(queryClientMock.invalidateQueries).toHaveBeenCalledWith({ - queryKey: ['myRequests'] - }); - }); - }); - - test('track-kind Request click opens confirm modal; Confirm calls createRequest', async () => { - vi.useFakeTimers(); - const r = result({ - mbid: 'tr-mbid', - artist_mbid: 'art-mbid', - album_mbid: 'al-mbid', - name: 'Roygbiv', - secondary_text: 'Music Has The Right To Children · Boards of Canada' - }); - mockedCreateQuery.mockReturnValue(mockQuery({ data: [r] })); - render(DiscoverPage); - // Type first so the kind tabs become visible (empty-input mode shows the - // suggestion feed and hides tabs). - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'roy' } }); - await vi.advanceTimersByTimeAsync(250); - // Switch to track kind. - await fireEvent.click(screen.getByRole('button', { name: 'Tracks' })); - vi.useRealTimers(); - - const requestBtn = await screen.findByRole('button', { - name: /request roygbiv/i - }); - await fireEvent.click(requestBtn); - // Modal opens with the explanation copy. - const dialog = await screen.findByRole('dialog'); - expect(dialog).toBeInTheDocument(); - expect(dialog.textContent).toMatch(/continue/i); - // Confirm fires createRequest with kind=track. - await fireEvent.click(screen.getByRole('button', { name: /add the album/i })); - expect(mockedCreateRequest).toHaveBeenCalledTimes(1); - expect(mockedCreateRequest).toHaveBeenCalledWith( - expect.objectContaining({ - kind: 'track', - track_mbid: 'tr-mbid', - album_mbid: 'al-mbid', - artist_mbid: 'art-mbid' - }) - ); - }); - - test('track-kind modal Cancel does not call createRequest', async () => { - vi.useFakeTimers(); - const r = result({ - mbid: 'tr-mbid', - artist_mbid: 'art-mbid', - album_mbid: 'al-mbid', - name: 'Roygbiv', - secondary_text: 'Music Has The Right To Children · Boards of Canada' - }); - mockedCreateQuery.mockReturnValue(mockQuery({ data: [r] })); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'roy' } }); - await vi.advanceTimersByTimeAsync(250); - await fireEvent.click(screen.getByRole('button', { name: 'Tracks' })); - vi.useRealTimers(); - - const requestBtn = await screen.findByRole('button', { - name: /request roygbiv/i - }); - await fireEvent.click(requestBtn); - await screen.findByRole('dialog'); - await fireEvent.click(screen.getByRole('button', { name: /^cancel$/i })); - expect(mockedCreateRequest).not.toHaveBeenCalled(); - expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); - }); - - test('in_library result renders kept state regardless of requested', async () => { - vi.useFakeTimers(); - const r = result({ - mbid: 'kept-1', - name: 'Kind of Blue', - in_library: true, - requested: true - }); - mockedCreateQuery.mockReturnValue(mockQuery({ data: [r] })); - render(DiscoverPage); - const input = screen.getByLabelText(/search musicbrainz/i); - await fireEvent.input(input, { target: { value: 'kind' } }); - await vi.advanceTimersByTimeAsync(250); - vi.useRealTimers(); - - await waitFor(() => { - expect(screen.getByRole('button', { name: /in library/i })).toBeDisabled(); - }); - }); -}); diff --git a/web/src/routes/requests/+page.svelte b/web/src/routes/requests/+page.svelte deleted file mode 100644 index 10753c59..00000000 --- a/web/src/routes/requests/+page.svelte +++ /dev/null @@ -1,101 +0,0 @@ - - -{pageTitle('Requests')} - - - -
-
-

- Your requests -

-

- What you've asked Minstrel to add to the library. -

-
- - {#if query.isError} - - {:else if query.isPending} -

Reading the ledger…

- {:else if requests.length === 0} -

Nothing requested yet.

- {:else} -
    - {#each requests as r (r.id)} - {@const href = listenHref(r)} - - {#snippet details()} - {#if r.status === 'rejected' && r.notes} -
    - {r.notes} -
    - {/if} - {/snippet} - {#snippet actions()} - {#if r.status === 'pending'} - - {:else if r.status === 'completed' && href} - - Listen - - {/if} - {/snippet} -
    - {/each} -
- {/if} -
diff --git a/web/src/routes/requests/requests.test.ts b/web/src/routes/requests/requests.test.ts deleted file mode 100644 index 8dc11509..00000000 --- a/web/src/routes/requests/requests.test.ts +++ /dev/null @@ -1,165 +0,0 @@ -import { afterEach, describe, expect, test, vi } from 'vitest'; -import { render, screen, fireEvent, waitFor } from '@testing-library/svelte'; -import { mockQuery } from '../../test-utils/query'; -import { apiClientMock } from '../../test-utils/mocks/client'; -import type { MusicRequest } from '#lib/api/types.js'; -import { pageUrlModule } from '../../test-utils/mocks/appState'; - -// $app/state / $app/navigation must be mocked or rendering the page -// pulls in SvelteKit's client runtime (`notifiable_store is not a -// function`) at module load. Same pattern as the other route tests. -const pageState = vi.hoisted(() => ({ - pageUrl: new URL('http://localhost/requests') -})); -vi.mock('$app/state', () => pageUrlModule(pageState)); -vi.mock('$app/navigation', () => ({ goto: vi.fn() })); - -// Capture invalidateQueries on the mocked QueryClient so the cancel test can -// assert against the same instance the page consumed. Per-file mock OVERRIDES -// the vitest.setup default so assertions see this named spy. -const invalidateQueries = vi.fn().mockResolvedValue(undefined); -vi.mock('@tanstack/svelte-query', async (orig) => { - const actual = (await orig()) as Record; - return { ...actual, useQueryClient: () => ({ invalidateQueries }) }; -}); - -vi.mock('#lib/api/requests.js', () => ({ - createMyRequestsQuery: vi.fn(), - cancelRequest: vi.fn().mockResolvedValue({ id: 'r1' }) -})); - -vi.mock('#lib/api/queries.js', () => ({ - qk: { - myRequests: () => ['myRequests'] - } -})); - -vi.mock('#lib/api/client.js', () => apiClientMock()); - -import RequestsPage from './+page.svelte'; -import { createMyRequestsQuery, cancelRequest } from '#lib/api/requests.js'; -import { qk } from '#lib/api/queries.js'; - -const mockedCreateMyRequestsQuery = createMyRequestsQuery as ReturnType; -const mockedCancelRequest = cancelRequest as ReturnType; - -function req(over: Partial = {}): MusicRequest { - return { - id: 'r1', - user_id: 'u1', - status: 'pending', - kind: 'album', - artist_mbid: 'art-mbid', - album_mbid: 'al-mbid', - track_mbid: null, - artist_name: 'Boards of Canada', - album_title: 'Music Has The Right To Children', - track_title: null, - decided_at: null, - decided_by: null, - notes: null, - completed_at: null, - matched_track_id: null, - matched_album_id: null, - matched_artist_id: null, - requested_at: '2026-04-28T12:00:00Z', - updated_at: '2026-04-28T12:00:00Z', - imported_album_count: 0, - imported_track_count: 0, - ...over - }; -} - -afterEach(() => { - vi.clearAllMocks(); -}); - -describe('Requests page', () => { - test('renders one row per request from the API', () => { - const rows: MusicRequest[] = [ - req({ id: 'r1', kind: 'artist', artist_name: 'Boards of Canada' }), - req({ id: 'r2', kind: 'album', album_title: 'Geogaddi', status: 'completed' }), - req({ id: 'r3', kind: 'track', track_title: 'Roygbiv', status: 'rejected', notes: 'Already in library' }) - ]; - mockedCreateMyRequestsQuery.mockReturnValue(mockQuery({ data: rows })); - render(RequestsPage); - expect(screen.getAllByTestId('request-row')).toHaveLength(3); - expect(screen.getByText('Boards of Canada')).toBeInTheDocument(); - expect(screen.getByText('Geogaddi')).toBeInTheDocument(); - expect(screen.getByText('Roygbiv')).toBeInTheDocument(); - }); - - test('pending row exposes Cancel; clicking calls cancelRequest and invalidates myRequests', async () => { - const rows: MusicRequest[] = [ - req({ id: 'r-pending', status: 'pending', kind: 'album', album_title: 'Geogaddi' }) - ]; - mockedCreateMyRequestsQuery.mockReturnValue(mockQuery({ data: rows })); - render(RequestsPage); - const cancelBtn = screen.getByRole('button', { name: /cancel request for geogaddi/i }); - await fireEvent.click(cancelBtn); - await waitFor(() => { - expect(mockedCancelRequest).toHaveBeenCalledWith('r-pending'); - expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: qk.myRequests() }); - }); - }); - - test('completed row with matched_track_id renders Listen link to /tracks/', () => { - const rows: MusicRequest[] = [ - req({ - id: 'r-track', - status: 'completed', - kind: 'track', - track_title: 'Roygbiv', - matched_track_id: 'tr-9', - matched_album_id: 'al-9', - matched_artist_id: 'art-9' - }) - ]; - mockedCreateMyRequestsQuery.mockReturnValue(mockQuery({ data: rows })); - render(RequestsPage); - const link = screen.getByRole('link', { name: /listen to roygbiv/i }); - expect(link).toHaveAttribute('href', '/tracks/tr-9'); - }); - - test('completed row with only matched_album_id renders Listen link to /albums/', () => { - const rows: MusicRequest[] = [ - req({ - id: 'r-album', - status: 'completed', - kind: 'album', - album_title: 'Geogaddi', - matched_track_id: null, - matched_album_id: 'al-7', - matched_artist_id: 'art-7' - }) - ]; - mockedCreateMyRequestsQuery.mockReturnValue(mockQuery({ data: rows })); - render(RequestsPage); - const link = screen.getByRole('link', { name: /listen to geogaddi/i }); - expect(link).toHaveAttribute('href', '/albums/al-7'); - }); - - test('rejected row renders notes and hides Cancel and Listen', () => { - const rows: MusicRequest[] = [ - req({ - id: 'r-rej', - status: 'rejected', - kind: 'album', - album_title: 'Geogaddi', - notes: 'Already kept in the library.' - }) - ]; - mockedCreateMyRequestsQuery.mockReturnValue(mockQuery({ data: rows })); - render(RequestsPage); - expect(screen.getByTestId('rejection-notes')).toHaveTextContent('Already kept in the library.'); - expect(screen.queryByRole('button', { name: /cancel request/i })).not.toBeInTheDocument(); - expect(screen.queryByRole('link', { name: /listen to/i })).not.toBeInTheDocument(); - }); - - test('empty list shows the voice-rule "Nothing requested yet." copy', () => { - mockedCreateMyRequestsQuery.mockReturnValue(mockQuery({ data: [] })); - render(RequestsPage); - expect(screen.getByText(/nothing requested yet\./i)).toBeInTheDocument(); - expect(screen.queryAllByTestId('request-row')).toHaveLength(0); - }); -});