Merge pull request #119 from mCaptcha/fix-progress-bar

fix: create max_recorded nonce for existing captcha configs
This commit is contained in:
Aravinth Manivannan 2023-10-29 13:03:57 +00:00 committed by GitHub
commit 0a3d93453e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 6 deletions

View file

@ -0,0 +1,12 @@
{
"db_name": "MySQL",
"query": "INSERT INTO\n mcaptcha_track_nonce (level_id, nonce)\n VALUES ((\n SELECT\n level_id\n FROM\n mcaptcha_levels\n WHERE\n config_id = (SELECT config_id FROM mcaptcha_config WHERE captcha_key =?)\n AND\n difficulty_factor = ?\n ), ?);",
"describe": {
"columns": [],
"parameters": {
"Right": 3
},
"nullable": []
},
"hash": "9def82dcec9c8d477824182bb2f71044cc264cf2073ab4f60a0000b435ed0f0b"
}

View file

@ -1163,7 +1163,12 @@ impl MCDatabase for Database {
nonce: i32, nonce: i32,
} }
let res = sqlx::query_as!( async fn inner_get_max_nonce(
pool: &MySqlPool,
captcha_key: &str,
difficulty_factor: u32,
) -> DBResult<X> {
sqlx::query_as!(
X, X,
"SELECT nonce FROM mcaptcha_track_nonce "SELECT nonce FROM mcaptcha_track_nonce
WHERE level_id = ( WHERE level_id = (
@ -1179,10 +1184,40 @@ impl MCDatabase for Database {
&captcha_key, &captcha_key,
difficulty_factor as i32, 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 captcha_key =?)
AND
difficulty_factor = ?
), ?);",
&captcha_key,
difficulty_factor as i32,
0,
)
.execute(&self.pool)
.await
.map_err(|e| map_row_not_found_err(e, DBError::CaptchaNotFound))?; .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)
}
} }
} }

View file

@ -0,0 +1,16 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO\n mcaptcha_track_nonce (level_id, nonce)\n VALUES ((\n SELECT\n level_id\n FROM\n mcaptcha_levels\n WHERE\n config_id = (SELECT config_id FROM mcaptcha_config WHERE key = ($1))\n AND\n difficulty_factor = $2\n ), $3);",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Int4",
"Int4"
]
},
"nullable": []
},
"hash": "e0088cf77c1c3a0184f35d1899a6168023fba021adf281cf1c8f9e8ccfe3a03e"
}

View file

@ -1170,7 +1170,12 @@ impl MCDatabase for Database {
nonce: i32, 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, X,
"SELECT nonce FROM mcaptcha_track_nonce "SELECT nonce FROM mcaptcha_track_nonce
WHERE level_id = ( WHERE level_id = (
@ -1186,10 +1191,41 @@ impl MCDatabase for Database {
&captcha_key, &captcha_key,
difficulty_factor as i32, 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))?; .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)
}
} }
} }