diff --git a/core/notifications/proto/notifications.proto b/core/notifications/proto/notifications.proto index 8298a7be4a1..ba37c1a1303 100644 --- a/core/notifications/proto/notifications.proto +++ b/core/notifications/proto/notifications.proto @@ -19,6 +19,7 @@ service NotificationsService { enum NotificationChannel { PUSH = 0; + EMAIL = 1; } enum NotificationCategory { diff --git a/core/notifications/src/app/mod.rs b/core/notifications/src/app/mod.rs index 5c9af50b5f0..95e9e4b3424 100644 --- a/core/notifications/src/app/mod.rs +++ b/core/notifications/src/app/mod.rs @@ -27,7 +27,7 @@ impl NotificationsApp { pub async fn init(pool: Pool, config: AppConfig) -> Result { let settings = UserNotificationSettingsRepo::new(&pool); let executor = Executor::init(config.executor.clone(), settings.clone()).await?; - let _email_executor = EmailExecutor::init(config.email_executor.clone())?; + let _email_executor = EmailExecutor::init(config.email_executor.clone(), settings.clone())?; let runner = job::start_job_runner(&pool, executor).await?; Ok(Self { _config: config, diff --git a/core/notifications/src/email_executor/error.rs b/core/notifications/src/email_executor/error.rs index e1e78be0c88..98bbf31d096 100644 --- a/core/notifications/src/email_executor/error.rs +++ b/core/notifications/src/email_executor/error.rs @@ -1,9 +1,12 @@ use thiserror::Error; use super::lettre::error::LettreError; +use crate::user_notification_settings::error::*; #[derive(Error, Debug)] pub enum EmailExecutorError { - #[error("EmailExecutorError - Lettre error: {0}")] + #[error("EmailExecutorError - LettreError: {0}")] LettreError(#[from] LettreError), + #[error("EmailExecutorError - UserNotificationSettingsError: {0}")] + UserNotificationSettingsError(#[from] UserNotificationSettingsError), } diff --git a/core/notifications/src/email_executor/lettre/mod.rs b/core/notifications/src/email_executor/lettre/mod.rs index a91077e4d7c..1052d43fe42 100644 --- a/core/notifications/src/email_executor/lettre/mod.rs +++ b/core/notifications/src/email_executor/lettre/mod.rs @@ -7,6 +7,8 @@ use lettre::{ AsyncSmtpTransport, AsyncTransport, Tokio1Executor, }; +use crate::messages::LocalizedMessage; + pub use config::*; use error::*; @@ -25,12 +27,12 @@ impl LettreClient { Ok(Self { client }) } - pub async fn send_email(&self, title: String, body: String) -> Result<(), LettreError> { + pub async fn send_email(&self, msg: LocalizedMessage) -> Result<(), LettreError> { let email = Message::builder() .from(Mailbox::new(None, "some-email".parse()?)) .to(Mailbox::new(None, "some-email".parse()?)) - .subject(title) - .body(body)?; + .subject(msg.title) + .body(msg.body)?; self.client.send(email).await?; Ok(()) } diff --git a/core/notifications/src/email_executor/mod.rs b/core/notifications/src/email_executor/mod.rs index 59a0ff3880c..0fe51bed39b 100644 --- a/core/notifications/src/email_executor/mod.rs +++ b/core/notifications/src/email_executor/mod.rs @@ -2,6 +2,8 @@ mod config; pub mod error; mod lettre; +use crate::{notification_event::*, primitives::*, user_notification_settings::*}; + pub use config::*; use error::*; use lettre::LettreClient; @@ -10,16 +12,31 @@ use lettre::LettreClient; pub struct EmailExecutor { pub config: EmailExecutorConfig, lettre: LettreClient, + settings: UserNotificationSettingsRepo, } impl EmailExecutor { - pub fn init(config: EmailExecutorConfig) -> Result { + pub fn init( + config: EmailExecutorConfig, + settings: UserNotificationSettingsRepo, + ) -> Result { let lettre = LettreClient::init(config.lettre.clone())?; - Ok(EmailExecutor { config, lettre }) + Ok(EmailExecutor { + config, + lettre, + settings, + }) } - pub async fn notify(&self, title: String, body: String) -> Result<(), EmailExecutorError> { - self.lettre.send_email(title, body).await?; + pub async fn notify(&self, event: &T) -> Result<(), EmailExecutorError> { + let settings = self.settings.find_for_user_id(event.user_id()).await?; + if !settings.should_send_notification(UserNotificationChannel::Email, event.category()) { + return Ok(()); + } + let msg = event.to_localized_msg(settings.locale().unwrap_or_default()); + + self.lettre.send_email(msg).await?; + Ok(()) } } diff --git a/core/notifications/src/primitives.rs b/core/notifications/src/primitives.rs index db2cdda597f..85cf98d6be1 100644 --- a/core/notifications/src/primitives.rs +++ b/core/notifications/src/primitives.rs @@ -84,6 +84,7 @@ impl std::fmt::Display for PushDeviceToken { #[graphql(name = "UserNotificationChannel")] pub enum UserNotificationChannel { Push, + Email, } #[derive(async_graphql::Enum, Debug, Hash, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)]