Implemented mail notifications.

This commit is contained in:
2024-02-12 17:17:26 +01:00
parent d18ef72d03
commit 1b6f544e55
9 changed files with 720 additions and 78 deletions
+31
View File
@@ -10,6 +10,7 @@ cfg_if! { if #[cfg(feature = "ssr")] {
use leptos_actix::{extract, redirect};
use log::{info, warn};
use crate::error::AppError;
use crate::backend::get_pool;
pub async fn has_admin_user(pool: &PgPool) -> Result<bool, Error> {
let count: (i64,) = query_as(r#"SELECT COUNT(id) FROM "user" WHERE admin = $1"#)
@@ -68,6 +69,36 @@ cfg_if! { if #[cfg(feature = "ssr")] {
false
}
}
pub async fn admin_email() -> Option<String> {
let pool = get_pool().await.ok()?;
let mail: Result<(String,), Error> = query_as(r#"SELECT email FROM "user" WHERE login = 'admin'"#)
.fetch_one(&pool)
.await;
if let Ok(m) = mail {
Some(m.0)
} else {
None
}
}
pub async fn emails_for_notify() -> Result<Vec<String>, ServerFnError> {
let pool = get_pool().await?;
let mails: Result<Vec<(String,)>, Error> = query_as(r#"SELECT email FROM "user" WHERE (email IS NOT NULL OR email <> '') AND get_emails = true"#)
.fetch_all(&pool)
.await;
if let Err(e) = mails {
if matches!(e, Error::RowNotFound) {
Ok(vec![])
} else {
Err(e.into())
}
} else {
Ok(mails.unwrap().into_iter().map(|m| m.0).collect())
}
}
}}
#[server]