mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-26 23:02:57 +08:00
refactor: add workspace unit
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
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';
|
||||
@@ -16,7 +20,7 @@ import { MessageCenter } from './message';
|
||||
* @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,
|
||||
};
|
||||
};
|
||||
@@ -68,7 +72,7 @@ export class DataCenter {
|
||||
}
|
||||
|
||||
public get workspaces() {
|
||||
return this._workspaceMetaCollection.workspaces;
|
||||
return this._workspaceUnitCollection.workspaces;
|
||||
}
|
||||
|
||||
public async refreshWorkspaces() {
|
||||
@@ -82,17 +86,15 @@ 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;
|
||||
@@ -103,7 +105,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.`);
|
||||
@@ -115,7 +117,7 @@ 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) ||
|
||||
@@ -149,7 +151,7 @@ export class DataCenter {
|
||||
* @returns {Promise<BlocksuiteWorkspace>}
|
||||
*/
|
||||
public async loadWorkspace(workspaceId: string) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspaceId);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspaceId);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const currentProvider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (currentProvider) {
|
||||
@@ -180,9 +182,9 @@ export class DataCenter {
|
||||
* @param {Function} callback callback function
|
||||
*/
|
||||
public async onWorkspacesChange(
|
||||
callback: (workspaces: WorkspaceMetaCollectionChangeEvent) => void
|
||||
callback: (workspaces: WorkspaceUnitCollectionChangeEvent) => void
|
||||
) {
|
||||
this._workspaceMetaCollection.on('change', callback);
|
||||
this._workspaceUnitCollection.on('change', callback);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,11 +193,11 @@ export class DataCenter {
|
||||
* @param {BlocksuiteWorkspace} workspace workspace instance
|
||||
*/
|
||||
public async updateWorkspaceMeta(
|
||||
{ name, avatar }: Partial<WorkspaceMeta>,
|
||||
{ name, avatar }: UpdateWorkspaceMetaParams,
|
||||
workspace: BlocksuiteWorkspace
|
||||
) {
|
||||
assert(workspace?.room, 'No workspace to set meta');
|
||||
const update: Partial<WorkspaceMeta> = {};
|
||||
const update: Partial<UpdateWorkspaceMetaParams> = {};
|
||||
if (name) {
|
||||
workspace.meta.setName(name);
|
||||
update.name = name;
|
||||
@@ -205,7 +207,7 @@ export class DataCenter {
|
||||
update.avatar = avatar;
|
||||
}
|
||||
// may run for change workspace meta
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(workspace.room);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(workspace.room);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
provider?.updateWorkspaceMeta(workspace.room, update);
|
||||
@@ -217,7 +219,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) {
|
||||
@@ -227,7 +229,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) {
|
||||
@@ -236,7 +238,7 @@ export class DataCenter {
|
||||
}
|
||||
|
||||
public async inviteMember(id: string, email: string) {
|
||||
const workspaceInfo = this._workspaceMetaCollection.find(id);
|
||||
const workspaceInfo = this._workspaceUnitCollection.find(id);
|
||||
assert(workspaceInfo, 'Workspace not found');
|
||||
const provider = this.providerMap.get(workspaceInfo.provider);
|
||||
if (provider) {
|
||||
@@ -249,7 +251,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) {
|
||||
@@ -280,7 +282,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');
|
||||
@@ -293,11 +295,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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user