forked from jmug/stoatchat
initial livekit support
fix up code undo changes to compose file add back .env.example
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -7,3 +7,5 @@ target
|
|||||||
|
|
||||||
.vercel
|
.vercel
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
|
livekit.yml
|
||||||
3301
Cargo.lock
generated
3301
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
members = ["crates/delta", "crates/bonfire", "crates/core/*"]
|
members = ["crates/delta", "crates/bonfire", "crates/voice-ingress", "crates/core/*"]
|
||||||
|
|
||||||
[patch.crates-io]
|
[patch.crates-io]
|
||||||
# mobc-redis = { git = "https://github.com/insertish/mobc", rev = "8b880bb59f2ba80b4c7bc40c649c113d8857a186" }
|
# mobc-redis = { git = "https://github.com/insertish/mobc", rev = "8b880bb59f2ba80b4c7bc40c649c113d8857a186" }
|
||||||
@@ -8,3 +8,4 @@ redis22 = { package = "redis", version = "0.22.3", git = "https://github.com/rev
|
|||||||
redis23 = { package = "redis", version = "0.23.1", git = "https://github.com/revoltchat/redis-rs", rev = "f8ca28ab85da59d2ccde526b4d2fb390eff5a5f9" }
|
redis23 = { package = "redis", version = "0.23.1", git = "https://github.com/revoltchat/redis-rs", rev = "f8ca28ab85da59d2ccde526b4d2fb390eff5a5f9" }
|
||||||
# authifier = { package = "authifier", version = "1.0.8", path = "../authifier/crates/authifier" }
|
# authifier = { package = "authifier", version = "1.0.8", path = "../authifier/crates/authifier" }
|
||||||
# rocket_authifier = { package = "rocket_authifier", version = "1.0.8", path = "../authifier/crates/rocket_authifier" }
|
# rocket_authifier = { package = "rocket_authifier", version = "1.0.8", path = "../authifier/crates/rocket_authifier" }
|
||||||
|
rocket = { git = "https://github.com/rwf2/Rocket/", rev = "4dcd928" }
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use revolt_database::{
|
use revolt_database::{
|
||||||
events::client::EventV1, util::permissions::DatabasePermissionQuery, Channel, Database, Member,
|
events::client::{ChannelVoiceState, EventV1, ReadyServer, UserVoiceState}, util::permissions::DatabasePermissionQuery, Channel, Database, Member,
|
||||||
MemberCompositeKey, Presence, RelationshipStatus,
|
MemberCompositeKey, Presence, RelationshipStatus,
|
||||||
};
|
};
|
||||||
use revolt_models::v0;
|
use revolt_models::v0;
|
||||||
@@ -9,6 +9,8 @@ use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
|
|||||||
use revolt_presence::filter_online;
|
use revolt_presence::filter_online;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
|
|
||||||
|
use redis_kiss::{get_connection, AsyncCommands};
|
||||||
|
|
||||||
use super::state::{Cache, State};
|
use super::state::{Cache, State};
|
||||||
|
|
||||||
/// Cache Manager
|
/// Cache Manager
|
||||||
@@ -197,9 +199,29 @@ impl State {
|
|||||||
self.insert_subscription(channel.id().to_string());
|
self.insert_subscription(channel.id().to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut conn = get_connection().await.unwrap();
|
||||||
|
let mut new_servers = Vec::with_capacity(servers.len());
|
||||||
|
|
||||||
|
for server in servers {
|
||||||
|
let mut voice_states = vec![];
|
||||||
|
|
||||||
|
for channel in &server.channels {
|
||||||
|
let members = conn.smembers::<_, Vec<String>>(format!("vc-members-{channel}")).await.unwrap();
|
||||||
|
|
||||||
|
if !members.is_empty() {
|
||||||
|
voice_states.push(ChannelVoiceState {
|
||||||
|
id: channel.clone(),
|
||||||
|
participants: members.into_iter().map(|id| UserVoiceState { id }).collect()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
new_servers.push(ReadyServer { server: server.into(), voice_states })
|
||||||
|
}
|
||||||
|
|
||||||
Ok(EventV1::Ready {
|
Ok(EventV1::Ready {
|
||||||
users,
|
users,
|
||||||
servers: servers.into_iter().map(Into::into).collect(),
|
servers: new_servers,
|
||||||
channels: channels.into_iter().map(Into::into).collect(),
|
channels: channels.into_iter().map(Into::into).collect(),
|
||||||
members: members.into_iter().map(Into::into).collect(),
|
members: members.into_iter().map(Into::into).collect(),
|
||||||
emojis: emojis.into_iter().map(Into::into).collect(),
|
emojis: emojis.into_iter().map(Into::into).collect(),
|
||||||
|
|||||||
@@ -69,3 +69,4 @@ emoji_size = 500000
|
|||||||
[sentry]
|
[sentry]
|
||||||
api = ""
|
api = ""
|
||||||
events = ""
|
events = ""
|
||||||
|
voice_ingress = ""
|
||||||
@@ -44,8 +44,7 @@ pub struct Hosts {
|
|||||||
pub events: String,
|
pub events: String,
|
||||||
pub autumn: String,
|
pub autumn: String,
|
||||||
pub january: String,
|
pub january: String,
|
||||||
pub voso_legacy: String,
|
pub livekit: String
|
||||||
pub voso_legacy_ws: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
@@ -94,6 +93,13 @@ pub struct ApiWorkers {
|
|||||||
pub max_concurrent_connections: usize,
|
pub max_concurrent_connections: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
pub struct ApiLiveKit {
|
||||||
|
pub url: String,
|
||||||
|
pub key: String,
|
||||||
|
pub secret: String
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
pub struct Api {
|
pub struct Api {
|
||||||
pub registration: ApiRegistration,
|
pub registration: ApiRegistration,
|
||||||
@@ -102,6 +108,7 @@ pub struct Api {
|
|||||||
pub fcm: ApiFcm,
|
pub fcm: ApiFcm,
|
||||||
pub security: ApiSecurity,
|
pub security: ApiSecurity,
|
||||||
pub workers: ApiWorkers,
|
pub workers: ApiWorkers,
|
||||||
|
pub livekit: ApiLiveKit,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
@@ -144,6 +151,7 @@ pub struct Features {
|
|||||||
pub struct Sentry {
|
pub struct Sentry {
|
||||||
pub api: String,
|
pub api: String,
|
||||||
pub events: String,
|
pub events: String,
|
||||||
|
pub voice_ingress: String
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Clone)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ pub enum DatabaseInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Database
|
/// Database
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Database {
|
pub enum Database {
|
||||||
/// Mock database
|
/// Mock database
|
||||||
Reference(ReferenceDb),
|
Reference(ReferenceDb),
|
||||||
@@ -35,7 +35,7 @@ impl DatabaseInfo {
|
|||||||
#[async_recursion]
|
#[async_recursion]
|
||||||
pub async fn connect(self) -> Result<Database, String> {
|
pub async fn connect(self) -> Result<Database, String> {
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
|
println!("{config:?}");
|
||||||
Ok(match self {
|
Ok(match self {
|
||||||
DatabaseInfo::Auto => {
|
DatabaseInfo::Auto => {
|
||||||
if std::env::var("TEST_DB").is_ok() {
|
if std::env::var("TEST_DB").is_ok() {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ use serde::Serialize;
|
|||||||
database_derived!(
|
database_derived!(
|
||||||
#[cfg(feature = "mongodb")]
|
#[cfg(feature = "mongodb")]
|
||||||
/// MongoDB implementation
|
/// MongoDB implementation
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct MongoDb(pub ::mongodb::Client, pub String);
|
pub struct MongoDb(pub ::mongodb::Client, pub String);
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use crate::{
|
|||||||
|
|
||||||
database_derived!(
|
database_derived!(
|
||||||
/// Reference implementation
|
/// Reference implementation
|
||||||
#[derive(Default)]
|
#[derive(Default, Debug)]
|
||||||
pub struct ReferenceDb {
|
pub struct ReferenceDb {
|
||||||
pub bots: Arc<Mutex<HashMap<String, Bot>>>,
|
pub bots: Arc<Mutex<HashMap<String, Bot>>>,
|
||||||
pub channels: Arc<Mutex<HashMap<String, Channel>>>,
|
pub channels: Arc<Mutex<HashMap<String, Channel>>>,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use revolt_models::v0::{
|
|||||||
AppendMessage, Channel, Emoji, FieldsChannel, FieldsMember, FieldsRole, FieldsServer,
|
AppendMessage, Channel, Emoji, FieldsChannel, FieldsMember, FieldsRole, FieldsServer,
|
||||||
FieldsUser, FieldsWebhook, Member, MemberCompositeKey, Message, PartialChannel, PartialMember,
|
FieldsUser, FieldsWebhook, Member, MemberCompositeKey, Message, PartialChannel, PartialMember,
|
||||||
PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, Report, Server, User,
|
PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, Report, Server, User,
|
||||||
UserSettings, Webhook,
|
UserSettings, Webhook
|
||||||
};
|
};
|
||||||
use revolt_result::Error;
|
use revolt_result::Error;
|
||||||
|
|
||||||
@@ -39,6 +39,25 @@ pub enum ErrorEvent {
|
|||||||
APIError(Error),
|
APIError(Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct UserVoiceState {
|
||||||
|
pub id: String
|
||||||
|
// TODO - muted, etc
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct ChannelVoiceState {
|
||||||
|
pub id: String,
|
||||||
|
pub participants: Vec<UserVoiceState>
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
|
pub struct ReadyServer {
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub server: Server,
|
||||||
|
pub voice_states: Vec<ChannelVoiceState>
|
||||||
|
}
|
||||||
|
|
||||||
/// Protocol Events
|
/// Protocol Events
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||||
#[serde(tag = "type")]
|
#[serde(tag = "type")]
|
||||||
@@ -51,7 +70,7 @@ pub enum EventV1 {
|
|||||||
/// Basic data to cache
|
/// Basic data to cache
|
||||||
Ready {
|
Ready {
|
||||||
users: Vec<User>,
|
users: Vec<User>,
|
||||||
servers: Vec<Server>,
|
servers: Vec<ReadyServer>,
|
||||||
channels: Vec<Channel>,
|
channels: Vec<Channel>,
|
||||||
members: Vec<Member>,
|
members: Vec<Member>,
|
||||||
emojis: Vec<Emoji>,
|
emojis: Vec<Emoji>,
|
||||||
@@ -225,6 +244,16 @@ pub enum EventV1 {
|
|||||||
|
|
||||||
/// Auth events
|
/// Auth events
|
||||||
Auth(AuthifierEvent),
|
Auth(AuthifierEvent),
|
||||||
|
|
||||||
|
/// Voice events
|
||||||
|
VoiceChannelJoin {
|
||||||
|
id: String,
|
||||||
|
user: String,
|
||||||
|
},
|
||||||
|
VoiceChannelLeave {
|
||||||
|
id: String,
|
||||||
|
user: String,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventV1 {
|
impl EventV1 {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use revolt_config::config;
|
use revolt_config::config;
|
||||||
use revolt_models::v0::{self, MessageAuthor};
|
use revolt_models::v0::{self, MessageAuthor, VoiceInformation};
|
||||||
use revolt_permissions::OverrideField;
|
use revolt_permissions::OverrideField;
|
||||||
use revolt_result::Result;
|
use revolt_result::Result;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -103,6 +103,10 @@ auto_derived!(
|
|||||||
/// Whether this channel is marked as not safe for work
|
/// Whether this channel is marked as not safe for work
|
||||||
#[serde(skip_serializing_if = "crate::if_false", default)]
|
#[serde(skip_serializing_if = "crate::if_false", default)]
|
||||||
nsfw: bool,
|
nsfw: bool,
|
||||||
|
|
||||||
|
/// Voice Information for when this channel is also a voice channel
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
voice: Option<VoiceInformation>
|
||||||
},
|
},
|
||||||
/// Voice channel belonging to a server
|
/// Voice channel belonging to a server
|
||||||
VoiceChannel {
|
VoiceChannel {
|
||||||
@@ -219,6 +223,7 @@ impl Channel {
|
|||||||
default_permissions: None,
|
default_permissions: None,
|
||||||
role_permissions: HashMap::new(),
|
role_permissions: HashMap::new(),
|
||||||
nsfw: data.nsfw.unwrap_or(false),
|
nsfw: data.nsfw.unwrap_or(false),
|
||||||
|
voice: data.voice
|
||||||
},
|
},
|
||||||
v0::LegacyServerChannelType::Voice => Channel::VoiceChannel {
|
v0::LegacyServerChannelType::Voice => Channel::VoiceChannel {
|
||||||
id: id.clone(),
|
id: id.clone(),
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use crate::{events::client::EventV1, Database, File, RatelimitEvent};
|
|||||||
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
|
use redis_kiss::{get_connection, AsyncCommands};
|
||||||
use revolt_config::config;
|
use revolt_config::config;
|
||||||
use revolt_models::v0;
|
use revolt_models::v0;
|
||||||
use revolt_presence::filter_online;
|
use revolt_presence::filter_online;
|
||||||
@@ -567,6 +568,15 @@ impl User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets current voice channel
|
||||||
|
pub async fn current_voice_channel(&self) -> Result<Option<String>> {
|
||||||
|
let mut conn = get_connection().await.map_err(|_| create_error!(InternalError))?;
|
||||||
|
|
||||||
|
conn.get::<_, Option<String>>(format!("vc-{}", &self.id))
|
||||||
|
.await
|
||||||
|
.map_err(|_| create_error!(InternalError))
|
||||||
|
}
|
||||||
|
|
||||||
/// Update user data
|
/// Update user data
|
||||||
pub async fn update<'a>(
|
pub async fn update<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|||||||
@@ -186,6 +186,7 @@ impl From<crate::Channel> for Channel {
|
|||||||
default_permissions,
|
default_permissions,
|
||||||
role_permissions,
|
role_permissions,
|
||||||
nsfw,
|
nsfw,
|
||||||
|
voice
|
||||||
} => Channel::TextChannel {
|
} => Channel::TextChannel {
|
||||||
id,
|
id,
|
||||||
server,
|
server,
|
||||||
@@ -196,6 +197,7 @@ impl From<crate::Channel> for Channel {
|
|||||||
default_permissions,
|
default_permissions,
|
||||||
role_permissions,
|
role_permissions,
|
||||||
nsfw,
|
nsfw,
|
||||||
|
voice
|
||||||
},
|
},
|
||||||
crate::Channel::VoiceChannel {
|
crate::Channel::VoiceChannel {
|
||||||
id,
|
id,
|
||||||
@@ -266,6 +268,7 @@ impl From<Channel> for crate::Channel {
|
|||||||
default_permissions,
|
default_permissions,
|
||||||
role_permissions,
|
role_permissions,
|
||||||
nsfw,
|
nsfw,
|
||||||
|
voice
|
||||||
} => crate::Channel::TextChannel {
|
} => crate::Channel::TextChannel {
|
||||||
id,
|
id,
|
||||||
server,
|
server,
|
||||||
@@ -276,6 +279,7 @@ impl From<Channel> for crate::Channel {
|
|||||||
default_permissions,
|
default_permissions,
|
||||||
role_permissions,
|
role_permissions,
|
||||||
nsfw,
|
nsfw,
|
||||||
|
voice
|
||||||
},
|
},
|
||||||
Channel::VoiceChannel {
|
Channel::VoiceChannel {
|
||||||
id,
|
id,
|
||||||
|
|||||||
@@ -107,8 +107,12 @@ auto_derived!(
|
|||||||
serde(skip_serializing_if = "crate::if_false", default)
|
serde(skip_serializing_if = "crate::if_false", default)
|
||||||
)]
|
)]
|
||||||
nsfw: bool,
|
nsfw: bool,
|
||||||
|
|
||||||
|
/// Voice Information for when this channel is also a voice channel
|
||||||
|
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
|
||||||
|
voice: Option<VoiceInformation>
|
||||||
},
|
},
|
||||||
/// Voice channel belonging to a server
|
/// DEPRECATED (use TextChannel { voice }): Voice channel belonging to a server
|
||||||
VoiceChannel {
|
VoiceChannel {
|
||||||
/// Unique Id
|
/// Unique Id
|
||||||
#[cfg_attr(feature = "serde", serde(rename = "_id"))]
|
#[cfg_attr(feature = "serde", serde(rename = "_id"))]
|
||||||
@@ -147,6 +151,15 @@ auto_derived!(
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Voice information for a channel
|
||||||
|
#[derive(Default)]
|
||||||
|
#[cfg_attr(feature = "validator", derive(validator::Validate))]
|
||||||
|
pub struct VoiceInformation {
|
||||||
|
/// Maximium amount of users allowed in the voice channel at once
|
||||||
|
#[cfg_attr(feature = "validator", validate(range(min = 1)))]
|
||||||
|
pub max_users: Option<u32>
|
||||||
|
}
|
||||||
|
|
||||||
/// Partial representation of a channel
|
/// Partial representation of a channel
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct PartialChannel {
|
pub struct PartialChannel {
|
||||||
@@ -260,6 +273,10 @@ auto_derived!(
|
|||||||
/// Whether this channel is age restricted
|
/// Whether this channel is age restricted
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub nsfw: Option<bool>,
|
pub nsfw: Option<bool>,
|
||||||
|
|
||||||
|
/// Voice Information for when this channel is also a voice channel
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub voice: Option<VoiceInformation>
|
||||||
}
|
}
|
||||||
|
|
||||||
/// New default permissions
|
/// New default permissions
|
||||||
|
|||||||
@@ -130,8 +130,9 @@ pub enum ErrorType {
|
|||||||
error: String,
|
error: String,
|
||||||
},
|
},
|
||||||
|
|
||||||
// ? Legacy errors
|
// ? Voice errors
|
||||||
VosoUnavailable,
|
LiveKitUnavailable,
|
||||||
|
AlreadyInVoiceChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
|
|||||||
@@ -71,10 +71,12 @@ impl<'r> Responder<'r, 'static> for Error {
|
|||||||
ErrorType::InvalidProperty => Status::BadRequest,
|
ErrorType::InvalidProperty => Status::BadRequest,
|
||||||
ErrorType::InvalidSession => Status::Unauthorized,
|
ErrorType::InvalidSession => Status::Unauthorized,
|
||||||
ErrorType::DuplicateNonce => Status::Conflict,
|
ErrorType::DuplicateNonce => Status::Conflict,
|
||||||
ErrorType::VosoUnavailable => Status::BadRequest,
|
|
||||||
ErrorType::NotFound => Status::NotFound,
|
ErrorType::NotFound => Status::NotFound,
|
||||||
ErrorType::NoEffect => Status::Ok,
|
ErrorType::NoEffect => Status::Ok,
|
||||||
ErrorType::FailedValidation { .. } => Status::BadRequest,
|
ErrorType::FailedValidation { .. } => Status::BadRequest,
|
||||||
|
|
||||||
|
ErrorType::LiveKitUnavailable => Status::BadRequest,
|
||||||
|
ErrorType::AlreadyInVoiceChannel => Status::BadRequest,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Serialize the error data structure into JSON.
|
// Serialize the error data structure into JSON.
|
||||||
|
|||||||
@@ -80,5 +80,8 @@ revolt-models = { path = "../core/models", features = [
|
|||||||
revolt-result = { path = "../core/result", features = ["rocket", "okapi"] }
|
revolt-result = { path = "../core/result", features = ["rocket", "okapi"] }
|
||||||
revolt-permissions = { path = "../core/permissions", features = ["schemas"] }
|
revolt-permissions = { path = "../core/permissions", features = ["schemas"] }
|
||||||
|
|
||||||
|
# voice
|
||||||
|
livekit-api = "0.3.2"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
vergen = "7.5.0"
|
vergen = "7.5.0"
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ use authifier::config::{
|
|||||||
};
|
};
|
||||||
use authifier::{Authifier, AuthifierEvent};
|
use authifier::{Authifier, AuthifierEvent};
|
||||||
use rocket::data::ToByteUnit;
|
use rocket::data::ToByteUnit;
|
||||||
|
use livekit_api::services::room::RoomClient;
|
||||||
|
|
||||||
pub async fn web() -> Rocket<Build> {
|
pub async fn web() -> Rocket<Build> {
|
||||||
// Get settings
|
// Get settings
|
||||||
@@ -34,6 +35,7 @@ pub async fn web() -> Rocket<Build> {
|
|||||||
|
|
||||||
// Setup database
|
// Setup database
|
||||||
let db = revolt_database::DatabaseInfo::Auto.connect().await.unwrap();
|
let db = revolt_database::DatabaseInfo::Auto.connect().await.unwrap();
|
||||||
|
log::info!("database_here {db:?}");
|
||||||
db.migrate_database().await.unwrap();
|
db.migrate_database().await.unwrap();
|
||||||
|
|
||||||
// Setup Authifier event channel
|
// Setup Authifier event channel
|
||||||
@@ -97,6 +99,11 @@ pub async fn web() -> Rocket<Build> {
|
|||||||
)
|
)
|
||||||
.into();
|
.into();
|
||||||
|
|
||||||
|
|
||||||
|
log::info!("{:?}", &config.api.livekit);
|
||||||
|
|
||||||
|
let room_client = RoomClient::with_api_key(&config.api.livekit.url, &config.api.livekit.key, &config.api.livekit.secret);
|
||||||
|
|
||||||
// Configure Rocket
|
// Configure Rocket
|
||||||
let rocket = rocket::build();
|
let rocket = rocket::build();
|
||||||
let prometheus = PrometheusMetrics::new();
|
let prometheus = PrometheusMetrics::new();
|
||||||
@@ -110,6 +117,7 @@ pub async fn web() -> Rocket<Build> {
|
|||||||
.manage(authifier)
|
.manage(authifier)
|
||||||
.manage(db)
|
.manage(db)
|
||||||
.manage(cors.clone())
|
.manage(cors.clone())
|
||||||
|
.manage(room_client)
|
||||||
.attach(util::ratelimiter::RatelimitFairing)
|
.attach(util::ratelimiter::RatelimitFairing)
|
||||||
.attach(cors)
|
.attach(cors)
|
||||||
.configure(rocket::Config {
|
.configure(rocket::Config {
|
||||||
|
|||||||
@@ -1,105 +1,75 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use revolt_config::config;
|
use revolt_config::config;
|
||||||
use revolt_database::{
|
|
||||||
util::{permissions::DatabasePermissionQuery, reference::Reference},
|
|
||||||
Channel, Database, User,
|
|
||||||
};
|
|
||||||
use revolt_models::v0;
|
use revolt_models::v0;
|
||||||
|
use revolt_database::{events::client::EventV1, util::{permissions::perms, reference::Reference}, Channel, Database, User};
|
||||||
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
|
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
|
||||||
use revolt_result::{create_error, Result};
|
use revolt_result::{create_error, Result};
|
||||||
|
|
||||||
|
use livekit_api::{access_token::{AccessToken, VideoGrants}, services::room::{CreateRoomOptions, RoomClient}};
|
||||||
use rocket::{serde::json::Json, State};
|
use rocket::{serde::json::Json, State};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// # Voice Server Token Response
|
||||||
|
#[derive(Serialize, Deserialize, JsonSchema)]
|
||||||
|
pub struct CreateVoiceUserResponse {
|
||||||
|
/// Token for authenticating with the voice server
|
||||||
|
token: String,
|
||||||
|
}
|
||||||
|
|
||||||
/// # Join Call
|
/// # Join Call
|
||||||
///
|
///
|
||||||
/// 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")]
|
||||||
pub async fn call(
|
pub async fn call(db: &State<Database>, rooms: &State<RoomClient>, user: User, target: Reference) -> Result<Json<CreateVoiceUserResponse>> {
|
||||||
db: &State<Database>,
|
|
||||||
user: User,
|
|
||||||
target: Reference,
|
|
||||||
) -> Result<Json<v0::LegacyCreateVoiceUserResponse>> {
|
|
||||||
let channel = target.as_channel(db).await?;
|
let channel = target.as_channel(db).await?;
|
||||||
let mut query = DatabasePermissionQuery::new(db, &user).channel(&channel);
|
|
||||||
calculate_channel_permissions(&mut query)
|
let mut permissions = perms(db, &user).channel(&channel);
|
||||||
|
|
||||||
|
calculate_channel_permissions(&mut permissions)
|
||||||
.await
|
.await
|
||||||
.throw_if_lacking_channel_permission(ChannelPermission::Connect)?;
|
.throw_if_lacking_channel_permission(ChannelPermission::Connect)?;
|
||||||
|
|
||||||
|
if user.current_voice_channel().await?.is_some() {
|
||||||
|
return Err(create_error!(AlreadyInVoiceChannel))
|
||||||
|
}
|
||||||
|
|
||||||
let config = config().await;
|
let config = config().await;
|
||||||
if config.api.security.voso_legacy_token.is_empty() {
|
|
||||||
return Err(create_error!(VosoUnavailable));
|
|
||||||
}
|
|
||||||
|
|
||||||
match channel {
|
if config.api.livekit.url.is_empty() {
|
||||||
Channel::SavedMessages { .. } | Channel::TextChannel { .. } => {
|
return Err(create_error!(LiveKitUnavailable));
|
||||||
return Err(create_error!(CannotJoinCall))
|
};
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// To join a call:
|
let voice = match &channel {
|
||||||
// - Check if the room exists.
|
Channel::DirectMessage { .. } | Channel::VoiceChannel { .. } => Cow::Owned(v0::VoiceInformation::default()),
|
||||||
// - If not, create it.
|
Channel::TextChannel { voice: Some(voice), .. } => Cow::Borrowed(voice),
|
||||||
let client = reqwest::Client::new();
|
_ => return Err(create_error!(CannotJoinCall))
|
||||||
let result = client
|
};
|
||||||
.get(&format!(
|
|
||||||
"{}/room/{}",
|
|
||||||
config.hosts.voso_legacy,
|
|
||||||
channel.id()
|
|
||||||
))
|
|
||||||
.header(
|
|
||||||
reqwest::header::AUTHORIZATION,
|
|
||||||
config.api.security.voso_legacy_token.clone(),
|
|
||||||
)
|
|
||||||
.send()
|
|
||||||
.await;
|
|
||||||
|
|
||||||
match result {
|
let token = AccessToken::with_api_key(&config.api.livekit.key, &config.api.livekit.secret)
|
||||||
Err(_) => return Err(create_error!(VosoUnavailable)),
|
.with_name(&format!("{}#{}", user.username, user.discriminator))
|
||||||
Ok(result) => match result.status() {
|
.with_identity(&user.id)
|
||||||
reqwest::StatusCode::OK => (),
|
.with_metadata(&serde_json::to_string(&user).map_err(|_| create_error!(InternalError))?)
|
||||||
reqwest::StatusCode::NOT_FOUND => {
|
.with_grants(VideoGrants {
|
||||||
if (client
|
room_join: true,
|
||||||
.post(&format!(
|
room: channel.id().to_string(),
|
||||||
"{}/room/{}",
|
..Default::default()
|
||||||
config.hosts.voso_legacy,
|
})
|
||||||
channel.id()
|
.to_jwt()
|
||||||
))
|
.inspect_err(|e| log::error!("{e:?}"))
|
||||||
.header(
|
.map_err(|_| create_error!(InternalError))?;
|
||||||
reqwest::header::AUTHORIZATION,
|
|
||||||
config.api.security.voso_legacy_token.clone(),
|
|
||||||
)
|
|
||||||
.send()
|
|
||||||
.await)
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
return Err(create_error!(VosoUnavailable));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => return Err(create_error!(VosoUnavailable)),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then create a user for the room.
|
let room = rooms.create_room(&channel.id(), CreateRoomOptions {
|
||||||
if let Ok(response) = client
|
max_participants: voice.max_users.unwrap_or(u32::MAX),
|
||||||
.post(&format!(
|
empty_timeout: 5 * 60, // 5 minutes
|
||||||
"{}/room/{}/user/{}",
|
..Default::default()
|
||||||
config.hosts.voso_legacy,
|
})
|
||||||
channel.id(),
|
|
||||||
user.id
|
|
||||||
))
|
|
||||||
.header(
|
|
||||||
reqwest::header::AUTHORIZATION,
|
|
||||||
config.api.security.voso_legacy_token,
|
|
||||||
)
|
|
||||||
.send()
|
|
||||||
.await
|
.await
|
||||||
{
|
.inspect_err(|e| log::error!("{e:?}"))
|
||||||
response
|
.map_err(|_| create_error!(InternalError))?;
|
||||||
.json()
|
|
||||||
.await
|
log::info!("created room {room:?}");
|
||||||
.map_err(|_| create_error!(InvalidOperation))
|
|
||||||
.map(Json)
|
Ok(Json(CreateVoiceUserResponse { token }))
|
||||||
} else {
|
|
||||||
Err(create_error!(VosoUnavailable))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,9 +27,7 @@ pub struct VoiceFeature {
|
|||||||
/// Whether voice is enabled
|
/// Whether voice is enabled
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
/// URL pointing to the voice API
|
/// URL pointing to the voice API
|
||||||
pub url: String,
|
pub url: String
|
||||||
/// URL pointing to the voice WebSocket server
|
|
||||||
pub ws: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Feature Configuration
|
/// # Feature Configuration
|
||||||
@@ -46,7 +44,7 @@ pub struct RevoltFeatures {
|
|||||||
/// Proxy service configuration
|
/// Proxy service configuration
|
||||||
pub january: Feature,
|
pub january: Feature,
|
||||||
/// Voice server configuration
|
/// Voice server configuration
|
||||||
pub voso: VoiceFeature,
|
pub livekit: VoiceFeature,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # Build Information
|
/// # Build Information
|
||||||
@@ -106,10 +104,9 @@ pub async fn root() -> Result<Json<RevoltConfig>> {
|
|||||||
enabled: !config.hosts.january.is_empty(),
|
enabled: !config.hosts.january.is_empty(),
|
||||||
url: config.hosts.january,
|
url: config.hosts.january,
|
||||||
},
|
},
|
||||||
voso: VoiceFeature {
|
livekit: VoiceFeature {
|
||||||
enabled: !config.hosts.voso_legacy.is_empty(),
|
enabled: !config.api.livekit.url.is_empty(),
|
||||||
url: config.hosts.voso_legacy,
|
url: config.api.livekit.url.to_string(),
|
||||||
ws: config.hosts.voso_legacy_ws,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
ws: config.hosts.events,
|
ws: config.hosts.events,
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ impl TestHarness {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut stream = self.sub.on_message();
|
let mut stream = self.sub.on_message();
|
||||||
while let Some(item) = stream.next().await {
|
while let Some(Ok(item)) = stream.next().await {
|
||||||
let msg_topic = item.get_channel_name();
|
let msg_topic = item.get_channel_name();
|
||||||
let payload: EventV1 = redis_kiss::decode_payload(&item).unwrap();
|
let payload: EventV1 = redis_kiss::decode_payload(&item).unwrap();
|
||||||
|
|
||||||
|
|||||||
1
crates/voice-ingress/.gitignore
vendored
Normal file
1
crates/voice-ingress/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
44
crates/voice-ingress/Cargo.toml
Normal file
44
crates/voice-ingress/Cargo.toml
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
[package]
|
||||||
|
name = "revolt-voice-ingress"
|
||||||
|
version = "0.7.1"
|
||||||
|
license = "AGPL-3.0-or-later"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
# util
|
||||||
|
log = "*"
|
||||||
|
sentry = "0.31.5"
|
||||||
|
lru = "0.7.6"
|
||||||
|
ulid = "0.5.0"
|
||||||
|
redis-kiss = "0.1.4"
|
||||||
|
|
||||||
|
# serde
|
||||||
|
serde_json = "1.0.79"
|
||||||
|
rmp-serde = "1.0.0"
|
||||||
|
serde = "1.0.136"
|
||||||
|
|
||||||
|
# http
|
||||||
|
rocket = { version = "0.5.0-rc.2", features = ["json"] }
|
||||||
|
rocket_empty = "0.1.1"
|
||||||
|
|
||||||
|
# async
|
||||||
|
futures = "0.3.21"
|
||||||
|
async-std = { version = "1.8.0", features = [
|
||||||
|
"tokio1",
|
||||||
|
"tokio02",
|
||||||
|
"attributes",
|
||||||
|
] }
|
||||||
|
|
||||||
|
# core
|
||||||
|
revolt-result = { path = "../core/result" }
|
||||||
|
revolt-models = { path = "../core/models" }
|
||||||
|
revolt-config = { path = "../core/config" }
|
||||||
|
revolt-database = { path = "../core/database" }
|
||||||
|
revolt-permissions = { version = "0.7.1", path = "../core/permissions" }
|
||||||
|
revolt-presence = { path = "../core/presence", features = ["redis-is-patched"] }
|
||||||
|
|
||||||
|
# voice
|
||||||
|
livekit-api = "0.3.2"
|
||||||
|
livekit-protocol = "0.3.2"
|
||||||
11
crates/voice-ingress/Dockerfile
Normal file
11
crates/voice-ingress/Dockerfile
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# Build Stage
|
||||||
|
FROM ghcr.io/revoltchat/base:latest AS builder
|
||||||
|
|
||||||
|
# Bundle Stage
|
||||||
|
FROM debian:bullseye-slim
|
||||||
|
RUN apt-get update && \
|
||||||
|
apt-get install -y ca-certificates && \
|
||||||
|
apt-get clean
|
||||||
|
COPY --from=builder /home/rust/src/target/release/revolt-voice-ingress ./
|
||||||
|
EXPOSE 9000
|
||||||
|
CMD ["./revolt-voice-ingress"]
|
||||||
75
crates/voice-ingress/src/main.rs
Normal file
75
crates/voice-ingress/src/main.rs
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
use std::env;
|
||||||
|
|
||||||
|
use redis_kiss::{get_connection, AsyncCommands};
|
||||||
|
|
||||||
|
use revolt_database::events::client::EventV1;
|
||||||
|
use rocket::{build, post, routes, serde::json::Json, Config};
|
||||||
|
use rocket_empty::EmptyResponse;
|
||||||
|
use livekit_protocol::WebhookEvent;
|
||||||
|
|
||||||
|
use revolt_config::Database;
|
||||||
|
use revolt_result::Result;
|
||||||
|
|
||||||
|
#[async_std::main]
|
||||||
|
async fn main() {
|
||||||
|
revolt_config::configure!(voice_ingress);
|
||||||
|
|
||||||
|
let rocket = build()
|
||||||
|
.manage(get_connection().await.unwrap())
|
||||||
|
.mount("/", routes![ingress])
|
||||||
|
.configure(Config { port: 8500, ..Default::default() })
|
||||||
|
.launch()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/", data="<body>")]
|
||||||
|
async fn ingress(body: Json<WebhookEvent>) -> Result<EmptyResponse> {
|
||||||
|
let mut redis = get_connection().await.unwrap();
|
||||||
|
|
||||||
|
log::debug!("received event: {body:?}");
|
||||||
|
|
||||||
|
let channel_id = body
|
||||||
|
.room
|
||||||
|
.as_ref()
|
||||||
|
.map(|r| &r.name);
|
||||||
|
|
||||||
|
let user_id = body
|
||||||
|
.participant
|
||||||
|
.as_ref()
|
||||||
|
.map(|r| &r.identity);
|
||||||
|
|
||||||
|
match body.event.as_str() {
|
||||||
|
"participant_joined" => {
|
||||||
|
let channel_id = channel_id.unwrap();
|
||||||
|
let user_id = user_id.unwrap();
|
||||||
|
|
||||||
|
redis.sadd::<_, _, u64>(format!("vc-members-{}", channel_id), user_id).await.unwrap();
|
||||||
|
redis.set::<_, _, String>(format!("vc-{}", user_id), &channel_id).await.unwrap();
|
||||||
|
|
||||||
|
EventV1::VoiceChannelJoin {
|
||||||
|
id: channel_id.clone(),
|
||||||
|
user: user_id.clone()
|
||||||
|
}
|
||||||
|
.p(channel_id.clone())
|
||||||
|
.await
|
||||||
|
},
|
||||||
|
"participant_left" => {
|
||||||
|
let channel_id = channel_id.unwrap();
|
||||||
|
let user_id = user_id.unwrap();
|
||||||
|
|
||||||
|
redis.srem::<_, _, u64>(format!("vc-members-{}", channel_id), user_id).await.unwrap();
|
||||||
|
redis.del::<_, u64>(format!("vc-{}", user_id)).await.unwrap();
|
||||||
|
|
||||||
|
EventV1::VoiceChannelLeave {
|
||||||
|
id: channel_id.clone(),
|
||||||
|
user: user_id.clone()
|
||||||
|
}
|
||||||
|
.p(channel_id.clone())
|
||||||
|
.await
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(EmptyResponse)
|
||||||
|
}
|
||||||
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
[toolchain]
|
||||||
|
channel = "stable"
|
||||||
Reference in New Issue
Block a user