mirror of
https://github.com/toeverything/AFFiNE.git
synced 2024-11-30 15:42:49 +03:00
feat: add blob apis
This commit is contained in:
parent
dd529f8035
commit
e46cf6c543
@ -1,10 +1,10 @@
|
||||
import { Workspaces } from './workspaces';
|
||||
import type { WorkspacesChangeEvent } from './workspaces';
|
||||
import { Workspace } from '@blocksuite/store';
|
||||
import { BlobStorage, Workspace } from '@blocksuite/store';
|
||||
import { BaseProvider } from './provider/base';
|
||||
import { LocalProvider } from './provider/local/local';
|
||||
import { AffineProvider } from './provider';
|
||||
import type { Workspace as WS, WorkspaceMeta } from './types';
|
||||
import type { WorkspaceMeta } from './types';
|
||||
import assert from 'assert';
|
||||
import { getLogger } from './logger';
|
||||
import { BlockSchema } from '@blocksuite/blocks/models';
|
||||
@ -17,6 +17,7 @@ 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.
|
||||
*/
|
||||
@ -34,12 +35,14 @@ 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,
|
||||
})
|
||||
);
|
||||
|
||||
@ -247,6 +250,24 @@ export class DataCenter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* get user info by email
|
||||
* @param workspaceId
|
||||
* @param email
|
||||
* @param provider
|
||||
* @returns {Promise<User>} User info
|
||||
*/
|
||||
public async getUserByEmail(
|
||||
workspaceId: string,
|
||||
email: string,
|
||||
provider = 'affine'
|
||||
) {
|
||||
const providerInstance = this.providerMap.get(provider);
|
||||
if (providerInstance) {
|
||||
return await providerInstance.getUserByEmail(workspaceId, email);
|
||||
}
|
||||
}
|
||||
|
||||
private async _transWorkspaceProvider(
|
||||
workspace: Workspace,
|
||||
providerId: string
|
||||
@ -312,4 +333,22 @@ export class DataCenter {
|
||||
id;
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import { getAuthorizer } from './apis/token';
|
||||
import { WebsocketProvider } from './sync';
|
||||
import { IndexedDBProvider } from '../indexeddb';
|
||||
import { getDefaultHeadImgBlob } from '../../utils';
|
||||
import { getUserByEmail } from './apis/user';
|
||||
|
||||
export class AffineProvider extends BaseProvider {
|
||||
public id = 'affine';
|
||||
@ -248,7 +249,8 @@ export class AffineProvider extends BaseProvider {
|
||||
if (!meta.avatar) {
|
||||
// set default avatar
|
||||
const blob = await getDefaultHeadImgBlob(meta.name);
|
||||
meta.avatar = (await this.setBlob(blob)) || '';
|
||||
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');
|
||||
@ -277,4 +279,19 @@ export class AffineProvider extends BaseProvider {
|
||||
public override async publish(id: string, isPublish: boolean): Promise<void> {
|
||||
await updateWorkspace({ id, public: isPublish });
|
||||
}
|
||||
|
||||
public override async getUserByEmail(
|
||||
workspace_id: string,
|
||||
email: string
|
||||
): Promise<User | null> {
|
||||
const user = await getUserByEmail({ workspace_id, email });
|
||||
return user
|
||||
? {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
avatar: user.avatar_url,
|
||||
email: user.email,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ const defaultLogger = () => {
|
||||
export interface ProviderConstructorParams {
|
||||
logger?: Logger;
|
||||
workspaces: WorkspacesScope;
|
||||
blobs: BlobStorage;
|
||||
}
|
||||
|
||||
export class BaseProvider {
|
||||
@ -17,9 +18,10 @@ export class BaseProvider {
|
||||
protected _logger!: Logger;
|
||||
protected _blobs!: BlobStorage;
|
||||
|
||||
public constructor({ logger, workspaces }: ProviderConstructorParams) {
|
||||
public constructor({ logger, workspaces, blobs }: ProviderConstructorParams) {
|
||||
this._logger = (logger || defaultLogger) as Logger;
|
||||
this._workspaces = workspaces;
|
||||
this._blobs = blobs;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -149,10 +151,25 @@ export class BaseProvider {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* create workspace by workspace meta
|
||||
* @param {WorkspaceMeta} meta
|
||||
*/
|
||||
public async createWorkspace(
|
||||
meta: WorkspaceMeta
|
||||
): Promise<Workspace | undefined> {
|
||||
meta;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* get user by email
|
||||
* @param {string} id
|
||||
* @param {string} email
|
||||
* @returns
|
||||
*/
|
||||
public async getUserByEmail(id: string, email: string): Promise<User | null> {
|
||||
email;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -82,7 +82,8 @@ export class LocalProvider extends BaseProvider {
|
||||
if (!meta.avatar) {
|
||||
// set default avatar
|
||||
const blob = await getDefaultHeadImgBlob(meta.name);
|
||||
meta.avatar = (await this.setBlob(blob)) || '';
|
||||
const blobId = await this.setBlob(blob);
|
||||
meta.avatar = (await this.getBlob(blobId)) || '';
|
||||
}
|
||||
this._logger('Creating affine workspace');
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user