add: ability to ban strings/characters through server config

This commit is contained in:
trisua 2025-06-02 16:36:06 -04:00
parent 701ea79c9a
commit c9983b8dcb
3 changed files with 48 additions and 0 deletions

View file

@ -211,6 +211,20 @@ impl Default for ManualsConfig {
}
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum StringBan {
/// An exact string.
String(String),
/// A unicode codepoint.
Unicode(u32),
}
impl Default for StringBan {
fn default() -> Self {
Self::String(String::new())
}
}
/// Configuration file
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Config {
@ -277,6 +291,9 @@ pub struct Config {
/// The relative paths to manuals.
#[serde(default)]
pub manuals: ManualsConfig,
/// A list of banned content in posts.
#[serde(default)]
pub banned_data: Vec<StringBan>,
}
fn default_name() -> String {
@ -353,6 +370,10 @@ fn default_manuals() -> ManualsConfig {
ManualsConfig::default()
}
fn default_banned_data() -> Vec<StringBan> {
Vec::new()
}
impl Default for Config {
fn default() -> Self {
Self {
@ -374,6 +395,7 @@ impl Default for Config {
html_footer_path: String::new(),
stripe: None,
manuals: default_manuals(),
banned_data: default_banned_data(),
}
}
}

View file

@ -1,6 +1,7 @@
use std::collections::HashMap;
use super::*;
use crate::cache::Cache;
use crate::config::StringBan;
use crate::model::auth::Notification;
use crate::model::communities::Question;
use crate::model::communities_permissions::CommunityPermission;
@ -1153,6 +1154,25 @@ impl DataManager {
/// # Arguments
/// * `data` - a mock [`Post`] object to insert
pub async fn create_post(&self, mut data: Post) -> Result<usize> {
// check characters
for ban in &self.0.banned_data {
match ban {
StringBan::String(x) => {
if data.content.contains(x) {
return Ok(0);
}
}
StringBan::Unicode(x) => {
if data.content.contains(&match char::from_u32(x.to_owned()) {
Some(c) => c.to_string(),
None => continue,
}) {
return Ok(0);
}
}
}
}
// check values (if this isn't reposting something else)
let is_reposting = if let Some(ref repost) = data.context.repost {
repost.reposting.is_some()