split logic of contact

This commit is contained in:
Mylloon 2024-11-09 16:58:52 +01:00
parent 744857d685
commit b9bc57c1e0
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 54 additions and 49 deletions

View file

@ -2,15 +2,15 @@ use actix_web::{get, routes, web, HttpRequest, Responder};
use cached::proc_macro::once; use cached::proc_macro::once;
use glob::glob; use glob::glob;
use ramhorns::Content; use ramhorns::Content;
use std::fs::read_to_string;
use crate::{ use crate::{
config::Config, config::Config,
template::{InfosPage, NavBar},
utils::{ utils::{
markdown::{File, TypeFileMetadata}, markdown::{File, TypeFileMetadata},
misc::{make_kw, read_file, Html}, misc::{make_kw, read_file, Html},
routes::contact::{find_links, remove_paragraphs},
}, },
template::{InfosPage, NavBar},
}; };
const CONTACT_DIR: &str = "contacts"; const CONTACT_DIR: &str = "contacts";
@ -32,47 +32,6 @@ async fn page(config: web::Data<Config>) -> impl Responder {
Html(build_page(config.get_ref().to_owned())) Html(build_page(config.get_ref().to_owned()))
} }
/// Contact node
#[derive(Clone, Debug)]
struct ContactLink {
service: String,
scope: Option<String>,
link: String,
}
#[once(time = 60)]
fn find_links(directory: String) -> Vec<ContactLink> {
// TOML filename
let toml_file = "links.toml";
// Read the TOML file and parse it
let toml_str = read_to_string(format!("{directory}/{toml_file}")).unwrap_or_default();
let mut redirections = vec![];
match toml::de::from_str::<toml::Value>(&toml_str) {
Ok(data) => {
if let Some(section) = data.as_table() {
section.iter().for_each(|(key, value)| {
// Scopes are delimited with `/`
let (service, scope) = match key.split_once('/') {
Some((service, scope)) => (service.to_owned(), Some(scope.to_owned())),
None => (key.to_owned(), None),
};
redirections.push(ContactLink {
service,
scope,
link: value.as_str().unwrap().to_owned(),
});
});
}
}
Err(_) => return vec![],
}
redirections
}
#[routes] #[routes]
#[get("/{service}")] #[get("/{service}")]
#[get("/{service}/{scope}")] #[get("/{service}/{scope}")]
@ -92,7 +51,7 @@ async fn service_redirection(config: web::Data<Config>, req: HttpRequest) -> imp
_ => false, _ => false,
}) })
// Returns the link // Returns the link
.map(|data| data.link.clone()) .map(|data| data.url.clone())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
// This shouldn't be more than one link here // This shouldn't be more than one link here
@ -119,11 +78,6 @@ struct NetworksTemplate {
others: Vec<File>, 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)] #[once(time = 60)]
fn build_page(config: Config) -> String { fn build_page(config: Config) -> String {
let contacts_dir = format!("{}/{}", config.locations.data_dir, CONTACT_DIR); let contacts_dir = format!("{}/{}", config.locations.data_dir, CONTACT_DIR);

View file

@ -0,0 +1,50 @@
use cached::proc_macro::once;
use std::fs::read_to_string;
use crate::utils::markdown::File;
/// Contact node
#[derive(Clone, Debug)]
pub struct Link {
pub service: String,
pub scope: Option<String>,
pub url: String,
}
#[once(time = 60)]
pub fn find_links(directory: String) -> Vec<Link> {
// TOML filename
let toml_file = "links.toml";
// Read the TOML file and parse it
let toml_str = read_to_string(format!("{directory}/{toml_file}")).unwrap_or_default();
let mut redirections = vec![];
match toml::de::from_str::<toml::Value>(&toml_str) {
Ok(data) => {
if let Some(section) = data.as_table() {
section.iter().for_each(|(key, value)| {
// Scopes are delimited with `/`
let (service, scope) = match key.split_once('/') {
Some((service, scope)) => (service.to_owned(), Some(scope.to_owned())),
None => (key.to_owned(), None),
};
redirections.push(Link {
service,
scope,
url: value.as_str().unwrap().to_owned(),
});
});
}
}
Err(_) => return vec![],
}
redirections
}
pub fn remove_paragraphs(list: &mut [File]) {
list.iter_mut()
.for_each(|file| file.content = file.content.replace("<p>", "").replace("</p>", ""));
}

View file

@ -1,2 +1,3 @@
pub mod blog; pub mod blog;
pub mod contact;
pub mod cours; pub mod cours;