pow stats

This commit is contained in:
realaravinth 2021-04-30 11:14:29 +05:30
parent a3ba746b6a
commit a5cfa3b305
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
5 changed files with 39 additions and 2 deletions

View file

@ -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 <realaravinth@batsense.net>"]
edition = "2018"
default-run = "guard"

View file

@ -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()
);

View file

@ -18,6 +18,7 @@
pub mod duration;
pub mod levels;
pub mod mcaptcha;
pub mod stats;
pub use super::auth::is_authenticated;

View file

@ -0,0 +1,28 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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;
}

View file

@ -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))
}
}