update get_post_light to return none when no data is found, so not found error can happen

This commit is contained in:
Gavin Mogan 2024-07-27 15:10:46 -07:00
parent 8bddddea2c
commit 3111f3e25c
2 changed files with 34 additions and 24 deletions

View file

@ -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<PostUrl> {
match self.posts.get(id) { match self.posts.get(id) {
Ok(Some(v)) => { Ok(Some(v)) => {
let cached: PostResp = bincode::deserialize(&v[..]).unwrap(); let cached: PostResp = bincode::deserialize(&v[..]).unwrap();
PostUrl { Some(PostUrl {
slug: cached.unique_slug, slug: cached.unique_slug,
username: cached.creator.username, username: cached.creator.username,
} })
} }
_ => { _ => {
let vars = get_post_light::Variables { id: id.to_owned() }; let vars = get_post_light::Variables { id: id.to_owned() };
@ -180,10 +180,16 @@ impl Data {
let res = post_graphql::<GetPostLight, _>(&self.client, URL, vars) let res = post_graphql::<GetPostLight, _>(&self.client, URL, vars)
.await .await
.unwrap(); .unwrap();
let res = res.data.expect("missing response data").post.unwrap(); if res.data.is_none() {
PostUrl { None
} else {
match res.data.expect("missing response data").post {
None => None,
Some(res) => Some(PostUrl {
slug: res.unique_slug, slug: res.unique_slug,
username: res.creator.username, username: res.creator.username,
}),
}
} }
} }
} }

View file

@ -158,29 +158,33 @@ async fn assets(path: web::Path<String>, data: AppData) -> impl Responder {
#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.proxy.by_post_id")] #[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.proxy.by_post_id")]
async fn by_post_id(path: web::Path<String>, data: AppData) -> impl Responder { async fn by_post_id(path: web::Path<String>, data: AppData) -> impl Responder {
let post_data = data.get_post_light(&path).await; match data.get_post_light(&path).await {
HttpResponse::Found() None => HttpResponse::NotFound().body("Post not found"),
Some(post_data) => HttpResponse::Found()
.append_header(( .append_header((
header::LOCATION, header::LOCATION,
crate::V1_API_ROUTES crate::V1_API_ROUTES
.proxy .proxy
.get_page(&post_data.username, &post_data.slug), .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")] #[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.proxy.top_level_post")]
async fn by_top_level_post(path: web::Path<String>, data: AppData) -> impl Responder { async fn by_top_level_post(path: web::Path<String>, data: AppData) -> impl Responder {
if let Some(post_id) = path.split('-').last() { if let Some(post_id) = path.split('-').last() {
let post_data = data.get_post_light(post_id).await; match data.get_post_light(post_id).await {
HttpResponse::Found() None => HttpResponse::NotFound().body("Post not found"),
Some(post_data) => HttpResponse::Found()
.append_header(( .append_header((
header::LOCATION, header::LOCATION,
crate::V1_API_ROUTES crate::V1_API_ROUTES
.proxy .proxy
.get_page(&post_data.username, &post_data.slug), .get_page(&post_data.username, &post_data.slug),
)) ))
.finish() .finish(),
}
} else { } else {
HttpResponse::NotFound().body("Post not found, please file bug report") HttpResponse::NotFound().body("Post not found, please file bug report")
} }