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

@ -33,7 +33,7 @@ pub struct Community {
private_key: Secret,
public_key: PublicKey,
pub interactables: Arc<RwLock<Vec<Arc<Box<dyn Interactable>>>>>,
pub connections: Arc<RwLock<HashMap<Uuid, Vec<Arc<CommunityConnection>>>>>,
pub connections: Arc<RwLock<HashMap<i64, Vec<Arc<CommunityConnection>>>>>,
}
impl Community {
@ -130,33 +130,33 @@ impl Community {
.connections
.read()
.await
.get(&other.get_user_id().await.unwrap())
.get(&other.get_user_id().await)
.cloned()
.unwrap_or_default();
vec.push(other.clone());
self.connections
.write()
.await
.insert(other.get_user_id().await.unwrap(), vec);
.insert(other.get_user_id().await, vec);
}
pub async fn remove_connection(self: &Arc<Self>, other: Arc<CommunityConnection>) {
let mut vec = self
.connections
.read()
.await
.get(&other.get_user_id().await.unwrap())
.get(&other.get_user_id().await)
.cloned()
.unwrap_or_default();
vec.retain(|conn| !Arc::ptr_eq(conn, &other));
self.connections
.write()
.await
.insert(other.get_user_id().await.unwrap(), vec);
.insert(other.get_user_id().await, vec);
}
pub async fn get_connections(&self) -> HashMap<Uuid, Vec<Arc<CommunityConnection>>> {
pub async fn get_connections(&self) -> HashMap<i64, Vec<Arc<CommunityConnection>>> {
self.connections.read().await.clone()
}
pub async fn get_connections_for_user(&self, user_id: Uuid) -> Vec<Arc<CommunityConnection>> {
pub async fn get_connections_for_user(&self, user_id: i64) -> Vec<Arc<CommunityConnection>> {
self.connections
.read()
.await
@ -166,7 +166,7 @@ impl Community {
}
pub async fn get_interactables(
&self,
user_id: Uuid,
user_id: i64,
) -> Vec<Arc<Box<dyn Interactable + 'static>>> {
self.interactables.read().await.clone()
}
@ -191,7 +191,7 @@ impl Community {
}
pub async fn run_function(
self: &mut Arc<Self>,
user_id: Uuid,
user_id: i64,
name: &str,
path: &str,
function: &str,

View file

@ -12,6 +12,7 @@ use hkdf::Hkdf;
use hyper::upgrade::Upgraded;
use hyper_util::rt::TokioIo;
use json::JsonValue;
use json::number::Number;
use rand::{Rng, distributions::Alphanumeric};
use sha2::Sha256;
use std::sync::Arc;
@ -24,7 +25,7 @@ use x448::PublicKey;
pub struct CommunityConnection {
pub sender: Arc<RwLock<SplitSink<WebSocketStream<TokioIo<Upgraded>>, Message>>>,
pub receiver: Arc<RwLock<SplitStream<WebSocketStream<TokioIo<Upgraded>>>>>,
pub user_id: Arc<RwLock<Option<Uuid>>>,
pub user_id: Arc<RwLock<i64>>,
pub community: Arc<RwLock<Option<Arc<Community>>>>,
identified: Arc<RwLock<bool>>,
challenged: Arc<RwLock<bool>>,
@ -41,7 +42,7 @@ impl CommunityConnection {
Arc::new(Self {
sender: Arc::new(RwLock::new(sender)),
receiver: Arc::new(RwLock::new(receiver)),
user_id: Arc::new(RwLock::new(None)),
user_id: Arc::new(RwLock::new(0)),
community: Arc::new(RwLock::new(Some(community))),
identified: Arc::new(RwLock::new(false)),
challenged: Arc::new(RwLock::new(false)),
@ -58,7 +59,7 @@ impl CommunityConnection {
pub async fn get_community(&self) -> Option<Arc<Community>> {
self.community.read().await.clone()
}
pub async fn get_user_id(&self) -> Option<Uuid> {
pub async fn get_user_id(&self) -> i64 {
*self.user_id.read().await
}
pub async fn is_identified(&self) -> bool {
@ -67,9 +68,8 @@ impl CommunityConnection {
pub async fn handle_message(self: Arc<Self>, message: String) {
let mut cv = CommunicationValue::from_json(&message);
if let Some(user_id) = self.get_user_id().await {
cv = cv.with_sender(user_id);
}
let user_id = self.get_user_id().await;
cv = cv.with_sender(user_id);
if cv.is_type(CommunicationType::identification) && !self.is_identified().await {
self.handle_identification(cv).await;
@ -109,30 +109,17 @@ impl CommunityConnection {
.get_community()
.await
.unwrap()
.run_function(self.get_user_id().await.unwrap(), name, path, function, &cv)
.run_function(self.get_user_id().await, name, path, function, &cv)
.await;
self.send_message(&result).await;
}
async fn handle_identification(&self, cv: CommunicationValue) {
let user_id = match cv.get_data(DataTypes::user_id) {
Some(id_str) => match Uuid::parse_str(&id_str.to_string()) {
Ok(id) => id,
Err(_) => {
self.send_error_response(
&cv.get_id(),
CommunicationType::error_invalid_user_id,
)
.await;
return;
}
},
None => {
self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_user_id)
.await;
return;
}
};
let user_id = cv
.get_data(DataTypes::user_id)
.unwrap_or(&JsonValue::Number(Number::from(0)))
.as_i64()
.unwrap_or(0);
let Some(user) = get_user(user_id).await else {
self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_user_id)
@ -145,7 +132,7 @@ impl CommunityConnection {
*auth_guard = Some(user.clone());
let mut user_id_guard = self.user_id.write().await;
*user_id_guard = Some(user_id);
*user_id_guard = user_id;
let mut identified_guard = self.identified.write().await;
*identified_guard = true;
@ -347,11 +334,12 @@ impl CommunityConnection {
};
let arc = Arc::new(community);
let Some(user_id) = self.get_user_id().await else {
let user_id = self.get_user_id().await;
if user_id == 0 {
self.send_error_response(&cv.get_id(), CommunicationType::error)
.await;
return;
};
}
arc.add_connection(self.clone()).await;
@ -382,7 +370,7 @@ impl CommunityConnection {
}
pub async fn handle_close(self: Arc<Self>) {
if self.is_identified().await {
if let Some(_) = self.get_user_id().await {
if self.get_user_id().await != 0 {
self.community
.read()
.await

View file

@ -28,7 +28,7 @@ impl TextChat {
community: Arc::new(Community::new()),
}
}
pub fn add_message(&self, send_time: u128, sender: Uuid, message: &str) {
pub fn add_message(&self, send_time: u128, sender: i64, message: &str) {
let user_dir = &format!(
"communities/{}/interactables/{}/{}",
self.get_community().get_name(),
@ -211,12 +211,11 @@ impl Interactable for TextChat {
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis();
self.add_message(milliseconds_timestamp, cv.get_sender().unwrap(), message);
self.add_message(milliseconds_timestamp, cv.get_sender(), message);
let mut distribution_payload = JsonValue::new_object();
distribution_payload["message"] = JsonValue::String(message.to_string());
distribution_payload["sender_id"] =
JsonValue::String(cv.get_sender().unwrap().to_string());
distribution_payload["sender_id"] = JsonValue::String(cv.get_sender().to_string());
distribution_payload["send_time"] =
JsonValue::String(milliseconds_timestamp.to_string());
let distribution = CommunicationValue::new(CommunicationType::update)
@ -226,7 +225,7 @@ impl Interactable for TextChat {
.add_data_str(DataTypes::result, "message_live".to_string())
.add_data(DataTypes::payload, distribution_payload);
let connections: HashMap<Uuid, Vec<Arc<CommunityConnection>>> =
let connections: HashMap<i64, Vec<Arc<CommunityConnection>>> =
self.get_community().get_connections().await.clone();
for con in connections.values() {