Skip to content

Commit f9469e0

Browse files
committed
Replace ExpiryNotificationEmail struct with EmailMessage::from_template()
1 parent 6323cee commit f9469e0

File tree

3 files changed

+20
-41
lines changed

3 files changed

+20
-41
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Hi {{ name }},
2+
3+
We noticed your token "{{ token_name }}" will expire on {{ expiry_date }}.
4+
5+
If this token is still needed, visit https://crates.io/settings/tokens/new?from={{ token_id }} to generate a new one.
6+
7+
Thanks,
8+
The crates.io team
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
crates.io: Your API token "{{ token_name }}" is about to expire

src/worker/jobs/expiry_notification.rs

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use crate::models::ApiToken;
22
use crate::schema::api_tokens;
3-
use crate::{Emails, email::Email, models::User, worker::Environment};
3+
use crate::{Emails, email::EmailMessage, models::User, worker::Environment};
44
use chrono::SecondsFormat;
55
use crates_io_worker::BackgroundJob;
66
use diesel::dsl::now;
77
use diesel::prelude::*;
88
use diesel::sql_types::Timestamptz;
99
use diesel_async::{AsyncPgConnection, RunQueryDsl};
10+
use minijinja::context;
1011
use std::sync::Arc;
1112

1213
/// The threshold for the expiry notification.
@@ -83,12 +84,15 @@ async fn handle_expiring_token(
8384
let recipient = user.email(conn).await?;
8485
if let Some(recipient) = recipient {
8586
debug!("Sending expiry notification to {}…", recipient);
86-
let email = ExpiryNotificationEmail {
87-
name: &user.gh_login,
88-
token_id: token.id,
89-
token_name: &token.name,
90-
expiry_date: token.expired_at.unwrap(),
91-
};
87+
let email = EmailMessage::from_template(
88+
"expiry_notification",
89+
context! {
90+
name => user.gh_login,
91+
token_id => token.id,
92+
token_name => token.name,
93+
expiry_date => token.expired_at.unwrap().to_rfc3339_opts(SecondsFormat::Secs, true)
94+
},
95+
)?;
9296
emails.send(&recipient, email).await?;
9397
} else {
9498
info!(
@@ -134,40 +138,6 @@ pub async fn find_expiring_tokens(
134138
.await
135139
}
136140

137-
#[derive(Debug, Clone)]
138-
struct ExpiryNotificationEmail<'a> {
139-
name: &'a str,
140-
token_id: i32,
141-
token_name: &'a str,
142-
expiry_date: chrono::DateTime<chrono::Utc>,
143-
}
144-
145-
impl Email for ExpiryNotificationEmail<'_> {
146-
fn subject(&self) -> String {
147-
format!(
148-
"crates.io: Your API token \"{}\" is about to expire",
149-
self.token_name
150-
)
151-
}
152-
153-
fn body(&self) -> String {
154-
format!(
155-
r#"Hi {},
156-
157-
We noticed your token "{}" will expire on {}.
158-
159-
If this token is still needed, visit https://crates.io/settings/tokens/new?from={} to generate a new one.
160-
161-
Thanks,
162-
The crates.io team"#,
163-
self.name,
164-
self.token_name,
165-
self.expiry_date.to_rfc3339_opts(SecondsFormat::Secs, true),
166-
self.token_id
167-
)
168-
}
169-
}
170-
171141
#[cfg(test)]
172142
mod tests {
173143
use super::*;

0 commit comments

Comments
 (0)