tuwunel_admin/user/
add_email.rs1use ruma::{MilliSecondsSinceUnixEpoch, thirdparty::Medium};
2use tuwunel_core::{Err, Result};
3use tuwunel_service::threepid::canonicalize_email;
4
5use crate::{admin_command, utils::parse_active_local_user_id};
6
7#[admin_command]
8pub(super) async fn add_email(&self, username: String, address: String) -> Result {
9 let user_id = parse_active_local_user_id(self.services, &username).await?;
10
11 if user_id == self.services.globals.server_user {
12 return Err!("Not allowed to bind an email address to the server account.");
13 }
14
15 let email_canon = canonicalize_email(&address)?;
16
17 if self
18 .services
19 .threepid
20 .bound_elsewhere(&user_id, &email_canon)
21 .await?
22 {
23 return Err!("Email {email_canon} is already bound to another user.");
24 }
25
26 let now = MilliSecondsSinceUnixEpoch::now();
27
28 self.services
29 .threepid
30 .put_binding(&user_id, &email_canon, Medium::Email, now, now)
31 .await;
32
33 write!(self, "Bound email {email_canon} to user {user_id}.").await
34}