pub mod account; pub mod live; pub mod log; pub mod smtp; pub mod templates; pub mod worker; use std::future::Future; use std::pin::Pin; use forge_core::mail::{MailError, MailKind, OutgoingMail}; use crate::state::AppState; pub use log::LogMailer; pub use smtp::SmtpMailer; pub type SendFuture<'a> = Pin> + Send + 'a>>; pub trait Mailer: Send + Sync { fn send<'a>(&'a self, mail: &'a OutgoingMail) -> SendFuture<'a>; } pub async fn enqueue( state: &AppState, idem_key: &str, kind: MailKind, user_id: Option<&str>, to: &str, subject: &str, body: &str, ) -> forge_core::Result { let mail = OutgoingMail { to: to.to_owned(), subject: subject.to_owned(), body: body.to_owned(), message_id: None, in_reply_to: None, kind, user_id: user_id.map(str::to_owned), }; enqueue_message(state, idem_key, &mail).await } pub async fn enqueue_message( state: &AppState, idem_key: &str, mail: &OutgoingMail, ) -> forge_core::Result { let queued = state.store.enqueue_mail(idem_key, mail).await?; if queued { state.mail_waker.notify_one(); live::bump(); } Ok(queued) } #[cfg(any(test, feature = "test-support"))] #[derive(Default)] pub struct FakeMailer { sent: std::sync::Mutex>, failures: std::sync::Mutex>, } #[cfg(any(test, feature = "test-support"))] impl FakeMailer { pub fn sent(&self) -> Vec { self.sent .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .clone() } pub fn fail_next(&self, error: MailError) { self.failures .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .push_back(error); } } #[cfg(any(test, feature = "test-support"))] impl Mailer for FakeMailer { fn send<'a>(&'a self, mail: &'a OutgoingMail) -> SendFuture<'a> { Box::pin(async move { let failure = self .failures .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .pop_front(); if let Some(error) = failure { return Err(error); } self.sent .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .push(mail.clone()); Ok(()) }) } }