Merge branch 'state-reviews' into messages

This commit is contained in:
Devin R 2020-04-29 15:49:36 -04:00
commit 178c6c06f8
4 changed files with 9 additions and 43 deletions

View file

@ -48,7 +48,7 @@ use crate::api;
use crate::base_client::Client as BaseClient;
use crate::models::Room;
use crate::session::Session;
use crate::state::{ClientState, StateStore};
use crate::state::StateStore;
use crate::VERSION;
use crate::{Error, EventEmitter, Result};
@ -674,7 +674,6 @@ impl AsyncClient {
for mut event in &mut room.timeline.events {
let decrypted_event = {
let mut client = self.base_client.write().await;
let mut timeline_update = false;
let (decrypt_ev, timeline_update) = client
.receive_joined_timeline_event(room_id, &mut event)
.await;
@ -748,12 +747,6 @@ impl AsyncClient {
let mut client = self.base_client.write().await;
client.receive_sync_response(&mut response, updated).await?;
if updated {
if let Some(store) = client.state_store.as_ref() {
let state = ClientState::from_base_client(&client);
store.store_client_state(state).await?;
}
}
Ok(response)
}

View file

@ -148,27 +148,16 @@ impl Client {
/// When a client is provided the state store will load state from the `StateStore`.
///
/// Returns `true` when a sync has successfully completed.
/// Returns `true` when a state store sync has successfully completed.
pub(crate) async fn sync_with_state_store(&mut self) -> Result<bool> {
if let Some(store) = self.state_store.as_ref() {
if let Some(sess) = self.session.as_ref() {
if let Some(client_state) = store.load_client_state(sess).await? {
let ClientState {
user_id,
device_id,
sync_token,
ignored_users,
push_ruleset,
} = client_state;
if let Some(sess) = self.session.as_mut() {
if let Some(device) = device_id {
sess.device_id = device;
}
if let Some(user) = user_id {
sess.user_id = user;
}
}
self.sync_token = sync_token;
self.ignored_users = ignored_users;
self.push_ruleset = push_ruleset;

View file

@ -22,7 +22,7 @@ use serde::{Deserialize, Serialize};
use crate::base_client::{Client as BaseClient, Token};
use crate::events::push_rules::Ruleset;
use crate::identifiers::{DeviceId, RoomId, UserId};
use crate::identifiers::{RoomId, UserId};
use crate::{Result, Room, Session};
/// `ClientState` holds all the information to restore a `BaseClient`
@ -33,10 +33,6 @@ use crate::{Result, Room, Session};
/// when needed in `StateStore::load/store_client_state`
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ClientState {
/// The `UserId` for the current logged in user.
pub user_id: Option<UserId>,
/// The `DeviceId` of the current logged in user.
pub device_id: Option<DeviceId>,
/// The current sync token that should be used for the next sync call.
pub sync_token: Option<Token>,
/// A list of ignored users.
@ -48,15 +44,12 @@ pub struct ClientState {
impl ClientState {
pub fn from_base_client(client: &BaseClient) -> ClientState {
let BaseClient {
session,
sync_token,
ignored_users,
push_ruleset,
..
} = client;
Self {
user_id: session.as_ref().map(|s| s.user_id.clone()),
device_id: session.as_ref().map(|s| s.device_id.clone()),
sync_token: sync_token.clone(),
ignored_users: ignored_users.clone(),
push_ruleset: push_ruleset.clone(),
@ -97,14 +90,12 @@ mod test {
let room = Room::new(&id, &user);
let state = ClientState {
user_id: Some(user.clone()),
device_id: None,
sync_token: Some("hello".into()),
ignored_users: vec![user],
push_ruleset: None,
};
assert_eq!(
r#"{"user_id":"@example:example.com","device_id":null,"sync_token":"hello","ignored_users":["@example:example.com"],"push_ruleset":null}"#,
r#"{"sync_token":"hello","ignored_users":["@example:example.com"],"push_ruleset":null}"#,
serde_json::to_string(&state).unwrap()
);
@ -146,8 +137,6 @@ mod test {
let room = Room::new(&id, &user);
let state = ClientState {
user_id: Some(user.clone()),
device_id: None,
sync_token: Some("hello".into()),
ignored_users: vec![user],
push_ruleset: None,

View file

@ -84,12 +84,6 @@ impl StateStore for JsonStore {
}
async fn store_client_state(&self, state: ClientState) -> Result<()> {
if !self.user_path_set.load(Ordering::SeqCst) {
if let Some(user) = &state.user_id {
self.user_path_set.swap(true, Ordering::SeqCst);
self.path.write().await.push(user.localpart())
}
}
let mut path = self.path.read().await.clone();
path.push("client.json");
@ -193,18 +187,19 @@ mod test {
device_id: "Tester".to_string(),
};
let store = JsonStore::open(path).unwrap();
let state = ClientState {
user_id: Some(user.clone()),
device_id: None,
sync_token: Some("hello".into()),
ignored_users: vec![user],
push_ruleset: None,
};
let mut path_with_user = PathBuf::from(path);
path_with_user.push(sess.user_id.localpart());
// we have to set the path since `JsonStore::store_client_state()` doesn't append to the path
let store = JsonStore::open(path_with_user).unwrap();
store.store_client_state(state.clone()).await.unwrap();
// the newly loaded store sets it own user_id local part when `load_client_state`
let store = JsonStore::open(path).unwrap();
let loaded = store.load_client_state(&sess).await.unwrap();
assert_eq!(loaded, Some(state));