From 79ff7b9917f036e6a21f8bbaafa705685b41fd7a Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 12 May 2022 19:09:44 +0530 Subject: [PATCH] feat: implement adding captcha for sqlx postgres --- db/db-sqlx-postgres/Cargo.toml | 5 +++-- db/db-sqlx-postgres/src/lib.rs | 36 ++++++++++++++++++++++++++++++++ db/db-sqlx-postgres/src/tests.rs | 17 ++++++++++++++- 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/db/db-sqlx-postgres/Cargo.toml b/db/db-sqlx-postgres/Cargo.toml index 350aac87..027b7a0e 100644 --- a/db/db-sqlx-postgres/Cargo.toml +++ b/db/db-sqlx-postgres/Cargo.toml @@ -9,9 +9,10 @@ license = "AGPLv3 or later version" authors = ["realaravinth "] [dependencies] -sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline" ] } -db-core = {path = "../db-core"} async-trait = "0.1.51" +db-core = {path = "../db-core"} +futures = "0.3.15" +sqlx = { version = "0.5.13", features = [ "runtime-actix-rustls", "postgres", "time", "offline" ] } [dev-dependencies] actix-rt = "2" diff --git a/db/db-sqlx-postgres/src/lib.rs b/db/db-sqlx-postgres/src/lib.rs index b755c008..4843b46e 100644 --- a/db/db-sqlx-postgres/src/lib.rs +++ b/db/db-sqlx-postgres/src/lib.rs @@ -295,6 +295,42 @@ impl MCDatabase for Database { .map_err(map_register_err)?; Ok(()) } + + /// Add levels to captcha + async fn add_captcha_levels( + &self, + username: &str, + captcha_key: &str, + levels: &[Level], + ) -> DBResult<()> { + use futures::future::try_join_all; + let mut futs = Vec::with_capacity(levels.len()); + + for level in levels.iter() { + let difficulty_factor = level.difficulty_factor as i32; + let visitor_threshold = level.visitor_threshold as i32; + let fut = sqlx::query!( + "INSERT INTO mcaptcha_levels ( + difficulty_factor, + visitor_threshold, + config_id) VALUES ( + $1, $2, ( + SELECT config_id FROM mcaptcha_config WHERE + key = ($3) AND user_id = ( + SELECT ID FROM mcaptcha_users WHERE name = $4 + )));", + difficulty_factor, + visitor_threshold, + &captcha_key, + username, + ) + .execute(&self.pool); + futs.push(fut); + } + + try_join_all(futs).await.map_err(map_register_err)?; + Ok(()) + } } fn now_unix_time_stamp() -> i64 { diff --git a/db/db-sqlx-postgres/src/tests.rs b/db/db-sqlx-postgres/src/tests.rs index e2e44e97..81048b83 100644 --- a/db/db-sqlx-postgres/src/tests.rs +++ b/db/db-sqlx-postgres/src/tests.rs @@ -34,6 +34,21 @@ async fn everyting_works() { const CAPTCHA_DESCRIPTION: &str = "postgrescaptchadescription"; const CAPTCHA_DURATION: i32 = 30; + const LEVELS: [Level; 3] = [ + Level { + difficulty_factor: 1, + visitor_threshold: 1, + }, + Level { + difficulty_factor: 2, + visitor_threshold: 2, + }, + Level { + difficulty_factor: 3, + visitor_threshold: 3, + }, + ]; + let url = env::var("POSTGRES_DATABASE_URL").unwrap(); let pool_options = PgPoolOptions::new().max_connections(2); let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url }); @@ -52,5 +67,5 @@ async fn everyting_works() { key: CAPTCHA_SECRET, description: CAPTCHA_DESCRIPTION, }; - database_works(&db, &p, &c).await; + database_works(&db, &p, &c, &LEVELS).await; }