fix: problems with pdu serialization

next
timokoesters 2020-07-27 21:53:28 +02:00
parent dd3dab39ae
commit 21eb8d4fe3
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
3 changed files with 88 additions and 25 deletions

3
Cargo.lock generated
View File

@ -1485,6 +1485,7 @@ dependencies = [
[[package]] [[package]]
name = "rocket" name = "rocket"
version = "0.5.0-dev" version = "0.5.0-dev"
source = "git+https://github.com/timokoesters/Rocket.git?branch=empty_parameters#f383f15047d0dda71dd21399dfea96161fe2bd0e"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"atomic", "atomic",
@ -1509,6 +1510,7 @@ dependencies = [
[[package]] [[package]]
name = "rocket_codegen" name = "rocket_codegen"
version = "0.5.0-dev" version = "0.5.0-dev"
source = "git+https://github.com/timokoesters/Rocket.git?branch=empty_parameters#f383f15047d0dda71dd21399dfea96161fe2bd0e"
dependencies = [ dependencies = [
"devise", "devise",
"glob", "glob",
@ -1520,6 +1522,7 @@ dependencies = [
[[package]] [[package]]
name = "rocket_http" name = "rocket_http"
version = "0.5.0-dev" version = "0.5.0-dev"
source = "git+https://github.com/timokoesters/Rocket.git?branch=empty_parameters#f383f15047d0dda71dd21399dfea96161fe2bd0e"
dependencies = [ dependencies = [
"cookie", "cookie",
"http", "http",

View File

@ -14,7 +14,7 @@ edition = "2018"
[dependencies] [dependencies]
# TODO: This can become optional as soon as proper configs are supported # 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 = { 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 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 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

View File

@ -79,39 +79,99 @@ impl PduEvent {
} }
pub fn to_sync_room_event(&self) -> Raw<AnySyncRoomEvent> { pub fn to_sync_room_event(&self) -> Raw<AnySyncRoomEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let mut json = json!({
serde_json::from_str::<AnySyncRoomEvent>(&json) "content": self.content,
.map(Raw::from) "type": self.kind,
.expect("AnySyncRoomEvent can always be built from a full PDU event") "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> { pub fn to_room_event(&self) -> Raw<AnyRoomEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let mut json = json!({
serde_json::from_str::<AnyRoomEvent>(&json) "content": self.content,
.map(Raw::from) "type": self.kind,
.expect("AnyRoomEvent can always be built from a full PDU event") "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> { pub fn to_state_event(&self) -> Raw<AnyStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = json!({
serde_json::from_str::<AnyStateEvent>(&json) "content": self.content,
.map(Raw::from) "type": self.kind,
.expect("AnyStateEvent can always be built from a full PDU event") "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> { pub fn to_sync_state_event(&self) -> Raw<AnySyncStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = json!({
serde_json::from_str::<AnySyncStateEvent>(&json) "content": self.content,
.map(Raw::from) "type": self.kind,
.expect("AnySyncStateEvent can always be built from a full PDU event") "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> { pub fn to_stripped_state_event(&self) -> Raw<AnyStrippedStateEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = json!({
serde_json::from_str::<AnyStrippedStateEvent>(&json) "content": self.content,
.map(Raw::from) "type": self.kind,
.expect("AnyStrippedStateEvent can always be built from a full PDU event") "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>> { pub fn to_member_event(&self) -> Raw<StateEvent<MemberEventContent>> {
let json = serde_json::to_string(&self).expect("PDUs are always valid"); let json = json!({
serde_json::from_str::<StateEvent<MemberEventContent>>(&json) "content": self.content,
.map(Raw::from) "type": self.kind,
.expect("StateEvent<MemberEventContent> can always be built from a full PDU event") "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")
} }
} }