Skip to main content

tuwunel_admin/room/
list.rs

1use futures::StreamExt;
2use tuwunel_core::{Err, Result, itertools::Itertools};
3
4use crate::{PAGE_SIZE, admin_command, get_room_info};
5
6#[admin_command]
7pub(super) async fn room_list(
8	&self,
9	page: Option<usize>,
10	exclude_disabled: bool,
11	exclude_banned: bool,
12	no_details: bool,
13) -> Result {
14	// TODO: i know there's a way to do this with clap, but i can't seem to find it
15	let page = page.unwrap_or(1);
16	let rooms = self
17		.services
18		.metadata
19		.iter_ids()
20		.filter_map(async |room_id| {
21			(!exclude_disabled || !self.services.metadata.is_disabled(room_id).await)
22				.then_some(room_id)
23		})
24		.filter_map(async |room_id| {
25			(!exclude_banned || !self.services.metadata.is_banned(room_id).await)
26				.then_some(room_id)
27		})
28		.then(|room_id| get_room_info(self.services, room_id))
29		.collect::<Vec<_>>()
30		.await
31		.into_iter()
32		.sorted_by_key(|r| r.1)
33		.rev()
34		.skip(page.saturating_sub(1).saturating_mul(PAGE_SIZE))
35		.take(PAGE_SIZE)
36		.collect::<Vec<_>>();
37
38	if rooms.is_empty() {
39		return Err!("No more rooms.");
40	}
41
42	write!(self, "Rooms ({}):\n```\n", rooms.len()).await?;
43	for (id, members, name) in &rooms {
44		if no_details {
45			writeln!(self, "{id}").await?;
46		} else {
47			writeln!(self, "{id}\tMembers: {members}\tName: {name}").await?;
48		}
49	}
50	write!(self, "```").await
51}