From 0952205e1e6b7cfeaa7dc6e2512ba5fd76b3b557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 1 Jan 2021 14:56:06 +0100 Subject: [PATCH] base: Restore rooms and the sync token when we restore the login. --- matrix_sdk_base/src/client.rs | 12 +----------- matrix_sdk_base/src/store.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index bbd086ac..de3862cc 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -352,17 +352,7 @@ impl BaseClient { /// * `session` - An session that the user already has from a /// previous login call. pub async fn restore_login(&self, session: Session) -> Result<()> { - // If there wasn't a state store opened, try to open the default one if - // a store path was provided. - // if self.state_store.read().await.is_none() { - // #[cfg(not(target_arch = "wasm32"))] - // if let Some(path) = &*self.store_path { - // let store = JsonStore::open(path)?; - // *self.state_store.write().await = Some(Box::new(store)); - // } - // } - - // self.sync_with_state_store(&session).await?; + self.store.restore_session(session.clone()).await; #[cfg(feature = "encryption")] { diff --git a/matrix_sdk_base/src/store.rs b/matrix_sdk_base/src/store.rs index 85c4b386..25b6f160 100644 --- a/matrix_sdk_base/src/store.rs +++ b/matrix_sdk_base/src/store.rs @@ -3,7 +3,7 @@ use std::{ }; use dashmap::DashMap; -use futures::stream::{self, Stream}; +use futures::stream::{self, Stream, StreamExt}; use matrix_sdk_common::{ events::{ presence::PresenceEvent, @@ -47,6 +47,21 @@ impl Store { } } + pub(crate) async fn restore_session(&self, session: Session) { + let mut infos = self.inner.get_room_infos().await; + + // TODO restore stripped rooms. + while let Some(info) = infos.next().await { + let room = Room::restore(&session.user_id, self.inner.clone(), info); + self.rooms.insert(room.room_id().to_owned(), room); + } + + let token = self.get_sync_token().await; + + *self.sync_token.write().await = token; + *self.session.write().await = Some(session); + } + pub fn open_default(path: impl AsRef) -> Self { let inner = SledStore::open_with_path(path); Self::new(