Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Add a condition on version id for document updates

Hakan Shehu87ea22f85cf0
diff --git a/desktop/src/main/handlers/mutations/document-save.ts b/desktop/src/main/handlers/mutations/document-save.ts
index 5f54ddf6..69cb5d95 100644
--- a/desktop/src/main/handlers/mutations/document-save.ts
+++ b/desktop/src/main/handlers/mutations/document-save.ts
@@ -7,6 +7,7 @@ import { generateId, IdType } from '@/lib/id';
import { LocalUpdateNodeChangeData } from '@/types/sync';
import { applyChangeToYDoc, mapContentsToBlocks } from '@/lib/editor';
import { LocalNodeAttributes, NodeBlock } from '@/types/nodes';
+import { hasUpdateChanges } from '@/main/utils';
export class DocumentSaveMutationHandler
implements MutationHandler<DocumentSaveMutationInput>
@@ -18,112 +19,132 @@ export class DocumentSaveMutationHandler
input.userId,
);
- const document = await workspaceDatabase
- .selectFrom('nodes')
- .selectAll()
- .where('id', '=', input.documentId)
- .executeTakeFirst();
-
- if (!document) {
- return {
- output: {
- success: false,
- },
- changes: [],
- };
- }
+ let count = 0;
+ while (count++ < 10) {
+ const document = await workspaceDatabase
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', input.documentId)
+ .executeTakeFirst();
+
+ if (!document) {
+ return {
+ output: {
+ success: false,
+ },
+ changes: [],
+ };
+ }
- const versionId = generateId(IdType.Version);
- const updatedAt = new Date().toISOString();
- const updates: string[] = [];
+ const versionId = generateId(IdType.Version);
+ const updatedAt = new Date().toISOString();
+ const updates: string[] = [];
- const doc = new Y.Doc({
- guid: document.id,
- });
- Y.applyUpdate(doc, toUint8Array(document.state));
+ const doc = new Y.Doc({
+ guid: document.id,
+ });
+ Y.applyUpdate(doc, toUint8Array(document.state));
- doc.on('update', (update) => {
- updates.push(fromUint8Array(update));
- });
+ doc.on('update', (update) => {
+ updates.push(fromUint8Array(update));
+ });
- const attributes = JSON.parse(document.attributes) as LocalNodeAttributes;
- const blocksMap = new Map<string, NodeBlock>();
- if (attributes.content) {
- for (const [key, value] of Object.entries(attributes.content)) {
- blocksMap.set(key, value);
+ const attributes = JSON.parse(document.attributes) as LocalNodeAttributes;
+ const blocksMap = new Map<string, NodeBlock>();
+ if (attributes.content) {
+ for (const [key, value] of Object.entries(attributes.content)) {
+ blocksMap.set(key, value);
+ }
}
- }
- const blocks = mapContentsToBlocks(
- document.id,
- input.content.content,
- blocksMap,
- );
+ const blocks = mapContentsToBlocks(
+ document.id,
+ input.content.content,
+ blocksMap,
+ );
- doc.transact(() => {
- applyChangeToYDoc(doc, blocks);
- });
+ doc.transact(() => {
+ applyChangeToYDoc(doc, blocks);
+ });
+
+ if (updates.length === 0) {
+ return {
+ output: {
+ success: true,
+ },
+ changes: [],
+ };
+ }
- if (updates.length === 0) {
- return {
- output: {
- success: true,
- },
- changes: [],
+ const attributesMap = doc.getMap('attributes');
+ const attributesJson = JSON.stringify(attributesMap.toJSON());
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const changeData: LocalUpdateNodeChangeData = {
+ type: 'node_update',
+ id: document.id,
+ updatedAt: updatedAt,
+ updatedBy: input.userId,
+ versionId: versionId,
+ updates: updates,
};
- }
- const attributesMap = doc.getMap('attributes');
- const attributesJson = JSON.stringify(attributesMap.toJSON());
- const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
-
- const changeData: LocalUpdateNodeChangeData = {
- type: 'node_update',
- id: document.id,
- updatedAt: updatedAt,
- updatedBy: input.userId,
- versionId: versionId,
- updates: updates,
- };
+ const result = await workspaceDatabase
+ .transaction()
+ .execute(async (trx) => {
+ const result = await trx
+ .updateTable('nodes')
+ .set({
+ attributes: attributesJson,
+ state: encodedState,
+ updated_at: updatedAt,
+ updated_by: input.userId,
+ version_id: versionId,
+ })
+ .where('id', '=', input.documentId)
+ .where('version_id', '=', document.version_id)
+ .execute();
- await workspaceDatabase.transaction().execute(async (trx) => {
- await trx
- .updateTable('nodes')
- .set({
- attributes: attributesJson,
- state: encodedState,
- updated_at: updatedAt,
- updated_by: input.userId,
- version_id: versionId,
- })
- .where('id', '=', input.documentId)
- .execute();
+ const hasChanges = hasUpdateChanges(result);
- await trx
- .insertInto('changes')
- .values({
- data: JSON.stringify(changeData),
- created_at: updatedAt,
- })
- .execute();
- });
+ if (hasChanges) {
+ await trx
+ .insertInto('changes')
+ .values({
+ data: JSON.stringify(changeData),
+ created_at: updatedAt,
+ })
+ .execute();
+ }
+
+ return hasChanges;
+ });
+
+ if (result) {
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'nodes',
+ userId: input.userId,
+ },
+ {
+ type: 'workspace',
+ table: 'changes',
+ userId: input.userId,
+ },
+ ],
+ };
+ }
+ }
return {
output: {
- success: true,
+ success: false,
},
- changes: [
- {
- type: 'workspace',
- table: 'nodes',
- userId: input.userId,
- },
- {
- type: 'workspace',
- table: 'changes',
- userId: input.userId,
- },
- ],
};
}
}
diff --git a/desktop/src/main/utils.ts b/desktop/src/main/utils.ts
index fa644367..9dfc380b 100644
--- a/desktop/src/main/utils.ts
+++ b/desktop/src/main/utils.ts
@@ -1,4 +1,5 @@
import { app } from 'electron';
+import { DeleteResult, InsertResult, UpdateResult } from 'kysely';
import path from 'path';
export const appPath = app.getPath('userData');
@@ -16,3 +17,25 @@ export const getWorkspaceFilesDirectoryPath = (userId: string): string => {
export const getAccountAvatarsDirectoryPath = (accountId: string): string => {
return path.join(appPath, 'avatars', accountId);
};
+
+export const hasInsertChanges = (result: InsertResult[]): boolean => {
+ if (result.length === 0) {
+ return false;
+ }
+
+ return result.some(
+ (r) => r.numInsertedOrUpdatedRows && r.numInsertedOrUpdatedRows > 0n,
+ );
+};
+
+export const hasUpdateChanges = (result: UpdateResult[]): boolean => {
+ if (result.length === 0) {
+ return false;
+ }
+
+ return result.some((r) => r.numUpdatedRows && r.numUpdatedRows > 0n);
+};
+
+export const hasDeleteChanges = (result: DeleteResult[]): boolean => {
+ return result.some((r) => r.numDeletedRows && r.numDeletedRows > 0n);
+};
diff --git a/server/src/routes/sync.ts b/server/src/routes/sync.ts
index 3112b0db..4ed7f6fa 100644
--- a/server/src/routes/sync.ts
+++ b/server/src/routes/sync.ts
@@ -1,6 +1,6 @@
import * as Y from 'yjs';
import { ApiError, NeuronRequest, NeuronResponse } from '@/types/api';
-import { database } from '@/data/database';
+import { database, hasUpdateChanges } from '@/data/database';
import { Router } from 'express';
import {
LocalChange,
@@ -190,72 +190,82 @@ const handleUpdateNodeChange = async (
workspaceUser: SelectWorkspaceUser,
changeData: LocalUpdateNodeChangeData,
): Promise<SyncLocalChangeResult> => {
- const existingNode = await database
- .selectFrom('nodes')
- .select(['id', 'workspace_id', 'attributes', 'state'])
- .where('id', '=', changeData.id)
- .executeTakeFirst();
-
- if (
- !existingNode ||
- existingNode.workspace_id != workspaceUser.workspace_id
- ) {
- return {
- status: 'error',
- };
- }
+ let count = 0;
+ while (count++ < 10) {
+ const existingNode = await database
+ .selectFrom('nodes')
+ .select(['id', 'workspace_id', 'attributes', 'state'])
+ .where('id', '=', changeData.id)
+ .executeTakeFirst();
- const role = await fetchCollaboratorRole(changeData.id, workspaceUser.id);
- if (role === null) {
- return {
- status: 'error',
- };
- }
+ if (
+ !existingNode ||
+ existingNode.workspace_id != workspaceUser.workspace_id
+ ) {
+ return {
+ status: 'error',
+ };
+ }
- const doc = new Y.Doc({
- guid: changeData.id,
- });
+ const role = await fetchCollaboratorRole(changeData.id, workspaceUser.id);
+ if (role === null) {
+ return {
+ status: 'error',
+ };
+ }
- Y.applyUpdate(doc, toUint8Array(existingNode.state));
+ const doc = new Y.Doc({
+ guid: changeData.id,
+ });
- for (const update of changeData.updates) {
- Y.applyUpdate(doc, toUint8Array(update));
- }
+ Y.applyUpdate(doc, toUint8Array(existingNode.state));
- const attributesMap = doc.getMap('attributes');
- const attributes = attributesMap.toJSON() as ServerNodeAttributes;
- const attributesJson = JSON.stringify(attributes);
- const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+ for (const update of changeData.updates) {
+ Y.applyUpdate(doc, toUint8Array(update));
+ }
- await database
- .updateTable('nodes')
- .set({
- attributes: attributesJson,
- state: encodedState,
- updated_at: new Date(changeData.updatedAt),
- updated_by: changeData.updatedBy,
- version_id: changeData.versionId,
- server_updated_at: new Date(),
- })
- .where('id', '=', changeData.id)
- .execute();
+ const attributesMap = doc.getMap('attributes');
+ const attributes = attributesMap.toJSON() as ServerNodeAttributes;
+ const attributesJson = JSON.stringify(attributes);
+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ const result = await database
+ .updateTable('nodes')
+ .set({
+ attributes: attributesJson,
+ state: encodedState,
+ updated_at: new Date(changeData.updatedAt),
+ updated_by: changeData.updatedBy,
+ version_id: changeData.versionId,
+ server_updated_at: new Date(),
+ })
+ .where('id', '=', changeData.id)
+ .where('version_id', '=', changeData.versionId)
+ .execute();
+
+ if (hasUpdateChanges(result)) {
+ const event: NodeUpdatedEvent = {
+ type: 'node_updated',
+ id: changeData.id,
+ workspaceId: workspaceUser.workspace_id,
+ beforeAttributes: existingNode.attributes,
+ afterAttributes: attributes,
+ updatedBy: changeData.updatedBy,
+ updatedAt: changeData.updatedAt,
+ serverUpdatedAt: new Date().toISOString(),
+ versionId: changeData.versionId,
+ };
- const event: NodeUpdatedEvent = {
- type: 'node_updated',
- id: changeData.id,
- workspaceId: workspaceUser.workspace_id,
- beforeAttributes: existingNode.attributes,
- afterAttributes: attributes,
- updatedBy: changeData.updatedBy,
- updatedAt: changeData.updatedAt,
- serverUpdatedAt: new Date().toISOString(),
- versionId: changeData.versionId,
- };
+ await enqueueEvent(event);
- await enqueueEvent(event);
+ return {
+ status: 'success',
+ };
+ }
+ }
return {
- status: 'success',
+ status: 'error',
};
};