Swap to UNIX timestamps as user ID's

This commit is contained in:
Alex Emmet 2025-12-07 16:10:31 +01:00
commit 9d43687f8b
18 changed files with 234 additions and 276 deletions

View file

@ -1,10 +1,9 @@
use json::{self, JsonValue};
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct Contact {
pub user_id: Option<Uuid>,
pub user_id: Option<i64>,
pub user_name: Option<String>,
pub last_message_at: Option<i64>,
}
@ -24,7 +23,7 @@ impl Default for Contact {
}
impl Contact {
pub fn new(user_id: Uuid) -> Self {
pub fn new(user_id: i64) -> Self {
Contact {
user_id: Some(user_id),
user_name: None,
@ -49,7 +48,7 @@ impl Contact {
obj
}
pub fn from_json(o: &JsonValue) -> Contact {
let user_id = o["user_id"].as_str().and_then(|s| Uuid::parse_str(s).ok());
let user_id = o["user_id"].as_i64();
let user_name = o["user_name"].as_str().map(|s| s.to_string());

View file

@ -2,12 +2,11 @@ use crate::util::file_util::save_file;
use json::{self, Array, JsonValue};
use std::fs;
use std::path::Path;
use uuid::Uuid;
pub struct UserCommunityUtil;
impl UserCommunityUtil {
pub fn add_community(storage_owner: Uuid, address: String, title: String, position: String) {
pub fn add_community(storage_owner: i64, address: String, title: String, position: String) {
let file_path = format!("users/{}/", storage_owner);
let mut communities = Self::load_array(&file_path);
@ -25,7 +24,7 @@ impl UserCommunityUtil {
);
}
pub fn remove_community(storage_owner: Uuid, community_address: String) {
pub fn remove_community(storage_owner: i64, community_address: String) {
let file_path = format!("users/{}/", storage_owner);
let communities = Self::load_array(&file_path);
@ -41,7 +40,7 @@ impl UserCommunityUtil {
);
}
pub fn get_communities(storage_owner: Uuid) -> Array {
pub fn get_communities(storage_owner: i64) -> Array {
let file_path = format!("users/{}/communities.json", storage_owner);
Self::load_array(&file_path)
}

View file

@ -13,9 +13,7 @@ use rand_core::OsRng;
use rand_core::RngCore;
use sha2::{Digest, Sha256};
use std::io::{self};
use std::str::FromStr;
use std::sync::Mutex;
use uuid::Uuid;
use x448::{PublicKey, Secret};
static USERS: Lazy<Mutex<Vec<UserProfile>>> = Lazy::new(|| Mutex::new(Vec::new()));
@ -25,7 +23,7 @@ static UNIQUE: Lazy<Mutex<bool>> = Lazy::new(|| Mutex::new(false));
pub async fn load_from_tu(username: &str) -> Result<(), ()> {
let file_content = load_file("", &format!("{}.tu", username));
let segments = file_content.split("::").collect::<Vec<&str>>();
let uuid = Uuid::from_str(segments[0]).unwrap();
let uuid = segments[0].parse::<i64>().unwrap_or(0);
let b64_private_key = segments[1];
let secret: Secret = crypto_helper::load_secret_key(b64_private_key).unwrap();
@ -86,7 +84,7 @@ pub async fn create_user(username: &str) -> (Option<UserProfile>, Option<String>
(Some(up), Some(STANDARD.encode(&private_key.as_bytes())))
}
pub fn get_user(user_id: Uuid) -> Option<UserProfile> {
pub fn get_user(user_id: i64) -> Option<UserProfile> {
USERS
.lock()
.unwrap()
@ -99,7 +97,7 @@ pub fn get_users() -> Vec<UserProfile> {
USERS.lock().unwrap().clone()
}
pub fn remove_user(user_id: Uuid) {
pub fn remove_user(user_id: i64) {
let mut users = USERS.lock().unwrap();
users.retain(|u| u.user_id != user_id);
*UNIQUE.lock().unwrap() = true;

View file

@ -8,12 +8,11 @@ use base64::{Engine as _, engine::general_purpose};
use json::{JsonValue, object};
use rand::Rng;
use rand::rngs::OsRng;
use uuid::Uuid;
// --- UserProfile ---
#[derive(Clone, Debug)]
pub struct UserProfile {
pub user_id: Uuid,
pub user_id: i64,
pub username: String,
pub public_key: String,
pub private_key_hash: String,
@ -24,7 +23,7 @@ pub struct UserProfile {
impl UserProfile {
pub fn new(
user_id: Uuid,
user_id: i64,
username: String,
display_name: Option<String>,
public_key: String,
@ -47,7 +46,7 @@ impl UserProfile {
pub fn to_json(&self) -> JsonValue {
let mut obj = object! {
"uuid" => self.user_id.to_string(),
"uuid" => self.user_id,
"username" => self.username.clone(),
"public_key" => self.public_key.clone(),
"private_key_hash" => self.private_key_hash.clone(),
@ -61,7 +60,7 @@ impl UserProfile {
}
pub fn frontend(&self) -> JsonValue {
let mut obj = object! {
"uuid" => self.user_id.to_string(),
"uuid" => self.user_id,
"username" => self.username.clone(),
"public_key" => self.public_key.clone(),
"private_key_hash" => self.private_key_hash.clone(),
@ -78,7 +77,7 @@ impl UserProfile {
obj
}
pub async fn from_json(j: &JsonValue) -> Option<Self> {
let user_id = Uuid::parse_str(j["uuid"].as_str()?).ok()?;
let user_id = j["uuid"].as_i64()?;
let username = j["username"].as_str()?.to_string();
let public_key = j["public_key"].as_str()?.to_string();
let private_key_hash = j["private_key_hash"].as_str()?.to_string();