Skip to main content

tuwunel_admin/user/
force_join_all_local_users.rs

1use futures::StreamExt;
2use ruma::{OwnedRoomOrAliasId, UserId};
3use tuwunel_core::{Err, Result, debug_warn};
4use tuwunel_service::membership::Join;
5
6use super::BULK_JOIN_REASON;
7use crate::admin_command;
8
9#[admin_command]
10pub(super) async fn force_join_all_local_users(
11	&self,
12	room: OwnedRoomOrAliasId,
13	yes_i_want_to_do_this: bool,
14) -> Result {
15	if !yes_i_want_to_do_this {
16		return Err!(
17			"You must pass the --yes-i-want-to-do-this-flag to ensure you really want to force \
18			 bulk join all local users.",
19		);
20	}
21
22	let (room_id, servers) = self
23		.services
24		.alias
25		.maybe_resolve_with_servers(&room, None)
26		.await?;
27
28	if !self
29		.services
30		.state_cache
31		.server_in_room(self.services.globals.server_name(), &room_id)
32		.await
33	{
34		return Err!("We are not joined in this room.");
35	}
36
37	let mut failed_joins: usize = 0;
38	let mut successful_joins: usize = 0;
39
40	for user_id in &self
41		.services
42		.users
43		.list_local_users()
44		.map(UserId::to_owned)
45		.collect::<Vec<_>>()
46		.await
47	{
48		if user_id == &self.services.globals.server_user {
49			continue;
50		}
51
52		match self
53			.services
54			.membership
55			.join(Join {
56				sender_user: user_id,
57				room_id: &room_id,
58				orig_room_id: Some(&room),
59				reason: Some(String::from(BULK_JOIN_REASON)),
60				servers: &servers,
61				is_appservice: false,
62				extra_content: None,
63			})
64			.await
65		{
66			| Ok(_res) => {
67				successful_joins = successful_joins.saturating_add(1);
68			},
69			| Err(e) => {
70				debug_warn!("Failed force joining {user_id} to {room_id} during bulk join: {e}");
71				failed_joins = failed_joins.saturating_add(1);
72			},
73		}
74	}
75
76	write!(
77		self,
78		"{successful_joins} local users have been joined to {room_id}. {failed_joins} joins \
79		 failed.",
80	)
81	.await
82}