sync_events: Make all fields in the response optional except next_batch.

As per spec only the next_batch token is required in a sync response.

This patch makes all of them optional and creates default values when
parsing a response. Serialization is skipped for all of them if they are
empty containers.
This commit is contained in:
Damir Jelić 2020-05-05 14:26:26 +02:00
parent 5cedf1a963
commit 7d80a7f23d

View File

@ -68,12 +68,15 @@ ruma_api! {
pub next_batch: String, pub next_batch: String,
/// Updates to rooms. /// Updates to rooms.
#[serde(default, skip_serializing_if = "Rooms::is_empty")]
pub rooms: Rooms, pub rooms: Rooms,
/// Updates to the presence status of other users. /// Updates to the presence status of other users.
#[serde(default, skip_serializing_if = "Presence::is_empty")]
pub presence: Presence, pub presence: Presence,
/// The global private data created by this user. /// The global private data created by this user.
#[serde(default, skip_serializing_if = "AccountData::is_empty")]
pub account_data: AccountData, pub account_data: AccountData,
/// Messages sent dirrectly between devices. /// Messages sent dirrectly between devices.
@ -139,7 +142,7 @@ pub enum Filter {
} }
/// Updates to rooms. /// Updates to rooms.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Rooms { pub struct Rooms {
/// The rooms that the user has left or been banned from. /// The rooms that the user has left or been banned from.
pub leave: BTreeMap<RoomId, LeftRoom>, pub leave: BTreeMap<RoomId, LeftRoom>,
@ -151,6 +154,12 @@ pub struct Rooms {
pub invite: BTreeMap<RoomId, InvitedRoom>, pub invite: BTreeMap<RoomId, InvitedRoom>,
} }
impl Rooms {
fn is_empty(&self) -> bool {
self.leave.is_empty() && self.join.is_empty() && self.invite.is_empty()
}
}
/// Historical updates to left rooms. /// Historical updates to left rooms.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LeftRoom { pub struct LeftRoom {
@ -229,12 +238,18 @@ pub struct State {
} }
/// The private data that this user has attached to this room. /// The private data that this user has attached to this room.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AccountData { pub struct AccountData {
/// A list of events. /// A list of events.
pub events: Vec<EventJson<NonRoomEvent>>, pub events: Vec<EventJson<NonRoomEvent>>,
} }
impl AccountData {
fn is_empty(&self) -> bool {
self.events.is_empty()
}
}
/// Ephemeral events not recorded in the timeline or state of the room. /// Ephemeral events not recorded in the timeline or state of the room.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Ephemeral { pub struct Ephemeral {
@ -284,14 +299,20 @@ pub struct InviteState {
} }
/// Updates to the presence status of other users. /// Updates to the presence status of other users.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Presence { pub struct Presence {
/// A list of events. /// A list of events.
pub events: Vec<EventJson<PresenceEvent>>, pub events: Vec<EventJson<PresenceEvent>>,
} }
impl Presence {
fn is_empty(&self) -> bool {
self.events.is_empty()
}
}
/// Messages sent dirrectly between devices. /// Messages sent dirrectly between devices.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ToDevice { pub struct ToDevice {
/// A list of to-device events. /// A list of to-device events.
pub events: Vec<EventJson<AnyToDeviceEvent>>, pub events: Vec<EventJson<AnyToDeviceEvent>>,
@ -303,12 +324,6 @@ impl ToDevice {
} }
} }
impl Default for ToDevice {
fn default() -> Self {
Self { events: Vec::new() }
}
}
/// Information on E2E device udpates. /// Information on E2E device udpates.
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DeviceLists { pub struct DeviceLists {