lemmy/crates/db_schema/src/impls/email_verification.rs
Nutomic 6f3bf4634b
Various pedantic clippy fixes (#2568)
* Various pedantic clippy fixes

* more clippy pedantic fixes

* try to fix ci

* add fix clippy script, use rust 1.65

* fix clippy
2022-11-19 04:33:54 +00:00

47 lines
1.2 KiB
Rust

use crate::{
newtypes::LocalUserId,
schema::email_verification::dsl::{
email_verification,
local_user_id,
published,
verification_token,
},
source::email_verification::{EmailVerification, EmailVerificationForm},
utils::{get_conn, DbPool},
};
use diesel::{
dsl::{now, IntervalDsl},
insert_into,
result::Error,
ExpressionMethods,
QueryDsl,
};
use diesel_async::RunQueryDsl;
impl EmailVerification {
pub async fn create(pool: &DbPool, form: &EmailVerificationForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(email_verification)
.values(form)
.get_result::<Self>(conn)
.await
}
pub async fn read_for_token(pool: &DbPool, token: &str) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
email_verification
.filter(verification_token.eq(token))
.filter(published.gt(now - 7.days()))
.first::<Self>(conn)
.await
}
pub async fn delete_old_tokens_for_local_user(
pool: &DbPool,
local_user_id_: LocalUserId,
) -> Result<usize, Error> {
let conn = &mut get_conn(pool).await?;
diesel::delete(email_verification.filter(local_user_id.eq(local_user_id_)))
.execute(conn)
.await
}
}