Skip to content

Commit 77b7087

Browse files
committed
Replace CrateDeletionEmail struct with EmailMessage::from_template()
1 parent e19260e commit 77b7087

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

src/controllers/krate/delete.rs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::app::AppState;
22
use crate::auth::AuthCheck;
33
use crate::controllers::helpers::authorization::Rights;
44
use crate::controllers::krate::CratePath;
5-
use crate::email::Email;
5+
use crate::email::EmailMessage;
66
use crate::models::NewDeletedCrate;
77
use crate::schema::{crate_downloads, crates, dependencies};
88
use crate::util::errors::{AppResult, BoxedAppError, custom};
@@ -18,6 +18,8 @@ use diesel_async::scoped_futures::ScopedFutureExt;
1818
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
1919
use http::StatusCode;
2020
use http::request::Parts;
21+
use minijinja::context;
22+
use tracing::error;
2123

2224
const DOWNLOADS_PER_MONTH_LIMIT: u64 = 500;
2325
const AVAILABLE_AFTER: TimeDelta = TimeDelta::hours(24);
@@ -147,10 +149,13 @@ pub async fn delete_crate(
147149

148150
let email_future = async {
149151
if let Some(recipient) = user.email(&mut conn).await? {
150-
let email = CrateDeletionEmail {
151-
user: &user.gh_login,
152-
krate: &crate_name,
153-
};
152+
let email = EmailMessage::from_template(
153+
"crate_deletion",
154+
context! {
155+
user => user.gh_login,
156+
krate => crate_name
157+
},
158+
)?;
154159

155160
app.emails.send(&recipient, email).await?
156161
}
@@ -193,33 +198,6 @@ async fn has_rev_dep(conn: &mut AsyncPgConnection, crate_id: i32) -> QueryResult
193198
Ok(rev_dep.is_some())
194199
}
195200

196-
/// Email template for notifying a crate owner about a crate being deleted.
197-
///
198-
/// The owner usually should be aware of the deletion since they initiated it,
199-
/// but this email can be helpful in detecting malicious account activity.
200-
#[derive(Debug, Clone)]
201-
struct CrateDeletionEmail<'a> {
202-
user: &'a str,
203-
krate: &'a str,
204-
}
205-
206-
impl Email for CrateDeletionEmail<'_> {
207-
fn subject(&self) -> String {
208-
format!("crates.io: Deleted \"{}\" crate", self.krate)
209-
}
210-
211-
fn body(&self) -> String {
212-
format!(
213-
"Hi {},
214-
215-
Your \"{}\" crate has been deleted, per your request.
216-
217-
If you did not initiate this deletion, your account may have been compromised. Please contact us at help@crates.io.",
218-
self.user, self.krate
219-
)
220-
}
221-
}
222-
223201
#[cfg(test)]
224202
mod tests {
225203
use super::*;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Hi {{ user }},
2+
3+
Your "{{ krate }}" crate has been deleted, per your request.
4+
5+
If you did not initiate this deletion, your account may have been compromised. Please contact us at help@crates.io.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
crates.io: Deleted "{{ krate }}" crate

0 commit comments

Comments
 (0)