From f279da51ffd5c9eb7677088ff2d4d904f0f3747b Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Wed, 3 Apr 2024 21:46:04 +0800 Subject: [PATCH] chore: migrate private section --- .../rust-lib/flowy-user/src/migrations/mod.rs | 1 + .../src/migrations/workspace_private_v1.rs | 60 +++++++++++++++++++ .../flowy-user/src/user_manager/manager.rs | 2 + 3 files changed, 63 insertions(+) create mode 100644 frontend/rust-lib/flowy-user/src/migrations/workspace_private_v1.rs diff --git a/frontend/rust-lib/flowy-user/src/migrations/mod.rs b/frontend/rust-lib/flowy-user/src/migrations/mod.rs index d5f83d47c9d1..63c7776d44b9 100644 --- a/frontend/rust-lib/flowy-user/src/migrations/mod.rs +++ b/frontend/rust-lib/flowy-user/src/migrations/mod.rs @@ -5,6 +5,7 @@ pub mod migration; pub mod session_migration; mod util; pub mod workspace_and_favorite_v1; +pub mod workspace_private_v1; pub mod workspace_trash_v1; #[derive(Clone, Debug)] diff --git a/frontend/rust-lib/flowy-user/src/migrations/workspace_private_v1.rs b/frontend/rust-lib/flowy-user/src/migrations/workspace_private_v1.rs new file mode 100644 index 000000000000..3a2a5aaca2cf --- /dev/null +++ b/frontend/rust-lib/flowy-user/src/migrations/workspace_private_v1.rs @@ -0,0 +1,60 @@ +use std::sync::Arc; + +use collab_folder::Folder; +use collab_plugins::local_storage::kv::{KVTransactionDB, PersistenceError}; +use tracing::instrument; + +use collab_integrate::{CollabKVAction, CollabKVDB}; +use flowy_error::FlowyResult; +use flowy_user_pub::entities::Authenticator; + +use crate::migrations::migration::UserDataMigration; +use crate::migrations::util::load_collab; +use flowy_user_pub::session::Session; + +/// Migrate the workspace: Add all the view_ids in the view_map into the private section +pub struct WorkspacePrivateSectionMigration; + +impl UserDataMigration for WorkspacePrivateSectionMigration { + fn name(&self) -> &str { + "workspace_private_section_migration" + } + + #[instrument(name = "WorkspacePrivateSectionMigration", skip_all, err)] + fn run( + &self, + session: &Session, + collab_db: &Arc, + _authenticator: &Authenticator, + ) -> FlowyResult<()> { + collab_db.with_write_txn(|write_txn| { + if let Ok(collab) = load_collab(session.user_id, write_txn, &session.user_workspace.id) { + let folder = Folder::open(session.user_id, collab, None) + .map_err(|err| PersistenceError::Internal(err.into()))?; + + let view_ids = folder + .get_workspace_views() + .into_iter() + .map(|view| view.id.clone()) + .collect::>(); + + if !view_ids.is_empty() { + folder.add_private_view_ids(view_ids); + } + + let encode = folder + .encode_collab_v1() + .map_err(|err| PersistenceError::Internal(err.into()))?; + write_txn.flush_doc_with( + session.user_id, + &session.user_workspace.id, + &encode.doc_state, + &encode.state_vector, + )?; + } + Ok(()) + })?; + + Ok(()) + } +} diff --git a/frontend/rust-lib/flowy-user/src/user_manager/manager.rs b/frontend/rust-lib/flowy-user/src/user_manager/manager.rs index fdb5dc57c500..c7c89566c9f9 100644 --- a/frontend/rust-lib/flowy-user/src/user_manager/manager.rs +++ b/frontend/rust-lib/flowy-user/src/user_manager/manager.rs @@ -28,6 +28,7 @@ use crate::event_map::{DefaultUserStatusCallback, UserStatusCallback}; use crate::migrations::document_empty_content::HistoricalEmptyDocumentMigration; use crate::migrations::migration::{UserDataMigration, UserLocalDataMigration}; use crate::migrations::workspace_and_favorite_v1::FavoriteV1AndWorkspaceArrayMigration; +use crate::migrations::workspace_private_v1::WorkspacePrivateSectionMigration; use crate::migrations::workspace_trash_v1::WorkspaceTrashMapToSectionMigration; use crate::migrations::AnonUser; use crate::services::authenticate_user::AuthenticateUser; @@ -838,6 +839,7 @@ pub(crate) fn run_collab_data_migration( Box::new(HistoricalEmptyDocumentMigration), Box::new(FavoriteV1AndWorkspaceArrayMigration), Box::new(WorkspaceTrashMapToSectionMigration), + Box::new(WorkspacePrivateSectionMigration), ]; match UserLocalDataMigration::new(session.clone(), collab_db, sqlite_pool) .run(migrations, &user.authenticator)