Skip to content

Commit 9fab8e5

Browse files
committed
Replace AdminAccountEmail struct with EmailMessage::from_template()
1 parent f9469e0 commit 9fab8e5

File tree

3 files changed

+31
-56
lines changed

3 files changed

+31
-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: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::email::Email;
1+
use crate::email::EmailMessage;
22
use crate::schema::{emails, users};
33
use crate::worker::Environment;
4+
use anyhow::Context;
45
use crates_io_worker::BackgroundJob;
56
use diesel::prelude::*;
67
use diesel_async::RunQueryDsl;
8+
use minijinja::context;
79
use std::collections::HashSet;
8-
use std::fmt::{Display, Formatter};
910
use std::sync::Arc;
1011

1112
/// See <https://github.com/rust-lang/team/pull/1197>.
@@ -138,72 +139,32 @@ impl BackgroundJob for SyncAdmins {
138139

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

144144
for database_admin in &database_admins {
145-
let (_, _, email_address) = database_admin;
145+
let (github_id, login, email_address) = database_admin;
146146
if let Some(email_address) = email_address {
147-
if let Err(error) = ctx.emails.send(email_address, email.clone()).await {
147+
if let Err(error) = send_email(&ctx, email_address, &context).await {
148148
warn!(
149-
"Failed to send email to admin {} ({}, github_id: {}): {}",
150-
database_admin.1, email_address, database_admin.0, error
149+
"Failed to send email to admin {login} ({email_address}, github_id: {github_id}): {error:?}",
151150
);
152151
}
153152
} else {
154-
warn!(
155-
"No email address found for admin {} (github_id: {})",
156-
database_admin.1, database_admin.0
157-
);
153+
warn!("No email address found for admin {login} (github_id: {github_id})",);
158154
}
159155
}
160156

161157
Ok(())
162158
}
163159
}
164160

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-
}
161+
async fn send_email(
162+
ctx: &Environment,
163+
address: &str,
164+
context: &minijinja::Value,
165+
) -> anyhow::Result<()> {
166+
let email = EmailMessage::from_template("admin_account", context);
167+
let email = email.context("Failed to render email template")?;
168+
let result = ctx.emails.send(address, email).await;
169+
result.context("Failed to send email")
209170
}

0 commit comments

Comments
 (0)