Connection With Omega
This commit is contained in:
parent
4d8c07a7e4
commit
504e5ff015
20 changed files with 880 additions and 320 deletions
|
|
@ -19,12 +19,4 @@ impl CallGroup {
|
|||
show: RwLock::new(true),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add_member(self: Arc<Self>, member: i64, inviter: i64) {
|
||||
*self.show.write().await = true;
|
||||
self.members
|
||||
.write()
|
||||
.await
|
||||
.push(Arc::new(Caller::new(member, self.call_id, inviter)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,24 +6,12 @@ use uuid::Uuid;
|
|||
|
||||
use crate::{
|
||||
calls::{call_group::CallGroup, caller::Caller},
|
||||
util::print::{PrintType, line},
|
||||
log,
|
||||
util::logger::PrintType,
|
||||
};
|
||||
|
||||
static CALL_GROUPS: Lazy<RwLock<Vec<Arc<CallGroup>>>> = Lazy::new(|| RwLock::new(Vec::new()));
|
||||
|
||||
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;
|
||||
for member in members.iter() {
|
||||
if member.user_id == user_id {
|
||||
callers.push(member.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
callers
|
||||
}
|
||||
|
||||
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() {
|
||||
|
|
@ -45,38 +33,28 @@ pub async fn get_call_token(user_id: i64, call_id: Uuid) -> Option<String> {
|
|||
call_groups.iter().find(|g| g.call_id == call_id).cloned()
|
||||
};
|
||||
|
||||
// if the group exists
|
||||
if let Some(cg) = existing_group {
|
||||
let mut members = cg.members.write().await;
|
||||
let members = cg.members.write().await;
|
||||
|
||||
// if the user is already a member
|
||||
if let Some(member) = members.iter().find(|m| m.user_id == user_id) {
|
||||
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;
|
||||
|
||||
if let Some(cg) = call_groups.iter().find(|g| g.call_id == call_id) {
|
||||
let cg_clone = cg.clone();
|
||||
drop(call_groups);
|
||||
|
||||
let mut members = cg_clone.members.write().await;
|
||||
if let Some(member) = members.iter().find(|m| m.user_id == user_id) {
|
||||
return Some(member.create_token());
|
||||
}
|
||||
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 caller = Arc::new(Caller::new(user_id, call_id, user_id));
|
||||
let caller = Arc::new(Caller::new(user_id, call_id, true));
|
||||
let call_group = CallGroup::new(call_id, caller.clone());
|
||||
|
||||
call_groups.push(Arc::new(call_group));
|
||||
|
|
@ -99,7 +77,7 @@ pub async fn add_invite(call_id: Uuid, inviter_id: i64, invitee_id: i64) -> bool
|
|||
|
||||
if is_inviter_member {
|
||||
if !members.iter().any(|m| m.user_id == invitee_id) {
|
||||
members.push(Arc::new(Caller::new(invitee_id, call_id, inviter_id)));
|
||||
members.push(Arc::new(Caller::new(invitee_id, call_id, false)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
@ -138,13 +116,11 @@ pub async fn clean_calls() {
|
|||
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
|
||||
),
|
||||
log!(
|
||||
PrintType::Call,
|
||||
"Cleaned {} calls, {} remaining",
|
||||
size_pre - size_post,
|
||||
size_post
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,11 @@ use livekit_api::access_token;
|
|||
use std::env;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub fn create_token(user_id: i64, call_id: Uuid) -> Result<String, access_token::AccessTokenError> {
|
||||
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");
|
||||
|
||||
|
|
@ -11,9 +15,11 @@ pub fn create_token(user_id: i64, call_id: Uuid) -> Result<String, access_token:
|
|||
.with_grants(access_token::VideoGrants {
|
||||
can_update_own_metadata: true,
|
||||
room_join: true,
|
||||
room_admin: has_admin,
|
||||
room: call_id.to_string(),
|
||||
..Default::default()
|
||||
})
|
||||
.with_metadata(&format!("{{\"isAdmin\":{}}}", has_admin))
|
||||
.to_jwt();
|
||||
return token;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,19 +5,25 @@ use crate::calls::call_util;
|
|||
pub struct Caller {
|
||||
pub user_id: i64,
|
||||
pub call_id: Uuid,
|
||||
pub inviters: Vec<i64>,
|
||||
pub has_admin: bool,
|
||||
}
|
||||
|
||||
impl Caller {
|
||||
pub fn new(user_id: i64, call_id: Uuid, inviter_id: i64) -> Self {
|
||||
pub fn new(user_id: i64, call_id: Uuid, has_admin: bool) -> Self {
|
||||
Caller {
|
||||
user_id,
|
||||
call_id,
|
||||
inviters: vec![inviter_id],
|
||||
has_admin,
|
||||
}
|
||||
}
|
||||
pub fn set_admin(&mut self, has_admin: bool) {
|
||||
self.has_admin = has_admin;
|
||||
}
|
||||
pub fn has_admin(&self) -> bool {
|
||||
self.has_admin
|
||||
}
|
||||
pub fn create_token(&self) -> String {
|
||||
if let Ok(token) = call_util::create_token(self.user_id, self.call_id) {
|
||||
if let Ok(token) = call_util::create_token(self.user_id, self.call_id, self.has_admin()) {
|
||||
token
|
||||
} else {
|
||||
String::new()
|
||||
|
|
|
|||
Loading…
Reference in a new issue