From 047fc9e887f162eca2b4e7c9f75d6e09956d318c Mon Sep 17 00:00:00 2001 From: Sanapach Date: Thu, 19 Nov 2026 21:19:22 +0200 Subject: [PATCH] Keep the podcast API's 401 path out of a large Result Clippy flagged the helper that returned the 401 response as its error variant (result_large_err); it now returns the account id as an Option. Co-Authored-By: Claude Opus 5.5 --- src/podcasts/http.rs | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/src/podcasts/http.rs b/src/podcasts/http.rs index b6bdbff..59948cd 100644 --- a/src/podcasts/http.rs +++ b/src/podcasts/http.rs @@ -93,16 +93,15 @@ fn api_response(result: api::ApiResult) -> cot::Result Result { - match auth::get_request_user(auth_ctx, session, db).await { - Some(user) => Ok(user.id), - None => Err(json_error(StatusCode::UNAUTHORIZED, "not authenticated")), - } +/// The signed-in account (session or bearer token). +async fn signed_in(auth_ctx: &auth::AuthContext, session: &Session, db: &Database) -> Option { + auth::get_request_user(auth_ctx, session, db) + .await + .map(|user| user.id) +} + +fn unauthorized() -> cot::response::Response { + json_error(StatusCode::UNAUTHORIZED, "not authenticated") } /// Largest JSON body accepted (a playback request carries one episode reference). @@ -413,9 +412,8 @@ async fn list_handler( db: Database, query: UrlQuery, ) -> cot::Result { - let user_id = match signed_in(&auth_ctx, &session, &db).await { - Ok(user_id) => user_id, - Err(response) => return Ok(response), + let Some(user_id) = signed_in(&auth_ctx, &session, &db).await else { + return Ok(unauthorized()); }; let all = matches!(query.0.all.as_deref(), Some("1" | "true")); let store = state.store().await?; @@ -429,9 +427,8 @@ async fn subscribe_url_handler( db: Database, request: cot::request::Request, ) -> cot::Result { - let user_id = match signed_in(&auth_ctx, &session, &db).await { - Ok(user_id) => user_id, - Err(response) => return Ok(response), + let Some(user_id) = signed_in(&auth_ctx, &session, &db).await else { + return Ok(unauthorized()); }; let body = match json_body(request).await { Ok(body) => body, @@ -458,9 +455,8 @@ async fn podcast_handler( db: Database, path: Path, ) -> cot::Result { - let user_id = match signed_in(&auth_ctx, &session, &db).await { - Ok(user_id) => user_id, - Err(response) => return Ok(response), + let Some(user_id) = signed_in(&auth_ctx, &session, &db).await else { + return Ok(unauthorized()); }; let podcast_id = match api::parse_podcast_id(&path.0.podcast_id) { Ok(id) => id, @@ -488,9 +484,8 @@ async fn episodes_handler( path: Path, query: UrlQuery, ) -> cot::Result { - let user_id = match signed_in(&auth_ctx, &session, &db).await { - Ok(user_id) => user_id, - Err(response) => return Ok(response), + let Some(user_id) = signed_in(&auth_ctx, &session, &db).await else { + return Ok(unauthorized()); }; let podcast_id = match api::parse_podcast_id(&path.0.podcast_id) { Ok(id) => id, @@ -517,9 +512,8 @@ async fn episode_handler( path: Path, request: cot::request::Request, ) -> cot::Result { - let user_id = match signed_in(&auth_ctx, &session, &db).await { - Ok(user_id) => user_id, - Err(response) => return Ok(response), + let Some(user_id) = signed_in(&auth_ctx, &session, &db).await else { + return Ok(unauthorized()); }; let episode_id = match api::parse_episode_id(&path.0.episode_id) { Ok(id) => id,