1
1
use crate :: models:: ApiToken ;
2
2
use crate :: { email:: Email , models:: User , worker:: Environment , Emails } ;
3
3
use anyhow:: anyhow;
4
+ use chrono:: SecondsFormat ;
4
5
use crates_io_worker:: BackgroundJob ;
5
6
use diesel:: {
6
7
dsl:: now, Connection , ExpressionMethods , NullableExpressionMethods , PgConnection , RunQueryDsl ,
@@ -48,8 +49,9 @@ fn check(emails: &Emails, conn: &mut PgConnection) -> anyhow::Result<()> {
48
49
return Err ( anyhow ! ( "No address found" ) ) ;
49
50
} ;
50
51
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 ( ) ,
53
55
} ;
54
56
emails. send ( & recipient, email) ?;
55
57
// Also update the token to prevent duplicate notifications.
@@ -65,18 +67,28 @@ fn check(emails: &Emails, conn: &mut PgConnection) -> anyhow::Result<()> {
65
67
}
66
68
67
69
#[ 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 > ,
71
74
}
72
75
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 " ;
75
78
76
79
fn body ( & self ) -> String {
77
80
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 )
80
92
)
81
93
}
82
94
}
0 commit comments