lemmy/crates/db_schema/src/impls/email_verification.rs
Dessalines d075acce43
Make all single-fetch database calls return an Option. (#4617)
- Diesel ordinarily throws an error when no results are returned for a
  single fetch, which is a bit confusing. This PR ensures that the
  missing value cases are all caught, and wrapped with new LemmyErrors,
  rather than diesel errors.
- Fixes #4601
2024-04-16 14:48:15 +02:00

52 lines
1.3 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,
sql_types::Timestamptz,
ExpressionMethods,
IntoSql,
OptionalExtension,
QueryDsl,
};
use diesel_async::RunQueryDsl;
impl EmailVerification {
pub async fn create(pool: &mut DbPool<'_>, form: &EmailVerificationForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(email_verification)
.values(form)
.get_result(conn)
.await
}
pub async fn read_for_token(pool: &mut DbPool<'_>, token: &str) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
email_verification
.filter(verification_token.eq(token))
.filter(published.gt(now.into_sql::<Timestamptz>() - 7.days()))
.first(conn)
.await
.optional()
}
pub async fn delete_old_tokens_for_local_user(
pool: &mut 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
}
}