Files
continuwuity/src/admin/room/directory.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.5 KiB
Rust
Raw Normal View History

2024-05-23 01:27:04 -04:00
use std::fmt::Write;
2024-03-22 03:37:55 -07:00
2024-07-27 00:11:41 +00:00
use clap::Subcommand;
use conduit::Result;
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
use ruma::{events::room::message::RoomMessageEventContent, RoomId};
2024-03-22 03:37:55 -07:00
2024-07-27 00:11:41 +00:00
use crate::{escape_html, get_room_info, Command, PAGE_SIZE};
2024-03-22 03:37:55 -07:00
2024-07-27 00:11:41 +00:00
#[derive(Debug, Subcommand)]
pub(crate) enum RoomDirectoryCommand {
/// - Publish a room to the room directory
Publish {
/// The room id of the room to publish
room_id: Box<RoomId>,
},
/// - Unpublish a room to the room directory
Unpublish {
/// The room id of the room to unpublish
room_id: Box<RoomId>,
},
/// - List rooms that are published
List {
page: Option<usize>,
},
}
pub(super) async fn process(command: RoomDirectoryCommand, context: &Command<'_>) -> Result<RoomMessageEventContent> {
let services = context.services;
2024-03-22 03:37:55 -07:00
match command {
RoomDirectoryCommand::Publish {
room_id,
2024-08-08 17:18:30 +00:00
} => {
services.rooms.directory.set_public(&room_id);
Ok(RoomMessageEventContent::notice_plain("Room published"))
2024-03-22 03:37:55 -07:00
},
RoomDirectoryCommand::Unpublish {
room_id,
2024-08-08 17:18:30 +00:00
} => {
services.rooms.directory.set_not_public(&room_id);
Ok(RoomMessageEventContent::notice_plain("Room unpublished"))
2024-03-22 03:37:55 -07:00
},
RoomDirectoryCommand::List {
page,
} => {
// TODO: i know there's a way to do this with clap, but i can't seem to find it
let page = page.unwrap_or(1);
2024-07-27 00:11:41 +00:00
let mut rooms = services
2024-03-22 03:37:55 -07:00
.rooms
.directory
.public_rooms()
2024-08-08 17:18:30 +00:00
.then(|room_id| get_room_info(services, room_id))
.collect::<Vec<_>>()
.await;
2024-03-22 03:37:55 -07:00
rooms.sort_by_key(|r| r.1);
rooms.reverse();
2024-03-25 17:05:11 -04:00
let rooms = rooms
.into_iter()
2024-05-05 20:40:58 -04:00
.skip(page.saturating_sub(1).saturating_mul(PAGE_SIZE))
2024-03-25 17:05:11 -04:00
.take(PAGE_SIZE)
.collect::<Vec<_>>();
2024-03-22 03:37:55 -07:00
if rooms.is_empty() {
return Ok(RoomMessageEventContent::text_plain("No more rooms."));
};
let output_plain = format!(
"Rooms:\n{}",
rooms
.iter()
.map(|(id, members, name)| format!("{id}\tMembers: {members}\tName: {name}"))
.collect::<Vec<_>>()
.join("\n")
);
let output_html = format!(
"<table><caption>Room directory - page \
{page}</caption>\n<tr><th>id</th>\t<th>members</th>\t<th>name</th></tr>\n{}</table>",
2024-03-25 17:05:11 -04:00
rooms
.iter()
.fold(String::new(), |mut output, (id, members, name)| {
writeln!(
output,
"<tr><td>{}</td>\t<td>{}</td>\t<td>{}</td></tr>",
escape_html(id.as_ref()),
members,
escape_html(name.as_ref())
)
2024-05-23 01:27:04 -04:00
.expect("should be able to write to string buffer");
2024-03-25 17:05:11 -04:00
output
})
2024-03-22 03:37:55 -07:00
);
Ok(RoomMessageEventContent::text_html(output_plain, output_html))
},
}
}