2024-10-25 01:16:01 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
|
|
use ruma::MilliSecondsSinceUnixEpoch;
|
2025-02-23 01:17:45 -05:00
|
|
|
use serde_json::value::{RawValue as RawJsonValue, Value as JsonValue, to_raw_value};
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2024-11-07 03:30:47 +00:00
|
|
|
use super::Pdu;
|
2025-04-27 02:39:28 +00:00
|
|
|
use crate::{Result, err, implement};
|
2024-10-25 01:16:01 +00:00
|
|
|
|
2024-11-07 03:30:47 +00:00
|
|
|
#[implement(Pdu)]
|
|
|
|
|
pub fn remove_transaction_id(&mut self) -> Result {
|
2025-02-06 10:23:17 +00:00
|
|
|
use BTreeMap as Map;
|
|
|
|
|
|
2024-10-25 01:16:01 +00:00
|
|
|
let Some(unsigned) = &self.unsigned else {
|
|
|
|
|
return Ok(());
|
|
|
|
|
};
|
|
|
|
|
|
2025-02-06 10:23:17 +00:00
|
|
|
let mut unsigned: Map<&str, Box<RawJsonValue>> = serde_json::from_str(unsigned.get())
|
2024-12-15 00:05:47 -05:00
|
|
|
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
2024-10-25 01:16:01 +00:00
|
|
|
|
|
|
|
|
unsigned.remove("transaction_id");
|
|
|
|
|
self.unsigned = to_raw_value(&unsigned)
|
|
|
|
|
.map(Some)
|
|
|
|
|
.expect("unsigned is valid");
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 03:30:47 +00:00
|
|
|
#[implement(Pdu)]
|
|
|
|
|
pub fn add_age(&mut self) -> Result {
|
2025-02-06 10:23:17 +00:00
|
|
|
use BTreeMap as Map;
|
|
|
|
|
|
|
|
|
|
let mut unsigned: Map<&str, Box<RawJsonValue>> = self
|
2024-10-25 01:16:01 +00:00
|
|
|
.unsigned
|
2025-02-06 10:23:17 +00:00
|
|
|
.as_deref()
|
|
|
|
|
.map(RawJsonValue::get)
|
|
|
|
|
.map_or_else(|| Ok(Map::new()), serde_json::from_str)
|
2024-10-25 01:16:01 +00:00
|
|
|
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
|
|
|
|
|
|
|
|
|
// deliberately allowing for the possibility of negative age
|
|
|
|
|
let now: i128 = MilliSecondsSinceUnixEpoch::now().get().into();
|
|
|
|
|
let then: i128 = self.origin_server_ts.into();
|
|
|
|
|
let this_age = now.saturating_sub(then);
|
|
|
|
|
|
2025-02-06 10:23:17 +00:00
|
|
|
unsigned.insert("age", to_raw_value(&this_age)?);
|
|
|
|
|
self.unsigned = Some(to_raw_value(&unsigned)?);
|
2024-10-25 01:16:01 +00:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 03:30:47 +00:00
|
|
|
#[implement(Pdu)]
|
2025-02-06 09:53:53 +00:00
|
|
|
pub fn add_relation(&mut self, name: &str, pdu: Option<&Pdu>) -> Result {
|
|
|
|
|
use serde_json::Map;
|
|
|
|
|
|
|
|
|
|
let mut unsigned: Map<String, JsonValue> = self
|
2024-11-07 03:30:47 +00:00
|
|
|
.unsigned
|
2025-02-06 10:23:17 +00:00
|
|
|
.as_deref()
|
|
|
|
|
.map(RawJsonValue::get)
|
|
|
|
|
.map_or_else(|| Ok(Map::new()), serde_json::from_str)
|
2024-11-07 03:30:47 +00:00
|
|
|
.map_err(|e| err!(Database("Invalid unsigned in pdu event: {e}")))?;
|
|
|
|
|
|
2025-02-06 09:53:53 +00:00
|
|
|
let pdu = pdu
|
|
|
|
|
.map(serde_json::to_value)
|
|
|
|
|
.transpose()?
|
|
|
|
|
.unwrap_or_else(|| JsonValue::Object(Map::new()));
|
2024-11-07 03:30:47 +00:00
|
|
|
|
2025-02-06 09:53:53 +00:00
|
|
|
unsigned
|
|
|
|
|
.entry("m.relations")
|
|
|
|
|
.or_insert(JsonValue::Object(Map::new()))
|
2024-11-07 03:30:47 +00:00
|
|
|
.as_object_mut()
|
2025-02-06 10:23:17 +00:00
|
|
|
.map(|object| object.insert(name.to_owned(), pdu));
|
2024-11-07 03:30:47 +00:00
|
|
|
|
2025-02-06 10:23:17 +00:00
|
|
|
self.unsigned = Some(to_raw_value(&unsigned)?);
|
2024-11-07 03:30:47 +00:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|