Merge pull request 'update get_post_light to return none when no data is found, so not found error can happen' (#53) from halkeye/libmedium:handle-post-not-found into master

Reviewed-on: #53
Reviewed-by: Aravinth Manivannan <realaravinth@batsense.net>
This commit is contained in:
Aravinth Manivannan 2024-07-28 16:14:19 +05:30
commit eb5f3067c1
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")
}