Skip to content

Commit

Permalink
feat(livekit): push unfinished code
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomatree committed Oct 13, 2024
1 parent 120ca44 commit 2f72947
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
46 changes: 43 additions & 3 deletions crates/delta/src/routes/servers/roles_delete.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference},
Database, User,
util::{permissions::DatabasePermissionQuery, reference::Reference}, Channel, Database, User
};
use revolt_permissions::{calculate_server_permissions, ChannelPermission};
use revolt_models::v0::PartialUserVoiceState;
use revolt_permissions::{calculate_channel_permissions, calculate_server_permissions, ChannelPermission};
use revolt_result::{create_error, Result};
use revolt_voice::{get_allowed_sources, get_voice_channel_members, get_voice_state};
use rocket::State;
use rocket_empty::EmptyResponse;

Expand Down Expand Up @@ -31,6 +32,45 @@ pub async fn delete(
return Err(create_error!(NotElevated));
}

for channel_id in &server.channels {
let channel = Reference::from_unchecked(channel_id.clone()).as_channel(db).await?;

if matches!(channel, Channel::VoiceChannel { .. }) {
for member_id in get_voice_channel_members(channel_id).await? {
let member = Reference::from_unchecked(member_id).as_member(db, &server.id).await?;

if member.roles.contains(&role_id) {
let user = Reference::from_unchecked(member.id.user.clone()).as_user(db).await?;
let voice_state = get_voice_state(channel_id, Some(&server.id), &user.id).await?.unwrap();

let mut query = DatabasePermissionQuery::new(db, &user)
.member(&member)
.channel(&channel)
.server(&server);

let permissions = calculate_channel_permissions(&mut query).await;

let sources = get_allowed_sources(permissions);

let mut update_event = PartialUserVoiceState {
id: Some(user.id.clone()),
..Default::default()
};

if !sources.contains(&"CAMERA".to_string()) {
update_event.camera =
update_event
}

if voice_state.camera && !sources.contains(&"MICROPHONE".to_string()) {
update_event. = Some(false);
}

}
}
}
};

role.delete(db, &server.id, &role_id)
.await
.map(|_| EmptyResponse)
Expand Down
42 changes: 38 additions & 4 deletions crates/delta/src/routes/servers/roles_edit.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference},
Database, PartialRole, User,
util::{permissions::DatabasePermissionQuery, reference::Reference}, Channel, Database, PartialRole, User
};
use revolt_models::v0;
use revolt_permissions::{calculate_server_permissions, ChannelPermission};
use revolt_models::v0::{self, PartialUserVoiceState};
use revolt_permissions::{calculate_channel_permissions, calculate_server_permissions, ChannelPermission, PermissionQuery};
use revolt_result::{create_error, Result};
use revolt_voice::{get_allowed_sources, get_voice_channel_members, get_voice_state, update_voice_state_tracks, VoiceClient};
use rocket::{serde::json::Json, State};
use serde::{Deserialize, Serialize};
use validator::Validate;
Expand All @@ -16,6 +16,7 @@ use validator::Validate;
#[patch("/<target>/roles/<role_id>", data = "<data>")]
pub async fn edit(
db: &State<Database>,
voice: &State<VoiceClient>,
user: User,
target: Reference,
role_id: String,
Expand Down Expand Up @@ -76,6 +77,39 @@ pub async fn edit(
)
.await?;

for channel_id in &server.channels {
let channel = Reference::from_unchecked(channel_id.clone()).as_channel(db).await?;

if matches!(channel, Channel::VoiceChannel { .. }) {
for member_id in get_voice_channel_members(channel_id).await? {
let member = Reference::from_unchecked(member_id).as_member(db, &server.id).await?;

if member.roles.contains(&role_id) {
let user = Reference::from_unchecked(member.id.user.clone()).as_user(db).await?;
let voice_state = get_voice_state(channel_id, Some(&server.id), &user.id).await?.unwrap();

let mut query = DatabasePermissionQuery::new(db, &user)
.member(&member)
.channel(&channel)
.server(&server);

let permissions = calculate_channel_permissions(&mut query).await;

let sources = get_allowed_sources(permissions);

let mut update_event = PartialUserVoiceState {
id: Some(user.id.clone()),
..Default::default()
};

if voice_state.camera && !sources.contains(&"CAMERA".to_string()) {
update_event.camera = Some(false);
}
}
}
}
};

Ok(Json(role.into()))
} else {
Err(create_error!(NotFound))
Expand Down

0 comments on commit 2f72947

Please sign in to comment.