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

@ -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
),
);
}
}