fix: validate categories
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use revolt_quark::{
|
use revolt_quark::{
|
||||||
models::{
|
models::{
|
||||||
server::{Category, FieldsServer, PartialServer, SystemMessageChannels},
|
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 {
|
if let Some(icon) = icon {
|
||||||
partial.icon = Some(File::use_server_icon(db, &icon, &server.id).await?);
|
partial.icon = Some(File::use_server_icon(db, &icon, &server.id).await?);
|
||||||
server.icon = partial.icon.clone();
|
server.icon = partial.icon.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Apply new banner
|
// 4. Apply new banner
|
||||||
if let Some(banner) = banner {
|
if let Some(banner) = banner {
|
||||||
partial.banner = Some(File::use_banner(db, &banner, &server.id).await?);
|
partial.banner = Some(File::use_banner(db, &banner, &server.id).await?);
|
||||||
server.banner = partial.banner.clone();
|
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
|
server
|
||||||
.update(db, partial, remove.unwrap_or_default())
|
.update(db, partial, remove.unwrap_or_default())
|
||||||
.await?;
|
.await?;
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use iso8601_timestamp::Timestamp;
|
use iso8601_timestamp::Timestamp;
|
||||||
use ulid::Ulid;
|
use ulid::Ulid;
|
||||||
|
|
||||||
@@ -310,23 +312,23 @@ impl Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SystemMessageChannels {
|
impl SystemMessageChannels {
|
||||||
pub fn into_channel_ids(self) -> Vec<String> {
|
pub fn into_channel_ids(self) -> HashSet<String> {
|
||||||
let mut ids = vec![];
|
let mut ids = HashSet::new();
|
||||||
|
|
||||||
if let Some(id) = self.user_joined {
|
if let Some(id) = self.user_joined {
|
||||||
ids.push(id);
|
ids.insert(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(id) = self.user_left {
|
if let Some(id) = self.user_left {
|
||||||
ids.push(id);
|
ids.insert(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(id) = self.user_kicked {
|
if let Some(id) = self.user_kicked {
|
||||||
ids.push(id);
|
ids.insert(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(id) = self.user_banned {
|
if let Some(id) = self.user_banned {
|
||||||
ids.push(id);
|
ids.insert(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
ids
|
ids
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use crate::Result;
|
||||||
|
|
||||||
mod admin {
|
mod admin {
|
||||||
pub mod migrations;
|
pub mod migrations;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user