From afd72f23da806e5af3a6b531291700abf3b95a6c Mon Sep 17 00:00:00 2001 From: strawberry Date: Sun, 21 Apr 2024 16:30:02 -0400 Subject: [PATCH] add `get-remote-pdu-list` debug admin command Signed-off-by: strawberry --- src/service/admin/debug/debug_commands.rs | 26 +++++++++++++++++++++++ src/service/admin/debug/mod.rs | 20 +++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/service/admin/debug/debug_commands.rs b/src/service/admin/debug/debug_commands.rs index b882b517..d4a82c2a 100644 --- a/src/service/admin/debug/debug_commands.rs +++ b/src/service/admin/debug/debug_commands.rs @@ -100,6 +100,32 @@ pub(super) async fn get_pdu(_body: Vec<&str>, event_id: Box) -> Result< } } +pub(super) async fn get_remote_pdu_list( + body: Vec<&str>, server: Box, force: bool, +) -> Result { + if body.len() > 2 && body[0].trim().starts_with("```") && body.last().unwrap().trim() == "```" { + let list = body + .clone() + .drain(1..body.len() - 1) + .filter_map(|pdu| EventId::parse(pdu).ok()) + .collect::>(); + + for pdu in list { + if force { + _ = get_remote_pdu(Vec::new(), Box::from(pdu), server.clone()).await; + } else { + get_remote_pdu(Vec::new(), Box::from(pdu), server.clone()).await?; + } + } + + return Ok(RoomMessageEventContent::text_plain("Fetched list of remote PDUs.")); + } + + Ok(RoomMessageEventContent::text_plain( + "Expected code block in command body. Add --help for details.", + )) +} + pub(super) async fn get_remote_pdu( _body: Vec<&str>, event_id: Box, server: Box, ) -> Result { diff --git a/src/service/admin/debug/mod.rs b/src/service/admin/debug/mod.rs index f25d7511..8b617218 100644 --- a/src/service/admin/debug/mod.rs +++ b/src/service/admin/debug/mod.rs @@ -2,8 +2,8 @@ use clap::Subcommand; use ruma::{events::room::message::RoomMessageEventContent, EventId, RoomId, ServerName}; use self::debug_commands::{ - change_log_level, force_device_list_updates, get_auth_chain, get_pdu, get_remote_pdu, get_room_state, parse_pdu, - ping, sign_json, verify_json, + change_log_level, force_device_list_updates, get_auth_chain, get_pdu, get_remote_pdu, get_remote_pdu_list, + get_room_state, parse_pdu, ping, sign_json, verify_json, }; use crate::Result; @@ -45,6 +45,18 @@ pub(crate) enum DebugCommand { server: Box, }, + /// Same as `get-remote-pdu` but accepts a codeblock newline delimited list + /// of PDUs and a single server to fetch from + GetRemotePduList { + /// Argument for us to attempt to fetch all the events from the + /// specified remote server. + server: Box, + + /// If set, ignores errors, else stops at the first error/failure. + #[arg(short, long)] + force: bool, + }, + /// - Gets all the room state events for the specified room. /// /// This is functionally equivalent to `GET @@ -122,5 +134,9 @@ pub(crate) async fn process(command: DebugCommand, body: Vec<&str>) -> Result change_log_level(body, filter, reset).await?, DebugCommand::SignJson => sign_json(body).await?, DebugCommand::VerifyJson => verify_json(body).await?, + DebugCommand::GetRemotePduList { + server, + force, + } => get_remote_pdu_list(body, server, force).await?, }) }