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,
});
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.

View File

@ -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<SyncResponse> {
// 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) {

View File

@ -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<KeyAlgorithm, UInt>,
/// 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<DeviceKeyAlgorithm, u64>,
}
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<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)]
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<String>,
/// 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<AnySyncStateEvent>,
}
impl State {
fn is_empty(&self) -> bool {
self.events.is_empty()
}
}