Skip to main content

tuwunel_admin/room/
directory.rs

1use clap::Subcommand;
2use futures::StreamExt;
3use ruma::OwnedRoomId;
4use tuwunel_core::Result;
5
6use crate::{Context, PAGE_SIZE, get_room_info};
7
8#[derive(Debug, Subcommand)]
9pub(crate) enum RoomDirectoryCommand {
10	/// - Publish a room to the room directory
11	Publish {
12		/// The room id of the room to publish
13		room_id: OwnedRoomId,
14	},
15
16	/// - Unpublish a room to the room directory
17	Unpublish {
18		/// The room id of the room to unpublish
19		room_id: OwnedRoomId,
20	},
21
22	/// - List rooms that are published
23	List {
24		page: Option<usize>,
25	},
26}
27
28pub(super) async fn process(command: RoomDirectoryCommand, context: &Context<'_>) -> Result {
29	let services = context.services;
30	match command {
31		| RoomDirectoryCommand::Publish { room_id } => {
32			services.directory.set_public(&room_id);
33			context.write_str("Room published").await
34		},
35		| RoomDirectoryCommand::Unpublish { room_id } => {
36			services.directory.set_not_public(&room_id);
37			context.write_str("Room unpublished").await
38		},
39		| RoomDirectoryCommand::List { page } => {
40			// TODO: i know there's a way to do this with clap, but i can't seem to find it
41			let page = page.unwrap_or(1);
42			let mut rooms: Vec<_> = services
43				.directory
44				.public_rooms()
45				.then(|room_id| get_room_info(services, room_id))
46				.collect()
47				.await;
48
49			rooms.sort_by_key(|r| r.1);
50			rooms.reverse();
51
52			let rooms: Vec<_> = rooms
53				.into_iter()
54				.skip(page.saturating_sub(1).saturating_mul(PAGE_SIZE))
55				.take(PAGE_SIZE)
56				.collect();
57
58			if rooms.is_empty() {
59				context
60					.write_str("No rooms are published.")
61					.await?;
62
63				return Ok(());
64			}
65
66			let body = rooms
67				.iter()
68				.map(|(id, members, name)| format!("{id} | Members: {members} | Name: {name}"))
69				.collect::<Vec<_>>()
70				.join("\n");
71
72			context
73				.write_str(&format!("Rooms (page {page}):\n```\n{body}\n```"))
74				.await
75		},
76	}
77}