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) {
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::<GetPostLight, _>(&self.client, URL, vars)
.await
.unwrap();
let res = res.data.expect("missing response data").post.unwrap();
PostUrl {
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,
}),
}
}
}
}

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")]
async fn by_post_id(path: web::Path<String>, data: AppData) -> impl Responder {
let post_data = data.get_post_light(&path).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<String>, 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(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()
.finish(),
}
} else {
HttpResponse::NotFound().body("Post not found, please file bug report")
}