From 3faa6ea8aaa02c24c5527ea8447b37fda171bdb2 Mon Sep 17 00:00:00 2001 From: "naps62-yolo (agent)" Date: Sun, 15 Nov 2026 15:15:04 +0000 Subject: [PATCH] feat(api): remove a release from the blacklist (#283) --- ...fd3c8527809ae8b8210215e156c0d698fe55e.json | 12 ++ DESIGN.md | 4 + crates/arr-api/src/blacklist.rs | 112 ++++++++++++++++++ crates/arr-api/src/lib.rs | 2 + crates/arr-api/src/movies.rs | 6 + crates/arr-db/src/blacklist.rs | 48 +++++++- 6 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 .sqlx/query-29730d6ced199423f839479c298fd3c8527809ae8b8210215e156c0d698fe55e.json create mode 100644 crates/arr-api/src/blacklist.rs diff --git a/.sqlx/query-29730d6ced199423f839479c298fd3c8527809ae8b8210215e156c0d698fe55e.json b/.sqlx/query-29730d6ced199423f839479c298fd3c8527809ae8b8210215e156c0d698fe55e.json new file mode 100644 index 0000000..a0e25a0 --- /dev/null +++ b/.sqlx/query-29730d6ced199423f839479c298fd3c8527809ae8b8210215e156c0d698fe55e.json @@ -0,0 +1,12 @@ +{ + "db_name": "SQLite", + "query": "DELETE FROM blacklist\n WHERE infohash = ?1\n OR normalised_name IN (SELECT normalised_name FROM blacklist WHERE infohash = ?1)", + "describe": { + "columns": [], + "parameters": { + "Right": 1 + }, + "nullable": [] + }, + "hash": "29730d6ced199423f839479c298fd3c8527809ae8b8210215e156c0d698fe55e" +} diff --git a/DESIGN.md b/DESIGN.md index f173df6..68f233d 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -541,6 +541,10 @@ single largest source of wasted queries in Radarr and it is free to avoid. Keyed on infohash and normalised release name. Anything that hard-failed post-`ffprobe` is never grabbed again, including by RSS. +The operator can lift one by hand: `DELETE /api/blacklist?infohash=…` drops +the infohash key and every name key it shares, for a hard fail that turned out +not to be one. Nothing in the grab or import path ever un-blacklists. + A manual `blocked` flag stops targeted search for a title entirely while leaving RSS matching on. diff --git a/crates/arr-api/src/blacklist.rs b/crates/arr-api/src/blacklist.rs new file mode 100644 index 0000000..53be6bb --- /dev/null +++ b/crates/arr-api/src/blacklist.rs @@ -0,0 +1,112 @@ +//! Manual removal from the release blacklist (`DESIGN.md` §6.3). + +use axum::extract::{Query, State}; +use axum::http::StatusCode; +use serde::Deserialize; +use utoipa::IntoParams; + +use crate::movies::{pool, ApiError, ErrorBody}; +use crate::state::AppState; + +#[derive(Debug, Deserialize, IntoParams)] +pub struct RemoveQuery { + /// The blacklisted torrent's v1 infohash, any case. + pub infohash: String, +} + +/// Un-blacklist one release, by both its infohash and its normalised name. +/// The operator's override for a hard fail that should not have been one. +#[utoipa::path( + delete, path = "/api/blacklist", tag = "blacklist", + params(RemoveQuery), + responses( + (status = 204), + (status = 404, body = ErrorBody), + (status = 500, body = ErrorBody), + (status = 503, body = ErrorBody) + ) +)] +pub async fn remove( + State(state): State, + Query(query): Query, +) -> Result { + let removed = arr_db::blacklist::remove(pool(&state)?, &query.infohash).await?; + if removed == 0 { + return Err(ApiError::BlacklistEntryNotFound); + } + tracing::info!( + infohash = query.infohash, + removed, + "blacklist entry removed by hand" + ); + Ok(StatusCode::NO_CONTENT) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{router, Upstreams}; + + const HASH: &str = "72be1ef095cacd4edece29c28b3d3b36f4fafa1d"; + + async fn application() -> (tempfile::TempDir, AppState, String) { + let dir = tempfile::tempdir().expect("tempdir"); + let database = arr_db::Db::connect(dir.path().join("arr.db")) + .await + .expect("connect database"); + database.migrate().await.expect("migrate database"); + let state = AppState::new(Upstreams::new( + "http://127.0.0.1:1".into(), + "http://127.0.0.1:1".into(), + )) + .expect("state") + .with_database(database); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0") + .await + .expect("bind"); + let address = listener.local_addr().expect("address"); + let app = router(state.clone()); + tokio::spawn(async move { axum::serve(listener, app).await.expect("serve") }); + (dir, state, format!("http://{address}")) + } + + async fn delete(base: &str, infohash: &str) -> StatusCode { + reqwest::Client::new() + .delete(format!("{base}/api/blacklist?infohash={infohash}")) + .send() + .await + .expect("delete") + .status() + } + + #[tokio::test] + async fn deleting_a_blacklisted_infohash_unblocks_it() { + let (_dir, state, base) = application().await; + let database = state.database().expect("database"); + arr_db::blacklist::add( + database.pool(), + Some(HASH), + "Silo.S03.2160p.ATVP.WEB-DL.DDP5.1.H.265-NTb", + "required_audio", + ) + .await + .expect("blacklist"); + + assert_eq!( + delete(&base, &HASH.to_ascii_uppercase()).await, + StatusCode::NO_CONTENT + ); + + let blacklist = arr_db::Blacklist::load(database.pool()) + .await + .expect("load"); + assert!(!blacklist.blocks_infohash(HASH)); + assert!(!blacklist.blocks_name("Silo.S03.2160p.ATVP.WEB-DL.DDP5.1.H.265-NTb")); + } + + #[tokio::test] + async fn deleting_an_unknown_infohash_is_not_found() { + let (_dir, _state, base) = application().await; + assert_eq!(delete(&base, HASH).await, StatusCode::NOT_FOUND); + } +} diff --git a/crates/arr-api/src/lib.rs b/crates/arr-api/src/lib.rs index 0eb0ed1..294d017 100644 --- a/crates/arr-api/src/lib.rs +++ b/crates/arr-api/src/lib.rs @@ -6,6 +6,7 @@ //! carrying a `#[utoipa::path]` annotation. A handler added without one fails //! to compile, and the gate in DESIGN.md §12 fails with it. +mod blacklist; mod downloads; mod health; pub mod jellyfin; @@ -97,6 +98,7 @@ fn api_router() -> OpenApiRouter { OpenApiRouter::with_openapi(ApiDoc::openapi()) .routes(routes!(health::health)) .routes(routes!(downloads::list)) + .routes(routes!(blacklist::remove)) .routes(routes!(movies::list, movies::create)) .routes(routes!(movies::get, movies::update, movies::delete)) .routes(routes!(movies::search)) diff --git a/crates/arr-api/src/movies.rs b/crates/arr-api/src/movies.rs index c48eb21..c53c61c 100644 --- a/crates/arr-api/src/movies.rs +++ b/crates/arr-api/src/movies.rs @@ -196,6 +196,7 @@ pub enum ApiError { OwnerNotFound, PolicyNotFound, RootNotFound, + BlacklistEntryNotFound, /// A `media_files` row that is not there. Named apart from /// [`Self::NotFound`] because the subtitle surface (§15) is keyed on /// files, not on titles, and "movie not found" would misdirect. @@ -244,6 +245,11 @@ impl IntoResponse for ApiError { Self::OwnerNotFound => (StatusCode::NOT_FOUND, "owner not found".to_string(), None), Self::PolicyNotFound => (StatusCode::NOT_FOUND, "policy not found".to_string(), None), Self::RootNotFound => (StatusCode::NOT_FOUND, "root not found".to_string(), None), + Self::BlacklistEntryNotFound => ( + StatusCode::NOT_FOUND, + "no blacklist entry for that infohash".to_string(), + None, + ), Self::MediaFileNotFound => ( StatusCode::NOT_FOUND, "media file not found".to_string(), diff --git a/crates/arr-db/src/blacklist.rs b/crates/arr-db/src/blacklist.rs index 2dd6448..1983cde 100644 --- a/crates/arr-db/src/blacklist.rs +++ b/crates/arr-db/src/blacklist.rs @@ -173,6 +173,27 @@ pub async fn add( Ok(()) } +/// Lift a blacklisting by hand: drop the row keyed on `infohash` and every +/// row sharing its normalised name, so neither §6.3 key blocks the release +/// any longer. Only the operator calls this; nothing in the grab or import +/// path un-blacklists. Returns the number of rows removed. +/// +/// # Errors +/// +/// If the delete fails. +pub async fn remove(pool: &SqlitePool, infohash: &str) -> Result { + let infohash = infohash.to_ascii_lowercase(); + let removed = sqlx::query!( + "DELETE FROM blacklist + WHERE infohash = ?1 + OR normalised_name IN (SELECT normalised_name FROM blacklist WHERE infohash = ?1)", + infohash + ) + .execute(pool) + .await?; + Ok(removed.rows_affected()) +} + /// The infohash a magnet link declares, lowercased. /// /// Only the 40-character hex form is recognised. Base32 `btih` values exist @@ -193,7 +214,7 @@ pub fn magnet_infohash(download_url: &str) -> Option { #[cfg(test)] #[allow(clippy::unwrap_used)] mod tests { - use super::{add, magnet_infohash, Blacklist}; + use super::{add, magnet_infohash, remove, Blacklist}; use crate::Db; const HASH: &str = "0123456789abcdef0123456789abcdef01234567"; @@ -224,6 +245,31 @@ mod tests { assert!(!blacklist.blocks_name("Dune Part Two 2024 1080p WEB-DL")); } + #[tokio::test] + async fn removing_by_infohash_clears_both_keys() { + let (database, _dir) = database().await; + let name = "Silo.S03.2160p.ATVP.WEB-DL.DDP5.1.H.265-NTb"; + add(database.pool(), Some(HASH), name, "required_audio") + .await + .unwrap(); + add(database.pool(), None, name, "required_audio") + .await + .unwrap(); + add(database.pool(), None, "Other.Release.2024.1080p", "size") + .await + .unwrap(); + + let removed = remove(database.pool(), &HASH.to_ascii_uppercase()) + .await + .unwrap(); + + assert_eq!(removed, 2); + let blacklist = Blacklist::load(database.pool()).await.unwrap(); + assert!(!blacklist.blocks_infohash(HASH)); + assert!(!blacklist.blocks_name(name)); + assert!(blacklist.blocks_name("Other.Release.2024.1080p")); + } + #[tokio::test] async fn a_key_carries_the_reason_it_was_blacklisted_for() { let (database, _dir) = database().await;