Skip to main content

tuwunel_admin/room/moderation/
ban_list_of_rooms.rs

1use ruma::{OwnedRoomId, RoomOrAliasId};
2use tuwunel_core::{Err, Result, is_equal_to, warn};
3
4use super::do_ban_room;
5use crate::admin_command;
6
7#[admin_command]
8pub(super) async fn ban_list_of_rooms(&self) -> 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 rooms_s = self
17		.body
18		.to_vec()
19		.drain(1..self.body.len().saturating_sub(1))
20		.collect::<Vec<_>>();
21
22	let admin_room_id = self.services.admin.get_admin_room().await.ok();
23
24	let mut room_ids: Vec<OwnedRoomId> = Vec::with_capacity(rooms_s.len());
25
26	for room in rooms_s {
27		let room_alias_or_id = match <&RoomOrAliasId>::try_from(room) {
28			| Ok(room_alias_or_id) => room_alias_or_id,
29			| Err(e) => {
30				warn!("Error parsing room {room} during bulk room banning, ignoring: {e}");
31				continue;
32			},
33		};
34
35		let room_id = match self
36			.services
37			.alias
38			.maybe_resolve(room_alias_or_id)
39			.await
40		{
41			| Ok(room_id) => room_id,
42			| Err(e) => {
43				warn!("Failed to resolve room alias {room_alias_or_id} to a room ID: {e}");
44				continue;
45			},
46		};
47
48		if admin_room_id
49			.as_ref()
50			.is_some_and(is_equal_to!(&room_id))
51		{
52			warn!("User specified admin room in bulk ban list, ignoring");
53			continue;
54		}
55
56		room_ids.push(room_id);
57	}
58
59	let rooms_len = room_ids.len();
60
61	for room_id in room_ids {
62		do_ban_room(self.services, &room_id).await;
63	}
64
65	write!(
66		self,
67		"Finished bulk room ban, banned {rooms_len} total rooms, evicted all users, and \
68		 disabled incoming federation with the room."
69	)
70	.await
71}