-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add doc permission service
- Loading branch information
Showing
27 changed files
with
985 additions
and
264 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/frontend/core/src/mobile/pages/workspace/detail/page-header-share-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/frontend/core/src/modules/permissions/entities/doc-granted-users.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
DocRole, | ||
type GetCurrentUserDocPermissionQuery, | ||
type GetPageGrantedUsersListQuery, | ||
} from '@affine/graphql'; | ||
import { | ||
backoffRetry, | ||
catchErrorInto, | ||
effect, | ||
Entity, | ||
fromPromise, | ||
LiveData, | ||
onComplete, | ||
onStart, | ||
} from '@toeverything/infra'; | ||
import { EMPTY, map, mergeMap, switchMap } from 'rxjs'; | ||
|
||
import { isBackendError, isNetworkError } from '../../cloud'; | ||
import type { DocService } from '../../doc'; | ||
import type { WorkspaceService } from '../../workspace'; | ||
import type { DocGrantedUsersStore } from '../stores/doc-granted-users'; | ||
|
||
export type GrantedUser = | ||
GetPageGrantedUsersListQuery['workspace']['pageGrantedUsersList']['edges'][number]['user']; | ||
|
||
export type UserDocPermission = | ||
GetCurrentUserDocPermissionQuery['workspace']['currentUserPermission']; | ||
Check failure on line 27 in packages/frontend/core/src/modules/permissions/entities/doc-granted-users.ts GitHub Actions / Lint
|
||
|
||
export class DocGrantedUsers extends Entity { | ||
constructor( | ||
private readonly store: DocGrantedUsersStore, | ||
private readonly workspaceService: WorkspaceService, | ||
private readonly docService: DocService | ||
) { | ||
super(); | ||
} | ||
|
||
pageNum$ = new LiveData(0); | ||
grantedUserCount$ = new LiveData<number | undefined>(undefined); | ||
docGrantedUsers$ = new LiveData<GrantedUser[] | undefined>(undefined); | ||
docOwner$ = this.docGrantedUsers$.map(users => | ||
users?.find(user => user.role === DocRole.Owner) | ||
); | ||
|
||
isLoading$ = new LiveData(false); | ||
error$ = new LiveData<any>(null); | ||
|
||
readonly PAGE_SIZE = 8; | ||
|
||
readonly revalidate = effect( | ||
map(() => this.pageNum$.value), | ||
switchMap(pageNum => { | ||
return fromPromise(async signal => { | ||
return this.store.fetchDocGrantedUsersList( | ||
this.workspaceService.workspace.id, | ||
this.docService.doc.id, | ||
{ | ||
first: pageNum * this.PAGE_SIZE, | ||
offset: this.PAGE_SIZE, | ||
}, | ||
signal | ||
); | ||
}).pipe( | ||
mergeMap(data => { | ||
const currentUsers = this.docGrantedUsers$.value || []; | ||
const newUsers = data.edges.map(edge => edge.user); | ||
const allUsers = [...currentUsers, ...newUsers]; | ||
this.grantedUserCount$.setValue(data.totalCount); | ||
this.docGrantedUsers$.setValue(allUsers); | ||
return EMPTY; | ||
}), | ||
backoffRetry({ | ||
when: isNetworkError, | ||
count: Infinity, | ||
}), | ||
backoffRetry({ | ||
when: isBackendError, | ||
}), | ||
catchErrorInto(this.error$), | ||
onStart(() => { | ||
this.isLoading$.setValue(true); | ||
}), | ||
onComplete(() => this.isLoading$.setValue(false)) | ||
); | ||
}) | ||
); | ||
|
||
setPageNum(pageNum: number) { | ||
this.pageNum$.setValue(pageNum); | ||
this.revalidate(); | ||
} | ||
|
||
override dispose(): void { | ||
this.revalidate.unsubscribe(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/frontend/core/src/modules/permissions/services/doc-granted-users.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import type { DocRole } from '@affine/graphql'; | ||
import { Service } from '@toeverything/infra'; | ||
|
||
import type { DocService } from '../../doc'; | ||
import type { WorkspaceService } from '../../workspace'; | ||
import { DocGrantedUsers } from '../entities/doc-granted-users'; | ||
import type { DocGrantedUsersStore } from '../stores/doc-granted-users'; | ||
|
||
export class DocGrantedUsersService extends Service { | ||
constructor( | ||
private readonly store: DocGrantedUsersStore, | ||
private readonly workspaceService: WorkspaceService, | ||
private readonly docService: DocService | ||
) { | ||
super(); | ||
} | ||
|
||
docGrantedUsers = this.framework.createEntity(DocGrantedUsers); | ||
|
||
async grantUsersRole(userIds: string[], role: DocRole) { | ||
return await this.store.grantDocUserRoles({ | ||
docId: this.docService.doc.id, | ||
workspaceId: this.workspaceService.workspace.id, | ||
userIds, | ||
role, | ||
}); | ||
} | ||
|
||
async revokeUsersRole(userIds: string[]) { | ||
return await this.store.revokeDocUserRoles(this.docService.doc.id, userIds); | ||
} | ||
|
||
async updateUserRole(userId: string, role: DocRole) { | ||
return await this.store.updateDocUserRole( | ||
this.docService.doc.id, | ||
userId, | ||
role | ||
); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
packages/frontend/core/src/modules/permissions/services/doc-permission.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { DebugLogger } from '@affine/debug'; | ||
import { | ||
DocRole, | ||
type GetCurrentUserDocPermissionQuery, | ||
} from '@affine/graphql'; | ||
import { | ||
backoffRetry, | ||
catchErrorInto, | ||
effect, | ||
fromPromise, | ||
LiveData, | ||
onComplete, | ||
onStart, | ||
Service, | ||
} from '@toeverything/infra'; | ||
import { EMPTY, exhaustMap, mergeMap } from 'rxjs'; | ||
|
||
import { isBackendError, isNetworkError } from '../../cloud'; | ||
import type { DocService } from '../../doc'; | ||
import type { WorkspaceService } from '../../workspace'; | ||
import type { DocPermissionStore } from '../stores/doc-permission'; | ||
|
||
const logger = new DebugLogger('affine:doc-permission'); | ||
|
||
export type DocRolePermissionActions = | ||
GetCurrentUserDocPermissionQuery['workspace']['currentUserPermission']['permissions']; | ||
Check failure on line 26 in packages/frontend/core/src/modules/permissions/services/doc-permission.ts GitHub Actions / Lint
|
||
|
||
export class DocPermissionService extends Service { | ||
isOwner$ = new LiveData<boolean | null>(null); | ||
canManage$ = new LiveData<boolean | null>(null); | ||
role$ = new LiveData<DocRole | undefined>(undefined); | ||
rolePermissions$ = new LiveData<DocRolePermissionActions | undefined>( | ||
undefined | ||
); | ||
|
||
isLoading$ = new LiveData(false); | ||
error$ = new LiveData<any>(null); | ||
|
||
constructor( | ||
private readonly workspaceService: WorkspaceService, | ||
private readonly docService: DocService, | ||
private readonly store: DocPermissionStore | ||
) { | ||
super(); | ||
} | ||
|
||
revalidate = effect( | ||
exhaustMap(() => { | ||
return fromPromise(async signal => { | ||
if (this.workspaceService.workspace.flavour !== 'local') { | ||
return await this.store.fetchCurrentUserDocPermissions( | ||
this.workspaceService.workspace.id, | ||
this.docService.doc.id, | ||
signal | ||
); | ||
} else { | ||
return null; | ||
} | ||
}).pipe( | ||
backoffRetry({ | ||
when: isNetworkError, | ||
count: Infinity, | ||
}), | ||
backoffRetry({ | ||
when: isBackendError, | ||
}), | ||
mergeMap(permission => { | ||
this.canManage$.next( | ||
permission?.role === DocRole.Manager || | ||
permission?.role === DocRole.Owner | ||
); | ||
this.isOwner$.next(permission?.role === DocRole.Owner); | ||
this.role$.next(permission?.role); | ||
this.rolePermissions$.next(permission?.permissions); | ||
return EMPTY; | ||
}), | ||
catchErrorInto(this.error$, error => { | ||
logger.error('Failed to fetch doc permission', error); | ||
}), | ||
onStart(() => this.isLoading$.setValue(true)), | ||
onComplete(() => this.isLoading$.setValue(false)) | ||
); | ||
}) | ||
); | ||
|
||
override dispose(): void { | ||
this.revalidate.unsubscribe(); | ||
} | ||
} |
Oops, something went wrong.