| 
									
										
										
										
											2022-07-04 14:28:41 +08:00
										 |  |  | use crate::entities::workspace::*;
 | 
					
						
							| 
									
										
										
										
											2022-05-05 10:45:53 +08:00
										 |  |  | use crate::manager::FolderManager;
 | 
					
						
							| 
									
										
										
										
											2021-07-20 14:03:21 +08:00
										 |  |  | use crate::{
 | 
					
						
							| 
									
										
										
										
											2022-01-13 11:16:26 +08:00
										 |  |  |     dart_notification::*,
 | 
					
						
							| 
									
										
										
										
											2021-07-20 14:03:21 +08:00
										 |  |  |     errors::*,
 | 
					
						
							| 
									
										
										
										
											2022-01-30 10:33:21 +08:00
										 |  |  |     event_map::{FolderCouldServiceV1, WorkspaceUser},
 | 
					
						
							| 
									
										
										
										
											2021-12-06 15:49:21 +08:00
										 |  |  |     services::{
 | 
					
						
							| 
									
										
										
										
											2022-01-17 11:55:36 +08:00
										 |  |  |         persistence::{FolderPersistence, FolderPersistenceTransaction, WorkspaceChangeset},
 | 
					
						
							| 
									
										
										
										
											2022-01-24 17:35:58 +08:00
										 |  |  |         read_local_workspace_apps, TrashController,
 | 
					
						
							| 
									
										
										
										
											2021-12-06 15:49:21 +08:00
										 |  |  |     },
 | 
					
						
							| 
									
										
										
										
											2021-09-07 23:30:43 +08:00
										 |  |  | };
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  | use flowy_database::kv::KV;
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  | use flowy_folder_data_model::revision::{AppRevision, WorkspaceRevision};
 | 
					
						
							| 
									
										
										
										
											2021-12-06 15:49:21 +08:00
										 |  |  | use std::sync::Arc;
 | 
					
						
							| 
									
										
										
										
											2021-07-13 23:08:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-05 14:37:45 +08:00
										 |  |  | pub struct WorkspaceController {
 | 
					
						
							| 
									
										
										
										
											2021-07-18 23:56:36 +08:00
										 |  |  |     pub user: Arc<dyn WorkspaceUser>,
 | 
					
						
							| 
									
										
										
										
											2022-01-17 11:55:36 +08:00
										 |  |  |     persistence: Arc<FolderPersistence>,
 | 
					
						
							| 
									
										
										
										
											2021-12-06 15:49:21 +08:00
										 |  |  |     pub(crate) trash_controller: Arc<TrashController>,
 | 
					
						
							| 
									
										
										
										
											2022-01-17 11:55:36 +08:00
										 |  |  |     cloud_service: Arc<dyn FolderCouldServiceV1>,
 | 
					
						
							| 
									
										
										
										
											2021-07-13 23:08:20 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl WorkspaceController {
 | 
					
						
							| 
									
										
										
										
											2021-09-01 22:50:22 +08:00
										 |  |  |     pub(crate) fn new(
 | 
					
						
							| 
									
										
										
										
											2021-07-20 14:03:21 +08:00
										 |  |  |         user: Arc<dyn WorkspaceUser>,
 | 
					
						
							| 
									
										
										
										
											2022-01-17 11:55:36 +08:00
										 |  |  |         persistence: Arc<FolderPersistence>,
 | 
					
						
							| 
									
										
										
										
											2021-12-06 15:49:21 +08:00
										 |  |  |         trash_can: Arc<TrashController>,
 | 
					
						
							| 
									
										
										
										
											2022-01-17 11:55:36 +08:00
										 |  |  |         cloud_service: Arc<dyn FolderCouldServiceV1>,
 | 
					
						
							| 
									
										
										
										
											2021-07-20 14:03:21 +08:00
										 |  |  |     ) -> Self {
 | 
					
						
							|  |  |  |         Self {
 | 
					
						
							|  |  |  |             user,
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |             persistence,
 | 
					
						
							| 
									
										
										
										
											2021-12-06 15:49:21 +08:00
										 |  |  |             trash_controller: trash_can,
 | 
					
						
							| 
									
										
										
										
											2022-01-10 23:45:59 +08:00
										 |  |  |             cloud_service,
 | 
					
						
							| 
									
										
										
										
											2021-07-20 14:03:21 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-07-13 23:08:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 19:19:02 +08:00
										 |  |  |     pub(crate) async fn create_workspace_from_params(
 | 
					
						
							|  |  |  |         &self,
 | 
					
						
							|  |  |  |         params: CreateWorkspaceParams,
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |     ) -> Result<WorkspaceRevision, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2021-09-02 19:57:19 +08:00
										 |  |  |         let workspace = self.create_workspace_on_server(params.clone()).await?;
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |         let user_id = self.user.user_id()?;
 | 
					
						
							| 
									
										
										
										
											2021-09-08 18:25:32 +08:00
										 |  |  |         let token = self.user.token()?;
 | 
					
						
							| 
									
										
										
										
											2022-01-20 23:51:11 +08:00
										 |  |  |         let workspaces = self
 | 
					
						
							|  |  |  |             .persistence
 | 
					
						
							|  |  |  |             .begin_transaction(|transaction| {
 | 
					
						
							|  |  |  |                 let _ = transaction.create_workspace(&user_id, workspace.clone())?;
 | 
					
						
							|  |  |  |                 transaction.read_workspaces(&user_id, None)
 | 
					
						
							|  |  |  |             })
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |             .await?
 | 
					
						
							|  |  |  |             .into_iter()
 | 
					
						
							|  |  |  |             .map(|workspace_rev| workspace_rev.into())
 | 
					
						
							|  |  |  |             .collect();
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |         let repeated_workspace = RepeatedWorkspace { items: workspaces };
 | 
					
						
							| 
									
										
										
										
											2022-01-27 20:39:54 +08:00
										 |  |  |         send_dart_notification(&token, FolderNotification::UserCreateWorkspace)
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |             .payload(repeated_workspace)
 | 
					
						
							|  |  |  |             .send();
 | 
					
						
							| 
									
										
										
										
											2021-11-08 19:19:02 +08:00
										 |  |  |         set_current_workspace(&workspace.id);
 | 
					
						
							| 
									
										
										
										
											2021-07-29 22:22:35 +08:00
										 |  |  |         Ok(workspace)
 | 
					
						
							| 
									
										
										
										
											2021-07-13 23:08:20 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 14:42:14 +08:00
										 |  |  |     #[allow(dead_code)]
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |     pub(crate) async fn update_workspace(&self, params: UpdateWorkspaceParams) -> Result<(), FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |         let changeset = WorkspaceChangeset::new(params.clone());
 | 
					
						
							| 
									
										
										
										
											2021-07-21 22:41:44 +08:00
										 |  |  |         let workspace_id = changeset.id.clone();
 | 
					
						
							| 
									
										
										
										
											2022-01-20 23:51:11 +08:00
										 |  |  |         let workspace = self
 | 
					
						
							|  |  |  |             .persistence
 | 
					
						
							|  |  |  |             .begin_transaction(|transaction| {
 | 
					
						
							|  |  |  |                 let _ = transaction.update_workspace(changeset)?;
 | 
					
						
							|  |  |  |                 let user_id = self.user.user_id()?;
 | 
					
						
							|  |  |  |                 self.read_local_workspace(workspace_id.clone(), &user_id, &transaction)
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |             .await?;
 | 
					
						
							| 
									
										
										
										
											2021-09-07 17:12:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 20:39:54 +08:00
										 |  |  |         send_dart_notification(&workspace_id, FolderNotification::WorkspaceUpdated)
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |             .payload(workspace)
 | 
					
						
							|  |  |  |             .send();
 | 
					
						
							| 
									
										
										
										
											2021-09-09 15:43:05 +08:00
										 |  |  |         let _ = self.update_workspace_on_server(params)?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-20 14:03:21 +08:00
										 |  |  |         Ok(())
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 14:42:14 +08:00
										 |  |  |     #[allow(dead_code)]
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |     pub(crate) async fn delete_workspace(&self, workspace_id: &str) -> Result<(), FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2021-07-29 22:22:35 +08:00
										 |  |  |         let user_id = self.user.user_id()?;
 | 
					
						
							| 
									
										
										
										
											2021-09-08 18:25:32 +08:00
										 |  |  |         let token = self.user.token()?;
 | 
					
						
							| 
									
										
										
										
											2022-01-20 23:51:11 +08:00
										 |  |  |         let repeated_workspace = self
 | 
					
						
							|  |  |  |             .persistence
 | 
					
						
							|  |  |  |             .begin_transaction(|transaction| {
 | 
					
						
							|  |  |  |                 let _ = transaction.delete_workspace(workspace_id)?;
 | 
					
						
							|  |  |  |                 self.read_local_workspaces(None, &user_id, &transaction)
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |             .await?;
 | 
					
						
							| 
									
										
										
										
											2022-01-27 20:39:54 +08:00
										 |  |  |         send_dart_notification(&token, FolderNotification::UserDeleteWorkspace)
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |             .payload(repeated_workspace)
 | 
					
						
							|  |  |  |             .send();
 | 
					
						
							| 
									
										
										
										
											2021-09-09 15:43:05 +08:00
										 |  |  |         let _ = self.delete_workspace_on_server(workspace_id)?;
 | 
					
						
							| 
									
										
										
										
											2021-07-29 22:22:35 +08:00
										 |  |  |         Ok(())
 | 
					
						
							| 
									
										
										
										
											2021-07-20 14:03:21 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-31 10:32:25 +08:00
										 |  |  |     pub(crate) async fn open_workspace(&self, params: WorkspaceId) -> Result<Workspace, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2021-08-27 23:53:53 +08:00
										 |  |  |         let user_id = self.user.user_id()?;
 | 
					
						
							| 
									
										
										
										
											2022-02-24 21:49:18 +08:00
										 |  |  |         if let Some(workspace_id) = params.value {
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |             let workspace = self
 | 
					
						
							|  |  |  |                 .persistence
 | 
					
						
							| 
									
										
										
										
											2022-01-20 23:51:11 +08:00
										 |  |  |                 .begin_transaction(|transaction| self.read_local_workspace(workspace_id, &user_id, &transaction))
 | 
					
						
							|  |  |  |                 .await?;
 | 
					
						
							| 
									
										
										
										
											2021-09-07 17:12:03 +08:00
										 |  |  |             set_current_workspace(&workspace.id);
 | 
					
						
							|  |  |  |             Ok(workspace)
 | 
					
						
							| 
									
										
										
										
											2021-09-04 09:00:15 +08:00
										 |  |  |         } else {
 | 
					
						
							| 
									
										
										
										
											2022-01-24 17:35:58 +08:00
										 |  |  |             Err(FlowyError::workspace_id().context("Opened workspace id should not be empty"))
 | 
					
						
							| 
									
										
										
										
											2021-08-27 23:53:53 +08:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |     pub(crate) async fn read_current_workspace_apps(&self) -> Result<Vec<AppRevision>, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2021-09-07 17:12:03 +08:00
										 |  |  |         let workspace_id = get_current_workspace()?;
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |         let app_revs = self
 | 
					
						
							| 
									
										
										
										
											2022-01-20 23:51:11 +08:00
										 |  |  |             .persistence
 | 
					
						
							|  |  |  |             .begin_transaction(|transaction| {
 | 
					
						
							|  |  |  |                 read_local_workspace_apps(&workspace_id, self.trash_controller.clone(), &transaction)
 | 
					
						
							|  |  |  |             })
 | 
					
						
							|  |  |  |             .await?;
 | 
					
						
							| 
									
										
										
										
											2021-09-07 17:12:03 +08:00
										 |  |  |         // TODO: read from server
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |         Ok(app_revs)
 | 
					
						
							| 
									
										
										
										
											2021-09-07 17:12:03 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |     #[tracing::instrument(level = "debug", skip(self, transaction), err)]
 | 
					
						
							|  |  |  |     pub(crate) fn read_local_workspaces<'a>(
 | 
					
						
							| 
									
										
										
										
											2021-09-07 21:31:04 +08:00
										 |  |  |         &self,
 | 
					
						
							|  |  |  |         workspace_id: Option<String>,
 | 
					
						
							|  |  |  |         user_id: &str,
 | 
					
						
							| 
									
										
										
										
											2022-01-17 11:55:36 +08:00
										 |  |  |         transaction: &'a (dyn FolderPersistenceTransaction + 'a),
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |     ) -> Result<RepeatedWorkspace, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2021-09-07 17:12:03 +08:00
										 |  |  |         let workspace_id = workspace_id.to_owned();
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |         let workspaces = transaction
 | 
					
						
							|  |  |  |             .read_workspaces(user_id, workspace_id)?
 | 
					
						
							|  |  |  |             .into_iter()
 | 
					
						
							|  |  |  |             .map(|workspace_rev| workspace_rev.into())
 | 
					
						
							|  |  |  |             .collect();
 | 
					
						
							| 
									
										
										
										
											2021-08-27 23:53:53 +08:00
										 |  |  |         Ok(RepeatedWorkspace { items: workspaces })
 | 
					
						
							| 
									
										
										
										
											2021-07-20 14:03:21 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 09:09:25 +08:00
										 |  |  |     pub(crate) fn read_local_workspace<'a>(
 | 
					
						
							| 
									
										
										
										
											2021-10-01 19:39:08 +08:00
										 |  |  |         &self,
 | 
					
						
							|  |  |  |         workspace_id: String,
 | 
					
						
							|  |  |  |         user_id: &str,
 | 
					
						
							| 
									
										
										
										
											2022-01-17 11:55:36 +08:00
										 |  |  |         transaction: &'a (dyn FolderPersistenceTransaction + 'a),
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |     ) -> Result<Workspace, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |         let mut workspace_revs = transaction.read_workspaces(user_id, Some(workspace_id.clone()))?;
 | 
					
						
							|  |  |  |         if workspace_revs.is_empty() {
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |             return Err(FlowyError::record_not_found().context(format!("{} workspace not found", workspace_id)));
 | 
					
						
							| 
									
										
										
										
											2021-08-28 23:08:12 +08:00
										 |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |         debug_assert_eq!(workspace_revs.len(), 1);
 | 
					
						
							|  |  |  |         let workspace = workspace_revs
 | 
					
						
							|  |  |  |             .drain(..1)
 | 
					
						
							|  |  |  |             .map(|workspace_rev| workspace_rev.into())
 | 
					
						
							|  |  |  |             .collect::<Vec<Workspace>>()
 | 
					
						
							|  |  |  |             .pop()
 | 
					
						
							|  |  |  |             .unwrap();
 | 
					
						
							| 
									
										
										
										
											2021-08-28 23:08:12 +08:00
										 |  |  |         Ok(workspace)
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-07-13 23:08:20 +08:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2021-08-24 21:38:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  | impl WorkspaceController {
 | 
					
						
							| 
									
										
										
										
											2022-01-23 22:33:47 +08:00
										 |  |  |     #[tracing::instrument(level = "trace", skip(self), err)]
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |     async fn create_workspace_on_server(&self, params: CreateWorkspaceParams) -> Result<WorkspaceRevision, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |         let token = self.user.token()?;
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |         self.cloud_service.create_workspace(&token, params).await
 | 
					
						
							| 
									
										
										
										
											2021-08-28 23:08:12 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-23 22:33:47 +08:00
										 |  |  |     #[tracing::instrument(level = "trace", skip(self), err)]
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |     fn update_workspace_on_server(&self, params: UpdateWorkspaceParams) -> Result<(), FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2022-01-10 23:45:59 +08:00
										 |  |  |         let (token, server) = (self.user.token()?, self.cloud_service.clone());
 | 
					
						
							| 
									
										
										
										
											2021-11-09 15:32:57 +08:00
										 |  |  |         tokio::spawn(async move {
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |             match server.update_workspace(&token, params).await {
 | 
					
						
							| 
									
										
										
										
											2022-01-24 17:35:58 +08:00
										 |  |  |                 Ok(_) => {}
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |                 Err(e) => {
 | 
					
						
							|  |  |  |                     // TODO: retry?
 | 
					
						
							|  |  |  |                     log::error!("Update workspace failed: {:?}", e);
 | 
					
						
							| 
									
										
										
										
											2022-01-24 17:35:58 +08:00
										 |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  |         Ok(())
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-08-24 21:38:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-23 22:33:47 +08:00
										 |  |  |     #[tracing::instrument(level = "trace", skip(self), err)]
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |     fn delete_workspace_on_server(&self, workspace_id: &str) -> Result<(), FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2021-12-31 10:32:25 +08:00
										 |  |  |         let params = WorkspaceId {
 | 
					
						
							| 
									
										
										
										
											2022-02-24 21:49:18 +08:00
										 |  |  |             value: Some(workspace_id.to_string()),
 | 
					
						
							| 
									
										
										
										
											2021-09-02 19:57:19 +08:00
										 |  |  |         };
 | 
					
						
							| 
									
										
										
										
											2022-01-10 23:45:59 +08:00
										 |  |  |         let (token, server) = (self.user.token()?, self.cloud_service.clone());
 | 
					
						
							| 
									
										
										
										
											2021-11-09 15:32:57 +08:00
										 |  |  |         tokio::spawn(async move {
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |             match server.delete_workspace(&token, params).await {
 | 
					
						
							| 
									
										
										
										
											2022-01-24 17:35:58 +08:00
										 |  |  |                 Ok(_) => {}
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |                 Err(e) => {
 | 
					
						
							|  |  |  |                     // TODO: retry?
 | 
					
						
							|  |  |  |                     log::error!("Delete workspace failed: {:?}", e);
 | 
					
						
							| 
									
										
										
										
											2022-01-24 17:35:58 +08:00
										 |  |  |                 }
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |             }
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  |         Ok(())
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-05 10:45:53 +08:00
										 |  |  | pub async fn notify_workspace_setting_did_change(
 | 
					
						
							|  |  |  |     folder_manager: &Arc<FolderManager>,
 | 
					
						
							|  |  |  |     view_id: &str,
 | 
					
						
							|  |  |  | ) -> FlowyResult<()> {
 | 
					
						
							|  |  |  |     let user_id = folder_manager.user.user_id()?;
 | 
					
						
							|  |  |  |     let token = folder_manager.user.token()?;
 | 
					
						
							|  |  |  |     let workspace_id = get_current_workspace()?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let workspace_setting = folder_manager
 | 
					
						
							|  |  |  |         .persistence
 | 
					
						
							|  |  |  |         .begin_transaction(|transaction| {
 | 
					
						
							|  |  |  |             let workspace = folder_manager.workspace_controller.read_local_workspace(
 | 
					
						
							|  |  |  |                 workspace_id.clone(),
 | 
					
						
							|  |  |  |                 &user_id,
 | 
					
						
							|  |  |  |                 &transaction,
 | 
					
						
							|  |  |  |             )?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             let setting = match transaction.read_view(view_id) {
 | 
					
						
							|  |  |  |                 Ok(latest_view) => CurrentWorkspaceSetting {
 | 
					
						
							|  |  |  |                     workspace,
 | 
					
						
							| 
									
										
										
										
											2022-06-13 20:59:46 +08:00
										 |  |  |                     latest_view: Some(latest_view.into()),
 | 
					
						
							| 
									
										
										
										
											2022-05-05 10:45:53 +08:00
										 |  |  |                 },
 | 
					
						
							|  |  |  |                 Err(_) => CurrentWorkspaceSetting {
 | 
					
						
							|  |  |  |                     workspace,
 | 
					
						
							|  |  |  |                     latest_view: None,
 | 
					
						
							|  |  |  |                 },
 | 
					
						
							|  |  |  |             };
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Ok(setting)
 | 
					
						
							|  |  |  |         })
 | 
					
						
							|  |  |  |         .await?;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     send_dart_notification(&token, FolderNotification::WorkspaceSetting)
 | 
					
						
							|  |  |  |         .payload(workspace_setting)
 | 
					
						
							|  |  |  |         .send();
 | 
					
						
							|  |  |  |     Ok(())
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  | const CURRENT_WORKSPACE_ID: &str = "current_workspace_id";
 | 
					
						
							| 
									
										
										
										
											2021-08-25 17:34:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-24 17:35:58 +08:00
										 |  |  | pub fn set_current_workspace(workspace_id: &str) {
 | 
					
						
							|  |  |  |     KV::set_str(CURRENT_WORKSPACE_ID, workspace_id.to_owned());
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2021-08-26 17:58:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  | pub fn get_current_workspace() -> Result<String, FlowyError> {
 | 
					
						
							| 
									
										
										
										
											2021-09-03 12:44:48 +08:00
										 |  |  |     match KV::get_str(CURRENT_WORKSPACE_ID) {
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |         None => {
 | 
					
						
							|  |  |  |             Err(FlowyError::record_not_found()
 | 
					
						
							|  |  |  |                 .context("Current workspace not found or should call open workspace first"))
 | 
					
						
							| 
									
										
										
										
											2022-01-24 17:35:58 +08:00
										 |  |  |         }
 | 
					
						
							| 
									
										
										
										
											2021-09-02 14:47:39 +08:00
										 |  |  |         Some(workspace_id) => Ok(workspace_id),
 | 
					
						
							|  |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2021-08-26 17:58:59 +08:00
										 |  |  | }
 |