Skip to content

Commit c828c61

Browse files
committed
feat(protolok): add and implement snowflakes
1 parent 86a4e3d commit c828c61

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[workspace]
22
members = ["loki-client", "loki-server", "loki-shared", "lokui", "protolok"]
3+
resolver = "2"

protolok/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod ids;
12
pub mod message;
23
pub mod user;

protolok/src/message.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use rsa::{PaddingScheme, Pkcs1v15Encrypt, PublicKey, RsaPrivateKey, RsaPublicKey};
1+
use rsa::{Pkcs1v15Encrypt, PublicKey, RsaPrivateKey, RsaPublicKey};
22

3-
use crate::user::UserHandle;
3+
use crate::{
4+
ids::{make_id, LokiMeta, Object},
5+
user::UserHandle,
6+
};
47

58
#[derive(Clone)]
69
pub struct UnencryptedContent {
@@ -19,11 +22,11 @@ pub enum MessageContent {
1922
Unencrypted(UnencryptedContent),
2023
}
2124
impl MessageContent {
22-
fn is_encrypted(&self) -> bool {
25+
pub fn is_encrypted(&self) -> bool {
2326
matches!(self, &MessageContent::Encrypted { .. })
2427
}
2528

26-
fn decrypt(&self, key: &RsaPrivateKey) -> Result<MessageContent, rsa::errors::Error> {
29+
pub fn decrypt(&self, key: &RsaPrivateKey) -> Result<MessageContent, rsa::errors::Error> {
2730
match self {
2831
MessageContent::Encrypted(EncryptedContent { hash, enc_text }) => {
2932
key.decrypt(Pkcs1v15Encrypt, &enc_text).map(|x| {
@@ -39,9 +42,9 @@ impl MessageContent {
3942
}
4043

4144
pub struct Message {
42-
id: Option<String>,
43-
from: Option<UserHandle>,
44-
content: MessageContent,
45+
pub id: u64,
46+
pub from: Option<UserHandle>,
47+
pub content: MessageContent,
4548
}
4649

4750
pub fn encrypt(
@@ -57,3 +60,9 @@ pub fn encrypt(
5760
)?,
5861
}))
5962
}
63+
64+
impl Object for Message {
65+
fn initialize(&mut self, meta: &LokiMeta) {
66+
self.id = make_id(meta);
67+
}
68+
}

protolok/src/user.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
1-
use std::time::SystemTime;
2-
3-
use sha2::{Digest, Sha256};
1+
use crate::ids::{make_id, LokiMeta, Object};
42

53
pub struct UserHandle {
6-
name: String,
7-
home: String,
4+
pub name: String,
5+
pub home: String,
6+
pub id: u64,
7+
}
8+
9+
impl Object for UserHandle {
10+
fn initialize(&mut self, meta: &LokiMeta) {
11+
self.id = make_id(meta);
12+
}
813
}
914

10-
impl UserHandle {
11-
pub fn get_id(&self) -> String {
12-
let mut buf = [0u8; 64];
13-
format!(
14-
"{:X}",
15-
SystemTime::now()
16-
.duration_since(SystemTime::UNIX_EPOCH)
17-
.unwrap()
18-
.as_millis()
19-
) + &base16ct::upper::encode_str(&Sha256::digest(self.name.as_bytes()), &mut buf).unwrap()
20-
[..6] + &base16ct::upper::encode_str(&Sha256::digest(self.home.as_bytes()), &mut buf)
21-
.unwrap()[..6]
15+
#[cfg(test)]
16+
mod tests {
17+
use std::sync::OnceLock;
18+
19+
use crate::ids::{id_to_parts, to_unix_time, LokiMeta, Object};
20+
21+
use super::UserHandle;
22+
23+
#[test]
24+
fn init_test() {
25+
static META: OnceLock<LokiMeta> = OnceLock::new();
26+
META.get_or_init(|| LokiMeta::new("test.lokichat.xyz"));
27+
let mut handle = UserHandle {
28+
name: "TudbuT".to_owned(),
29+
home: "test.lokichat.xyz".to_owned(),
30+
id: 0,
31+
};
32+
handle.initialize(META.get().unwrap());
33+
dbg!(handle.id);
34+
dbg!(id_to_parts(handle.id));
35+
dbg!(to_unix_time(id_to_parts(handle.id).0));
2236
}
2337
}

0 commit comments

Comments
 (0)