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

Merged
realaravinth merged 1 commit from halkeye/libmedium:handle-post-not-found into master 2024-07-28 16:14:19 +05:30
2 changed files with 34 additions and 24 deletions
Showing only changes of commit 3111f3e25c - Show all commits

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
slug: res.unique_slug, } else {
username: res.creator.username, 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")] #[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"),
.append_header(( Some(post_data) => HttpResponse::Found()
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<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()
.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")]
async fn by_top_level_post(path: web::Path<String>, 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 { } else {
HttpResponse::NotFound().body("Post not found, please file bug report") HttpResponse::NotFound().body("Post not found, please file bug report")
} }