From 29923697dbf2c31aad551f7f9d6063cdae6c91c5 Mon Sep 17 00:00:00 2001 From: Sam Peters Date: Mon, 19 Feb 2024 17:48:46 -0600 Subject: [PATCH] chore(notifications): add LocalizedEmail --- core/notifications/src/email_executor/mod.rs | 6 ++- .../src/email_executor/smtp/mod.rs | 6 +-- core/notifications/src/messages/mod.rs | 41 +++++++++++++++++++ core/notifications/src/notification_event.rs | 39 ++++++++++++++++++ 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/core/notifications/src/email_executor/mod.rs b/core/notifications/src/email_executor/mod.rs index 30f1250229..2fd688f4b2 100644 --- a/core/notifications/src/email_executor/mod.rs +++ b/core/notifications/src/email_executor/mod.rs @@ -39,8 +39,10 @@ impl EmailExecutor { .ok() .and_then(|s| s.email_address().map(|addr| (s, addr))) { - let msg = event.to_localized_push_msg(settings.locale().unwrap_or_default()); - self.smtp.send_email(msg, addr).await?; + let msg = event.to_localized_email(settings.locale().unwrap_or_default()); + if let Some(msg) = msg { + self.smtp.send_email(msg, addr).await?; + } } Ok(()) } diff --git a/core/notifications/src/email_executor/smtp/mod.rs b/core/notifications/src/email_executor/smtp/mod.rs index dde967f93b..8e14881c9a 100644 --- a/core/notifications/src/email_executor/smtp/mod.rs +++ b/core/notifications/src/email_executor/smtp/mod.rs @@ -7,7 +7,7 @@ use lettre::{ AsyncSmtpTransport, AsyncTransport, Tokio1Executor, }; -use crate::{messages::LocalizedPushMessage, primitives::GaloyEmailAddress}; +use crate::{messages::LocalizedEmail, primitives::GaloyEmailAddress}; pub use config::*; use error::*; @@ -33,13 +33,13 @@ impl SmtpClient { pub async fn send_email( &self, - msg: LocalizedPushMessage, + msg: LocalizedEmail, recipient_addr: GaloyEmailAddress, ) -> Result<(), SmtpError> { let email = Message::builder() .from(Mailbox::new(None, self.from_email.parse()?)) .to(Mailbox::new(None, recipient_addr.into_inner().parse()?)) - .subject(msg.title) + .subject(msg.subject) .body(msg.body)?; self.client.send(email).await?; Ok(()) diff --git a/core/notifications/src/messages/mod.rs b/core/notifications/src/messages/mod.rs index 63aaf05fce..3c76af508b 100644 --- a/core/notifications/src/messages/mod.rs +++ b/core/notifications/src/messages/mod.rs @@ -136,6 +136,47 @@ impl PushMessages { } } +pub struct LocalizedEmail { + pub subject: String, + pub body: String, +} + +pub struct EmailMessages {} + +impl EmailMessages { + pub fn circle_grew(_locale: &str, _event: &CircleGrew) -> Option { + None + } + + pub fn circle_threshold_reached( + _locale: &str, + _event: &CircleThresholdReached, + ) -> Option { + None + } + + pub fn identity_verification_approved( + _locale: &str, + _event: &IdentityVerificationApproved, + ) -> Option { + None + } + + pub fn identity_verification_declined( + _locale: &str, + _event: &IdentityVerificationDeclined, + ) -> Option { + None + } + + pub fn identity_verification_review_pending( + _locale: &str, + _event: &IdentityVerificationReviewPending, + ) -> Option { + None + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/core/notifications/src/notification_event.rs b/core/notifications/src/notification_event.rs index 93bdd9e37b..1b8a1bb423 100644 --- a/core/notifications/src/notification_event.rs +++ b/core/notifications/src/notification_event.rs @@ -13,6 +13,7 @@ pub trait NotificationEvent: std::fmt::Debug + Into + fn deep_link(&self) -> DeepLink; fn to_localized_push_msg(&self, locale: GaloyLocale) -> LocalizedPushMessage; fn should_send_email(&self) -> bool; + fn to_localized_email(&self, locale: GaloyLocale) -> Option; } #[derive(Debug, Serialize, Deserialize, Clone)] @@ -74,6 +75,24 @@ impl NotificationEvent for NotificationEventPayload { } } + fn to_localized_email(&self, locale: GaloyLocale) -> Option { + match self { + NotificationEventPayload::CircleGrew(event) => event.to_localized_email(locale), + NotificationEventPayload::CircleThresholdReached(event) => { + event.to_localized_email(locale) + } + NotificationEventPayload::IdentityVerificationApproved(event) => { + event.to_localized_email(locale) + } + NotificationEventPayload::IdentityVerificationDeclined(event) => { + event.to_localized_email(locale) + } + NotificationEventPayload::IdentityVerificationReviewPending(event) => { + event.to_localized_email(locale) + } + } + } + fn should_send_email(&self) -> bool { match self { NotificationEventPayload::CircleGrew(event) => event.should_send_email(), @@ -116,6 +135,10 @@ impl NotificationEvent for CircleGrew { PushMessages::circle_grew(locale.as_ref(), self) } + fn to_localized_email(&self, locale: GaloyLocale) -> Option { + EmailMessages::circle_grew(locale.as_ref(), self) + } + fn should_send_email(&self) -> bool { false } @@ -152,6 +175,10 @@ impl NotificationEvent for CircleThresholdReached { PushMessages::circle_threshold_reached(locale.as_ref(), self) } + fn to_localized_email(&self, locale: GaloyLocale) -> Option { + EmailMessages::circle_threshold_reached(locale.as_ref(), self) + } + fn should_send_email(&self) -> bool { false } @@ -185,6 +212,10 @@ impl NotificationEvent for IdentityVerificationApproved { PushMessages::identity_verification_approved(locale.as_ref(), self) } + fn to_localized_email(&self, locale: GaloyLocale) -> Option { + EmailMessages::identity_verification_approved(locale.as_ref(), self) + } + fn should_send_email(&self) -> bool { false } @@ -229,6 +260,10 @@ impl NotificationEvent for IdentityVerificationDeclined { PushMessages::identity_verification_declined(locale.as_ref(), self) } + fn to_localized_email(&self, locale: GaloyLocale) -> Option { + EmailMessages::identity_verification_declined(locale.as_ref(), self) + } + fn should_send_email(&self) -> bool { false } @@ -262,6 +297,10 @@ impl NotificationEvent for IdentityVerificationReviewPending { PushMessages::identity_verification_review_pending(locale.as_ref(), self) } + fn to_localized_email(&self, locale: GaloyLocale) -> Option { + EmailMessages::identity_verification_review_pending(locale.as_ref(), self) + } + fn should_send_email(&self) -> bool { false }