feat: support multiple voice nodes

This commit is contained in:
Zomatree
2025-02-17 01:39:00 +00:00
parent 060b4c43f4
commit 367ac887f4
20 changed files with 209 additions and 83 deletions

View File

@@ -35,6 +35,24 @@ pub async fn raise_if_in_voice(user: &User, target: &str) -> Result<()> {
}
}
pub async fn set_channel_node(channel: &str, node: &str) -> Result<()> {
get_connection()
.await?
.set(format!("node:{channel}"), node)
.await
.to_internal_error()?;
Ok(())
}
pub async fn get_channel_node(channel: &str) -> Result<String> {
get_connection()
.await?
.get(format!("node:{channel}"))
.await
.to_internal_error()
}
pub async fn get_user_voice_channels(user_id: &str) -> Result<Vec<String>> {
get_connection()
.await?
@@ -207,10 +225,10 @@ pub async fn update_voice_state(
Ok(())
}
pub async fn get_voice_channel_members(channel_id: &str) -> Result<Vec<String>> {
pub async fn get_voice_channel_members(channel_id: &str) -> Result<Option<Vec<String>>> {
get_connection()
.await?
.smembers::<_, Vec<String>>(format!("vc_members:{}", channel_id))
.smembers(format!("vc_members:{}", channel_id))
.await
.to_internal_error()
}
@@ -255,8 +273,9 @@ pub async fn get_channel_voice_state(channel: &Channel) -> Result<Option<v0::Cha
_ => None
};
if !members.is_empty() {
if let Some(members) = members {
let mut participants = Vec::with_capacity(members.len());
let node = get_channel_node(channel.id()).await?;
for user_id in members {
if let Some(voice_state) = get_voice_state(channel.id(), server, &user_id).await? {
@@ -271,6 +290,7 @@ pub async fn get_channel_voice_state(channel: &Channel) -> Result<Option<v0::Cha
Ok(Some(v0::ChannelVoiceState {
id: channel.id().to_string(),
participants,
node
}))
} else {
Ok(None)
@@ -292,17 +312,18 @@ pub async fn move_user(user: &str, from: &str, to: &str) -> Result<()> {
}
pub async fn sync_voice_permissions(db: &Database, voice_client: &VoiceClient, channel: &Channel, server: Option<&Server>, role_id: Option<&str>) -> Result<()> {
let node = get_channel_node(channel.id()).await?;
for user_id in get_voice_channel_members(channel.id()).await? {
for user_id in get_voice_channel_members(channel.id()).await?.iter().flatten() {
let user = Reference::from_unchecked(user_id.clone()).as_user(db).await?;
sync_user_voice_permissions(db, voice_client, &user, channel, server, role_id).await?;
sync_user_voice_permissions(db, voice_client, &node, &user, channel, server, role_id).await?;
};
Ok(())
}
pub async fn sync_user_voice_permissions(db: &Database, voice_client: &VoiceClient, user: &User, channel: &Channel, server: Option<&Server>, role_id: Option<&str>) -> Result<()> {
pub async fn sync_user_voice_permissions(db: &Database, voice_client: &VoiceClient, node: &str, user: &User, channel: &Channel, server: Option<&Server>, role_id: Option<&str>) -> Result<()> {
let channel_id = channel.id();
let server_id: Option<&str> = server.as_ref().map(|s| s.id.as_str());
@@ -315,7 +336,8 @@ pub async fn sync_user_voice_permissions(db: &Database, voice_client: &VoiceClie
let voice_state = get_voice_state(channel_id, server_id, &user.id).await?.unwrap();
let mut query = DatabasePermissionQuery::new(db, user)
.channel(channel);
.channel(channel)
.user(user);
if let (Some(server), Some(member)) = (server, member.as_ref()) {
query = query.member(member).server(server)
@@ -340,7 +362,7 @@ pub async fn sync_user_voice_permissions(db: &Database, voice_client: &VoiceClie
update_voice_state(channel_id, server_id, &user.id, &update_event).await?;
voice_client.update_permissions(user, channel_id, ParticipantPermission {
voice_client.update_permissions(node, user, channel_id, ParticipantPermission {
can_subscribe: can_listen,
can_publish: can_speak,
can_publish_data: can_speak,

View File

@@ -1,9 +1,9 @@
use livekit_api::{
access_token::{AccessToken, VideoGrants},
services::room::{CreateRoomOptions, RoomClient, UpdateParticipantOptions},
services::room::{CreateRoomOptions, RoomClient as InnerRoomClient, UpdateParticipantOptions},
};
use livekit_protocol::{ParticipantInfo, ParticipantPermission, Room};
use revolt_config::config;
use revolt_config::{config, LiveKitNode};
use crate::models::{Channel, User};
use revolt_models::v0;
use revolt_permissions::{ChannelPermission, PermissionValue};
@@ -12,41 +12,59 @@ use std::{borrow::Cow, collections::HashMap, time::Duration};
use super::get_allowed_sources;
#[derive(Debug)]
pub struct RoomClient {
pub client: InnerRoomClient,
pub node: LiveKitNode
}
#[derive(Debug)]
pub struct VoiceClient {
rooms: RoomClient,
api_key: String,
api_secret: String,
pub rooms: HashMap<String, RoomClient>
}
impl VoiceClient {
pub fn new(url: String, api_key: String, api_secret: String) -> Self {
pub fn new(nodes: HashMap<String, LiveKitNode>) -> Self {
Self {
rooms: RoomClient::with_api_key(&url, &api_key, &api_secret),
api_key,
api_secret,
rooms: nodes
.into_iter()
.map(|(name, node)|
(
name,
RoomClient {
client: InnerRoomClient::with_api_key(&node.url, &node.key, &node.secret),
node
}
)
)
.collect()
}
}
pub async fn from_revolt_config() -> Self {
let config = config().await;
Self::new(
config.hosts.livekit,
config.api.livekit.key,
config.api.livekit.secret,
)
Self::new(config.api.livekit.nodes.clone())
}
pub fn get_node(&self, name: &str) -> Result<&RoomClient> {
self.rooms.get(name)
.ok_or_else(|| create_error!(UnknownNode))
}
pub fn create_token(
&self,
node: &str,
user: &User,
permissions: PermissionValue,
channel: &Channel,
) -> Result<String> {
let room = self.get_node(node)?;
let allowed_sources = get_allowed_sources(permissions);
AccessToken::with_api_key(&self.api_key, &self.api_secret)
AccessToken::with_api_key(&room.node.key, &room.node.secret)
.with_name(&format!("{}#{}", user.username, user.discriminator))
.with_identity(&user.id)
.with_metadata(&serde_json::to_string(&user).to_internal_error()?)
@@ -63,7 +81,9 @@ impl VoiceClient {
.to_internal_error()
}
pub async fn create_room(&self, channel: &Channel) -> Result<Room> {
pub async fn create_room(&self, node: &str, channel: &Channel) -> Result<Room> {
let room = self.get_node(node)?;
let voice = match channel {
Channel::DirectMessage { .. } | Channel::VoiceChannel { .. } => Some(Cow::Owned(v0::VoiceInformation::default())),
Channel::TextChannel { voice: Some(voice), .. } => Some(Cow::Borrowed(voice)),
@@ -72,7 +92,7 @@ impl VoiceClient {
.ok_or_else(|| create_error!(NotAVoiceChannel))?;
self.rooms
room.client
.create_room(
channel.id(),
CreateRoomOptions {
@@ -87,11 +107,14 @@ impl VoiceClient {
pub async fn update_permissions(
&self,
node: &str,
user: &User,
channel_id: &str,
new_permissions: ParticipantPermission,
) -> Result<ParticipantInfo> {
self.rooms
let room = self.get_node(node)?;
room.client
.update_participant(
channel_id,
&user.id,
@@ -106,8 +129,10 @@ impl VoiceClient {
.to_internal_error()
}
pub async fn remove_user(&self, user_id: &str, channel_id: &str) -> Result<()> {
self.rooms.remove_participant(channel_id, user_id)
pub async fn remove_user(&self, node: &str, user_id: &str, channel_id: &str) -> Result<()> {
let room = self.get_node(node)?;
room.client.remove_participant(channel_id, user_id)
.await
.to_internal_error()
}