Merge remote-tracking branch 'origin/feat/cloud-sync-saika' into feat/cloud-sync-saika

This commit is contained in:
alt0 2023-01-09 15:49:23 +08:00
commit dee6355b29
4 changed files with 48 additions and 44 deletions

View File

@ -17,7 +17,6 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs';
export class DataCenter {
private readonly _workspaces = new Workspaces();
private readonly _logger = getLogger('dc');
private readonly _blobStorage: BlobStorage = new BlobStorage();
/**
* A mainProvider must exist as the only data trustworthy source.
*/
@ -35,14 +34,12 @@ export class DataCenter {
new LocalProvider({
logger: dc._logger,
workspaces: dc._workspaces.createScope(),
blobs: dc._blobStorage,
})
);
dc.registerProvider(
new AffineProvider({
logger: dc._logger,
workspaces: dc._workspaces.createScope(),
blobs: dc._blobStorage,
})
);
@ -335,21 +332,21 @@ export class DataCenter {
return;
}
/**
* get blob url by workspaces id
* @param id
* @returns {Promise<string | null>} blob url
*/
async getBlob(id: string): Promise<string | null> {
return await this._blobStorage.get(id);
}
// /**
// * get blob url by workspaces id
// * @param id
// * @returns {Promise<string | null>} blob url
// */
// async getBlob(id: string): Promise<string | null> {
// return await this._blobStorage.get(id);
// }
/**
* up load blob and get a blob url
* @param id
* @returns {Promise<string | null>} blob url
*/
async setBlob(blob: Blob): Promise<string> {
return await this._blobStorage.set(blob);
}
// /**
// * up load blob and get a blob url
// * @param id
// * @returns {Promise<string | null>} blob url
// */
// async setBlob(blob: Blob): Promise<string> {
// return await this._blobStorage.set(blob);
// }
}

View File

@ -246,19 +246,12 @@ export class AffineProvider extends BaseProvider {
meta: WorkspaceMeta
): Promise<Workspace | undefined> {
assert(meta.name, 'Workspace name is required');
if (!meta.avatar) {
// set default avatar
const blob = await getDefaultHeadImgBlob(meta.name);
const blobId = await this.setBlob(blob);
meta.avatar = (await this.getBlob(blobId)) || '';
}
const { id } = await createWorkspace(meta as Required<WorkspaceMeta>);
this._logger('Creating affine workspace');
const nw = new Workspace({
room: id,
}).register(BlockSchema);
nw.meta.setName(meta.name);
nw.meta.setAvatar(meta.avatar);
this._initWorkspaceDb(nw);
const workspaceInfo: WS = {
@ -272,6 +265,18 @@ export class AffineProvider extends BaseProvider {
provider: 'local',
};
if (!meta.avatar) {
// set default avatar
const blob = await getDefaultHeadImgBlob(meta.name);
const blobStorage = await nw.blobs;
assert(blobStorage, 'No blob storage');
const blobId = await blobStorage.set(blob);
const avatar = await blobStorage.get(blobId);
if (avatar) {
nw.meta.setAvatar(avatar);
workspaceInfo.avatar = avatar;
}
}
this._workspaces.add(workspaceInfo);
return nw;
}

View File

@ -9,19 +9,16 @@ const defaultLogger = () => {
export interface ProviderConstructorParams {
logger?: Logger;
workspaces: WorkspacesScope;
blobs: BlobStorage;
}
export class BaseProvider {
public readonly id: string = 'base';
protected _workspaces!: WorkspacesScope;
protected _logger!: Logger;
protected _blobs!: BlobStorage;
public constructor({ logger, workspaces, blobs }: ProviderConstructorParams) {
public constructor({ logger, workspaces }: ProviderConstructorParams) {
this._logger = (logger || defaultLogger) as Logger;
this._workspaces = workspaces;
this._blobs = blobs;
}
/**
@ -69,19 +66,19 @@ export class BaseProvider {
return;
}
async getBlob(id: string): Promise<string | null> {
return await this._blobs.get(id);
}
// async getBlob(id: string): Promise<string | null> {
// return await this._blobs.get(id);
// }
async setBlob(blob: Blob): Promise<string> {
return await this._blobs.set(blob);
}
// async setBlob(blob: Blob): Promise<string> {
// return await this._blobs.set(blob);
// }
/**
* clear all local data in provider
*/
async clear() {
this._blobs.clear();
// this._blobs.clear();
}
/**

View File

@ -79,12 +79,6 @@ export class LocalProvider extends BaseProvider {
meta: WorkspaceMeta
): Promise<Workspace | undefined> {
assert(meta.name, 'Workspace name is required');
if (!meta.avatar) {
// set default avatar
const blob = await getDefaultHeadImgBlob(meta.name);
const blobId = await this.setBlob(blob);
meta.avatar = (await this.getBlob(blobId)) || '';
}
this._logger('Creating affine workspace');
const workspaceInfo: WS = {
@ -101,7 +95,18 @@ export class LocalProvider extends BaseProvider {
const workspace = new Workspace({ room: workspaceInfo.id });
this._initWorkspaceDb(workspace);
workspace.meta.setName(meta.name);
workspace.meta.setAvatar(meta.avatar);
if (!meta.avatar) {
// set default avatar
const blob = await getDefaultHeadImgBlob(meta.name);
const blobStorage = await workspace.blobs;
assert(blobStorage, 'No blob storage');
const blobId = await blobStorage.set(blob);
const avatar = await blobStorage.get(blobId);
if (avatar) {
workspace.meta.setAvatar(avatar);
workspaceInfo.avatar = avatar;
}
}
this._workspaces.add(workspaceInfo);
this._storeWorkspaces(this._workspaces.list());