From d4217007fe311896e8e685e0c95c50a50336d486 Mon Sep 17 00:00:00 2001
From: M0dEx <jakub.kubik.it@protonmail.com>
Date: Sat, 12 Feb 2022 21:40:07 +0100
Subject: [PATCH] fix: do not panic on a JSON not containing the PDU

Do not panic on a JSON not containing the PDU when executing the parse-pdu admin command.
---
 src/database/admin.rs | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/src/database/admin.rs b/src/database/admin.rs
index 50fac3e0..f9d4f425 100644
--- a/src/database/admin.rs
+++ b/src/database/admin.rs
@@ -354,24 +354,26 @@ fn process_admin_command(
                 let string = body[1..body.len() - 1].join("\n");
                 match serde_json::from_str(&string) {
                     Ok(value) => {
-                        let event_id = EventId::parse(format!(
-                            "${}",
-                            // Anything higher than version3 behaves the same
-                            ruma::signatures::reference_hash(&value, &RoomVersionId::V6)
-                                .expect("ruma can calculate reference hashes")
-                        ))
-                        .expect("ruma's reference hashes are valid event ids");
+                        match ruma::signatures::reference_hash(&value, &RoomVersionId::V6) {
+                            Ok(hash) => {
+                                let event_id = EventId::parse(format!("${}", hash));
 
-                        match serde_json::from_value::<PduEvent>(
-                            serde_json::to_value(value).expect("value is json"),
-                        ) {
-                            Ok(pdu) => RoomMessageEventContent::text_plain(format!(
-                                "EventId: {:?}\n{:#?}",
-                                event_id, pdu
-                            )),
+                                match serde_json::from_value::<PduEvent>(
+                                    serde_json::to_value(value).expect("value is json"),
+                                ) {
+                                    Ok(pdu) => RoomMessageEventContent::text_plain(format!(
+                                        "EventId: {:?}\n{:#?}",
+                                        event_id, pdu
+                                    )),
+                                    Err(e) => RoomMessageEventContent::text_plain(format!(
+                                        "EventId: {:?}\nCould not parse event: {}",
+                                        event_id, e
+                                    )),
+                                }
+                            }
                             Err(e) => RoomMessageEventContent::text_plain(format!(
-                                "EventId: {:?}\nCould not parse event: {}",
-                                event_id, e
+                                "Could not parse PDU JSON: {:?}",
+                                e
                             )),
                         }
                     }