feat: load workspace in ipc provider

This commit is contained in:
linonetwo 2023-01-10 16:55:23 +08:00
parent 6c784184b1
commit a76fadb1bc
6 changed files with 159 additions and 44 deletions

View File

@ -50,6 +50,7 @@ pub enum IWorkspaceParameters {
CreateWorkspace(CreateWorkspace), CreateWorkspace(CreateWorkspace),
GetWorkspace(GetWorkspace), GetWorkspace(GetWorkspace),
GetWorkspaces(GetWorkspaces), GetWorkspaces(GetWorkspaces),
GetWorkspaceResult(GetWorkspaceResult),
GetWorkspacesResult(GetWorkspacesResult), GetWorkspacesResult(GetWorkspacesResult),
UpdateWorkspace(UpdateWorkspace), UpdateWorkspace(UpdateWorkspace),
CreateWorkspaceResult(CreateWorkspaceResult), CreateWorkspaceResult(CreateWorkspaceResult),

View File

@ -13,7 +13,7 @@ export enum WorkspaceType {
export enum PermissionType { export enum PermissionType {
Read = 0, Read = 0,
Write = 1, Write = 1,
Admin = 2, Admin = 10,
Owner = 99, Owner = 99,
} }

View File

@ -10,10 +10,12 @@ import { ConfigStore } from 'src/store.js';
import { User, Workspace as WS, WorkspaceMeta, Logger } from '../../types'; import { User, Workspace as WS, WorkspaceMeta, Logger } from '../../types';
import { getDefaultHeadImgBlob } from 'src/utils/index.js'; import { getDefaultHeadImgBlob } from 'src/utils/index.js';
import { IPCBlobProvider } from './blocksuite-provider/blob.js'; import { IPCBlobProvider } from './blocksuite-provider/blob.js';
import { PermissionType, WorkspaceDetail } from '../affine/apis/workspace.js';
export class TauriIPCProvider extends LocalProvider { export class TauriIPCProvider extends LocalProvider {
static id = 'tauri-ipc'; static id = 'tauri-ipc';
#ipc = ipcMethods; #ipc = ipcMethods;
private _workspacesCache: Map<string, Workspace> = new Map();
constructor(params: ProviderConstructorParams) { constructor(params: ProviderConstructorParams) {
super(params); super(params);
@ -134,8 +136,10 @@ export class TauriIPCProvider extends LocalProvider {
override async loadWorkspaces() { override async loadWorkspaces() {
// TODO: get user id here // TODO: get user id here
const workspacesList = await this.#ipc.getWorkspaces({ user_id: 0 }); const { workspaces: workspacesList } = await this.#ipc.getWorkspaces({
const workspaces: WS[] = workspacesList.workspaces.map(w => { user_id: 0,
});
const workspaces: WS[] = workspacesList.map(w => {
return { return {
...w, ...w,
memberCount: 0, memberCount: 0,
@ -152,8 +156,8 @@ export class TauriIPCProvider extends LocalProvider {
this._workspacesCache.set(id, workspace); this._workspacesCache.set(id, workspace);
if (workspace) { if (workspace) {
return new Promise<Workspace>(resolve => { return new Promise<Workspace>(resolve => {
downloadWorkspace(id).then(data => { this.#ipc.getYDocument({ id }).then(({ update }) => {
applyUpdate(workspace.doc, new Uint8Array(data)); Y.applyUpdate(workspace.doc, new Uint8Array(update));
resolve(workspace); resolve(workspace);
}); });
}); });
@ -171,16 +175,37 @@ export class TauriIPCProvider extends LocalProvider {
}; };
} }
}); });
const getDetailList = workspacesList.map(w => { const getDetailList = workspacesList.map(
const { id } = w; async (
return new Promise<{ id: string; detail: WorkspaceDetail | null }>( workspaceWithPermission
resolve => { ): Promise<{
getWorkspaceDetail({ id }).then(data => { id: string;
resolve({ id, detail: data || null }); detail:
}); | (Omit<WorkspaceDetail, 'owner'> & {
} owner?: Partial<WorkspaceDetail['owner']>;
); })
}); | 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); const ownerList = await Promise.all(getDetailList);
(await Promise.all(ownerList)).forEach(detail => { (await Promise.all(ownerList)).forEach(detail => {
if (detail) { if (detail) {
@ -188,12 +213,12 @@ export class TauriIPCProvider extends LocalProvider {
if (workspaceDetail) { if (workspaceDetail) {
const { owner, member_count } = workspaceDetail; const { owner, member_count } = workspaceDetail;
const currentWorkspace = workspaces.find(w => w.id === id); const currentWorkspace = workspaces.find(w => w.id === id);
if (currentWorkspace) { if (currentWorkspace && owner) {
currentWorkspace.owner = { currentWorkspace.owner = {
id: owner.id, id: owner.id!,
name: owner.name, name: owner.name!,
avatar: owner.avatar_url, avatar: owner.avatar_url!,
email: owner.email, email: owner.email!,
}; };
currentWorkspace.memberCount = member_count; currentWorkspace.memberCount = member_count;
} }

View File

@ -7,6 +7,8 @@ import {
import { import {
CreateWorkspace, CreateWorkspace,
CreateWorkspaceResult, CreateWorkspaceResult,
GetWorkspace,
GetWorkspaceResult,
GetWorkspaces, GetWorkspaces,
GetWorkspacesResult, GetWorkspacesResult,
} from './types/workspace'; } from './types/workspace';
@ -26,8 +28,14 @@ export const createWorkspace = async (parameters: CreateWorkspace) =>
await invoke<CreateWorkspaceResult>('create_workspace', { await invoke<CreateWorkspaceResult>('create_workspace', {
parameters, parameters,
}); });
export const getWorkspaces = async (parameters: GetWorkspaces) => export const getWorkspaces = async (parameters: GetWorkspaces) =>
await invoke<GetWorkspacesResult>('create_workspace', { await invoke<GetWorkspacesResult>('get_workspaces', {
parameters,
});
export const getWorkspace = async (parameters: GetWorkspace) =>
await invoke<GetWorkspaceResult>('get_workspace', {
parameters, parameters,
}); });

View File

@ -32,6 +32,16 @@
}, },
"additionalProperties": false "additionalProperties": false
}, },
{
"type": "object",
"required": ["GetWorkspaceResult"],
"properties": {
"GetWorkspaceResult": {
"$ref": "#/definitions/GetWorkspaceResult"
}
},
"additionalProperties": false
},
{ {
"type": "object", "type": "object",
"required": ["GetWorkspacesResult"], "required": ["GetWorkspacesResult"],
@ -99,6 +109,15 @@
} }
} }
}, },
"GetWorkspaceResult": {
"type": "object",
"required": ["workspace"],
"properties": {
"workspace": {
"$ref": "#/definitions/WorkspaceDetail"
}
}
},
"GetWorkspaces": { "GetWorkspaces": {
"type": "object", "type": "object",
"required": ["user_id"], "required": ["user_id"],
@ -122,20 +141,8 @@
} }
}, },
"PermissionType": { "PermissionType": {
"anyOf": [ "type": "integer",
{ "enum": [0, 1, 10, 99]
"type": "null"
},
{
"type": "null"
},
{
"type": "null"
},
{
"type": "null"
}
]
}, },
"UpdateWorkspace": { "UpdateWorkspace": {
"type": "object", "type": "object",
@ -150,15 +157,65 @@
} }
} }
}, },
"WorkspaceType": { "User": {
"anyOf": [ "type": "object",
{ "required": ["created_at", "email", "id", "name"],
"type": "null" "properties": {
"avatar_url": {
"type": ["string", "null"]
}, },
{ "created_at": {
"type": "null" "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": { "WorkspaceWithPermission": {
"type": "object", "type": "object",

View File

@ -15,6 +15,9 @@ export type IWorkspaceParameters =
| { | {
GetWorkspaces: GetWorkspaces; GetWorkspaces: GetWorkspaces;
} }
| {
GetWorkspaceResult: GetWorkspaceResult;
}
| { | {
GetWorkspacesResult: GetWorkspacesResult; GetWorkspacesResult: GetWorkspacesResult;
} }
@ -24,8 +27,8 @@ export type IWorkspaceParameters =
| { | {
CreateWorkspaceResult: CreateWorkspaceResult; CreateWorkspaceResult: CreateWorkspaceResult;
}; };
export type PermissionType = null | null | null | null; export type WorkspaceType = 0 | 1;
export type WorkspaceType = null | null; export type PermissionType = 0 | 1 | 10 | 99;
export interface CreateWorkspace { export interface CreateWorkspace {
/** /**
@ -43,6 +46,27 @@ export interface GetWorkspaces {
user_id: number; user_id: number;
[k: string]: unknown; [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 { export interface GetWorkspacesResult {
workspaces: WorkspaceWithPermission[]; workspaces: WorkspaceWithPermission[];
[k: string]: unknown; [k: string]: unknown;