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.

86 lines
2.2 KiB
Rust
Raw Normal View History

2024-07-27 00:11:41 +00:00
use clap::Subcommand;
2024-12-14 21:58:01 -05:00
use conduwuit::Result;
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
use ruma::{RoomId, events::room::message::RoomMessageEventContent};
2024-03-22 03:37:55 -07:00
use crate::{Command, PAGE_SIZE, get_room_info};
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>,
},
}
2025-01-04 16:57:07 +00:00
pub(super) async fn process(command: RoomDirectoryCommand, context: &Command<'_>) -> Result {
let c = reprocess(command, context).await?;
context.write_str(c.body()).await?;
Ok(())
}
pub(super) async fn reprocess(
command: RoomDirectoryCommand,
context: &Command<'_>,
) -> Result<RoomMessageEventContent> {
2024-07-27 00:11:41 +00:00
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 } => {
2024-03-22 03:37:55 -07:00
// 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-10-01 02:47:39 +00:00
let mut rooms: Vec<_> = 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))
2024-10-01 02:47:39 +00:00
.collect()
2024-08-08 17:18:30 +00:00
.await;
2024-03-22 03:37:55 -07:00
rooms.sort_by_key(|r| r.1);
rooms.reverse();
2024-10-01 02:47:39 +00:00
let rooms: Vec<_> = rooms
2024-03-25 17:05:11 -04:00
.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)
2024-10-01 02:47:39 +00:00
.collect();
2024-03-22 03:37:55 -07:00
if rooms.is_empty() {
return Ok(RoomMessageEventContent::text_plain("No more rooms."));
};
let output = format!(
"Rooms (page {page}):\n```\n{}\n```",
2024-03-22 03:37:55 -07:00
rooms
.iter()
.map(|(id, members, name)| format!(
"{id} | Members: {members} | Name: {name}"
))
2024-03-22 03:37:55 -07:00
.collect::<Vec<_>>()
.join("\n")
);
Ok(RoomMessageEventContent::text_markdown(output))
2024-03-22 03:37:55 -07:00
},
}
}