From d061824660dc6f871260212de39dfc0434fd55b1 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 14 May 2022 16:27:44 +0530 Subject: [PATCH] feat: migrate get captcha cooldown period to use db_* --- sqlx-data.json | 20 ++++++++++++++++++++ src/api/v1/pow/get_config.rs | 23 ++--------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/sqlx-data.json b/sqlx-data.json index e749d0b1..c4bec8b1 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -470,6 +470,26 @@ }, "query": "SELECT name FROM mcaptcha_config \n WHERE key = $1 \n AND user_id = (\n SELECT user_id FROM mcaptcha_users WHERE NAME = $2)" }, + "ad196ab3ef9dc32f6de2313577ccd6c26eae9ab19df5f71ce182651983efb99a": { + "describe": { + "columns": [ + { + "name": "duration", + "ordinal": 0, + "type_info": "Int4" + } + ], + "nullable": [ + false + ], + "parameters": { + "Left": [ + "Text" + ] + } + }, + "query": "SELECT duration FROM mcaptcha_config \n WHERE key = $1" + }, "ad23588ee4bcbb13e208460ce21e2fa9f1373893934b530b339fea10360b34a8": { "describe": { "columns": [ diff --git a/src/api/v1/pow/get_config.rs b/src/api/v1/pow/get_config.rs index 472f40af..9c4a1060 100644 --- a/src/api/v1/pow/get_config.rs +++ b/src/api/v1/pow/get_config.rs @@ -41,13 +41,6 @@ pub async fn get_config( payload: web::Json, data: AppData, ) -> ServiceResult { - // let res = sqlx::query!( - // "SELECT EXISTS (SELECT 1 from mcaptcha_config WHERE key = $1)", - // &payload.key, - // ) - // .fetch_one(&data.db) - // .await?; - //if res.exists.is_none() { if !data.dblib.captcha_exists(None, &payload.key).await? { return Err(ServiceError::TokenNotFound); @@ -108,19 +101,7 @@ pub async fn get_config( async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> { // get levels let levels = data.dblib.get_captcha_levels(None, key).await?; - struct DurationResp { - duration: i32, - } - // get duration - let duration = sqlx::query_as!( - DurationResp, - "SELECT duration FROM mcaptcha_config - WHERE key = $1", - &key, - ) - .fetch_one(&data.db) - .await?; - //let (levels, duration) = futures::try_join!(levels_fut, duration_fut).await?; + let duration = data.dblib.get_captcha_cooldown(&key).await?; // build defense let mut defense = DefenseBuilder::default(); @@ -141,7 +122,7 @@ async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> { let mcaptcha = MCaptchaBuilder::default() .defense(defense) // leaky bucket algorithm's emission interval - .duration(duration.duration as u64) + .duration(duration as u64) // .cache(cache) .build() .unwrap();