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

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

70 lines
1.8 KiB
Rust
Raw Normal View History

2024-12-14 21:58:01 -05:00
use conduwuit::Result;
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
use ruma::{OwnedRoomId, events::room::message::RoomMessageEventContent};
2024-04-20 19:55:14 -04:00
use crate::{PAGE_SIZE, admin_command, get_room_info};
2024-04-20 19:55:14 -04:00
2024-07-27 00:11:41 +00:00
#[admin_command]
pub(super) async fn list_rooms(
&self,
page: Option<usize>,
exclude_disabled: bool,
exclude_banned: bool,
no_details: bool,
) -> Result<RoomMessageEventContent> {
2024-04-20 19:55:14 -04: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-07-27 00:11:41 +00:00
let mut rooms = self
.services
2024-04-20 19:55:14 -04:00
.rooms
.metadata
.iter_ids()
2024-09-29 00:50:12 -04:00
.filter_map(|room_id| async move {
(!exclude_disabled || !self.services.rooms.metadata.is_disabled(room_id).await)
.then_some(room_id)
2024-09-29 00:50:12 -04:00
})
.filter_map(|room_id| async move {
(!exclude_banned || !self.services.rooms.metadata.is_banned(room_id).await)
.then_some(room_id)
2024-09-29 00:50:12 -04:00
})
2024-08-08 17:18:30 +00:00
.then(|room_id| get_room_info(self.services, room_id))
.collect::<Vec<_>>()
.await;
2024-04-20 19:55:14 -04:00
rooms.sort_by_key(|r| r.1);
rooms.reverse();
let rooms = rooms
.into_iter()
2024-05-05 20:40:58 -04:00
.skip(page.saturating_sub(1).saturating_mul(PAGE_SIZE))
2024-04-20 19:55:14 -04:00
.take(PAGE_SIZE)
.collect::<Vec<_>>();
if rooms.is_empty() {
return Ok(RoomMessageEventContent::text_plain("No more rooms."));
2025-02-25 18:38:12 +00:00
}
2024-04-20 19:55:14 -04:00
let output_plain = format!(
"Rooms ({}):\n```\n{}\n```",
rooms.len(),
2024-04-20 19:55:14 -04:00
rooms
.iter()
.map(|(id, members, name)| if no_details {
format!("{id}")
} else {
format!("{id}\tMembers: {members}\tName: {name}")
})
2024-04-20 19:55:14 -04:00
.collect::<Vec<_>>()
.join("\n")
);
Ok(RoomMessageEventContent::notice_markdown(output_plain))
2024-04-20 19:55:14 -04:00
}
2024-08-08 17:18:30 +00:00
#[admin_command]
pub(super) async fn exists(&self, room_id: OwnedRoomId) -> Result<RoomMessageEventContent> {
let result = self.services.rooms.metadata.exists(&room_id).await;
Ok(RoomMessageEventContent::notice_markdown(format!("{result}")))
}