Skip to content

Commit 37cadc7

Browse files
committed
Add From<RoomEvent> for SyncRoomEvent
Since SyncRoomEvents are just RoomEvents without a room_id, it is safe to just discard the information that this field exists in the underlying json.
1 parent 87de0b5 commit 37cadc7

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

crates/matrix-sdk-common/src/deserialized_responses.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ impl From<Raw<AnySyncRoomEvent>> for SyncRoomEvent {
103103
}
104104
}
105105

106+
impl From<RoomEvent> for SyncRoomEvent {
107+
fn from(o: RoomEvent) -> Self {
108+
// This conversion is unproblematic since a SyncRoomEvent is just a
109+
// RoomEvent without the room_id. By converting the raw value in this
110+
// way, we simply cause the `room_id` field in the json to be ignored by
111+
// a subsequent deserialization.
112+
Self { encryption_info: o.encryption_info, event: Raw::from_json(o.event.into_json()) }
113+
}
114+
}
115+
106116
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
107117
pub struct SyncResponse {
108118
/// The batch token to supply in the `since` param of the next `/sync`
@@ -343,3 +353,48 @@ pub struct MembersResponse {
343353
/// Collection of ambiguity changes that room member events trigger.
344354
pub ambiguity_changes: AmbiguityChanges,
345355
}
356+
357+
#[cfg(test)]
358+
mod test {
359+
use ruma::{
360+
event_id,
361+
events::{
362+
room::message::RoomMessageEventContent, AnyMessageEvent, AnySyncMessageEvent,
363+
AnySyncRoomEvent, MessageEvent,
364+
},
365+
room_id, user_id, MilliSecondsSinceUnixEpoch,
366+
};
367+
368+
use super::{Raw, RoomEvent, SyncRoomEvent, Unsigned};
369+
370+
#[test]
371+
fn room_event_to_sync_room_event() {
372+
let content = RoomMessageEventContent::text_plain("foobar");
373+
374+
let event: AnyMessageEvent = MessageEvent {
375+
content,
376+
event_id: event_id!("$xxxxx:example.org").to_owned(),
377+
room_id: room_id!("!someroom:example.com").to_owned(),
378+
origin_server_ts: MilliSecondsSinceUnixEpoch::now(),
379+
sender: user_id!("@carl:example.com").to_owned(),
380+
unsigned: Unsigned::default(),
381+
}
382+
.into();
383+
384+
let room_event =
385+
RoomEvent { event: Raw::new(&event.clone().into()).unwrap(), encryption_info: None };
386+
387+
let converted_room_event: SyncRoomEvent = room_event.into();
388+
389+
let converted_event: AnySyncRoomEvent = converted_room_event.event.deserialize().unwrap();
390+
391+
let sync_event: AnySyncMessageEvent = event.into();
392+
let sync_event: AnySyncRoomEvent = sync_event.into();
393+
394+
// There is no PartialEq implementation for AnySyncRoomEvent, so we
395+
// just compare a couple of fields here. The important thing is that
396+
// the deserialization above worked.
397+
assert_eq!(converted_event.event_id(), sync_event.event_id());
398+
assert_eq!(converted_event.sender(), sync_event.sender());
399+
}
400+
}

0 commit comments

Comments
 (0)