2025-04-26 08:24:47 +00:00
|
|
|
use ruma::{RoomVersionId, canonical_json::redact_content_in_place};
|
2026-09-21 00:39:34 +01:00
|
|
|
use serde_json::{json, value::to_raw_value};
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2026-06-26 06:31:46 +01:00
|
|
|
use crate::{Err, Result, err};
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2026-06-26 06:31:46 +01:00
|
|
|
impl super::Pdu {
|
2026-09-21 00:39:34 +01:00
|
|
|
pub fn redact(
|
|
|
|
|
&mut self,
|
|
|
|
|
room_version_id: &RoomVersionId,
|
|
|
|
|
redacted_because: Option<&ruma::EventId>,
|
|
|
|
|
) -> Result {
|
2026-06-26 06:31:46 +01:00
|
|
|
let Some(rules) = room_version_id.rules() else {
|
|
|
|
|
return Err!("Cannot redact event for unknown room version {room_version_id}");
|
|
|
|
|
};
|
2026-03-29 12:25:42 -04:00
|
|
|
|
2026-06-26 06:31:46 +01:00
|
|
|
self.unsigned = None;
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2026-08-02 10:14:17 +02:00
|
|
|
// a redacted sticky event is just a normal event (MSC4354)
|
|
|
|
|
self.sticky = None;
|
|
|
|
|
|
2026-06-26 06:31:46 +01:00
|
|
|
let mut content = serde_json::from_str(self.content.get()).map_err(|e| {
|
|
|
|
|
err!(Request(BadJson("Failed to deserialize content into type: {e}")))
|
|
|
|
|
})?;
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2026-06-26 06:31:46 +01:00
|
|
|
redact_content_in_place(&mut content, &rules.redaction, self.kind.to_string());
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2026-09-21 00:39:34 +01:00
|
|
|
if let Some(event_id) = redacted_because {
|
|
|
|
|
self.unsigned = to_raw_value(&json!({
|
2026-09-21 03:01:34 +01:00
|
|
|
"org.continuwuity.redacted_by": event_id,
|
2026-09-21 00:39:34 +01:00
|
|
|
}))
|
2026-06-26 06:31:46 +01:00
|
|
|
.expect("Failed to serialize unsigned")
|
|
|
|
|
.into();
|
2026-09-21 00:39:34 +01:00
|
|
|
}
|
2025-04-26 08:24:47 +00:00
|
|
|
|
2026-06-26 06:31:46 +01:00
|
|
|
self.content = to_raw_value(&content).expect("Failed to serialize content");
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2026-06-26 06:31:46 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-10-25 01:16:01 +00:00
|
|
|
}
|