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

@@ -20,10 +20,14 @@ api = "http://local.revolt.chat:14702"
events = "ws://local.revolt.chat:14703" events = "ws://local.revolt.chat:14703"
autumn = "http://local.revolt.chat:14704" autumn = "http://local.revolt.chat:14704"
january = "http://local.revolt.chat:14705" january = "http://local.revolt.chat:14705"
livekit = "ws://local.revolt.chat:14706"
voso_legacy = "" voso_legacy = ""
voso_legacy_ws = "" voso_legacy_ws = ""
# Public urls for livekit nodes
# each entry here should have a corresponding entry under `api.livekit.nodes`
[hosts.livekit]
worldwide = "ws://local.revolt.chat:14706"
[api] [api]
[api.smtp] [api.smtp]
@@ -38,9 +42,16 @@ port = 14025
use_tls = false use_tls = false
[api.livekit] [api.livekit]
url = "ws://local.revolt.chat:14706"
key = "" # Config for livekit nodes
secret = "" # Make sure to change the secret when deploying
# The key and secret should match the values livekit is using
[api.livekit.nodes.worldwide]
url = "http://livekit"
lat = 0.0
lon = 0.0
key = "worldwide_key"
secret = "ZjCofRlfm6GGtjlifmNpCDkcQbEIIVC0"
[files.s3] [files.s3]
# S3 protocol endpoint # S3 protocol endpoint

View File

@@ -17,10 +17,11 @@ api = "http://local.revolt.chat/api"
events = "ws://local.revolt.chat/ws" events = "ws://local.revolt.chat/ws"
autumn = "http://local.revolt.chat/autumn" autumn = "http://local.revolt.chat/autumn"
january = "http://local.revolt.chat/january" january = "http://local.revolt.chat/january"
livekit = "ws://local.revolt.chat/livekit"
voso_legacy = "" voso_legacy = ""
voso_legacy_ws = "" voso_legacy_ws = ""
[hosts.livekit]
[rabbit] [rabbit]
host = "rabbit" host = "rabbit"
port = 5672 port = 5672
@@ -64,13 +65,8 @@ hcaptcha_sitekey = ""
max_concurrent_connections = 50 max_concurrent_connections = 50
[api.livekit] [api.livekit]
# Livekit server url
url = "ws://local.revolt.chat/livekit"
# Livekit security key name
key = ""
# Livekit security secret value
secret = ""
[api.livekit.nodes]
[pushd] [pushd]
# this changes the names of the queues to not overlap # this changes the names of the queues to not overlap

View File

