From 176df3c7a70637cb9550667e47212a3410b46a85 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 13 May 2022 19:09:29 +0530 Subject: [PATCH] feat: migrate get captcha levels to use db_* --- src/api/v1/mcaptcha/get.rs | 17 +++-------------- src/api/v1/mcaptcha/update.rs | 24 ++++++++---------------- src/api/v1/pow/get_config.rs | 18 ++++-------------- 3 files changed, 15 insertions(+), 44 deletions(-) diff --git a/src/api/v1/mcaptcha/get.rs b/src/api/v1/mcaptcha/get.rs index 3319123c..dac6c921 100644 --- a/src/api/v1/mcaptcha/get.rs +++ b/src/api/v1/mcaptcha/get.rs @@ -17,6 +17,7 @@ use actix_identity::Identity; use actix_web::{web, HttpResponse, Responder}; +use libmcaptcha::defense::Level; use serde::{Deserialize, Serialize}; use super::create::MCaptchaDetails; @@ -56,20 +57,8 @@ pub mod runner { key: &str, username: &str, data: &AppData, - ) -> ServiceResult> { - let levels = sqlx::query_as!( - I32Levels, - "SELECT difficulty_factor, visitor_threshold FROM mcaptcha_levels WHERE - config_id = ( - SELECT config_id FROM mcaptcha_config WHERE key = ($1) - AND user_id = (SELECT ID from mcaptcha_users WHERE name = $2) - ) - ORDER BY difficulty_factor ASC;", - key, - &username - ) - .fetch_all(&data.db) - .await?; + ) -> ServiceResult> { + let levels = data.dblib.get_captcha_levels(Some(username), key).await?; Ok(levels) } diff --git a/src/api/v1/mcaptcha/update.rs b/src/api/v1/mcaptcha/update.rs index 93c83204..b45bd008 100644 --- a/src/api/v1/mcaptcha/update.rs +++ b/src/api/v1/mcaptcha/update.rs @@ -14,8 +14,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -use std::borrow::Cow; - use actix_identity::Identity; use actix_web::{web, HttpResponse, Responder}; use libmcaptcha::defense::Level; @@ -45,19 +43,14 @@ pub async fn update_key( loop { key = get_random(32); - let mut key; - loop { - key = get_random(32); - - match data - .dblib - .update_captcha_key(&username, &payload.key, &key) - .await - { - Ok(_) => break, - Err(DBError::SecretTaken) => continue, - Err(e) => return Err(e.into()), - } + match data + .dblib + .update_captcha_key(&username, &payload.key, &key) + .await + { + Ok(_) => break, + Err(DBError::SecretTaken) => continue, + Err(e) => return Err(e.into()), } } @@ -100,7 +93,6 @@ pub async fn update_captcha( } pub mod runner { - use futures::future::try_join_all; use libmcaptcha::{master::messages::RemoveCaptcha, DefenseBuilder}; use super::*; diff --git a/src/api/v1/pow/get_config.rs b/src/api/v1/pow/get_config.rs index e797e80d..b9f0c792 100644 --- a/src/api/v1/pow/get_config.rs +++ b/src/api/v1/pow/get_config.rs @@ -23,7 +23,6 @@ use libmcaptcha::{ }; use serde::{Deserialize, Serialize}; -use super::I32Levels; use crate::errors::*; use crate::stats::record::record_fetch; use crate::AppData; @@ -108,29 +107,20 @@ pub async fn get_config( /// creates [MCaptcha][libmcaptcha::MCaptcha] and adds it to [Master][libmcaptcha::Defense] async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> { // get levels - let levels_fut = sqlx::query_as!( - I32Levels, - "SELECT difficulty_factor, visitor_threshold FROM mcaptcha_levels WHERE - config_id = ( - SELECT config_id FROM mcaptcha_config WHERE key = ($1) - ) ORDER BY difficulty_factor ASC;", - &key, - ) - .fetch_all(&data.db); - + let levels = data.dblib.get_captcha_levels(None, key).await?; struct DurationResp { duration: i32, } // get duration - let duration_fut = sqlx::query_as!( + let duration = sqlx::query_as!( DurationResp, "SELECT duration FROM mcaptcha_config WHERE key = $1", &key, ) - .fetch_one(&data.db); + .fetch_one(&data.db) + .await?; //let (levels, duration) = futures::try_join!(levels_fut, duration_fut).await?; - let (levels, duration) = futures::try_join!(levels_fut, duration_fut)?; // build defense let mut defense = DefenseBuilder::default();