From 74b29ce06738a1e728649c5e54e5cc7fc7506b33 Mon Sep 17 00:00:00 2001 From: strawberry <strawberry@puppygock.gay> Date: Mon, 10 Jun 2024 03:27:15 -0400 Subject: [PATCH] adminroom: improved room list outputs, add counts Signed-off-by: strawberry <strawberry@puppygock.gay> --- src/admin/federation/federation_commands.rs | 10 ++-- src/admin/room/room_moderation_commands.rs | 66 ++++++++++++++------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/src/admin/federation/federation_commands.rs b/src/admin/federation/federation_commands.rs index 7f6b7004f..b52a62f29 100644 --- a/src/admin/federation/federation_commands.rs +++ b/src/admin/federation/federation_commands.rs @@ -101,7 +101,8 @@ pub(crate) async fn remote_user_in_rooms(_body: Vec<&str>, user_id: Box<UserId>) rooms.reverse(); let output_plain = format!( - "Rooms {user_id} shares with us:\n{}", + "Rooms {user_id} shares with us ({}):\n{}", + rooms.len(), rooms .iter() .map(|(id, members, name)| format!("{id}\tMembers: {members}\tName: {name}")) @@ -109,15 +110,16 @@ pub(crate) async fn remote_user_in_rooms(_body: Vec<&str>, user_id: Box<UserId>) .join("\n") ); let output_html = format!( - "<table><caption>Rooms {user_id} shares with \ - us</caption>\n<tr><th>id</th>\t<th>members</th>\t<th>name</th></tr>\n{}</table>", + "<table><caption>Rooms {user_id} shares with us \ + ({})</caption>\n<tr><th>id</th>\t<th>members</th>\t<th>name</th></tr>\n{}</table>", + rooms.len(), 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()), + id, members, escape_html(name) ) diff --git a/src/admin/room/room_moderation_commands.rs b/src/admin/room/room_moderation_commands.rs index f6d3206ab..9886756b6 100644 --- a/src/admin/room/room_moderation_commands.rs +++ b/src/admin/room/room_moderation_commands.rs @@ -6,11 +6,8 @@ }; use tracing::{debug, error, info, warn}; -use super::{ - super::{escape_html, Service}, - RoomModerationCommand, -}; -use crate::{services, user_is_local, Result}; +use super::{super::Service, RoomModerationCommand}; +use crate::{escape_html, get_room_info, services, user_is_local, Result}; pub(crate) async fn process(command: RoomModerationCommand, body: Vec<&str>) -> Result<RoomMessageEventContent> { match command { @@ -479,26 +476,51 @@ async fn list_banned_rooms(_body: Vec<&str>) -> Result<RoomMessageEventContent> match rooms { Ok(room_ids) => { - // TODO: add room name from our state cache if available, default to the room ID - // as the room name if we dont have it TODO: do same if we have a room alias for - // this - let plain_list = room_ids.iter().fold(String::new(), |mut output, room_id| { - writeln!(output, "- `{room_id}`").unwrap(); - output - }); - - let html_list = room_ids.iter().fold(String::new(), |mut output, room_id| { - writeln!(output, "<li><code>{}</code></li>", escape_html(room_id.as_ref())).unwrap(); - output - }); - - let plain = format!("Rooms:\n{plain_list}"); - let html = format!("Rooms:\n<ul>{html_list}</ul>"); - Ok(RoomMessageEventContent::text_html(plain, html)) + if room_ids.is_empty() { + return Ok(RoomMessageEventContent::text_plain("No rooms are banned.")); + } + + let mut rooms = room_ids + .into_iter() + .map(|room_id| get_room_info(&room_id)) + .collect::<Vec<_>>(); + rooms.sort_by_key(|r| r.1); + rooms.reverse(); + + let output_plain = format!( + "Rooms Banned ({}):\n```\n{}```", + rooms.len(), + rooms + .iter() + .map(|(id, members, name)| format!("{id}\tMembers: {members}\tName: {name}")) + .collect::<Vec<_>>() + .join("\n") + ); + + let output_html = format!( + "<table><caption>Rooms Banned ({}) \ + </caption>\n<tr><th>id</th>\t<th>members</th>\t<th>name</th></tr>\n{}</table>", + rooms.len(), + rooms + .iter() + .fold(String::new(), |mut output, (id, members, name)| { + writeln!( + output, + "<tr><td>{}</td>\t<td>{}</td>\t<td>{}</td></tr>", + id, + members, + escape_html(name.as_ref()) + ) + .expect("should be able to write to string buffer"); + output + }) + ); + + Ok(RoomMessageEventContent::text_html(output_plain, output_html)) }, Err(e) => { error!("Failed to list banned rooms: {}", e); - Ok(RoomMessageEventContent::text_plain(format!("Unable to list room aliases: {e}"))) + Ok(RoomMessageEventContent::text_plain(format!("Unable to list banned rooms: {e}"))) }, } } -- GitLab