diff options
Diffstat (limited to 'src/frontend/mod.rs')
-rw-r--r-- | src/frontend/mod.rs | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 5426f7e..76114c5 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -1,3 +1,5 @@ +use std::convert::TryInto; + use crate::database::Storage; use crate::ApplicationState; use log::{error, info}; @@ -6,8 +8,9 @@ use tide::{Next, Request, Response, Result, StatusCode}; static POSTS_PER_PAGE: usize = 20; -mod templates; +pub mod login; +mod templates; use templates::{ErrorPage, MainPage, OnboardingPage, Template}; #[derive(Clone, Serialize, Deserialize)] @@ -30,11 +33,11 @@ struct FrontendError { code: StatusCode, } impl FrontendError { - pub fn with_code(code: StatusCode, msg: &str) -> Self { + pub fn with_code<C>(code: C, msg: &str) -> Self where C: TryInto<StatusCode> { Self { msg: msg.to_string(), source: None, - code, + code: code.try_into().unwrap_or_else(|_| StatusCode::InternalServerError), } } pub fn msg(&self) -> &str { @@ -227,7 +230,7 @@ pub async fn mainpage<S: Storage>(mut req: Request<ApplicationState<S>>) -> Resu let query = req.query::<QueryParams>()?; let authorization_endpoint = req.state().authorization_endpoint.to_string(); let token_endpoint = req.state().token_endpoint.to_string(); - let user: Option<String> = None; + let user: Option<String> = req.session().get("user"); #[cfg(any(not(debug_assertions), test))] let url = req.url(); @@ -260,6 +263,7 @@ pub async fn mainpage<S: Storage>(mut req: Request<ApplicationState<S>>) -> Resu microsub: None, }, feeds: Vec::default(), + user: None, content: OnboardingPage {}.to_string(), } .to_string(), @@ -288,6 +292,7 @@ pub async fn mainpage<S: Storage>(mut req: Request<ApplicationState<S>>) -> Resu .get_channels(hcard_url) .await .unwrap_or_else(|_| Vec::default()), + user, content: MainPage { feed: &feed?, card: &card?, @@ -304,7 +309,7 @@ pub async fn render_post<S: Storage>(mut req: Request<ApplicationState<S>>) -> R let query = req.query::<QueryParams>()?; let authorization_endpoint = req.state().authorization_endpoint.to_string(); let token_endpoint = req.state().token_endpoint.to_string(); - let user: Option<String> = None; + let user: Option<String> = req.session().get("user"); // This cannot error out as the URL must be valid. Or there is something horribly wrong // and we shouldn't serve this request anyway. @@ -381,6 +386,7 @@ pub async fn render_post<S: Storage>(mut req: Request<ApplicationState<S>>) -> R .get_channels(&owner) .await .unwrap_or_else(|_| Vec::default()), + user, content: template, } .to_string(), @@ -415,11 +421,16 @@ where .get_channels(&owner) .await .unwrap_or_else(|_| Vec::default()); + let user: Option<String> = request.session().get("user"); let mut res = next.run(request).await; let mut code: Option<StatusCode> = None; + let mut msg: Option<String> = None; if let Some(err) = res.downcast_error::<FrontendError>() { code = Some(err.code()); error!("Error caught while processing request: {}", err.msg()); + if err.code() == 400 { + msg = Some(err.msg().to_string()); + } let mut err: &dyn std::error::Error = err; while let Some(e) = err.source() { error!("Caused by: {}", e); @@ -439,8 +450,9 @@ where webmention: None, microsub: None, }, - feeds: feeds, - content: ErrorPage { code }.to_string(), + feeds, + user, + content: ErrorPage { code, msg }.to_string(), } .to_string(), ); |