Swapped Id's to be Unix Timestamps instead of UUID's

This commit is contained in:
Alex Emmet 2025-12-07 16:11:22 +01:00
commit b9208c190e
13 changed files with 352 additions and 291 deletions

View file

@ -8,6 +8,7 @@ use crate::calls::caller::Caller;
pub struct CallGroup {
pub call_id: Uuid,
pub members: RwLock<Vec<Arc<Caller>>>,
pub show: RwLock<bool>,
}
impl CallGroup {
@ -15,13 +16,15 @@ impl CallGroup {
CallGroup {
call_id,
members: RwLock::new(vec![user]),
show: RwLock::new(true),
}
}
pub async fn add_member(self: Arc<Self>, member: Uuid, inviter: Uuid) {
pub async fn add_member(self: Arc<Self>, member: i64, inviter: i64) {
*self.show.write().await = true;
self.members
.write()
.await
.push(Arc::new(Caller::new(member, inviter, self.call_id)));
.push(Arc::new(Caller::new(member, self.call_id, inviter)));
}
}

View file

@ -1,13 +1,17 @@
use livekit_api::services::room::RoomClient;
use once_cell::sync::Lazy;
use std::sync::Arc;
use std::{str::FromStr, sync::Arc, time::Duration};
use tokio::sync::RwLock;
use uuid::Uuid;
use crate::calls::{call_group::CallGroup, caller::Caller};
use crate::{
calls::{call_group::CallGroup, caller::Caller},
util::print::{PrintType, line},
};
static CALL_GROUPS: Lazy<RwLock<Vec<Arc<CallGroup>>>> = Lazy::new(|| RwLock::new(Vec::new()));
pub async fn get_call_invites(user_id: Uuid) -> Vec<Arc<Caller>> {
pub async fn get_call_invites(user_id: i64) -> Vec<Arc<Caller>> {
let mut callers = Vec::new();
for cg in CALL_GROUPS.read().await.iter() {
let members = cg.members.read().await;
@ -20,7 +24,7 @@ pub async fn get_call_invites(user_id: Uuid) -> Vec<Arc<Caller>> {
callers
}
pub async fn get_call_groups(user_id: Uuid) -> Vec<Arc<CallGroup>> {
pub async fn get_call_groups(user_id: i64) -> Vec<Arc<CallGroup>> {
let mut call_groups = Vec::new();
for cg in CALL_GROUPS.read().await.iter() {
let is_member = {
@ -35,7 +39,7 @@ pub async fn get_call_groups(user_id: Uuid) -> Vec<Arc<CallGroup>> {
call_groups
}
pub async fn get_call_token(user_id: Uuid, call_id: Uuid) -> Option<String> {
pub async fn get_call_token(user_id: i64, call_id: Uuid) -> Option<String> {
let existing_group = {
let call_groups = CALL_GROUPS.read().await;
call_groups.iter().find(|g| g.call_id == call_id).cloned()
@ -80,11 +84,13 @@ pub async fn get_call_token(user_id: Uuid, call_id: Uuid) -> Option<String> {
Some(caller.create_token())
}
pub async fn add_invite(call_id: Uuid, inviter_id: Uuid, invitee_id: Uuid) -> bool {
let target_group = {
let call_groups = CALL_GROUPS.read().await;
call_groups.iter().find(|g| g.call_id == call_id).cloned()
};
pub async fn add_invite(call_id: Uuid, inviter_id: i64, invitee_id: i64) -> bool {
let target_group: Option<Arc<CallGroup>> = CALL_GROUPS
.read()
.await
.iter()
.find(|g| g.call_id == call_id)
.cloned();
if let Some(cg) = target_group {
let mut members = cg.members.write().await;
@ -100,18 +106,45 @@ pub async fn add_invite(call_id: Uuid, inviter_id: Uuid, invitee_id: Uuid) -> bo
}
false
}
pub async fn get_call_group_by_user(user_id: Uuid) -> Option<Arc<CallGroup>> {
let call_groups = CALL_GROUPS.read().await;
for cg in call_groups.iter() {
let is_member = {
let members = cg.members.read().await;
members.iter().any(|m| m.user_id == user_id)
};
if is_member {
return Some(cg.clone());
pub fn garbage_collect_calls() {
tokio::spawn(async move {
loop {
clean_calls().await;
tokio::time::sleep(Duration::from_secs(2)).await;
}
});
}
pub async fn clean_calls() {
let room_service = RoomClient::new("https://call.tensamin.net").unwrap();
let rooms = room_service.list_rooms(Vec::new()).await.unwrap();
let mut call_ids: Vec<Uuid> = Vec::new();
let mut no_users: Vec<Uuid> = Vec::new();
for room in rooms {
if let Ok(id) = Uuid::from_str(&room.name) {
if room.num_participants == 0 {
no_users.push(id);
}
call_ids.push(id);
}
}
None
let mut call_groups = CALL_GROUPS.write().await;
let size_pre = call_groups.len();
call_groups.retain(|cg| call_ids.contains(&cg.call_id));
for cg in call_groups.iter() {
*cg.show.write().await = !no_users.contains(&cg.call_id);
}
let size_post = call_groups.len();
drop(call_groups);
if size_pre - size_post != 0 {
line(
PrintType::CallIn,
&format!(
"Cleaned {} calls, {} remaining",
size_pre - size_post,
size_post
),
);
}
}

View file

@ -2,10 +2,7 @@ use livekit_api::access_token;
use std::env;
use uuid::Uuid;
pub fn create_token(
user_id: Uuid,
call_id: Uuid,
) -> Result<String, access_token::AccessTokenError> {
pub fn create_token(user_id: i64, call_id: Uuid) -> Result<String, access_token::AccessTokenError> {
let api_key = env::var("LIVEKIT_API_KEY").expect("LIVEKIT_API_KEY is not set");
let api_secret = env::var("LIVEKIT_API_SECRET").expect("LIVEKIT_API_SECRET is not set");

View file

@ -3,13 +3,13 @@ use uuid::Uuid;
use crate::calls::call_util;
pub struct Caller {
pub user_id: Uuid,
pub user_id: i64,
pub call_id: Uuid,
pub inviters: Vec<Uuid>,
pub inviters: Vec<i64>,
}
impl Caller {
pub fn new(user_id: Uuid, call_id: Uuid, inviter_id: Uuid) -> Self {
pub fn new(user_id: i64, call_id: Uuid, inviter_id: i64) -> Self {
Caller {
user_id,
call_id,