There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.
TypeScript appcrdteditorelectronknowledge-basemarkdownmironotesnotionnotion-alternativerustrust-langrust-languagetabletableviewwhiteboardwikiworkspace
fix(server): check state changes before saving history record (#5038)
liuyi1740e7efa17a
diff --git a/packages/backend/server/src/modules/doc/history.ts b/packages/backend/server/src/modules/doc/history.tsindex f3cdb8ad52..61ae73e55d 100644--- a/packages/backend/server/src/modules/doc/history.ts+++ b/packages/backend/server/src/modules/doc/history.ts@@ -1,3 +1,5 @@+import { isDeepStrictEqual } from 'node:util';+import { Injectable, Logger } from '@nestjs/common';import { OnEvent } from '@nestjs/event-emitter';import { Cron, CronExpression } from '@nestjs/schedule';@@ -41,6 +43,14 @@ export class DocHistoryManager {}if (shouldCreateHistory) {+ // skip the history recording when no actual update on snapshot happended+ if (last && isDeepStrictEqual(last.state, snapshot.state)) {+ this.logger.debug(+ `State matches, skip creating history record for ${snapshot.id} in workspace ${snapshot.workspaceId}`+ );+ return;+ }+await this.db.snapshotHistory.create({select: {@@ -129,6 +139,7 @@ export class DocHistoryManager {},select: {timestamp: true,+ state: true,},orderBy: {timestamp: 'desc',diff --git a/packages/backend/server/tests/history.spec.ts b/packages/backend/server/tests/history.spec.tsindex 2ce320797d..5555206a3b 100644--- a/packages/backend/server/tests/history.spec.ts+++ b/packages/backend/server/tests/history.spec.ts@@ -74,9 +74,9 @@ test('should create doc history if never created before', async t => {t.is(history?.timestamp.getTime(), timestamp.getTime());});-test('should not create history is timestamp equals to last record', async t => {+test('should not create history if timestamp equals to last record', async t => {const timestamp = new Date();- Sinon.stub(manager, 'last').resolves({ timestamp });+ Sinon.stub(manager, 'last').resolves({ timestamp, state: null });await manager.onDocUpdated({...snapshot,@@ -93,10 +93,11 @@ test('should not create history is timestamp equals to last record', async t =>t.falsy(history);});-test('should create history if time diff is larger than interval config', async t => {+test('should not create history if state equals to last record', async t => {const timestamp = new Date();Sinon.stub(manager, 'last').resolves({- timestamp: new Date(timestamp.getTime() - 1000 * 60 * 20),+ timestamp: new Date(timestamp.getTime() - 1),+ state: snapshot.state,});await manager.onDocUpdated({@@ -111,13 +112,14 @@ test('should create history if time diff is larger than interval config', async},});- t.truthy(history);+ t.falsy(history);});test('should not create history if time diff is less than interval config', async t => {const timestamp = new Date();Sinon.stub(manager, 'last').resolves({timestamp: new Date(timestamp.getTime() - 1000),+ state: Buffer.from([0, 1]),});await manager.onDocUpdated({@@ -135,10 +137,33 @@ test('should not create history if time diff is less than interval config', asynt.falsy(history);});+test('should create history if time diff is larger than interval config and state diff', async t => {+ const timestamp = new Date();+ Sinon.stub(manager, 'last').resolves({+ timestamp: new Date(timestamp.getTime() - 1000 * 60 * 20),+ state: Buffer.from([0, 1]),+ });++ await manager.onDocUpdated({+ ...snapshot,+ updatedAt: timestamp,+ });++ const history = await db.snapshotHistory.findFirst({+ where: {+ workspaceId: '1',+ id: 'doc1',+ },+ });++ t.truthy(history);+});+test('should create history with force flag even if time diff in small', async t => {const timestamp = new Date();Sinon.stub(manager, 'last').resolves({timestamp: new Date(timestamp.getTime() - 1),+ state: Buffer.from([0, 1]),});await manager.onDocUpdated(@@ -309,4 +334,8 @@ test('should be able to cleanup expired history', async t => {count = await db.snapshotHistory.count();t.is(count, 10);++ const example = await db.snapshotHistory.findFirst();+ t.truthy(example);+ t.true(example!.expiredAt > new Date());});