mirror of
https://github.com/stoatchat/stoatchat.git
synced 2026-07-14 13:36:59 +00:00
feat: implement missing permission syncs
This commit is contained in:
@@ -254,6 +254,11 @@ pub enum EventV1 {
|
||||
id: String,
|
||||
user: String,
|
||||
},
|
||||
VoiceChannelMove {
|
||||
user: String,
|
||||
from: String,
|
||||
to: String,
|
||||
},
|
||||
UserVoiceStateUpdate {
|
||||
id: String,
|
||||
channel_id: String,
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
|
||||
use redis_kiss::{get_connection, redis::Pipeline, AsyncCommands};
|
||||
use crate::models::{Channel, User};
|
||||
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, Role, Server};
|
||||
use revolt_models::v0::{self, PartialUserVoiceState, UserVoiceState};
|
||||
use revolt_permissions::{ChannelPermission, PermissionValue};
|
||||
use revolt_permissions::{calculate_channel_permissions, ChannelPermission, PermissionValue};
|
||||
use revolt_result::{create_error, Result, ToRevoltError};
|
||||
|
||||
mod voice_client;
|
||||
pub use voice_client::VoiceClient;
|
||||
|
||||
async fn get_connection() -> Result<Conn> {
|
||||
_get_connection().await.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn raise_if_in_voice(user: &User, target: &str) -> Result<()> {
|
||||
let mut conn = get_connection().await.to_internal_error()?;
|
||||
let mut conn = get_connection().await?;
|
||||
|
||||
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
|
||||
@@ -30,11 +35,27 @@ pub async fn raise_if_in_voice(user: &User, target: &str) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_user_voice_channels(user_id: &str) -> Result<Vec<String>> {
|
||||
get_connection()
|
||||
.await?
|
||||
.smembers(format!("vc:{user_id}"))
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
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)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn get_user_voice_channel_in_server(
|
||||
user_id: &str,
|
||||
server_id: &str,
|
||||
) -> Result<Option<String>> {
|
||||
let mut conn = get_connection().await.to_internal_error()?;
|
||||
let mut conn = get_connection().await?;
|
||||
|
||||
let unique_key = format!("{}:{}", user_id, server_id);
|
||||
|
||||
@@ -89,7 +110,7 @@ pub async fn create_voice_state(
|
||||
voice_state.screensharing,
|
||||
)
|
||||
.set(format!("camera:{unique_key}"), voice_state.camera)
|
||||
.query_async(&mut get_connection().await.to_internal_error()?.into_inner())
|
||||
.query_async(&mut get_connection().await?.into_inner())
|
||||
.await
|
||||
.to_internal_error()?;
|
||||
|
||||
@@ -113,7 +134,7 @@ pub async fn delete_voice_state(
|
||||
format!("camera:{unique_key}"),
|
||||
unique_key.clone(),
|
||||
])
|
||||
.query_async(&mut get_connection().await.to_internal_error()?.into_inner())
|
||||
.query_async(&mut get_connection().await?.into_inner())
|
||||
.await
|
||||
.to_internal_error()?;
|
||||
|
||||
@@ -179,7 +200,7 @@ pub async fn update_voice_state(
|
||||
}
|
||||
|
||||
pipeline
|
||||
.query_async(&mut get_connection().await.to_internal_error()?.into_inner())
|
||||
.query_async(&mut get_connection().await?.into_inner())
|
||||
.await
|
||||
.to_internal_error()?;
|
||||
|
||||
@@ -188,8 +209,7 @@ pub async fn update_voice_state(
|
||||
|
||||
pub async fn get_voice_channel_members(channel_id: &str) -> Result<Vec<String>> {
|
||||
get_connection()
|
||||
.await
|
||||
.to_internal_error()?
|
||||
.await?
|
||||
.smembers::<_, Vec<String>>(format!("vc_members:{}", channel_id))
|
||||
.await
|
||||
.to_internal_error()
|
||||
@@ -203,8 +223,7 @@ pub async fn get_voice_state(
|
||||
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()?
|
||||
.await?
|
||||
.mget::<_, (Option<bool>, Option<bool>, Option<bool>, Option<bool>)>(&[
|
||||
format!("is_publishing:{unique_key}"),
|
||||
format!("is_receiving:{unique_key}"),
|
||||
@@ -257,3 +276,85 @@ pub async fn get_channel_voice_state(channel: &Channel) -> Result<Option<v0::Cha
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn move_user(user: &str, from: &str, to: &str) -> Result<()> {
|
||||
get_connection()
|
||||
.await?
|
||||
.smove(
|
||||
format!("vc-members-{from}"),
|
||||
format!("vc-members-{to}"),
|
||||
user,
|
||||
)
|
||||
.await
|
||||
.to_internal_error()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn sync_voice_permissions(db: &Database, voice_client: &VoiceClient, channel: &Channel, server: Option<&Server>, role_id: Option<&str>) -> Result<()> {
|
||||
|
||||
for user_id in get_voice_channel_members(channel.id()).await? {
|
||||
let user = Reference::from_unchecked(user_id.clone()).as_user(db).await?;
|
||||
|
||||
sync_user_voice_permissions(db, voice_client, &user, channel, server, role_id).await?;
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn sync_user_voice_permissions(db: &Database, voice_client: &VoiceClient, 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
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
if let (Some(server), Some(member)) = (server, member.as_ref()) {
|
||||
query = query.member(member).server(server)
|
||||
}
|
||||
|
||||
let permissions = calculate_channel_permissions(&mut query).await;
|
||||
|
||||
let mut update_event = PartialUserVoiceState {
|
||||
id: Some(user.id.clone()),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let before = update_event.clone();
|
||||
|
||||
let can_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);
|
||||
|
||||
update_event.camera = voice_state.camera.then_some(can_video);
|
||||
update_event.screensharing = voice_state.screensharing.then_some(can_video);
|
||||
update_event.is_publishing = voice_state.is_publishing.then_some(can_speak);
|
||||
|
||||
update_voice_state(channel_id, server_id, &user.id, &update_event).await?;
|
||||
|
||||
voice_client.update_permissions(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;
|
||||
};
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -105,4 +105,10 @@ impl VoiceClient {
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
|
||||
pub async fn remove_user(&self, user: &User, channel_id: &str) -> Result<()> {
|
||||
self.rooms.remove_participant(channel_id, &user.id)
|
||||
.await
|
||||
.to_internal_error()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user