From 586368ce9c3eee2186e841d8e31efa1ae5de7faf Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Mon, 22 Jun 2026 11:16:23 +0100 Subject: [PATCH] Create and use `Event::redacts()` --- rust/src/events/mod.rs | 18 ++++++++++++++---- rust/src/events/serialize.rs | 7 +------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/rust/src/events/mod.rs b/rust/src/events/mod.rs index 47293df356..21d56e8e7a 100644 --- a/rust/src/events/mod.rs +++ b/rust/src/events/mod.rs @@ -58,6 +58,7 @@ use pyo3::{ wrap_pyfunction, Bound, IntoPyObject, PyAny, PyResult, Python, }; use pythonize::{depythonize, pythonize}; +use serde_json::Value; use crate::events::{ constants::event_type::M_ROOM_MEMBER, @@ -604,8 +605,19 @@ impl Event { } } - #[getter] - fn redacts<'py>(&self, py: Python<'py>) -> PyResult>> { + /// Returns the `redacts` field of this event, if it has one. + #[getter(redacts)] + fn redacts_py<'py>(&self, py: Python<'py>) -> PyResult>> { + let value = self.redacts(); + value + .map(|v| pythonize(py, v).map_err(Into::into)) + .transpose() + } +} + +impl Event { + /// Returns the `redacts` field of this event, if it has one. + pub fn redacts(&self) -> Option<&Value> { let common = &self.parsed_event.common_fields; let value = if self.room_version.updated_redaction_rules { common.content.get_field(REDACTS) @@ -613,8 +625,6 @@ impl Event { common.other_fields.get(REDACTS) }; value - .map(|v| pythonize(py, v).map_err(Into::into)) - .transpose() } } diff --git a/rust/src/events/serialize.rs b/rust/src/events/serialize.rs index 42e08d155e..019fd67e5f 100644 --- a/rust/src/events/serialize.rs +++ b/rust/src/events/serialize.rs @@ -549,12 +549,7 @@ fn serialize_event_value( // already in the version-correct place; copy it to the *other* one too, // for forwards/backwards-compatibility with clients. if event.r#type() == M_ROOM_REDACTION { - let common = &event.parsed_event.common_fields; - let redacts = if event.room_version.updated_redaction_rules { - common.content.get_field(REDACTS) - } else { - common.other_fields.get(REDACTS) - }; + let redacts = event.redacts(); // Skip a present-but-null value: the Python `e.redacts` property // surfaced JSON null as `None`, and the old code guarded with // `e.redacts is not None`.