diff --git a/src/r0/sync/sync_events.rs b/src/r0/sync/sync_events.rs index 33ceb155..c6de6a98 100644 --- a/src/r0/sync/sync_events.rs +++ b/src/r0/sync/sync_events.rs @@ -17,7 +17,10 @@ use ruma_events::{ use ruma_identifiers::RoomId; use serde::{Deserialize, Serialize}; -use crate::r0::{filter::FilterDefinition, keys::KeyAlgorithm}; +use crate::{ + r0::{filter::FilterDefinition, keys::KeyAlgorithm}, + serde::is_default, +}; ruma_api! { metadata { @@ -35,22 +38,22 @@ ruma_api! { #[ruma_api(query)] pub filter: Option, /// A point in time to continue a sync from. + /// /// Should be a token from the `next_batch` field of a previous `/sync` /// request. #[serde(skip_serializing_if = "Option::is_none")] #[ruma_api(query)] pub since: Option, /// Controls whether to include the full state for all rooms the user is a member of. - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "is_default")] #[ruma_api(query)] - pub full_state: Option, + pub full_state: bool, /// Controls whether the client is automatically marked as online by polling this API. - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(default, skip_serializing_if = "is_default")] #[ruma_api(query)] - pub set_presence: Option, + pub set_presence: SetPresence, /// The maximum time to poll in milliseconds before returning this request. - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(with = "crate::serde::duration::opt_ms")] + #[serde(with = "crate::serde::duration::opt_ms", skip_serializing_if = "Option::is_none")] #[ruma_api(query)] pub timeout: Option, } @@ -65,23 +68,25 @@ ruma_api! { #[wrap_incoming] pub presence: Presence, /// Messages sent dirrectly between devices. + #[serde(default, skip_serializing_if = "ToDevice::is_empty")] #[wrap_incoming] pub to_device: ToDevice, /// Information on E2E device updates. + /// /// Only present on an incremental sync. #[serde(skip_serializing_if = "Option::is_none")] pub device_lists: Option, /// For each key algorithm, the number of unclaimed one-time keys /// currently held on the server for a device. - #[serde(skip_serializing_if = "Option::is_none")] - pub device_one_time_keys_count: Option> + #[serde(skip_serializing_if = "HashMap::is_empty")] + pub device_one_time_keys_count: HashMap, } error: crate::Error } /// Whether to set presence or not during sync. -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)] #[serde(rename_all = "lowercase")] pub enum SetPresence { /// Do not set the presence of the user calling this API. @@ -92,6 +97,12 @@ pub enum SetPresence { Unavailable, } +impl Default for SetPresence { + fn default() -> Self { + Self::Online + } +} + /// A filter represented either as its full JSON definition or the ID of a saved filter. #[derive(Clone, Debug, Deserialize, Serialize)] #[allow(clippy::large_enum_variant)] @@ -294,15 +305,27 @@ pub struct ToDevice { pub events: Vec, } +impl ToDevice { + fn is_empty(&self) -> bool { + self.events.is_empty() + } +} + +impl Default for IncomingToDevice { + fn default() -> Self { + Self { events: Vec::new() } + } +} + /// Information on E2E device udpates. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct DeviceLists { /// List of users who have updated their device identity keys or who now /// share an encrypted room with the client since the previous sync - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default, skip_serializing_if = "Vec::is_empty")] pub changed: Vec, /// List of users who no longer share encrypted rooms since the previous sync /// response. - #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(default, skip_serializing_if = "Vec::is_empty")] pub left: Vec, } diff --git a/src/serde.rs b/src/serde.rs index 4cb6139c..6ca75b9b 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -1,3 +1,7 @@ //! Modules to hold functions for de-/serializing remote types pub mod duration; + +pub fn is_default(val: &T) -> bool { + val == &T::default() +}