Skip to content

Commit

Permalink
redirect page when title has only one page
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Sep 6, 2020
1 parent f09066e commit 748ea23
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/handler/title.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::handler_helpers::is_all;
use crate::helpers::{read_obsoleted_map, read_title_map};
use crate::page_id::PageId;
use crate::page_title::PageTitle;
use crate::template::{PageItemTemplate, TitleTemplate};
use crate::url_helpers::{page_url, title_url};
Expand All @@ -13,22 +14,32 @@ pub async fn title(req: actix_web::HttpRequest) -> std::io::Result<HttpResponse>
let title_map = read_title_map()?;
let title = PageTitle::from_str(&params.0);
if let Some(page_ids) = title_map.get(&title) {
let pages = page_ids
let page_ids = page_ids
.iter()
.map(|page_id| PageItemTemplate {
id: page_id.to_string(),
obsoleted: obsoleted_map.get(page_id).is_some(),
url: page_url(&page_id),
})
.filter(|template| all || !template.obsoleted)
.collect::<Vec<PageItemTemplate>>();
let template = TitleTemplate {
title: title.as_str(),
title_url: &title_url(&title),
pages: &pages,
};
let html = template.render().unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(html))
.filter(|page_id| all || !obsoleted_map.get(page_id).is_some())
.map(|&x| x)
.collect::<Vec<PageId>>();
if page_ids.len() == 1 {
Ok(HttpResponse::Found()
.header(actix_web::http::header::LOCATION, page_url(&page_ids[0]))
.finish())
} else {
let pages = page_ids
.iter()
.map(|page_id| PageItemTemplate {
id: page_id.to_string(),
obsoleted: obsoleted_map.get(page_id).is_some(),
url: page_url(&page_id),
})
.collect::<Vec<PageItemTemplate>>();
let template = TitleTemplate {
title: title.as_str(),
title_url: &title_url(&title),
pages: &pages,
};
let html = template.render().unwrap();
Ok(HttpResponse::Ok().content_type("text/html").body(html))
}
} else {
Ok(HttpResponse::NotFound().body("Not Found"))
}
Expand Down

0 comments on commit 748ea23

Please sign in to comment.