feat(infra): framework
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
import { WorkspaceFlavour } from '@affine/env/workspace';
|
||||
import type { WorkspaceService } from '@toeverything/infra';
|
||||
import {
|
||||
backoffRetry,
|
||||
catchErrorInto,
|
||||
effect,
|
||||
Entity,
|
||||
fromPromise,
|
||||
LiveData,
|
||||
mapInto,
|
||||
onComplete,
|
||||
onStart,
|
||||
} from '@toeverything/infra';
|
||||
import { exhaustMap } from 'rxjs';
|
||||
|
||||
import { isBackendError, isNetworkError } from '../../cloud';
|
||||
import type { WorkspacePermissionStore } from '../stores/permission';
|
||||
|
||||
const logger = new DebugLogger('affine:workspace-permission');
|
||||
|
||||
export class WorkspacePermission extends Entity {
|
||||
isOwner$ = new LiveData<boolean | null>(null);
|
||||
isLoading$ = new LiveData(false);
|
||||
error$ = new LiveData<any>(null);
|
||||
|
||||
constructor(
|
||||
private readonly workspaceService: WorkspaceService,
|
||||
private readonly store: WorkspacePermissionStore
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
revalidate = effect(
|
||||
exhaustMap(() => {
|
||||
return fromPromise(async signal => {
|
||||
if (
|
||||
this.workspaceService.workspace.flavour ===
|
||||
WorkspaceFlavour.AFFINE_CLOUD
|
||||
) {
|
||||
return await this.store.fetchIsOwner(
|
||||
this.workspaceService.workspace.id,
|
||||
signal
|
||||
);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}).pipe(
|
||||
backoffRetry({
|
||||
when: isNetworkError,
|
||||
count: Infinity,
|
||||
}),
|
||||
backoffRetry({
|
||||
when: isBackendError,
|
||||
}),
|
||||
mapInto(this.isOwner$),
|
||||
catchErrorInto(this.error$, error => {
|
||||
logger.error('Failed to fetch isOwner', error);
|
||||
}),
|
||||
onStart(() => this.isLoading$.setValue(true)),
|
||||
onComplete(() => this.isLoading$.setValue(false))
|
||||
);
|
||||
})
|
||||
);
|
||||
}
|
||||
20
packages/frontend/core/src/modules/permissions/index.ts
Normal file
20
packages/frontend/core/src/modules/permissions/index.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export { WorkspacePermissionService } from './services/permission';
|
||||
|
||||
import { GraphQLService } from '@affine/core/modules/cloud';
|
||||
import {
|
||||
type Framework,
|
||||
WorkspaceScope,
|
||||
WorkspaceService,
|
||||
} from '@toeverything/infra';
|
||||
|
||||
import { WorkspacePermission } from './entities/permission';
|
||||
import { WorkspacePermissionService } from './services/permission';
|
||||
import { WorkspacePermissionStore } from './stores/permission';
|
||||
|
||||
export function configurePermissionsModule(framework: Framework) {
|
||||
framework
|
||||
.scope(WorkspaceScope)
|
||||
.service(WorkspacePermissionService)
|
||||
.store(WorkspacePermissionStore, [GraphQLService])
|
||||
.entity(WorkspacePermission, [WorkspaceService, WorkspacePermissionStore]);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { Service } from '@toeverything/infra';
|
||||
|
||||
import { WorkspacePermission } from '../entities/permission';
|
||||
|
||||
export class WorkspacePermissionService extends Service {
|
||||
permission = this.framework.createEntity(WorkspacePermission);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import type { GraphQLService } from '@affine/core/modules/cloud';
|
||||
import { getIsOwnerQuery } from '@affine/graphql';
|
||||
import { Store } from '@toeverything/infra';
|
||||
|
||||
export class WorkspacePermissionStore extends Store {
|
||||
constructor(private readonly graphqlService: GraphQLService) {
|
||||
super();
|
||||
}
|
||||
|
||||
async fetchIsOwner(workspaceId: string, signal?: AbortSignal) {
|
||||
const isOwner = await this.graphqlService.gql({
|
||||
query: getIsOwnerQuery,
|
||||
variables: {
|
||||
workspaceId,
|
||||
},
|
||||
context: { signal },
|
||||
});
|
||||
|
||||
return isOwner.isOwner;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user