fix: use existing node if someone is already in the voice channel

This commit is contained in:
Zomatree
2025-03-25 20:37:06 +00:00
parent 8b5e085232
commit 091aa4705b
4 changed files with 26 additions and 9 deletions

View File

@@ -42,6 +42,10 @@ impl VoiceClient {
}
}
pub fn is_enabled(&self) -> bool {
!self.rooms.is_empty()
}
pub async fn from_revolt_config() -> Self {
let config = config().await;
@@ -89,8 +93,7 @@ impl VoiceClient {
Channel::TextChannel { voice: Some(voice), .. } => Some(Cow::Borrowed(voice)),
_ => None
}
.ok_or_else(|| create_error!(NotAVoiceChannel))?;
.ok_or_else(|| create_error!(NotAVoiceChannel))?;
room.client
.create_room(

View File

@@ -324,7 +324,7 @@ auto_derived!(
/// Join a voice channel
pub struct DataJoinCall {
pub node: String
pub node: Option<String>
}
);

View File

@@ -1,6 +1,6 @@
use revolt_config::config;
use revolt_models::v0;
use revolt_database::{util::{permissions::perms, reference::Reference}, voice::{raise_if_in_voice, VoiceClient}, Channel, Database, SystemMessage, User, AMQP};
use revolt_database::{util::{permissions::perms, reference::Reference}, voice::{get_channel_node, raise_if_in_voice, VoiceClient}, Channel, Database, SystemMessage, User, AMQP};
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_result::{create_error, Result};
@@ -12,10 +12,11 @@ use rocket::{serde::json::Json, State};
#[openapi(tag = "Voice")]
#[post("/<target>/join_call", data="<data>")]
pub async fn call(db: &State<Database>, amqp: &State<AMQP>, voice: &State<VoiceClient>, user: User, target: Reference, data: Json<v0::DataJoinCall>) -> Result<Json<v0::CreateVoiceUserResponse>> {
let config = config().await;
if !voice.is_enabled() {
return Err(create_error!(LiveKitUnavailable))
}
let node_host = config.hosts.livekit.get(&data.node)
.ok_or_else(|| create_error!(UnknownNode))?;
let config = config().await;
let channel = target.as_channel(db).await?;
@@ -26,8 +27,17 @@ pub async fn call(db: &State<Database>, amqp: &State<AMQP>, voice: &State<VoiceC
let current_permissions = calculate_channel_permissions(&mut permissions).await;
current_permissions.throw_if_lacking_channel_permission(ChannelPermission::Connect)?;
let token = voice.create_token(&data.node, &user, current_permissions, &channel)?;
let room = voice.create_room(&data.node, &channel).await?;
let existing_node = get_channel_node(channel.id()).await?;
let node = existing_node.or(data.into_inner().node)
.ok_or_else(|| create_error!(UnknownNode))?;
let node_host = config.hosts.livekit.get(&node)
.ok_or_else(|| create_error!(UnknownNode))?
.clone();
let token = voice.create_token(&node, &user, current_permissions, &channel)?;
let room = voice.create_room(&node, &channel).await?;
log::debug!("Created room {}", room.name);

View File

@@ -104,6 +104,10 @@ pub async fn edit(
}
let new_voice_channel = if let Some(new_channel) = &data.voice_channel {
if !voice_client.is_enabled() {
return Err(create_error!(LiveKitUnavailable))
};
permissions.throw_if_lacking_channel_permission(ChannelPermission::MoveMembers)?;
// ensure the channel we are moving them to is in the server and is a voice channel