From 3111f3e25c7e4881e9f6e67b182112645fd7d6b7 Mon Sep 17 00:00:00 2001 From: Gavin Mogan Date: Sat, 27 Jul 2024 15:10:46 -0700 Subject: [PATCH] update get_post_light to return none when no data is found, so not found error can happen --- src/data.rs | 20 +++++++++++++------- src/proxy.rs | 38 +++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/data.rs b/src/data.rs index 8d891fb..e840249 100644 --- a/src/data.rs +++ b/src/data.rs @@ -164,14 +164,14 @@ impl Data { } } - pub async fn get_post_light(&self, id: &str) -> PostUrl { + pub async fn get_post_light(&self, id: &str) -> Option { match self.posts.get(id) { Ok(Some(v)) => { let cached: PostResp = bincode::deserialize(&v[..]).unwrap(); - PostUrl { + Some(PostUrl { slug: cached.unique_slug, username: cached.creator.username, - } + }) } _ => { let vars = get_post_light::Variables { id: id.to_owned() }; @@ -180,10 +180,16 @@ impl Data { let res = post_graphql::(&self.client, URL, vars) .await .unwrap(); - let res = res.data.expect("missing response data").post.unwrap(); - PostUrl { - slug: res.unique_slug, - username: res.creator.username, + if res.data.is_none() { + None + } else { + match res.data.expect("missing response data").post { + None => None, + Some(res) => Some(PostUrl { + slug: res.unique_slug, + username: res.creator.username, + }), + } } } } diff --git a/src/proxy.rs b/src/proxy.rs index 310039b..69a35c6 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -158,29 +158,33 @@ async fn assets(path: web::Path, data: AppData) -> impl Responder { #[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.proxy.by_post_id")] async fn by_post_id(path: web::Path, data: AppData) -> impl Responder { - let post_data = data.get_post_light(&path).await; - HttpResponse::Found() - .append_header(( - header::LOCATION, - crate::V1_API_ROUTES - .proxy - .get_page(&post_data.username, &post_data.slug), - )) - .finish() -} - -#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.proxy.top_level_post")] -async fn by_top_level_post(path: web::Path, data: AppData) -> impl Responder { - if let Some(post_id) = path.split('-').last() { - let post_data = data.get_post_light(post_id).await; - HttpResponse::Found() + match data.get_post_light(&path).await { + None => HttpResponse::NotFound().body("Post not found"), + Some(post_data) => HttpResponse::Found() .append_header(( header::LOCATION, crate::V1_API_ROUTES .proxy .get_page(&post_data.username, &post_data.slug), )) - .finish() + .finish(), + } +} + +#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.proxy.top_level_post")] +async fn by_top_level_post(path: web::Path, data: AppData) -> impl Responder { + if let Some(post_id) = path.split('-').last() { + match data.get_post_light(post_id).await { + None => HttpResponse::NotFound().body("Post not found"), + Some(post_data) => HttpResponse::Found() + .append_header(( + header::LOCATION, + crate::V1_API_ROUTES + .proxy + .get_page(&post_data.username, &post_data.slug), + )) + .finish(), + } } else { HttpResponse::NotFound().body("Post not found, please file bug report") } -- 2.39.2