initial livekit support

fix up code

undo changes to compose file

add back .env.example
This commit is contained in:
Zomatree
2024-04-09 02:18:34 +01:00
parent 7703475868
commit ad0dcad497
26 changed files with 2397 additions and 1334 deletions

2
.gitignore vendored
View File

@@ -7,3 +7,5 @@ target
.vercel
.DS_Store
livekit.yml

3301
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["crates/delta", "crates/bonfire", "crates/core/*"]
members = ["crates/delta", "crates/bonfire", "crates/voice-ingress", "crates/core/*"]
[patch.crates-io]
# 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" }
# 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 = { git = "https://github.com/rwf2/Rocket/", rev = "4dcd928" }

View File

@@ -1,7 +1,7 @@
use std::collections::HashSet;
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,
};
use revolt_models::v0;
@@ -9,6 +9,8 @@ use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
use revolt_presence::filter_online;
use revolt_result::Result;
use redis_kiss::{get_connection, AsyncCommands};
use super::state::{Cache, State};
/// Cache Manager
@@ -197,9 +199,29 @@ impl State {
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 {
users,
servers: servers.into_iter().map(Into::into).collect(),
servers: new_servers,
channels: channels.into_iter().map(Into::into).collect(),
members: members.into_iter().map(Into::into).collect(),
emojis: emojis.into_iter().map(Into::into).collect(),

View File

@@ -69,3 +69,4 @@ emoji_size = 500000
[sentry]
api = ""
events = ""
voice_ingress = ""

View File

@@ -44,8 +44,7 @@ pub struct Hosts {
pub events: String,
pub autumn: String,
pub january: String,
pub voso_legacy: String,
pub voso_legacy_ws: String,
pub livekit: String
}
#[derive(Deserialize, Debug, Clone)]
@@ -94,6 +93,13 @@ pub struct ApiWorkers {
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)]
pub struct Api {
pub registration: ApiRegistration,
@@ -102,6 +108,7 @@ pub struct Api {
pub fcm: ApiFcm,
pub security: ApiSecurity,
pub workers: ApiWorkers,
pub livekit: ApiLiveKit,
}
#[derive(Deserialize, Debug, Clone)]
@@ -144,6 +151,7 @@ pub struct Features {
pub struct Sentry {
pub api: String,
pub events: String,
pub voice_ingress: String
}
#[derive(Deserialize, Debug, Clone)]

View File

@@ -22,7 +22,7 @@ pub enum DatabaseInfo {
}
/// Database
#[derive(Clone)]
#[derive(Clone, Debug)]
pub enum Database {
/// Mock database
Reference(ReferenceDb),
@@ -35,7 +35,7 @@ impl DatabaseInfo {
#[async_recursion]
pub async fn connect(self) -> Result<Database, String> {
let config = config().await;
println!("{config:?}");
Ok(match self {
DatabaseInfo::Auto => {
if std::env::var("TEST_DB").is_ok() {

View File

@@ -12,6 +12,7 @@ use serde::Serialize;
database_derived!(
#[cfg(feature = "mongodb")]
/// MongoDB implementation
#[derive(Debug)]
pub struct MongoDb(pub ::mongodb::Client, pub String);
);

View File

@@ -10,7 +10,7 @@ use crate::{
database_derived!(
/// Reference implementation
#[derive(Default)]
#[derive(Default, Debug)]
pub struct ReferenceDb {
pub bots: Arc<Mutex<HashMap<String, Bot>>>,
pub channels: Arc<Mutex<HashMap<String, Channel>>>,

View File

@@ -5,7 +5,7 @@ use revolt_models::v0::{
AppendMessage, Channel, Emoji, FieldsChannel, FieldsMember, FieldsRole, FieldsServer,
FieldsUser, FieldsWebhook, Member, MemberCompositeKey, Message, PartialChannel, PartialMember,
PartialMessage, PartialRole, PartialServer, PartialUser, PartialWebhook, Report, Server, User,
UserSettings, Webhook,
UserSettings, Webhook
};
use revolt_result::Error;
@@ -39,6 +39,25 @@ pub enum ErrorEvent {
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
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type")]
@@ -51,7 +70,7 @@ pub enum EventV1 {
/// Basic data to cache
Ready {
users: Vec<User>,
servers: Vec<Server>,
servers: Vec<ReadyServer>,
channels: Vec<Channel>,
members: Vec<Member>,
emojis: Vec<Emoji>,
@@ -225,6 +244,16 @@ pub enum EventV1 {
/// Auth events
Auth(AuthifierEvent),
/// Voice events
VoiceChannelJoin {
id: String,
user: String,
},
VoiceChannelLeave {
id: String,
user: String,
}
}
impl EventV1 {

View File

@@ -1,7 +1,7 @@
use std::collections::HashMap;
use revolt_config::config;
use revolt_models::v0::{self, MessageAuthor};
use revolt_models::v0::{self, MessageAuthor, VoiceInformation};
use revolt_permissions::OverrideField;
use revolt_result::Result;
use serde::{Deserialize, Serialize};
@@ -103,6 +103,10 @@ auto_derived!(
/// Whether this channel is marked as not safe for work
#[serde(skip_serializing_if = "crate::if_false", default)]
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
VoiceChannel {
@@ -219,6 +223,7 @@ impl Channel {
default_permissions: None,
role_permissions: HashMap::new(),
nsfw: data.nsfw.unwrap_or(false),
voice: data.voice
},
v0::LegacyServerChannelType::Voice => Channel::VoiceChannel {
id: id.clone(),

View File

@@ -4,6 +4,7 @@ use crate::{events::client::EventV1, Database, File, RatelimitEvent};
use once_cell::sync::Lazy;
use rand::seq::SliceRandom;
use redis_kiss::{get_connection, AsyncCommands};
use revolt_config::config;
use revolt_models::v0;
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
pub async fn update<'a>(
&mut self,

View File

@@ -186,6 +186,7 @@ impl From<crate::Channel> for Channel {
default_permissions,
role_permissions,
nsfw,
voice
} => Channel::TextChannel {
id,
server,
@@ -196,6 +197,7 @@ impl From<crate::Channel> for Channel {
default_permissions,
role_permissions,
nsfw,
voice
},
crate::Channel::VoiceChannel {
id,
@@ -266,6 +268,7 @@ impl From<Channel> for crate::Channel {
default_permissions,
role_permissions,
nsfw,
voice
} => crate::Channel::TextChannel {
id,
server,
@@ -276,6 +279,7 @@ impl From<Channel> for crate::Channel {
default_permissions,
role_permissions,
nsfw,
voice
},
Channel::VoiceChannel {
id,

View File

@@ -107,8 +107,12 @@ auto_derived!(
serde(skip_serializing_if = "crate::if_false", default)
)]
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 {
/// Unique 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
#[derive(Default)]
pub struct PartialChannel {
@@ -260,6 +273,10 @@ auto_derived!(
/// Whether this channel is age restricted
#[serde(skip_serializing_if = "Option::is_none")]
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

View File

@@ -130,8 +130,9 @@ pub enum ErrorType {
error: String,
},
// ? Legacy errors
VosoUnavailable,
// ? Voice errors
LiveKitUnavailable,
AlreadyInVoiceChannel
}
#[macro_export]

View File

@@ -71,10 +71,12 @@ impl<'r> Responder<'r, 'static> for Error {
ErrorType::InvalidProperty => Status::BadRequest,
ErrorType::InvalidSession => Status::Unauthorized,
ErrorType::DuplicateNonce => Status::Conflict,
ErrorType::VosoUnavailable => Status::BadRequest,
ErrorType::NotFound => Status::NotFound,
ErrorType::NoEffect => Status::Ok,
ErrorType::FailedValidation { .. } => Status::BadRequest,
ErrorType::LiveKitUnavailable => Status::BadRequest,
ErrorType::AlreadyInVoiceChannel => Status::BadRequest,
};
// Serialize the error data structure into JSON.

View File

@@ -80,5 +80,8 @@ revolt-models = { path = "../core/models", features = [
revolt-result = { path = "../core/result", features = ["rocket", "okapi"] }
revolt-permissions = { path = "../core/permissions", features = ["schemas"] }
# voice
livekit-api = "0.3.2"
[build-dependencies]
vergen = "7.5.0"

View File

@@ -24,6 +24,7 @@ use authifier::config::{
};
use authifier::{Authifier, AuthifierEvent};
use rocket::data::ToByteUnit;
use livekit_api::services::room::RoomClient;
pub async fn web() -> Rocket<Build> {
// Get settings
@@ -34,6 +35,7 @@ pub async fn web() -> Rocket<Build> {
// Setup database
let db = revolt_database::DatabaseInfo::Auto.connect().await.unwrap();
log::info!("database_here {db:?}");
db.migrate_database().await.unwrap();
// Setup Authifier event channel
@@ -97,6 +99,11 @@ pub async fn web() -> Rocket<Build> {
)
.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
let rocket = rocket::build();
let prometheus = PrometheusMetrics::new();
@@ -110,6 +117,7 @@ pub async fn web() -> Rocket<Build> {
.manage(authifier)
.manage(db)
.manage(cors.clone())
.manage(room_client)
.attach(util::ratelimiter::RatelimitFairing)
.attach(cors)
.configure(rocket::Config {

View File

@@ -1,105 +1,75 @@
use std::borrow::Cow;
use revolt_config::config;
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference},
Channel, Database, User,
};
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_result::{create_error, Result};
use livekit_api::{access_token::{AccessToken, VideoGrants}, services::room::{CreateRoomOptions, RoomClient}};
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
///
/// Asks the voice server for a token to join the call.
#[openapi(tag = "Voice")]
#[post("/<target>/join_call")]
pub async fn call(
db: &State<Database>,
user: User,
target: Reference,
) -> Result<Json<v0::LegacyCreateVoiceUserResponse>> {
pub async fn call(db: &State<Database>, rooms: &State<RoomClient>, user: User, target: Reference) -> Result<Json<CreateVoiceUserResponse>> {
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
.throw_if_lacking_channel_permission(ChannelPermission::Connect)?;
if user.current_voice_channel().await?.is_some() {
return Err(create_error!(AlreadyInVoiceChannel))
}
let config = config().await;
if config.api.security.voso_legacy_token.is_empty() {
return Err(create_error!(VosoUnavailable));
}
match channel {
Channel::SavedMessages { .. } | Channel::TextChannel { .. } => {
return Err(create_error!(CannotJoinCall))
}
_ => {}
}
if config.api.livekit.url.is_empty() {
return Err(create_error!(LiveKitUnavailable));
};
// To join a call:
// - Check if the room exists.
// - If not, create it.
let client = reqwest::Client::new();
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;
let voice = match &channel {
Channel::DirectMessage { .. } | Channel::VoiceChannel { .. } => Cow::Owned(v0::VoiceInformation::default()),
Channel::TextChannel { voice: Some(voice), .. } => Cow::Borrowed(voice),
_ => return Err(create_error!(CannotJoinCall))
};
match result {
Err(_) => return Err(create_error!(VosoUnavailable)),
Ok(result) => match result.status() {
reqwest::StatusCode::OK => (),
reqwest::StatusCode::NOT_FOUND => {
if (client
.post(&format!(
"{}/room/{}",
config.hosts.voso_legacy,
channel.id()
))
.header(
reqwest::header::AUTHORIZATION,
config.api.security.voso_legacy_token.clone(),
)
.send()
.await)
.is_err()
{
return Err(create_error!(VosoUnavailable));
}
}
_ => return Err(create_error!(VosoUnavailable)),
},
}
let token = AccessToken::with_api_key(&config.api.livekit.key, &config.api.livekit.secret)
.with_name(&format!("{}#{}", user.username, user.discriminator))
.with_identity(&user.id)
.with_metadata(&serde_json::to_string(&user).map_err(|_| create_error!(InternalError))?)
.with_grants(VideoGrants {
room_join: true,
room: channel.id().to_string(),
..Default::default()
})
.to_jwt()
.inspect_err(|e| log::error!("{e:?}"))
.map_err(|_| create_error!(InternalError))?;
// Then create a user for the room.
if let Ok(response) = client
.post(&format!(
"{}/room/{}/user/{}",
config.hosts.voso_legacy,
channel.id(),
user.id
))
.header(
reqwest::header::AUTHORIZATION,
config.api.security.voso_legacy_token,
)
.send()
.await
{
response
.json()
.await
.map_err(|_| create_error!(InvalidOperation))
.map(Json)
} else {
Err(create_error!(VosoUnavailable))
}
let room = rooms.create_room(&channel.id(), CreateRoomOptions {
max_participants: voice.max_users.unwrap_or(u32::MAX),
empty_timeout: 5 * 60, // 5 minutes
..Default::default()
})
.await
.inspect_err(|e| log::error!("{e:?}"))
.map_err(|_| create_error!(InternalError))?;
log::info!("created room {room:?}");
Ok(Json(CreateVoiceUserResponse { token }))
}

View File

@@ -27,9 +27,7 @@ pub struct VoiceFeature {
/// Whether voice is enabled
pub enabled: bool,
/// URL pointing to the voice API
pub url: String,
/// URL pointing to the voice WebSocket server
pub ws: String,
pub url: String
}
/// # Feature Configuration
@@ -46,7 +44,7 @@ pub struct RevoltFeatures {
/// Proxy service configuration
pub january: Feature,
/// Voice server configuration
pub voso: VoiceFeature,
pub livekit: VoiceFeature,
}
/// # Build Information
@@ -106,10 +104,9 @@ pub async fn root() -> Result<Json<RevoltConfig>> {
enabled: !config.hosts.january.is_empty(),
url: config.hosts.january,
},
voso: VoiceFeature {
enabled: !config.hosts.voso_legacy.is_empty(),
url: config.hosts.voso_legacy,
ws: config.hosts.voso_legacy_ws,
livekit: VoiceFeature {
enabled: !config.api.livekit.url.is_empty(),
url: config.api.livekit.url.to_string(),
},
},
ws: config.hosts.events,

View File

@@ -100,7 +100,7 @@ impl TestHarness {
}
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 payload: EventV1 = redis_kiss::decode_payload(&item).unwrap();

1
crates/voice-ingress/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

View 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"

View 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"]

View 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
View File

@@ -0,0 +1,2 @@
[toolchain]
channel = "stable"