fix: validate categories

This commit is contained in:
Paul Makles
2022-09-01 09:41:00 +01:00
parent 81fc3e7760
commit 345e4d5f13
3 changed files with 39 additions and 19 deletions

View File

@@ -1,3 +1,5 @@
use std::collections::HashSet;
use revolt_quark::{
models::{
server::{Category, FieldsServer, PartialServer, SystemMessageChannels},
@@ -135,29 +137,43 @@ pub async fn req(
}
}
// 2. Apply new icon
// 2. Validate changes
let mut unknown_channels = HashSet::new();
if let Some(system_messages) = &partial.system_messages {
unknown_channels = system_messages.clone().into_channel_ids();
}
if let Some(categories) = &partial.categories {
let mut channel_ids = HashSet::new();
for category in categories {
for channel in &category.channels {
if channel_ids.contains(channel) {
return Err(Error::InvalidOperation);
}
channel_ids.insert(channel.to_string());
}
}
unknown_channels.extend(channel_ids);
}
if !db.check_channels_exist(&unknown_channels).await? {
return Err(Error::NotFound);
}
// 3. Apply new icon
if let Some(icon) = icon {
partial.icon = Some(File::use_server_icon(db, &icon, &server.id).await?);
server.icon = partial.icon.clone();
}
// 3. Apply new banner
// 4. Apply new banner
if let Some(banner) = banner {
partial.banner = Some(File::use_banner(db, &banner, &server.id).await?);
server.banner = partial.banner.clone();
}
// 4. Validate changes
if let Some(system_messages) = &partial.system_messages {
let channels = system_messages.clone().into_channel_ids();
if !db
.check_channels_exist(&channels.into_iter().collect())
.await?
{
return Err(Error::NotFound);
}
}
server
.update(db, partial, remove.unwrap_or_default())
.await?;

View File

@@ -1,3 +1,5 @@
use std::collections::HashSet;
use iso8601_timestamp::Timestamp;
use ulid::Ulid;
@@ -310,23 +312,23 @@ impl Server {
}
impl SystemMessageChannels {
pub fn into_channel_ids(self) -> Vec<String> {
let mut ids = vec![];
pub fn into_channel_ids(self) -> HashSet<String> {
let mut ids = HashSet::new();
if let Some(id) = self.user_joined {
ids.push(id);
ids.insert(id);
}
if let Some(id) = self.user_left {
ids.push(id);
ids.insert(id);
}
if let Some(id) = self.user_kicked {
ids.push(id);
ids.insert(id);
}
if let Some(id) = self.user_banned {
ids.push(id);
ids.insert(id);
}
ids

View File

@@ -1,3 +1,5 @@
use crate::Result;
mod admin {
pub mod migrations;
}