use crate::{
newtypes::LocalUserId,
- schema::password_reset_request::dsl::*,
- source::password_reset_request::*,
+ 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},
};
-use diesel::{dsl::*, result::Error, ExpressionMethods, QueryDsl};
+use diesel::{
+ dsl::{insert_into, now, IntervalDsl},
+ result::Error,
+ ExpressionMethods,
+ QueryDsl,
+};
use diesel_async::RunQueryDsl;
use sha2::{Digest, Sha256};
type InsertForm = PasswordResetRequestForm;
type UpdateForm = PasswordResetRequestForm;
type IdType = i32;
- async fn read(pool: &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 {
let mut str = String::new();
for byte in bytes {
- str = format!("{}{:02x}", str, byte);
+ str = format!("{str}{byte:02x}");
}
str
}
#[cfg(test)]
mod tests {
+ #![allow(clippy::unwrap_used)]
+ #![allow(clippy::indexing_slicing)]
+
use crate::{
source::{
instance::Instance,
local_user::{LocalUser, LocalUserInsertForm},
password_reset_request::PasswordResetRequest,
- person::*,
+ person::{Person, PersonInsertForm},
},
traits::Crud,
utils::build_db_pool_for_tests,
#[serial]
async fn test_crud() {
let pool = &build_db_pool_for_tests().await;
+ let pool = &mut pool.into();
- let inserted_instance = Instance::create(pool, "my_domain.tld").await.unwrap();
+ let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+ .await
+ .unwrap();
let new_person = PersonInsertForm::builder()
.name("thommy prw".into())