tuwunel_admin/debug/
parse_pdu.rs1use ruma::{OwnedEventId, RoomVersionId};
2use tuwunel_core::{Err, Result, err, matrix::pdu::PduEvent, utils::string::EMPTY};
3
4use crate::admin_command;
5
6#[admin_command]
7pub(super) async fn parse_pdu(&self) -> Result {
8 if self.body.len() < 2
9 || !self.body[0].trim().starts_with("```")
10 || self.body.last().unwrap_or(&EMPTY).trim() != "```"
11 {
12 return Err!("Expected code block in command body. Add --help for details.");
13 }
14
15 let string = self.body[1..self.body.len().saturating_sub(1)].join("\n");
16 let rules = RoomVersionId::V6
17 .rules()
18 .expect("rules for V6 rooms");
19
20 let value =
21 serde_json::from_str(&string).map_err(|e| err!("Invalid json in command body: {e}"))?;
22
23 let hash = ruma::signatures::reference_hash(&value, &rules)
24 .map_err(|e| err!("Could not parse PDU JSON: {e:?}"))?;
25
26 let event_id = OwnedEventId::parse(format!("${hash}"));
27
28 let value = serde_json::to_value(value)?;
29
30 match serde_json::from_value::<PduEvent>(value) {
31 | Err(e) => return Err!("EventId: {event_id:?}\nCould not parse event: {e}"),
32 | Ok(pdu) => write!(self, "EventId: {event_id:?}\n{pdu:#?}"),
33 }
34 .await
35}