fix pdu add_relation() helper

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-02-06 09:53:53 +00:00 committed by strawberry
parent 3ec43be959
commit 2d71d5590a

View file

@ -46,23 +46,26 @@ pub fn add_age(&mut self) -> Result {
} }
#[implement(Pdu)] #[implement(Pdu)]
pub fn add_relation(&mut self, name: &str, pdu: &Pdu) -> Result { pub fn add_relation(&mut self, name: &str, pdu: Option<&Pdu>) -> Result {
let mut unsigned: BTreeMap<String, JsonValue> = self use serde_json::Map;
let mut unsigned: Map<String, JsonValue> = self
.unsigned .unsigned
.as_ref() .as_ref()
.map_or_else(|| Ok(BTreeMap::new()), |u| serde_json::from_str(u.get())) .map_or_else(|| Ok(Map::new()), |u| serde_json::from_str(u.get()))
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?; .map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
let relations: &mut JsonValue = unsigned.entry("m.relations".into()).or_default(); let pdu = pdu
if relations.as_object_mut().is_none() { .map(serde_json::to_value)
let mut object = serde_json::Map::<String, JsonValue>::new(); .transpose()?
_ = relations.as_object_mut().insert(&mut object); .unwrap_or_else(|| JsonValue::Object(Map::new()));
}
relations unsigned
.entry("m.relations")
.or_insert(JsonValue::Object(Map::new()))
.as_object_mut() .as_object_mut()
.expect("we just created it") .unwrap()
.insert(name.to_owned(), serde_json::to_value(pdu)?); .insert(name.to_owned(), pdu);
self.unsigned = to_raw_value(&unsigned) self.unsigned = to_raw_value(&unsigned)
.map(Some) .map(Some)