fix: problems with pdu serialization
parent
dd3dab39ae
commit
21eb8d4fe3
|
@ -1485,6 +1485,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "rocket"
|
||||
version = "0.5.0-dev"
|
||||
source = "git+https://github.com/timokoesters/Rocket.git?branch=empty_parameters#f383f15047d0dda71dd21399dfea96161fe2bd0e"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"atomic",
|
||||
|
@ -1509,6 +1510,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "rocket_codegen"
|
||||
version = "0.5.0-dev"
|
||||
source = "git+https://github.com/timokoesters/Rocket.git?branch=empty_parameters#f383f15047d0dda71dd21399dfea96161fe2bd0e"
|
||||
dependencies = [
|
||||
"devise",
|
||||
"glob",
|
||||
|
@ -1520,6 +1522,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "rocket_http"
|
||||
version = "0.5.0-dev"
|
||||
source = "git+https://github.com/timokoesters/Rocket.git?branch=empty_parameters#f383f15047d0dda71dd21399dfea96161fe2bd0e"
|
||||
dependencies = [
|
||||
"cookie",
|
||||
"http",
|
||||
|
|
|
@ -14,7 +14,7 @@ edition = "2018"
|
|||
[dependencies]
|
||||
# TODO: This can become optional as soon as proper configs are supported
|
||||
#rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "8d779caa22c63b15a6c3ceb75d8f6d4971b2eb67", features = ["tls"] } # Used to handle requests
|
||||
rocket = { path = "../rocket/core/lib", features = ["tls"] }
|
||||
rocket = { git = "https://github.com/timokoesters/Rocket.git", branch = "empty_parameters", features = ["tls"] }
|
||||
|
||||
tokio = "0.2.22" # Used for long polling
|
||||
ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "d5d2d1d893fa12d27960e4c58d6c09b215d06e95" } # Used for matrix spec type definitions and helpers
|
||||
|
|
108
src/pdu.rs
108
src/pdu.rs
|
@ -79,39 +79,99 @@ impl PduEvent {
|
|||
}
|
||||
|
||||
pub fn to_sync_room_event(&self) -> Raw<AnySyncRoomEvent> {
|
||||
let json = serde_json::to_string(&self).expect("PDUs are always valid");
|
||||
serde_json::from_str::<AnySyncRoomEvent>(&json)
|
||||
.map(Raw::from)
|
||||
.expect("AnySyncRoomEvent can always be built from a full PDU event")
|
||||
let mut json = json!({
|
||||
"content": self.content,
|
||||
"type": self.kind,
|
||||
"event_id": self.event_id,
|
||||
"sender": self.sender,
|
||||
"origin_server_ts": self.origin_server_ts,
|
||||
"unsigned": self.unsigned,
|
||||
});
|
||||
|
||||
if let Some(state_key) = &self.state_key {
|
||||
json["state_key"] = json!(state_key);
|
||||
}
|
||||
if let Some(redacts) = &self.redacts {
|
||||
json["redacts"] = json!(redacts);
|
||||
}
|
||||
|
||||
serde_json::from_value(json).expect("Raw::from_value always works")
|
||||
}
|
||||
|
||||
pub fn to_room_event(&self) -> Raw<AnyRoomEvent> {
|
||||
let json = serde_json::to_string(&self).expect("PDUs are always valid");
|
||||
serde_json::from_str::<AnyRoomEvent>(&json)
|
||||
.map(Raw::from)
|
||||
.expect("AnyRoomEvent can always be built from a full PDU event")
|
||||
let mut json = json!({
|
||||
"content": self.content,
|
||||
"type": self.kind,
|
||||
"event_id": self.event_id,
|
||||
"sender": self.sender,
|
||||
"origin_server_ts": self.origin_server_ts,
|
||||
"unsigned": self.unsigned,
|
||||
"room_id": self.room_id,
|
||||
});
|
||||
|
||||
if let Some(state_key) = &self.state_key {
|
||||
json["state_key"] = json!(state_key);
|
||||
}
|
||||
if let Some(redacts) = &self.redacts {
|
||||
json["redacts"] = json!(redacts);
|
||||
}
|
||||
|
||||
serde_json::from_value(json).expect("Raw::from_value always works")
|
||||
}
|
||||
|
||||
pub fn to_state_event(&self) -> Raw<AnyStateEvent> {
|
||||
let json = serde_json::to_string(&self).expect("PDUs are always valid");
|
||||
serde_json::from_str::<AnyStateEvent>(&json)
|
||||
.map(Raw::from)
|
||||
.expect("AnyStateEvent can always be built from a full PDU event")
|
||||
let json = json!({
|
||||
"content": self.content,
|
||||
"type": self.kind,
|
||||
"event_id": self.event_id,
|
||||
"sender": self.sender,
|
||||
"origin_server_ts": self.origin_server_ts,
|
||||
"unsigned": self.unsigned,
|
||||
"room_id": self.room_id,
|
||||
"state_key": self.state_key,
|
||||
});
|
||||
|
||||
serde_json::from_value(json).expect("Raw::from_value always works")
|
||||
}
|
||||
|
||||
pub fn to_sync_state_event(&self) -> Raw<AnySyncStateEvent> {
|
||||
let json = serde_json::to_string(&self).expect("PDUs are always valid");
|
||||
serde_json::from_str::<AnySyncStateEvent>(&json)
|
||||
.map(Raw::from)
|
||||
.expect("AnySyncStateEvent can always be built from a full PDU event")
|
||||
let json = json!({
|
||||
"content": self.content,
|
||||
"type": self.kind,
|
||||
"event_id": self.event_id,
|
||||
"sender": self.sender,
|
||||
"origin_server_ts": self.origin_server_ts,
|
||||
"unsigned": self.unsigned,
|
||||
"state_key": self.state_key,
|
||||
});
|
||||
|
||||
serde_json::from_value(json).expect("Raw::from_value always works")
|
||||
}
|
||||
|
||||
pub fn to_stripped_state_event(&self) -> Raw<AnyStrippedStateEvent> {
|
||||
let json = serde_json::to_string(&self).expect("PDUs are always valid");
|
||||
serde_json::from_str::<AnyStrippedStateEvent>(&json)
|
||||
.map(Raw::from)
|
||||
.expect("AnyStrippedStateEvent can always be built from a full PDU event")
|
||||
let json = json!({
|
||||
"content": self.content,
|
||||
"type": self.kind,
|
||||
"sender": self.sender,
|
||||
"state_key": self.state_key,
|
||||
});
|
||||
|
||||
serde_json::from_value(json).expect("Raw::from_value always works")
|
||||
}
|
||||
|
||||
pub fn to_member_event(&self) -> Raw<StateEvent<MemberEventContent>> {
|
||||
let json = serde_json::to_string(&self).expect("PDUs are always valid");
|
||||
serde_json::from_str::<StateEvent<MemberEventContent>>(&json)
|
||||
.map(Raw::from)
|
||||
.expect("StateEvent<MemberEventContent> can always be built from a full PDU event")
|
||||
let json = json!({
|
||||
"content": self.content,
|
||||
"type": self.kind,
|
||||
"event_id": self.event_id,
|
||||
"sender": self.sender,
|
||||
"origin_server_ts": self.origin_server_ts,
|
||||
"redacts": self.redacts,
|
||||
"unsigned": self.unsigned,
|
||||
"room_id": self.room_id,
|
||||
"state_key": self.state_key,
|
||||
});
|
||||
|
||||
serde_json::from_value(json).expect("Raw::from_value always works")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue