feat: Move some PDU commands to !admin rooms graph

This commit is contained in:
timedout committed 2026-10-31 22:56:13 +00:00
1 parent 5de3d74436
commit b651fbd088
3 files changed
+103 -169

No files matched your search

+101 -2
View File
@@ -1,8 +1,8 @@
use clap::Subcommand;
use conduwuit::{Err, Event, Result, utils::stream::BroadbandExt};
use conduwuit::{Err, Event, Result, err, utils::stream::BroadbandExt};
use conduwuit_macros::admin_command_dispatch;
use futures::StreamExt;
use ruma::OwnedRoomOrAliasId;
use ruma::{OwnedEventId, OwnedRoomId, OwnedRoomOrAliasId};
#[admin_command_dispatch]
#[derive(Debug, Subcommand)]
@@ -11,6 +11,26 @@ pub enum GraphCommand {
ListExtremities {
room: OwnedRoomOrAliasId,
},
/// Fetches a PDU (event) by its event ID, as stored in the database. Also
/// includes details about the PDU state.
GetPDU {
event_id: OwnedEventId,
},
/// Yields the first PDU we became aware of in a room.
/// This is always the room's create event for local rooms, or the first
/// membership event that introduced us to the remote room.
#[clap(name = "first-pdu")]
FirstPduInRoom {
room_id: OwnedRoomId,
},
/// Yields the most recent PDU in a room.
#[clap(name = "latest-pdu")]
LatestPduInRoom {
room_id: OwnedRoomId,
},
}
impl crate::Context<'_> {
@@ -54,4 +74,83 @@ impl crate::Context<'_> {
Ok(())
}
pub(super) async fn get_pdu(&self, event_id: OwnedEventId) -> Result {
let mut outlier = false;
let mut pdu_json = self
.services
.rooms
.timeline
.get_non_outlier_pdu_json(&event_id)
.await;
if pdu_json.is_err() {
outlier = true;
pdu_json = self.services.rooms.timeline.get_pdu_json(&event_id).await;
}
let rejected = self
.services
.rooms
.pdu_metadata
.is_event_rejected(&event_id)
.await;
let soft_failed = self
.services
.rooms
.pdu_metadata
.is_event_soft_failed(&event_id)
.await;
match pdu_json {
| Err(_) => return Err!("PDU not found locally."),
| Ok(json) => {
let text = serde_json::to_string_pretty(&json)?;
let msg = if rejected {
"Rejected PDU:"
} else if soft_failed {
"Soft-failed PDU:"
} else if outlier {
"Outlier PDU:"
} else {
"Timeline PDU:"
};
write!(self, "{msg}\n```json\n{text}\n```")
},
}
.await
}
pub(super) async fn first_pdu_in_room(&self, room_id: OwnedRoomId) -> Result {
self.bail_restricted()?;
let first_pdu = self
.services
.rooms
.timeline
.first_pdu_in_room(&room_id)
.await
.map(Event::into_canonical_object)
.and_then(|v| serde_json::to_string_pretty(&v).map_err(Into::into))
.map_err(|e| err!(Database("Failed to find the first PDU in database: {e:?}")))?;
let out = format!("```json\n{first_pdu}\n```");
self.write_str(&out).await
}
pub(super) async fn latest_pdu_in_room(&self, room_id: OwnedRoomId) -> Result {
self.bail_restricted()?;
let latest_pdu = self
.services
.rooms
.timeline
.latest_pdu_in_room(&room_id)
.await
.map(Event::into_canonical_object)
.and_then(|v| serde_json::to_string_pretty(&v).map_err(Into::into))
.map_err(|e| err!(Database("Failed to find the latest PDU in database: {e:?}")))?;
let out = format!("```json\n{latest_pdu}\n```");
self.write_str(&out).await
}
}