AppFlowy/frontend/rust-lib/flowy-folder/src/manager.rs

294 lines
10 KiB
Rust
Raw Normal View History

2022-10-22 13:57:44 +00:00
use crate::entities::view::ViewDataFormatPB;
use crate::entities::{ViewLayoutTypePB, ViewPB};
use crate::services::folder_editor::FolderRevisionCompress;
2021-12-06 07:49:21 +00:00
use crate::{
dart_notification::{send_dart_notification, FolderNotification},
2022-07-19 06:11:29 +00:00
entities::workspace::RepeatedWorkspacePB,
2022-01-18 14:56:57 +00:00
errors::FlowyResult,
2022-01-30 02:33:21 +00:00
event_map::{FolderCouldServiceV1, WorkspaceDatabase, WorkspaceUser},
services::{
2022-05-26 09:28:44 +00:00
folder_editor::FolderEditor, persistence::FolderPersistence, set_current_workspace, AppController,
2022-01-24 09:35:58 +00:00
TrashController, ViewController, WorkspaceController,
},
2021-12-06 07:49:21 +00:00
};
2022-03-05 14:30:42 +00:00
use bytes::Bytes;
2022-11-01 10:59:53 +00:00
use flowy_document::editor::initial_read_me;
2022-03-06 01:03:02 +00:00
use flowy_error::FlowyError;
2022-11-06 01:59:53 +00:00
use flowy_revision::{
RevisionManager, RevisionPersistence, RevisionPersistenceConfiguration, RevisionWebSocket,
SQLiteRevisionSnapshotPersistence,
};
use folder_rev_model::user_default;
2022-03-05 14:30:42 +00:00
use lazy_static::lazy_static;
2022-03-06 01:03:02 +00:00
use lib_infra::future::FutureResult;
2022-10-22 13:57:44 +00:00
2022-11-01 10:59:53 +00:00
use crate::services::persistence::rev_sqlite::SQLiteFolderRevisionPersistence;
use flowy_http_model::ws_data::ServerRevisionWSData;
use flowy_sync::client_folder::FolderPad;
2022-03-05 14:30:42 +00:00
use std::{collections::HashMap, convert::TryInto, fmt::Formatter, sync::Arc};
use tokio::sync::RwLock as TokioRwLock;
2021-12-06 07:49:21 +00:00
lazy_static! {
2022-01-23 14:33:47 +00:00
static ref INIT_FOLDER_FLAG: TokioRwLock<HashMap<String, bool>> = TokioRwLock::new(HashMap::new());
2021-12-06 07:49:21 +00:00
}
2022-01-23 14:33:47 +00:00
const FOLDER_ID: &str = "folder";
2022-01-22 10:48:43 +00:00
const FOLDER_ID_SPLIT: &str = ":";
#[derive(Clone)]
pub struct FolderId(String);
impl FolderId {
2022-01-24 09:35:58 +00:00
pub fn new(user_id: &str) -> Self {
Self(format!("{}{}{}", user_id, FOLDER_ID_SPLIT, FOLDER_ID))
}
2022-01-22 10:48:43 +00:00
}
impl std::fmt::Display for FolderId {
2022-01-24 09:35:58 +00:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(FOLDER_ID)
}
2022-01-22 10:48:43 +00:00
}
impl std::fmt::Debug for FolderId {
2022-01-24 09:35:58 +00:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(FOLDER_ID)
}
2022-01-22 10:48:43 +00:00
}
impl AsRef<str> for FolderId {
2022-01-24 09:35:58 +00:00
fn as_ref(&self) -> &str {
&self.0
}
2022-01-22 10:48:43 +00:00
}
2022-01-14 12:52:03 +00:00
pub struct FolderManager {
2021-12-06 07:49:21 +00:00
pub user: Arc<dyn WorkspaceUser>,
2022-01-17 03:55:36 +00:00
pub(crate) cloud_service: Arc<dyn FolderCouldServiceV1>,
pub(crate) persistence: Arc<FolderPersistence>,
2022-01-20 15:51:11 +00:00
pub(crate) workspace_controller: Arc<WorkspaceController>,
2021-12-06 07:49:21 +00:00
pub(crate) app_controller: Arc<AppController>,
pub(crate) view_controller: Arc<ViewController>,
pub(crate) trash_controller: Arc<TrashController>,
2022-01-20 15:51:11 +00:00
web_socket: Arc<dyn RevisionWebSocket>,
2022-05-26 09:28:44 +00:00
folder_editor: Arc<TokioRwLock<Option<Arc<FolderEditor>>>>,
2021-12-06 07:49:21 +00:00
}
2022-01-14 12:52:03 +00:00
impl FolderManager {
2022-01-23 14:33:47 +00:00
pub async fn new(
2021-12-06 07:49:21 +00:00
user: Arc<dyn WorkspaceUser>,
2022-01-17 03:55:36 +00:00
cloud_service: Arc<dyn FolderCouldServiceV1>,
2022-01-20 15:51:11 +00:00
database: Arc<dyn WorkspaceDatabase>,
2022-03-06 01:03:02 +00:00
data_processors: ViewDataProcessorMap,
2022-01-20 15:51:11 +00:00
web_socket: Arc<dyn RevisionWebSocket>,
2021-12-06 07:49:21 +00:00
) -> Self {
if let Ok(user_id) = user.user_id() {
// Reset the flag if the folder manager gets initialized, otherwise,
// the folder_editor will not be initialized after flutter hot reload.
INIT_FOLDER_FLAG.write().await.insert(user_id.to_owned(), false);
}
2022-01-20 15:51:11 +00:00
let folder_editor = Arc::new(TokioRwLock::new(None));
let persistence = Arc::new(FolderPersistence::new(database.clone(), folder_editor.clone()));
2022-01-14 12:52:03 +00:00
let trash_controller = Arc::new(TrashController::new(
persistence.clone(),
cloud_service.clone(),
user.clone(),
));
let view_controller = Arc::new(ViewController::new(
user.clone(),
persistence.clone(),
cloud_service.clone(),
trash_controller.clone(),
2022-10-22 13:57:44 +00:00
data_processors,
2022-01-14 12:52:03 +00:00
));
let app_controller = Arc::new(AppController::new(
user.clone(),
persistence.clone(),
trash_controller.clone(),
cloud_service.clone(),
));
let workspace_controller = Arc::new(WorkspaceController::new(
user.clone(),
persistence.clone(),
trash_controller.clone(),
cloud_service.clone(),
));
2021-12-06 07:49:21 +00:00
Self {
user,
2022-01-10 15:45:59 +00:00
cloud_service,
2022-01-14 01:09:25 +00:00
persistence,
2021-12-06 07:49:21 +00:00
workspace_controller,
app_controller,
view_controller,
trash_controller,
2022-01-20 15:51:11 +00:00
web_socket,
folder_editor,
2021-12-06 07:49:21 +00:00
}
}
2022-01-10 15:45:59 +00:00
// pub fn network_state_changed(&self, new_type: NetworkType) {
// match new_type {
// NetworkType::UnknownNetworkType => {},
// NetworkType::Wifi => {},
// NetworkType::Cell => {},
// NetworkType::Ethernet => {},
// }
// }
2021-12-06 07:49:21 +00:00
2022-01-20 15:51:11 +00:00
pub async fn did_receive_ws_data(&self, data: Bytes) {
let result: Result<ServerRevisionWSData, protobuf::ProtobufError> = data.try_into();
match result {
Ok(data) => match self.folder_editor.read().await.clone() {
2022-01-24 09:35:58 +00:00
None => {}
2022-01-20 15:51:11 +00:00
Some(editor) => match editor.receive_ws_data(data).await {
2022-01-24 09:35:58 +00:00
Ok(_) => {}
2022-01-20 15:51:11 +00:00
Err(e) => tracing::error!("Folder receive data error: {:?}", e),
},
},
Err(e) => {
tracing::error!("Folder ws data parser failed: {:?}", e);
2022-01-24 09:35:58 +00:00
}
2022-01-20 15:51:11 +00:00
}
}
2022-01-14 12:52:03 +00:00
2022-10-22 13:57:44 +00:00
/// Called immediately after the application launched with the user sign in/sign up.
#[tracing::instrument(level = "trace", skip(self), err)]
2022-01-24 08:27:40 +00:00
pub async fn initialize(&self, user_id: &str, token: &str) -> FlowyResult<()> {
2022-01-23 14:33:47 +00:00
let mut write_guard = INIT_FOLDER_FLAG.write().await;
if let Some(is_init) = write_guard.get(user_id) {
2022-01-18 14:56:57 +00:00
if *is_init {
return Ok(());
}
}
tracing::debug!("Initialize folder editor");
2022-01-22 10:48:43 +00:00
let folder_id = FolderId::new(user_id);
let _ = self.persistence.initialize(user_id, &folder_id).await?;
2022-01-20 15:51:11 +00:00
let pool = self.persistence.db_pool()?;
2022-06-10 14:27:19 +00:00
let object_id = folder_id.as_ref();
2022-11-01 10:59:53 +00:00
let disk_cache = SQLiteFolderRevisionPersistence::new(user_id, pool.clone());
let configuration = RevisionPersistenceConfiguration::new(100, false);
2022-11-06 01:59:53 +00:00
let rev_persistence = RevisionPersistence::new(user_id, object_id, disk_cache, configuration);
let rev_compactor = FolderRevisionCompress();
2022-07-20 06:07:54 +00:00
// let history_persistence = SQLiteRevisionHistoryPersistence::new(object_id, pool.clone());
2022-06-10 14:27:19 +00:00
let snapshot_persistence = SQLiteRevisionSnapshotPersistence::new(object_id, pool);
let rev_manager = RevisionManager::new(
user_id,
folder_id.as_ref(),
rev_persistence,
rev_compactor,
2022-07-20 06:07:54 +00:00
// history_persistence,
2022-06-10 14:27:19 +00:00
snapshot_persistence,
);
2022-03-11 13:36:00 +00:00
2022-05-26 09:28:44 +00:00
let folder_editor = FolderEditor::new(user_id, &folder_id, token, rev_manager, self.web_socket.clone()).await?;
2022-01-20 15:51:11 +00:00
*self.folder_editor.write().await = Some(Arc::new(folder_editor));
2022-01-18 14:56:57 +00:00
let _ = self.app_controller.initialize()?;
let _ = self.view_controller.initialize()?;
2022-01-23 14:33:47 +00:00
write_guard.insert(user_id.to_owned(), true);
2022-01-18 14:56:57 +00:00
Ok(())
}
2021-12-06 07:49:21 +00:00
2022-10-22 13:57:44 +00:00
pub async fn initialize_with_new_user(
&self,
user_id: &str,
token: &str,
view_data_format: ViewDataFormatPB,
) -> FlowyResult<()> {
DefaultFolderBuilder::build(
token,
user_id,
self.persistence.clone(),
self.view_controller.clone(),
|| (view_data_format.clone(), Bytes::from(initial_read_me())),
)
.await?;
2022-01-24 08:27:40 +00:00
self.initialize(user_id, token).await
}
2022-01-24 09:35:58 +00:00
pub async fn clear(&self) {
*self.folder_editor.write().await = None;
}
}
struct DefaultFolderBuilder();
impl DefaultFolderBuilder {
2022-10-22 13:57:44 +00:00
async fn build<F: Fn() -> (ViewDataFormatPB, Bytes)>(
token: &str,
user_id: &str,
persistence: Arc<FolderPersistence>,
view_controller: Arc<ViewController>,
2022-10-22 13:57:44 +00:00
create_view_fn: F,
) -> FlowyResult<()> {
2021-12-06 07:49:21 +00:00
log::debug!("Create user default workspace");
let workspace_rev = user_default::create_default_workspace();
set_current_workspace(&workspace_rev.id);
for app in workspace_rev.apps.iter() {
for (index, view) in app.belongings.iter().enumerate() {
2022-10-22 13:57:44 +00:00
let (view_data_type, view_data) = create_view_fn();
if index == 0 {
let _ = view_controller.set_latest_view(&view.id);
let layout_type = ViewLayoutTypePB::from(view.layout.clone());
let _ = view_controller
2022-10-22 13:57:44 +00:00
.create_view(&view.id, view_data_type, layout_type, view_data)
.await?;
}
2021-12-06 07:49:21 +00:00
}
}
let folder = FolderPad::new(vec![workspace_rev.clone()], vec![])?;
2022-01-22 10:48:43 +00:00
let folder_id = FolderId::new(user_id);
let _ = persistence.save_folder(user_id, &folder_id, folder).await?;
2022-07-19 06:11:29 +00:00
let repeated_workspace = RepeatedWorkspacePB {
items: vec![workspace_rev.into()],
};
send_dart_notification(token, FolderNotification::UserCreateWorkspace)
2021-12-06 07:49:21 +00:00
.payload(repeated_workspace)
.send();
2021-12-06 13:47:21 +00:00
Ok(())
}
2021-12-06 07:49:21 +00:00
}
2022-01-22 10:48:43 +00:00
#[cfg(feature = "flowy_unit_test")]
impl FolderManager {
2022-05-26 09:28:44 +00:00
pub async fn folder_editor(&self) -> Arc<FolderEditor> {
2022-01-24 09:35:58 +00:00
self.folder_editor.read().await.clone().unwrap()
}
2022-01-22 10:48:43 +00:00
}
2022-03-05 14:30:42 +00:00
pub trait ViewDataProcessor {
2022-10-22 13:57:44 +00:00
fn create_view(
2022-09-23 03:23:35 +00:00
&self,
user_id: &str,
view_id: &str,
layout: ViewLayoutTypePB,
2022-10-22 13:57:44 +00:00
view_data: Bytes,
2022-09-23 03:23:35 +00:00
) -> FutureResult<(), FlowyError>;
2022-03-06 01:03:02 +00:00
2022-10-22 13:57:44 +00:00
fn close_view(&self, view_id: &str) -> FutureResult<(), FlowyError>;
2022-03-06 01:03:02 +00:00
2022-10-22 13:57:44 +00:00
fn get_view_data(&self, view: &ViewPB) -> FutureResult<Bytes, FlowyError>;
2022-03-06 01:03:02 +00:00
2022-08-10 09:59:28 +00:00
fn create_default_view(
&self,
user_id: &str,
view_id: &str,
2022-08-18 13:52:47 +00:00
layout: ViewLayoutTypePB,
2022-10-22 13:57:44 +00:00
data_format: ViewDataFormatPB,
2022-08-10 09:59:28 +00:00
) -> FutureResult<Bytes, FlowyError>;
2022-03-15 11:00:28 +00:00
2022-06-15 03:43:24 +00:00
fn create_view_from_delta_data(
&self,
user_id: &str,
view_id: &str,
data: Vec<u8>,
2022-09-23 03:23:35 +00:00
layout: ViewLayoutTypePB,
2022-06-15 03:43:24 +00:00
) -> FutureResult<Bytes, FlowyError>;
2022-03-06 01:03:02 +00:00
2022-10-22 13:57:44 +00:00
fn data_types(&self) -> Vec<ViewDataFormatPB>;
2022-03-05 14:30:42 +00:00
}
2022-10-22 13:57:44 +00:00
pub type ViewDataProcessorMap = Arc<HashMap<ViewDataFormatPB, Arc<dyn ViewDataProcessor + Send + Sync>>>;