feat: add parent and update permission calculators

This commit is contained in:
Zomatree
2025-05-07 00:55:03 +01:00
parent c7f09ab9d0
commit 9e17a5e73a
7 changed files with 109 additions and 8 deletions

View File

@@ -192,6 +192,7 @@ mod test {
d: ChannelPermission::ViewChannel as i64,
}),
last_message_id: None,
parent: None,
};
locked_channel
.update(&harness.db, partial, vec![])

View File

@@ -1,6 +1,6 @@
use revolt_config::config;
use revolt_database::{
util::{permissions::DatabasePermissionQuery, reference::Reference}, Category, Database, PartialCategory, Role, User
util::{permissions::DatabasePermissionQuery, reference::Reference}, Category, Channel, Database, PartialCategory, PartialChannel, Role, User
};
use revolt_models::v0::{self, DataEditCategory};
use revolt_permissions::{calculate_server_permissions, ChannelPermission};
@@ -41,16 +41,34 @@ pub async fn edit(
let DataEditCategory {
title,
channels,
mut channels,
remove
} = data;
// remove the channels from any existing categories to avoid it having two
for category in server.categories.values_mut() {
category.channels.retain(|c| channels.as_ref().map_or(false, |cs| cs.contains(c)));
}
// only keep channels which exist in the server
if let Some(channels) = &mut channels {
channels.retain(|c| server.channels.contains(c));
}
// unset parent from all channels which are removed from the category
for channel_id in &category.channels {
if channels.as_ref().is_some_and(|cs| !cs.contains(channel_id)) {
db.update_channel(channel_id, &PartialChannel { parent: None, ..Default::default() }, Vec::new()).await?;
};
};
// update the category with the new values
category.update(
db,
&mut server,
PartialCategory {
title,
channels,
channels: channels.clone(),
..Default::default()
},
remove
@@ -58,5 +76,16 @@ pub async fn edit(
.unwrap_or_default()
).await?;
let channels = db.fetch_channels(&channels.unwrap_or_default()).await?;
// update all channels to have the parent set
for channel in channels {
if let Channel::TextChannel { ref parent, .. } | Channel::VoiceChannel { ref parent, .. } = channel {
if parent.as_ref() != Some(&category.id) {
db.update_channel(channel.id(), &PartialChannel { parent: Some(category.id.clone()), ..Default::default() }, Vec::new()).await?;
};
};
};
Ok(Json(category.into()))
}