feat: reimplement voice chat

This commit is contained in:
Paul Makles
2022-04-21 20:34:43 +01:00
parent 350bd9c0be
commit 180ac67056
3 changed files with 86 additions and 6 deletions

View File

@@ -99,7 +99,7 @@ pub async fn edit_bot(
}
}
db.update_bot(&bot.id, &partial, remove.unwrap_or_else(Vec::new))
db.update_bot(&bot.id, &partial, remove.unwrap_or_default())
.await?;
bot.apply_options(partial);

View File

@@ -36,6 +36,6 @@ pub async fn fetch_public_bot(db: &Db, target: Ref) -> Result<Json<PublicBot>> {
id: bot.id,
username: user.username,
avatar: user.avatar,
description: user.profile.map(|p| p.content).flatten(),
description: user.profile.and_then(|p| p.content),
}))
}

View File

@@ -1,4 +1,9 @@
use revolt_quark::{models::User, Ref, Result};
use revolt_quark::{
models::{Channel, User},
perms,
variables::delta::{USE_VOSO, VOSO_MANAGE_TOKEN, VOSO_URL},
Db, Error, Permission, Ref, Result,
};
use rocket::serde::json::Json;
use serde::{Deserialize, Serialize};
@@ -14,7 +19,82 @@ pub struct CreateVoiceUserResponse {
///
/// Asks the voice server for a token to join the call.
#[openapi(tag = "Voice")]
#[post("/<_target>/join_call")]
pub async fn req(_user: User, _target: Ref) -> Result<Json<CreateVoiceUserResponse>> {
unimplemented!()
#[post("/<target>/join_call")]
pub async fn req(db: &Db, user: User, target: Ref) -> Result<Json<CreateVoiceUserResponse>> {
let channel = target.as_channel(db).await?;
let mut permissions = perms(&user).channel(&channel);
permissions
.throw_permission_and_view_channel(db, Permission::Connect)
.await?;
if !*USE_VOSO {
return Err(Error::VosoUnavailable);
}
match channel {
Channel::SavedMessages { .. } | Channel::TextChannel { .. } => {
return Err(Error::CannotJoinCall)
}
_ => {}
}
// To join a call:
// - Check if the room exists.
// - If not, create it.
let client = reqwest::Client::new();
let result = client
.get(&format!("{}/room/{}", *VOSO_URL, channel.id()))
.header(
reqwest::header::AUTHORIZATION,
VOSO_MANAGE_TOKEN.to_string(),
)
.send()
.await;
match result {
Err(_) => return Err(Error::VosoUnavailable),
Ok(result) => match result.status() {
reqwest::StatusCode::OK => (),
reqwest::StatusCode::NOT_FOUND => {
if (client
.post(&format!("{}/room/{}", *VOSO_URL, channel.id()))
.header(
reqwest::header::AUTHORIZATION,
VOSO_MANAGE_TOKEN.to_string(),
)
.send()
.await)
.is_err()
{
return Err(Error::VosoUnavailable);
}
}
_ => return Err(Error::VosoUnavailable),
},
}
// Then create a user for the room.
if let Ok(response) = client
.post(&format!(
"{}/room/{}/user/{}",
*VOSO_URL,
channel.id(),
user.id
))
.header(
reqwest::header::AUTHORIZATION,
VOSO_MANAGE_TOKEN.to_string(),
)
.send()
.await
{
response
.json()
.await
.map_err(|_| Error::InvalidOperation)
.map(Json)
} else {
Err(Error::VosoUnavailable)
}
}