captcha stats

This commit is contained in:
realaravinth
2021-07-27 15:28:21 +05:30
parent 9bc11f3518
commit 1d759fcb25
18 changed files with 249 additions and 159 deletions

View File

@@ -257,8 +257,10 @@ pub struct StatsPayload {
async fn get_stats(
payload: web::Json<StatsPayload>,
data: AppData,
id: Identity,
) -> ServiceResult<impl Responder> {
let stats = Stats::new(&payload.key, &data.db).await?;
let username = id.identity().unwrap();
let stats = Stats::new(&username, &payload.key, &data.db).await?;
let stats = StatsUnixTimestamp::from_stats(&stats);
Ok(HttpResponse::Ok().json(&stats))
}

View File

@@ -139,7 +139,7 @@ async fn update_levels(
defense.build()?;
let mut futs = Vec::with_capacity(payload.levels.len() + 2);
let del_fut = sqlx::query!(
sqlx::query!(
"DELETE FROM mcaptcha_levels
WHERE config_id = (
SELECT config_id FROM mcaptcha_config where key = ($1)
@@ -150,7 +150,8 @@ async fn update_levels(
&payload.key,
&username
)
.execute(&data.db); //.await?;
.execute(&data.db)
.await?;
let update_fut = sqlx::query!(
"UPDATE mcaptcha_config SET name = $1, duration = $2
@@ -163,7 +164,6 @@ async fn update_levels(
)
.execute(&data.db); //.await?;
futs.push(del_fut);
futs.push(update_fut);
for level in payload.levels.iter() {
@@ -291,9 +291,7 @@ mod tests {
assert_eq!(res_levels, add_level.levels);
// 3. update level
let levels = vec![L1, L2];
let update_level = UpdateLevels {
key: key.key.clone(),
levels: levels.clone(),

View File

@@ -65,7 +65,10 @@ pub async fn get_config(
match res.exists {
Some(true) => {
match data.captcha.get_pow(payload.key.clone()).await {
Some(config) => Ok(HttpResponse::Ok().json(config)),
Some(config) => {
record_fetch(&payload.key, &data.db).await;
Ok(HttpResponse::Ok().json(config))
}
None => {
init_mcaptcha(&data, &payload.key).await?;
let config = data