mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-17 22:37:04 +08:00
fix: some login & enable affine cloud issues (#999)
Co-authored-by: himself65 <himself65@outlook.com>
This commit is contained in:
@@ -15,9 +15,10 @@ import {
|
||||
loadWorkspaceUnit,
|
||||
createWorkspaceUnit,
|
||||
migrateBlobDB,
|
||||
createBlocksuiteWorkspaceWithAuth,
|
||||
} from './utils.js';
|
||||
import { WorkspaceUnit } from '../../workspace-unit.js';
|
||||
import { createBlocksuiteWorkspace, applyUpdate } from '../../utils/index.js';
|
||||
import { applyUpdate } from '../../utils/index.js';
|
||||
import type { SyncMode } from '../../workspace-unit';
|
||||
import { MessageCenter } from '../../message/index.js';
|
||||
|
||||
@@ -43,7 +44,10 @@ export class AffineProvider extends BaseProvider {
|
||||
private _channel?: WebsocketClient;
|
||||
private _refreshToken?: string;
|
||||
// private _idbMap: Map<string, IndexedDBProvider> = new Map();
|
||||
private _workspaceLoadingQueue: Set<string> = new Set();
|
||||
private _workspaceLoadingQueue: Map<string, Promise<WorkspaceUnit>> =
|
||||
new Map();
|
||||
|
||||
private _workspaces$: Promise<Workspace[]> | undefined;
|
||||
|
||||
constructor({ apis, ...params }: AffineProviderConstructorParams) {
|
||||
super(params);
|
||||
@@ -141,11 +145,10 @@ export class AffineProvider extends BaseProvider {
|
||||
this._workspaces.update(id, workspace);
|
||||
} else {
|
||||
if (!this._workspaceLoadingQueue.has(id)) {
|
||||
const workspaceUnit = await loadWorkspaceUnit(
|
||||
{ id, ...workspace },
|
||||
this._apis
|
||||
);
|
||||
newlyCreatedWorkspaces.push(workspaceUnit);
|
||||
const p = loadWorkspaceUnit({ id, ...workspace }, this._apis);
|
||||
this._workspaceLoadingQueue.set(id, p);
|
||||
newlyCreatedWorkspaces.push(await p);
|
||||
this._workspaceLoadingQueue.delete(id);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -201,6 +204,9 @@ export class AffineProvider extends BaseProvider {
|
||||
}
|
||||
|
||||
override async warpWorkspace(workspace: BlocksuiteWorkspace) {
|
||||
workspace.setGettingBlobOptions(
|
||||
(k: string) => ({ api: '/api/workspace', token: this.getToken() }[k])
|
||||
);
|
||||
// FIXME: if add indexedDB cache in the future, can remove following line.
|
||||
await this._applyCloudUpdates(workspace);
|
||||
const { room } = workspace;
|
||||
@@ -229,27 +235,46 @@ export class AffineProvider extends BaseProvider {
|
||||
if (!this._apis.auth.isLogin) {
|
||||
return [];
|
||||
}
|
||||
const workspacesList = await this._apis.getWorkspaces();
|
||||
|
||||
// cache workspaces and workspaceUnits results so that simultaneous calls
|
||||
// to loadWorkspaces will not cause multiple requests
|
||||
if (!this._workspaces$) {
|
||||
this._workspaces$ = this._apis.getWorkspaces();
|
||||
}
|
||||
|
||||
const workspacesList = await this._workspaces$;
|
||||
const workspaceUnits = await Promise.all(
|
||||
workspacesList.map(w => {
|
||||
this._workspaceLoadingQueue.add(w.id);
|
||||
return loadWorkspaceUnit(
|
||||
{
|
||||
id: w.id,
|
||||
name: '',
|
||||
avatar: undefined,
|
||||
owner: undefined,
|
||||
published: w.public,
|
||||
memberCount: 1,
|
||||
provider: this.id,
|
||||
syncMode: 'core',
|
||||
},
|
||||
this._apis
|
||||
).finally(() => {
|
||||
this._workspaceLoadingQueue.delete(w.id);
|
||||
});
|
||||
workspacesList.map(async w => {
|
||||
let p = this._workspaceLoadingQueue.get(w.id);
|
||||
if (!p) {
|
||||
// may only need to load the primary one instead of all of them?
|
||||
// it will take a long time to load all of the workspaces
|
||||
// at least we shall use p-map to load them in chunks
|
||||
p = loadWorkspaceUnit(
|
||||
{
|
||||
id: w.id,
|
||||
name: '',
|
||||
avatar: undefined,
|
||||
owner: undefined,
|
||||
published: w.public,
|
||||
memberCount: 1,
|
||||
provider: this.id,
|
||||
syncMode: 'core',
|
||||
},
|
||||
this._apis
|
||||
);
|
||||
this._workspaceLoadingQueue.set(w.id, p);
|
||||
}
|
||||
const workspaceUnit = await p;
|
||||
this._workspaceLoadingQueue.delete(w.id);
|
||||
|
||||
return workspaceUnit;
|
||||
})
|
||||
);
|
||||
|
||||
// release cache
|
||||
this._workspaces$ = undefined;
|
||||
|
||||
this._workspaces.add(workspaceUnits);
|
||||
return workspaceUnits;
|
||||
}
|
||||
@@ -420,7 +445,7 @@ export class AffineProvider extends BaseProvider {
|
||||
});
|
||||
await migrateBlobDB(workspaceUnit.id, id);
|
||||
|
||||
const blocksuiteWorkspace = createBlocksuiteWorkspace(id);
|
||||
const blocksuiteWorkspace = await createBlocksuiteWorkspaceWithAuth(id);
|
||||
assert(workspaceUnit.blocksuiteWorkspace);
|
||||
await applyUpdate(
|
||||
blocksuiteWorkspace,
|
||||
|
||||
@@ -181,6 +181,7 @@ export const auth = new Auth();
|
||||
|
||||
export const getAuthorizer = () => {
|
||||
let _firebaseAuth: FirebaseAuth | null = null;
|
||||
const logger = getLogger('authorizer');
|
||||
|
||||
// getAuth will send requests on calling thus we can lazy init it
|
||||
const getAuth = () => {
|
||||
@@ -200,8 +201,7 @@ export const getAuthorizer = () => {
|
||||
}
|
||||
return _firebaseAuth;
|
||||
} catch (error) {
|
||||
getLogger('getAuthorizer')(error);
|
||||
console.error('getAuthorizer', error);
|
||||
logger(error);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,6 +11,8 @@ const _sendMessage = messageCenter.getMessageSender('affine');
|
||||
export const bareClient: KyInstance = ky.extend({
|
||||
prefixUrl: '/',
|
||||
retry: 1,
|
||||
// todo: report timeout error
|
||||
timeout: 60000,
|
||||
hooks: {
|
||||
// afterResponse: [
|
||||
// async (_request, _options, response) => {
|
||||
|
||||
@@ -1,17 +1,31 @@
|
||||
import { WorkspaceUnit } from '../../workspace-unit.js';
|
||||
import type { WorkspaceUnitCtorParams } from '../../workspace-unit';
|
||||
import { createBlocksuiteWorkspace } from '../../utils/index.js';
|
||||
import { createBlocksuiteWorkspace as _createBlocksuiteWorkspace } from '../../utils/index.js';
|
||||
import type { Apis } from './apis';
|
||||
import { setDefaultAvatar } from '../utils.js';
|
||||
import { applyUpdate } from '../../utils/index.js';
|
||||
import { getDatabase } from './idb-kv.js';
|
||||
import { auth } from './apis/auth.js';
|
||||
|
||||
export const createBlocksuiteWorkspaceWithAuth = async (id: string) => {
|
||||
if (auth.isExpired && auth.isLogin) {
|
||||
await auth.refreshToken();
|
||||
}
|
||||
return _createBlocksuiteWorkspace(id, {
|
||||
blobOptionsGetter: (k: string) =>
|
||||
// token could be expired
|
||||
({ api: '/api/workspace', token: auth.token }[k]),
|
||||
});
|
||||
};
|
||||
|
||||
export const loadWorkspaceUnit = async (
|
||||
params: WorkspaceUnitCtorParams,
|
||||
apis: Apis
|
||||
) => {
|
||||
const workspaceUnit = new WorkspaceUnit(params);
|
||||
const blocksuiteWorkspace = createBlocksuiteWorkspace(workspaceUnit.id);
|
||||
const blocksuiteWorkspace = await createBlocksuiteWorkspaceWithAuth(
|
||||
workspaceUnit.id
|
||||
);
|
||||
|
||||
const updates = await apis.downloadWorkspace(
|
||||
workspaceUnit.id,
|
||||
@@ -43,7 +57,9 @@ export const loadWorkspaceUnit = async (
|
||||
export const createWorkspaceUnit = async (params: WorkspaceUnitCtorParams) => {
|
||||
const workspaceUnit = new WorkspaceUnit(params);
|
||||
|
||||
const blocksuiteWorkspace = createBlocksuiteWorkspace(workspaceUnit.id);
|
||||
const blocksuiteWorkspace = await createBlocksuiteWorkspaceWithAuth(
|
||||
workspaceUnit.id
|
||||
);
|
||||
|
||||
blocksuiteWorkspace.meta.setName(workspaceUnit.name);
|
||||
if (!workspaceUnit.avatar) {
|
||||
|
||||
Reference in New Issue
Block a user