forked from jmug/stoatchat
fix: include voice states in servercreate event
feat: call started system message in dms
This commit is contained in:
@@ -100,3 +100,8 @@ authifier = { version = "1.0.9", features = ["rocket_impl"] }
|
||||
|
||||
# RabbitMQ
|
||||
amqprs = { version = "1.7.0" }
|
||||
|
||||
# Voice
|
||||
livekit-api = "0.4.1"
|
||||
livekit-protocol = "0.3.6"
|
||||
livekit-runtime = { version = "0.3.1", features = ["tokio"] }
|
||||
@@ -120,6 +120,7 @@ pub enum EventV1 {
|
||||
server: Server,
|
||||
channels: Vec<Channel>,
|
||||
emojis: Vec<Emoji>,
|
||||
voice_states: Vec<ChannelVoiceState>
|
||||
},
|
||||
|
||||
/// Update existing server
|
||||
|
||||
@@ -108,6 +108,9 @@ pub mod tasks;
|
||||
mod amqp;
|
||||
pub use amqp::amqp::AMQP;
|
||||
|
||||
pub mod voice;
|
||||
|
||||
|
||||
/// Utility function to check if a boolean value is false
|
||||
pub fn if_false(t: &bool) -> bool {
|
||||
!t
|
||||
|
||||
@@ -106,6 +106,8 @@ auto_derived!(
|
||||
MessagePinned { id: String, by: String },
|
||||
#[serde(rename = "message_unpinned")]
|
||||
MessageUnpinned { id: String, by: String },
|
||||
#[serde(rename = "call_started")]
|
||||
CallStarted { by: String },
|
||||
}
|
||||
|
||||
/// Name and / or avatar override information
|
||||
@@ -658,6 +660,9 @@ impl Message {
|
||||
v0::SystemMessage::MessageUnpinned { by, .. } => {
|
||||
users.push(by.clone());
|
||||
}
|
||||
v0::SystemMessage::CallStarted { by } => {
|
||||
users.push(by.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
users
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use iso8601_timestamp::Timestamp;
|
||||
use revolt_permissions::{calculate_channel_permissions, ChannelPermission};
|
||||
use revolt_result::{create_error, Result};
|
||||
use crate::voice::get_channel_voice_state;
|
||||
|
||||
use crate::{
|
||||
events::client::EventV1, if_false, util::permissions::DatabasePermissionQuery, Channel,
|
||||
@@ -132,6 +133,14 @@ impl Member {
|
||||
|
||||
let emojis = db.fetch_emoji_by_parent_id(&server.id).await?;
|
||||
|
||||
let mut voice_states = Vec::new();
|
||||
|
||||
for channel in &channels {
|
||||
if let Ok(Some(voice_state)) = get_channel_voice_state(channel).await {
|
||||
voice_states.push(voice_state)
|
||||
}
|
||||
}
|
||||
|
||||
EventV1::ServerMemberJoin {
|
||||
id: server.id.clone(),
|
||||
user: user.id.clone(),
|
||||
@@ -148,6 +157,7 @@ impl Member {
|
||||
.map(|channel| channel.into())
|
||||
.collect(),
|
||||
emojis: emojis.into_iter().map(|emoji| emoji.into()).collect(),
|
||||
voice_states
|
||||
}
|
||||
.private(user.id.clone())
|
||||
.await;
|
||||
|
||||
@@ -545,6 +545,7 @@ impl From<crate::SystemMessage> for SystemMessage {
|
||||
crate::SystemMessage::UserRemove { id, by } => Self::UserRemove { id, by },
|
||||
crate::SystemMessage::MessagePinned { id, by } => Self::MessagePinned { id, by },
|
||||
crate::SystemMessage::MessageUnpinned { id, by } => Self::MessageUnpinned { id, by },
|
||||
crate::SystemMessage::CallStarted { by } => Self::CallStarted { by }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,25 @@
|
||||
use livekit_api::{
|
||||
access_token::{AccessToken, VideoGrants},
|
||||
services::room::{CreateRoomOptions, RoomClient, UpdateParticipantOptions},
|
||||
};
|
||||
use livekit_protocol::{ParticipantInfo, ParticipantPermission, Room};
|
||||
|
||||
use redis_kiss::{get_connection, redis::Pipeline, AsyncCommands};
|
||||
use revolt_config::config;
|
||||
use revolt_database::{Channel, User};
|
||||
use crate::models::{Channel, User};
|
||||
use revolt_models::v0::{self, PartialUserVoiceState, UserVoiceState};
|
||||
use revolt_permissions::{ChannelPermission, PermissionValue};
|
||||
use revolt_result::{create_error, Result, ToRevoltError};
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
|
||||
mod voice_client;
|
||||
pub use voice_client::VoiceClient;
|
||||
|
||||
pub async fn raise_if_in_voice(user: &User, target: &str) -> Result<()> {
|
||||
let mut conn = get_connection().await.to_internal_error()?;
|
||||
|
||||
if user.bot.is_some()
|
||||
// bots can be in as many voice channels as it wants so we just check if its already connected to the one its trying to connect to
|
||||
&& conn.sismember(format!("vc-{}", &user.id), target)
|
||||
&& conn.sismember(format!("vc:{}", &user.id), target)
|
||||
.await
|
||||
.to_internal_error()?
|
||||
{
|
||||
Err(create_error!(AlreadyConnected))
|
||||
} else if conn
|
||||
.scard::<_, u32>(format!("vc-{}", &user.id)) // check if the current vc set is empty
|
||||
.scard::<_, u32>(format!("vc:{}", &user.id)) // check if the current vc set is empty
|
||||
.await
|
||||
.to_internal_error()?
|
||||
> 0
|
||||
@@ -39,7 +36,7 @@ pub async fn get_user_voice_channel_in_server(
|
||||
) -> Result<Option<String>> {
|
||||
let mut conn = get_connection().await.to_internal_error()?;
|
||||
|
||||
let unique_key = format!("{}-{}", user_id, server_id);
|
||||
let unique_key = format!("{}:{}", user_id, server_id);
|
||||
|
||||
conn.get::<&str, Option<String>>(&unique_key)
|
||||
.await
|
||||
@@ -65,7 +62,7 @@ pub async fn create_voice_state(
|
||||
server_id: Option<&str>,
|
||||
user_id: &str,
|
||||
) -> Result<UserVoiceState> {
|
||||
let unique_key = format!("{}-{}", &user_id, server_id.unwrap_or(channel_id));
|
||||
let unique_key = format!("{}:{}", &user_id, server_id.unwrap_or(channel_id));
|
||||
|
||||
let voice_state = UserVoiceState {
|
||||
id: user_id.to_string(),
|
||||
@@ -76,22 +73,22 @@ pub async fn create_voice_state(
|
||||
};
|
||||
|
||||
Pipeline::new()
|
||||
.sadd(format!("vc-members-{channel_id}"), user_id)
|
||||
.sadd(format!("vc-{user_id}"), channel_id)
|
||||
.sadd(format!("vc_members:{channel_id}"), user_id)
|
||||
.sadd(format!("vc:{user_id}"), channel_id)
|
||||
.set(&unique_key, channel_id)
|
||||
.set(
|
||||
format!("is_publishing-{unique_key}"),
|
||||
format!("is_publishing:{unique_key}"),
|
||||
voice_state.is_publishing,
|
||||
)
|
||||
.set(
|
||||
format!("is_receiving-{unique_key}"),
|
||||
format!("is_receiving:{unique_key}"),
|
||||
voice_state.is_receiving,
|
||||
)
|
||||
.set(
|
||||
format!("screensharing-{unique_key}"),
|
||||
format!("screensharing:{unique_key}"),
|
||||
voice_state.screensharing,
|
||||
)
|
||||
.set(format!("camera-{unique_key}"), voice_state.camera)
|
||||
.set(format!("camera:{unique_key}"), voice_state.camera)
|
||||
.query_async(&mut get_connection().await.to_internal_error()?.into_inner())
|
||||
.await
|
||||
.to_internal_error()?;
|
||||
@@ -104,16 +101,16 @@ pub async fn delete_voice_state(
|
||||
server_id: Option<&str>,
|
||||
user_id: &str,
|
||||
) -> Result<()> {
|
||||
let unique_key = format!("{}-{}", &user_id, server_id.unwrap_or(channel_id));
|
||||
let unique_key = format!("{}:{}", &user_id, server_id.unwrap_or(channel_id));
|
||||
|
||||
Pipeline::new()
|
||||
.srem(format!("vc-members-{channel_id}"), user_id)
|
||||
.srem(format!("vc-{user_id}"), channel_id)
|
||||
.srem(format!("vc_members:{channel_id}"), user_id)
|
||||
.srem(format!("vc:{user_id}"), channel_id)
|
||||
.del(&[
|
||||
format!("is_publishing-{unique_key}"),
|
||||
format!("is_receiving-{unique_key}"),
|
||||
format!("screensharing-{unique_key}"),
|
||||
format!("camera-{unique_key}"),
|
||||
format!("is_publishing:{unique_key}"),
|
||||
format!("is_receiving:{unique_key}"),
|
||||
format!("screensharing:{unique_key}"),
|
||||
format!("camera:{unique_key}"),
|
||||
unique_key.clone(),
|
||||
])
|
||||
.query_async(&mut get_connection().await.to_internal_error()?.into_inner())
|
||||
@@ -161,24 +158,24 @@ pub async fn update_voice_state(
|
||||
user_id: &str,
|
||||
partial: &PartialUserVoiceState,
|
||||
) -> Result<()> {
|
||||
let unique_key = format!("{}-{}", &user_id, server_id.unwrap_or(channel_id));
|
||||
let unique_key = format!("{}:{}", &user_id, server_id.unwrap_or(channel_id));
|
||||
|
||||
let mut pipeline = Pipeline::new();
|
||||
|
||||
if let Some(camera) = &partial.camera {
|
||||
pipeline.set(format!("camera-{unique_key}"), camera);
|
||||
pipeline.set(format!("camera:{unique_key}"), camera);
|
||||
};
|
||||
|
||||
if let Some(is_publishing) = &partial.is_publishing {
|
||||
pipeline.set(format!("is_publishing-{unique_key}"), is_publishing);
|
||||
pipeline.set(format!("is_publishing:{unique_key}"), is_publishing);
|
||||
}
|
||||
|
||||
if let Some(is_receiving) = &partial.is_receiving {
|
||||
pipeline.set(format!("is_receiving-{unique_key}"), is_receiving);
|
||||
pipeline.set(format!("is_receiving:{unique_key}"), is_receiving);
|
||||
}
|
||||
|
||||
if let Some(screensharing) = &partial.screensharing {
|
||||
pipeline.set(format!("screensharing-{unique_key}"), screensharing);
|
||||
pipeline.set(format!("screensharing:{unique_key}"), screensharing);
|
||||
}
|
||||
|
||||
pipeline
|
||||
@@ -193,7 +190,7 @@ pub async fn get_voice_channel_members(channel_id: &str) -> Result<Vec<String>>
|
||||
get_connection()
|
||||
.await
|
||||
.to_internal_error()?
|
||||
.smembers::<_, Vec<String>>(format!("vc-members-{}", channel_id))
|
||||
.smembers::<_, Vec<String>>(format!("vc_members:{}", channel_id))
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
@@ -203,16 +200,16 @@ pub async fn get_voice_state(
|
||||
server_id: Option<&str>,
|
||||
user_id: &str,
|
||||
) -> Result<Option<UserVoiceState>> {
|
||||
let unique_key = format!("{}-{user_id}", server_id.unwrap_or(channel_id));
|
||||
let unique_key = format!("{}:{user_id}", server_id.unwrap_or(channel_id));
|
||||
|
||||
let (is_publishing, is_receiving, screensharing, camera) = get_connection()
|
||||
.await
|
||||
.to_internal_error()?
|
||||
.mget::<_, (Option<bool>, Option<bool>, Option<bool>, Option<bool>)>(&[
|
||||
format!("is_publishing-{unique_key}"),
|
||||
format!("is_receiving-{unique_key}"),
|
||||
format!("screensharing-{unique_key}"),
|
||||
format!("camera-{unique_key}"),
|
||||
format!("is_publishing:{unique_key}"),
|
||||
format!("is_receiving:{unique_key}"),
|
||||
format!("screensharing:{unique_key}"),
|
||||
format!("camera:{unique_key}"),
|
||||
])
|
||||
.await
|
||||
.to_internal_error()?;
|
||||
@@ -231,92 +228,32 @@ pub async fn get_voice_state(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VoiceClient {
|
||||
rooms: RoomClient,
|
||||
api_key: String,
|
||||
api_secret: String,
|
||||
}
|
||||
pub async fn get_channel_voice_state(channel: &Channel) -> Result<Option<v0::ChannelVoiceState>> {
|
||||
let members = get_voice_channel_members(channel.id()).await?;
|
||||
|
||||
impl VoiceClient {
|
||||
pub fn new(url: String, api_key: String, api_secret: String) -> Self {
|
||||
Self {
|
||||
rooms: RoomClient::with_api_key(&url, &api_key, &api_secret),
|
||||
api_key,
|
||||
api_secret,
|
||||
let server = match channel {
|
||||
Channel::TextChannel { server, .. } | Channel::VoiceChannel { server, .. } => Some(server.as_str()),
|
||||
_ => None
|
||||
};
|
||||
|
||||
if !members.is_empty() {
|
||||
let mut participants = Vec::with_capacity(members.len());
|
||||
|
||||
for user_id in members {
|
||||
if let Some(voice_state) = get_voice_state(channel.id(), server, &user_id).await? {
|
||||
participants.push(voice_state);
|
||||
} else {
|
||||
log::info!("Voice state not found but member in voice channel members, removing.");
|
||||
|
||||
delete_voice_state(channel.id(), server, &user_id).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn from_revolt_config() -> Self {
|
||||
let config = config().await;
|
||||
|
||||
Self::new(
|
||||
config.hosts.livekit,
|
||||
config.api.livekit.key,
|
||||
config.api.livekit.secret,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn create_token(
|
||||
&self,
|
||||
user: &User,
|
||||
permissions: PermissionValue,
|
||||
channel: &Channel,
|
||||
) -> Result<String> {
|
||||
let allowed_sources = get_allowed_sources(permissions);
|
||||
|
||||
AccessToken::with_api_key(&self.api_key, &self.api_secret)
|
||||
.with_name(&format!("{}#{}", user.username, user.discriminator))
|
||||
.with_identity(&user.id)
|
||||
.with_metadata(&serde_json::to_string(&user).to_internal_error()?)
|
||||
.with_ttl(Duration::from_secs(10))
|
||||
.with_grants(VideoGrants {
|
||||
room_join: true,
|
||||
can_publish_sources: allowed_sources.into_iter().map(ToString::to_string).collect(),
|
||||
can_subscribe: permissions.has_channel_permission(ChannelPermission::Listen),
|
||||
room: channel.id().to_string(),
|
||||
..Default::default()
|
||||
})
|
||||
.to_jwt()
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn create_room(&self, channel: &Channel) -> Result<Room> {
|
||||
let voice = channel
|
||||
.voice()
|
||||
.ok_or_else(|| create_error!(NotAVoiceChannel))?;
|
||||
|
||||
self.rooms
|
||||
.create_room(
|
||||
channel.id(),
|
||||
CreateRoomOptions {
|
||||
max_participants: voice.max_users.unwrap_or(u32::MAX),
|
||||
empty_timeout: 5 * 60, // 5 minutes
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn update_permissions(
|
||||
&self,
|
||||
user: &User,
|
||||
channel_id: &str,
|
||||
new_permissions: ParticipantPermission,
|
||||
) -> Result<ParticipantInfo> {
|
||||
self.rooms
|
||||
.update_participant(
|
||||
channel_id,
|
||||
&user.id,
|
||||
UpdateParticipantOptions {
|
||||
permission: Some(new_permissions),
|
||||
attributes: HashMap::new(),
|
||||
name: "".to_string(),
|
||||
metadata: "".to_string(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.to_internal_error()
|
||||
Ok(Some(v0::ChannelVoiceState {
|
||||
id: channel.id().to_string(),
|
||||
participants,
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
108
crates/core/database/src/voice/voice_client.rs
Normal file
108
crates/core/database/src/voice/voice_client.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
use livekit_api::{
|
||||
access_token::{AccessToken, VideoGrants},
|
||||
services::room::{CreateRoomOptions, RoomClient, UpdateParticipantOptions},
|
||||
};
|
||||
use livekit_protocol::{ParticipantInfo, ParticipantPermission, Room};
|
||||
use revolt_config::config;
|
||||
use crate::models::{Channel, User};
|
||||
use revolt_models::v0;
|
||||
use revolt_permissions::{ChannelPermission, PermissionValue};
|
||||
use revolt_result::{create_error, Result, ToRevoltError};
|
||||
use std::{borrow::Cow, collections::HashMap, time::Duration};
|
||||
|
||||
use super::get_allowed_sources;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VoiceClient {
|
||||
rooms: RoomClient,
|
||||
api_key: String,
|
||||
api_secret: String,
|
||||
}
|
||||
|
||||
impl VoiceClient {
|
||||
pub fn new(url: String, api_key: String, api_secret: String) -> Self {
|
||||
Self {
|
||||
rooms: RoomClient::with_api_key(&url, &api_key, &api_secret),
|
||||
api_key,
|
||||
api_secret,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn from_revolt_config() -> Self {
|
||||
let config = config().await;
|
||||
|
||||
Self::new(
|
||||
config.hosts.livekit,
|
||||
config.api.livekit.key,
|
||||
config.api.livekit.secret,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn create_token(
|
||||
&self,
|
||||
user: &User,
|
||||
permissions: PermissionValue,
|
||||
channel: &Channel,
|
||||
) -> Result<String> {
|
||||
let allowed_sources = get_allowed_sources(permissions);
|
||||
|
||||
AccessToken::with_api_key(&self.api_key, &self.api_secret)
|
||||
.with_name(&format!("{}#{}", user.username, user.discriminator))
|
||||
.with_identity(&user.id)
|
||||
.with_metadata(&serde_json::to_string(&user).to_internal_error()?)
|
||||
.with_ttl(Duration::from_secs(10))
|
||||
.with_grants(VideoGrants {
|
||||
room_join: true,
|
||||
can_publish: true,
|
||||
can_publish_sources: vec![], // allowed_sources.into_iter().map(ToString::to_string).collect(),
|
||||
can_subscribe: permissions.has_channel_permission(ChannelPermission::Listen),
|
||||
room: channel.id().to_string(),
|
||||
..Default::default()
|
||||
})
|
||||
.to_jwt()
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn create_room(&self, channel: &Channel) -> Result<Room> {
|
||||
let voice = match channel {
|
||||
Channel::DirectMessage { .. } | Channel::VoiceChannel { .. } => Some(Cow::Owned(v0::VoiceInformation::default())),
|
||||
Channel::TextChannel { voice: Some(voice), .. } => Some(Cow::Borrowed(voice)),
|
||||
_ => None
|
||||
}
|
||||
|
||||
.ok_or_else(|| create_error!(NotAVoiceChannel))?;
|
||||
|
||||
self.rooms
|
||||
.create_room(
|
||||
channel.id(),
|
||||
CreateRoomOptions {
|
||||
max_participants: voice.max_users.unwrap_or(u32::MAX),
|
||||
empty_timeout: 5 * 60, // 5 minutes
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn update_permissions(
|
||||
&self,
|
||||
user: &User,
|
||||
channel_id: &str,
|
||||
new_permissions: ParticipantPermission,
|
||||
) -> Result<ParticipantInfo> {
|
||||
self.rooms
|
||||
.update_participant(
|
||||
channel_id,
|
||||
&user.id,
|
||||
UpdateParticipantOptions {
|
||||
permission: Some(new_permissions),
|
||||
attributes: HashMap::new(),
|
||||
name: "".to_string(),
|
||||
metadata: "".to_string(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
}
|
||||
@@ -134,6 +134,8 @@ auto_derived!(
|
||||
MessagePinned { id: String, by: String },
|
||||
#[serde(rename = "message_unpinned")]
|
||||
MessageUnpinned { id: String, by: String },
|
||||
#[serde(rename = "call_started")]
|
||||
CallStarted { by: String },
|
||||
}
|
||||
|
||||
/// Name and / or avatar override information
|
||||
@@ -438,6 +440,7 @@ impl From<SystemMessage> for String {
|
||||
}
|
||||
SystemMessage::MessagePinned { .. } => "Message pinned.".to_string(),
|
||||
SystemMessage::MessageUnpinned { .. } => "Message unpinned.".to_string(),
|
||||
SystemMessage::CallStarted { .. } => "Call started.".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,6 +218,7 @@ pub trait ToRevoltError<T> {
|
||||
}
|
||||
|
||||
impl<T, E: std::fmt::Debug> ToRevoltError<T> for Result<T, E> {
|
||||
#[track_caller]
|
||||
fn to_internal_error(self) -> Result<T, Error> {
|
||||
self
|
||||
.inspect_err(|e| log::error!("{e:?}"))
|
||||
@@ -233,6 +234,7 @@ impl<T, E: std::fmt::Debug> ToRevoltError<T> for Result<T, E> {
|
||||
}
|
||||
|
||||
impl<T> ToRevoltError<T> for Option<T> {
|
||||
#[track_caller]
|
||||
fn to_internal_error(self) -> Result<T, Error> {
|
||||
self.ok_or_else(|| {
|
||||
let loc = Location::caller();
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
[package]
|
||||
name = "revolt-voice"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
# voice
|
||||
livekit-api = "0.4.1"
|
||||
livekit-protocol = "0.3.6"
|
||||
|
||||
# core
|
||||
revolt-result = { path = "../result" }
|
||||
revolt-models = { path = "../models" }
|
||||
revolt-config = { path = "../config" }
|
||||
revolt-database = { path = "../database" }
|
||||
revolt-permissions = { path = "../permissions" }
|
||||
|
||||
# async
|
||||
futures = "0.3.21"
|
||||
async-std = { version = "1.8.0", features = [
|
||||
"tokio1",
|
||||
"tokio02",
|
||||
"attributes",
|
||||
] }
|
||||
|
||||
# util
|
||||
redis-kiss = "0.1.4"
|
||||
|
||||
# serde
|
||||
serde_json = "1.0.79"
|
||||
serde = "1.0.136"
|
||||
Reference in New Issue
Block a user