Merge branch 'feat/poc' into feat/datacenter

This commit is contained in:
DiamondThree 2023-01-09 21:43:50 +08:00
commit 48940b746e
12 changed files with 174 additions and 92 deletions

View File

@ -1,6 +1,6 @@
import { Workspaces } from './workspaces';
import type { WorkspacesChangeEvent } from './workspaces';
import { Workspace } from '@blocksuite/store';
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
import { BaseProvider } from './provider/base';
import { LocalProvider } from './provider/local/local';
import { AffineProvider } from './provider';
@ -17,7 +17,7 @@ import { applyUpdate, encodeStateAsUpdate } from 'yjs';
export class DataCenter {
private readonly _workspaces = new Workspaces();
private readonly _logger = getLogger('dc');
private _workspaceInstances: Map<string, Workspace> = new Map();
private _workspaceInstances: Map<string, BlocksuiteWorkspace> = new Map();
/**
* A mainProvider must exist as the only data trustworthy source.
*/
@ -114,7 +114,7 @@ export class DataCenter {
assert(workspaceInfo, 'Workspace not found');
return (
this._workspaceInstances.get(workspaceId) ||
new Workspace({
new BlocksuiteWorkspace({
room: workspaceId,
}).register(BlockSchema)
);
@ -143,7 +143,7 @@ export class DataCenter {
/**
* load workspace instance by id
* @param {string} workspaceId workspace id
* @returns {Promise<Workspace>}
* @returns {Promise<BlocksuiteWorkspace>}
*/
public async loadWorkspace(workspaceId: string) {
const workspaceInfo = this._workspaces.find(workspaceId);
@ -185,11 +185,11 @@ export class DataCenter {
/**
* change workspaces meta
* @param {WorkspaceMeta} workspaceMeta workspace meta
* @param {Workspace} workspace workspace instance
* @param {BlocksuiteWorkspace} workspace workspace instance
*/
public async resetWorkspaceMeta(
{ name, avatar }: WorkspaceMeta,
workspace: Workspace
workspace: BlocksuiteWorkspace
) {
assert(workspace?.room, 'No workspace to set meta');
const update: Partial<WorkspaceMeta> = {};
@ -273,7 +273,7 @@ export class DataCenter {
}
private async _transWorkspaceProvider(
workspace: Workspace,
workspace: BlocksuiteWorkspace,
providerId: string
) {
assert(workspace.room, 'No workspace id');
@ -306,7 +306,7 @@ export class DataCenter {
* Enable workspace cloud
* @param {string} id ID of workspace.
*/
public async enableWorkspaceCloud(workspace: Workspace) {
public async enableWorkspaceCloud(workspace: BlocksuiteWorkspace) {
assert(workspace?.room, 'No workspace to enable cloud');
return await this._transWorkspaceProvider(workspace, 'affine');
}
@ -344,7 +344,10 @@ export class DataCenter {
* @param id
* @returns {Promise<string | null>} blob url
*/
async getBlob(workspace: Workspace, id: string): Promise<string | null> {
async getBlob(
workspace: BlocksuiteWorkspace,
id: string
): Promise<string | null> {
const blob = await workspace.blobs;
return (await blob?.get(id)) || '';
}
@ -354,7 +357,7 @@ export class DataCenter {
* @param id
* @returns {Promise<string | null>} blob url
*/
async setBlob(workspace: Workspace, blob: Blob): Promise<string> {
async setBlob(workspace: BlocksuiteWorkspace, blob: Blob): Promise<string> {
const blobStorage = await workspace.blobs;
return (await blobStorage?.set(blob)) || '';
}

View File

@ -26,6 +26,5 @@ const _initializeDataCenter = () => {
export const getDataCenter = _initializeDataCenter();
export type { AccessTokenMessage } from './provider/affine/apis';
export * from './types';
export { DataCenter } from './datacenter';
export type { WorkspaceInfo } from './types';
export { getLogger } from './logger';

View File

@ -0,0 +1,53 @@
import { test, expect } from '@playwright/test';
import { AffineProvider } from '../affine.js';
import { Workspaces } from '../../../workspaces/index.js';
import { apis } from './mock-apis.js';
import 'fake-indexeddb/auto';
test.describe.serial('affine provider', async () => {
const workspaces = new Workspaces();
const provider = new AffineProvider({
workspaces: workspaces.createScope(),
apis,
});
await provider.auth();
const workspaceName = 'workspace-test';
let workspaceId: string | undefined;
test('create workspace', async () => {
const w = await provider.createWorkspace({
name: workspaceName,
avatar: 'avatar-url-test',
});
workspaceId = w?.room;
expect(workspaces.workspaces.length).toEqual(1);
expect(workspaces.workspaces[0].name).toEqual(workspaceName);
});
// test('workspace list cache', async () => {
// const workspaces1 = new Workspaces();
// const provider1 = new AffineProvider({
// workspaces: workspaces1.createScope(),
// });
// await provider1.loadWorkspaces();
// expect(workspaces1.workspaces.length).toEqual(1);
// expect(workspaces1.workspaces[0].name).toEqual(workspaceName);
// expect(workspaces1.workspaces[0].id).toEqual(workspaceId);
// });
// test('update workspace', async () => {
// await provider.updateWorkspaceMeta(workspaceId!, {
// name: '1111',
// });
// expect(workspaces.workspaces[0].name).toEqual('1111');
// });
// test('delete workspace', async () => {
// expect(workspaces.workspaces.length).toEqual(1);
// await provider.deleteWorkspace(workspaces.workspaces[0].id);
// expect(workspaces.workspaces.length).toEqual(0);
// });
});

View File

@ -0,0 +1,19 @@
import type { Apis, AccessTokenMessage } from '../apis';
const user: AccessTokenMessage = {
create_at: Date.now(),
exp: 100000000,
email: 'demo@demo.demo',
id: '123',
name: 'demo',
avatar_url: 'demo-avatar-url',
};
export const apis = {
signInWithGoogle: () => {
return Promise.resolve(user);
},
createWorkspace: mate => {
return Promise.resolve({ id: 'test' });
},
} as Apis;

View File

@ -1,18 +1,16 @@
import { BaseProvider } from '../base';
import { BaseProvider } from '../base.js';
import type { ProviderConstructorParams } from '../base';
import { User, Workspace as WS, WorkspaceMeta } from '../../types';
import { Workspace } from '@blocksuite/store';
import type { User, WorkspaceInfo, WorkspaceMeta } from '../../types';
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
import { BlockSchema } from '@blocksuite/blocks/models';
import { applyUpdate } from 'yjs';
import { token, Callback } from './apis';
import { varStorage as storage } from 'lib0/storage';
import { storage } from './storage.js';
import assert from 'assert';
import { getAuthorizer } from './apis/token';
import { WebsocketProvider } from './sync';
import { WebsocketProvider } from './sync.js';
// import { IndexedDBProvider } from '../local/indexeddb';
import { getDefaultHeadImgBlob } from '../../utils';
import { getDefaultHeadImgBlob } from '../../utils/index.js';
import { getApis } from './apis/index.js';
import type { Apis, WorkspaceDetail } from './apis';
import type { Apis, WorkspaceDetail, Callback } from './apis';
export interface AffineProviderConstructorParams
extends ProviderConstructorParams {
@ -21,10 +19,8 @@ export interface AffineProviderConstructorParams
export class AffineProvider extends BaseProvider {
public id = 'affine';
private _workspacesCache: Map<string, Workspace> = new Map();
private _workspacesCache: Map<string, BlocksuiteWorkspace> = new Map();
private _onTokenRefresh?: Callback = undefined;
private readonly _authorizer = getAuthorizer();
private _user: User | undefined = undefined;
private _wsMap: Map<string, WebsocketProvider> = new Map();
private _apis: Apis;
// private _idbMap: Map<string, IndexedDBProvider> = new Map();
@ -36,38 +32,38 @@ export class AffineProvider extends BaseProvider {
override async init() {
this._onTokenRefresh = () => {
if (token.refresh) {
storage.setItem('token', token.refresh);
if (this._apis.token.refresh) {
storage.setItem('token', this._apis.token.refresh);
}
};
token.onChange(this._onTokenRefresh);
this._apis.token.onChange(this._onTokenRefresh);
// initial login token
if (token.isExpired) {
if (this._apis.token.isExpired) {
try {
const refreshToken = storage.getItem('token');
await token.refreshToken(refreshToken);
await this._apis.token.refreshToken(refreshToken);
if (token.refresh) {
storage.set('token', token.refresh);
if (this._apis.token.refresh) {
storage.set('token', this._apis.token.refresh);
}
assert(token.isLogin);
assert(this._apis.token.isLogin);
} catch (_) {
// this._logger('Authorization failed, fallback to local mode');
}
} else {
storage.setItem('token', token.refresh);
storage.setItem('token', this._apis.token.refresh);
}
}
override async warpWorkspace(workspace: Workspace) {
override async warpWorkspace(workspace: BlocksuiteWorkspace) {
const { doc, room } = workspace;
assert(room);
this.linkLocal(workspace);
const updates = await this._apis.downloadWorkspace(room);
if (updates) {
if (updates && updates.byteLength) {
await new Promise(resolve => {
doc.once('update', resolve);
applyUpdate(doc, new Uint8Array(updates));
@ -97,11 +93,11 @@ export class AffineProvider extends BaseProvider {
}
override async loadWorkspaces() {
if (!token.isLogin) {
if (!this._apis.token.isLogin) {
return [];
}
const workspacesList = await this._apis.getWorkspaces();
const workspaces: WS[] = workspacesList.map(w => {
const workspaces: WorkspaceInfo[] = workspacesList.map(w => {
return {
...w,
memberCount: 0,
@ -112,12 +108,12 @@ export class AffineProvider extends BaseProvider {
const workspaceInstances = workspaces.map(({ id }) => {
const workspace =
this._workspacesCache.get(id) ||
new Workspace({
new BlocksuiteWorkspace({
room: id,
}).register(BlockSchema);
this._workspacesCache.set(id, workspace);
if (workspace) {
return new Promise<Workspace>(resolve => {
return new Promise<BlocksuiteWorkspace>(resolve => {
this._apis.downloadWorkspace(id).then(data => {
applyUpdate(workspace.doc, new Uint8Array(data));
resolve(workspace);
@ -177,24 +173,25 @@ export class AffineProvider extends BaseProvider {
override async auth() {
const refreshToken = await storage.getItem('token');
if (refreshToken) {
await token.refreshToken(refreshToken);
if (token.isLogin && !token.isExpired) {
await this._apis.token.refreshToken(refreshToken);
if (this._apis.token.isLogin && !this._apis.token.isExpired) {
// login success
return;
}
}
const user = await this._authorizer[0]?.();
assert(user);
this._user = {
id: user.id,
name: user.name,
avatar: user.avatar_url,
email: user.email,
};
await this._apis.signInWithGoogle?.();
}
public override async getUserInfo(): Promise<User | undefined> {
return this._user;
const user = this._apis.token.user;
return user
? {
id: user.id,
name: user.name,
avatar: user.avatar_url,
email: user.email,
}
: undefined;
}
public override async deleteWorkspace(id: string): Promise<void> {
@ -237,7 +234,7 @@ export class AffineProvider extends BaseProvider {
return await this._apis.removeMember({ permissionId });
}
public override async linkLocal(workspace: Workspace) {
public override async linkLocal(workspace: BlocksuiteWorkspace) {
return workspace;
// assert(workspace.room);
// let idb = this._idbMap.get(workspace.room);
@ -251,19 +248,19 @@ export class AffineProvider extends BaseProvider {
public override async createWorkspace(
meta: WorkspaceMeta
): Promise<Workspace | undefined> {
): Promise<BlocksuiteWorkspace | undefined> {
assert(meta.name, 'Workspace name is required');
const { id } = await this._apis.createWorkspace(
meta as Required<WorkspaceMeta>
);
this._logger('Creating affine workspace');
const nw = new Workspace({
const nw = new BlocksuiteWorkspace({
room: id,
}).register(BlockSchema);
nw.meta.setName(meta.name);
this.linkLocal(nw);
const workspaceInfo: WS = {
const workspaceInfo: WorkspaceInfo = {
name: meta.name,
id,
isPublish: false,
@ -271,7 +268,7 @@ export class AffineProvider extends BaseProvider {
owner: undefined,
isLocal: true,
memberCount: 1,
provider: 'local',
provider: 'affine',
};
if (!meta.avatar) {

View File

@ -1,15 +1,16 @@
export { token } from './token.js';
// export { token } from './token.js';
export type { Callback } from './token.js';
import { getAuthorizer } from './token.js';
import * as user from './user.js';
import * as workspace from './workspace.js';
import { token } from './token.js';
export type Apis = typeof user &
typeof workspace & {
Omit<typeof workspace, 'WorkspaceType' | 'PermissionType'> & {
signInWithGoogle: ReturnType<typeof getAuthorizer>[0];
onAuthStateChanged: ReturnType<typeof getAuthorizer>[1];
};
} & { token: typeof token };
export const getApis = (): Apis => {
const [signInWithGoogle, onAuthStateChanged] = getAuthorizer();
@ -18,6 +19,7 @@ export const getApis = (): Apis => {
...workspace,
signInWithGoogle,
onAuthStateChanged,
token,
};
};

View File

@ -2,8 +2,8 @@ import { initializeApp } from 'firebase/app';
import { getAuth, GoogleAuthProvider, signInWithPopup } from 'firebase/auth';
import type { User } from 'firebase/auth';
import { getLogger } from '../../../logger';
import { bareClient } from './request';
import { getLogger } from '../../../logger.js';
import { bareClient } from './request.js';
export interface AccessTokenMessage {
create_at: number;
@ -46,6 +46,10 @@ class Token {
this._setToken(); // fill with default value
}
get user() {
return this._user;
}
private _setToken(login?: LoginResponse) {
this._accessToken = login?.token || '';
this._refreshToken = login?.refresh || '';

View File

@ -0,0 +1 @@
export { varStorage as storage } from 'lib0/storage';

View File

@ -1,5 +1,5 @@
import { BlobStorage, Workspace } from '@blocksuite/store';
import { Logger, User, Workspace as WS, WorkspaceMeta } from '../types';
import { Workspace as BlocksuiteWorkspace } from '@blocksuite/store';
import { Logger, User, WorkspaceInfo, WorkspaceMeta } from '../types';
import type { WorkspacesScope } from '../workspaces';
const defaultLogger = () => {
@ -47,14 +47,16 @@ export class BaseProvider {
* @param workspace
* @returns
*/
public async warpWorkspace(workspace: Workspace): Promise<Workspace> {
public async warpWorkspace(
workspace: BlocksuiteWorkspace
): Promise<BlocksuiteWorkspace> {
return workspace;
}
/**
* load workspaces
**/
public async loadWorkspaces(): Promise<WS[]> {
public async loadWorkspaces(): Promise<WorkspaceInfo[]> {
throw new Error(`provider: ${this.id} loadWorkSpace Not implemented`);
}
@ -154,7 +156,7 @@ export class BaseProvider {
*/
public async createWorkspace(
meta: WorkspaceMeta
): Promise<Workspace | undefined> {
): Promise<BlocksuiteWorkspace | undefined> {
meta;
return;
}
@ -175,7 +177,9 @@ export class BaseProvider {
* @param workspace
* @returns
*/
public async linkLocal(workspace: Workspace): Promise<Workspace> {
public async linkLocal(
workspace: BlocksuiteWorkspace
): Promise<BlocksuiteWorkspace> {
return workspace;
}
}

View File

@ -1,8 +1,8 @@
import { BaseProvider } from '../base.js';
import type { ProviderConstructorParams } from '../base';
import { varStorage as storage } from 'lib0/storage';
import { Workspace as WS, WorkspaceMeta } from '../../types';
import { Workspace, uuidv4 } from '@blocksuite/store';
import { WorkspaceInfo, WorkspaceMeta } from '../../types';
import { Workspace as BlocksuiteWorkspace, uuidv4 } from '@blocksuite/store';
import { IndexedDBProvider } from './indexeddb.js';
import assert from 'assert';
import { getDefaultHeadImgBlob } from '../../utils/index.js';
@ -18,11 +18,11 @@ export class LocalProvider extends BaseProvider {
this.loadWorkspaces();
}
private _storeWorkspaces(workspaces: WS[]) {
private _storeWorkspaces(workspaces: WorkspaceInfo[]) {
storage.setItem(WORKSPACE_KEY, JSON.stringify(workspaces));
}
public override async linkLocal(workspace: Workspace) {
public override async linkLocal(workspace: BlocksuiteWorkspace) {
assert(workspace.room);
let idb = this._idbMap.get(workspace.room);
if (!idb) {
@ -34,19 +34,19 @@ export class LocalProvider extends BaseProvider {
}
public override async warpWorkspace(
workspace: Workspace
): Promise<Workspace> {
workspace: BlocksuiteWorkspace
): Promise<BlocksuiteWorkspace> {
assert(workspace.room);
await this.linkLocal(workspace);
return workspace;
}
override loadWorkspaces(): Promise<WS[]> {
override loadWorkspaces(): Promise<WorkspaceInfo[]> {
const workspaceStr = storage.getItem(WORKSPACE_KEY);
let workspaces: WS[] = [];
let workspaces: WorkspaceInfo[] = [];
if (workspaceStr) {
try {
workspaces = JSON.parse(workspaceStr) as WS[];
workspaces = JSON.parse(workspaceStr) as WorkspaceInfo[];
workspaces.forEach(workspace => {
this._workspaces.add(workspace);
});
@ -78,11 +78,11 @@ export class LocalProvider extends BaseProvider {
public override async createWorkspace(
meta: WorkspaceMeta
): Promise<Workspace | undefined> {
): Promise<BlocksuiteWorkspace | undefined> {
assert(meta.name, 'Workspace name is required');
this._logger('Creating affine workspace');
const workspaceInfo: WS = {
const workspaceInfo: WorkspaceInfo = {
name: meta.name,
id: uuidv4(),
isPublish: false,
@ -93,8 +93,8 @@ export class LocalProvider extends BaseProvider {
provider: 'local',
};
const workspace = new Workspace({ room: workspaceInfo.id });
this.linkLocal(workspace);
const workspace = new BlocksuiteWorkspace({ room: workspaceInfo.id });
await this.linkLocal(workspace);
workspace.meta.setName(meta.name);
if (!meta.avatar) {
// set default avatar

View File

@ -1,6 +1,6 @@
import { getLogger } from '../logger';
export type Workspace = {
export type WorkspaceInfo = {
name: string;
id: string;
isPublish?: boolean;
@ -18,6 +18,6 @@ export type User = {
avatar: string;
};
export type WorkspaceMeta = Pick<Workspace, 'name' | 'avatar'>;
export type WorkspaceMeta = Pick<WorkspaceInfo, 'name' | 'avatar'>;
export type Logger = ReturnType<typeof getLogger>;

View File

@ -1,25 +1,25 @@
import { Observable } from 'lib0/observable';
import type { Workspace, WorkspaceMeta } from '../types';
import type { WorkspaceInfo, WorkspaceMeta } from '../types';
export interface WorkspacesScope {
get: (workspaceId: string) => Workspace | undefined;
list: () => Workspace[];
add: (workspace: Workspace) => void;
get: (workspaceId: string) => WorkspaceInfo | undefined;
list: () => WorkspaceInfo[];
add: (workspace: WorkspaceInfo) => void;
remove: (workspaceId: string) => boolean;
clear: () => void;
update: (workspaceId: string, workspaceMeta: Partial<WorkspaceMeta>) => void;
}
export interface WorkspacesChangeEvent {
added?: Workspace;
deleted?: Workspace;
updated?: Workspace;
added?: WorkspaceInfo;
deleted?: WorkspaceInfo;
updated?: WorkspaceInfo;
}
export class Workspaces extends Observable<'change'> {
private _workspacesMap = new Map<string, Workspace>();
private _workspacesMap = new Map<string, WorkspaceInfo>();
get workspaces(): Workspace[] {
get workspaces(): WorkspaceInfo[] {
return Array.from(this._workspacesMap.values());
}
@ -37,7 +37,7 @@ export class Workspaces extends Observable<'change'> {
return this._workspacesMap.get(workspaceId);
};
const add = (workspace: Workspace) => {
const add = (workspace: WorkspaceInfo) => {
if (this._workspacesMap.has(workspace.id)) {
throw new Error(`Duplicate workspace id.`);
}
@ -105,7 +105,7 @@ export class Workspaces extends Observable<'change'> {
// TODO: need to optimize
const list = () => {
const workspaces: Workspace[] = [];
const workspaces: WorkspaceInfo[] = [];
scopedWorkspaceIds.forEach(id => {
const workspace = this._workspacesMap.get(id);
if (workspace) {