From 12edac7915c7870e1c028488bdefd77f601dc9a7 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 26 May 2022 20:37:16 +0530 Subject: [PATCH] feat: migrate get_email to use db_* --- sqlx-data.json | 26 -------------------------- src/api/v1/mcaptcha/get.rs | 20 ++++---------------- src/errors.rs | 9 +++++++++ src/pages/panel/notifications.rs | 11 +---------- src/pages/panel/settings.rs | 19 +++++-------------- src/pages/panel/sitekey/view.rs | 21 ++++----------------- 6 files changed, 23 insertions(+), 83 deletions(-) diff --git a/sqlx-data.json b/sqlx-data.json index fd0640f1..494c045c 100644 --- a/sqlx-data.json +++ b/sqlx-data.json @@ -13,32 +13,6 @@ }, "query": "INSERT INTO mcaptcha_pow_confirmed_stats \n (config_id, time) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1), $2)" }, - "06699fda6b1542bf4544c0bdece91531a3020c24c9c76bcf967980e71ee25b42": { - "describe": { - "columns": [ - { - "name": "email", - "ordinal": 0, - "type_info": "Varchar" - }, - { - "name": "secret", - "ordinal": 1, - "type_info": "Varchar" - } - ], - "nullable": [ - true, - false - ], - "parameters": { - "Left": [ - "Text" - ] - } - }, - "query": "SELECT email, secret FROM mcaptcha_users WHERE name = ($1)" - }, "4303f5c6ef98e0de9d8d3c2d781d3ffaa3dee5f7d27db831d327b26f03ba9d68": { "describe": { "columns": [ diff --git a/src/api/v1/mcaptcha/get.rs b/src/api/v1/mcaptcha/get.rs index dac6c921..7a47fc0f 100644 --- a/src/api/v1/mcaptcha/get.rs +++ b/src/api/v1/mcaptcha/get.rs @@ -34,7 +34,10 @@ pub async fn get_captcha( id: Identity, ) -> ServiceResult { let username = id.identity().unwrap(); - let levels = runner::get_captcha(&payload.key, &username, &data).await?; + let levels = data + .dblib + .get_captcha_levels(Some(&username), &payload.key) + .await?; Ok(HttpResponse::Ok().json(levels)) } @@ -48,18 +51,3 @@ pub struct I32Levels { pub difficulty_factor: i32, pub visitor_threshold: i32, } - -pub mod runner { - use super::*; - - // TODO get metadata from mcaptcha_config table - pub async fn get_captcha( - key: &str, - username: &str, - data: &AppData, - ) -> ServiceResult> { - let levels = data.dblib.get_captcha_levels(Some(username), key).await?; - - Ok(levels) - } -} diff --git a/src/errors.rs b/src/errors.rs index 0b82835b..3b657f45 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -310,6 +310,15 @@ impl From for PageError { } } +#[cfg(not(tarpaulin_include))] +impl From for PageError { + #[cfg(not(tarpaulin_include))] + fn from(e: DBError) -> Self { + let se: ServiceError = e.into(); + se.into() + } +} + impl ResponseError for PageError { fn error_response(&self) -> HttpResponse { use crate::PAGES; diff --git a/src/pages/panel/notifications.rs b/src/pages/panel/notifications.rs index 67dd1437..d20e24c9 100644 --- a/src/pages/panel/notifications.rs +++ b/src/pages/panel/notifications.rs @@ -74,16 +74,7 @@ pub async fn notifications(data: AppData, id: Identity) -> PageResult { async fn settings(data: AppData, id: Identity) -> PageResult { let username = id.identity().unwrap(); - struct DBResult { - email: Option, - secret: String, - } - - let details = sqlx::query_as!( - DBResult, - r#"SELECT email, secret FROM mcaptcha_users WHERE name = ($1)"#, - &username, - ) - .fetch_one(&data.db) - .await?; + let secret = data.dblib.get_secret(&username).await?; + let secret = secret.secret; + let email = data.dblib.get_email(&username).await?; let data = IndexPage { - email: details.email, - secret: details.secret, + email, + secret, username: &username, }; diff --git a/src/pages/panel/sitekey/view.rs b/src/pages/panel/sitekey/view.rs index ebfccc79..04e3ed64 100644 --- a/src/pages/panel/sitekey/view.rs +++ b/src/pages/panel/sitekey/view.rs @@ -20,6 +20,8 @@ use actix_web::{web, HttpResponse, Responder}; use futures::{future::TryFutureExt, try_join}; use sailfish::TemplateOnce; +use libmcaptcha::defense::Level; + use crate::errors::*; use crate::stats::fetch::Stats; use crate::AppData; @@ -33,11 +35,6 @@ struct McaptchaConfig { name: String, } -#[derive(Clone)] -struct Level { - difficulty_factor: i32, - visitor_threshold: i32, -} #[derive(TemplateOnce, Clone)] #[template(path = "panel/sitekey/view/index.html")] struct IndexPage { @@ -89,19 +86,9 @@ pub async fn view_sitekey( .fetch_one(&data.db) .await?; - let levels_fut = sqlx::query_as!( - Level, - "SELECT - difficulty_factor, visitor_threshold - FROM - mcaptcha_levels - WHERE config_id = $1 ORDER BY difficulty_factor ASC", - &config.config_id - ) - .fetch_all(&data.db) - .err_into(); + let levels = data.dblib.get_captcha_levels(Some(&username), &key).await?; - let (stats, levels) = try_join!(Stats::new(&username, &key, &data.db), levels_fut)?; + let stats = Stats::new(&username, &key, &data.db).await?; let body = IndexPage::new(stats, config, levels, key) .render_once()