Added a service for generating IDs

This commit is contained in:
Natty 2024-11-12 22:20:57 +01:00
parent 5241b18b0d
commit 766fd8ea7d
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
3 changed files with 43 additions and 1 deletions

View File

@ -79,6 +79,7 @@ tower-http = "0.5"
tracing = "0.1"
tracing-subscriber = "0.3"
ts-rs = "7"
ulid = "1"
unicode-segmentation = "1.10"
url = "2.3"
walkdir = "2.3"
@ -108,6 +109,7 @@ tokio = { workspace = true, features = ["full"] }
tokio-stream = { workspace = true }
tower = { workspace = true }
tower-http = { workspace = true, features = ["cors", "trace", "fs"] }
ulid = { workspace = true }
url = { workspace = true }
idna = { workspace = true }
@ -118,6 +120,7 @@ tracing = { workspace = true }
cfg-if = { workspace = true }
bytes = { workspace = true }
compact_str = { workspace = true }
either = { workspace = true }
futures = { workspace = true }

29
src/service/gen_id.rs Normal file
View File

@ -0,0 +1,29 @@
use std::{sync::Arc, time::SystemTime};
use super::MagnetarService;
pub struct GenIdService;
impl GenIdService {
pub fn new_id(&self) -> ulid::Ulid {
ulid::Ulid::new()
}
pub fn new_id_str(&self) -> String {
self.new_id().to_string()
}
pub fn new_for_time(&self, time: impl Into<SystemTime>) -> ulid::Ulid {
ulid::Ulid::from_datetime(time.into())
}
pub fn new_str_for_time(&self, time: impl Into<SystemTime>) -> String {
self.new_for_time(time).to_string()
}
}
impl AsRef<GenIdService> for Arc<MagnetarService> {
fn as_ref(&self) -> &GenIdService {
&self.gen_id
}
}

View File

@ -1,3 +1,4 @@
use gen_id::GenIdService;
use magnetar_common::config::MagnetarConfig;
use magnetar_model::{ck, CalckeyCache, CalckeyModel};
use std::fmt::{Debug, Formatter};
@ -5,15 +6,16 @@ use std::time::Duration;
use thiserror::Error;
pub mod emoji_cache;
pub mod gen_id;
pub mod generic_id_cache;
pub mod instance_cache;
pub mod instance_meta_cache;
pub mod local_user_cache;
#[non_exhaustive]
pub struct MagnetarService {
pub db: CalckeyModel,
pub gen_id: GenIdService,
pub cache: CalckeyCache,
pub config: &'static MagnetarConfig,
pub local_user_cache: local_user_cache::LocalUserCacheService,
@ -67,6 +69,14 @@ impl MagnetarService {
remote_instance_cache,
emoji_cache,
drive_file_cache,
gen_id: GenIdService,
})
}
pub fn service<T>(&self) -> &T
where
Self: AsRef<T>,
{
self.as_ref()
}
}