use axum::response::Response; use lazy_static::lazy_static; use rand::{distributions::Alphanumeric, Rng}; use tokio::sync::Mutex; use crate::{types::{user::User, http::ResponseCode, post::Post, session::Session}, console::{self, sanatize}}; lazy_static! { static ref SECRET: Mutex = Mutex::new(String::new()); } pub fn new_secret() -> String { rand::thread_rng() .sample_iter(&Alphanumeric) .take(32) .map(char::from) .collect() } pub async fn get_secret() -> String { let mut secret = SECRET.lock().await; if secret.is_empty() { *secret = new_secret(); } return secret.clone(); } pub async fn regen_secret() -> String { let mut secret = SECRET.lock().await; *secret = new_secret(); return secret.clone(); } pub fn generate_users() -> Response { let users = match User::reterieve_all() { Ok(users) => users, Err(err) => return err, }; let mut html = r#" User ID First Name Last Name Email Password Gender Date Day Month Year "#.to_string(); for user in users { html.push_str( &format!("{}{}{}{}{}{}{}{}{}{}", user.user_id, sanatize(user.firstname), sanatize(user.lastname), sanatize(user.email), sanatize(user.password), sanatize(user.gender), user.date, user.day, user.month, user.year ) ); } ResponseCode::Success.text(&html) } pub fn generate_posts() -> Response { let posts = match Post::reterieve_all() { Ok(posts) => posts, Err(err) => return err, }; let mut html = r#" Post ID User ID Content Likes Comments Date "#.to_string(); for post in posts { let Ok(likes) = serde_json::to_string(&post.likes) else { continue }; let Ok(comments) = serde_json::to_string(&post.comments) else { continue }; html.push_str( &format!("{}{}{}{}{}{}", post.post_id, post.user_id, sanatize(post.content), console::beautify(likes), console::beautify(comments), post.date ) ); } ResponseCode::Success.text(&html) } pub fn generate_sessions() -> Response { let sessions = match Session::reterieve_all() { Ok(sessions) => sessions, Err(err) => return err, }; let mut html = r#" User ID Token "#.to_string(); for session in sessions { html.push_str( &format!("{}{}", session.user_id, session.token ) ); } ResponseCode::Success.text(&html) }