diff --git a/crates/core/database/src/voice/mod.rs b/crates/core/database/src/voice/mod.rs index dde70a71..1705bece 100644 --- a/crates/core/database/src/voice/mod.rs +++ b/crates/core/database/src/voice/mod.rs @@ -16,7 +16,9 @@ mod voice_client; pub use voice_client::VoiceClient; async fn get_connection() -> Result { - _get_connection().await.map_err(|_| create_error!(InternalError)) + _get_connection() + .await + .map_err(|_| create_error!(InternalError)) } pub async fn raise_if_in_voice(user: &User, channel_id: &str) -> Result<()> { @@ -59,6 +61,14 @@ pub async fn get_channel_node(channel: &str) -> Result> { .to_internal_error() } +pub async fn delete_channel_node(channel: &str) -> Result<()> { + get_connection() + .await? + .del(format!("node:{channel}")) + .await + .to_internal_error() +} + pub async fn get_user_voice_channels(user_id: &str) -> Result> { get_connection() .await? @@ -226,6 +236,7 @@ pub async fn delete_channel_voice_state( let mut pipeline = Pipeline::new(); pipeline.del(format!("vc_members:{channel_id}")); + pipeline.del(format!("node:{channel_id}")); for user_id in user_ids { let unique_key = format!("{user_id}:{parent_id}"); @@ -567,15 +578,24 @@ pub async fn get_call_notification_recipients( .to_internal_error() } -pub async fn remove_user_from_voice_channels(db: &Database, voice_client: &VoiceClient, user_id: &str) -> Result<()> { +pub async fn remove_user_from_voice_channels( + db: &Database, + voice_client: &VoiceClient, + user_id: &str, +) -> Result<()> { for channel_id in get_user_voice_channels(user_id).await? { remove_user_from_voice_channel(db, voice_client, &channel_id, user_id).await?; - }; + } Ok(()) } -pub async fn remove_user_from_voice_channel(db: &Database, voice_client: &VoiceClient, channel_id: &str, user_id: &str) -> Result<()> { +pub async fn remove_user_from_voice_channel( + db: &Database, + voice_client: &VoiceClient, + channel_id: &str, + user_id: &str, +) -> Result<()> { if let Some(node) = get_channel_node(channel_id).await? { let _ = voice_client.remove_user(&node, user_id, channel_id).await; } @@ -587,7 +607,11 @@ pub async fn remove_user_from_voice_channel(db: &Database, voice_client: &VoiceC Ok(()) } -pub async fn delete_voice_channel(voice_client: &VoiceClient, channel_id: &str, server_id: Option<&str>) -> Result<()> { +pub async fn delete_voice_channel( + voice_client: &VoiceClient, + channel_id: &str, + server_id: Option<&str>, +) -> Result<()> { if let Some(users) = get_voice_channel_members(channel_id).await? { let node = get_channel_node(channel_id).await?.unwrap(); @@ -597,4 +621,4 @@ pub async fn delete_voice_channel(voice_client: &VoiceClient, channel_id: &str, }; Ok(()) -} \ No newline at end of file +} diff --git a/crates/daemons/voice-ingress/src/api.rs b/crates/daemons/voice-ingress/src/api.rs index 2af3ab26..56e5b2f7 100644 --- a/crates/daemons/voice-ingress/src/api.rs +++ b/crates/daemons/voice-ingress/src/api.rs @@ -5,9 +5,8 @@ use revolt_database::{ iso8601_timestamp::{Duration, Timestamp}, util::reference::Reference, voice::{ - create_voice_state, delete_voice_state, - get_user_moved_from_voice, get_user_moved_to_voice, - update_voice_state_tracks, VoiceClient, + create_voice_state, delete_channel_node, delete_voice_state, get_user_moved_from_voice, + get_user_moved_to_voice, get_voice_channel_members, update_voice_state_tracks, VoiceClient, }, Database, AMQP, }; @@ -160,6 +159,8 @@ pub async fn ingress( // let members = get_voice_channel_members(channel_id).await?; // if members.is_none_or(|m| m.is_empty()) { + // delete_channel_node(channel_id).await?; + // // // The channel is empty so send out an "end" message for ringing // if let Err(e) = amqp // .dm_call_updated(user_id, channel_id, None, true, None) diff --git a/crates/delta/src/routes/channels/voice_join.rs b/crates/delta/src/routes/channels/voice_join.rs index 670c5032..fe585f6f 100644 --- a/crates/delta/src/routes/channels/voice_join.rs +++ b/crates/delta/src/routes/channels/voice_join.rs @@ -3,7 +3,7 @@ use revolt_database::{ util::{permissions::perms, reference::Reference}, voice::{ delete_voice_state, get_channel_node, get_user_voice_channels, get_voice_channel_members, - raise_if_in_voice, set_call_notification_recipients, VoiceClient, + raise_if_in_voice, set_call_notification_recipients, set_channel_node, VoiceClient, }, Database, User, }; @@ -60,6 +60,7 @@ pub async fn call( } let existing_node = get_channel_node(channel.id()).await?; + let has_existing_node = existing_node.is_some(); // we move existing_node in the next statement so this is the quickest way to know if we need to set it. let node = existing_node .or(node) @@ -86,9 +87,11 @@ pub async fn call( let channel = Reference::from_unchecked(&channel_id) .as_channel(db) - .await?; + .await; - delete_voice_state(&channel_id, channel.server(), &user.id).await?; + if channel.is_ok() { + delete_voice_state(&channel_id, channel.unwrap().server(), &user.id).await?; + } } } else { raise_if_in_voice(&user, channel.id()).await?; @@ -100,6 +103,10 @@ pub async fn call( let room = voice_client.create_room(&node, &channel).await?; + if !has_existing_node { + set_channel_node(channel.id(), &node).await?; + } + log::debug!("Created room {}", room.name); if let Some(recipients) = recipients {