add: ability to ban strings/characters through server config
This commit is contained in:
parent
701ea79c9a
commit
c9983b8dcb
3 changed files with 48 additions and 0 deletions
|
@ -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
|
/// Configuration file
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug)]
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
@ -277,6 +291,9 @@ pub struct Config {
|
||||||
/// The relative paths to manuals.
|
/// The relative paths to manuals.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub manuals: ManualsConfig,
|
pub manuals: ManualsConfig,
|
||||||
|
/// A list of banned content in posts.
|
||||||
|
#[serde(default)]
|
||||||
|
pub banned_data: Vec<StringBan>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_name() -> String {
|
fn default_name() -> String {
|
||||||
|
@ -353,6 +370,10 @@ fn default_manuals() -> ManualsConfig {
|
||||||
ManualsConfig::default()
|
ManualsConfig::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_banned_data() -> Vec<StringBan> {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -374,6 +395,7 @@ impl Default for Config {
|
||||||
html_footer_path: String::new(),
|
html_footer_path: String::new(),
|
||||||
stripe: None,
|
stripe: None,
|
||||||
manuals: default_manuals(),
|
manuals: default_manuals(),
|
||||||
|
banned_data: default_banned_data(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::cache::Cache;
|
use crate::cache::Cache;
|
||||||
|
use crate::config::StringBan;
|
||||||
use crate::model::auth::Notification;
|
use crate::model::auth::Notification;
|
||||||
use crate::model::communities::Question;
|
use crate::model::communities::Question;
|
||||||
use crate::model::communities_permissions::CommunityPermission;
|
use crate::model::communities_permissions::CommunityPermission;
|
||||||
|
@ -1153,6 +1154,25 @@ impl DataManager {
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `data` - a mock [`Post`] object to insert
|
/// * `data` - a mock [`Post`] object to insert
|
||||||
pub async fn create_post(&self, mut data: Post) -> Result<usize> {
|
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)
|
// check values (if this isn't reposting something else)
|
||||||
let is_reposting = if let Some(ref repost) = data.context.repost {
|
let is_reposting = if let Some(ref repost) = data.context.repost {
|
||||||
repost.reposting.is_some()
|
repost.reposting.is_some()
|
||||||
|
|
|
@ -43,3 +43,9 @@ privacy = "/public/privacy.html"
|
||||||
[turnstile]
|
[turnstile]
|
||||||
site_key = "1x00000000000000000000AA"
|
site_key = "1x00000000000000000000AA"
|
||||||
secret_key = "1x0000000000000000000000000000000AA"
|
secret_key = "1x0000000000000000000000000000000AA"
|
||||||
|
|
||||||
|
[[banned_data]]
|
||||||
|
String = "test banned string"
|
||||||
|
|
||||||
|
[[banned_data]]
|
||||||
|
Unicode = 3662
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue