Skip to content

Commit c9e92df

Browse files
committed
Replace PublishNotificationEmail struct with EmailMessage::from_template()
1 parent c9f5e34 commit c9e92df

File tree

3 files changed

+30
-47
lines changed

3 files changed

+30
-47
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Hello {{ recipient }}!
2+
3+
A new version of the package {{ krate }} ({{ version }}) was published{{ publisher_info }} at {{ publish_time }}.
4+
5+
If you have questions or security concerns, you can contact us at help@crates.io. If you would like to stop receiving these security notifications, you can disable them in your account settings.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
crates.io: Successfully published {{ krate }}@{{ version }}

src/worker/jobs/send_publish_notifications.rs

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::email::Email;
1+
use crate::email::EmailMessage;
22
use crate::models::OwnerKind;
33
use crate::schema::{crate_owners, crates, emails, users, versions};
44
use crate::worker::Environment;
@@ -7,7 +7,9 @@ use chrono::{DateTime, SecondsFormat, Utc};
77
use crates_io_worker::BackgroundJob;
88
use diesel::prelude::*;
99
use diesel_async::{AsyncPgConnection, RunQueryDsl};
10+
use minijinja::context;
1011
use std::sync::Arc;
12+
use tracing::warn;
1113

1214
/// Background job that sends email notifications to all crate owners when a
1315
/// new crate version is published.
@@ -83,18 +85,29 @@ impl BackgroundJob for SendPublishNotificationsJob {
8385
None => "",
8486
};
8587

86-
let email = PublishNotificationEmail {
87-
recipient,
88-
krate,
89-
version,
90-
publish_time: &publish_time,
91-
publisher_info,
92-
};
88+
let email = EmailMessage::from_template(
89+
"publish_notification",
90+
context! {
91+
recipient => recipient,
92+
krate => krate,
93+
version => version,
94+
publish_time => publish_time,
95+
publisher_info => publisher_info
96+
},
97+
);
9398

9499
debug!("Sending publish notification for {krate}@{version} to {email_address}…");
95-
let result = ctx.emails.send(&email_address, email).await.inspect_err(|err| {
96-
warn!("Failed to send publish notification for {krate}@{version} to {email_address}: {err}")
97-
});
100+
let result = match email {
101+
Ok(email_msg) => {
102+
ctx.emails.send(&email_address, email_msg).await.inspect_err(|err| {
103+
warn!("Failed to send publish notification for {krate}@{version} to {email_address}: {err}")
104+
}).map_err(|_| ())
105+
}
106+
Err(err) => {
107+
warn!("Failed to render publish notification email template for {krate}@{version} to {email_address}: {err}");
108+
Err(())
109+
}
110+
};
98111

99112
results.push(result);
100113
}
@@ -153,39 +166,3 @@ impl PublishDetails {
153166
.await
154167
}
155168
}
156-
157-
/// Email template for notifying crate owners about a new crate version
158-
/// being published.
159-
#[derive(Debug, Clone)]
160-
struct PublishNotificationEmail<'a> {
161-
recipient: &'a str,
162-
krate: &'a str,
163-
version: &'a str,
164-
publish_time: &'a str,
165-
publisher_info: &'a str,
166-
}
167-
168-
impl Email for PublishNotificationEmail<'_> {
169-
fn subject(&self) -> String {
170-
let Self { krate, version, .. } = self;
171-
format!("crates.io: Successfully published {krate}@{version}")
172-
}
173-
174-
fn body(&self) -> String {
175-
let Self {
176-
recipient,
177-
krate,
178-
version,
179-
publish_time,
180-
publisher_info,
181-
} = self;
182-
183-
format!(
184-
"Hello {recipient}!
185-
186-
A new version of the package {krate} ({version}) was published{publisher_info} at {publish_time}.
187-
188-
If you have questions or security concerns, you can contact us at help@crates.io. If you would like to stop receiving these security notifications, you can disable them in your account settings."
189-
)
190-
}
191-
}

0 commit comments

Comments
 (0)