Skip to content

Commit

Permalink
chore(notifications): add LocalizedEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleSamtoshi committed Feb 19, 2024
1 parent fb853aa commit 2992369
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
6 changes: 4 additions & 2 deletions core/notifications/src/email_executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
6 changes: 3 additions & 3 deletions core/notifications/src/email_executor/smtp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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(())
Expand Down
41 changes: 41 additions & 0 deletions core/notifications/src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LocalizedEmail> {
None
}

pub fn circle_threshold_reached(
_locale: &str,
_event: &CircleThresholdReached,
) -> Option<LocalizedEmail> {
None
}

pub fn identity_verification_approved(
_locale: &str,
_event: &IdentityVerificationApproved,
) -> Option<LocalizedEmail> {
None
}

pub fn identity_verification_declined(
_locale: &str,
_event: &IdentityVerificationDeclined,
) -> Option<LocalizedEmail> {
None
}

pub fn identity_verification_review_pending(
_locale: &str,
_event: &IdentityVerificationReviewPending,
) -> Option<LocalizedEmail> {
None
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
39 changes: 39 additions & 0 deletions core/notifications/src/notification_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub trait NotificationEvent: std::fmt::Debug + Into<NotificationEventPayload> +
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<LocalizedEmail>;
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -74,6 +75,24 @@ impl NotificationEvent for NotificationEventPayload {
}
}

fn to_localized_email(&self, locale: GaloyLocale) -> Option<LocalizedEmail> {
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(),
Expand Down Expand Up @@ -116,6 +135,10 @@ impl NotificationEvent for CircleGrew {
PushMessages::circle_grew(locale.as_ref(), self)
}

fn to_localized_email(&self, locale: GaloyLocale) -> Option<LocalizedEmail> {
EmailMessages::circle_grew(locale.as_ref(), self)
}

fn should_send_email(&self) -> bool {
false
}
Expand Down Expand Up @@ -152,6 +175,10 @@ impl NotificationEvent for CircleThresholdReached {
PushMessages::circle_threshold_reached(locale.as_ref(), self)
}

fn to_localized_email(&self, locale: GaloyLocale) -> Option<LocalizedEmail> {
EmailMessages::circle_threshold_reached(locale.as_ref(), self)
}

fn should_send_email(&self) -> bool {
false
}
Expand Down Expand Up @@ -185,6 +212,10 @@ impl NotificationEvent for IdentityVerificationApproved {
PushMessages::identity_verification_approved(locale.as_ref(), self)
}

fn to_localized_email(&self, locale: GaloyLocale) -> Option<LocalizedEmail> {
EmailMessages::identity_verification_approved(locale.as_ref(), self)
}

fn should_send_email(&self) -> bool {
false
}
Expand Down Expand Up @@ -229,6 +260,10 @@ impl NotificationEvent for IdentityVerificationDeclined {
PushMessages::identity_verification_declined(locale.as_ref(), self)
}

fn to_localized_email(&self, locale: GaloyLocale) -> Option<LocalizedEmail> {
EmailMessages::identity_verification_declined(locale.as_ref(), self)
}

fn should_send_email(&self) -> bool {
false
}
Expand Down Expand Up @@ -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<LocalizedEmail> {
EmailMessages::identity_verification_review_pending(locale.as_ref(), self)
}

fn should_send_email(&self) -> bool {
false
}
Expand Down

0 comments on commit 2992369

Please sign in to comment.