102 lines
2.6 KiB
Rust
102 lines
2.6 KiB
Rust
|
use channels::emails::{reset_password, test_mail, welcome};
|
||
|
|
||
|
use super::{Error, Result};
|
||
|
use crate::SharedContext;
|
||
|
|
||
|
static STYLE: &str = include_str!("../assets/style.css");
|
||
|
|
||
|
pub async fn test_mail(msg: test_mail::Input, ctx: SharedContext) -> Result<()> {
|
||
|
welcome(
|
||
|
welcome::Input {
|
||
|
login: model::Login::new("Test User"),
|
||
|
email: msg.receiver,
|
||
|
},
|
||
|
ctx,
|
||
|
)
|
||
|
.await
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, serde::Serialize)]
|
||
|
struct ResetPasswordContext {
|
||
|
url: String,
|
||
|
style: &'static str,
|
||
|
}
|
||
|
|
||
|
pub async fn reset_password(msg: reset_password::Input, ctx: SharedContext) -> Result<()> {
|
||
|
let host = { ctx.config.lock().web().host() };
|
||
|
let context = ResetPasswordContext {
|
||
|
url: format!(
|
||
|
"{host}/reset-password/{reset_token}",
|
||
|
reset_token = msg.reset_token
|
||
|
),
|
||
|
style: STYLE,
|
||
|
};
|
||
|
let html = ctx
|
||
|
.template
|
||
|
.render("reset-password", &context)
|
||
|
.map_err(|e| {
|
||
|
tracing::error!("{e:?}");
|
||
|
Error::ResetPassTemplate
|
||
|
})?;
|
||
|
|
||
|
let smtp_from = ctx.config.lock().mail().smtp_from();
|
||
|
let status = ctx
|
||
|
.send_grid
|
||
|
.send(
|
||
|
sendgrid::Mail::new()
|
||
|
.add_to((msg.email.as_str(), msg.login.as_str()).into())
|
||
|
.add_from(&smtp_from)
|
||
|
.add_subject("Reset Password")
|
||
|
.add_html(html.as_str())
|
||
|
.build(),
|
||
|
)
|
||
|
.await;
|
||
|
|
||
|
tracing::debug!("{:?}", status);
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
|
||
|
#[derive(Debug, serde::Serialize)]
|
||
|
struct WelcomeContext {
|
||
|
login: model::Login,
|
||
|
service_name: String,
|
||
|
signature: String,
|
||
|
style: &'static str,
|
||
|
}
|
||
|
|
||
|
pub async fn welcome(msg: welcome::Input, ctx: SharedContext) -> Result<()> {
|
||
|
let (signature, service_name) = {
|
||
|
let l = ctx.config.lock();
|
||
|
let w = l.web();
|
||
|
(w.signature(), w.service_name())
|
||
|
};
|
||
|
let context = WelcomeContext {
|
||
|
login: msg.login.clone(),
|
||
|
service_name,
|
||
|
signature,
|
||
|
style: STYLE,
|
||
|
};
|
||
|
let html = ctx.template.render("welcome", &context).map_err(|e| {
|
||
|
tracing::error!("{e:?}");
|
||
|
Error::ResetPassTemplate
|
||
|
})?;
|
||
|
|
||
|
let smtp_from = ctx.config.lock().mail().smtp_from().clone();
|
||
|
let status = ctx
|
||
|
.send_grid
|
||
|
.send(
|
||
|
sendgrid::Mail::new()
|
||
|
.add_to((msg.email.as_str(), msg.login.as_str()).into())
|
||
|
.add_from(&smtp_from)
|
||
|
.add_subject("Welcome")
|
||
|
.add_html(html.as_str())
|
||
|
.build(),
|
||
|
)
|
||
|
.await;
|
||
|
|
||
|
tracing::debug!("{:?}", status);
|
||
|
|
||
|
Ok(())
|
||
|
}
|