1
0
Fork 0
mirror of https://github.com/mCaptcha/mCaptcha.git synced 2025-05-03 21:52:49 +03:00

fix: create max_recorded nonce for existing captcha configs

This commit is contained in:
Aravinth Manivannan 2023-10-29 18:11:06 +05:30
parent 3a787a6592
commit 939fb5f8b9
No known key found for this signature in database
GPG key ID: F8F50389936984FF
4 changed files with 105 additions and 6 deletions
db/db-sqlx-postgres/src

View file

@ -1170,7 +1170,12 @@ impl MCDatabase for Database {
nonce: i32,
}
let res = sqlx::query_as!(
async fn inner_get_max_nonce(
pool: &PgPool,
captcha_key: &str,
difficulty_factor: u32,
) -> DBResult<X> {
sqlx::query_as!(
X,
"SELECT nonce FROM mcaptcha_track_nonce
WHERE level_id = (
@ -1186,10 +1191,41 @@ impl MCDatabase for Database {
&captcha_key,
difficulty_factor as i32,
)
.fetch_one(&self.pool).await
.fetch_one(pool)
.await
.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))
}
let res = inner_get_max_nonce(&self.pool, captcha_key, difficulty_factor).await;
if let Err(DBError::CaptchaNotFound) = res {
sqlx::query!(
"INSERT INTO
mcaptcha_track_nonce (level_id, nonce)
VALUES ((
SELECT
level_id
FROM
mcaptcha_levels
WHERE
config_id = (SELECT config_id FROM mcaptcha_config WHERE key = ($1))
AND
difficulty_factor = $2
), $3);",
&captcha_key,
difficulty_factor as i32,
0,
)
.execute(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?;
Ok(res.nonce as u32)
let res =
inner_get_max_nonce(&self.pool, captcha_key, difficulty_factor).await?;
Ok(res.nonce as u32)
} else {
let res = res?;
Ok(res.nonce as u32)
}
}
}