mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-09 05:05:52 +08:00
Merge remote-tracking branch 'refs/remotes/origin/feat/cloud-sync-saika'
Conflicts: packages/data-center/package.json packages/data-center/src/datacenter.ts packages/data-center/src/index.ts pnpm-lock.yaml
This commit is contained in:
@@ -1,22 +1,26 @@
|
||||
import { WorkspaceMetaCollection } from './workspace-meta-collection.js';
|
||||
import type { WorkspaceMetaCollectionChangeEvent } from './workspace-meta-collection';
|
||||
import { WorkspaceUnitCollection } from './workspace-unit-collection.js';
|
||||
import type { WorkspaceUnitCollectionChangeEvent } from './workspace-unit-collection';
|
||||
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
|
||||
import { BaseProvider } from './provider/base';
|
||||
import type {
|
||||
BaseProvider,
|
||||
CreateWorkspaceInfoParams,
|
||||
UpdateWorkspaceMetaParams,
|
||||
} from './provider/base';
|
||||
import { LocalProvider } from './provider/local/local';
|
||||
import { AffineProvider } from './provider';
|
||||
import type { Message, WorkspaceMeta } from './types';
|
||||
import type { Message } from './types';
|
||||
import assert from 'assert';
|
||||
import { getLogger } from './logger';
|
||||
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
|
||||
import { createBlocksuiteWorkspace } from './utils/index.js';
|
||||
import { MessageCenter } from './message';
|
||||
import type { WorkspaceUnit } from './workspace-unit';
|
||||
|
||||
/**
|
||||
* @class DataCenter
|
||||
* @classdesc Data center is made for managing different providers for business
|
||||
*/
|
||||
export class DataCenter {
|
||||
private readonly _workspaceMetaCollection = new WorkspaceMetaCollection();
|
||||
private readonly _workspaceUnitCollection = new WorkspaceUnitCollection();
|
||||
private readonly _logger = getLogger('dc');
|
||||
private _workspaceInstances: Map<string, BlocksuiteWorkspace> = new Map();
|
||||
private _messageCenter = new MessageCenter();
|
||||
@@ -35,7 +39,7 @@ export class DataCenter {
|
||||
const getInitParams = () => {
|
||||
return {
|
||||
logger: dc._logger,
|
||||
workspaces: dc._workspaceMetaCollection.createScope(),
|
||||
workspaces: dc._workspaceUnitCollection.createScope(),
|
||||
messageCenter: dc._messageCenter,
|
||||
};
|
||||
};
|
||||
@@ -46,11 +50,15 @@ export class DataCenter {
|
||||
typeof window.__TAURI_IPC__ === 'function'
|
||||
) {
|
||||
const { TauriIPCProvider } = await import('./provider/tauri-ipc');
|
||||
dc.registerProvider(new TauriIPCProvider(getInitParams()));
|
||||
await dc.registerProvider(new TauriIPCProvider(getInitParams()));
|
||||
} else {
|
||||
dc.registerProvider(new LocalProvider(getInitParams()));
|
||||
await dc.registerProvider(new LocalProvider(getInitParams()));
|
||||
}
|
||||
await dc.registerProvider(new AffineProvider(getInitParams()));
|
||||
|
||||
for (const provider of dc.providerMap.values()) {
|
||||
await provider.loadWorkspaces();
|
||||
}
|
||||
dc.registerProvider(new AffineProvider(getInitParams()));
|
||||
|
||||
return dc;
|
||||
}
|
||||
@@ -59,12 +67,12 @@ export class DataCenter {
|
||||
* Register provider.
|
||||
* We will automatically set the first provider to default provider.
|
||||
*/
|
||||
registerProvider(provider: BaseProvider) {
|
||||
async registerProvider(provider: BaseProvider) {
|
||||
if (!this._mainProvider) {
|
||||
this._mainProvider = provider;
|
||||
}
|
||||
|
||||
provider.init();
|
||||
await provider.init();
|
||||
this.providerMap.set(provider.id, provider);
|
||||
}
|
||||
|
||||
@@ -77,7 +85,7 @@ export class DataCenter {
|
||||
}
|
||||
|
||||
public get workspaces() {
|
||||
return this._workspaceMetaCollection.workspaces;
|
||||
return this._workspaceUnitCollection.workspaces;
|
||||
}
|
||||
|
||||
public async refreshWorkspaces() {
|
||||
@@ -91,20 +99,19 @@ export class DataCenter {
|
||||
* @param {string} name workspace name
|
||||
* @returns {Promise<Workspace>}
|
||||
*/
|
||||
public async createWorkspace(workspaceMeta: WorkspaceMeta) {
|
||||
public async createWorkspace(params: CreateWorkspaceInfoParams) {
|
||||
assert(
|
||||
this._mainProvider,
|
||||
'There is no provider. You should add provider first.'
|
||||
);
|
||||
|
||||
const workspaceInfo = await this._mainProvider.createWorkspaceInfo(
|
||||
workspaceMeta
|
||||
);
|
||||
const workspaceMeta = await this._mainProvider.createWorkspaceInfo(params);
|
||||
|
||||
const workspace = createBlocksuiteWorkspace(workspaceInfo.id);
|
||||
const workspace = createBlocksuiteWorkspace(workspaceMeta.id);
|
||||
|
||||
await this._mainProvider.createWorkspace(workspace, workspaceMeta);
|
||||
return workspace;
|
||||
const workspaceUnit = this._workspaceUnitCollection.find(workspaceMeta.id);
|
||||
return workspaceUnit;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,7 +119,7 @@ export class DataCenter {
|
||||
* @param {string} workspaceId workspace id
|
||||
*/
|
||||
public async deleteWorkspace(workspaceId: string) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
assert(provider, `Workspace exists, but we couldn't find its provider.`);
|
||||
@@ -124,10 +131,10 @@ export class DataCenter {
|
||||
* @param {string} workspaceId workspace id
|
||||
*/
|
||||
private _getBlocksuiteWorkspace(workspaceId: string) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
return (
|
||||
this._workspaceInstances.get(workspaceId) ||
|
||||
// this._workspaceInstances.get(workspaceId) ||
|
||||
createBlocksuiteWorkspace(workspaceId)
|
||||
);
|
||||
}
|
||||
@@ -155,21 +162,26 @@ export class DataCenter {
|
||||
/**
|
||||
* load workspace instance by id
|
||||
* @param {string} workspaceId workspace id
|
||||
* @returns {Promise<BlocksuiteWorkspace>}
|
||||
* @returns {Promise<WorkspaceUnit>}
|
||||
*/
|
||||
public async loadWorkspace(workspaceId: string) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const currentProvider = this.providerMap.get(workspaceInfo.provider);
|
||||
const workspaceUnit = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceUnit, 'Workspace not found');
|
||||
const currentProvider = this.providerMap.get(workspaceUnit.provider);
|
||||
if (currentProvider) {
|
||||
currentProvider.closeWorkspace(workspaceId);
|
||||
}
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
assert(provider, `provide '${workspaceInfo.provider}' is not registered`);
|
||||
this._logger(`Loading ${workspaceInfo.provider} workspace: `, workspaceId);
|
||||
const provider = this.providerMap.get(workspaceUnit.provider);
|
||||
assert(provider, `provide '${workspaceUnit.provider}' is not registered`);
|
||||
this._logger(`Loading ${workspaceUnit.provider} workspace: `, workspaceId);
|
||||
const workspace = this._getBlocksuiteWorkspace(workspaceId);
|
||||
this._workspaceInstances.set(workspaceId, workspace);
|
||||
return await provider.warpWorkspace(workspace);
|
||||
await provider.warpWorkspace(workspace);
|
||||
this._workspaceUnitCollection.workspaces.forEach(workspaceUnit => {
|
||||
workspaceUnit.setBlocksuiteWorkspace(null);
|
||||
});
|
||||
workspaceUnit.setBlocksuiteWorkspace(workspace);
|
||||
return workspaceUnit;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,22 +201,30 @@ export class DataCenter {
|
||||
* @param {Function} callback callback function
|
||||
*/
|
||||
public async onWorkspacesChange(
|
||||
callback: (workspaces: WorkspaceMetaCollectionChangeEvent) => void
|
||||
callback: (workspaces: WorkspaceUnitCollectionChangeEvent) => void,
|
||||
{ immediate = true }: { immediate?: boolean } = {}
|
||||
) {
|
||||
this._workspaceMetaCollection.on('change', callback);
|
||||
if (immediate) {
|
||||
callback({
|
||||
added: this._workspaceUnitCollection.workspaces,
|
||||
});
|
||||
}
|
||||
this._workspaceUnitCollection.on('change', callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* change workspaces meta
|
||||
* @param {WorkspaceMeta} workspaceMeta workspace meta
|
||||
* @param {BlocksuiteWorkspace} workspace workspace instance
|
||||
* @param {WorkspaceUnit} workspace workspace instance
|
||||
*/
|
||||
public async updateWorkspaceMeta(
|
||||
{ name, avatar }: Partial<WorkspaceMeta>,
|
||||
workspace: BlocksuiteWorkspace
|
||||
{ name, avatar }: UpdateWorkspaceMetaParams,
|
||||
workspaceUnit: WorkspaceUnit
|
||||
) {
|
||||
assert(workspace?.room, 'No workspace to set meta');
|
||||
const update: Partial<WorkspaceMeta> = {};
|
||||
assert(workspaceUnit?.id, 'No workspace to set meta');
|
||||
const workspace = workspaceUnit.blocksuiteWorkspace;
|
||||
assert(workspace);
|
||||
const update: Partial<UpdateWorkspaceMetaParams> = {};
|
||||
if (name) {
|
||||
workspace.meta.setName(name);
|
||||
update.name = name;
|
||||
@@ -214,10 +234,10 @@ export class DataCenter {
|
||||
update.avatar = avatar;
|
||||
}
|
||||
// may run for change workspace meta
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspace.room);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceUnit.id);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
provider?.updateWorkspaceMeta(workspace.room, update);
|
||||
provider?.updateWorkspaceMeta(workspaceUnit.id, update);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,7 +246,7 @@ export class DataCenter {
|
||||
* @param id workspace id
|
||||
*/
|
||||
public async leaveWorkspace(workspaceId: string) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
@@ -236,7 +256,7 @@ export class DataCenter {
|
||||
}
|
||||
|
||||
public async setWorkspacePublish(workspaceId: string, isPublish: boolean) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
@@ -244,12 +264,17 @@ export class DataCenter {
|
||||
}
|
||||
}
|
||||
|
||||
public async inviteMember(id: string, email: string) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(id);
|
||||
/**
|
||||
* invite the new member to the workspace
|
||||
* @param {string} workspaceId workspace id
|
||||
* @param {string} email
|
||||
*/
|
||||
public async inviteMember(workspaceId: string, email: string) {
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
await provider.invite(id, email);
|
||||
await provider.invite(workspaceId, email);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,7 +283,7 @@ export class DataCenter {
|
||||
* @param {number} permissionId permission id
|
||||
*/
|
||||
public async removeMember(workspaceId: string, permissionId: number) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
@@ -289,7 +314,7 @@ export class DataCenter {
|
||||
providerId: string
|
||||
) {
|
||||
assert(workspace.room, 'No workspace id');
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspace.room);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspace.room);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
if (workspaceInfo.provider === providerId) {
|
||||
this._logger('Workspace provider is same');
|
||||
@@ -302,11 +327,12 @@ export class DataCenter {
|
||||
this._logger(`create ${providerId} workspace: `, workspaceInfo.name);
|
||||
const newWorkspaceInfo = await newProvider.createWorkspaceInfo({
|
||||
name: workspaceInfo.name,
|
||||
avatar: workspaceInfo.avatar,
|
||||
// avatar: workspaceInfo.avatar,
|
||||
});
|
||||
const newWorkspace = createBlocksuiteWorkspace(newWorkspaceInfo.id);
|
||||
// TODO optimize this function
|
||||
await newProvider.createWorkspace(newWorkspace, {
|
||||
...newWorkspaceInfo,
|
||||
name: workspaceInfo.name,
|
||||
avatar: workspaceInfo.avatar,
|
||||
});
|
||||
@@ -315,7 +341,7 @@ export class DataCenter {
|
||||
this._logger(
|
||||
`update workspace data from ${workspaceInfo.provider} to ${providerId}`
|
||||
);
|
||||
applyUpdate(newWorkspace.doc, encodeStateAsUpdate(workspace.doc));
|
||||
await newProvider.assign(newWorkspace, workspace);
|
||||
assert(newWorkspace, 'Create workspace failed');
|
||||
await currentProvider.deleteWorkspace(workspace.room);
|
||||
return newWorkspace.room;
|
||||
@@ -325,9 +351,13 @@ export class DataCenter {
|
||||
* Enable workspace cloud
|
||||
* @param {string} id ID of workspace.
|
||||
*/
|
||||
public async enableWorkspaceCloud(workspace: BlocksuiteWorkspace) {
|
||||
assert(workspace?.room, 'No workspace to enable cloud');
|
||||
return await this._transWorkspaceProvider(workspace, 'affine');
|
||||
public async enableWorkspaceCloud(workspace: WorkspaceUnit) {
|
||||
assert(workspace?.id, 'No workspace to enable cloud');
|
||||
assert(workspace.blocksuiteWorkspace);
|
||||
return await this._transWorkspaceProvider(
|
||||
workspace.blocksuiteWorkspace,
|
||||
'affine'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -364,10 +394,10 @@ export class DataCenter {
|
||||
* @returns {Promise<string | null>} blob url
|
||||
*/
|
||||
async getBlob(
|
||||
workspace: BlocksuiteWorkspace,
|
||||
workspaceUnit: WorkspaceUnit,
|
||||
id: string
|
||||
): Promise<string | null> {
|
||||
const blob = await workspace.blobs;
|
||||
const blob = await workspaceUnit.blocksuiteWorkspace?.blobs;
|
||||
return (await blob?.get(id)) || '';
|
||||
}
|
||||
|
||||
@@ -376,11 +406,25 @@ export class DataCenter {
|
||||
* @param id
|
||||
* @returns {Promise<string | null>} blob url
|
||||
*/
|
||||
async setBlob(workspace: BlocksuiteWorkspace, blob: Blob): Promise<string> {
|
||||
const blobStorage = await workspace.blobs;
|
||||
async setBlob(workspace: WorkspaceUnit, blob: Blob): Promise<string> {
|
||||
const blobStorage = await workspace.blocksuiteWorkspace?.blobs;
|
||||
return (await blobStorage?.set(blob)) || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* get members of a workspace
|
||||
* @param workspaceId
|
||||
*/
|
||||
async getMembers(workspaceId: string) {
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
return await provider.getWorkspaceMembers(workspaceId);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
onMessage(cb: (message: Message) => void) {
|
||||
return this._messageCenter.onMessage(cb);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user