Skip to main content

tuwunel_admin/debug/
parse_pdu.rs

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