base: Add some more sync response fields.

master
Damir Jelić 2020-11-30 15:24:49 +01:00
parent b4d0179c18
commit 7dd834a214
3 changed files with 32 additions and 45 deletions

View File

@ -1500,12 +1500,9 @@ impl Client {
timeout: sync_settings.timeout, timeout: sync_settings.timeout,
}); });
let mut response = self.send(request).await?; let response = self.send(request).await?;
Ok(self Ok(self.base_client.receive_sync_response(response).await?)
.base_client
.receive_sync_response(&mut response)
.await?)
} }
/// Repeatedly call sync to synchronize the client state with the server. /// Repeatedly call sync to synchronize the client state with the server.

View File

@ -463,7 +463,7 @@ impl BaseClient {
/// * `response` - The response that we received after a successful sync. /// * `response` - The response that we received after a successful sync.
pub async fn receive_sync_response( pub async fn receive_sync_response(
&self, &self,
response: &mut api::sync::sync_events::Response, mut response: api::sync::sync_events::Response,
) -> Result<SyncResponse> { ) -> Result<SyncResponse> {
// The server might respond multiple times with the same sync token, in // The server might respond multiple times with the same sync token, in
// that case we already received this response and there's nothing to // 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. // decryptes to-device events, but leaves room events alone.
// This makes sure that we have the deryption keys for the room // This makes sure that we have the deryption keys for the room
// events at hand. // 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.sync_token.write().await = Some(response.next_batch.clone());
self.apply_changes(&changes).await; self.apply_changes(&changes).await;
Ok(SyncResponse::new( Ok(SyncResponse::new(response, rooms, changes))
response.next_batch.clone(),
rooms,
changes,
))
} }
async fn apply_changes(&self, changes: &StateChanges) { async fn apply_changes(&self, changes: &StateChanges) {

View File

@ -2,8 +2,9 @@ use serde::{Deserialize, Serialize};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use matrix_sdk_common::{ use matrix_sdk_common::{
events::{presence::PresenceEvent, AnySyncRoomEvent, AnySyncStateEvent}, api::r0::sync::sync_events::{self, DeviceLists},
identifiers::RoomId, events::{presence::PresenceEvent, AnySyncRoomEvent, AnySyncStateEvent, AnyToDeviceEvent},
identifiers::{DeviceKeyAlgorithm, RoomId},
}; };
use crate::store::StateChanges; use crate::store::StateChanges;
@ -19,31 +20,31 @@ pub struct SyncResponse {
///// The global private data created by this user. ///// The global private data created by this user.
//#[serde(default, skip_serializing_if = "AccountData::is_empty")] //#[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. pub to_device: ToDevice,
//#[serde(default, skip_serializing_if = "ToDevice::is_empty")] /// Information on E2E device updates.
//pub to_device: ToDevice, ///
/// Only present on an incremental sync.
///// Information on E2E device updates. pub device_lists: DeviceLists,
///// /// For each key algorithm, the number of unclaimed one-time keys
///// Only present on an incremental sync. /// currently held on the server for a device.
//#[serde(default, skip_serializing_if = "DeviceLists::is_empty")] pub device_one_time_keys_count: BTreeMap<DeviceKeyAlgorithm, u64>,
//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<KeyAlgorithm, UInt>,
} }
impl SyncResponse { 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 { Self {
next_batch, next_batch: response.next_batch,
rooms, rooms,
presence: Presence { presence: Presence {
events: changes.presence.into_iter().map(|(_, v)| v).collect(), 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() ..Default::default()
} }
} }
@ -63,6 +64,13 @@ pub struct Presence {
pub events: Vec<PresenceEvent>, pub events: Vec<PresenceEvent>,
} }
/// Messages sent dirrectly between devices.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ToDevice {
/// A list of events.
pub events: Vec<AnyToDeviceEvent>,
}
#[derive(Clone, Debug, Default, 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.
@ -88,13 +96,11 @@ pub struct JoinedRoom {
// #[serde(default, skip_serializing_if = "UnreadNotificationsCount::is_empty")] // #[serde(default, skip_serializing_if = "UnreadNotificationsCount::is_empty")]
// pub unread_notifications: UnreadNotificationsCount, // pub unread_notifications: UnreadNotificationsCount,
/// The timeline of messages and state changes in the room. /// The timeline of messages and state changes in the room.
#[serde(default, skip_serializing_if = "Timeline::is_empty")]
pub timeline: Timeline, pub timeline: Timeline,
/// Updates to the state, between the time indicated by the `since` parameter, and the start /// 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 /// of the `timeline` (or all state up to the start of the `timeline`, if `since` is not
/// given, or `full_state` is true). /// given, or `full_state` is true).
#[serde(default, skip_serializing_if = "State::is_empty")]
pub state: State, pub state: State,
// /// The private data that this user has attached to this room. // /// The private data that this user has attached to this room.
// #[serde(default, skip_serializing_if = "AccountData::is_empty")] // #[serde(default, skip_serializing_if = "AccountData::is_empty")]
@ -116,12 +122,10 @@ impl JoinedRoom {
#[derive(Clone, Debug, Default, Deserialize, Serialize)] #[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Timeline { pub struct Timeline {
/// True if the number of events returned was limited by the `limit` on the filter. /// True if the number of events returned was limited by the `limit` on the filter.
#[serde(default)]
pub limited: bool, pub limited: bool,
/// A token that can be supplied to to the `from` parameter of the /// A token that can be supplied to to the `from` parameter of the
/// `/rooms/{roomId}/messages` endpoint. /// `/rooms/{roomId}/messages` endpoint.
#[serde(skip_serializing_if = "Option::is_none")]
pub prev_batch: Option<String>, pub prev_batch: Option<String>,
/// A list of events. /// A list of events.
@ -136,10 +140,6 @@ impl Timeline {
..Default::default() ..Default::default()
} }
} }
fn is_empty(&self) -> bool {
self.events.is_empty()
}
} }
/// State events in the room. /// State events in the room.
@ -148,9 +148,3 @@ pub struct State {
/// A list of state events. /// A list of state events.
pub events: Vec<AnySyncStateEvent>, pub events: Vec<AnySyncStateEvent>,
} }
impl State {
fn is_empty(&self) -> bool {
self.events.is_empty()
}
}