fix page history generation

This commit is contained in:
Philipinho
2024-06-07 16:23:23 +01:00
parent 942917072b
commit 2afbede8ce
8 changed files with 122 additions and 99 deletions

View File

@ -8,12 +8,20 @@ import { Injectable, Logger } from '@nestjs/common';
import { TiptapTransformer } from '@hocuspocus/transformer';
import { getPageId, jsonToText, tiptapExtensions } from '../collaboration.util';
import { PageRepo } from '@docmost/db/repos/page/page.repo';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
import { EventEmitter2 } from '@nestjs/event-emitter';
@Injectable()
export class PersistenceExtension implements Extension {
private readonly logger = new Logger(PersistenceExtension.name);
constructor(private readonly pageRepo: PageRepo) {}
constructor(
private readonly pageRepo: PageRepo,
@InjectKysely() private readonly db: KyselyDB,
private eventEmitter: EventEmitter2,
) {}
async onLoadDocument(data: onLoadDocumentPayload) {
const { documentName, document } = data;
@ -72,16 +80,38 @@ export class PersistenceExtension implements Extension {
const textContent = jsonToText(tiptapJson);
try {
await this.pageRepo.updatePage(
{
content: tiptapJson,
textContent: textContent,
ydoc: ydocState,
let page = null;
await executeTx(this.db, async (trx) => {
page = await this.pageRepo.findById(pageId, {
withLock: true,
trx,
});
if (!page) {
this.logger.error(`Page with id ${pageId} not found`);
return;
}
await this.pageRepo.updatePage(
{
content: tiptapJson,
textContent: textContent,
ydoc: ydocState,
lastUpdatedById: context.user.id,
},
pageId,
trx,
);
});
this.eventEmitter.emit('collab.page.updated', {
page: {
...page,
lastUpdatedById: context.user.id,
updatedAt: new Date(),
content: tiptapJson,
},
pageId,
);
});
} catch (err) {
this.logger.error(`Failed to update page ${pageId}`, err);
}