[QOL] Call Utility
This commit is contained in:
parent
23b6f4ef4a
commit
b58729e919
5 changed files with 77 additions and 26 deletions
|
|
@ -44,6 +44,12 @@ impl CallGroup {
|
|||
pub async fn set_anonymous_joining(&self, enable: bool) {
|
||||
*self.anonymous_joining.write().await = enable;
|
||||
|
||||
let _ = call_util::set_room_metadata(
|
||||
self.call_id,
|
||||
format!("{{\"anonymous_joining\": \"{}\"}}", enable),
|
||||
)
|
||||
.await;
|
||||
|
||||
if self.short_link.read().await.is_none() {
|
||||
let long_link = format!(
|
||||
"https://app.tensamin.net/call/anonymous?call_id={}&omikron_id={}",
|
||||
|
|
@ -88,6 +94,7 @@ impl CallGroup {
|
|||
}
|
||||
|
||||
pub async fn remove_caller(&self, user_id: i64) {
|
||||
let _ = call_util::remove_participant(self.call_id, user_id).await;
|
||||
self.members
|
||||
.write()
|
||||
.await
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use livekit_api::{
|
|||
access_token::{self},
|
||||
services::room::RoomClient,
|
||||
};
|
||||
use livekit_protocol::Room;
|
||||
use std::env;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
|
|
@ -9,7 +10,14 @@ use uuid::Uuid;
|
|||
|
||||
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, ()> {
|
||||
pub fn get_livekit() -> Result<(String, String, String), ()> {
|
||||
let hostname = match env::var("LIVEKI_HOSTNAME") {
|
||||
Ok(secret) => secret,
|
||||
Err(_) => {
|
||||
log_err!(PrintType::General, "LIVEKI_HOSTNAME not set!");
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
let api_key = match env::var("LIVEKIT_API_KEY") {
|
||||
Ok(key) => key,
|
||||
Err(_) => {
|
||||
|
|
@ -24,11 +32,15 @@ pub fn create_token(user_id: i64, call_id: Uuid, has_admin: bool) -> Result<Stri
|
|||
return Err(());
|
||||
}
|
||||
};
|
||||
Ok((hostname, api_key, api_secret))
|
||||
}
|
||||
|
||||
pub fn create_token(user_id: i64, call_id: Uuid, has_admin: bool) -> Result<String, ()> {
|
||||
let (_, api_key, api_secret) = get_livekit()?;
|
||||
|
||||
let token = access_token::AccessToken::with_api_key(&api_key, &api_secret)
|
||||
.with_identity(&user_id.to_string())
|
||||
.with_grants(access_token::VideoGrants {
|
||||
can_update_own_metadata: true,
|
||||
room_join: true,
|
||||
room_admin: has_admin,
|
||||
room: call_id.to_string(),
|
||||
|
|
@ -42,33 +54,62 @@ pub fn create_token(user_id: i64, call_id: Uuid, has_admin: bool) -> Result<Stri
|
|||
Err(())
|
||||
}
|
||||
}
|
||||
pub async fn get_room(call_id: Uuid) -> Result<(RoomClient, Room), ()> {
|
||||
if let Ok((hostname, api_key, api_secret)) = get_livekit() {
|
||||
let room_service = RoomClient::with_api_key(&hostname, &api_key, &api_secret);
|
||||
let rooms = room_service.list_rooms(Vec::new()).await;
|
||||
if let Ok(rooms) = rooms {
|
||||
for room in rooms {
|
||||
if room.name == call_id.to_string() {
|
||||
return Ok((room_service, room));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Err(());
|
||||
}
|
||||
|
||||
pub async fn remove_participant(call_id: Uuid, user_id: i64) -> Result<(), ()> {
|
||||
if let Ok((hostname, api_key, api_secret)) = get_livekit() {
|
||||
let room_service = RoomClient::with_api_key(&hostname, &api_key, &api_secret);
|
||||
if let Ok(_) = room_service
|
||||
.remove_participant(&call_id.to_string(), &user_id.to_string())
|
||||
.await
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
return Err(());
|
||||
}
|
||||
|
||||
pub async fn get_room_metadata(call_id: Uuid) -> Result<String, ()> {
|
||||
if let Ok((_, room)) = get_room(call_id).await {
|
||||
Ok(room.metadata)
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_room_metadata(call_id: Uuid, metadata: String) -> Result<(), ()> {
|
||||
if let Ok((hostname, api_key, api_secret)) = get_livekit() {
|
||||
let room_service = RoomClient::with_api_key(&hostname, &api_key, &api_secret);
|
||||
if let Ok(_) = room_service
|
||||
.update_room_metadata(&call_id.to_string(), &metadata)
|
||||
.await
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
return 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;
|
||||
if let Ok((hostname, api_key, api_secret)) = get_livekit() {
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue