#[macro_export] macro_rules! write_template { ($into:ident->$path:literal($as:expr)) => { std::fs::write($into.join($path), $as).unwrap(); }; ($into:ident->$path:literal($as:expr) --config=$config:ident) => { std::fs::write( $into.join($path), $crate::assets::replace_in_html($as, &$config, false, None).await, ) .unwrap(); }; ($into:ident->$path:literal($as:expr) --config=$config:ident --lisp $plugins:ident) => { std::fs::write( $into.join($path), $crate::assets::replace_in_html($as, &$config, true, Some(&mut $plugins)).await, ) .unwrap(); }; ($into:ident->$path:literal($as:expr) -d $dir_path:literal) => { let dir = $into.join($dir_path); if !std::fs::exists(&dir).unwrap() { std::fs::create_dir(dir).unwrap(); } std::fs::write($into.join($path), $as).unwrap(); }; ($into:ident->$path:literal($as:expr) -d $dir_path:literal --config=$config:ident) => { let dir = $into.join($dir_path); if !std::fs::exists(&dir).unwrap() { std::fs::create_dir(dir).unwrap(); } std::fs::write( $into.join($path), $crate::assets::replace_in_html($as, &$config, false, None).await, ) .unwrap(); }; ($into:ident->$path:literal($as:expr) -d $dir_path:literal --config=$config:ident --lisp $plugins:ident) => { let dir = $into.join($dir_path); if !std::fs::exists(&dir).unwrap() { std::fs::create_dir(dir).unwrap(); } std::fs::write( $into.join($path), $crate::assets::replace_in_html($as, &$config, true, Some(&mut $plugins)).await, ) .unwrap(); }; } #[macro_export] macro_rules! write_if_track { ($into:ident->$path:literal($as:expr) --config=$config:ident) => { if !$config.no_track.contains(&$path.to_string()) { write_template!($into->$path($as)); } }; } #[macro_export] macro_rules! create_dir_if_not_exists { ($dir_path:expr) => { if !std::fs::exists(&$dir_path).unwrap() { std::fs::create_dir($dir_path).unwrap(); } }; } #[macro_export] macro_rules! get_user_from_token { ($jar:ident, $db:expr) => {{ // pages; regular token only if let Some(token) = $jar.get("__Secure-atto-token") { match $db .get_user_by_token(&tetratto_shared::hash::hash( token.to_string().replace("__Secure-atto-token=", ""), )) .await { Ok(ua) => { if ua.permissions.check_banned() { let mut banned_user = tetratto_core::model::auth::User::banned(); banned_user.ban_reason = ua.ban_reason; Some(banned_user) } else { Some(ua) } } Err(_) => None, } } else { None } }}; ($jar:ident, $db:expr, $grant_scope:expr) => {{ if let Some(token) = $jar.get("Atto-Grant") { // grant token match $db .get_user_by_grant_token(&token.to_string().replace("Atto-Grant=", ""), true) .await { Ok((grant, ua)) => { if grant.scopes.contains(&$grant_scope) { if ua.permissions.check_banned() { None } else { Some(ua) } } else { None } } Err(_) => None, } } else if let Some(token) = $jar.get("__Secure-atto-token") { // regular token match $db .get_user_by_token(&tetratto_shared::hash::hash( token.to_string().replace("__Secure-atto-token=", ""), )) .await { Ok(ua) => { if ua.permissions.check_banned() { Some(tetratto_core::model::auth::User::banned()) } else { Some(ua) } } Err(_) => None, } } else { None } }}; (--browser_session=$browser_session:expr, $db:expr) => {{ // browser session id match $db.get_user_by_browser_session(&$browser_session).await { Ok(ua) => { if ua.permissions.check_banned() { None } else { Some(ua) } } Err(_) => None, } }}; } #[macro_export] macro_rules! get_lang { ($jar:ident, $db:expr) => {{ if let Some(lang) = $jar.get("__Secure-atto-lang") { match $db .1 .get(&lang.to_string().replace("__Secure-atto-lang=", "")) { Some(lang) => lang, None => $db.1.get("com.tetratto.langs:en-US").unwrap(), } } else { $db.1.get("com.tetratto.langs:en-US").unwrap() } }}; } #[macro_export] macro_rules! user_banned { ($user:expr, $other_user:ident, $data:ident, $jar:ident) => { let lang = get_lang!($jar, $data.0); let mut context = initial_context(&$data.0.0.0, lang, &$user).await; context.insert("profile", &$other_user); return Err(Html( $data.1.render("profile/banned.html", &context).unwrap(), )); }; } #[macro_export] macro_rules! check_user_blocked_or_private { ($user:expr, $other_user:ident, $data:ident, $jar:ident) => { // check is_deactivated if ($user.is_none() && $other_user.is_deactivated) | ($user.is_some() && !$user .as_ref() .unwrap() .permissions .check(tetratto_core::model::permissions::FinePermission::MANAGE_USERS) && $other_user.is_deactivated) { return Err(Html( render_error( Error::GeneralNotFound("user".to_string()), &$jar, &$data, &$user, ) .await, )); } // check require_account if $user.is_none() && $other_user.settings.require_account { return Err(Html( render_error( Error::MiscError( "This profile requires you are logged in to view it.".to_string(), ), &$jar, &$data, &$user, ) .await, )); } // check if other user is banned if $other_user.permissions.check_banned() { if let Some(ref ua) = $user { if !ua .permissions .check(tetratto_core::model::permissions::FinePermission::MANAGE_USERS) { $crate::user_banned!($user, $other_user, $data, $jar); } } else { $crate::user_banned!($user, $other_user, $data, $jar); } } // check if we're blocked if let Some(ref ua) = $user { if ($data .0 .get_userblock_by_initiator_receiver($other_user.id, ua.id) .await .is_ok() | $data .0 .get_user_stack_blocked_users($other_user.id) .await .contains(&ua.id)) && !ua .permissions .check(tetratto_core::model::permissions::FinePermission::MANAGE_USERS) { let lang = get_lang!($jar, $data.0); let mut context = initial_context(&$data.0.0.0, lang, &$user).await; context.insert("profile", &$other_user); context.insert( "is_blocking", &$data .0 .get_userblock_by_initiator_receiver(ua.id, $other_user.id) .await .is_ok(), ); return Err(Html( $data.1.render("profile/blocked.html", &context).unwrap(), )); } } // check for private profile if $other_user.settings.private_profile { if let Some(ref ua) = $user { if (ua.id != $other_user.id) && !ua .permissions .check(tetratto_core::model::permissions::FinePermission::MANAGE_USERS) && $data .0 .get_userfollow_by_initiator_receiver($other_user.id, ua.id) .await .is_err() { let lang = get_lang!($jar, $data.0); let mut context = initial_context(&$data.0.0.0, lang, &$user).await; context.insert("profile", &$other_user); context.insert( "follow_requested", &$data .0 .get_request_by_id_linked_asset(ua.id, $other_user.id) .await .is_ok(), ); context.insert( "is_blocking", &$data .0 .get_userblock_by_initiator_receiver(ua.id, $other_user.id) .await .is_ok(), ); context.insert( "is_following", &$data .0 .get_userfollow_by_initiator_receiver(ua.id, $other_user.id) .await .is_ok(), ); return Err(Html( $data.1.render("profile/private.html", &context).unwrap(), )); } } else { let lang = get_lang!($jar, $data.0); let mut context = initial_context(&$data.0.0.0, lang, &$user).await; context.insert("profile", &$other_user); context.insert("follow_requested", &false); context.insert("is_following", &false); return Err(Html( $data.1.render("profile/private.html", &context).unwrap(), )); } } }; ($user:expr, $other_user:ident, $data:ident, @api) => { // check if we're blocked if let Some(ref ua) = $user { if ($data .get_userblock_by_initiator_receiver($other_user.id, ua.id) .await .is_ok() | $data .get_user_stack_blocked_users($other_user.id) .await .contains(&ua.id)) && !ua .permissions .check(tetratto_core::model::permissions::FinePermission::MANAGE_USERS) { return Json( tetratto_core::model::Error::MiscError("You're blocked".to_string()).into(), ); } } // check for private profile if $other_user.settings.private_profile { if let Some(ref ua) = $user { if (ua.id != $other_user.id) && !ua .permissions .check(tetratto_core::model::permissions::FinePermission::MANAGE_USERS) && $data .get_userfollow_by_initiator_receiver($other_user.id, ua.id) .await .is_err() { return Json( tetratto_core::model::Error::MiscError("Profile is private".to_string()) .into(), ); } } else { return Json( tetratto_core::model::Error::MiscError("Profile is private".to_string()).into(), ); } } }; } #[macro_export] macro_rules! ignore_users_gen { ($user:ident, $data:ident) => { if let Some(ref ua) = $user { [ $data .0 .get_userblocks_receivers( ua.id, &ua.associated, ua.settings.hide_associated_blocked_users, ) .await, $data.0.get_userblocks_initiator_by_receivers(ua.id).await, $data.0.get_user_stack_blocked_users(ua.id).await, ] .concat() } else { Vec::new() } }; ($user:ident!, $data:ident) => {{ [ $data .0 .get_userblocks_receivers( $user.id, &$user.associated, $user.settings.hide_associated_blocked_users, ) .await, $data .0 .get_userblocks_initiator_by_receivers($user.id) .await, $data.0.get_user_stack_blocked_users($user.id).await, ] .concat() }}; ($user:ident!, #$data:ident) => { [ $data .get_userblocks_receivers( $user.id, &$user.associated, $user.settings.hide_associated_blocked_users, ) .await, $data.get_userblocks_initiator_by_receivers($user.id).await, ] .concat() }; } #[macro_export] macro_rules! get_app_from_key { ($db:ident, $headers:ident) => { if let Some(token) = $headers.get("Atto-Secret-Key") { match $db.get_app_by_api_key(token.to_str().unwrap()).await { Ok(x) => Some(x), Err(_) => None, } } else { None } }; }