diff --git a/rustfmt.toml b/rustfmt.toml index 218e203..ade1f81 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1 +1,8 @@ hard_tabs = true +newline_style = "unix" + +unstable_features = true +format_code_in_doc_comments = true +format_macro_bodies = true +format_macro_matchers = true +format_strings = true diff --git a/src/server.rs b/src/server.rs index 2c150da..0a096f6 100644 --- a/src/server.rs +++ b/src/server.rs @@ -160,12 +160,7 @@ async fn serve_index<'a>( .render( "index.html", &IndexTemplate { - common: CommonTemplate { - lang: "fr", - root_url: &config.root_url, - title: &config.title, - errors, - }, + common: CommonTemplate::new(&config, errors), ads: &to_json( dbs.ad_by_group .scan_prefix(ROOT_GROUP_ID) @@ -209,70 +204,73 @@ async fn serve_group<'a>( ) -> tide::Result { if let Ok(group_name) = req.param("group") { if let Some(group_id) = dbs.group_by_name.get(group_name).unwrap() { - let selected_ad = req.param("ad").ok(); + if let Some(group) = dbs.group.get(&group_id).unwrap() { + if let Ok(group) = bincode::deserialize::(&group) { + let selected_ad = req.param("ad").ok(); - Ok(tide::Response::builder(200) - .content_type(tide::http::mime::HTML) - .body( - templates - .hb - .render( - "group.html", - &GroupTemplate { - common: CommonTemplate { - lang: "fr", - root_url: &config.root_url, - title: &config.title, - errors, - }, - ads: &to_json( - dbs.ad_by_group - .scan_prefix(group_id.clone()) - .filter_map(|x| { - let (k, ad) = x.ok()?; - let ad_id = hex::encode( - AdId::try_from(&k.as_ref()[16..32]).ok()?, - ); - Some(AdWithId { - ad: bincode::deserialize::(&ad).ok()?, - selected: selected_ad.map_or(false, |i| i == ad_id), - id: ad_id, - }) - }) - .collect::>(), - ), - groups: &to_json( - dbs.group_by_group - .scan_prefix(group_id) - .keys() - .filter_map(|k| { - GroupId::try_from(&k.ok()?.as_ref()[16..32]).ok() - }) - .filter_map(|subgroup_id| { - bincode::deserialize(&dbs.group.get(subgroup_id).ok()??) - .ok() - }) - .collect::>(), - ), - new_ad_form_refill, - }, + return Ok(tide::Response::builder(200) + .content_type(tide::http::mime::HTML) + .body( + templates + .hb + .render( + "group.html", + &GroupTemplate { + common: CommonTemplate::new(&config, errors), + ads: &to_json( + dbs.ad_by_group + .scan_prefix(group_id.clone()) + .filter_map(|x| { + let (k, ad) = x.ok()?; + let ad_id = hex::encode( + AdId::try_from(&k.as_ref()[16..32]).ok()?, + ); + Some(AdWithId { + ad: bincode::deserialize::(&ad).ok()?, + selected: selected_ad + .map_or(false, |i| i == ad_id), + id: ad_id, + }) + }) + .collect::>(), + ), + group: &to_json(group), + groups: &to_json( + dbs.group_by_group + .scan_prefix(group_id) + .keys() + .filter_map(|k| { + GroupId::try_from(&k.ok()?.as_ref()[16..32]) + .ok() + }) + .filter_map(|subgroup_id| { + bincode::deserialize( + &dbs.group.get(subgroup_id).ok()??, + ) + .ok() + }) + .collect::>(), + ), + new_ad_form_refill, + }, + ) + .unwrap_or_else(|e| e.to_string()), ) - .unwrap_or_else(|e| e.to_string()), - ) - .build()) - } else { - serve_index( - req, - config, - templates, - dbs, - &[ErrorTemplate { - text: "Le groupe demandé n'existe pas.", - }], - None, - ) - .await + .build()); + } + } } + serve_index( + req, + config, + templates, + dbs, + &[ErrorTemplate { + text: "Le groupe demandé n'existe pas.", + }], + None, + ) + .await } else { serve_index(req, config, templates, dbs, &[], None).await } @@ -300,12 +298,7 @@ async fn serve_admin_group<'a>( .render( "admin_group.html", &AdminGroupTemplate { - common: CommonTemplate { - lang: "fr", - root_url: &config.root_url, - title: &config.title, - errors, - }, + common: CommonTemplate::new(&config, errors), ads: &to_json( dbs.ad_by_group .scan_prefix(group_id.clone()) @@ -380,12 +373,7 @@ async fn serve_admin<'a>( .render( "admin.html", &AdminTemplate { - common: CommonTemplate { - lang: "fr", - root_url: &config.root_url, - title: &config.title, - errors, - }, + common: CommonTemplate::new(&config, errors), ads: &to_json( dbs.ad_by_group .scan_prefix(ROOT_GROUP_ID) @@ -432,12 +420,7 @@ async fn serve_admin_login<'a>( .render( "admin_login.html", &AdminLoginTemplate { - common: CommonTemplate { - lang: "fr", - root_url: &config.root_url, - title: &config.title, - errors, - }, + common: CommonTemplate::new(&config, errors), }, ) .unwrap_or_else(|e| e.to_string()), @@ -477,24 +460,27 @@ async fn handle_new_ad( } else { Some(match format_pubkey(&query.pubkey) { Ok(pubkey) => pubkey, - Err(e) => return serve_index( - req, - config, - templates, - dbs, - &[ErrorTemplate { - text: match e { - PubkeyDecodeError::BadChecksum => { - "La somme de contrôle de la clé publique est incorrecte." - } - PubkeyDecodeError::BadFormat => { - "Le format de la clé publique est incorrect." - } - }, - }], - Some(query), - ) - .await, + Err(e) => { + return serve_index( + req, + config, + templates, + dbs, + &[ErrorTemplate { + text: match e { + PubkeyDecodeError::BadChecksum => { + "La somme de contrôle de la clé publique est \ + incorrecte." + } + PubkeyDecodeError::BadFormat => { + "Le format de la clé publique est incorrect." + } + }, + }], + Some(query), + ) + .await + } }) }, author: query.author, diff --git a/src/static_files.rs b/src/static_files.rs index c12ded0..43e4479 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -11,9 +11,16 @@ static STATIC_FILES: &[(&str, &[u8])] = &[ "LinBiolinum_R.otf", include_bytes!("../static/LinBiolinum_R.otf"), ), - ("background.jpg", include_bytes!("../static/background.jpg")), + ("bg0.jpg", include_bytes!("../static/bg0.jpg")), + ("bg1.jpg", include_bytes!("../static/bg1.jpg")), + ("bg2.jpg", include_bytes!("../static/bg2.jpg")), ("script1.js", include_bytes!("../static/script1.js")), ]; +pub static BACKGROUND_ABOUT: &[&str] = &[ + "Claudia Peters, FreeImages.com", + r#"Muhammad Haris, CC BY-SA 4.0"#, + r#"Wilfredor, CC BY-SA 4.0"#, +]; pub fn init_static_files(dir: &Path) { let dir = dir.join(STATIC_DIR); @@ -27,3 +34,9 @@ pub fn init_static_files(dir: &Path) { } } } + +pub fn get_background() -> usize { + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |t| t.as_secs() as usize / 86400 % 3) +} diff --git a/src/templates.rs b/src/templates.rs index 7446429..d11f07d 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,9 +1,9 @@ -use super::queries::*; +use crate::{config::Config, queries::*}; use handlebars::Handlebars; use serde::Serialize; use serde_json::value::Value as Json; -use std::path::Path; +use std::{path::Path, sync::Arc}; const TEMPLATES_DIR: &str = "templates"; static TEMPLATE_FILES: &[(&str, &str)] = &[ @@ -57,6 +57,22 @@ pub struct CommonTemplate<'a> { pub root_url: &'a str, pub title: &'a str, pub errors: &'a [ErrorTemplate<'a>], + pub bg_id: usize, + pub bg_about: &'a str, +} + +impl<'a> CommonTemplate<'a> { + pub fn new(config: &'a Arc, errors: &'a [ErrorTemplate<'a>]) -> Self { + let bg_id = crate::static_files::get_background(); + Self { + lang: "fr", + root_url: &config.as_ref().root_url, + title: &config.as_ref().title, + errors, + bg_id, + bg_about: crate::static_files::BACKGROUND_ABOUT[bg_id], + } + } } #[derive(Serialize)] @@ -82,12 +98,12 @@ pub struct AdminTemplate<'a> { pub groups: &'a Json, } -// TODO add subgroups, parent groups, title #[derive(Serialize)] pub struct GroupTemplate<'a> { #[serde(flatten)] pub common: CommonTemplate<'a>, pub ads: &'a Json, + pub group: &'a Json, pub groups: &'a Json, pub new_ad_form_refill: Option, } diff --git a/static/background.jpg b/static/bg0.jpg similarity index 100% rename from static/background.jpg rename to static/bg0.jpg diff --git a/static/bg1.jpg b/static/bg1.jpg new file mode 100644 index 0000000..8477d77 Binary files /dev/null and b/static/bg1.jpg differ diff --git a/static/bg2.jpg b/static/bg2.jpg new file mode 100644 index 0000000..44c6903 Binary files /dev/null and b/static/bg2.jpg differ diff --git a/static/style1.css b/static/style1.css index 4799069..4612fbd 100644 --- a/static/style1.css +++ b/static/style1.css @@ -17,10 +17,6 @@ html, body { font-size: 110%; } -body { - background-image: linear-gradient(rgba(255,255,255,0.2), rgba(255,255,255,0.2)), url("/static/background.jpg"); -} - h1, h2, h3, h4 { font-family: "Karma"; } diff --git a/templates/admin.html b/templates/admin.html index 4026d21..0ec0112 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -7,7 +7,7 @@ - +
@@ -15,6 +15,7 @@

Administration – {{title}}

+

{{group.title}}

{{#if errors}}
@@ -88,7 +89,7 @@

Code source sous licence GNU AGPL v3. 🦀 Écrit en Rust. Images de Attilax.
CopyLeft 2020-2022 Pascal Engélibert
- Image de fond : Claudia Peters, FreeImages.com

+ Image de fond : {{{bg_about}}}

AccueilVerrouiller

diff --git a/templates/admin_group.html b/templates/admin_group.html index 8a39600..9ed3fe8 100644 --- a/templates/admin_group.html +++ b/templates/admin_group.html @@ -7,14 +7,17 @@ - +
-

{{group.title}} – Administration – {{title}}

+

Administration – {{title}}

+

{{group.title}}

+ + Groupe parent
{{#if errors}}
@@ -88,7 +91,7 @@

Code source sous licence GNU AGPL v3. 🦀 Écrit en Rust. Images de Attilax.
CopyLeft 2020-2022 Pascal Engélibert
- Image de fond : Claudia Peters, FreeImages.com

+ Image de fond : {{{bg_about}}}

AccueilVerrouiller

diff --git a/templates/admin_login.html b/templates/admin_login.html index 6d4253d..2ed284b 100644 --- a/templates/admin_login.html +++ b/templates/admin_login.html @@ -6,7 +6,7 @@ - +
@@ -43,7 +43,7 @@

Code source sous licence GNU AGPL v3. 🦀 Écrit en Rust. Images de Attilax.
CopyLeft 2020-2022 Pascal Engélibert
- Image de fond : Claudia Peters, FreeImages.com

+ Image de fond : {{{bg_about}}}

Accueil

diff --git a/templates/group.html b/templates/group.html index 3af6c8d..789364d 100644 --- a/templates/group.html +++ b/templates/group.html @@ -2,12 +2,12 @@ - {{group-title}} | {{title}} + {{group.title}} | {{title}} - +
@@ -15,6 +15,9 @@

{{title}}

+

{{group.title}}

+ + Groupe parent
{{#if errors}}
@@ -100,7 +103,7 @@

Code source sous licence GNU AGPL v3. 🦀 Écrit en Rust. Images de Attilax.
CopyLeft 2020-2022 Pascal Engélibert
- Image de fond : Claudia Peters, FreeImages.com

+ Image de fond : {{{bg_about}}}

Administration

diff --git a/templates/index.html b/templates/index.html index 8c8db2f..e489694 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,7 +7,7 @@ - +
@@ -15,6 +15,7 @@

{{title}}

+

{{group.title}}

{{#if errors}}
@@ -102,7 +103,7 @@

Code source sous licence GNU AGPL v3. 🦀 Écrit en Rust. Images de Attilax.
CopyLeft 2020-2022 Pascal Engélibert
- Image de fond : Claudia Peters, FreeImages.com

+ Image de fond : {{{bg_about}}}

Administration