Skip to content

Commit 32b9327

Browse files
committed
Replace AdminAccountEmail struct with EmailMessage::from_template()
1 parent d3b7054 commit 32b9327

File tree

3 files changed

+28
-56
lines changed

3 files changed

+28
-56
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% if added_admins -%}
2+
Granted admin access:
3+
4+
{% for admin in added_admins -%}
5+
- {{ admin }}
6+
{% endfor %}
7+
{% endif -%}
8+
{% if removed_admins -%}
9+
Revoked admin access:
10+
{% for admin in removed_admins -%}
11+
- {{ admin }}
12+
{% endfor -%}
13+
{% endif %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
crates.io: Admin account changes

src/worker/jobs/sync_admins.rs

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::email::Email;
1+
use crate::email::EmailMessage;
22
use crate::schema::{emails, users};
33
use crate::worker::Environment;
44
use crates_io_worker::BackgroundJob;
55
use diesel::prelude::*;
66
use diesel_async::RunQueryDsl;
7+
use minijinja::context;
78
use std::collections::HashSet;
8-
use std::fmt::{Display, Formatter};
99
use std::sync::Arc;
1010

1111
/// See <https://github.com/rust-lang/team/pull/1197>.
@@ -138,72 +138,30 @@ impl BackgroundJob for SyncAdmins {
138138

139139
let added_admins = format_repo_admins(&added_admin_ids);
140140
let removed_admins = format_database_admins(&removed_admin_ids);
141-
142-
let email = AdminAccountEmail::new(added_admins, removed_admins);
141+
let context = context! { added_admins, removed_admins };
143142

144143
for database_admin in &database_admins {
145-
let (_, _, email_address) = database_admin;
144+
let (github_id, login, email_address) = database_admin;
146145
if let Some(email_address) = email_address {
147-
if let Err(error) = ctx.emails.send(email_address, email.clone()).await {
146+
if let Err(error) = send_email(&ctx, email_address, &context).await {
148147
warn!(
149-
"Failed to send email to admin {} ({}, github_id: {}): {}",
150-
database_admin.1, email_address, database_admin.0, error
148+
"Failed to send email to admin {login} ({email_address}, github_id: {github_id}): {error:?}",
151149
);
152150
}
153151
} else {
154-
warn!(
155-
"No email address found for admin {} (github_id: {})",
156-
database_admin.1, database_admin.0
157-
);
152+
warn!("No email address found for admin {login} (github_id: {github_id})",);
158153
}
159154
}
160155

161156
Ok(())
162157
}
163158
}
164159

165-
#[derive(Debug, Clone)]
166-
struct AdminAccountEmail {
167-
added_admins: Vec<String>,
168-
removed_admins: Vec<String>,
169-
}
170-
171-
impl AdminAccountEmail {
172-
fn new(added_admins: Vec<String>, removed_admins: Vec<String>) -> Self {
173-
Self {
174-
added_admins,
175-
removed_admins,
176-
}
177-
}
178-
}
179-
180-
impl Email for AdminAccountEmail {
181-
fn subject(&self) -> String {
182-
"crates.io: Admin account changes".into()
183-
}
184-
185-
fn body(&self) -> String {
186-
self.to_string()
187-
}
188-
}
189-
190-
impl Display for AdminAccountEmail {
191-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
192-
if !self.added_admins.is_empty() {
193-
writeln!(f, "Granted admin access:\n")?;
194-
for new_admin in &self.added_admins {
195-
writeln!(f, "- {}", new_admin)?;
196-
}
197-
writeln!(f)?;
198-
}
199-
200-
if !self.removed_admins.is_empty() {
201-
writeln!(f, "Revoked admin access:")?;
202-
for obsolete_admin in &self.removed_admins {
203-
writeln!(f, "- {}", obsolete_admin)?;
204-
}
205-
}
206-
207-
Ok(())
208-
}
160+
async fn send_email(
161+
ctx: &Environment,
162+
address: &str,
163+
context: &minijinja::Value,
164+
) -> anyhow::Result<()> {
165+
let email = EmailMessage::from_template("admin_account", context)?;
166+
Ok(ctx.emails.send(address, email).await?)
209167
}

0 commit comments

Comments
 (0)