From 45e3fdba69696b6ac1d772bcc50cac96a7d0e6ab Mon Sep 17 00:00:00 2001 From: strawberry Date: Thu, 30 May 2024 23:42:29 -0400 Subject: [PATCH] admin room: add `get-latest-pdu` and `get-first-pdu` commands Signed-off-by: strawberry --- src/admin/debug/debug_commands.rs | 42 +++++++++++++++++++++++++++++++ src/admin/debug/mod.rs | 24 +++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/admin/debug/debug_commands.rs b/src/admin/debug/debug_commands.rs index 6a5cee35..4522b678 100644 --- a/src/admin/debug/debug_commands.rs +++ b/src/admin/debug/debug_commands.rs @@ -430,6 +430,48 @@ pub(crate) async fn verify_json(body: Vec<&str>) -> Result, room_id: Box) -> Result { + if !services() + .rooms + .state_cache + .server_in_room(&services().globals.config.server_name, &room_id)? + { + return Ok(RoomMessageEventContent::text_plain( + "We are not participating in the room / we don't know about the room ID.", + )); + } + + let first_pdu = services() + .rooms + .timeline + .first_pdu_in_room(&room_id)? + .ok_or_else(|| Error::bad_database("Failed to find the first PDU in database"))?; + + Ok(RoomMessageEventContent::text_plain(format!("{first_pdu:?}"))) +} + +#[tracing::instrument(skip(_body))] +pub(crate) async fn latest_pdu_in_room(_body: Vec<&str>, room_id: Box) -> Result { + if !services() + .rooms + .state_cache + .server_in_room(&services().globals.config.server_name, &room_id)? + { + return Ok(RoomMessageEventContent::text_plain( + "We are not participating in the room / we don't know about the room ID.", + )); + } + + let latest_pdu = services() + .rooms + .timeline + .latest_pdu_in_room(&room_id)? + .ok_or_else(|| Error::bad_database("Failed to find the latest PDU in database"))?; + + Ok(RoomMessageEventContent::text_plain(format!("{latest_pdu:?}"))) +} + pub(crate) async fn resolve_true_destination( _body: Vec<&str>, server_name: Box, no_cache: bool, ) -> Result { diff --git a/src/admin/debug/mod.rs b/src/admin/debug/mod.rs index 260e16d5..614a4ae5 100644 --- a/src/admin/debug/mod.rs +++ b/src/admin/debug/mod.rs @@ -1,4 +1,5 @@ use clap::Subcommand; +use debug_commands::{first_pdu_in_room, latest_pdu_in_room}; use ruma::{events::room::message::RoomMessageEventContent, EventId, RoomId, ServerName}; use self::debug_commands::{ @@ -45,7 +46,8 @@ pub(crate) enum DebugCommand { server: Box, }, - /// Same as `get-remote-pdu` but accepts a codeblock newline delimited list + /// - 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 @@ -107,6 +109,20 @@ pub(crate) enum DebugCommand { /// the command. VerifyJson, + /// - Prints the very first PDU in the specified room (typically + /// m.room.create) + FirstPduInRoom { + /// The room ID + room_id: Box, + }, + + /// - Prints the latest ("last") PDU in the specified room (typically a + /// message) + LatestPduInRoom { + /// The room ID + room_id: Box, + }, + /// - Runs a server name through conduwuit's true destination resolution /// process /// @@ -148,6 +164,12 @@ 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::FirstPduInRoom { + room_id, + } => first_pdu_in_room(body, room_id).await?, + DebugCommand::LatestPduInRoom { + room_id, + } => latest_pdu_in_room(body, room_id).await?, DebugCommand::GetRemotePduList { server, force,