mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 18:15:39 +00:00
feat: add database method to get all easy captcha configurations with pagination
This commit is contained in:
@@ -1273,6 +1273,54 @@ impl MCDatabase for Database {
|
||||
Err(e) => Err(map_row_not_found_err(e, DBError::CaptchaNotFound)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Get all easy captcha configurations on instance
|
||||
async fn get_all_easy_captchas(
|
||||
&self,
|
||||
limit: usize,
|
||||
offset: usize,
|
||||
) -> DBResult<Vec<EasyCaptcha>> {
|
||||
|
||||
struct InnerEasyCaptcha {
|
||||
captcha_key: String,
|
||||
peak_sustainable_traffic: i32,
|
||||
avg_traffic: i32,
|
||||
broke_my_site_traffic: Option<i32>,
|
||||
}
|
||||
let mut inner_res = sqlx::query_as!(
|
||||
InnerEasyCaptcha,
|
||||
"SELECT
|
||||
mcaptcha_sitekey_user_provided_avg_traffic.avg_traffic,
|
||||
mcaptcha_sitekey_user_provided_avg_traffic.peak_sustainable_traffic,
|
||||
mcaptcha_sitekey_user_provided_avg_traffic.broke_my_site_traffic,
|
||||
mcaptcha_config.captcha_key
|
||||
FROM
|
||||
mcaptcha_sitekey_user_provided_avg_traffic
|
||||
INNER JOIN
|
||||
mcaptcha_config
|
||||
ON
|
||||
mcaptcha_config.config_id = mcaptcha_sitekey_user_provided_avg_traffic.config_id
|
||||
ORDER BY mcaptcha_config.config_id
|
||||
LIMIT ? OFFSET ?",
|
||||
limit as i64,
|
||||
offset as i64
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| map_row_not_found_err(e, DBError::TrafficPatternNotFound))?;
|
||||
let mut res = Vec::with_capacity(inner_res.len());
|
||||
inner_res.drain(0..).for_each(|v|
|
||||
res.push(EasyCaptcha {
|
||||
key: v.captcha_key,
|
||||
traffic_pattern: TrafficPattern {
|
||||
broke_my_site_traffic: v.broke_my_site_traffic.as_ref().map(|v| *v as u32),
|
||||
avg_traffic: v.avg_traffic as u32,
|
||||
peak_sustainable_traffic: v.peak_sustainable_traffic as u32,
|
||||
}
|
||||
}));
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
Reference in New Issue
Block a user