diff --git a/frontend/rust-lib/event-integration-test/tests/user/migration_test/document_test.rs b/frontend/rust-lib/event-integration-test/tests/user/migration_test/document_test.rs index f4ba5ec831..f3b73d55d3 100644 --- a/frontend/rust-lib/event-integration-test/tests/user/migration_test/document_test.rs +++ b/frontend/rust-lib/event-integration-test/tests/user/migration_test/document_test.rs @@ -11,6 +11,7 @@ async fn migrate_historical_empty_document_test() { "historical_empty_document", ) .unwrap(); + let s = user_db_path.to_str().unwrap().to_string(); let test = EventIntegrationTest::new_with_user_data_path(user_db_path, DEFAULT_NAME.to_string()).await; diff --git a/frontend/rust-lib/flowy-user/src/migrations/session_migration.rs b/frontend/rust-lib/flowy-user/src/migrations/session_migration.rs index df477f4a33..847ab8a516 100644 --- a/frontend/rust-lib/flowy-user/src/migrations/session_migration.rs +++ b/frontend/rust-lib/flowy-user/src/migrations/session_migration.rs @@ -1,4 +1,6 @@ +use crate::user_manager::manager_history_user::ANON_USER; use flowy_sqlite::kv::KVStorePreferences; +use flowy_user_pub::entities::AuthType; use flowy_user_pub::session::Session; use serde_json::{json, Value}; use std::sync::Arc; @@ -6,7 +8,7 @@ use uuid::Uuid; const MIGRATION_USER_NO_USER_UUID: &str = "migration_user_no_user_uuid"; -pub fn migrate_session_with_user_uuid( +pub fn migrate_session( session_cache_key: &str, store_preferences: &Arc, ) -> Option { @@ -24,10 +26,20 @@ pub fn migrate_session_with_user_uuid( if let Ok(new_session) = serde_json::from_value::(value) { let _ = store_preferences.set_object(session_cache_key, &new_session); - return Some(new_session); } } } - None + if let Some(mut session) = store_preferences.get_object::(session_cache_key) { + if let Some(anon_session) = store_preferences.get_object::(ANON_USER) { + if session.user_id == anon_session.user_id + && session.user_workspace.workspace_type != AuthType::Local + { + session.user_workspace.workspace_type = AuthType::Local; + let _ = store_preferences.set_object(session_cache_key, &session); + } + } + } + + store_preferences.get_object::(session_cache_key) } diff --git a/frontend/rust-lib/flowy-user/src/services/authenticate_user.rs b/frontend/rust-lib/flowy-user/src/services/authenticate_user.rs index d21ca20283..b725a082f5 100644 --- a/frontend/rust-lib/flowy-user/src/services/authenticate_user.rs +++ b/frontend/rust-lib/flowy-user/src/services/authenticate_user.rs @@ -1,9 +1,8 @@ -use crate::migrations::session_migration::migrate_session_with_user_uuid; +use crate::migrations::session_migration::migrate_session; use crate::services::db::UserDB; use crate::services::entities::{UserConfig, UserPaths}; use collab_integrate::CollabKVDB; -use crate::user_manager::manager_history_user::ANON_USER; use arc_swap::ArcSwapOption; use collab_plugins::local_storage::kv::doc::CollabKVAction; use collab_plugins::local_storage::kv::KVTransactionDB; @@ -30,9 +29,7 @@ impl AuthenticateUser { pub fn new(user_config: UserConfig, store_preferences: Arc) -> Self { let user_paths = UserPaths::new(user_config.storage_path.clone()); let database = Arc::new(UserDB::new(user_paths.clone())); - let session = - migrate_session_with_user_uuid(&user_config.session_cache_key, &store_preferences) - .map(Arc::new); + let session = migrate_session(&user_config.session_cache_key, &store_preferences).map(Arc::new); Self { user_config, database, @@ -150,17 +147,10 @@ impl AuthenticateUser { .get_object::(&self.user_config.session_cache_key) { None => Err(FlowyError::new( - ErrorCode::UserUnauthorized, + ErrorCode::RecordNotFound, "Can't find user session. Please login again", )), - Some(mut session) => { - // Set the workspace type to local if the user is anon. - if let Some(anon_session) = self.store_preferences.get_object::(ANON_USER) { - if session.user_id == anon_session.user_id { - session.user_workspace.workspace_type = AuthType::Local; - } - } - + Some(session) => { let session = Arc::new(session); self.session.store(Some(session.clone())); Ok(session) diff --git a/frontend/rust-lib/flowy-user/src/services/data_import/appflowy_data_import.rs b/frontend/rust-lib/flowy-user/src/services/data_import/appflowy_data_import.rs index 90113b8062..c79d80bbf1 100644 --- a/frontend/rust-lib/flowy-user/src/services/data_import/appflowy_data_import.rs +++ b/frontend/rust-lib/flowy-user/src/services/data_import/appflowy_data_import.rs @@ -1,4 +1,4 @@ -use crate::migrations::session_migration::migrate_session_with_user_uuid; +use crate::migrations::session_migration::migrate_session; use crate::services::data_import::importer::load_collab_by_object_ids; use crate::services::db::UserDBPath; @@ -80,7 +80,7 @@ pub(crate) fn prepare_import( } let user_paths = UserPaths::new(path.to_string()); let other_store_preferences = Arc::new(KVStorePreferences::new(path)?); - migrate_session_with_user_uuid("appflowy_session_cache", &other_store_preferences); + migrate_session("appflowy_session_cache", &other_store_preferences); let imported_session = other_store_preferences .get_object::("appflowy_session_cache") .ok_or(anyhow!(