From 0321eff62ba883a9dd76a129ba93e1ec6b6cbeb8 Mon Sep 17 00:00:00 2001 From: Zomatree Date: Sun, 12 Mar 2023 23:30:01 +0000 Subject: [PATCH] chore: move to once_cell from lazy_static --- Cargo.lock | 6 +- crates/delta/Cargo.toml | 3 +- crates/delta/src/main.rs | 2 - .../delta/src/routes/channels/message_send.rs | 6 +- crates/quark/Cargo.toml | 3 +- .../impl/generic/channels/channel_invite.rs | 14 +-- crates/quark/src/impl/generic/media/emoji.rs | 12 +- .../impl/mongo/admin/migrations/scripts.rs | 11 +- crates/quark/src/impl/mongo/users/user.rs | 19 ++-- crates/quark/src/lib.rs | 2 - .../quark/src/permissions/defn/permission.rs | 33 +++--- .../quark/src/permissions/impl/permission.rs | 2 +- crates/quark/src/presence/entry.rs | 14 +-- crates/quark/src/tasks/ack.rs | 5 +- crates/quark/src/tasks/last_message_id.rs | 5 +- crates/quark/src/tasks/process_embeds.rs | 6 +- crates/quark/src/tasks/web_push.rs | 6 +- crates/quark/src/types/january.rs | 9 +- crates/quark/src/util/variables/delta.rs | 103 +++++++----------- crates/quark/src/web/idempotency.rs | 5 +- crates/quark/src/web/ratelimiter.rs | 5 +- 21 files changed, 110 insertions(+), 161 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73c3cec9..7d1c376e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2106,9 +2106,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.13.0" +version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" [[package]] name = "opaque-debug" @@ -2819,7 +2819,6 @@ dependencies = [ "env_logger", "futures", "impl_ops", - "lazy_static", "lettre", "linkify 0.6.0", "log", @@ -2866,7 +2865,6 @@ dependencies = [ "impl_ops", "indexmap", "iso8601-timestamp", - "lazy_static", "linkify 0.8.1", "log", "lru", diff --git a/crates/delta/Cargo.toml b/crates/delta/Cargo.toml index 70c4ad70..7a4ba256 100644 --- a/crates/delta/Cargo.toml +++ b/crates/delta/Cargo.toml @@ -15,9 +15,8 @@ log = "0.4.11" dotenv = "0.15.0" dashmap = "5.2.0" linkify = "0.6.0" -once_cell = "1.4.1" +once_cell = "1.17.1" env_logger = "0.7.1" -lazy_static = "1.4.0" # Lang. Utilities regex = "1" diff --git a/crates/delta/src/main.rs b/crates/delta/src/main.rs index 4e9bba0a..4dc3ba38 100644 --- a/crates/delta/src/main.rs +++ b/crates/delta/src/main.rs @@ -4,8 +4,6 @@ extern crate rocket; extern crate revolt_rocket_okapi; #[macro_use] extern crate serde_json; -#[macro_use] -extern crate lazy_static; pub mod routes; pub mod util; diff --git a/crates/delta/src/routes/channels/message_send.rs b/crates/delta/src/routes/channels/message_send.rs index 3b45b1b8..355feff0 100644 --- a/crates/delta/src/routes/channels/message_send.rs +++ b/crates/delta/src/routes/channels/message_send.rs @@ -15,6 +15,7 @@ use rocket::serde::json::Json; use serde::{Deserialize, Serialize}; use ulid::Ulid; use validator::Validate; +use once_cell::sync::Lazy; #[derive(Validate, Serialize, Deserialize, JsonSchema)] pub struct DataMessageSend { @@ -44,10 +45,7 @@ pub struct DataMessageSend { interactions: Option, } -lazy_static! { - // ignoring I L O and U is intentional - static ref RE_MENTION: Regex = Regex::new(r"<@([0-9A-HJKMNP-TV-Z]{26})>").unwrap(); -} +static RE_MENTION: Lazy = Lazy::new(|| Regex::new(r"<@([0-9A-HJKMNP-TV-Z]{26})>").unwrap()); /// # Send Message /// diff --git a/crates/quark/Cargo.toml b/crates/quark/Cargo.toml index ca734fdb..9b882359 100644 --- a/crates/quark/Cargo.toml +++ b/crates/quark/Cargo.toml @@ -67,8 +67,7 @@ impl_ops = "0.1.1" num_enum = "0.5.6" reqwest = "0.11.10" bitfield = "0.13.2" -once_cell = "1.13.0" -lazy_static = "1.4.0" +once_cell = "1.17.1" async-lock = "2.6.0" lru = { version = "0.7.6", optional = true } diff --git a/crates/quark/src/impl/generic/channels/channel_invite.rs b/crates/quark/src/impl/generic/channels/channel_invite.rs index ac9f645b..4193c34d 100644 --- a/crates/quark/src/impl/generic/channels/channel_invite.rs +++ b/crates/quark/src/impl/generic/channels/channel_invite.rs @@ -5,13 +5,11 @@ use crate::{ Database, Error, Result, }; -lazy_static! { - static ref ALPHABET: [char; 54] = [ - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', - 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', - 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' - ]; -} +static ALPHABET: [char; 54] = [ + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', + 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' +]; impl Invite { /// Get the invite code for this invite @@ -30,7 +28,7 @@ impl Invite { /// Create a new invite from given information pub async fn create(db: &Database, creator: &User, target: &Channel) -> Result { - let code = nanoid!(8, &*ALPHABET); + let code = nanoid!(8, &ALPHABET); let invite = match &target { Channel::Group { id, .. } => Ok(Invite::Group { code, diff --git a/crates/quark/src/impl/generic/media/emoji.rs b/crates/quark/src/impl/generic/media/emoji.rs index 6c58b38a..77b3677f 100644 --- a/crates/quark/src/impl/generic/media/emoji.rs +++ b/crates/quark/src/impl/generic/media/emoji.rs @@ -1,4 +1,5 @@ use std::{collections::HashSet, str::FromStr}; +use once_cell::sync::Lazy; use ulid::Ulid; @@ -8,13 +9,10 @@ use crate::{ Database, Result, }; -lazy_static! { - /// Permissible emojis - static ref PERMISSIBLE_EMOJIS: HashSet = include_str!(crate::asset!("emojis.txt")) - .split('\n') - .map(|x| x.into()) - .collect(); -} +static PERMISSIBLE_EMOJIS: Lazy> = Lazy::new(|| include_str!(crate::asset!("emojis.txt")) + .split('\n') + .map(|x| x.into()) + .collect()); impl Emoji { /// Get parent id diff --git a/crates/quark/src/impl/mongo/admin/migrations/scripts.rs b/crates/quark/src/impl/mongo/admin/migrations/scripts.rs index 2c83d369..168029e6 100644 --- a/crates/quark/src/impl/mongo/admin/migrations/scripts.rs +++ b/crates/quark/src/impl/mongo/admin/migrations/scripts.rs @@ -1,4 +1,4 @@ -use std::time::Duration; +use std::{time::Duration, ops::BitXor}; use bson::{Bson, DateTime}; use futures::StreamExt; @@ -503,13 +503,8 @@ pub async fn run_migrations(db: &MongoDb, revision: i32) -> i32 { update.insert( "default_permissions", - (*DEFAULT_PERMISSION_SERVER - // Remove Send Message permission if it wasn't originally granted - ^ (if has_send { - 0 - } else { - Permission::SendMessage as u64 - })) as i64, + // Remove Send Message permission if it wasn't originally granted + DEFAULT_PERMISSION_SERVER.bitxor(if has_send { 0 } else { Permission::SendMessage as u64}) as i64, ); if let Some(Bson::Document(mut roles)) = document.remove("roles") { diff --git a/crates/quark/src/impl/mongo/users/user.rs b/crates/quark/src/impl/mongo/users/user.rs index a5459f9d..e7dd2383 100644 --- a/crates/quark/src/impl/mongo/users/user.rs +++ b/crates/quark/src/impl/mongo/users/user.rs @@ -1,6 +1,7 @@ use bson::Document; use futures::StreamExt; use mongodb::options::{Collation, CollationStrength, FindOneOptions, FindOptions}; +use once_cell::sync::Lazy; use crate::models::user::{FieldsUser, PartialUser, RelationshipStatus, User}; use crate::r#impl::mongo::IntoDocumentPath; @@ -8,16 +9,14 @@ use crate::{AbstractUser, Error, Result}; use super::super::MongoDb; -lazy_static! { - static ref FIND_USERNAME_OPTIONS: FindOneOptions = FindOneOptions::builder() - .collation( - Collation::builder() - .locale("en") - .strength(CollationStrength::Secondary) - .build() - ) - .build(); -} +static FIND_USERNAME_OPTIONS: Lazy = Lazy::new(|| FindOneOptions::builder() + .collation( + Collation::builder() + .locale("en") + .strength(CollationStrength::Secondary) + .build() + ) + .build()); static COL: &str = "users"; diff --git a/crates/quark/src/lib.rs b/crates/quark/src/lib.rs index 2d063639..13b1ce82 100644 --- a/crates/quark/src/lib.rs +++ b/crates/quark/src/lib.rs @@ -11,8 +11,6 @@ extern crate impl_ops; #[macro_use] extern crate optional_struct; #[macro_use] -extern crate lazy_static; -#[macro_use] extern crate bitfield; #[macro_use] extern crate bson; diff --git a/crates/quark/src/permissions/defn/permission.rs b/crates/quark/src/permissions/defn/permission.rs index 2ebb0257..a73c6216 100644 --- a/crates/quark/src/permissions/defn/permission.rs +++ b/crates/quark/src/permissions/defn/permission.rs @@ -1,6 +1,7 @@ use num_enum::TryFromPrimitive; use serde::{Deserialize, Serialize}; -use std::ops; +use once_cell::sync::Lazy; +use std::ops::{self, Add}; /// Permission value on Revolt /// @@ -97,24 +98,18 @@ pub enum Permission { impl_op_ex!(+ |a: &Permission, b: &Permission| -> u64 { *a as u64 | *b as u64 }); impl_op_ex_commutative!(+ |a: &u64, b: &Permission| -> u64 { *a | *b as u64 }); -lazy_static! { - pub static ref ALLOW_IN_TIMEOUT: u64 = Permission::ViewChannel + Permission::ReadMessageHistory; - pub static ref DEFAULT_PERMISSION_VIEW_ONLY: u64 = - Permission::ViewChannel + Permission::ReadMessageHistory; - pub static ref DEFAULT_PERMISSION: u64 = *DEFAULT_PERMISSION_VIEW_ONLY - + Permission::SendMessage - + Permission::InviteOthers - + Permission::SendEmbeds - + Permission::UploadFiles - + Permission::Connect - + Permission::Speak; - pub static ref DEFAULT_PERMISSION_SAVED_MESSAGES: u64 = Permission::GrantAllSafe as u64; - pub static ref DEFAULT_PERMISSION_DIRECT_MESSAGE: u64 = *DEFAULT_PERMISSION - + Permission::ManageChannel - + Permission::React; - pub static ref DEFAULT_PERMISSION_SERVER: u64 = - *DEFAULT_PERMISSION + Permission::React + Permission::ChangeNickname + Permission::ChangeAvatar; -} +pub static ALLOW_IN_TIMEOUT: Lazy = Lazy::new(|| Permission::ViewChannel + Permission::ReadMessageHistory); +pub static DEFAULT_PERMISSION_VIEW_ONLY: Lazy = Lazy::new(|| Permission::ViewChannel + Permission::ReadMessageHistory); +pub static DEFAULT_PERMISSION: Lazy = Lazy::new(|| DEFAULT_PERMISSION_VIEW_ONLY.add( + Permission::SendMessage + + Permission::InviteOthers + + Permission::SendEmbeds + + Permission::UploadFiles + + Permission::Connect + + Permission::Speak)); +pub static DEFAULT_PERMISSION_SAVED_MESSAGES: u64 = Permission::GrantAllSafe as u64; +pub static DEFAULT_PERMISSION_DIRECT_MESSAGE: Lazy = Lazy::new(|| DEFAULT_PERMISSION.add(Permission::ManageChannel + Permission::React)); +pub static DEFAULT_PERMISSION_SERVER: Lazy = Lazy::new(|| DEFAULT_PERMISSION.add(Permission::React + Permission::ChangeNickname + Permission::ChangeAvatar)); bitfield! { #[derive(Default)] diff --git a/crates/quark/src/permissions/impl/permission.rs b/crates/quark/src/permissions/impl/permission.rs index 04ed720f..7ff36616 100644 --- a/crates/quark/src/permissions/impl/permission.rs +++ b/crates/quark/src/permissions/impl/permission.rs @@ -113,7 +113,7 @@ async fn calculate_channel_permission( let value: PermissionValue = match channel { Channel::SavedMessages { user, .. } => { if user == &data.perspective.id { - (*DEFAULT_PERMISSION_SAVED_MESSAGES).into() + DEFAULT_PERMISSION_SAVED_MESSAGES.into() } else { 0_u64.into() } diff --git a/crates/quark/src/presence/entry.rs b/crates/quark/src/presence/entry.rs index 8373ae25..4d43cd2e 100644 --- a/crates/quark/src/presence/entry.rs +++ b/crates/quark/src/presence/entry.rs @@ -1,14 +1,14 @@ use std::env; use serde::{Deserialize, Serialize}; +use once_cell::sync::Lazy; -lazy_static! { - pub static ref REGION_ID: u16 = env::var("REGION_ID") - .unwrap_or_else(|_| "0".to_string()) - .parse() - .unwrap(); - pub static ref REGION_KEY: String = format!("region{}", &*REGION_ID); -} +pub static REGION_ID: Lazy = Lazy::new(|| env::var("REGION_ID") + .unwrap_or_else(|_| "0".to_string()) + .parse() + .unwrap()); + +pub static REGION_KEY: Lazy = Lazy::new(|| format!("region{}", &*REGION_ID)); /// Compact presence information for a user #[derive(Serialize, Deserialize, Debug)] diff --git a/crates/quark/src/tasks/ack.rs b/crates/quark/src/tasks/ack.rs index eee72be1..9c0d16f9 100644 --- a/crates/quark/src/tasks/ack.rs +++ b/crates/quark/src/tasks/ack.rs @@ -4,6 +4,7 @@ use crate::Database; use deadqueue::limited::Queue; use mongodb::bson::doc; use std::{collections::HashMap, time::Duration}; +use once_cell::sync::Lazy; use super::DelayedTask; @@ -38,9 +39,7 @@ struct Task { event: AckEvent, } -lazy_static! { - static ref Q: Queue = Queue::new(10_000); -} +static Q: Lazy> = Lazy::new(|| Queue::new(10_000)); /// Queue a new task for a worker pub async fn queue(channel: String, user: String, event: AckEvent) { diff --git a/crates/quark/src/tasks/last_message_id.rs b/crates/quark/src/tasks/last_message_id.rs index a6264578..facd5ffb 100644 --- a/crates/quark/src/tasks/last_message_id.rs +++ b/crates/quark/src/tasks/last_message_id.rs @@ -4,6 +4,7 @@ use crate::{models::channel::PartialChannel, Database}; use deadqueue::limited::Queue; use mongodb::bson::doc; use std::{collections::HashMap, time::Duration}; +use once_cell::sync::Lazy; use super::DelayedTask; @@ -26,9 +27,7 @@ struct Task { is_dm: bool, } -lazy_static! { - static ref Q: Queue = Queue::new(10_000); -} +static Q: Lazy> = Lazy::new(|| Queue::new(10_000)); /// Queue a new task for a worker pub async fn queue(channel: String, id: String, is_dm: bool) { diff --git a/crates/quark/src/tasks/process_embeds.rs b/crates/quark/src/tasks/process_embeds.rs index d36d0496..aef165fb 100644 --- a/crates/quark/src/tasks/process_embeds.rs +++ b/crates/quark/src/tasks/process_embeds.rs @@ -9,6 +9,7 @@ use async_lock::Semaphore; use async_std::task::spawn; use deadqueue::limited::Queue; use std::sync::Arc; +use once_cell::sync::Lazy; /// Task information #[derive(Debug)] @@ -21,9 +22,8 @@ struct EmbedTask { content: String, } -lazy_static! { - static ref Q: Queue = Queue::new(10_000); -} +static Q: Lazy> = Lazy::new(|| Queue::new(10_000)); + /// Queue a new task for a worker pub async fn queue(channel: String, id: String, content: String) { diff --git a/crates/quark/src/tasks/web_push.rs b/crates/quark/src/tasks/web_push.rs index c9428f6f..ac2f8dc9 100644 --- a/crates/quark/src/tasks/web_push.rs +++ b/crates/quark/src/tasks/web_push.rs @@ -3,6 +3,7 @@ use crate::util::variables::delta::VAPID_PRIVATE_KEY; use authifier::Database; use deadqueue::limited::Queue; +use once_cell::sync::Lazy; use web_push::{ ContentEncoding, SubscriptionInfo, SubscriptionKeys, VapidSignatureBuilder, WebPushClient, WebPushMessageBuilder, @@ -17,9 +18,8 @@ struct PushTask { payload: String, } -lazy_static! { - static ref Q: Queue = Queue::new(10_000); -} +static Q: Lazy> = Lazy::new(|| Queue::new(10_000)); + /// Queue a new task for a worker pub async fn queue(recipients: Vec, payload: String) { diff --git a/crates/quark/src/types/january.rs b/crates/quark/src/types/january.rs index 2a8859f5..e7ed95a0 100644 --- a/crates/quark/src/types/january.rs +++ b/crates/quark/src/types/january.rs @@ -5,6 +5,7 @@ use linkify::{LinkFinder, LinkKind}; use regex::Regex; use serde::{Deserialize, Serialize}; use std::{collections::HashSet, sync::Arc}; +use once_cell::sync::Lazy; use crate::{models::attachment::File, Error, Result}; @@ -175,6 +176,9 @@ pub enum Embed { None, } +static RE_CODE: Lazy = Lazy::new(|| Regex::new("```(?:.|\n)+?```|`(?:.|\n)+?`").unwrap()); +static RE_IGNORED: Lazy = Lazy::new(|| Regex::new("()").unwrap()); + impl Embed { /// Generate embeds from given content pub async fn generate( @@ -183,10 +187,7 @@ impl Embed { max_embeds: usize, semaphore: Arc, ) -> Result> { - lazy_static! { - static ref RE_CODE: Regex = Regex::new("```(?:.|\n)+?```|`(?:.|\n)+?`").unwrap(); - static ref RE_IGNORED: Regex = Regex::new("()").unwrap(); - } + // Ignore code blocks. let content = RE_CODE.replace_all(&content, ""); diff --git a/crates/quark/src/util/variables/delta.rs b/crates/quark/src/util/variables/delta.rs index bb789987..3981b77d 100644 --- a/crates/quark/src/util/variables/delta.rs +++ b/crates/quark/src/util/variables/delta.rs @@ -1,73 +1,50 @@ use std::env; +use once_cell::sync::Lazy; -lazy_static! { - // Application Settings - pub static ref PUBLIC_URL: String = - env::var("REVOLT_PUBLIC_URL").expect("Missing REVOLT_PUBLIC_URL environment variable."); - pub static ref APP_URL: String = - env::var("REVOLT_APP_URL").expect("Missing REVOLT_APP_URL environment variable."); - pub static ref EXTERNAL_WS_URL: String = - env::var("REVOLT_EXTERNAL_WS_URL").expect("Missing REVOLT_EXTERNAL_WS_URL environment variable."); +// Application Settings +pub static PUBLIC_URL: Lazy = Lazy::new(|| env::var("REVOLT_PUBLIC_URL").expect("Missing REVOLT_PUBLIC_URL environment variable.")); +pub static APP_URL: Lazy = Lazy::new(|| env::var("REVOLT_APP_URL").expect("Missing REVOLT_APP_URL environment variable.")); +pub static EXTERNAL_WS_URL: Lazy = Lazy::new(|| env::var("REVOLT_EXTERNAL_WS_URL").expect("Missing REVOLT_EXTERNAL_WS_URL environment variable.")); - pub static ref AUTUMN_URL: String = - env::var("AUTUMN_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string()); - pub static ref JANUARY_URL: String = - env::var("JANUARY_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string()); - pub static ref JANUARY_CONCURRENT_CONNECTIONS: usize = - env::var("JANUARY_CONCURRENT_CONNECTIONS").map_or(50, |v| v.parse().unwrap()); - pub static ref VOSO_URL: String = - env::var("VOSO_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string()); - pub static ref VOSO_WS_HOST: String = - env::var("VOSO_WS_HOST").unwrap_or_else(|_| "wss://example.com".to_string()); - pub static ref VOSO_MANAGE_TOKEN: String = - env::var("VOSO_MANAGE_TOKEN").unwrap_or_else(|_| "0".to_string()); +pub static AUTUMN_URL: Lazy = Lazy::new(|| env::var("AUTUMN_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string())); +pub static JANUARY_URL: Lazy = Lazy::new(|| env::var("JANUARY_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string())); +pub static JANUARY_CONCURRENT_CONNECTIONS: Lazy = Lazy::new(|| env::var("JANUARY_CONCURRENT_CONNECTIONS").map_or(50, |v| v.parse().unwrap())); +pub static VOSO_URL: Lazy = Lazy::new(|| env::var("VOSO_PUBLIC_URL").unwrap_or_else(|_| "https://example.com".to_string())); +pub static VOSO_WS_HOST: Lazy = Lazy::new(|| env::var("VOSO_WS_HOST").unwrap_or_else(|_| "wss://example.com".to_string())); +pub static VOSO_MANAGE_TOKEN: Lazy = Lazy::new(|| env::var("VOSO_MANAGE_TOKEN").unwrap_or_else(|_| "0".to_string())); - pub static ref HCAPTCHA_KEY: String = - env::var("REVOLT_HCAPTCHA_KEY").unwrap_or_else(|_| "0x0000000000000000000000000000000000000000".to_string()); - pub static ref HCAPTCHA_SITEKEY: String = - env::var("REVOLT_HCAPTCHA_SITEKEY").unwrap_or_else(|_| "10000000-ffff-ffff-ffff-000000000001".to_string()); - pub static ref VAPID_PRIVATE_KEY: String = - env::var("REVOLT_VAPID_PRIVATE_KEY").expect("Missing REVOLT_VAPID_PRIVATE_KEY environment variable."); - pub static ref VAPID_PUBLIC_KEY: String = - env::var("REVOLT_VAPID_PUBLIC_KEY").expect("Missing REVOLT_VAPID_PUBLIC_KEY environment variable."); - pub static ref AUTHIFIER_SHIELD_KEY: Option = - env::var("REVOLT_AUTHIFIER_SHIELD_KEY").ok(); +pub static HCAPTCHA_KEY: Lazy = Lazy::new(|| env::var("REVOLT_HCAPTCHA_KEY").unwrap_or_else(|_| "0x0000000000000000000000000000000000000000".to_string())); +pub static HCAPTCHA_SITEKEY: Lazy = Lazy::new(|| env::var("REVOLT_HCAPTCHA_SITEKEY").unwrap_or_else(|_| "10000000-ffff-ffff-ffff-000000000001".to_string())); +pub static VAPID_PRIVATE_KEY: Lazy = Lazy::new(|| env::var("REVOLT_VAPID_PRIVATE_KEY").expect("Missing REVOLT_VAPID_PRIVATE_KEY environment variable.")); +pub static VAPID_PUBLIC_KEY: Lazy = Lazy::new(|| env::var("REVOLT_VAPID_PUBLIC_KEY").expect("Missing REVOLT_VAPID_PUBLIC_KEY environment variable.")); +pub static AUTHIFIER_SHIELD_KEY: Lazy> = Lazy::new(|| env::var("REVOLT_AUTHIFIER_SHIELD_KEY").ok()); - // Application Flags - pub static ref INVITE_ONLY: bool = env::var("REVOLT_INVITE_ONLY").map_or(false, |v| v == "1"); - pub static ref USE_EMAIL: bool = env::var("REVOLT_USE_EMAIL_VERIFICATION").map_or( - env::var("REVOLT_SMTP_HOST").is_ok() - && env::var("REVOLT_SMTP_USERNAME").is_ok() - && env::var("REVOLT_SMTP_PASSWORD").is_ok() - && env::var("REVOLT_SMTP_FROM").is_ok(), - |v| v == *"1" - ); - pub static ref USE_HCAPTCHA: bool = env::var("REVOLT_HCAPTCHA_KEY").is_ok(); - pub static ref USE_AUTUMN: bool = env::var("AUTUMN_PUBLIC_URL").is_ok(); - pub static ref USE_JANUARY: bool = env::var("JANUARY_PUBLIC_URL").is_ok(); - pub static ref USE_VOSO: bool = env::var("VOSO_PUBLIC_URL").is_ok() && env::var("VOSO_MANAGE_TOKEN").is_ok(); +// Application Flags +pub static INVITE_ONLY: Lazy = Lazy::new(|| env::var("REVOLT_INVITE_ONLY").map_or(false, |v| v == "1")); +pub static USE_EMAIL: Lazy = Lazy::new(|| env::var("REVOLT_USE_EMAIL_VERIFICATION").map_or( + env::var("REVOLT_SMTP_HOST").is_ok() + && env::var("REVOLT_SMTP_USERNAME").is_ok() + && env::var("REVOLT_SMTP_PASSWORD").is_ok() + && env::var("REVOLT_SMTP_FROM").is_ok(), + |v| v == *"1" +)); +pub static USE_HCAPTCHA: Lazy = Lazy::new(|| env::var("REVOLT_HCAPTCHA_KEY").is_ok()); +pub static USE_AUTUMN: Lazy = Lazy::new(|| env::var("AUTUMN_PUBLIC_URL").is_ok()); +pub static USE_JANUARY: Lazy = Lazy::new(|| env::var("JANUARY_PUBLIC_URL").is_ok()); +pub static USE_VOSO: Lazy = Lazy::new(|| env::var("VOSO_PUBLIC_URL").is_ok() && env::var("VOSO_MANAGE_TOKEN").is_ok()); - // SMTP Settings - pub static ref SMTP_HOST: String = - env::var("REVOLT_SMTP_HOST").unwrap_or_else(|_| "".to_string()); - pub static ref SMTP_USERNAME: String = - env::var("REVOLT_SMTP_USERNAME").unwrap_or_else(|_| "".to_string()); - pub static ref SMTP_PASSWORD: String = - env::var("REVOLT_SMTP_PASSWORD").unwrap_or_else(|_| "".to_string()); - pub static ref SMTP_FROM: String = env::var("REVOLT_SMTP_FROM").unwrap_or_else(|_| "".to_string()); +// SMTP Settings +pub static SMTP_HOST: Lazy = Lazy::new(|| env::var("REVOLT_SMTP_HOST").unwrap_or_else(|_| "".to_string())); +pub static SMTP_USERNAME: Lazy = Lazy::new(|| env::var("REVOLT_SMTP_USERNAME").unwrap_or_else(|_| "".to_string())); +pub static SMTP_PASSWORD: Lazy = Lazy::new(|| env::var("REVOLT_SMTP_PASSWORD").unwrap_or_else(|_| "".to_string())); +pub static SMTP_FROM: Lazy = Lazy::new(|| env::var("REVOLT_SMTP_FROM").unwrap_or_else(|_| "".to_string())); - // Application Logic Settings - pub static ref MAX_GROUP_SIZE: usize = - env::var("REVOLT_MAX_GROUP_SIZE").unwrap_or_else(|_| "50".to_string()).parse().unwrap(); - pub static ref MAX_BOT_COUNT: usize = - env::var("REVOLT_MAX_BOT_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap(); - pub static ref MAX_EMBED_COUNT: usize = - env::var("REVOLT_MAX_EMBED_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap(); - pub static ref MAX_SERVER_COUNT: usize = - env::var("REVOLT_MAX_SERVER_COUNT").unwrap_or_else(|_| "100".to_string()).parse().unwrap(); - pub static ref EARLY_ADOPTER_BADGE: i64 = - env::var("REVOLT_EARLY_ADOPTER_BADGE").unwrap_or_else(|_| "0".to_string()).parse().unwrap(); -} +// Application Logic Settings +pub static MAX_GROUP_SIZE: Lazy = Lazy::new(|| env::var("REVOLT_MAX_GROUP_SIZE").unwrap_or_else(|_| "50".to_string()).parse().unwrap()); +pub static MAX_BOT_COUNT: Lazy = Lazy::new(|| env::var("REVOLT_MAX_BOT_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap()); +pub static MAX_EMBED_COUNT: Lazy = Lazy::new(|| env::var("REVOLT_MAX_EMBED_COUNT").unwrap_or_else(|_| "5".to_string()).parse().unwrap()); +pub static MAX_SERVER_COUNT: Lazy = Lazy::new(|| env::var("REVOLT_MAX_SERVER_COUNT").unwrap_or_else(|_| "100".to_string()).parse().unwrap()); +pub static EARLY_ADOPTER_BADGE: Lazy = Lazy::new(|| env::var("REVOLT_EARLY_ADOPTER_BADGE").unwrap_or_else(|_| "0".to_string()).parse().unwrap()); pub fn preflight_checks() { format!("url = {}", *APP_URL); diff --git a/crates/quark/src/web/idempotency.rs b/crates/quark/src/web/idempotency.rs index 769341e7..49004a3a 100644 --- a/crates/quark/src/web/idempotency.rs +++ b/crates/quark/src/web/idempotency.rs @@ -9,6 +9,7 @@ use rocket::request::{FromRequest, Outcome}; use schemars::schema::{InstanceType, SchemaObject, SingleOrVec}; use serde::{Deserialize, Serialize}; use validator::Validate; +use once_cell::sync::Lazy; #[derive(Validate, Serialize, Deserialize)] pub struct IdempotencyKey { @@ -16,9 +17,7 @@ pub struct IdempotencyKey { key: String, } -lazy_static! { - static ref TOKEN_CACHE: Mutex> = Mutex::new(lru::LruCache::new(100)); -} +static TOKEN_CACHE: Lazy>> = Lazy::new(|| Mutex::new(lru::LruCache::new(100))); impl IdempotencyKey { // Backwards compatibility. diff --git a/crates/quark/src/web/ratelimiter.rs b/crates/quark/src/web/ratelimiter.rs index 631fa368..53415a69 100644 --- a/crates/quark/src/web/ratelimiter.rs +++ b/crates/quark/src/web/ratelimiter.rs @@ -22,6 +22,7 @@ use revolt_rocket_okapi::request::{OpenApiFromRequest, RequestHeaderInput}; use serde::Serialize; use dashmap::DashMap; +use once_cell::sync::Lazy; /// Ratelimit Bucket #[derive(Clone, Copy)] @@ -30,9 +31,7 @@ struct Entry { reset: u128, } -lazy_static! { - static ref MAP: DashMap = DashMap::new(); -} +static MAP: Lazy> = Lazy::new(|| DashMap::new()); /// Get the current time from Unix Epoch as a Duration fn now() -> Duration {