2023-12-21 14:13:21 +08:00
|
|
|
use std::sync::Arc;
|
2023-12-23 13:35:24 +08:00
|
|
|
|
|
|
|
|
use serde_json::{json, Value};
|
2023-12-21 14:13:21 +08:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
2023-12-23 13:35:24 +08:00
|
|
|
use flowy_sqlite::kv::StorePreferences;
|
|
|
|
|
|
|
|
|
|
use crate::manager::UserConfig;
|
|
|
|
|
use crate::services::entities::Session;
|
|
|
|
|
|
|
|
|
|
const MIGRATION_USER_NO_USER_UUID: &str = "migration_user_no_user_uuid";
|
|
|
|
|
|
2023-12-21 14:13:21 +08:00
|
|
|
pub fn migrate_session_with_user_uuid(
|
|
|
|
|
user_config: &UserConfig,
|
|
|
|
|
session: &Arc<parking_lot::RwLock<Option<Session>>>,
|
|
|
|
|
store_preferences: &Arc<StorePreferences>,
|
|
|
|
|
) {
|
2023-12-23 13:35:24 +08:00
|
|
|
if !store_preferences.get_bool(MIGRATION_USER_NO_USER_UUID) {
|
|
|
|
|
if store_preferences
|
|
|
|
|
.set_bool(MIGRATION_USER_NO_USER_UUID, true)
|
|
|
|
|
.is_ok()
|
|
|
|
|
{
|
|
|
|
|
if let Some(mut value) = store_preferences.get_object::<Value>(&user_config.session_cache_key)
|
|
|
|
|
{
|
|
|
|
|
if value.get("user_uuid").is_none() {
|
|
|
|
|
value.as_object_mut().map(|map| {
|
|
|
|
|
map.insert("user_uuid".to_string(), json!(Uuid::new_v4()));
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-12-21 14:13:21 +08:00
|
|
|
|
2023-12-23 13:35:24 +08:00
|
|
|
if let Ok(new_session) = serde_json::from_value::<Session>(value) {
|
|
|
|
|
*session.write() = Some(new_session.clone());
|
|
|
|
|
let _ = store_preferences.set_object(&user_config.session_cache_key, &new_session);
|
|
|
|
|
}
|
2023-12-21 14:13:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|