use crate::{
newtypes::LocalUserId,
- schema::password_reset_request::dsl::{password_reset_request, published, token_encrypted},
+ schema::password_reset_request::dsl::{
+ local_user_id,
+ password_reset_request,
+ published,
+ token_encrypted,
+ },
source::password_reset_request::{PasswordResetRequest, PasswordResetRequestForm},
traits::Crud,
utils::{get_conn, DbPool},
type InsertForm = PasswordResetRequestForm;
type UpdateForm = PasswordResetRequestForm;
type IdType = i32;
- async fn read(pool: &DbPool, password_reset_request_id: i32) -> Result<Self, Error> {
+ async fn read(pool: &mut DbPool<'_>, password_reset_request_id: i32) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
password_reset_request
.find(password_reset_request_id)
.first::<Self>(conn)
.await
}
- async fn create(pool: &DbPool, form: &PasswordResetRequestForm) -> Result<Self, Error> {
+ async fn create(pool: &mut DbPool<'_>, form: &PasswordResetRequestForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(password_reset_request)
.values(form)
.await
}
async fn update(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
password_reset_request_id: i32,
form: &PasswordResetRequestForm,
) -> Result<Self, Error> {
impl PasswordResetRequest {
pub async fn create_token(
- pool: &DbPool,
+ pool: &mut DbPool<'_>,
from_local_user_id: LocalUserId,
token: &str,
) -> Result<PasswordResetRequest, Error> {
Self::create(pool, &form).await
}
- pub async fn read_from_token(pool: &DbPool, token: &str) -> Result<PasswordResetRequest, Error> {
+ pub async fn read_from_token(
+ pool: &mut DbPool<'_>,
+ token: &str,
+ ) -> Result<PasswordResetRequest, Error> {
let conn = &mut get_conn(pool).await?;
let mut hasher = Sha256::new();
hasher.update(token);
.first::<Self>(conn)
.await
}
+
+ pub async fn get_recent_password_resets_count(
+ pool: &mut DbPool<'_>,
+ user_id: LocalUserId,
+ ) -> Result<i64, Error> {
+ let conn = &mut get_conn(pool).await?;
+ password_reset_request
+ .filter(local_user_id.eq(user_id))
+ .filter(published.gt(now - 1.days()))
+ .count()
+ .get_result(conn)
+ .await
+ }
}
fn bytes_to_hex(bytes: Vec<u8>) -> String {
#[cfg(test)]
mod tests {
+ #![allow(clippy::unwrap_used)]
+ #![allow(clippy::indexing_slicing)]
+
use crate::{
source::{
instance::Instance,
#[serial]
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
.await