Upgrade Ruma
Co-authored-by: Timo Kösters <timo@koesters.xyz>
This commit is contained in:
parent
47f3263396
commit
09895a20c8
21 changed files with 628 additions and 623 deletions
95
src/pdu.rs
95
src/pdu.rs
|
@ -1,19 +1,28 @@
|
|||
use crate::Error;
|
||||
use ruma::{
|
||||
events::{
|
||||
pdu::EventHash, room::member::MemberEventContent, AnyEphemeralRoomEvent,
|
||||
AnyInitialStateEvent, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent,
|
||||
AnySyncStateEvent, EventType, StateEvent,
|
||||
room::member::RoomMemberEventContent, AnyEphemeralRoomEvent, AnyInitialStateEvent,
|
||||
AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent,
|
||||
EventType, StateEvent,
|
||||
},
|
||||
serde::{CanonicalJsonObject, CanonicalJsonValue, Raw},
|
||||
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
|
||||
ServerSigningKeyId, UInt, UserId,
|
||||
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, UInt, UserId,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use serde_json::{
|
||||
json,
|
||||
value::{to_raw_value, RawValue as RawJsonValue},
|
||||
};
|
||||
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom};
|
||||
use tracing::warn;
|
||||
|
||||
/// Content hashes of a PDU.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct EventHash {
|
||||
/// The SHA-256 hash.
|
||||
pub sha256: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize, Debug)]
|
||||
pub struct PduEvent {
|
||||
pub event_id: EventId,
|
||||
|
@ -22,7 +31,7 @@ pub struct PduEvent {
|
|||
pub origin_server_ts: UInt,
|
||||
#[serde(rename = "type")]
|
||||
pub kind: EventType,
|
||||
pub content: serde_json::Value,
|
||||
pub content: Box<RawJsonValue>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub state_key: Option<String>,
|
||||
pub prev_events: Vec<EventId>,
|
||||
|
@ -30,16 +39,17 @@ pub struct PduEvent {
|
|||
pub auth_events: Vec<EventId>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub redacts: Option<EventId>,
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
pub unsigned: BTreeMap<String, serde_json::Value>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub unsigned: Option<Box<RawJsonValue>>,
|
||||
pub hashes: EventHash,
|
||||
pub signatures: BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub signatures: Option<Box<RawJsonValue>>, // BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>
|
||||
}
|
||||
|
||||
impl PduEvent {
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn redact(&mut self, reason: &PduEvent) -> crate::Result<()> {
|
||||
self.unsigned.clear();
|
||||
self.unsigned = None;
|
||||
|
||||
let allowed: &[&str] = match self.kind {
|
||||
EventType::RoomMember => &["membership"],
|
||||
|
@ -59,10 +69,9 @@ impl PduEvent {
|
|||
_ => &[],
|
||||
};
|
||||
|
||||
let old_content = self
|
||||
.content
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| Error::bad_database("PDU in db has invalid content."))?;
|
||||
let mut old_content =
|
||||
serde_json::from_str::<BTreeMap<String, serde_json::Value>>(self.content.get())
|
||||
.map_err(|_| Error::bad_database("PDU in db has invalid content."))?;
|
||||
|
||||
let mut new_content = serde_json::Map::new();
|
||||
|
||||
|
@ -72,12 +81,23 @@ impl PduEvent {
|
|||
}
|
||||
}
|
||||
|
||||
self.unsigned.insert(
|
||||
"redacted_because".to_owned(),
|
||||
serde_json::to_value(reason).expect("to_value(PduEvent) always works"),
|
||||
);
|
||||
self.unsigned = Some(to_raw_value(&json!({
|
||||
"redacted_because": serde_json::to_value(reason).expect("to_value(PduEvent) always works")
|
||||
})).expect("to string always works"));
|
||||
|
||||
self.content = new_content.into();
|
||||
self.content = to_raw_value(&new_content).expect("to string always works");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove_transaction_id(&mut self) -> crate::Result<()> {
|
||||
if let Some(unsigned) = &self.unsigned {
|
||||
let mut unsigned =
|
||||
serde_json::from_str::<BTreeMap<String, Box<RawJsonValue>>>(unsigned.get())
|
||||
.map_err(|_| Error::bad_database("Invalid unsigned in pdu event"))?;
|
||||
unsigned.remove("transaction_id");
|
||||
self.unsigned = Some(to_raw_value(&unsigned).expect("unsigned is valid"));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -192,7 +212,7 @@ impl PduEvent {
|
|||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
pub fn to_member_event(&self) -> Raw<StateEvent<MemberEventContent>> {
|
||||
pub fn to_member_event(&self) -> Raw<StateEvent<RoomMemberEventContent>> {
|
||||
let json = json!({
|
||||
"content": self.content,
|
||||
"type": self.kind,
|
||||
|
@ -212,7 +232,7 @@ impl PduEvent {
|
|||
#[tracing::instrument]
|
||||
pub fn convert_to_outgoing_federation_event(
|
||||
mut pdu_json: CanonicalJsonObject,
|
||||
) -> Raw<ruma::events::pdu::Pdu> {
|
||||
) -> Box<RawJsonValue> {
|
||||
if let Some(unsigned) = pdu_json
|
||||
.get_mut("unsigned")
|
||||
.and_then(|val| val.as_object_mut())
|
||||
|
@ -229,10 +249,7 @@ impl PduEvent {
|
|||
// )
|
||||
// .expect("Raw::from_value always works")
|
||||
|
||||
serde_json::from_value::<Raw<_>>(
|
||||
serde_json::to_value(pdu_json).expect("CanonicalJson is valid serde_json::Value"),
|
||||
)
|
||||
.expect("Raw::from_value always works")
|
||||
to_raw_value(&pdu_json).expect("CanonicalJson is valid serde_json::Value")
|
||||
}
|
||||
|
||||
pub fn from_id_val(
|
||||
|
@ -265,7 +282,7 @@ impl state_res::Event for PduEvent {
|
|||
&self.kind
|
||||
}
|
||||
|
||||
fn content(&self) -> &serde_json::Value {
|
||||
fn content(&self) -> &RawJsonValue {
|
||||
&self.content
|
||||
}
|
||||
|
||||
|
@ -281,10 +298,6 @@ impl state_res::Event for PduEvent {
|
|||
Box::new(self.prev_events.iter())
|
||||
}
|
||||
|
||||
fn depth(&self) -> &UInt {
|
||||
&self.depth
|
||||
}
|
||||
|
||||
fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + '_> {
|
||||
Box::new(self.auth_events.iter())
|
||||
}
|
||||
|
@ -292,18 +305,6 @@ impl state_res::Event for PduEvent {
|
|||
fn redacts(&self) -> Option<&EventId> {
|
||||
self.redacts.as_ref()
|
||||
}
|
||||
|
||||
fn hashes(&self) -> &EventHash {
|
||||
&self.hashes
|
||||
}
|
||||
|
||||
fn signatures(&self) -> BTreeMap<Box<ServerName>, BTreeMap<ruma::ServerSigningKeyId, String>> {
|
||||
self.signatures.clone()
|
||||
}
|
||||
|
||||
fn unsigned(&self) -> &BTreeMap<String, serde_json::Value> {
|
||||
&self.unsigned
|
||||
}
|
||||
}
|
||||
|
||||
// These impl's allow us to dedup state snapshots when resolving state
|
||||
|
@ -329,9 +330,9 @@ impl Ord for PduEvent {
|
|||
///
|
||||
/// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`.
|
||||
pub(crate) fn gen_event_id_canonical_json(
|
||||
pdu: &Raw<ruma::events::pdu::Pdu>,
|
||||
pdu: &RawJsonValue,
|
||||
) -> crate::Result<(EventId, CanonicalJsonObject)> {
|
||||
let value = serde_json::from_str(pdu.json().get()).map_err(|e| {
|
||||
let value = serde_json::from_str(pdu.get()).map_err(|e| {
|
||||
warn!("Error parsing incoming event {:?}: {:?}", pdu, e);
|
||||
Error::BadServerResponse("Invalid PDU in server response")
|
||||
})?;
|
||||
|
@ -352,7 +353,7 @@ pub(crate) fn gen_event_id_canonical_json(
|
|||
pub struct PduBuilder {
|
||||
#[serde(rename = "type")]
|
||||
pub event_type: EventType,
|
||||
pub content: serde_json::Value,
|
||||
pub content: Box<RawJsonValue>,
|
||||
pub unsigned: Option<BTreeMap<String, serde_json::Value>>,
|
||||
pub state_key: Option<String>,
|
||||
pub redacts: Option<EventId>,
|
||||
|
@ -363,7 +364,7 @@ impl From<AnyInitialStateEvent> for PduBuilder {
|
|||
fn from(event: AnyInitialStateEvent) -> Self {
|
||||
Self {
|
||||
event_type: EventType::from(event.event_type()),
|
||||
content: serde_json::value::to_value(event.content())
|
||||
content: to_raw_value(&event.content())
|
||||
.expect("AnyStateEventContent came from JSON and can thus turn back into JSON."),
|
||||
unsigned: None,
|
||||
state_key: Some(event.state_key().to_owned()),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue