From 853734e30fe59df4d72342d16bfbc7864889ef56 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 24 Feb 2026 13:19:24 +0000 Subject: [PATCH] WIP --- rust/src/events/mod.rs | 59 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/rust/src/events/mod.rs b/rust/src/events/mod.rs index 209efb917b..834e67f461 100644 --- a/rust/src/events/mod.rs +++ b/rust/src/events/mod.rs @@ -20,10 +20,15 @@ //! Classes for representing Events. +use std::{collections::HashMap, sync::Arc}; + use pyo3::{ + exceptions::PyKeyError, + pyclass, pymethods, types::{PyAnyMethods, PyModule, PyModuleMethods}, - wrap_pyfunction, Bound, PyResult, Python, + wrap_pyfunction, Bound, IntoPyObject, PyAny, PyResult, Python, }; +use pythonize::{depythonize, pythonize}; pub mod filter; mod internal_metadata; @@ -32,6 +37,7 @@ mod internal_metadata; pub fn register_module(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { let child_module = PyModule::new(py, "events")?; child_module.add_class::()?; + child_module.add_class::()?; child_module.add_function(wrap_pyfunction!(filter::event_visible_to_server_py, m)?)?; m.add_submodule(&child_module)?; @@ -44,3 +50,54 @@ pub fn register_module(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> Ok(()) } + +struct Hashes { + sha256: Option<[u8; 32]>, + others: std::collections::HashMap, Box>, +} + +#[pyclass] +struct EventInner { + #[pyo3(get)] + content: JsonObject, + depth: i64, + hashes: Hashes, + origin_server_ts: i64, + sender: Box, + state_key: Option>, + type_: Box, + + unsigned: JsonObject, + signatures: HashMap, HashMap, Box>>, +} + +#[pyclass(mapping)] +#[derive(Clone)] +struct JsonObject { + object: Arc, serde_json::Value>>, +} + +#[pymethods] +impl JsonObject { + #[new] + fn new<'a, 'py>(object: &'a Bound<'py, PyAny>) -> PyResult { + Ok(Self { + object: Arc::new(depythonize(&object)?), + }) + } + + fn __len__(&self) -> usize { + self.object.len() + } + + fn __contains__(&self, key: &str) -> bool { + self.object.contains_key(key) + } + + fn __getitem__<'py>(&self, py: Python<'py>, key: &str) -> PyResult>> { + let Some(value) = self.object.get(key) else { + return Err(PyKeyError::new_err(key.to_string())); + }; + Ok(Some(pythonize(py, value)?)) + } +}