refactor: optimize setDefaultAvatar in provider

This commit is contained in:
alt0 2023-01-10 11:34:05 +08:00
parent dcfe8237ef
commit 39f0b1b252
3 changed files with 22 additions and 22 deletions

View File

@ -8,9 +8,9 @@ import { storage } from './storage.js';
import assert from 'assert'; import assert from 'assert';
import { WebsocketProvider } from './sync.js'; import { WebsocketProvider } from './sync.js';
// import { IndexedDBProvider } from '../local/indexeddb'; // import { IndexedDBProvider } from '../local/indexeddb';
import { getDefaultHeadImgBlob } from '../../utils/index.js';
import { getApis } from './apis/index.js'; import { getApis } from './apis/index.js';
import type { Apis, WorkspaceDetail, Callback } from './apis'; import type { Apis, WorkspaceDetail, Callback } from './apis';
import { setDefaultAvatar } from '../utils.js';
export interface AffineProviderConstructorParams export interface AffineProviderConstructorParams
extends ProviderConstructorParams { extends ProviderConstructorParams {
@ -295,16 +295,8 @@ export class AffineProvider extends BaseProvider {
}; };
if (!blocksuiteWorkspace.meta.avatar) { if (!blocksuiteWorkspace.meta.avatar) {
// set default avatar await setDefaultAvatar(blocksuiteWorkspace);
const blob = await getDefaultHeadImgBlob(meta.name); workspaceInfo.avatar = blocksuiteWorkspace.meta.avatar;
const blobStorage = await blocksuiteWorkspace.blobs;
assert(blobStorage, 'No blob storage');
const blobId = await blobStorage.set(blob);
const avatar = await blobStorage.get(blobId);
if (avatar) {
blocksuiteWorkspace.meta.setAvatar(avatar);
workspaceInfo.avatar = avatar;
}
} }
this._workspaces.add(workspaceInfo); this._workspaces.add(workspaceInfo);
return blocksuiteWorkspace; return blocksuiteWorkspace;

View File

@ -5,7 +5,7 @@ import { WorkspaceInfo, WorkspaceMeta } from '../../types';
import { Workspace as BlocksuiteWorkspace, uuidv4 } from '@blocksuite/store'; import { Workspace as BlocksuiteWorkspace, uuidv4 } from '@blocksuite/store';
import { IndexedDBProvider } from './indexeddb.js'; import { IndexedDBProvider } from './indexeddb.js';
import assert from 'assert'; import assert from 'assert';
import { getDefaultHeadImgBlob } from '../../utils/index.js'; import { setDefaultAvatar } from '../utils.js';
const WORKSPACE_KEY = 'workspaces'; const WORKSPACE_KEY = 'workspaces';
@ -97,16 +97,8 @@ export class LocalProvider extends BaseProvider {
blocksuiteWorkspace.meta.setName(meta.name); blocksuiteWorkspace.meta.setName(meta.name);
if (!meta.avatar) { if (!meta.avatar) {
// set default avatar await setDefaultAvatar(blocksuiteWorkspace);
const blob = await getDefaultHeadImgBlob(meta.name); workspaceInfo.avatar = blocksuiteWorkspace.meta.avatar;
const blobStorage = await blocksuiteWorkspace.blobs;
assert(blobStorage, 'No blob storage');
const blobId = await blobStorage.set(blob);
const avatar = await blobStorage.get(blobId);
if (avatar) {
blocksuiteWorkspace.meta.setAvatar(avatar);
workspaceInfo.avatar = avatar;
}
} }
this._workspaces.add(workspaceInfo); this._workspaces.add(workspaceInfo);

View File

@ -0,0 +1,16 @@
import assert from 'assert';
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
import { getDefaultHeadImgBlob } from '../utils/index.js';
export const setDefaultAvatar = async (
blocksuiteWorkspace: BlocksuiteWorkspace
) => {
const blob = await getDefaultHeadImgBlob(blocksuiteWorkspace.meta.name);
const blobStorage = await blocksuiteWorkspace.blobs;
assert(blobStorage, 'No blob storage');
const blobId = await blobStorage.set(blob);
const avatar = await blobStorage.get(blobId);
if (avatar) {
blocksuiteWorkspace.meta.setAvatar(avatar);
}
};