2025-04-26 08:24:47 +00:00
|
|
|
use ruma::{RoomVersionId, canonical_json::redact_content_in_place};
|
|
|
|
|
use serde_json::{json, value::to_raw_value};
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2025-04-26 08:24:47 +00:00
|
|
|
use crate::{Error, Result, err, implement};
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2024-11-07 03:30:47 +00:00
|
|
|
#[implement(super::Pdu)]
|
2024-12-07 04:42:35 +00:00
|
|
|
pub fn redact(&mut self, room_version_id: &RoomVersionId, reason: &Self) -> Result {
|
2024-10-25 01:16:01 +00:00
|
|
|
self.unsigned = None;
|
|
|
|
|
|
2024-12-15 00:05:47 -05:00
|
|
|
let mut content = serde_json::from_str(self.content.get())
|
2025-04-26 08:24:47 +00:00
|
|
|
.map_err(|e| err!(Request(BadJson("Failed to deserialize content into type: {e}"))))?;
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2024-12-07 04:42:35 +00:00
|
|
|
redact_content_in_place(&mut content, room_version_id, self.kind.to_string())
|
2024-10-25 01:16:01 +00:00
|
|
|
.map_err(|e| Error::Redaction(self.sender.server_name().to_owned(), e))?;
|
|
|
|
|
|
2025-04-26 08:24:47 +00:00
|
|
|
let reason = serde_json::to_value(reason).expect("Failed to preserialize reason");
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2025-04-26 08:24:47 +00:00
|
|
|
let redacted_because = json!({
|
|
|
|
|
"redacted_because": reason,
|
|
|
|
|
});
|
2025-01-30 05:14:45 +00:00
|
|
|
|
2025-04-26 08:24:47 +00:00
|
|
|
self.unsigned = to_raw_value(&redacted_because)
|
|
|
|
|
.expect("Failed to serialize unsigned")
|
|
|
|
|
.into();
|
2025-01-30 05:14:45 +00:00
|
|
|
|
2025-04-26 08:24:47 +00:00
|
|
|
self.content = to_raw_value(&content).expect("Failed to serialize content");
|
2025-01-30 05:14:45 +00:00
|
|
|
|
2025-04-26 08:24:47 +00:00
|
|
|
Ok(())
|
2025-01-30 05:14:45 +00:00
|
|
|
}
|