diff --git a/client-app/src-OctoBase b/client-app/src-OctoBase index eb1826dbef..b22b072a1b 160000 --- a/client-app/src-OctoBase +++ b/client-app/src-OctoBase @@ -1 +1 @@ -Subproject commit eb1826dbef56153f3ae3838ed2514a46e11756d7 +Subproject commit b22b072a1b5c939575bcade4db437842ab434f36 diff --git a/client-app/src-tauri/src/commands.rs b/client-app/src-tauri/src/commands.rs index 5848ba9fd6..6a98817605 100644 --- a/client-app/src-tauri/src/commands.rs +++ b/client-app/src-tauri/src/commands.rs @@ -12,6 +12,7 @@ pub fn invoke_handler() -> impl Fn(tauri::Invoke) + Send + Sync + 'static { create_workspace, update_workspace, get_workspaces, + get_workspace, get_doc, put_blob, get_blob diff --git a/client-app/src-tauri/src/commands/workspace.rs b/client-app/src-tauri/src/commands/workspace.rs index 37178a2020..77261de8e5 100644 --- a/client-app/src-tauri/src/commands/workspace.rs +++ b/client-app/src-tauri/src/commands/workspace.rs @@ -1,5 +1,6 @@ use ipc_types::workspace::{ - CreateWorkspace, CreateWorkspaceResult, GetWorkspacesResult, UpdateWorkspace, + CreateWorkspace, CreateWorkspaceResult, GetWorkspace, GetWorkspaceResult, GetWorkspaces, + GetWorkspacesResult, UpdateWorkspace, }; use jwst::{DocStorage, Workspace as OctoBaseWorkspace}; use lib0::any::Any; @@ -10,7 +11,7 @@ use crate::state::AppState; /// create yDoc for a workspace pub async fn get_workspaces<'s>( state: tauri::State<'s, AppState>, - parameters: CreateWorkspace, + parameters: GetWorkspaces, ) -> Result { match &state .0 @@ -27,6 +28,30 @@ pub async fn get_workspaces<'s>( } } +#[tauri::command] +/// create yDoc for a workspace +pub async fn get_workspace<'s>( + state: tauri::State<'s, AppState>, + parameters: GetWorkspace, +) -> Result { + match &state + .0 + .lock() + .await + .metadata_db + .get_workspace_by_id(parameters.id) + .await + { + Ok(user_workspace_option) => match user_workspace_option { + Some(user_workspace) => Ok(GetWorkspaceResult { + workspace: user_workspace.clone(), + }), + None => Err("Get workspace has no result".to_string()) + }, + Err(error_message) => Err(error_message.to_string()), + } +} + #[tauri::command] /// create yDoc for a workspace pub async fn create_workspace<'s>( diff --git a/client-app/src-tauri/types/src/workspace.rs b/client-app/src-tauri/types/src/workspace.rs index 9b501f29a0..de04b1b488 100644 --- a/client-app/src-tauri/types/src/workspace.rs +++ b/client-app/src-tauri/types/src/workspace.rs @@ -1,4 +1,4 @@ -use jwst_storage::model::WorkspaceWithPermission; +use jwst_storage::{model::WorkspaceWithPermission, WorkspaceDetail}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -18,26 +18,37 @@ pub struct GetWorkspaces { pub user_id: i32, } -#[derive(Debug, Clone, Serialize, JsonSchema)] +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +pub struct GetWorkspace { + pub id: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct CreateWorkspaceResult { pub id: String, pub name: String, } -#[derive(Debug, Clone, Serialize, JsonSchema)] +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct GetWorkspacesResult { pub workspaces: Vec, } +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] +pub struct GetWorkspaceResult { + pub workspace: WorkspaceDetail, +} + #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub struct UpdateWorkspace { pub id: i64, pub public: bool, } -#[derive(Debug, Clone, Serialize, JsonSchema)] +#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] pub enum IWorkspaceParameters { CreateWorkspace(CreateWorkspace), + GetWorkspace(GetWorkspace), GetWorkspaces(GetWorkspaces), GetWorkspacesResult(GetWorkspacesResult), UpdateWorkspace(UpdateWorkspace), diff --git a/packages/data-center/src/provider/tauri-ipc/index.ts b/packages/data-center/src/provider/tauri-ipc/index.ts index 252281e10b..1e3a852f2c 100644 --- a/packages/data-center/src/provider/tauri-ipc/index.ts +++ b/packages/data-center/src/provider/tauri-ipc/index.ts @@ -132,10 +132,79 @@ export class TauriIPCProvider extends LocalProvider { return nw; } - async getWorkspaces( - config: Readonly> - ): Promise | undefined> { - const entries = await config.entries(); - return new Map(entries); + override async loadWorkspaces() { + // TODO: get user id here + const workspacesList = await this.#ipc.getWorkspaces({ user_id: 0 }); + const workspaces: WS[] = workspacesList.workspaces.map(w => { + return { + ...w, + memberCount: 0, + name: '', + provider: 'affine', + }; + }); + const workspaceInstances = workspaces.map(({ id }) => { + const workspace = + this._workspacesCache.get(id) || + new Workspace({ + room: id, + }).register(BlockSchema); + this._workspacesCache.set(id, workspace); + if (workspace) { + return new Promise(resolve => { + downloadWorkspace(id).then(data => { + applyUpdate(workspace.doc, new Uint8Array(data)); + resolve(workspace); + }); + }); + } else { + return Promise.resolve(null); + } + }); + + (await Promise.all(workspaceInstances)).forEach((workspace, i) => { + if (workspace) { + workspaces[i] = { + ...workspaces[i], + name: workspace.doc.meta.name, + avatar: workspace.doc.meta.avatar, + }; + } + }); + const getDetailList = workspacesList.map(w => { + const { id } = w; + return new Promise<{ id: string; detail: WorkspaceDetail | null }>( + resolve => { + getWorkspaceDetail({ id }).then(data => { + resolve({ id, detail: data || null }); + }); + } + ); + }); + const ownerList = await Promise.all(getDetailList); + (await Promise.all(ownerList)).forEach(detail => { + if (detail) { + const { id, detail: workspaceDetail } = detail; + if (workspaceDetail) { + const { owner, member_count } = workspaceDetail; + const currentWorkspace = workspaces.find(w => w.id === id); + if (currentWorkspace) { + currentWorkspace.owner = { + id: owner.id, + name: owner.name, + avatar: owner.avatar_url, + email: owner.email, + }; + currentWorkspace.memberCount = member_count; + } + } + } + }); + + workspaces.forEach(workspace => { + this._workspaces.add(workspace); + }); + + return workspaces; } } diff --git a/packages/data-center/src/provider/tauri-ipc/ipc/types/workspace.json b/packages/data-center/src/provider/tauri-ipc/ipc/types/workspace.json index aa1c6a40f3..debbd09d29 100644 --- a/packages/data-center/src/provider/tauri-ipc/ipc/types/workspace.json +++ b/packages/data-center/src/provider/tauri-ipc/ipc/types/workspace.json @@ -12,6 +12,16 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": ["GetWorkspace"], + "properties": { + "GetWorkspace": { + "$ref": "#/definitions/GetWorkspace" + } + }, + "additionalProperties": false + }, { "type": "object", "required": ["GetWorkspaces"], @@ -80,6 +90,15 @@ } } }, + "GetWorkspace": { + "type": "object", + "required": ["id"], + "properties": { + "id": { + "type": "string" + } + } + }, "GetWorkspaces": { "type": "object", "required": ["user_id"], @@ -103,8 +122,20 @@ } }, "PermissionType": { - "type": "string", - "enum": ["Read", "Write", "Admin", "Owner"] + "anyOf": [ + { + "type": "null" + }, + { + "type": "null" + }, + { + "type": "null" + }, + { + "type": "null" + } + ] }, "UpdateWorkspace": { "type": "object", @@ -120,8 +151,14 @@ } }, "WorkspaceType": { - "type": "string", - "enum": ["Private", "Normal"] + "anyOf": [ + { + "type": "null" + }, + { + "type": "null" + } + ] }, "WorkspaceWithPermission": { "type": "object", diff --git a/packages/data-center/src/provider/tauri-ipc/ipc/types/workspace.ts b/packages/data-center/src/provider/tauri-ipc/ipc/types/workspace.ts index 97d08ed90a..be5a78e634 100644 --- a/packages/data-center/src/provider/tauri-ipc/ipc/types/workspace.ts +++ b/packages/data-center/src/provider/tauri-ipc/ipc/types/workspace.ts @@ -9,6 +9,9 @@ export type IWorkspaceParameters = | { CreateWorkspace: CreateWorkspace; } + | { + GetWorkspace: GetWorkspace; + } | { GetWorkspaces: GetWorkspaces; } @@ -21,8 +24,8 @@ export type IWorkspaceParameters = | { CreateWorkspaceResult: CreateWorkspaceResult; }; -export type PermissionType = 'Read' | 'Write' | 'Admin' | 'Owner'; -export type WorkspaceType = 'Private' | 'Normal'; +export type PermissionType = null | null | null | null; +export type WorkspaceType = null | null; export interface CreateWorkspace { /** @@ -32,6 +35,10 @@ export interface CreateWorkspace { user_id: number; [k: string]: unknown; } +export interface GetWorkspace { + id: string; + [k: string]: unknown; +} export interface GetWorkspaces { user_id: number; [k: string]: unknown;