From 7dd834a214e664b928f31b48183bf5f1ae6a3759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 30 Nov 2020 15:24:49 +0100 Subject: [PATCH] base: Add some more sync response fields. --- matrix_sdk/src/client.rs | 7 ++-- matrix_sdk_base/src/client.rs | 10 ++---- matrix_sdk_base/src/responses.rs | 60 ++++++++++++++------------------ 3 files changed, 32 insertions(+), 45 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 841ea29c..fc869882 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -1500,12 +1500,9 @@ impl Client { timeout: sync_settings.timeout, }); - let mut response = self.send(request).await?; + let response = self.send(request).await?; - Ok(self - .base_client - .receive_sync_response(&mut response) - .await?) + Ok(self.base_client.receive_sync_response(response).await?) } /// Repeatedly call sync to synchronize the client state with the server. diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index fc7807a0..82f26853 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -463,7 +463,7 @@ impl BaseClient { /// * `response` - The response that we received after a successful sync. pub async fn receive_sync_response( &self, - response: &mut api::sync::sync_events::Response, + mut response: api::sync::sync_events::Response, ) -> Result { // The server might respond multiple times with the same sync token, in // that case we already received this response and there's nothing to @@ -481,7 +481,7 @@ impl BaseClient { // decryptes to-device events, but leaves room events alone. // This makes sure that we have the deryption keys for the room // events at hand. - o.receive_sync_response(response).await?; + o.receive_sync_response(&mut response).await?; } } @@ -589,11 +589,7 @@ impl BaseClient { *self.sync_token.write().await = Some(response.next_batch.clone()); self.apply_changes(&changes).await; - Ok(SyncResponse::new( - response.next_batch.clone(), - rooms, - changes, - )) + Ok(SyncResponse::new(response, rooms, changes)) } async fn apply_changes(&self, changes: &StateChanges) { diff --git a/matrix_sdk_base/src/responses.rs b/matrix_sdk_base/src/responses.rs index 58fd6607..c7d72c62 100644 --- a/matrix_sdk_base/src/responses.rs +++ b/matrix_sdk_base/src/responses.rs @@ -2,8 +2,9 @@ use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; use matrix_sdk_common::{ - events::{presence::PresenceEvent, AnySyncRoomEvent, AnySyncStateEvent}, - identifiers::RoomId, + api::r0::sync::sync_events::{self, DeviceLists}, + events::{presence::PresenceEvent, AnySyncRoomEvent, AnySyncStateEvent, AnyToDeviceEvent}, + identifiers::{DeviceKeyAlgorithm, RoomId}, }; use crate::store::StateChanges; @@ -19,31 +20,31 @@ pub struct SyncResponse { ///// The global private data created by this user. //#[serde(default, skip_serializing_if = "AccountData::is_empty")] //pub account_data: AccountData, - - ///// Messages sent dirrectly between devices. - //#[serde(default, skip_serializing_if = "ToDevice::is_empty")] - //pub to_device: ToDevice, - - ///// Information on E2E device updates. - ///// - ///// Only present on an incremental sync. - //#[serde(default, skip_serializing_if = "DeviceLists::is_empty")] - //pub device_lists: DeviceLists, - - ///// For each key algorithm, the number of unclaimed one-time keys - ///// currently held on the server for a device. - //#[serde(default, skip_serializing_if = "BTreeMap::is_empty")] - //pub device_one_time_keys_count: BTreeMap, + /// Messages sent dirrectly between devices. + pub to_device: ToDevice, + /// Information on E2E device updates. + /// + /// Only present on an incremental sync. + pub device_lists: DeviceLists, + /// For each key algorithm, the number of unclaimed one-time keys + /// currently held on the server for a device. + pub device_one_time_keys_count: BTreeMap, } impl SyncResponse { - pub fn new(next_batch: String, rooms: Rooms, changes: StateChanges) -> Self { + pub fn new(response: sync_events::Response, rooms: Rooms, changes: StateChanges) -> Self { Self { - next_batch, + next_batch: response.next_batch, rooms, presence: Presence { events: changes.presence.into_iter().map(|(_, v)| v).collect(), }, + device_lists: response.device_lists, + device_one_time_keys_count: response + .device_one_time_keys_count + .into_iter() + .map(|(k, v)| (k, v.into())) + .collect(), ..Default::default() } } @@ -63,6 +64,13 @@ pub struct Presence { pub events: Vec, } +/// Messages sent dirrectly between devices. +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +pub struct ToDevice { + /// A list of events. + pub events: Vec, +} + #[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct Rooms { // /// The rooms that the user has left or been banned from. @@ -88,13 +96,11 @@ pub struct JoinedRoom { // #[serde(default, skip_serializing_if = "UnreadNotificationsCount::is_empty")] // pub unread_notifications: UnreadNotificationsCount, /// The timeline of messages and state changes in the room. - #[serde(default, skip_serializing_if = "Timeline::is_empty")] pub timeline: Timeline, /// Updates to the state, between the time indicated by the `since` parameter, and the start /// of the `timeline` (or all state up to the start of the `timeline`, if `since` is not /// given, or `full_state` is true). - #[serde(default, skip_serializing_if = "State::is_empty")] pub state: State, // /// The private data that this user has attached to this room. // #[serde(default, skip_serializing_if = "AccountData::is_empty")] @@ -116,12 +122,10 @@ impl JoinedRoom { #[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct Timeline { /// True if the number of events returned was limited by the `limit` on the filter. - #[serde(default)] pub limited: bool, /// A token that can be supplied to to the `from` parameter of the /// `/rooms/{roomId}/messages` endpoint. - #[serde(skip_serializing_if = "Option::is_none")] pub prev_batch: Option, /// A list of events. @@ -136,10 +140,6 @@ impl Timeline { ..Default::default() } } - - fn is_empty(&self) -> bool { - self.events.is_empty() - } } /// State events in the room. @@ -148,9 +148,3 @@ pub struct State { /// A list of state events. pub events: Vec, } - -impl State { - fn is_empty(&self) -> bool { - self.events.is_empty() - } -}