Sfoglia il codice sorgente

fix: paser workspce pb (#2005)

Nathan.fooo 2 anni fa
parent
commit
888c7977eb

+ 7 - 4
frontend/appflowy_flutter/lib/user/application/user_listener.dart

@@ -83,11 +83,10 @@ class UserWorkspaceListener {
       PublishNotifier();
 
   FolderNotificationListener? _listener;
-  final UserProfilePB _userProfile;
 
   UserWorkspaceListener({
     required UserProfilePB userProfile,
-  }) : _userProfile = userProfile;
+  });
 
   void start({
     void Function(AuthNotifyValue)? onAuthChanged,
@@ -106,14 +105,18 @@ class UserWorkspaceListener {
       _settingChangedNotifier?.addPublishListener(onSettingUpdated);
     }
 
+    // The "current-workspace" is predefined in the backend. Do not try to
+    // modify it
     _listener = FolderNotificationListener(
-      objectId: _userProfile.token,
+      objectId: "current-workspace",
       handler: _handleObservableType,
     );
   }
 
   void _handleObservableType(
-      FolderNotification ty, Either<Uint8List, FlowyError> result) {
+    FolderNotification ty,
+    Either<Uint8List, FlowyError> result,
+  ) {
     switch (ty) {
       case FolderNotification.DidCreateWorkspace:
       case FolderNotification.DidDeleteWorkspace:

+ 17 - 15
frontend/rust-lib/flowy-folder/src/services/workspace/controller.rs

@@ -11,6 +11,7 @@ use crate::{
 };
 use flowy_sqlite::kv::KV;
 use folder_model::{AppRevision, WorkspaceRevision};
+use lib_dispatch::prelude::ToBytes;
 use std::sync::Arc;
 
 pub struct WorkspaceController {
@@ -41,7 +42,6 @@ impl WorkspaceController {
   ) -> Result<WorkspaceRevision, FlowyError> {
     let workspace = self.create_workspace_on_server(params.clone()).await?;
     let user_id = self.user.user_id()?;
-    let token = self.user.token()?;
     let workspaces = self
       .persistence
       .begin_transaction(|transaction| {
@@ -53,9 +53,7 @@ impl WorkspaceController {
       .map(|workspace_rev| workspace_rev.into())
       .collect();
     let repeated_workspace = RepeatedWorkspacePB { items: workspaces };
-    send_notification(&token, FolderNotification::DidCreateWorkspace)
-      .payload(repeated_workspace)
-      .send();
+    send_workspace_notification(FolderNotification::DidCreateWorkspace, repeated_workspace);
     set_current_workspace(&user_id, &workspace.id);
     Ok(workspace)
   }
@@ -76,9 +74,7 @@ impl WorkspaceController {
       })
       .await?;
 
-    send_notification(&workspace_id, FolderNotification::DidUpdateWorkspace)
-      .payload(workspace)
-      .send();
+    send_workspace_notification(FolderNotification::DidUpdateWorkspace, workspace);
     self.update_workspace_on_server(params)?;
 
     Ok(())
@@ -87,7 +83,6 @@ impl WorkspaceController {
   #[allow(dead_code)]
   pub(crate) async fn delete_workspace(&self, workspace_id: &str) -> Result<(), FlowyError> {
     let user_id = self.user.user_id()?;
-    let token = self.user.token()?;
     let repeated_workspace = self
       .persistence
       .begin_transaction(|transaction| {
@@ -95,9 +90,8 @@ impl WorkspaceController {
         self.read_workspaces(None, &user_id, &transaction)
       })
       .await?;
-    send_notification(&token, FolderNotification::DidDeleteWorkspace)
-      .payload(repeated_workspace)
-      .send();
+
+    send_workspace_notification(FolderNotification::DidDeleteWorkspace, repeated_workspace);
     self.delete_workspace_on_server(workspace_id)?;
     Ok(())
   }
@@ -224,7 +218,6 @@ pub async fn notify_workspace_setting_did_change(
   view_id: &str,
 ) -> FlowyResult<()> {
   let user_id = folder_manager.user.user_id()?;
-  let token = folder_manager.user.token()?;
   let workspace_id = get_current_workspace(&user_id)?;
 
   let workspace_setting = folder_manager
@@ -250,11 +243,20 @@ pub async fn notify_workspace_setting_did_change(
       Ok(setting)
     })
     .await?;
+  send_workspace_notification(
+    FolderNotification::DidUpdateWorkspaceSetting,
+    workspace_setting,
+  );
+  Ok(())
+}
 
-  send_notification(&token, FolderNotification::DidUpdateWorkspaceSetting)
-    .payload(workspace_setting)
+/// The [CURRENT_WORKSPACE] represents as the current workspace that opened by the
+/// user. Only one workspace can be opened at a time.
+const CURRENT_WORKSPACE: &str = "current-workspace";
+fn send_workspace_notification<T: ToBytes>(ty: FolderNotification, payload: T) {
+  send_notification(CURRENT_WORKSPACE, ty)
+    .payload(payload)
     .send();
-  Ok(())
 }
 
 const CURRENT_WORKSPACE_ID: &str = "current_workspace_id";