From f397b8bdf948f5889f994fe30d5bd3d14b519a25 Mon Sep 17 00:00:00 2001 From: DataHearth Date: Wed, 24 May 2023 15:58:11 +0200 Subject: [PATCH] add checks on search params --- Cargo.lock | 12 +++++++----- Cargo.toml | 7 ++++--- src/commands.rs | 8 +++++++- src/database.rs | 16 ++++++++++++---- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb64a00..0e03d76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -929,7 +929,7 @@ dependencies = [ [[package]] name = "polodb_core" -version = "4.3.1" +version = "4.3.2" dependencies = [ "bson", "byteorder", @@ -1035,9 +1035,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.8.1" +version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" +checksum = "d1a59b5d8e97dee33696bf13c5ba8ab85341c002922fba050069326b9c498974" dependencies = [ "aho-corasick", "memchr", @@ -1046,9 +1046,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" +checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78" [[package]] name = "reqwest" @@ -1357,8 +1357,10 @@ dependencies = [ "once_cell", "poise", "polodb_core", + "regex", "serde", "tokio", + "url", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 91058e0..2ecc91a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,9 +6,10 @@ authors = ["Antoine Langlois "] description = "A Discord bot for the Tech channel" [dependencies] -once_cell = "1.17.1" +once_cell = "1.17" poise = "0.5" polodb_core = { path = "../PoloDB/src/polodb_core" } -# polodb_core = "4.2.0" +regex = "1.8" serde = { version = "1.0", features = ["derive"] } -tokio = { version = "1.28.0", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.28", features = ["macros", "rt-multi-thread"] } +url = "2.3" diff --git a/src/commands.rs b/src/commands.rs index bf381bb..6767d96 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,4 +1,5 @@ use poise::command; +use url::Url; use crate::database::*; @@ -52,6 +53,11 @@ pub async fn add( #[description = "Technology name"] technology: String, #[description = "Git repository link"] link: String, ) -> Result<(), Error> { + if !Url::parse(&link).is_ok() { + ctx.say(format!("Link {link} is not a valid URL")).await?; + return Ok(()); + } + add_tech(link.clone(), technology.clone())?; ctx.say(format!("Added {technology} with link {link}",)) @@ -86,7 +92,7 @@ pub async fn list(ctx: Context<'_>) -> Result<(), Error> { #[command(slash_command, prefix_command)] pub async fn search( ctx: Context<'_>, - #[description = "Technology name"] technology: String, + #[description = "Technology name (can be a regex string)"] technology: String, #[description = "Regex options"] options: Option, ) -> Result<(), Error> { let found_techs = search_tech(technology, options.map_or(String::new(), |opts| opts))?; diff --git a/src/database.rs b/src/database.rs index 52770c5..45d0d78 100644 --- a/src/database.rs +++ b/src/database.rs @@ -53,14 +53,22 @@ pub fn list_tech() -> Result, Error> { } pub fn search_tech(name: String, options: String) -> Result, Error> { + let doc = if regex::Regex::new(&name).is_ok() { + doc! { "name": {"$regex": Regex { + pattern: name, + options: options, + }} } + } else { + doc! { "name": { + "$eq": name + } } + }; + Ok(DB .get() .unwrap() .collection::("technologies") - .find(doc! { "name": {"$regex": Regex { - pattern: name, - options: options, - }} }) + .find(doc) .map_err(|err| Error::new(ErrorKind::InvalidInput, err))? .map( |doc| doc.unwrap(), // todo: find a way to handle error