mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
feat: - track call length
- move voice and video limits to config - seperate VoiceInformation into model and db model - fix build scripts
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
use std::{borrow::Cow, collections::HashMap};
|
||||
|
||||
use revolt_config::config;
|
||||
use revolt_models::v0::{self, MessageAuthor, VoiceInformation};
|
||||
use revolt_models::v0::{self, MessageAuthor};
|
||||
use revolt_permissions::OverrideField;
|
||||
use revolt_result::Result;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -141,6 +141,13 @@ auto_derived!(
|
||||
nsfw: bool,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct VoiceInformation {
|
||||
/// Maximium amount of users allowed in the voice channel at once
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub max_users: Option<u32>
|
||||
}
|
||||
);
|
||||
|
||||
auto_derived!(
|
||||
@@ -167,7 +174,7 @@ auto_derived!(
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub last_message_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub voice: Option<VoiceInformation>
|
||||
pub voice: Option<VoiceInformation>,
|
||||
}
|
||||
|
||||
/// Optional fields on channel object
|
||||
@@ -226,7 +233,7 @@ impl Channel {
|
||||
default_permissions: None,
|
||||
role_permissions: HashMap::new(),
|
||||
nsfw: data.nsfw.unwrap_or(false),
|
||||
voice: data.voice
|
||||
voice: data.voice.map(|voice| voice.into())
|
||||
},
|
||||
v0::LegacyServerChannelType::Voice => Channel::TextChannel {
|
||||
id: id.clone(),
|
||||
@@ -238,7 +245,7 @@ impl Channel {
|
||||
default_permissions: None,
|
||||
role_permissions: HashMap::new(),
|
||||
nsfw: data.nsfw.unwrap_or(false),
|
||||
voice: Some(data.voice.unwrap_or_default())
|
||||
voice: Some(data.voice.unwrap_or_default().into())
|
||||
},
|
||||
};
|
||||
|
||||
@@ -455,7 +462,7 @@ impl Channel {
|
||||
/// Gets this channel's voice information
|
||||
pub fn voice(&self) -> Option<Cow<VoiceInformation>> {
|
||||
match self {
|
||||
Self::DirectMessage { .. } | Channel::VoiceChannel { .. } => Some(Cow::Owned(v0::VoiceInformation::default())),
|
||||
Self::DirectMessage { .. } | Channel::VoiceChannel { .. } => Some(Cow::Owned(VoiceInformation::default())),
|
||||
Self::TextChannel { voice: Some(voice), .. } => Some(Cow::Borrowed(voice)),
|
||||
_ => None
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ auto_derived!(
|
||||
#[serde(rename = "message_unpinned")]
|
||||
MessageUnpinned { id: String, by: String },
|
||||
#[serde(rename = "call_started")]
|
||||
CallStarted { by: String },
|
||||
CallStarted { by: String, finished_at: Option<Timestamp> },
|
||||
}
|
||||
|
||||
/// Name and / or avatar override information
|
||||
@@ -833,7 +833,7 @@ impl Message {
|
||||
v0::SystemMessage::MessageUnpinned { by, .. } => {
|
||||
users.push(by.clone());
|
||||
}
|
||||
v0::SystemMessage::CallStarted { by } => {
|
||||
v0::SystemMessage::CallStarted { by, .. } => {
|
||||
users.push(by.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,7 +200,7 @@ impl From<crate::Channel> for Channel {
|
||||
default_permissions,
|
||||
role_permissions,
|
||||
nsfw,
|
||||
voice,
|
||||
voice: voice.map(|voice| voice.into()),
|
||||
},
|
||||
crate::Channel::VoiceChannel {
|
||||
id,
|
||||
@@ -283,7 +283,7 @@ impl From<Channel> for crate::Channel {
|
||||
default_permissions,
|
||||
role_permissions,
|
||||
nsfw,
|
||||
voice,
|
||||
voice: voice.map(|voice| voice.into()),
|
||||
},
|
||||
Channel::VoiceChannel {
|
||||
id,
|
||||
@@ -321,7 +321,7 @@ impl From<crate::PartialChannel> for PartialChannel {
|
||||
role_permissions: value.role_permissions,
|
||||
default_permissions: value.default_permissions,
|
||||
last_message_id: value.last_message_id,
|
||||
voice: value.voice
|
||||
voice: value.voice.map(|voice| voice.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -339,7 +339,7 @@ impl From<PartialChannel> for crate::PartialChannel {
|
||||
role_permissions: value.role_permissions,
|
||||
default_permissions: value.default_permissions,
|
||||
last_message_id: value.last_message_id,
|
||||
voice: value.voice
|
||||
voice: value.voice.map(|voice| voice.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -551,7 +551,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 }
|
||||
crate::SystemMessage::CallStarted { by, finished_at } => Self::CallStarted { by, finished_at }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1406,3 +1406,19 @@ impl From<FieldsMessage> for crate::FieldsMessage {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<VoiceInformation> for crate::VoiceInformation {
|
||||
fn from(value: VoiceInformation) -> Self {
|
||||
crate::VoiceInformation {
|
||||
max_users: value.max_users
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<crate::VoiceInformation> for VoiceInformation {
|
||||
fn from(value: crate::VoiceInformation) -> Self {
|
||||
VoiceInformation {
|
||||
max_users: value.max_users
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
|
||||
use crate::{
|
||||
events::client::EventV1,
|
||||
models::{Channel, User},
|
||||
util::{permissions::DatabasePermissionQuery, reference::Reference},
|
||||
Database, Server,
|
||||
};
|
||||
use livekit_protocol::ParticipantPermission;
|
||||
use redis_kiss::{get_connection as _get_connection, Conn, redis::Pipeline, AsyncCommands};
|
||||
use crate::{events::client::EventV1, models::{Channel, User}, util::{permissions::DatabasePermissionQuery, reference::Reference}, Database, Server};
|
||||
use redis_kiss::{get_connection as _get_connection, redis::Pipeline, AsyncCommands, Conn};
|
||||
use revolt_config::FeaturesLimits;
|
||||
use revolt_models::v0::{self, PartialUserVoiceState, UserVoiceState};
|
||||
use revolt_permissions::{calculate_channel_permissions, ChannelPermission, PermissionValue};
|
||||
use revolt_result::{create_error, Result, ToRevoltError};
|
||||
@@ -59,7 +64,35 @@ pub async fn get_user_voice_channels(user_id: &str) -> Result<Vec<String>> {
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn set_user_moved_from_voice(old_channel: &str, new_channel: &str, user_id: &str) -> Result<()> {
|
||||
pub async fn set_user_moved_from_voice(
|
||||
old_channel: &str,
|
||||
new_channel: &str,
|
||||
user_id: &str,
|
||||
) -> Result<()> {
|
||||
get_connection()
|
||||
.await?
|
||||
.set_ex(
|
||||
format!("moved_from:{user_id}:{old_channel}"),
|
||||
new_channel,
|
||||
10,
|
||||
)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn get_user_moved_from_voice(channel_id: &str, user_id: &str) -> Result<Option<String>> {
|
||||
get_connection()
|
||||
.await?
|
||||
.get(format!("moved_from:{user_id}:{channel_id}"))
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn set_user_moved_to_voice(
|
||||
new_channel: &str,
|
||||
old_channel: &str,
|
||||
user_id: &str,
|
||||
) -> Result<()> {
|
||||
get_connection()
|
||||
.await?
|
||||
.set_ex(format!("moved_to:{user_id}:{new_channel}"), old_channel, 10)
|
||||
@@ -67,7 +100,7 @@ pub async fn set_user_moved_from_voice(old_channel: &str, new_channel: &str, use
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn get_user_moved_from_voice(channel_id: &str, user_id: &str) -> Result<Option<String>> {
|
||||
pub async fn get_user_moved_to_voice(channel_id: &str, user_id: &str) -> Result<Option<String>> {
|
||||
get_connection()
|
||||
.await?
|
||||
.get(format!("moved_to:{user_id}:{channel_id}"))
|
||||
@@ -78,7 +111,7 @@ pub async fn get_user_moved_from_voice(channel_id: &str, user_id: &str) -> Resul
|
||||
pub async fn is_in_voice_channel(user_id: &str, channel_id: &str) -> Result<bool> {
|
||||
get_connection()
|
||||
.await?
|
||||
.sismember(format!("vc:{}", user_id), channel_id)
|
||||
.sismember(format!("vc:{user_id}"), channel_id)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
@@ -89,21 +122,21 @@ pub async fn get_user_voice_channel_in_server(
|
||||
) -> Result<Option<String>> {
|
||||
let mut conn = get_connection().await?;
|
||||
|
||||
let unique_key = format!("{}:{}", user_id, server_id);
|
||||
let unique_key = format!("{user_id}:{server_id}");
|
||||
|
||||
conn.get::<&str, Option<String>>(&unique_key)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub fn get_allowed_sources(permissions: PermissionValue) -> Vec<&'static str> {
|
||||
pub fn get_allowed_sources(limits: &FeaturesLimits, permissions: PermissionValue) -> Vec<&'static str> {
|
||||
let mut allowed_sources = Vec::new();
|
||||
|
||||
if permissions.has(ChannelPermission::Speak as u64) {
|
||||
allowed_sources.push("microphone")
|
||||
};
|
||||
|
||||
if permissions.has(ChannelPermission::Video as u64) {
|
||||
if permissions.has(ChannelPermission::Video as u64) && limits.video {
|
||||
allowed_sources.extend(["camera", "screen_share", "screen_share_audio"]);
|
||||
};
|
||||
|
||||
@@ -238,14 +271,10 @@ pub async fn update_voice_state(
|
||||
pub async fn get_voice_channel_members(channel_id: &str) -> Result<Option<Vec<String>>> {
|
||||
get_connection()
|
||||
.await?
|
||||
.smembers::<_, Option<Vec<String>>>(format!("vc_members:{}", channel_id))
|
||||
.smembers::<_, Option<Vec<String>>>(format!("vc_members:{channel_id}"))
|
||||
.await
|
||||
.to_internal_error()
|
||||
.map(|opt| opt.and_then(|v| if v.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(v)
|
||||
}))
|
||||
.map(|opt| opt.and_then(|v| if v.is_empty() { None } else { Some(v) }))
|
||||
}
|
||||
|
||||
pub async fn get_voice_state(
|
||||
@@ -294,13 +323,13 @@ pub async fn get_channel_voice_state(channel: &Channel) -> Result<Option<v0::Cha
|
||||
} else {
|
||||
log::info!("Voice state not found but member in voice channel members, removing.");
|
||||
|
||||
delete_voice_state(channel.id(), server, &user_id).await?;
|
||||
delete_voice_state(channel.id(), server, &user_id).await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Some(v0::ChannelVoiceState {
|
||||
id: channel.id().to_string(),
|
||||
participants
|
||||
participants,
|
||||
}))
|
||||
} else {
|
||||
Ok(None)
|
||||
@@ -319,39 +348,71 @@ pub async fn move_user(user: &str, from: &str, to: &str) -> Result<()> {
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
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?.unwrap();
|
||||
|
||||
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?;
|
||||
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?;
|
||||
|
||||
sync_user_voice_permissions(db, voice_client, &node, &user, channel, server, role_id).await?;
|
||||
};
|
||||
sync_user_voice_permissions(db, voice_client, &node, &user, channel, server, role_id)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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<()> {
|
||||
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 server_id: Option<&str> = server.as_ref().map(|s| s.id.as_str());
|
||||
|
||||
let member = match server_id {
|
||||
Some(server_id) => Some(Reference::from_unchecked(user.id.clone()).as_member(db, server_id).await?),
|
||||
None => None
|
||||
Some(server_id) => Some(
|
||||
Reference::from_unchecked(user.id.clone())
|
||||
.as_member(db, server_id)
|
||||
.await?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
|
||||
if role_id.is_none_or(|role_id| member.as_ref().is_none_or(|member| member.roles.iter().any(|r| r == role_id))) {
|
||||
let voice_state = get_voice_state(channel_id, server_id, &user.id).await?.unwrap();
|
||||
if role_id.is_none_or(|role_id| {
|
||||
member
|
||||
.as_ref()
|
||||
.is_none_or(|member| member.roles.iter().any(|r| r == role_id))
|
||||
}) {
|
||||
let voice_state = get_voice_state(channel_id, server_id, &user.id)
|
||||
.await?
|
||||
.unwrap();
|
||||
|
||||
let mut query = DatabasePermissionQuery::new(db, user)
|
||||
.channel(channel)
|
||||
.user(user);
|
||||
|
||||
if let (Some(server), Some(member)) = (server, member.as_ref()) {
|
||||
query = query.member(member).server(server)
|
||||
query = query.member(member).server(server)
|
||||
}
|
||||
|
||||
let permissions = calculate_channel_permissions(&mut query).await;
|
||||
let limits = user.limits().await;
|
||||
|
||||
let mut update_event = PartialUserVoiceState {
|
||||
id: Some(user.id.clone()),
|
||||
@@ -360,7 +421,7 @@ pub async fn sync_user_voice_permissions(db: &Database, voice_client: &VoiceClie
|
||||
|
||||
let before = update_event.clone();
|
||||
|
||||
let can_video = permissions.has_channel_permission(ChannelPermission::Video);
|
||||
let can_video = limits.video && permissions.has_channel_permission(ChannelPermission::Video);
|
||||
let can_speak = permissions.has_channel_permission(ChannelPermission::Speak);
|
||||
let can_listen = permissions.has_channel_permission(ChannelPermission::Listen);
|
||||
|
||||
@@ -370,21 +431,49 @@ pub async fn sync_user_voice_permissions(db: &Database, voice_client: &VoiceClie
|
||||
|
||||
update_voice_state(channel_id, server_id, &user.id, &update_event).await?;
|
||||
|
||||
voice_client.update_permissions(node, user, channel_id, ParticipantPermission {
|
||||
can_subscribe: can_listen,
|
||||
can_publish: can_speak,
|
||||
can_publish_data: can_speak,
|
||||
..Default::default()
|
||||
}).await?;
|
||||
voice_client
|
||||
.update_permissions(
|
||||
node,
|
||||
user,
|
||||
channel_id,
|
||||
ParticipantPermission {
|
||||
can_subscribe: can_listen,
|
||||
can_publish: can_speak,
|
||||
can_publish_data: can_speak,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
if update_event != before {
|
||||
EventV1::UserVoiceStateUpdate {
|
||||
id: user.id.clone(),
|
||||
channel_id: channel_id.to_string(),
|
||||
data: update_event
|
||||
}.p(channel_id.to_string()).await;
|
||||
data: update_event,
|
||||
}
|
||||
.p(channel_id.to_string())
|
||||
.await;
|
||||
};
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn set_channel_call_started_system_message(
|
||||
channel_id: &str,
|
||||
message_id: &str,
|
||||
) -> Result<()> {
|
||||
get_connection()
|
||||
.await?
|
||||
.set(format!("call_started_message:{channel_id}"), message_id)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn take_channel_call_started_system_message(channel_id: &str) -> Result<Option<String>> {
|
||||
get_connection()
|
||||
.await?
|
||||
.get_del(format!("call_started_message:{channel_id}"))
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
@@ -1,27 +1,25 @@
|
||||
use crate::models::{Channel, User};
|
||||
use livekit_api::{
|
||||
access_token::{AccessToken, VideoGrants},
|
||||
services::room::{CreateRoomOptions, RoomClient as InnerRoomClient, UpdateParticipantOptions},
|
||||
};
|
||||
use livekit_protocol::{ParticipantInfo, ParticipantPermission, Room};
|
||||
use revolt_config::{config, LiveKitNode};
|
||||
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 std::{collections::HashMap, time::Duration};
|
||||
|
||||
use super::get_allowed_sources;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RoomClient {
|
||||
pub client: InnerRoomClient,
|
||||
pub node: LiveKitNode
|
||||
pub node: LiveKitNode,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct VoiceClient {
|
||||
pub rooms: HashMap<String, RoomClient>
|
||||
pub rooms: HashMap<String, RoomClient>,
|
||||
}
|
||||
|
||||
impl VoiceClient {
|
||||
@@ -29,16 +27,20 @@ impl VoiceClient {
|
||||
Self {
|
||||
rooms: nodes
|
||||
.into_iter()
|
||||
.map(|(name, node)|
|
||||
.map(|(name, node)| {
|
||||
(
|
||||
name,
|
||||
RoomClient {
|
||||
client: InnerRoomClient::with_api_key(&node.url, &node.key, &node.secret),
|
||||
node
|
||||
}
|
||||
client: InnerRoomClient::with_api_key(
|
||||
&node.url,
|
||||
&node.key,
|
||||
&node.secret,
|
||||
),
|
||||
node,
|
||||
},
|
||||
)
|
||||
)
|
||||
.collect()
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,11 +55,12 @@ impl VoiceClient {
|
||||
}
|
||||
|
||||
pub fn get_node(&self, name: &str) -> Result<&RoomClient> {
|
||||
self.rooms.get(name)
|
||||
self.rooms
|
||||
.get(name)
|
||||
.ok_or_else(|| create_error!(UnknownNode))
|
||||
}
|
||||
|
||||
pub fn create_token(
|
||||
pub async fn create_token(
|
||||
&self,
|
||||
node: &str,
|
||||
user: &User,
|
||||
@@ -66,7 +69,8 @@ impl VoiceClient {
|
||||
) -> Result<String> {
|
||||
let room = self.get_node(node)?;
|
||||
|
||||
let allowed_sources = get_allowed_sources(permissions);
|
||||
let limits = user.limits().await;
|
||||
let allowed_sources = get_allowed_sources(&limits, permissions);
|
||||
|
||||
AccessToken::with_api_key(&room.node.key, &room.node.secret)
|
||||
.with_name(&format!("{}#{}", user.username, user.discriminator))
|
||||
@@ -76,7 +80,10 @@ impl VoiceClient {
|
||||
.with_grants(VideoGrants {
|
||||
room_join: true,
|
||||
can_publish: true,
|
||||
can_publish_sources: allowed_sources.into_iter().map(ToString::to_string).collect(),
|
||||
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()
|
||||
@@ -88,12 +95,9 @@ impl VoiceClient {
|
||||
pub async fn create_room(&self, node: &str, channel: &Channel) -> Result<Room> {
|
||||
let room = self.get_node(node)?;
|
||||
|
||||
let voice = match channel {
|
||||
Channel::DirectMessage { .. } | Channel::Group { .. } => Some(Cow::Owned(v0::VoiceInformation::default())),
|
||||
Channel::TextChannel { voice: Some(voice), .. } => Some(Cow::Borrowed(voice)),
|
||||
_ => None
|
||||
}
|
||||
.ok_or_else(|| create_error!(NotAVoiceChannel))?;
|
||||
let voice = channel
|
||||
.voice()
|
||||
.ok_or_else(|| create_error!(NotAVoiceChannel))?;
|
||||
|
||||
room.client
|
||||
.create_room(
|
||||
@@ -135,7 +139,8 @@ impl VoiceClient {
|
||||
pub async fn remove_user(&self, node: &str, user_id: &str, channel_id: &str) -> Result<()> {
|
||||
let room = self.get_node(node)?;
|
||||
|
||||
room.client.remove_participant(channel_id, user_id)
|
||||
room.client
|
||||
.remove_participant(channel_id, user_id)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user