state_store: remove device_id and user_id from ClientState

master
Devin R 2020-04-29 15:42:20 -04:00
parent 955f6248cf
commit 376381b1a7
4 changed files with 9 additions and 42 deletions

View File

@ -48,7 +48,7 @@ use crate::api;
use crate::base_client::Client as BaseClient; use crate::base_client::Client as BaseClient;
use crate::models::Room; use crate::models::Room;
use crate::session::Session; use crate::session::Session;
use crate::state::{ClientState, StateStore}; use crate::state::StateStore;
use crate::VERSION; use crate::VERSION;
use crate::{Error, EventEmitter, Result}; use crate::{Error, EventEmitter, Result};
@ -747,12 +747,6 @@ impl AsyncClient {
let mut client = self.base_client.write().await; let mut client = self.base_client.write().await;
client.receive_sync_response(&mut response, updated).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) Ok(response)
} }

View File

@ -148,27 +148,16 @@ impl Client {
/// When a client is provided the state store will load state from the `StateStore`. /// 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> { pub(crate) async fn sync_with_state_store(&mut self) -> Result<bool> {
if let Some(store) = self.state_store.as_ref() { if let Some(store) = self.state_store.as_ref() {
if let Some(sess) = self.session.as_ref() { if let Some(sess) = self.session.as_ref() {
if let Some(client_state) = store.load_client_state(sess).await? { if let Some(client_state) = store.load_client_state(sess).await? {
let ClientState { let ClientState {
user_id,
device_id,
sync_token, sync_token,
ignored_users, ignored_users,
push_ruleset, push_ruleset,
} = client_state; } = 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.sync_token = sync_token;
self.ignored_users = ignored_users; self.ignored_users = ignored_users;
self.push_ruleset = push_ruleset; 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::base_client::{Client as BaseClient, Token};
use crate::events::push_rules::Ruleset; use crate::events::push_rules::Ruleset;
use crate::identifiers::{DeviceId, RoomId, UserId}; use crate::identifiers::{RoomId, UserId};
use crate::{Result, Room, Session}; use crate::{Result, Room, Session};
/// `ClientState` holds all the information to restore a `BaseClient` /// `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` /// when needed in `StateStore::load/store_client_state`
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ClientState { 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. /// The current sync token that should be used for the next sync call.
pub sync_token: Option<Token>, pub sync_token: Option<Token>,
/// A list of ignored users. /// A list of ignored users.
@ -48,15 +44,12 @@ pub struct ClientState {
impl ClientState { impl ClientState {
pub fn from_base_client(client: &BaseClient) -> ClientState { pub fn from_base_client(client: &BaseClient) -> ClientState {
let BaseClient { let BaseClient {
session,
sync_token, sync_token,
ignored_users, ignored_users,
push_ruleset, push_ruleset,
.. ..
} = client; } = client;
Self { 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(), sync_token: sync_token.clone(),
ignored_users: ignored_users.clone(), ignored_users: ignored_users.clone(),
push_ruleset: push_ruleset.clone(), push_ruleset: push_ruleset.clone(),
@ -97,14 +90,12 @@ mod test {
let room = Room::new(&id, &user); let room = Room::new(&id, &user);
let state = ClientState { let state = ClientState {
user_id: Some(user.clone()),
device_id: None,
sync_token: Some("hello".into()), sync_token: Some("hello".into()),
ignored_users: vec![user], ignored_users: vec![user],
push_ruleset: None, push_ruleset: None,
}; };
assert_eq!( 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() serde_json::to_string(&state).unwrap()
); );
@ -145,8 +136,6 @@ mod test {
let room = Room::new(&id, &user); let room = Room::new(&id, &user);
let state = ClientState { let state = ClientState {
user_id: Some(user.clone()),
device_id: None,
sync_token: Some("hello".into()), sync_token: Some("hello".into()),
ignored_users: vec![user], ignored_users: vec![user],
push_ruleset: None, push_ruleset: None,

View File

@ -84,12 +84,6 @@ impl StateStore for JsonStore {
} }
async fn store_client_state(&self, state: ClientState) -> Result<()> { 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(); let mut path = self.path.read().await.clone();
path.push("client.json"); path.push("client.json");
@ -193,18 +187,19 @@ mod test {
device_id: "Tester".to_string(), device_id: "Tester".to_string(),
}; };
let store = JsonStore::open(path).unwrap();
let state = ClientState { let state = ClientState {
user_id: Some(user.clone()),
device_id: None,
sync_token: Some("hello".into()), sync_token: Some("hello".into()),
ignored_users: vec![user], ignored_users: vec![user],
push_ruleset: None, 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(); 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 store = JsonStore::open(path).unwrap();
let loaded = store.load_client_state(&sess).await.unwrap(); let loaded = store.load_client_state(&sess).await.unwrap();
assert_eq!(loaded, Some(state)); assert_eq!(loaded, Some(state));