[QOL] Call Utility
This commit is contained in:
parent
23b6f4ef4a
commit
b58729e919
5 changed files with 77 additions and 26 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -30,6 +30,7 @@ dependencies = [
|
||||||
"json",
|
"json",
|
||||||
"livekit",
|
"livekit",
|
||||||
"livekit-api",
|
"livekit-api",
|
||||||
|
"livekit-protocol",
|
||||||
"log",
|
"log",
|
||||||
"loom",
|
"loom",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
|
|
||||||
|
|
@ -88,3 +88,4 @@ strum_macros = "0.27.2"
|
||||||
livekit = "0.7.25"
|
livekit = "0.7.25"
|
||||||
livekit-api = { version = "0.4.10", features = ["native-tls"] }
|
livekit-api = { version = "0.4.10", features = ["native-tls"] }
|
||||||
hyper = { version = "1.8.1", features = ["http1", "http2"] }
|
hyper = { version = "1.8.1", features = ["http1", "http2"] }
|
||||||
|
livekit-protocol = "0.6.0"
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,12 @@ impl CallGroup {
|
||||||
pub async fn set_anonymous_joining(&self, enable: bool) {
|
pub async fn set_anonymous_joining(&self, enable: bool) {
|
||||||
*self.anonymous_joining.write().await = enable;
|
*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() {
|
if self.short_link.read().await.is_none() {
|
||||||
let long_link = format!(
|
let long_link = format!(
|
||||||
"https://app.tensamin.net/call/anonymous?call_id={}&omikron_id={}",
|
"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) {
|
pub async fn remove_caller(&self, user_id: i64) {
|
||||||
|
let _ = call_util::remove_participant(self.call_id, user_id).await;
|
||||||
self.members
|
self.members
|
||||||
.write()
|
.write()
|
||||||
.await
|
.await
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use livekit_api::{
|
||||||
access_token::{self},
|
access_token::{self},
|
||||||
services::room::RoomClient,
|
services::room::RoomClient,
|
||||||
};
|
};
|
||||||
|
use livekit_protocol::Room;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
@ -9,7 +10,14 @@ use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{calls::call_manager::CALL_GROUPS, log, log_err, util::logger::PrintType};
|
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") {
|
let api_key = match env::var("LIVEKIT_API_KEY") {
|
||||||
Ok(key) => key,
|
Ok(key) => key,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
|
@ -24,11 +32,15 @@ pub fn create_token(user_id: i64, call_id: Uuid, has_admin: bool) -> Result<Stri
|
||||||
return Err(());
|
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)
|
let token = access_token::AccessToken::with_api_key(&api_key, &api_secret)
|
||||||
.with_identity(&user_id.to_string())
|
.with_identity(&user_id.to_string())
|
||||||
.with_grants(access_token::VideoGrants {
|
.with_grants(access_token::VideoGrants {
|
||||||
can_update_own_metadata: true,
|
|
||||||
room_join: true,
|
room_join: true,
|
||||||
room_admin: has_admin,
|
room_admin: has_admin,
|
||||||
room: call_id.to_string(),
|
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(())
|
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() {
|
pub fn garbage_collect_calls() {
|
||||||
tokio::spawn(async move {
|
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 {
|
loop {
|
||||||
let room_service = RoomClient::with_api_key(&hostname, &api_key, &api_secret);
|
if let Ok((hostname, api_key, api_secret)) = get_livekit() {
|
||||||
clean_calls(room_service).await;
|
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;
|
tokio::time::sleep(Duration::from_secs(2)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ use uuid::Uuid;
|
||||||
|
|
||||||
use super::{rho_connection::RhoConnection, rho_manager};
|
use super::{rho_connection::RhoConnection, rho_manager};
|
||||||
use crate::anonymous_clients::anonymous_manager;
|
use crate::anonymous_clients::anonymous_manager;
|
||||||
use crate::calls::call_manager;
|
use crate::calls::{call_manager, call_util};
|
||||||
use crate::omega::omega_connection::{WAITING_TASKS, get_omega_connection};
|
use crate::omega::omega_connection::{WAITING_TASKS, get_omega_connection};
|
||||||
use crate::util::crypto_helper::{load_public_key, public_key_to_base64};
|
use crate::util::crypto_helper::{load_public_key, public_key_to_base64};
|
||||||
use crate::util::crypto_util::{DataFormat, SecurePayload};
|
use crate::util::crypto_util::{DataFormat, SecurePayload};
|
||||||
|
|
@ -532,6 +532,7 @@ impl ClientConnection {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.has_admin()
|
.has_admin()
|
||||||
{
|
{
|
||||||
|
let _ = call_util::remove_participant(call_id, user_id).await;
|
||||||
call.get_caller(user_id)
|
call.get_caller(user_id)
|
||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue