2022-11-09 10:05:00 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::LocalUserId,
|
2022-11-19 04:33:54 +00:00
|
|
|
schema::email_verification::dsl::{
|
|
|
|
email_verification,
|
|
|
|
local_user_id,
|
|
|
|
published,
|
|
|
|
verification_token,
|
|
|
|
},
|
|
|
|
source::email_verification::{EmailVerification, EmailVerificationForm},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2021-12-15 19:49:59 +00:00
|
|
|
};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{
|
|
|
|
dsl::{now, IntervalDsl},
|
|
|
|
insert_into,
|
|
|
|
result::Error,
|
|
|
|
ExpressionMethods,
|
|
|
|
QueryDsl,
|
|
|
|
};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
2021-12-15 19:49:59 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
impl EmailVerification {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn create(pool: &DbPool, form: &EmailVerificationForm) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
insert_into(email_verification)
|
|
|
|
.values(form)
|
|
|
|
.get_result::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn read_for_token(pool: &DbPool, token: &str) -> Result<Self, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
email_verification
|
|
|
|
.filter(verification_token.eq(token))
|
|
|
|
.filter(published.gt(now - 7.days()))
|
|
|
|
.first::<Self>(conn)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn delete_old_tokens_for_local_user(
|
|
|
|
pool: &DbPool,
|
2021-12-15 19:49:59 +00:00
|
|
|
local_user_id_: LocalUserId,
|
|
|
|
) -> Result<usize, Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
diesel::delete(email_verification.filter(local_user_id.eq(local_user_id_)))
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2021-12-15 19:49:59 +00:00
|
|
|
}
|
|
|
|
}
|