Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

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

alt0c7f974ed7791
diff --cc packages/data-center/src/provider/affine/affine.ts
index 572c05b72c,1b69259258..9fab104f2a
--- a/packages/data-center/src/provider/affine/affine.ts
+++ b/packages/data-center/src/provider/affine/affine.ts
@@@ -10,19 -11,19 +10,26 @@@ 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, Workspace } from './apis/index.js';
import type { Apis, WorkspaceDetail, Callback } from './apis';
import { setDefaultAvatar } from '../utils.js';
-import { MessageCode } from '../../message';
+import { MessageCode } from '../../message/index.js';
import { token } from './apis/token.js';
import { WebsocketClient } from './channel';
-import { SyncMode } from '../../workspace-unit';
+import {
+ loadWorkspaceUnit,
+ createWorkspaceUnit,
+ syncToCloud,
+} from './utils.js';
+import { WorkspaceUnit } from '../../workspace-unit.js';
+import { createBlocksuiteWorkspace } from '../../utils/index.js';
++import type { SyncMode } from '../../workspace-unit';
+
+ type ChannelMessage = {
+ ws_list: Workspace[];
+ ws_details: Record<string, WorkspaceDetail>;
+ metadata: Record<string, { avatar: string; name: string }>;
+ };
export interface AffineProviderConstructorParams
extends ProviderConstructorParams {
@@@ -85,11 -81,48 +87,52 @@@ export class AffineProvider extends Bas
}
private _connectChannel() {
- this._channel.connect();
+ if (!this._channel) {
+ this._channel = new WebsocketClient(
+ `${window.location.protocol === 'https:' ? 'wss' : 'ws'}://${
+ window.location.host
+ }/api/global/sync/`,
+ this._logger,
+ {
+ params: {
+ token: this._apis.token.refresh,
+ },
+ }
+ );
+ }
+ this._channel.on('message', (msg: ChannelMessage) => {
- this.handlerAffineListMessage(msg);
++ this._handlerAffineListMessage(msg);
+ });
+ }
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- this._channel.on('message', (message: any) => {
- console.log('message', message);
- private handlerAffineListMessage({ ws_details, metadata }: ChannelMessage) {
++ private _handlerAffineListMessage({ ws_details, metadata }: ChannelMessage) {
+ this._logger('receive server message');
- Object.entries(ws_details).forEach(([id, detail]) => {
++ Object.entries(ws_details).forEach(async ([id, detail]) => {
+ const { name, avatar } = metadata[id];
+ assert(name);
+ const workspace = {
+ name: name,
+ avatar,
+ owner: {
+ name: detail.owner.name,
+ id: detail.owner.id,
+ email: detail.owner.email,
+ avatar: detail.owner.avatar_url,
+ },
+ published: detail.public,
+ memberCount: detail.member_count,
+ provider: 'affine',
+ syncMode: 'core' as SyncMode,
+ };
+ if (this._workspaces.get(id)) {
+ this._workspaces.update(id, workspace);
+ } else {
- this._workspaces.add({ id, ...workspace });
++ const workspaceUnit = await loadWorkspaceUnit(
++ { id, ...workspace },
++ this._apis
++ );
++ this._workspaces.add(workspaceUnit);
+ }
});
}