Call Kicking and Timeouting, basics of anonymous calls
This commit is contained in:
parent
494e08241c
commit
309342b730
5 changed files with 159 additions and 22 deletions
|
|
@ -9,6 +9,7 @@ pub struct CallGroup {
|
|||
pub call_id: Uuid,
|
||||
pub members: RwLock<Vec<Arc<Caller>>>,
|
||||
pub show: RwLock<bool>,
|
||||
pub anonymous_joining: RwLock<bool>,
|
||||
}
|
||||
|
||||
impl CallGroup {
|
||||
|
|
@ -17,6 +18,27 @@ impl CallGroup {
|
|||
call_id,
|
||||
members: RwLock::new(vec![user]),
|
||||
show: RwLock::new(true),
|
||||
anonymous_joining: RwLock::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_caller(&self, user_id: i64) -> Option<Arc<Caller>> {
|
||||
self.members
|
||||
.read()
|
||||
.await
|
||||
.iter()
|
||||
.find(|caller| caller.user_id == user_id)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub async fn set_anonymous_joining(&self, enable: bool) {
|
||||
*self.anonymous_joining.write().await = enable;
|
||||
}
|
||||
|
||||
pub async fn remove_caller(&self, user_id: i64) {
|
||||
self.members
|
||||
.write()
|
||||
.await
|
||||
.retain(|caller| caller.user_id != user_id);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use livekit_api::services::room::RoomClient;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{env, str::FromStr, sync::Arc, time::Duration};
|
||||
use std::{str::FromStr, sync::Arc, time::Duration};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use uuid::Uuid;
|
||||
|
|
@ -28,6 +28,11 @@ pub async fn get_call_groups(user_id: i64) -> Vec<Arc<CallGroup>> {
|
|||
call_groups
|
||||
}
|
||||
|
||||
pub async fn get_call(call_id: Uuid) -> Option<Arc<CallGroup>> {
|
||||
let call_groups = CALL_GROUPS.read().await;
|
||||
call_groups.iter().find(|g| g.call_id == call_id).cloned()
|
||||
}
|
||||
|
||||
pub async fn get_call_token(user_id: i64, call_id: Uuid) -> Option<String> {
|
||||
let existing_group = {
|
||||
let call_groups = CALL_GROUPS.read().await;
|
||||
|
|
@ -36,21 +41,16 @@ pub async fn get_call_token(user_id: i64, call_id: Uuid) -> Option<String> {
|
|||
|
||||
// if the group exists
|
||||
if let Some(cg) = existing_group {
|
||||
let members = cg.members.write().await;
|
||||
let members = cg.members.read().await;
|
||||
|
||||
// if the user is already a member
|
||||
if let Some(member) = members.iter().find(|m| m.user_id == user_id) {
|
||||
if member.is_timeout().await {
|
||||
return None;
|
||||
}
|
||||
return Some(member.create_token());
|
||||
}
|
||||
return None;
|
||||
/*
|
||||
let new_caller = Arc::new(Caller::new(user_id, call_id, user_id));
|
||||
let token = new_caller.create_token();
|
||||
|
||||
members.push(new_caller);
|
||||
|
||||
return Some(token);
|
||||
*/
|
||||
}
|
||||
|
||||
let mut call_groups = CALL_GROUPS.write().await;
|
||||
|
|
@ -85,6 +85,7 @@ pub async fn add_invite(call_id: Uuid, inviter_id: i64, invitee_id: i64) -> bool
|
|||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn garbage_collect_calls() {
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,9 @@
|
|||
use std::{
|
||||
sync::Arc,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use tokio::sync::RwLock;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::calls::call_util;
|
||||
|
|
@ -6,6 +12,7 @@ pub struct Caller {
|
|||
pub user_id: i64,
|
||||
pub call_id: Uuid,
|
||||
pub has_admin: bool,
|
||||
pub timeout: RwLock<i64>,
|
||||
}
|
||||
|
||||
impl Caller {
|
||||
|
|
@ -14,6 +21,7 @@ impl Caller {
|
|||
user_id,
|
||||
call_id,
|
||||
has_admin,
|
||||
timeout: RwLock::new(0),
|
||||
}
|
||||
}
|
||||
pub fn set_admin(&mut self, has_admin: bool) {
|
||||
|
|
@ -22,6 +30,16 @@ impl Caller {
|
|||
pub fn has_admin(&self) -> bool {
|
||||
self.has_admin
|
||||
}
|
||||
pub async fn is_timeout(&self) -> bool {
|
||||
*self.timeout.read().await
|
||||
> SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as i64
|
||||
}
|
||||
pub async fn set_timeout(&self, timeout: i64) {
|
||||
*self.timeout.write().await = timeout;
|
||||
}
|
||||
pub fn create_token(&self) -> String {
|
||||
if let Ok(token) = call_util::create_token(self.user_id, self.call_id, self.has_admin()) {
|
||||
token
|
||||
|
|
|
|||
Loading…
Reference in a new issue