X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fdb_schema%2Fsrc%2Fimpls%2Fpassword_reset_request.rs;h=ae4483d6ef53ef3bfdfae50a9f84ea7c6d0f92ef;hb=92568956353f21649ed9aff68b42699c9d036f30;hp=ebe8a00ecff87738618c5327309caf65d94abbcb;hpb=d9e7f0100ad14121b43b89d54b80fb2345d8d83e;p=lemmy.git diff --git a/crates/db_schema/src/impls/password_reset_request.rs b/crates/db_schema/src/impls/password_reset_request.rs index ebe8a00e..ae4483d6 100644 --- a/crates/db_schema/src/impls/password_reset_request.rs +++ b/crates/db_schema/src/impls/password_reset_request.rs @@ -1,6 +1,11 @@ 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}, @@ -19,14 +24,14 @@ impl Crud for PasswordResetRequest { type InsertForm = PasswordResetRequestForm; type UpdateForm = PasswordResetRequestForm; type IdType = i32; - async fn read(pool: &DbPool, password_reset_request_id: i32) -> Result { + async fn read(pool: &mut DbPool<'_>, password_reset_request_id: i32) -> Result { let conn = &mut get_conn(pool).await?; password_reset_request .find(password_reset_request_id) .first::(conn) .await } - async fn create(pool: &DbPool, form: &PasswordResetRequestForm) -> Result { + async fn create(pool: &mut DbPool<'_>, form: &PasswordResetRequestForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(password_reset_request) .values(form) @@ -34,7 +39,7 @@ impl Crud for PasswordResetRequest { .await } async fn update( - pool: &DbPool, + pool: &mut DbPool<'_>, password_reset_request_id: i32, form: &PasswordResetRequestForm, ) -> Result { @@ -48,7 +53,7 @@ impl Crud for PasswordResetRequest { impl PasswordResetRequest { pub async fn create_token( - pool: &DbPool, + pool: &mut DbPool<'_>, from_local_user_id: LocalUserId, token: &str, ) -> Result { @@ -63,7 +68,10 @@ impl PasswordResetRequest { Self::create(pool, &form).await } - pub async fn read_from_token(pool: &DbPool, token: &str) -> Result { + pub async fn read_from_token( + pool: &mut DbPool<'_>, + token: &str, + ) -> Result { let conn = &mut get_conn(pool).await?; let mut hasher = Sha256::new(); hasher.update(token); @@ -74,6 +82,19 @@ impl PasswordResetRequest { .first::(conn) .await } + + pub async fn get_recent_password_resets_count( + pool: &mut DbPool<'_>, + user_id: LocalUserId, + ) -> Result { + 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) -> String { @@ -86,6 +107,9 @@ fn bytes_to_hex(bytes: Vec) -> String { #[cfg(test)] mod tests { + #![allow(clippy::unwrap_used)] + #![allow(clippy::indexing_slicing)] + use crate::{ source::{ instance::Instance, @@ -102,6 +126,7 @@ mod tests { #[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