Skip to content

Commit d3345d0

Browse files
committed
Replace PublishNotificationsUnsubscribeEmail struct with EmailMessage::from_template()
1 parent 0f2cc3f commit d3345d0

File tree

3 files changed

+23
-31
lines changed

3 files changed

+23
-31
lines changed

src/controllers/user/update.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,23 @@ pub async fn update_user(
7070
let email_address = user.verified_email(&mut conn).await?;
7171

7272
if let Some(email_address) = email_address {
73-
let email = PublishNotificationsUnsubscribeEmail {
74-
user_name: &user.gh_login,
75-
domain: &state.emails.domain,
76-
};
77-
78-
if let Err(error) = state.emails.send(&email_address, email).await {
79-
warn!(
80-
"Failed to send publish notifications unsubscribe email to {email_address}: {error}"
81-
);
73+
let email = EmailMessage::from_template(
74+
"unsubscribe_notifications",
75+
context! {
76+
user_name => user.gh_login,
77+
domain => state.emails.domain
78+
},
79+
);
80+
81+
match email {
82+
Ok(email) => {
83+
if let Err(error) = state.emails.send(&email_address, email).await {
84+
warn!(
85+
"Failed to send publish notifications unsubscribe email to {email_address}: {error}"
86+
);
87+
}
88+
}
89+
Err(error) => warn!("Failed to render unsubscribe email template: {error}"),
8290
}
8391
}
8492
}
@@ -129,25 +137,3 @@ pub async fn update_user(
129137

130138
Ok(OkResponse::new())
131139
}
132-
133-
pub struct PublishNotificationsUnsubscribeEmail<'a> {
134-
pub user_name: &'a str,
135-
pub domain: &'a str,
136-
}
137-
138-
impl crate::email::Email for PublishNotificationsUnsubscribeEmail<'_> {
139-
fn subject(&self) -> String {
140-
"crates.io: Unsubscribed from publish notifications".into()
141-
}
142-
143-
fn body(&self) -> String {
144-
let Self { user_name, domain } = self;
145-
format!(
146-
"Hello {user_name}!
147-
148-
You have been unsubscribed from publish notifications.
149-
150-
If you would like to resubscribe, please visit https://{domain}/settings/profile",
151-
)
152-
}
153-
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Hello {{ user_name }}!
2+
3+
You have been unsubscribed from publish notifications.
4+
5+
If you would like to resubscribe, please visit https://{{ domain }}/settings/profile
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
crates.io: Unsubscribed from publish notifications

0 commit comments

Comments
 (0)