|
1 |
| -use crate::email::Email; |
| 1 | +use crate::email::EmailMessage; |
2 | 2 | use crate::schema::{emails, users};
|
3 | 3 | use crate::worker::Environment;
|
4 | 4 | use crates_io_worker::BackgroundJob;
|
5 | 5 | use diesel::prelude::*;
|
6 | 6 | use diesel_async::RunQueryDsl;
|
| 7 | +use minijinja::context; |
7 | 8 | use std::collections::HashSet;
|
8 |
| -use std::fmt::{Display, Formatter}; |
9 | 9 | use std::sync::Arc;
|
10 | 10 |
|
11 | 11 | /// See <https://github.com/rust-lang/team/pull/1197>.
|
@@ -138,72 +138,30 @@ impl BackgroundJob for SyncAdmins {
|
138 | 138 |
|
139 | 139 | let added_admins = format_repo_admins(&added_admin_ids);
|
140 | 140 | 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 }; |
143 | 142 |
|
144 | 143 | for database_admin in &database_admins {
|
145 |
| - let (_, _, email_address) = database_admin; |
| 144 | + let (github_id, login, email_address) = database_admin; |
146 | 145 | 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 { |
148 | 147 | 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:?}", |
151 | 149 | );
|
152 | 150 | }
|
153 | 151 | } 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})",); |
158 | 153 | }
|
159 | 154 | }
|
160 | 155 |
|
161 | 156 | Ok(())
|
162 | 157 | }
|
163 | 158 | }
|
164 | 159 |
|
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?) |
209 | 167 | }
|
0 commit comments