From a5cfa3b305b1136ce7c42ac6a60fcad6167aa47d Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 30 Apr 2021 11:14:29 +0530 Subject: [PATCH] pow stats --- Cargo.toml | 2 +- ...10430032935_mcaptcha_pow_fetched_stats.sql | 4 +++ src/api/v1/mcaptcha/mod.rs | 1 + src/api/v1/mcaptcha/stats.rs | 28 +++++++++++++++++++ src/api/v1/pow/get_config.rs | 6 +++- 5 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 migrations/20210430032935_mcaptcha_pow_fetched_stats.sql create mode 100644 src/api/v1/mcaptcha/stats.rs diff --git a/Cargo.toml b/Cargo.toml index 5d72a22c..296f58e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ description = "mCaptcha - a PoW-based CAPTCHA system" homepage = "https://mcaptcha.org" repository = "https://github.com/mCaptcha/guard" documentation = "https://mcaptcha.org/docs/" -lisense = "AGPLv3 or later version" +license = "AGPLv3 or later version" authors = ["realaravinth "] edition = "2018" default-run = "guard" diff --git a/migrations/20210430032935_mcaptcha_pow_fetched_stats.sql b/migrations/20210430032935_mcaptcha_pow_fetched_stats.sql new file mode 100644 index 00000000..4c6f0097 --- /dev/null +++ b/migrations/20210430032935_mcaptcha_pow_fetched_stats.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS mcaptcha_pow_fetched_stats ( + config_id INTEGER references mcaptcha_config(config_id) ON DELETE CASCADE, + fetched_at timestamptz NOT NULL DEFAULT now() +); diff --git a/src/api/v1/mcaptcha/mod.rs b/src/api/v1/mcaptcha/mod.rs index a6c05a0c..8a649ea0 100644 --- a/src/api/v1/mcaptcha/mod.rs +++ b/src/api/v1/mcaptcha/mod.rs @@ -18,6 +18,7 @@ pub mod duration; pub mod levels; pub mod mcaptcha; +pub mod stats; pub use super::auth::is_authenticated; diff --git a/src/api/v1/mcaptcha/stats.rs b/src/api/v1/mcaptcha/stats.rs new file mode 100644 index 00000000..958f5907 --- /dev/null +++ b/src/api/v1/mcaptcha/stats.rs @@ -0,0 +1,28 @@ +/* +* Copyright (C) 2021 Aravinth Manivannan +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU Affero General Public License as +* published by the Free Software Foundation, either version 3 of the +* License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Affero General Public License for more details. +* +* You should have received a copy of the GNU Affero General Public License +* along with this program. If not, see . +*/ + +use sqlx::PgPool; + +pub async fn fetched(key: &str, db: &PgPool) { + let _ = sqlx::query!( + "INSERT INTO mcaptcha_pow_fetched_stats + (config_id) VALUES ((SELECT config_id FROM mcaptcha_config WHERE key = $1))", + &key, + ) + .execute(db) + .await; +} diff --git a/src/api/v1/pow/get_config.rs b/src/api/v1/pow/get_config.rs index 5e796219..82c07bce 100644 --- a/src/api/v1/pow/get_config.rs +++ b/src/api/v1/pow/get_config.rs @@ -22,6 +22,7 @@ use serde::{Deserialize, Serialize}; use super::GetDurationResp; use super::I32Levels; +use crate::api::v1::mcaptcha::stats::fetched; use crate::errors::*; use crate::Data; @@ -64,9 +65,12 @@ pub async fn get_config( init_mcaptcha(&data, &payload.key).await?; let config = data .captcha - .get_pow(payload.key) + .get_pow(payload.key.clone()) .await .expect("mcaptcha should be initialized and ready to go"); + // background it. would require data::Data to be static + // to satidfy lifetime + fetched(&payload.key, &data.db).await; Ok(HttpResponse::Ok().json(config)) } }