Skip to content

Commit 74751b5

Browse files
0xPoeTurbo87
authored andcommitted
refactor: better email template
Signed-off-by: hi-rustin <[email protected]>
1 parent bbd869f commit 74751b5

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/worker/jobs/expiry_notification.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::models::ApiToken;
22
use crate::{email::Email, models::User, worker::Environment, Emails};
33
use anyhow::anyhow;
4+
use chrono::SecondsFormat;
45
use crates_io_worker::BackgroundJob;
56
use diesel::{
67
dsl::now, Connection, ExpressionMethods, NullableExpressionMethods, PgConnection, RunQueryDsl,
@@ -48,8 +49,9 @@ fn check(emails: &Emails, conn: &mut PgConnection) -> anyhow::Result<()> {
4849
return Err(anyhow!("No address found"));
4950
};
5051
let email = ExpiryNotificationEmail {
51-
token_name: token.name.clone(),
52-
expiry_date: token.expired_at.unwrap().date().to_string(),
52+
name: &user.gh_login,
53+
token_name: &token.name,
54+
expiry_date: token.expired_at.unwrap().and_utc(),
5355
};
5456
emails.send(&recipient, email)?;
5557
// Also update the token to prevent duplicate notifications.
@@ -65,18 +67,28 @@ fn check(emails: &Emails, conn: &mut PgConnection) -> anyhow::Result<()> {
6567
}
6668

6769
#[derive(Debug, Clone)]
68-
struct ExpiryNotificationEmail {
69-
token_name: String,
70-
expiry_date: String,
70+
struct ExpiryNotificationEmail<'a> {
71+
name: &'a str,
72+
token_name: &'a str,
73+
expiry_date: chrono::DateTime<chrono::Utc>,
7174
}
7275

73-
impl Email for ExpiryNotificationEmail {
74-
const SUBJECT: &'static str = "Token Expiry Notification";
76+
impl<'a> Email for ExpiryNotificationEmail<'a> {
77+
const SUBJECT: &'static str = "Your token is about to expire";
7578

7679
fn body(&self) -> String {
7780
format!(
78-
"The token {} is about to expire on {}. Please take action.",
79-
self.token_name, self.expiry_date
81+
r#"Hi {},
82+
83+
We noticed your token "{}" will expire on {}.
84+
85+
If this token is still needed, visit https://crates.io/settings/tokens/new to generate a new one.
86+
87+
Thanks,
88+
The crates.io team"#,
89+
self.name,
90+
self.token_name,
91+
self.expiry_date.to_rfc3339_opts(SecondsFormat::Secs, true)
8092
)
8193
}
8294
}

0 commit comments

Comments
 (0)