From eab86dffb8a8ce01b1a6333cd2353d7f6980d82d Mon Sep 17 00:00:00 2001 From: MingLiang Wang Date: Tue, 10 Jan 2023 22:44:49 +0800 Subject: [PATCH 1/6] feat: add logout --- packages/data-center/src/datacenter.ts | 8 ++++---- packages/data-center/src/provider/affine/affine.ts | 8 +++++++- packages/data-center/src/provider/affine/apis/token.ts | 4 ++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index 8e70718c36..64ead6cfd9 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -43,8 +43,8 @@ export class DataCenter { }; }; // TODO: switch different provider - dc.registerProvider(new LocalProvider(getInitParams())); - dc.registerProvider(new AffineProvider(getInitParams())); + await dc.registerProvider(new LocalProvider(getInitParams())); + await dc.registerProvider(new AffineProvider(getInitParams())); for (const provider of dc.providerMap.values()) { await provider.loadWorkspaces(); @@ -57,12 +57,12 @@ export class DataCenter { * Register provider. * We will automatically set the first provider to default provider. */ - registerProvider(provider: BaseProvider) { + async registerProvider(provider: BaseProvider) { if (!this._mainProvider) { this._mainProvider = provider; } - provider.init(); + await provider.init(); this.providerMap.set(provider.id, provider); } diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts index 890fc6dc00..0480a60a1b 100644 --- a/packages/data-center/src/provider/affine/affine.ts +++ b/packages/data-center/src/provider/affine/affine.ts @@ -16,7 +16,7 @@ import { getApis } from './apis/index.js'; import type { Apis, WorkspaceDetail, Callback } from './apis'; import { setDefaultAvatar } from '../utils.js'; import { MessageCode } from '../../message'; -import { blob } from 'stream/consumers'; +import { token } from './apis/token.js'; export interface AffineProviderConstructorParams extends ProviderConstructorParams { @@ -213,6 +213,7 @@ export class AffineProvider extends BaseProvider { public override async getUserInfo(): Promise { const user = this._apis.token.user; + await this.init; return user ? { id: user.id, @@ -359,4 +360,9 @@ export class AffineProvider extends BaseProvider { }); return to; } + + public override async logout(): Promise { + token.clear(); + storage.removeItem('token'); + } } diff --git a/packages/data-center/src/provider/affine/apis/token.ts b/packages/data-center/src/provider/affine/apis/token.ts index 62da8def94..4df48b3075 100644 --- a/packages/data-center/src/provider/affine/apis/token.ts +++ b/packages/data-center/src/provider/affine/apis/token.ts @@ -140,6 +140,10 @@ class Token { this.callbacks.splice(index, 1); } } + + clear() { + this._setToken(); + } } export const token = new Token(); From cdf07fd7c2ecce72e60bc97050c725e9965a5435 Mon Sep 17 00:00:00 2001 From: MingLiang Wang Date: Tue, 10 Jan 2023 23:01:50 +0800 Subject: [PATCH 2/6] feat: remove workspaces pool --- packages/data-center/src/datacenter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index 64ead6cfd9..76499b11e5 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -123,7 +123,7 @@ export class DataCenter { const workspaceInfo = this._workspaceUnitCollection.find(workspaceId); assert(workspaceInfo, 'Workspace not found'); return ( - this._workspaceInstances.get(workspaceId) || + // this._workspaceInstances.get(workspaceId) || createBlocksuiteWorkspace(workspaceId) ); } From 48264edf9184bd409b443d6014a6a2c2dab0e3d1 Mon Sep 17 00:00:00 2001 From: MingLiang Wang Date: Wed, 11 Jan 2023 12:34:57 +0800 Subject: [PATCH 3/6] feat: add query members --- packages/data-center/src/datacenter.ts | 14 ++++++++++++++ packages/data-center/src/provider/affine/affine.ts | 6 +++++- packages/data-center/src/provider/base.ts | 11 +++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/data-center/src/datacenter.ts b/packages/data-center/src/datacenter.ts index 76499b11e5..1221b114f3 100644 --- a/packages/data-center/src/datacenter.ts +++ b/packages/data-center/src/datacenter.ts @@ -384,6 +384,20 @@ export class DataCenter { return (await blobStorage?.set(blob)) || ''; } + /** + * get members of a workspace + * @param workspaceId + */ + async getMembers(workspaceId: string) { + const workspaceInfo = this._workspaceUnitCollection.find(workspaceId); + assert(workspaceInfo, 'Workspace not found'); + const provider = this.providerMap.get(workspaceInfo.provider); + if (provider) { + return await provider.getWorkspaceMembers(workspaceId); + } + return []; + } + onMessage(cb: (message: Message) => void) { return this._messageCenter.onMessage(cb); } diff --git a/packages/data-center/src/provider/affine/affine.ts b/packages/data-center/src/provider/affine/affine.ts index 0480a60a1b..0794ca10e9 100644 --- a/packages/data-center/src/provider/affine/affine.ts +++ b/packages/data-center/src/provider/affine/affine.ts @@ -12,7 +12,7 @@ import { storage } from './storage.js'; import assert from 'assert'; import { WebsocketProvider } from './sync.js'; // import { IndexedDBProvider } from '../local/indexeddb'; -import { getApis } from './apis/index.js'; +import { getApis, Member } from './apis/index.js'; import type { Apis, WorkspaceDetail, Callback } from './apis'; import { setDefaultAvatar } from '../utils.js'; import { MessageCode } from '../../message'; @@ -365,4 +365,8 @@ export class AffineProvider extends BaseProvider { token.clear(); storage.removeItem('token'); } + + public override async getWorkspaceMembers(id: string) { + return this._apis.getWorkspaceMembers({ id }); + } } diff --git a/packages/data-center/src/provider/base.ts b/packages/data-center/src/provider/base.ts index 6542b279f1..17834abf34 100644 --- a/packages/data-center/src/provider/base.ts +++ b/packages/data-center/src/provider/base.ts @@ -3,6 +3,7 @@ import { MessageCenter } from '../message'; import { Logger, User } from '../types'; import type { WorkspaceUnitCollectionScope } from '../workspace-unit-collection'; import type { WorkspaceUnitCtorParams } from '../workspace-unit'; +import { Member } from './affine/apis'; const defaultLogger = () => { return; @@ -213,4 +214,14 @@ export class BaseProvider { from; return to; } + + /** + * get workspace members + * @param {string} workspaceId + * @returns + */ + public getWorkspaceMembers(workspaceId: string): Promise { + workspaceId; + return Promise.resolve([]); + } } From 57b204b6e2b7daacc385ea5d75b618cc40cfc03a Mon Sep 17 00:00:00 2001 From: alt0 Date: Wed, 11 Jan 2023 13:52:56 +0800 Subject: [PATCH 4/6] test: fix test case --- packages/data-center/src/workspace-unit-collection.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/data-center/src/workspace-unit-collection.spec.ts b/packages/data-center/src/workspace-unit-collection.spec.ts index 06eee95bd0..f010c31d12 100644 --- a/packages/data-center/src/workspace-unit-collection.spec.ts +++ b/packages/data-center/src/workspace-unit-collection.spec.ts @@ -11,7 +11,7 @@ test.describe.serial('workspace meta collection observable', () => { workspaceUnitCollection.once( 'change', (event: WorkspaceUnitCollectionChangeEvent) => { - expect(event.added?.id).toEqual('123'); + expect(event.added?.[0]?.id).toEqual('123'); } ); scope.add({ From 3f6af0a96425de0620c26b1b696c24fe39349411 Mon Sep 17 00:00:00 2001 From: DiamondThree <857159145@qq.com> Date: Wed, 11 Jan 2023 14:35:14 +0800 Subject: [PATCH 5/6] feat:member list --- .../components/workspace-setting/MembersPage.tsx | 13 ++++++++++--- packages/app/src/hooks/use-workspace-helper.ts | 8 +++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/app/src/components/workspace-setting/MembersPage.tsx b/packages/app/src/components/workspace-setting/MembersPage.tsx index 08b2640c97..c2a57032ee 100644 --- a/packages/app/src/components/workspace-setting/MembersPage.tsx +++ b/packages/app/src/components/workspace-setting/MembersPage.tsx @@ -28,13 +28,20 @@ import { WorkspaceUnit } from '@affine/datacenter'; import { useTemporaryHelper } from '@/providers/temporary-helper-provider'; import { StyledMemberWarp } from './general/style'; import { useConfirm } from '@/providers/ConfirmProvider'; +import { useAppState } from '@/providers/app-state-provider'; // import { useAppState } from '@/providers/app-state-provider'; export const MembersPage = ({ workspace }: { workspace: WorkspaceUnit }) => { const [isInviteModalShow, setIsInviteModalShow] = useState(false); - const [members, setMembers] = useState<[{ name: string; email: string }]>([ - { name: 'affine', email: 'tttt' }, - ]); + + const { currentMetaWorkSpace, currentWorkspace, dataCenter } = useAppState(); + + const [members, setMembers] = useState<[{ name: string; email: string }]>(); + + // const getMembers = async () =>{ + // const members = await dataCenter. + // } + console.log('setMembers: ', setMembers); const { user, login, updateWorkspaceMeta } = useTemporaryHelper(); const { confirm } = useConfirm(); diff --git a/packages/app/src/hooks/use-workspace-helper.ts b/packages/app/src/hooks/use-workspace-helper.ts index af5fb5ad71..eb26794105 100644 --- a/packages/app/src/hooks/use-workspace-helper.ts +++ b/packages/app/src/hooks/use-workspace-helper.ts @@ -6,7 +6,8 @@ import router from 'next/router'; export const useWorkspaceHelper = () => { const { confirm } = useConfirm(); - const { dataCenter, currentWorkspace, user, login } = useAppState(); + const { dataCenter, currentWorkspace, user, login, currentMetaWorkSpace } = + useAppState(); const createWorkspace = async (name: string) => { const workspaceInfo = await dataCenter.createWorkspace({ name: name, @@ -57,6 +58,11 @@ export const useWorkspaceHelper = () => { }); }; + const inviteMember = async (email: string) => { + currentMetaWorkSpace && + (await dataCenter.inviteMember(currentMetaWorkSpace?.id, email)); + }; + return { createWorkspace, publishWorkspace, From 3159d6ff1b7b5f9991947dcb9162c49ad674ef4e Mon Sep 17 00:00:00 2001 From: DiamondThree <857159145@qq.com> Date: Wed, 11 Jan 2023 14:48:50 +0800 Subject: [PATCH 6/6] fixed memeber init --- .../app/src/components/workspace-setting/MembersPage.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/app/src/components/workspace-setting/MembersPage.tsx b/packages/app/src/components/workspace-setting/MembersPage.tsx index c2a57032ee..fd65f47df8 100644 --- a/packages/app/src/components/workspace-setting/MembersPage.tsx +++ b/packages/app/src/components/workspace-setting/MembersPage.tsx @@ -36,7 +36,9 @@ export const MembersPage = ({ workspace }: { workspace: WorkspaceUnit }) => { const { currentMetaWorkSpace, currentWorkspace, dataCenter } = useAppState(); - const [members, setMembers] = useState<[{ name: string; email: string }]>(); + const [members, setMembers] = useState<[{ name: string; email: string }?]>( + [] + ); // const getMembers = async () =>{ // const members = await dataCenter. @@ -90,9 +92,9 @@ export const MembersPage = ({ workspace }: { workspace: WorkspaceUnit }) => { - {member.name} + {member?.name} - {member.email} + {member?.email}