[Clean]
This commit is contained in:
parent
e8faca34e2
commit
0990ccd526
16 changed files with 139 additions and 393 deletions
|
|
@ -1,17 +1,11 @@
|
|||
use dashmap::DashMap;
|
||||
use livekit::Room;
|
||||
use livekit_api::services::room::RoomClient;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::{env, str::FromStr, sync::Arc, time::Duration};
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
calls::{call_group::CallGroup, caller::Caller},
|
||||
log, log_err,
|
||||
util::logger::PrintType,
|
||||
};
|
||||
use crate::calls::{call_group::CallGroup, caller::Caller};
|
||||
|
||||
static CALL_GROUPS: Lazy<DashMap<Uuid, Arc<CallGroup>>> = Lazy::new(|| DashMap::new());
|
||||
pub static CALL_GROUPS: Lazy<DashMap<Uuid, Arc<CallGroup>>> = Lazy::new(|| DashMap::new());
|
||||
|
||||
pub async fn get_call_invites(user_id: i64) -> Vec<Arc<Caller>> {
|
||||
let mut callers = Vec::new();
|
||||
|
|
@ -101,59 +95,3 @@ 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 {
|
||||
clean_calls().await;
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
pub async fn clean_calls() {
|
||||
let api_key = match env::var("LIVEKIT_API_KEY") {
|
||||
Ok(key) => key,
|
||||
Err(_) => {
|
||||
log_err!(PrintType::General, "LIVEKIT_API_KEY not set!");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let api_secret = match env::var("LIVEKIT_API_SECRET") {
|
||||
Ok(secret) => secret,
|
||||
Err(_) => {
|
||||
log_err!(PrintType::General, "LIVEKIT_API_SECRET not set!");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let room_service = RoomClient::with_api_key("https:call.tensamin.net", &api_key, &api_secret);
|
||||
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);
|
||||
}
|
||||
}
|
||||
let size_pre = CALL_GROUPS.len();
|
||||
for (id, _) in CALL_GROUPS.clone().into_iter() {
|
||||
if !call_ids.contains(&id) {
|
||||
CALL_GROUPS.remove(&id);
|
||||
}
|
||||
}
|
||||
for (_, cg) in CALL_GROUPS.clone().into_iter() {
|
||||
*cg.show.write().await = !no_users.contains(&cg.call_id);
|
||||
}
|
||||
|
||||
let size_post = CALL_GROUPS.len();
|
||||
if size_pre - size_post != 0 {
|
||||
log!(
|
||||
PrintType::Call,
|
||||
"Cleaned {} calls, {} remaining",
|
||||
size_pre - size_post,
|
||||
size_post
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,29 @@
|
|||
use livekit_api::access_token;
|
||||
use livekit_api::{
|
||||
access_token::{self},
|
||||
services::room::RoomClient,
|
||||
};
|
||||
use std::env;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn create_token(
|
||||
user_id: i64,
|
||||
call_id: Uuid,
|
||||
has_admin: bool,
|
||||
) -> 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");
|
||||
use crate::{calls::call_manager::CALL_GROUPS, log, log_err, util::logger::PrintType};
|
||||
|
||||
pub fn create_token(user_id: i64, call_id: Uuid, has_admin: bool) -> Result<String, ()> {
|
||||
let api_key = match env::var("LIVEKIT_API_KEY") {
|
||||
Ok(key) => key,
|
||||
Err(_) => {
|
||||
log_err!(PrintType::General, "LIVEKIT_API_KEY not set!");
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
let api_secret = match env::var("LIVEKIT_API_SECRET") {
|
||||
Ok(secret) => secret,
|
||||
Err(_) => {
|
||||
log_err!(PrintType::General, "LIVEKIT_API_SECRET not set!");
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
|
||||
let token = access_token::AccessToken::with_api_key(&api_key, &api_secret)
|
||||
.with_identity(&user_id.to_string())
|
||||
|
|
@ -21,5 +36,72 @@ pub fn create_token(
|
|||
})
|
||||
.with_metadata(&format!("{{\"isAdmin\":{}}}", has_admin))
|
||||
.to_jwt();
|
||||
return token;
|
||||
if let Ok(token) = token {
|
||||
Ok(token)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn garbage_collect_calls() {
|
||||
tokio::spawn(async move {
|
||||
let api_key = match env::var("LIVEKIT_API_KEY") {
|
||||
Ok(key) => key,
|
||||
Err(_) => {
|
||||
log_err!(PrintType::General, "LIVEKIT_API_KEY not set!");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let api_secret = match env::var("LIVEKIT_API_SECRET") {
|
||||
Ok(secret) => secret,
|
||||
Err(_) => {
|
||||
log_err!(PrintType::General, "LIVEKIT_API_SECRET not set!");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let hostname = match env::var("LIVEKI_HOSTNAME") {
|
||||
Ok(secret) => secret,
|
||||
Err(_) => {
|
||||
log_err!(PrintType::General, "LIVEKI_HOSTNAME not set!");
|
||||
return;
|
||||
}
|
||||
};
|
||||
loop {
|
||||
let room_service = RoomClient::with_api_key(&hostname, &api_key, &api_secret);
|
||||
clean_calls(room_service).await;
|
||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
pub async fn clean_calls(room_service: RoomClient) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
let size_pre = CALL_GROUPS.len();
|
||||
for (id, _) in CALL_GROUPS.clone().into_iter() {
|
||||
if !call_ids.contains(&id) {
|
||||
CALL_GROUPS.remove(&id);
|
||||
}
|
||||
}
|
||||
for (_, cg) in CALL_GROUPS.clone().into_iter() {
|
||||
*cg.show.write().await = !no_users.contains(&cg.call_id);
|
||||
}
|
||||
|
||||
let size_post = CALL_GROUPS.len();
|
||||
if size_pre - size_post != 0 {
|
||||
log!(
|
||||
PrintType::Call,
|
||||
"Cleaned {} calls, {} remaining",
|
||||
size_pre - size_post,
|
||||
size_post
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ impl Caller {
|
|||
pub fn has_admin(&self) -> bool {
|
||||
self.has_admin
|
||||
}
|
||||
pub async fn is_timeout(&self) -> bool {
|
||||
pub async fn is_timeouted(&self) -> bool {
|
||||
*self.timeout.read().await
|
||||
> SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
|
|
|
|||
Loading…
Reference in a new issue