tuwunel_admin/user/
deactivate_all.rs1use ruma::OwnedUserId;
2use tuwunel_core::{Err, Result};
3
4use super::deactivate_user;
5use crate::{admin_command, utils::parse_active_local_user_id};
6
7#[admin_command]
8pub(super) async fn deactivate_all(&self, no_leave_rooms: bool, force: bool) -> Result {
9 if self.body.len() < 2
10 || !self.body[0].trim().starts_with("```")
11 || self.body.last().unwrap_or(&"").trim() != "```"
12 {
13 return Err!("Expected code block in command body. Add --help for details.",);
14 }
15
16 let usernames = self
17 .body
18 .to_vec()
19 .drain(1..self.body.len().saturating_sub(1))
20 .collect::<Vec<_>>();
21
22 let mut user_ids: Vec<OwnedUserId> = Vec::with_capacity(usernames.len());
23 let mut admins = Vec::new();
24
25 for username in usernames {
26 match parse_active_local_user_id(self.services, username).await {
27 | Err(e) => {
28 self.services
29 .admin
30 .send_text(&format!("{username} is not a valid username, skipping over: {e}"))
31 .await;
32 },
33 | Ok(user_id) => {
34 if self.services.admin.user_is_admin(&user_id).await && !force {
35 self.services
36 .admin
37 .send_text(&format!(
38 "{username} is an admin and --force is not set, skipping over"
39 ))
40 .await;
41
42 admins.push(username);
43 continue;
44 }
45
46 if user_id == self.services.globals.server_user {
48 self.services
49 .admin
50 .send_text(&format!(
51 "{username} is the server service account, skipping over"
52 ))
53 .await;
54
55 continue;
56 }
57
58 user_ids.push(user_id);
59 },
60 }
61 }
62
63 let mut deactivation_count: usize = 0;
64
65 for user_id in user_ids {
66 match deactivate_user(self.services, &user_id, no_leave_rooms).await {
67 | Ok(()) => {
68 deactivation_count = deactivation_count.saturating_add(1);
69 },
70 | Err(e) => {
71 self.services
72 .admin
73 .send_text(&format!("Failed deactivating user: {e}"))
74 .await;
75 },
76 }
77 }
78
79 if admins.is_empty() {
80 write!(self, "Deactivated {deactivation_count} accounts.")
81 } else {
82 write!(
83 self,
84 "Deactivated {deactivation_count} accounts.\nSkipped admin accounts: {}. Use \
85 --force to deactivate admin accounts",
86 admins.join(", ")
87 )
88 }
89 .await
90}