feat(server): auto create page owner role (#9944)

This commit is contained in:
forehalo
2025-02-05 10:09:57 +00:00
parent 5913715e33
commit 0ff8d3af6f
2 changed files with 23 additions and 0 deletions

View File

@@ -32,6 +32,11 @@ declare global {
workspaceId: string; workspaceId: string;
docId: string; docId: string;
}; };
'doc.update.pushed': {
workspaceId: string;
docId: string;
editor?: string;
};
} }
} }
@Injectable() @Injectable()
@@ -105,6 +110,11 @@ export class PgWorkspaceDocStorageAdapter extends DocStorageAdapter {
await this.updateCachedUpdatesCount(workspaceId, docId, batch.length); await this.updateCachedUpdatesCount(workspaceId, docId, batch.length);
} }
}); });
this.event.emit('doc.update.pushed', {
workspaceId,
docId,
editor: editorId,
});
} catch (e) { } catch (e) {
this.logger.error('Failed to insert doc updates', e); this.logger.error('Failed to insert doc updates', e);
metrics.doc.counter('doc_update_insert_failed').add(1); metrics.doc.counter('doc_update_insert_failed').add(1);

View File

@@ -6,6 +6,7 @@ import { groupBy } from 'lodash-es';
import { import {
DocAccessDenied, DocAccessDenied,
EventBus, EventBus,
OnEvent,
SpaceAccessDenied, SpaceAccessDenied,
SpaceOwnerNotFound, SpaceOwnerNotFound,
SpaceShouldHaveOnlyOneOwner, SpaceShouldHaveOnlyOneOwner,
@@ -29,6 +30,18 @@ export class PermissionService {
private readonly event: EventBus private readonly event: EventBus
) {} ) {}
@OnEvent('doc.update.pushed')
async onDocUpdatePushed(payload: Events['doc.update.pushed']) {
const { workspaceId, docId, editor } = payload;
await this.prisma.$queryRaw`
INSERT INTO "workspace_page_user_permissions" ("workspace_id", "page_id", "user_id", "type", "accepted", "created_at", "updated_at")
VALUES (${workspaceId}, ${docId}, ${editor}, ${DocRole.Owner}, true, now(), now())
ON CONFLICT ("workspace_id", "page_id", "user_id")
DO NOTHING
`;
}
private get acceptedCondition() { private get acceptedCondition() {
return [ return [
{ {