Add to_*_event_stub methods to pdu, filter for correct event kind

When creating some responses (sync) an AnyRoomEventStub is needed for this
PduEvent will deserialize the JSON as a Stub event and a non stub event
when needed. Ephemeral and account events are checked to be the correct
type and filtered out if not. This requires an extra `deserialize` call
which could/should be removed.

TODO: Possibly get rid of EventJson in some places.
This commit is contained in:
Devin R 2020-06-22 07:26:09 -04:00
parent 7c38e53839
commit 24b6702047
3 changed files with 136 additions and 102 deletions

View file

@ -2,8 +2,8 @@ use crate::{Error, Result};
use js_int::UInt;
use ruma::{
events::{
pdu::EventHash, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEventStub, EventJson,
EventType,
pdu::EventHash, AnyRoomEvent, AnyRoomEventStub, AnyStateEvent, AnyStateEventStub,
AnyStrippedStateEventStub, EventJson, EventType,
},
identifiers::{EventId, RoomId, UserId},
};
@ -78,6 +78,11 @@ impl PduEvent {
Ok(())
}
pub fn to_room_event_stub(&self) -> EventJson<AnyRoomEventStub> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnyRoomEventStub>>(&json)
.expect("EventJson::from_str always works")
}
pub fn to_room_event(&self) -> EventJson<AnyRoomEvent> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnyRoomEvent>>(&json)
@ -88,6 +93,11 @@ impl PduEvent {
serde_json::from_str::<EventJson<AnyStateEvent>>(&json)
.expect("EventJson::from_str always works")
}
pub fn to_state_event_stub(&self) -> EventJson<AnyStateEventStub> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnyStateEventStub>>(&json)
.expect("EventJson::from_str always works")
}
pub fn to_stripped_state_event(&self) -> EventJson<AnyStrippedStateEventStub> {
let json = serde_json::to_string(&self).expect("PDUs are always valid");
serde_json::from_str::<EventJson<AnyStrippedStateEventStub>>(&json)