forked from jmug/stoatchat
fix: strip unknown channels from categories (fixes #201)
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
Rocket.toml
|
Rocket.toml
|
||||||
target
|
target
|
||||||
.data
|
.data
|
||||||
|
|
||||||
|
.vercel
|
||||||
|
|||||||
@@ -138,12 +138,15 @@ pub async fn req(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2. Validate changes
|
// 2. Validate changes
|
||||||
let mut unknown_channels = HashSet::new();
|
|
||||||
if let Some(system_messages) = &partial.system_messages {
|
if let Some(system_messages) = &partial.system_messages {
|
||||||
unknown_channels = system_messages.clone().into_channel_ids();
|
for id in system_messages.clone().into_channel_ids() {
|
||||||
|
if !server.channels.contains(&id) {
|
||||||
|
return Err(Error::NotFound);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(categories) = &partial.categories {
|
if let Some(categories) = &mut partial.categories {
|
||||||
let mut channel_ids = HashSet::new();
|
let mut channel_ids = HashSet::new();
|
||||||
for category in categories {
|
for category in categories {
|
||||||
for channel in &category.channels {
|
for channel in &category.channels {
|
||||||
@@ -153,13 +156,11 @@ pub async fn req(
|
|||||||
|
|
||||||
channel_ids.insert(channel.to_string());
|
channel_ids.insert(channel.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
category
|
||||||
|
.channels
|
||||||
|
.retain(|item| server.channels.contains(item));
|
||||||
}
|
}
|
||||||
|
|
||||||
unknown_channels.extend(channel_ids);
|
|
||||||
}
|
|
||||||
|
|
||||||
if !db.check_channels_exist(&unknown_channels).await? {
|
|
||||||
return Err(Error::NotFound);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Apply new icon
|
// 3. Apply new icon
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
|
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
|
||||||
use crate::{AbstractAttachment, AbstractChannel, Error, OverrideField, Result};
|
use crate::{AbstractAttachment, AbstractChannel, Error, OverrideField, Result};
|
||||||
|
|
||||||
@@ -83,8 +81,4 @@ impl AbstractChannel for DummyDb {
|
|||||||
info!("Updating permissions for role {role} in {channel} with {permissions:?}");
|
info!("Updating permissions for role {role} in {channel} with {permissions:?}");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_channels_exist(&self, _channels: &HashSet<String>) -> Result<bool> {
|
|
||||||
Ok(true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use bson::{Bson, Document};
|
use bson::{Bson, Document};
|
||||||
|
|
||||||
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
|
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
|
||||||
@@ -291,25 +289,6 @@ impl AbstractChannel for MongoDb {
|
|||||||
with: "channel",
|
with: "channel",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn check_channels_exist(&self, channels: &HashSet<String>) -> Result<bool> {
|
|
||||||
let count = channels.len() as u64;
|
|
||||||
self.col::<Document>(COL)
|
|
||||||
.count_documents(
|
|
||||||
doc! {
|
|
||||||
"_id": {
|
|
||||||
"$in": channels.iter().cloned().collect::<Vec<String>>()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map(|x| x == count)
|
|
||||||
.map_err(|_| Error::DatabaseError {
|
|
||||||
operation: "count_documents",
|
|
||||||
with: "channel",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoDocumentPath for FieldsChannel {
|
impl IntoDocumentPath for FieldsChannel {
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
use std::collections::HashSet;
|
|
||||||
|
|
||||||
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
|
use crate::models::channel::{Channel, FieldsChannel, PartialChannel};
|
||||||
use crate::{OverrideField, Result};
|
use crate::{OverrideField, Result};
|
||||||
|
|
||||||
@@ -55,7 +53,4 @@ pub trait AbstractChannel: Sync + Send {
|
|||||||
role: &str,
|
role: &str,
|
||||||
permissions: OverrideField,
|
permissions: OverrideField,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
|
|
||||||
/// Validate existence of channels
|
|
||||||
async fn check_channels_exist(&self, channels: &HashSet<String>) -> Result<bool>;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user