From 192c1e08da38b8be621fd39a9a67be85493136c8 Mon Sep 17 00:00:00 2001 From: strawberry Date: Sun, 7 Jul 2024 15:18:07 -0400 Subject: [PATCH] add `exclude_disabled` and `exclude_banned` room list admin cmd arguments Signed-off-by: strawberry --- src/admin/room/mod.rs | 12 ++++++++++- src/admin/room/room_commands.rs | 36 +++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/admin/room/mod.rs b/src/admin/room/mod.rs index 72e72793..b4fa15bd 100644 --- a/src/admin/room/mod.rs +++ b/src/admin/room/mod.rs @@ -16,6 +16,14 @@ pub(super) enum RoomCommand { /// - List all rooms the server knows about List { page: Option, + + /// Excludes rooms that we have federation disabled with + #[arg(long)] + exclude_disabled: bool, + + /// Excludes rooms that we have banned + #[arg(long)] + exclude_banned: bool, }, #[command(subcommand)] @@ -179,6 +187,8 @@ pub(super) async fn process(command: RoomCommand, body: Vec<&str>) -> Result list(body, page).await?, + exclude_disabled, + exclude_banned, + } => list(body, page, exclude_disabled, exclude_banned).await?, }) } diff --git a/src/admin/room/room_commands.rs b/src/admin/room/room_commands.rs index f64ccf30..d47edce2 100644 --- a/src/admin/room/room_commands.rs +++ b/src/admin/room/room_commands.rs @@ -1,18 +1,46 @@ use std::fmt::Write; -use ruma::{events::room::message::RoomMessageEventContent, OwnedRoomId}; +use ruma::events::room::message::RoomMessageEventContent; use crate::{escape_html, get_room_info, handler::PAGE_SIZE, services, Result}; -pub(super) async fn list(_body: Vec<&str>, page: Option) -> Result { +pub(super) async fn list( + _body: Vec<&str>, page: Option, exclude_disabled: bool, exclude_banned: bool, +) -> Result { // 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); let mut rooms = services() .rooms .metadata .iter_ids() - .filter_map(Result::ok) - .map(|id: OwnedRoomId| get_room_info(&id)) + .filter_map(|room_id| { + room_id + .ok() + .filter(|room_id| { + if exclude_disabled + && services() + .rooms + .metadata + .is_disabled(room_id) + .unwrap_or(false) + { + return false; + } + + if exclude_banned + && services() + .rooms + .metadata + .is_banned(room_id) + .unwrap_or(false) + { + return false; + } + + true + }) + .map(|room_id| get_room_info(&room_id)) + }) .collect::>(); rooms.sort_by_key(|r| r.1); rooms.reverse();