Skip to content

Commit

Permalink
chore(notifications): add Email as new notification channel
Browse files Browse the repository at this point in the history
  • Loading branch information
thevaibhav-dixit committed Feb 15, 2024
1 parent 2a80600 commit 720d9c1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions core/notifications/proto/notifications.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ service NotificationsService {

enum NotificationChannel {
PUSH = 0;
EMAIL = 1;
}

enum NotificationCategory {
Expand Down
2 changes: 1 addition & 1 deletion core/notifications/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl NotificationsApp {
pub async fn init(pool: Pool<Postgres>, config: AppConfig) -> Result<Self, ApplicationError> {
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,
Expand Down
5 changes: 4 additions & 1 deletion core/notifications/src/email_executor/error.rs
Original file line number Diff line number Diff line change
@@ -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),
}
8 changes: 5 additions & 3 deletions core/notifications/src/email_executor/lettre/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use lettre::{
AsyncSmtpTransport, AsyncTransport, Tokio1Executor,
};

use crate::messages::LocalizedMessage;

pub use config::*;
use error::*;

Expand All @@ -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(())
}
Expand Down
25 changes: 21 additions & 4 deletions core/notifications/src/email_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Self, EmailExecutorError> {
pub fn init(
config: EmailExecutorConfig,
settings: UserNotificationSettingsRepo,
) -> Result<Self, EmailExecutorError> {
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<T: NotificationEvent>(&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(())
}
}
1 change: 1 addition & 0 deletions core/notifications/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 720d9c1

Please sign in to comment.