From 38663228f524d15fae9c4b3d1e7c64a7bf61d308 Mon Sep 17 00:00:00 2001
From: Timo <timo@koesters.xyz>
Date: Sun, 23 Aug 2020 16:47:27 +0200
Subject: [PATCH] fix: put reason of redaction in the redacted event

---
 src/database/rooms.rs | 22 ++++------------------
 src/pdu.rs            |  4 ++--
 2 files changed, 6 insertions(+), 20 deletions(-)

diff --git a/src/database/rooms.rs b/src/database/rooms.rs
index d2cd5e9fb..3c1febd48 100644
--- a/src/database/rooms.rs
+++ b/src/database/rooms.rs
@@ -11,7 +11,6 @@
         room::{
             join_rules, member,
             power_levels::{self, PowerLevelsEventContent},
-            redaction,
         },
         EventType,
     },
@@ -566,7 +565,7 @@ pub fn append_pdu(
         self.eventid_pduid
             .insert(pdu.event_id.to_string(), pdu_id.clone())?;
 
-        if let Some(state_key) = pdu.state_key {
+        if let Some(state_key) = &pdu.state_key {
             let mut key = room_id.to_string().as_bytes().to_vec();
             key.push(0xff);
             key.extend_from_slice(pdu.kind.to_string().as_bytes());
@@ -578,20 +577,7 @@ pub fn append_pdu(
         match event_type {
             EventType::RoomRedaction => {
                 if let Some(redact_id) = &redacts {
-                    // TODO: Reason
-                    let _reason =
-                        serde_json::from_value::<Raw<redaction::RedactionEventContent>>(content)
-                            .expect("Raw::from_value always works.")
-                            .deserialize()
-                            .map_err(|_| {
-                                Error::BadRequest(
-                                    ErrorKind::InvalidParam,
-                                    "Invalid redaction event content.",
-                                )
-                            })?
-                            .reason;
-
-                    self.redact_pdu(&redact_id)?;
+                    self.redact_pdu(&redact_id, &pdu)?;
                 }
             }
             EventType::RoomMember => {
@@ -758,12 +744,12 @@ pub fn pdus_after(
     }
 
     /// Replace a PDU with the redacted form.
-    pub fn redact_pdu(&self, event_id: &EventId) -> Result<()> {
+    pub fn redact_pdu(&self, event_id: &EventId, reason: &PduEvent) -> Result<()> {
         if let Some(pdu_id) = self.get_pdu_id(event_id)? {
             let mut pdu = self
                 .get_pdu_from_id(&pdu_id)?
                 .ok_or_else(|| Error::bad_database("PDU ID points to invalid PDU."))?;
-            pdu.redact()?;
+            pdu.redact(&reason)?;
             self.replace_pdu(&pdu_id, &pdu)?;
             Ok(())
         } else {
diff --git a/src/pdu.rs b/src/pdu.rs
index 9936802e0..44584237c 100644
--- a/src/pdu.rs
+++ b/src/pdu.rs
@@ -35,7 +35,7 @@ pub struct PduEvent {
 }
 
 impl PduEvent {
-    pub fn redact(&mut self) -> Result<()> {
+    pub fn redact(&mut self, reason: &PduEvent) -> Result<()> {
         self.unsigned.clear();
 
         let allowed: &[&str] = match self.kind {
@@ -71,7 +71,7 @@ pub fn redact(&mut self) -> Result<()> {
 
         self.unsigned.insert(
             "redacted_because".to_owned(),
-            json!({"content": {}, "type": "m.room.redaction"}),
+            serde_json::to_string(reason).expect("PduEvent::to_string always works").into()
         );
 
         self.content = new_content.into();
-- 
GitLab