@@ -105,7 +105,7 @@ pub struct Hosts {
pub events: String, pub events: String,
pub autumn: String, pub autumn: String,
pub january: String, pub january: String,
pub livekit: String pub livekit: HashMap<String, String>
} }
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
@@ -176,7 +176,14 @@ pub struct ApiWorkers {
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
pub struct ApiLiveKit { pub struct ApiLiveKit {
pub nodes: HashMap<String, LiveKitNode>
}
#[derive(Deserialize, Debug, Clone)]
pub struct LiveKitNode {
pub url: String, pub url: String,
pub lat: f64,
pub lon: f64,
pub key: String, pub key: String,
pub secret: String pub secret: String
} }

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>> { pub async fn get_user_voice_channels(user_id: &str) -> Result<Vec<String>> {
get_connection() get_connection()
.await? .await?
@@ -207,10 +225,10 @@ pub async fn update_voice_state(
Ok(()) 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() get_connection()
.await? .await?
.smembers::<_, Vec<String>>(format!("vc_members:{}", channel_id)) .smembers(format!("vc_members:{}", channel_id))
.await .await
.to_internal_error() .to_internal_error()
} }
@@ -255,8 +273,9 @@ pub async fn get_channel_voice_state(channel: &Channel) -> Result<Option<v0::Cha
_ => None _ => None
}; };
if !members.is_empty() { if let Some(members) = members {
let mut participants = Vec::with_capacity(members.len()); let mut participants = Vec::with_capacity(members.len());
let node = get_channel_node(channel.id()).await?;
for user_id in members { for user_id in members {
if let Some(voice_state) = get_voice_state(channel.id(), server, &user_id).await? { 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 { Ok(Some(v0::ChannelVoiceState {
id: channel.id().to_string(), id: channel.id().to_string(),
participants, participants,
node
})) }))
} else { } else {
Ok(None) 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<()> { 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?; 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(()) 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 channel_id = channel.id();
let server_id: Option<&str> = server.as_ref().map(|s| s.id.as_str()); 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 voice_state = get_voice_state(channel_id, server_id, &user.id).await?.unwrap();
let mut query = DatabasePermissionQuery::new(db, user) let mut query = DatabasePermissionQuery::new(db, user)
.channel(channel); .channel(channel)
.user(user);
if let (Some(server), Some(member)) = (server, member.as_ref()) { if let (Some(server), Some(member)) = (server, member.as_ref()) {
query = query.member(member).server(server) 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?; 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_subscribe: can_listen,
can_publish: can_speak, can_publish: can_speak,
can_publish_data: can_speak, can_publish_data: can_speak,

View File

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

View File

@@ -315,7 +315,14 @@ auto_derived!(
pub struct ChannelVoiceState { pub struct ChannelVoiceState {
pub id: String, pub id: String,
/// The states of the users who are connected to the channel /// The states of the users who are connected to the channel
pub participants: Vec<UserVoiceState> pub participants: Vec<UserVoiceState>,
/// The node's node which the channel is currently using
pub node: String
}
/// Join a voice channel
pub struct DataJoinCall {
pub node: String
} }
); );

View File

@@ -128,6 +128,7 @@ pub static DEFAULT_PERMISSION: Lazy<u64> = Lazy::new(|| {
+ ChannelPermission::Connect + ChannelPermission::Connect
+ ChannelPermission::Speak + ChannelPermission::Speak
+ ChannelPermission::Listen + ChannelPermission::Listen
+ ChannelPermission::Video
) )
}); });

View File

@@ -78,6 +78,7 @@ impl IntoResponse for Error {
ErrorType::AlreadyInVoiceChannel => StatusCode::BAD_REQUEST, ErrorType::AlreadyInVoiceChannel => StatusCode::BAD_REQUEST,
ErrorType::NotAVoiceChannel => StatusCode::BAD_REQUEST, ErrorType::NotAVoiceChannel => StatusCode::BAD_REQUEST,
ErrorType::AlreadyConnected => StatusCode::BAD_REQUEST, ErrorType::AlreadyConnected => StatusCode::BAD_REQUEST,
ErrorType::UnknownNode => StatusCode::BAD_REQUEST,
ErrorType::ProxyError => StatusCode::BAD_REQUEST, ErrorType::ProxyError => StatusCode::BAD_REQUEST,
ErrorType::FileTooSmall => StatusCode::UNPROCESSABLE_ENTITY, ErrorType::FileTooSmall => StatusCode::UNPROCESSABLE_ENTITY,
ErrorType::FileTooLarge { .. } => StatusCode::UNPROCESSABLE_ENTITY, ErrorType::FileTooLarge { .. } => StatusCode::UNPROCESSABLE_ENTITY,

View File

@@ -161,6 +161,7 @@ pub enum ErrorType {
AlreadyInVoiceChannel, AlreadyInVoiceChannel,
NotAVoiceChannel, NotAVoiceChannel,
AlreadyConnected, AlreadyConnected,
UnknownNode,
// ? Micro-service errors // ? Micro-service errors
ProxyError, ProxyError,
FileTooSmall, FileTooSmall,

View File

@@ -82,6 +82,7 @@ impl<'r> Responder<'r, 'static> for Error {
ErrorType::AlreadyInVoiceChannel => Status::BadRequest, ErrorType::AlreadyInVoiceChannel => Status::BadRequest,
ErrorType::NotAVoiceChannel => Status::BadRequest, ErrorType::NotAVoiceChannel => Status::BadRequest,
ErrorType::AlreadyConnected => Status::BadRequest, ErrorType::AlreadyConnected => Status::BadRequest,
ErrorType::UnknownNode => Status::BadRequest,
ErrorType::ProxyError => Status::BadRequest, ErrorType::ProxyError => Status::BadRequest,
ErrorType::FileTooSmall => Status::UnprocessableEntity, ErrorType::FileTooSmall => Status::UnprocessableEntity,
ErrorType::FileTooLarge { .. } => Status::UnprocessableEntity, ErrorType::FileTooLarge { .. } => Status::UnprocessableEntity,

View File

@@ -1,15 +1,16 @@
use std::env; use std::env;
use livekit_protocol::{TrackType, WebhookEvent}; use livekit_protocol::TrackType;
use livekit_api::{access_token::TokenVerifier, webhooks::WebhookReceiver};
use revolt_database::{ use revolt_database::{
events::client::EventV1, util::reference::Reference, Database, DatabaseInfo, events::client::EventV1, util::reference::Reference, Database, DatabaseInfo,
voice::{create_voice_state, delete_voice_state, update_voice_state_tracks, VoiceClient} voice::{create_voice_state, delete_voice_state, update_voice_state_tracks, VoiceClient}
}; };
use rocket::{build, post, routes, serde::json::Json, Config, State}; use rocket::{build, http::Status, post, request::{FromRequest, Outcome}, routes, Config, Request, State};
use rocket_empty::EmptyResponse; use rocket_empty::EmptyResponse;
use std::net::Ipv4Addr;
use revolt_result::{Result, ToRevoltError}; use revolt_result::{create_error, Error, Result, ToRevoltError};
#[rocket::main] #[rocket::main]
async fn main() -> Result<(), rocket::Error> { async fn main() -> Result<(), rocket::Error> {
@@ -17,13 +18,13 @@ async fn main() -> Result<(), rocket::Error> {
let database = DatabaseInfo::Auto.connect().await.unwrap(); let database = DatabaseInfo::Auto.connect().await.unwrap();
let voice_client = VoiceClient::from_revolt_config().await; let voice_client = VoiceClient::from_revolt_config().await;
let _rocket = build() let _rocket = build()
.manage(database) .manage(database)
.manage(voice_client) .manage(voice_client)
.mount("/", routes![ingress]) .mount("/", routes![ingress])
.configure(Config { .configure(Config {
port: 8500, port: 8500,
address: Ipv4Addr::new(0, 0, 0, 0).into(),
..Default::default() ..Default::default()
}) })
.ignite() .ignite()
@@ -34,14 +35,44 @@ async fn main() -> Result<(), rocket::Error> {
Ok(()) Ok(())
} }
#[post("/", data = "<body>")] struct AuthHeader<'a>(&'a str);
async fn ingress(db: &State<Database>, voice_client: &State<VoiceClient>, body: Json<WebhookEvent>) -> Result<EmptyResponse> {
#[rocket::async_trait]
impl<'r> FromRequest<'r> for AuthHeader<'r> {
type Error = Error;
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
match request.headers().get("Authorization").next() {
Some(token) => Outcome::Success(Self(token)),
None => Outcome::Error((Status::Unauthorized, create_error!(NotAuthenticated)))
}
}
}
impl std::ops::Deref for AuthHeader<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0
}
}
#[post("/<node>", data = "<body>")]
async fn ingress(db: &State<Database>, node: &str, voice_client: &State<VoiceClient>, auth_header: AuthHeader<'_>, body: &str) -> Result<EmptyResponse> {
log::debug!("received event: {body:?}"); log::debug!("received event: {body:?}");
let channel_id = body.room.as_ref().map(|r| &r.name); let config = revolt_config::config().await;
let user_id = body.participant.as_ref().map(|r| &r.identity);
match body.event.as_str() { let node_info = config.api.livekit.nodes.get(node)
.ok_or_else(|| create_error!(NotAuthenticated))?;
let webhook_receiver = WebhookReceiver::new(TokenVerifier::with_api_key(&node_info.key, &node_info.secret));
let event = webhook_receiver.receive(body, &auth_header).to_internal_error()?;
let channel_id = event.room.as_ref().map(|r| &r.name);
let user_id = event.participant.as_ref().map(|r| &r.identity);
match event.event.as_str() {
"participant_joined" => { "participant_joined" => {
let channel_id = channel_id.to_internal_error()?; let channel_id = channel_id.to_internal_error()?;
let user_id = user_id.to_internal_error()?; let user_id = user_id.to_internal_error()?;
@@ -79,20 +110,22 @@ async fn ingress(db: &State<Database>, voice_client: &State<VoiceClient>, body:
"track_published" | "track_unpublished" => { "track_published" | "track_unpublished" => {
let channel_id = channel_id.to_internal_error()?; let channel_id = channel_id.to_internal_error()?;
let user_id = user_id.to_internal_error()?; let user_id = user_id.to_internal_error()?;
let track = body.track.as_ref().to_internal_error()?; let track = event.track.as_ref().to_internal_error()?;
let channel = Reference::from_unchecked(channel_id.clone()) let channel = Reference::from_unchecked(channel_id.clone())
.as_channel(db) .as_channel(db)
.await?; .await?;
// remove the user if they try publish a video larger than 1080x720 or they publish data // remove the user if they try publish a video larger than 1080x720 or they publish data
if body.event == "track_published" // TODO: move to config
if event.event == "track_published"
&& ( && (
(track.r#type == TrackType::Video as i32 && (track.width > 1080 || track.height > 720)) // handle any size which goes over the limit of "1080x720" to stop people from making too tall or too wide and bypassing the limit
(track.r#type == TrackType::Video as i32 && (track.width * track.height) >= (1080 * 720))
| (track.r#type == TrackType::Data as i32) | (track.r#type == TrackType::Data as i32)
) )
{ {
voice_client.remove_user(user_id, channel_id).await?; voice_client.remove_user(node, user_id, channel_id).await?;
delete_voice_state(channel_id, channel.server(), user_id).await?; delete_voice_state(channel_id, channel.server(), user_id).await?;
} }
@@ -100,7 +133,7 @@ async fn ingress(db: &State<Database>, voice_client: &State<VoiceClient>, body:
channel_id, channel_id,
channel.server(), channel.server(),
user_id, user_id,
body.event == "track_published", // to avoid duplicating this entire case twice event.event == "track_published", // to avoid duplicating this entire case twice
track.source track.source
).await?; ).await?;

View File

@@ -84,7 +84,7 @@ pub async fn web() -> Rocket<Build> {
.into(); .into();
// Voice handler // Voice handler
let voice_client = VoiceClient::new(config.api.livekit.url.clone(), config.api.livekit.key.clone(), config.api.livekit.secret.clone()); let voice_client = VoiceClient::new(config.api.livekit.nodes.clone());
// Configure Rabbit // Configure Rabbit
let connection = Connection::open(&OpenConnectionArguments::new( let connection = Connection::open(&OpenConnectionArguments::new(
&config.rabbit.host, &config.rabbit.host,

View File

@@ -1,5 +1,5 @@
use revolt_database::{ use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference}, voice::{delete_voice_state, get_voice_channel_members, VoiceClient}, Channel, Database, PartialChannel, User, AMQP util::{permissions::DatabasePermissionQuery, reference::Reference}, voice::{delete_voice_state, get_channel_node, get_voice_channel_members, VoiceClient}, Channel, Database, PartialChannel, User, AMQP
}; };
use revolt_models::v0; use revolt_models::v0;
use revolt_permissions::{calculate_channel_permissions, ChannelPermission}; use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
@@ -53,9 +53,13 @@ pub async fn delete(
} }
}; };
for user_id in get_voice_channel_members(channel.id()).await? { if let Some(users) = get_voice_channel_members(channel.id()).await? {
voice_client.remove_user(&user_id, channel.id()).await?; let node = get_channel_node(channel.id()).await?;
delete_voice_state(channel.id(), channel.server(), &user.id).await?;
for user in users {
voice_client.remove_user(&node, &user, channel.id()).await?;
delete_voice_state(channel.id(), channel.server(), &user).await?;
}
}; };
Ok(EmptyResponse) Ok(EmptyResponse)

View File

@@ -1,4 +1,4 @@
use revolt_database::{util::reference::Reference, voice::{delete_voice_state, is_in_voice_channel, raise_if_in_voice, VoiceClient}, Channel, Database, User, AMQP}; use revolt_database::{util::reference::Reference, voice::{delete_voice_state, get_channel_node, is_in_voice_channel, raise_if_in_voice, VoiceClient}, Channel, Database, User, AMQP};
use revolt_permissions::ChannelPermission; use revolt_permissions::ChannelPermission;
use revolt_result::{create_error, Result}; use revolt_result::{create_error, Result};
@@ -48,7 +48,9 @@ pub async fn remove_member(
}; };
if is_in_voice_channel(&user.id, channel.id()).await? { if is_in_voice_channel(&user.id, channel.id()).await? {
voice_client.remove_user(&user.id, channel.id()).await?; let node = get_channel_node(channel.id()).await?;
voice_client.remove_user(&node, &user.id, channel.id()).await?;
delete_voice_state(channel.id(), None, &user.id).await?; delete_voice_state(channel.id(), None, &user.id).await?;
}; };

View File

@@ -1,7 +1,7 @@
use revolt_models::v0; 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::{raise_if_in_voice, VoiceClient}, Channel, Database, SystemMessage, User, AMQP};
use revolt_permissions::{calculate_channel_permissions, ChannelPermission}; use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_result::Result; use revolt_result::{create_error, Result};
use rocket::{serde::json::Json, State}; use rocket::{serde::json::Json, State};
@@ -9,8 +9,12 @@ use rocket::{serde::json::Json, State};
/// ///
/// Asks the voice server for a token to join the call. /// Asks the voice server for a token to join the call.
#[openapi(tag = "Voice")] #[openapi(tag = "Voice")]
#[post("/<target>/join_call")] #[post("/<target>/join_call", data="<data>")]
pub async fn call(db: &State<Database>, amqp: &State<AMQP>, voice: &State<VoiceClient>, user: User, target: Reference) -> Result<Json<v0::CreateVoiceUserResponse>> { 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>> {
if !voice.rooms.contains_key(&data.node) {
return Err(create_error!(UnknownNode))
}
let channel = target.as_channel(db).await?; let channel = target.as_channel(db).await?;
raise_if_in_voice(&user, channel.id()).await?; raise_if_in_voice(&user, channel.id()).await?;
@@ -20,8 +24,8 @@ pub async fn call(db: &State<Database>, amqp: &State<AMQP>, voice: &State<VoiceC
let current_permissions = calculate_channel_permissions(&mut permissions).await; let current_permissions = calculate_channel_permissions(&mut permissions).await;
current_permissions.throw_if_lacking_channel_permission(ChannelPermission::Connect)?; current_permissions.throw_if_lacking_channel_permission(ChannelPermission::Connect)?;
let token = voice.create_token(&user, current_permissions, &channel)?; let token = voice.create_token(&data.node, &user, current_permissions, &channel)?;
let room = voice.create_room(&channel).await?; let room = voice.create_room(&data.node, &channel).await?;
log::debug!("Created room {}", room.name); log::debug!("Created room {}", room.name);

View File

@@ -25,9 +25,7 @@ pub struct Feature {
#[derive(Serialize, JsonSchema, Debug)] #[derive(Serialize, JsonSchema, Debug)]
pub struct VoiceFeature { pub struct VoiceFeature {
/// Whether voice is enabled /// Whether voice is enabled
pub enabled: bool, pub enabled: bool
/// URL pointing to the voice API
pub url: String
} }
/// # Feature Configuration /// # Feature Configuration
@@ -105,8 +103,7 @@ pub async fn root() -> Result<Json<RevoltConfig>> {
url: config.hosts.january, url: config.hosts.january,
}, },
livekit: VoiceFeature { livekit: VoiceFeature {
enabled: !config.hosts.livekit.is_empty(), enabled: !config.hosts.livekit.is_empty()
url: config.hosts.livekit.to_string(),
}, },
}, },
ws: config.hosts.events, ws: config.hosts.events,

View File

@@ -1,6 +1,6 @@
use revolt_database::{ use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference}, util::{permissions::DatabasePermissionQuery, reference::Reference},
voice::{delete_voice_state, get_user_voice_channel_in_server, VoiceClient}, voice::{delete_voice_state, get_channel_node, get_user_voice_channel_in_server, VoiceClient},
Database, RemovalIntention, ServerBan, User, Database, RemovalIntention, ServerBan, User,
}; };
use revolt_models::v0; use revolt_models::v0;
@@ -61,7 +61,8 @@ pub async fn ban(
// If the member is in a voice channel while banned kick them from the voice channel // If the member is in a voice channel while banned kick them from the voice channel
if let Some(channel_id) = get_user_voice_channel_in_server(&user.id, &server.id).await? { if let Some(channel_id) = get_user_voice_channel_in_server(&user.id, &server.id).await? {
voice_client.remove_user(&user.id, &channel_id).await?; let node = get_channel_node(&channel_id).await?;
voice_client.remove_user(&node, &user.id, &channel_id).await?;
delete_voice_state(&channel_id, Some(&server.id), &user.id).await? delete_voice_state(&channel_id, Some(&server.id), &user.id).await?
} }
} }

View File

@@ -7,7 +7,7 @@ use redis_kiss::{get_connection, redis::Pipeline, AsyncCommands};
use revolt_database::{ use revolt_database::{
events::client::EventV1, events::client::EventV1,
util::{permissions::DatabasePermissionQuery, reference::Reference}, util::{permissions::DatabasePermissionQuery, reference::Reference},
voice::{get_user_voice_channel_in_server, move_user, sync_user_voice_permissions, VoiceClient}, voice::{get_channel_node, get_user_voice_channel_in_server, move_user, sync_user_voice_permissions, VoiceClient},
Database, File, PartialMember, User, Database, File, PartialMember, User,
}; };
use revolt_models::v0::{self, FieldsMember, PartialUserVoiceState}; use revolt_models::v0::{self, FieldsMember, PartialUserVoiceState};
@@ -185,6 +185,8 @@ pub async fn edit(
partial.avatar = Some(File::use_user_avatar(db, &avatar, &user.id, &user.id).await?); partial.avatar = Some(File::use_user_avatar(db, &avatar, &user.id, &user.id).await?);
} }
// TODO: impelement moving users
let remove_contains_voice = remove let remove_contains_voice = remove
.as_ref() .as_ref()
.map(|r| r.contains(FieldsMember::CanPublish) || r.contains(FieldsMember::CanReceive)) .map(|r| r.contains(FieldsMember::CanPublish) || r.contains(FieldsMember::CanReceive))
@@ -196,9 +198,11 @@ pub async fn edit(
|| remove_contains_voice || remove_contains_voice
{ {
if let Some(channel) = get_user_voice_channel_in_server(&target_user.id, &server.id).await? { if let Some(channel) = get_user_voice_channel_in_server(&target_user.id, &server.id).await? {
let node = get_channel_node(&channel).await?;
let channel = Reference::from_unchecked(channel).as_channel(db).await?; let channel = Reference::from_unchecked(channel).as_channel(db).await?;
sync_user_voice_permissions(db, voice_client, &user, &channel, Some(&server), None).await?;
sync_user_voice_permissions(db, voice_client, &node, &user, &channel, Some(&server), None).await?;
}; };
}; };

View File

@@ -1,6 +1,6 @@
use revolt_database::{ use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference}, util::{permissions::DatabasePermissionQuery, reference::Reference},
voice::{delete_voice_state, get_user_voice_channel_in_server, VoiceClient}, voice::{delete_voice_state, get_channel_node, get_user_voice_channel_in_server, VoiceClient},
Database, RemovalIntention, User, Database, RemovalIntention, User,
}; };
use revolt_permissions::{calculate_server_permissions, ChannelPermission}; use revolt_permissions::{calculate_server_permissions, ChannelPermission};
@@ -43,7 +43,9 @@ pub async fn kick(
} }
if let Some(channel_id) = get_user_voice_channel_in_server(&user.id, &server.id).await? { if let Some(channel_id) = get_user_voice_channel_in_server(&user.id, &server.id).await? {
voice_client.remove_user(&user.id, &channel_id).await?; let node = get_channel_node(&channel_id).await?;
voice_client.remove_user(&node, &user.id, &channel_id).await?;
delete_voice_state(&channel_id, Some(&server.id), &user.id).await?; delete_voice_state(&channel_id, Some(&server.id), &user.id).await?;
} }

View File

@@ -1,6 +1,6 @@
use revolt_database::{ use revolt_database::{
util::reference::Reference, util::reference::Reference,
voice::{delete_voice_state, get_user_voice_channel_in_server, VoiceClient}, voice::{delete_voice_state, get_channel_node, get_user_voice_channel_in_server, get_voice_channel_members, VoiceClient},
Database, RemovalIntention, User, Database, RemovalIntention, User,
}; };
use revolt_models::v0; use revolt_models::v0;
@@ -26,15 +26,22 @@ pub async fn delete(
if server.owner == user.id { if server.owner == user.id {
for channel_id in &server.channels { for channel_id in &server.channels {
voice_client.remove_user(&user.id, channel_id).await?; if let Some(users) = get_voice_channel_members(channel_id).await? {
delete_voice_state(channel_id, Some(&server.id), &user.id).await?; let node = get_channel_node(channel_id).await?;
for user in users {
voice_client.remove_user(&node, &user, channel_id).await?;
delete_voice_state(channel_id, Some(&server.id), &user).await?;
}
}
} }
server.delete(db).await server.delete(db).await
} else { } else {
if let Some(channel_id) = get_user_voice_channel_in_server(&user.id, &server.id).await? { if let Some(channel_id) = get_user_voice_channel_in_server(&user.id, &server.id).await? {
if server.channels.iter().any(|c| c == &channel_id) { if server.channels.iter().any(|c| c == &channel_id) {
voice_client.remove_user(&user.id, &channel_id).await?; let node = get_channel_node(&channel_id).await?;
voice_client.remove_user(&node, &user.id, &channel_id).await?;
delete_voice_state(&channel_id, Some(&server.id), &user.id).await?; delete_voice_state(&channel_id, Some(&server.id), &user.id).await?;
} }
}; };