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.

75 lines
1.8 KiB
Rust
Raw Normal View History

2024-07-27 00:11:41 +00:00
use clap::Subcommand;
use conduwuit::{Err, Result};
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
use ruma::OwnedRoomId;
2024-03-22 03:37:55 -07:00
use crate::{Context, PAGE_SIZE, get_room_info};
2024-03-22 03:37:55 -07:00
2024-07-27 00:11:41 +00:00
#[derive(Debug, Subcommand)]
2025-05-24 00:28:09 +01:00
pub enum RoomDirectoryCommand {
2024-07-27 00:11:41 +00:00
/// - Publish a room to the room directory
Publish {
/// The room id of the room to publish
room_id: OwnedRoomId,
2024-07-27 00:11:41 +00:00
},
/// - Unpublish a room to the room directory
Unpublish {
/// The room id of the room to unpublish
room_id: OwnedRoomId,
2024-07-27 00:11:41 +00:00
},
/// - List rooms that are published
List {
page: Option<usize>,
},
}
pub(super) async fn process(command: RoomDirectoryCommand, context: &Context<'_>) -> Result {
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);
context.write_str("Room published").await
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);
context.write_str("Room unpublished").await
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 Err!("No more rooms.");
2025-02-25 18:38:12 +00:00
}
2024-03-22 03:37:55 -07:00
let body = rooms
.iter()
.map(|(id, members, name)| format!("{id} | Members: {members} | Name: {name}"))
.collect::<Vec<_>>()
.join("\n");
context
.write_str(&format!("Rooms (page {page}):\n```\n{body}\n```",))
.await
2024-03-22 03:37:55 -07:00
},
}
}