tuwunel_admin/user/
add_email.rs1use ruma::{MilliSecondsSinceUnixEpoch, thirdparty::Medium};
2use tuwunel_core::{Err, Result, err, utils::BoolExt};
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 let now = MilliSecondsSinceUnixEpoch::now();
18
19 self.services
20 .threepid
21 .bind_unclaimed(&user_id, &email_canon, Medium::Email, now)
22 .await?
23 .into_option()
24 .ok_or_else(|| err!("Email {email_canon} is already bound to another user."))?;
25
26 write!(self, "Bound email {email_canon} to user {user_id}.").await
27}