From 9a2791d745f4a9f0307eb69b50de5629f51564cc Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 17 May 2021 18:44:59 +0300 Subject: Make rustfmt happy --- src/frontend/mod.rs | 300 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 199 insertions(+), 101 deletions(-) (limited to 'src/frontend') diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index c92619b..eefc257 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -1,17 +1,17 @@ -use serde::{Serialize, Deserialize}; -use tide::{Request, Response, Result, StatusCode, Next}; -use log::{info,error}; -use crate::ApplicationState; use crate::database::Storage; +use crate::ApplicationState; +use log::{error, info}; +use serde::{Deserialize, Serialize}; +use tide::{Next, Request, Response, Result, StatusCode}; static POSTS_PER_PAGE: usize = 20; mod templates { - use log::error; - use http_types::StatusCode; - use ellipse::Ellipse; - use chrono; use super::IndiewebEndpoints; + use chrono; + use ellipse::Ellipse; + use http_types::StatusCode; + use log::error; /// Return a pretty location specifier from a geo: URI. fn decode_geo_uri(uri: &str) -> String { @@ -21,12 +21,12 @@ mod templates { let lat = parts.next().unwrap(); let lon = parts.next().unwrap(); // TODO - format them as proper latitude and longitude - return format!("{}, {}", lat, lon) + return format!("{}, {}", lat, lon); } else { - return uri.to_string() + return uri.to_string(); } } else { - return uri.to_string() + return uri.to_string(); } } @@ -124,7 +124,7 @@ mod templates { div.form_group { label[for="hcard_name"] { "Your name" } input#hcard_name[name="hcard_name", placeholder="Your name"]; - small { + small { "No need to write the name as in your passport, this is not a legal document " "- just write how you want to be called on the network. This name will be also " "shown whenever you leave a comment on someone else's post using your website." @@ -165,7 +165,7 @@ mod templates { small { "A little bit of introduction. Just one paragraph, and note, you can't use HTML here (yet)." } // TODO: HTML e-note instead of p-note } - + // TODO: u-photo upload - needs media endpoint cooperation div.switch_card_buttons { @@ -438,7 +438,7 @@ mod templates { @if card["properties"]["photo"][0].is_string() { img."u-photo"[src=card["properties"]["photo"][0].as_str().unwrap()]; } - h1 { + h1 { a."u-url"."u-uid"."p-name"[href=card["properties"]["uid"][0].as_str().unwrap()] { @card["properties"]["name"][0].as_str().unwrap() } @@ -508,7 +508,7 @@ mod templates { } } @if feed["children"].as_array().map(|a| a.len()).unwrap_or(0) == super::POSTS_PER_PAGE { - a[rel="prev", href=feed["properties"]["uid"][0].as_str().unwrap().to_string() + a[rel="prev", href=feed["properties"]["uid"][0].as_str().unwrap().to_string() + "?after=" + feed["children"][super::POSTS_PER_PAGE - 1]["properties"]["uid"][0].as_str().unwrap()] { "Older posts" } @@ -521,8 +521,8 @@ mod templates { #dynamicstuff { p { "This section will provide interesting statistics or tidbits about my life in this exact moment (with maybe a small delay)." } p { "It will probably require JavaScript to self-update, but I promise to keep this widget lightweight and open-source!" } - p { small { - "JavaScript isn't a menace, stop fearing it or I will switch to WebAssembly " + p { small { + "JavaScript isn't a menace, stop fearing it or I will switch to WebAssembly " "and knock your nico-nico-kneecaps so fast with its speed you won't even notice that... " small { "omae ha mou shindeiru" } // NANI?!!! @@ -557,7 +557,7 @@ mod templates { StatusCode::ImATeapot => { p { "Wait, do you seriously expect my website to brew you coffee? It's not a coffee machine!" } - p { + p { small { "I could brew you some coffee tho if we meet one day... " small { i { "i-it's nothing personal, I just like brewing coffee, b-baka!!!~ >. { p { "It seems like you have found an error. Not to worry, it has already been logged." } } } P { "For now, may I suggest to visit " a[href="/"] {"the main page"} " of this website?" } - + } } } -use templates::{Template,ErrorPage,MainPage,OnboardingPage}; +use templates::{ErrorPage, MainPage, OnboardingPage, Template}; #[derive(Clone, Serialize, Deserialize)] pub struct IndiewebEndpoints { authorization_endpoint: String, token_endpoint: String, webmention: Option, - microsub: Option + microsub: Option, } #[derive(Deserialize)] struct QueryParams { - after: Option + after: Option, } #[derive(Debug)] struct FrontendError { msg: String, source: Option>, - code: StatusCode + code: StatusCode, } impl FrontendError { pub fn with_code(code: StatusCode, msg: &str) -> Self { - Self { msg: msg.to_string(), source: None, code } + Self { + msg: msg.to_string(), + source: None, + code, + } + } + pub fn msg(&self) -> &str { + &self.msg + } + pub fn code(&self) -> StatusCode { + self.code } - pub fn msg(&self) -> &str { &self.msg } - pub fn code(&self) -> StatusCode { self.code } } impl From for FrontendError { fn from(err: crate::database::StorageError) -> Self { Self { msg: "Database error".to_string(), source: Some(Box::new(err)), - code: StatusCode::InternalServerError + code: StatusCode::InternalServerError, } } } impl std::error::Error for FrontendError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - self.source.as_ref().map(|e| e.as_ref() as &(dyn std::error::Error + 'static)) + self.source + .as_ref() + .map(|e| e.as_ref() as &(dyn std::error::Error + 'static)) } } impl std::fmt::Display for FrontendError { @@ -618,30 +628,47 @@ impl std::fmt::Display for FrontendError { } } -async fn get_post_from_database(db: &S, url: &str, after: Option, user: &Option) -> std::result::Result { - match db.read_feed_with_limit(url, &after, POSTS_PER_PAGE, user).await { +async fn get_post_from_database( + db: &S, + url: &str, + after: Option, + user: &Option, +) -> std::result::Result { + match db + .read_feed_with_limit(url, &after, POSTS_PER_PAGE, user) + .await + { Ok(result) => match result { Some(post) => Ok(post), - None => Err(FrontendError::with_code(StatusCode::NotFound, "Post not found in the database")) + None => Err(FrontendError::with_code( + StatusCode::NotFound, + "Post not found in the database", + )), }, Err(err) => match err.kind() { crate::database::ErrorKind::PermissionDenied => { // TODO: Authentication if user.is_some() { - Err(FrontendError::with_code(StatusCode::Forbidden, "User authenticated AND forbidden to access this resource")) + Err(FrontendError::with_code( + StatusCode::Forbidden, + "User authenticated AND forbidden to access this resource", + )) } else { - Err(FrontendError::with_code(StatusCode::Unauthorized, "User needs to authenticate themselves")) + Err(FrontendError::with_code( + StatusCode::Unauthorized, + "User needs to authenticate themselves", + )) } } - _ => Err(err.into()) - } + _ => Err(err.into()), + }, } } #[derive(Deserialize)] struct OnboardingFeed { slug: String, - name: String + name: String, } #[derive(Deserialize)] @@ -649,7 +676,7 @@ struct OnboardingData { user: serde_json::Value, first_post: serde_json::Value, blog_name: String, - feeds: Vec + feeds: Vec, } pub async fn onboarding_receiver(mut req: Request>) -> Result { @@ -663,16 +690,24 @@ pub async fn onboarding_receiver(mut req: Request(mut req: Request(mut req: Request(mut req: Request(_: Request>) -> Result { - Err(FrontendError::with_code(StatusCode::ImATeapot, "Someone asked this website to brew them some coffee..."))?; - return Ok(Response::builder(500).build()) // unreachable + Err(FrontendError::with_code( + StatusCode::ImATeapot, + "Someone asked this website to brew them some coffee...", + ))?; + return Ok(Response::builder(500).build()); // unreachable } pub async fn mainpage(req: Request>) -> Result { @@ -726,7 +767,7 @@ pub async fn mainpage(req: Request>) -> Result { info!("Request at {}", url); let hcard_url = url.as_str(); let feed_url = url.join("feeds/main").unwrap().to_string(); - + let card = get_post_from_database(backend, hcard_url, None, &user).await; let feed = get_post_from_database(backend, &feed_url, query.after, &user).await; @@ -737,34 +778,51 @@ pub async fn mainpage(req: Request>) -> Result { let feed_err = feed.unwrap_err(); if card_err.code == 404 { // Yes, we definitely need some onboarding here. - Ok(Response::builder(200).content_type("text/html; charset=utf-8").body(Template { - title: "Kittybox - Onboarding", - blog_name: "Kitty Box!", - endpoints: IndiewebEndpoints { - authorization_endpoint, token_endpoint, - webmention: None, microsub: None - }, - content: OnboardingPage {}.to_string() - }.to_string()).build()) + Ok(Response::builder(200) + .content_type("text/html; charset=utf-8") + .body( + Template { + title: "Kittybox - Onboarding", + blog_name: "Kitty Box!", + endpoints: IndiewebEndpoints { + authorization_endpoint, + token_endpoint, + webmention: None, + microsub: None, + }, + content: OnboardingPage {}.to_string(), + } + .to_string(), + ) + .build()) } else { Err(feed_err)? } } else { Ok(Response::builder(200) .content_type("text/html; charset=utf-8") - .body(Template { - title: &format!("{} - Main page", url.host().unwrap().to_string()), - blog_name: &backend.get_setting("site_name", &url.host().unwrap().to_string()).await.unwrap_or_else(|_| "Kitty Box!".to_string()), - endpoints: IndiewebEndpoints { - authorization_endpoint, token_endpoint, - webmention: None, microsub: None - }, - content: MainPage { - feed: &feed?, - card: &card? - }.to_string() - }.to_string() - ).build()) + .body( + Template { + title: &format!("{} - Main page", url.host().unwrap().to_string()), + blog_name: &backend + .get_setting("site_name", &url.host().unwrap().to_string()) + .await + .unwrap_or_else(|_| "Kitty Box!".to_string()), + endpoints: IndiewebEndpoints { + authorization_endpoint, + token_endpoint, + webmention: None, + microsub: None, + }, + content: MainPage { + feed: &feed?, + card: &card?, + } + .to_string(), + } + .to_string(), + ) + .build()) } } @@ -777,41 +835,73 @@ pub async fn render_post(req: Request>) -> Resul #[cfg(any(not(debug_assertions), test))] let url = req.url(); #[cfg(all(debug_assertions, not(test)))] - let url = url::Url::parse("http://localhost:8080/").unwrap().join(req.url().path()).unwrap(); - - let post = get_post_from_database(&req.state().storage, url.as_str(), query.after, &user).await?; - - let template: String = match post["type"][0].as_str().expect("Empty type array or invalid type") { + let url = url::Url::parse("http://localhost:8080/") + .unwrap() + .join(req.url().path()) + .unwrap(); + + let post = + get_post_from_database(&req.state().storage, url.as_str(), query.after, &user).await?; + + let template: String = match post["type"][0] + .as_str() + .expect("Empty type array or invalid type") + { "h-entry" => templates::Entry { post: &post }.to_string(), "h-card" => templates::VCard { card: &post }.to_string(), "h-feed" => templates::Feed { feed: &post }.to_string(), - _ => Err(FrontendError::with_code(StatusCode::InternalServerError, "Couldn't render an unknown type"))? + _ => Err(FrontendError::with_code( + StatusCode::InternalServerError, + "Couldn't render an unknown type", + ))?, }; Ok(Response::builder(200) .content_type("text/html; charset=utf-8") - .body(Template { - title: post["properties"]["name"][0].as_str().unwrap_or(&format!("Note at {}", url.host().unwrap().to_string())), - blog_name: &req.state().storage.get_setting("site_name", &url.host().unwrap().to_string()).await.unwrap_or_else(|_| "Kitty Box!".to_string()), - endpoints: IndiewebEndpoints { - authorization_endpoint, token_endpoint, - webmention: None, microsub: None - }, - content: template - }.to_string() - ).build()) + .body( + Template { + title: post["properties"]["name"][0] + .as_str() + .unwrap_or(&format!("Note at {}", url.host().unwrap().to_string())), + blog_name: &req + .state() + .storage + .get_setting("site_name", &url.host().unwrap().to_string()) + .await + .unwrap_or_else(|_| "Kitty Box!".to_string()), + endpoints: IndiewebEndpoints { + authorization_endpoint, + token_endpoint, + webmention: None, + microsub: None, + }, + content: template, + } + .to_string(), + ) + .build()) } pub struct ErrorHandlerMiddleware {} #[async_trait::async_trait] -impl tide::Middleware> for ErrorHandlerMiddleware where - S: crate::database::Storage +impl tide::Middleware> for ErrorHandlerMiddleware +where + S: crate::database::Storage, { - async fn handle(&self, request: Request>, next: Next<'_, ApplicationState>) -> Result { + async fn handle( + &self, + request: Request>, + next: Next<'_, ApplicationState>, + ) -> Result { let authorization_endpoint = request.state().authorization_endpoint.to_string(); let token_endpoint = request.state().token_endpoint.to_string(); - let site_name = &request.state().storage.get_setting("site_name", &request.url().host().unwrap().to_string()).await.unwrap_or_else(|_| "Kitty Box!".to_string()); + let site_name = &request + .state() + .storage + .get_setting("site_name", &request.url().host().unwrap().to_string()) + .await + .unwrap_or_else(|_| "Kitty Box!".to_string()); let mut res = next.run(request).await; let mut code: Option = None; if let Some(err) = res.downcast_error::() { @@ -826,15 +916,20 @@ impl tide::Middleware> for ErrorHandlerMiddleware where if let Some(code) = code { res.set_status(code); res.set_content_type("text/html; charset=utf-8"); - res.set_body(Template { - title: "Error", - blog_name: site_name, - endpoints: IndiewebEndpoints { - authorization_endpoint, token_endpoint, - webmention: None, microsub: None - }, - content: ErrorPage { code }.to_string() - }.to_string()); + res.set_body( + Template { + title: "Error", + blog_name: site_name, + endpoints: IndiewebEndpoints { + authorization_endpoint, + token_endpoint, + webmention: None, + microsub: None, + }, + content: ErrorPage { code }.to_string(), + } + .to_string(), + ); } Ok(res) } @@ -858,7 +953,10 @@ pub async fn handle_static(req: Request>) -> Res .content_type("text/css; charset=utf-8") .body(ONBOARDING_CSS) .build()), - Ok(_) => Err(FrontendError::with_code(StatusCode::NotFound, "Static file not found")), - Err(_) => panic!("Invalid usage of the frontend::handle_static() function") + Ok(_) => Err(FrontendError::with_code( + StatusCode::NotFound, + "Static file not found", + )), + Err(_) => panic!("Invalid usage of the frontend::handle_static() function"), }?) } -- cgit 1.4.1