From a76fadb1bc83dd1391af8e632c9f875db54c37c3 Mon Sep 17 00:00:00 2001 From: linonetwo Date: Tue, 10 Jan 2023 16:55:23 +0800 Subject: [PATCH] feat: load workspace in ipc provider --- client-app/src-tauri/types/src/workspace.rs | 1 + .../src/provider/affine/apis/workspace.ts | 2 +- .../src/provider/tauri-ipc/index.ts | 63 ++++++++---- .../src/provider/tauri-ipc/ipc/methods.ts | 10 +- .../tauri-ipc/ipc/types/workspace.json | 99 +++++++++++++++---- .../provider/tauri-ipc/ipc/types/workspace.ts | 28 +++++- 6 files changed, 159 insertions(+), 44 deletions(-) diff --git a/client-app/src-tauri/types/src/workspace.rs b/client-app/src-tauri/types/src/workspace.rs index de04b1b488..1c68c2790a 100644 --- a/client-app/src-tauri/types/src/workspace.rs +++ b/client-app/src-tauri/types/src/workspace.rs @@ -50,6 +50,7 @@ pub enum IWorkspaceParameters { CreateWorkspace(CreateWorkspace), GetWorkspace(GetWorkspace), GetWorkspaces(GetWorkspaces), + GetWorkspaceResult(GetWorkspaceResult), GetWorkspacesResult(GetWorkspacesResult), UpdateWorkspace(UpdateWorkspace), CreateWorkspaceResult(CreateWorkspaceResult), diff --git a/packages/data-center/src/provider/affine/apis/workspace.ts b/packages/data-center/src/provider/affine/apis/workspace.ts index 0c145928d5..3764c8d84e 100644 --- a/packages/data-center/src/provider/affine/apis/workspace.ts +++ b/packages/data-center/src/provider/affine/apis/workspace.ts @@ -13,7 +13,7 @@ export enum WorkspaceType { export enum PermissionType { Read = 0, Write = 1, - Admin = 2, + Admin = 10, Owner = 99, } diff --git a/packages/data-center/src/provider/tauri-ipc/index.ts b/packages/data-center/src/provider/tauri-ipc/index.ts index 1e3a852f2c..5a2ec89cca 100644 --- a/packages/data-center/src/provider/tauri-ipc/index.ts +++ b/packages/data-center/src/provider/tauri-ipc/index.ts @@ -10,10 +10,12 @@ import { ConfigStore } from 'src/store.js'; import { User, Workspace as WS, WorkspaceMeta, Logger } from '../../types'; import { getDefaultHeadImgBlob } from 'src/utils/index.js'; import { IPCBlobProvider } from './blocksuite-provider/blob.js'; +import { PermissionType, WorkspaceDetail } from '../affine/apis/workspace.js'; export class TauriIPCProvider extends LocalProvider { static id = 'tauri-ipc'; #ipc = ipcMethods; + private _workspacesCache: Map = new Map(); constructor(params: ProviderConstructorParams) { super(params); @@ -134,8 +136,10 @@ export class TauriIPCProvider extends LocalProvider { override async loadWorkspaces() { // TODO: get user id here - const workspacesList = await this.#ipc.getWorkspaces({ user_id: 0 }); - const workspaces: WS[] = workspacesList.workspaces.map(w => { + const { workspaces: workspacesList } = await this.#ipc.getWorkspaces({ + user_id: 0, + }); + const workspaces: WS[] = workspacesList.map(w => { return { ...w, memberCount: 0, @@ -152,8 +156,8 @@ export class TauriIPCProvider extends LocalProvider { this._workspacesCache.set(id, workspace); if (workspace) { return new Promise(resolve => { - downloadWorkspace(id).then(data => { - applyUpdate(workspace.doc, new Uint8Array(data)); + this.#ipc.getYDocument({ id }).then(({ update }) => { + Y.applyUpdate(workspace.doc, new Uint8Array(update)); resolve(workspace); }); }); @@ -171,16 +175,37 @@ export class TauriIPCProvider extends LocalProvider { }; } }); - 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 getDetailList = workspacesList.map( + async ( + workspaceWithPermission + ): Promise<{ + id: string; + detail: + | (Omit & { + owner?: Partial; + }) + | null; + }> => { + const { id, permission, created_at } = workspaceWithPermission; + const { workspace } = await this.#ipc.getWorkspace({ id }); + return { + id, + detail: { + ...workspace, + owner: workspace.owner + ? { + ...workspace.owner, + create_at: String(created_at), + avatar_url: workspace.owner.avatar_url ?? undefined, + id: String(workspace.owner.id), + } + : undefined, + permission_type: permission, + create_at: created_at, + }, + }; + } + ); const ownerList = await Promise.all(getDetailList); (await Promise.all(ownerList)).forEach(detail => { if (detail) { @@ -188,12 +213,12 @@ export class TauriIPCProvider extends LocalProvider { if (workspaceDetail) { const { owner, member_count } = workspaceDetail; const currentWorkspace = workspaces.find(w => w.id === id); - if (currentWorkspace) { + if (currentWorkspace && owner) { currentWorkspace.owner = { - id: owner.id, - name: owner.name, - avatar: owner.avatar_url, - email: owner.email, + id: owner.id!, + name: owner.name!, + avatar: owner.avatar_url!, + email: owner.email!, }; currentWorkspace.memberCount = member_count; } diff --git a/packages/data-center/src/provider/tauri-ipc/ipc/methods.ts b/packages/data-center/src/provider/tauri-ipc/ipc/methods.ts index b68f99293e..b637bfffa2 100644 --- a/packages/data-center/src/provider/tauri-ipc/ipc/methods.ts +++ b/packages/data-center/src/provider/tauri-ipc/ipc/methods.ts @@ -7,6 +7,8 @@ import { import { CreateWorkspace, CreateWorkspaceResult, + GetWorkspace, + GetWorkspaceResult, GetWorkspaces, GetWorkspacesResult, } from './types/workspace'; @@ -26,8 +28,14 @@ export const createWorkspace = async (parameters: CreateWorkspace) => await invoke('create_workspace', { parameters, }); + export const getWorkspaces = async (parameters: GetWorkspaces) => - await invoke('create_workspace', { + await invoke('get_workspaces', { + parameters, + }); + +export const getWorkspace = async (parameters: GetWorkspace) => + await invoke('get_workspace', { parameters, }); 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 debbd09d29..26afd9e32d 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 @@ -32,6 +32,16 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": ["GetWorkspaceResult"], + "properties": { + "GetWorkspaceResult": { + "$ref": "#/definitions/GetWorkspaceResult" + } + }, + "additionalProperties": false + }, { "type": "object", "required": ["GetWorkspacesResult"], @@ -99,6 +109,15 @@ } } }, + "GetWorkspaceResult": { + "type": "object", + "required": ["workspace"], + "properties": { + "workspace": { + "$ref": "#/definitions/WorkspaceDetail" + } + } + }, "GetWorkspaces": { "type": "object", "required": ["user_id"], @@ -122,20 +141,8 @@ } }, "PermissionType": { - "anyOf": [ - { - "type": "null" - }, - { - "type": "null" - }, - { - "type": "null" - }, - { - "type": "null" - } - ] + "type": "integer", + "enum": [0, 1, 10, 99] }, "UpdateWorkspace": { "type": "object", @@ -150,15 +157,65 @@ } } }, - "WorkspaceType": { - "anyOf": [ - { - "type": "null" + "User": { + "type": "object", + "required": ["created_at", "email", "id", "name"], + "properties": { + "avatar_url": { + "type": ["string", "null"] }, - { - "type": "null" + "created_at": { + "type": "integer", + "format": "int64" + }, + "email": { + "type": "string" + }, + "id": { + "type": "integer", + "format": "int32" + }, + "name": { + "type": "string" } - ] + } + }, + "WorkspaceDetail": { + "type": "object", + "required": ["created_at", "id", "member_count", "public", "type"], + "properties": { + "created_at": { + "type": "integer", + "format": "int64" + }, + "id": { + "type": "string" + }, + "member_count": { + "type": "integer", + "format": "int64" + }, + "owner": { + "anyOf": [ + { + "$ref": "#/definitions/User" + }, + { + "type": "null" + } + ] + }, + "public": { + "type": "boolean" + }, + "type": { + "$ref": "#/definitions/WorkspaceType" + } + } + }, + "WorkspaceType": { + "type": "integer", + "enum": [0, 1] }, "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 be5a78e634..174553c939 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 @@ -15,6 +15,9 @@ export type IWorkspaceParameters = | { GetWorkspaces: GetWorkspaces; } + | { + GetWorkspaceResult: GetWorkspaceResult; + } | { GetWorkspacesResult: GetWorkspacesResult; } @@ -24,8 +27,8 @@ export type IWorkspaceParameters = | { CreateWorkspaceResult: CreateWorkspaceResult; }; -export type PermissionType = null | null | null | null; -export type WorkspaceType = null | null; +export type WorkspaceType = 0 | 1; +export type PermissionType = 0 | 1 | 10 | 99; export interface CreateWorkspace { /** @@ -43,6 +46,27 @@ export interface GetWorkspaces { user_id: number; [k: string]: unknown; } +export interface GetWorkspaceResult { + workspace: WorkspaceDetail; + [k: string]: unknown; +} +export interface WorkspaceDetail { + created_at: number; + id: string; + member_count: number; + owner?: User | null; + public: boolean; + type: WorkspaceType; + [k: string]: unknown; +} +export interface User { + avatar_url?: string | null; + created_at: number; + email: string; + id: number; + name: string; + [k: string]: unknown; +} export interface GetWorkspacesResult { workspaces: WorkspaceWithPermission[]; [k: string]: unknown;