fix pdu add_relation() helper
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
3ec43be959
commit
2d71d5590a
1 changed files with 14 additions and 11 deletions
|
@ -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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue