mylloon.fr/src/routes/contact.rs

152 lines
4.8 KiB
Rust
Raw Normal View History

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},
};
pub fn pages(cfg: &mut web::ServiceConfig) {
// Here define the services used
let routes = |route_path| {
web::scope(route_path)
.service(page)
.service(service_redirection)
};
// Here define the routes aliases
cfg.service(routes("/contact")).service(routes("/c"));
}
#[get("")]
async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
}
#[routes]
#[get("/{service}")]
#[get("/{service}/{scope}")]
async fn service_redirection(req: HttpRequest) -> impl Responder {
let info = req.match_info();
let find_redirection = match info.query("service") {
// TODO: XML file with link, so it's not hardcoded here
/* Socials links */
"twitter" => Some("https://twitter.com/Mylloon".to_owned()),
"mastodon" => Some("https://piaille.fr/@mylloon".to_owned()),
2023-10-19 01:52:22 +02:00
"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<File>,
forges_exists: bool,
forges: Vec<File>,
others_exists: bool,
others: Vec<File>,
}
fn remove_paragraphs(list: &mut [File]) {
list.iter_mut()
.for_each(|file| file.content = file.content.replace("<p>", "").replace("</p>", ""));
}
#[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::<Vec<File>>();
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::<Vec<File>>();
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::<Vec<File>>();
// 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,
},
)
}