use actix_web::{get, routes, web, HttpRequest, HttpResponse, Responder}; use cached::proc_macro::once; use glob::glob; use ramhorns::Content; use crate::{ config::Config, misc::{ markdown::{read_file, File, TypeFileMetadata}, utils::get_url, }, template::{Infos, NavBar}, }; #[get("/contact")] async fn page(req: HttpRequest, config: web::Data) -> impl Responder { HttpResponse::Ok().body(build_page( config.get_ref().to_owned(), get_url(req.connection_info()), )) } #[routes] #[get("/contact/{service}")] #[get("/contact/{service}/{scope}")] async fn service_redirection(req: HttpRequest) -> impl Responder { let info = req.match_info(); let find_redirection = match info.query("service") { /* Socials links */ "twitter" => Some("https://twitter.com/Mylloon".to_owned()), "mastodon" => Some("https://piaille.fr/@mylloon".to_owned()), "bluesky" => Some("https://bsky.app/profile/mylloon.fr".to_owned()), "discord" => match info.get("scope") { Some("user") => Some("https://discord.com/users/158260864623968257/".to_owned()), Some("guild") => Some("https://discord.gg/Z5ePxH4".to_owned()), _ => None, }, "reddit" => Some("https://www.reddit.com/user/mylloon".to_owned()), "instagram" => Some("https://www.instagram.com/mylloon/".to_owned()), "kitsu" => Some("https://kitsu.io/users/Mylloon/library?status=completed".to_owned()), "steam" => Some("https://steamcommunity.com/id/mylloon/".to_owned()), "youtube" => Some("https://www.youtube.com/c/Mylloon".to_owned()), "twitch" => Some("https://www.twitch.tv/mylloon".to_owned()), /* Forges */ "github" => Some("https://github.com/Mylloon".to_owned()), "gitlab" => Some("https://gitlab.com/Mylloon".to_owned()), "codeberg" => Some("https://codeberg.org/Mylloon".to_owned()), "forgejo" => Some("https://git.mylloon.fr/Anri".to_owned()), /* Others */ "keyoxide" => { Some("https://keyoxide.org/27024A99057E58B8087A5022A82D63DFF8D1317F".to_owned()) } _ => None, }; if let Some(redirection) = find_redirection { // Redirect to the desired service actix_web::web::Redirect::to(redirection) } else { // By default, returns to the contact page actix_web::web::Redirect::to("/contact") } } #[derive(Content, Debug)] struct NetworksTemplate { navbar: NavBar, socials_exists: bool, socials: Vec, forges_exists: bool, forges: Vec, others_exists: bool, others: Vec, } fn remove_paragraphs(list: &mut [File]) { list.iter_mut() .for_each(|file| file.content = file.content.replace("

", "").replace("

", "")); } #[once(time = 60)] fn build_page(config: Config, url: String) -> String { let contacts_dir = "data/contacts"; let ext = ".md"; let socials_dir = "socials"; let mut socials = glob(&format!("{contacts_dir}/{socials_dir}/*{ext}")) .unwrap() .map(|e| read_file(&e.unwrap().to_string_lossy(), TypeFileMetadata::Contact).unwrap()) .collect::>(); let forges_dir = "forges"; let mut forges = glob(&format!("{contacts_dir}/{forges_dir}/*{ext}")) .unwrap() .map(|e| read_file(&e.unwrap().to_string_lossy(), TypeFileMetadata::Contact).unwrap()) .collect::>(); let others_dir = "others"; let mut others = glob(&format!("{contacts_dir}/{others_dir}/*{ext}")) .unwrap() .map(|e| read_file(&e.unwrap().to_string_lossy(), TypeFileMetadata::Contact).unwrap()) .collect::>(); // Remove paragraphs in custom statements [&mut socials, &mut forges, &mut others] .iter_mut() .for_each(|it| remove_paragraphs(it)); config.tmpl.render( "contact/index.html", NetworksTemplate { navbar: NavBar { contact: true, ..NavBar::default() }, socials_exists: !socials.is_empty(), socials, forges_exists: !forges.is_empty(), forges, others_exists: !others.is_empty(), others, }, Infos { page_title: Some("Contacts".into()), page_desc: Some(format!("Réseaux d'{}", config.fc.name.unwrap_or_default())), page_kw: None, url, }, ) }