From e9fc9b03fd4345ea822b63b5ce4ffccbe63f538b Mon Sep 17 00:00:00 2001 From: Victor Berger Date: Mon, 7 Jan 2019 23:48:57 +0100 Subject: [PATCH] Make room_id optional This field can be absent in some contextes, notably in the responses to the /sync endpoint, where the events are summarised in the initial sync of a client. Fixes #19 --- src/lib.rs | 5 ++++- src/macros.rs | 10 ++++++---- src/receipt.rs | 6 +++++- src/room/pinned_events.rs | 2 +- src/typing.rs | 6 +++++- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a1e15bba..bbb8382a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -214,7 +214,10 @@ pub trait RoomEvent: Event { fn origin_server_ts(&self) -> u64; /// The unique identifier for the room associated with this event. - fn room_id(&self) -> &RoomId; + /// + /// This can be `None` if the event came from a context where there is + /// no ambiguity which room it belongs to, like a `/sync` response for example. + fn room_id(&self) -> Option<&RoomId>; /// The unique identifier for the user who sent this event. fn sender(&self) -> &UserId; diff --git a/src/macros.rs b/src/macros.rs index 42fb1bec..828cdbc2 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -94,7 +94,8 @@ macro_rules! room_event { pub origin_server_ts: u64, /// The unique identifier for the room associated with this event. - pub room_id: ::ruma_identifiers::RoomId, + #[serde(skip_serializing_if="Option::is_none")] + pub room_id: Option<::ruma_identifiers::RoomId>, /// Additional key-value pairs not signed by the homeserver. #[serde(skip_serializing_if="Option::is_none")] @@ -126,8 +127,8 @@ macro_rules! impl_room_event { self.origin_server_ts } - fn room_id(&self) -> &::ruma_identifiers::RoomId { - &self.room_id + fn room_id(&self) -> Option<&::ruma_identifiers::RoomId> { + self.room_id.as_ref() } fn unsigned(&self) -> Option<&::serde_json::Value> { @@ -172,7 +173,8 @@ macro_rules! state_event { pub prev_content: Option<$content_type>, /// The unique identifier for the room associated with this event. - pub room_id: ::ruma_identifiers::RoomId, + #[serde(skip_serializing_if="Option::is_none")] + pub room_id: Option<::ruma_identifiers::RoomId>, /// A key that determines which piece of room state the event represents. pub state_key: String, diff --git a/src/receipt.rs b/src/receipt.rs index a534bf36..06902af5 100644 --- a/src/receipt.rs +++ b/src/receipt.rs @@ -8,7 +8,11 @@ event! { /// Informs the client of new receipts. pub struct ReceiptEvent(ReceiptEventContent) { /// The unique identifier for the room associated with this event. - pub room_id: RoomId + /// + /// This can be `None` if the event came from a context where there is + /// no ambiguity which room it belongs to, like a `/sync` response for example. + #[serde(skip_serializing_if="Option::is_none")] + pub room_id: Option } } diff --git a/src/room/pinned_events.rs b/src/room/pinned_events.rs index c4d00371..3059e92f 100644 --- a/src/room/pinned_events.rs +++ b/src/room/pinned_events.rs @@ -38,7 +38,7 @@ mod tests { event_type: EventType::RoomPinnedEvents, origin_server_ts: 1432804485886, prev_content: None, - room_id: RoomId::new("example.com").unwrap(), + room_id: Some(RoomId::new("example.com").unwrap()), sender: UserId::new("example.com").unwrap(), state_key: "".to_string(), unsigned: None, diff --git a/src/typing.rs b/src/typing.rs index 6ba07abd..4e66782f 100644 --- a/src/typing.rs +++ b/src/typing.rs @@ -6,7 +6,11 @@ event! { /// Informs the client of the list of users currently typing. pub struct TypingEvent(TypingEventContent) { /// The unique identifier for the room associated with this event. - pub room_id: RoomId + /// + /// This can be `None` if the event came from a context where there is + /// no ambiguity which room it belongs to, like a `/sync` response for example. + #[serde(skip_serializing_if="Option::is_none")] + pub room_id: Option } }