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 continue;
34 },
35 | Ok(user_id) => {
36 if self.services.admin.user_is_admin(&user_id).await && !force {
37 self.services
38 .admin
39 .send_text(&format!(
40 "{username} is an admin and --force is not set, skipping over"
41 ))
42 .await;
43
44 admins.push(username);
45 continue;
46 }
47
48 if user_id == self.services.globals.server_user {
50 self.services
51 .admin
52 .send_text(&format!(
53 "{username} is the server service account, skipping over"
54 ))
55 .await;
56
57 continue;
58 }
59
60 user_ids.push(user_id);
61 },
62 }
63 }
64
65 let mut deactivation_count: usize = 0;
66
67 for user_id in user_ids {
68 match deactivate_user(self.services, &user_id, no_leave_rooms).await {
69 | Ok(()) => {
70 deactivation_count = deactivation_count.saturating_add(1);
71 },
72 | Err(e) => {
73 self.services
74 .admin
75 .send_text(&format!("Failed deactivating user: {e}"))
76 .await;
77 },
78 }
79 }
80
81 if admins.is_empty() {
82 write!(self, "Deactivated {deactivation_count} accounts.")
83 } else {
84 write!(
85 self,
86 "Deactivated {deactivation_count} accounts.\nSkipped admin accounts: {}. Use \
87 --force to deactivate admin accounts",
88 admins.join(", ")
89 )
90 }
91 .await
92}