Skip to content

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

Change the sync flow in server side

Hakan Shehuf7d1d12e95f7
diff --git a/desktop/src/main/handlers/messages/index.ts b/desktop/src/main/handlers/messages/index.ts
index ff2c2063..acca75cc 100644
--- a/desktop/src/main/handlers/messages/index.ts
+++ b/desktop/src/main/handlers/messages/index.ts
@@ -1,6 +1,8 @@
import { MessageHandler, MessageMap } from '@/operations/messages';
import { LocalNodeSyncMessageHandler } from '@/main/handlers/messages/local-node-sync';
-import { ServerNodesSyncMessageHandler } from '@/main/handlers/messages/server-nodes-sync';
+import { LocalNodeDeleteMessageHandler } from '@/main/handlers/messages/local-node-delete';
+import { ServerNodeSyncMessageHandler } from '@/main/handlers/messages/server-node-sync';
+import { ServerNodeDeleteMessageHandler } from '@/main/handlers/messages/server-node-delete';
type MessageHandlerMap = {
[K in keyof MessageMap]: MessageHandler<MessageMap[K]>;
@@ -8,5 +10,7 @@ type MessageHandlerMap = {
export const messageHandlerMap: MessageHandlerMap = {
local_node_sync: new LocalNodeSyncMessageHandler(),
- server_nodes_sync: new ServerNodesSyncMessageHandler(),
+ local_node_delete: new LocalNodeDeleteMessageHandler(),
+ server_node_sync: new ServerNodeSyncMessageHandler(),
+ server_node_delete: new ServerNodeDeleteMessageHandler(),
};
diff --git a/desktop/src/main/handlers/messages/local-node-delete.ts b/desktop/src/main/handlers/messages/local-node-delete.ts
new file mode 100644
index 00000000..3e16394a
--- /dev/null
+++ b/desktop/src/main/handlers/messages/local-node-delete.ts
@@ -0,0 +1,14 @@
+import { MessageContext, MessageHandler } from '@/operations/messages';
+import { LocalNodeDeleteMessageInput } from '@/operations/messages/local-node-delete';
+import { socketManager } from '@/main/sockets/socket-manager';
+
+export class LocalNodeDeleteMessageHandler
+ implements MessageHandler<LocalNodeDeleteMessageInput>
+{
+ public async handleMessage(
+ context: MessageContext,
+ input: LocalNodeDeleteMessageInput,
+ ): Promise<void> {
+ socketManager.sendMessage(context.accountId, input);
+ }
+}
diff --git a/desktop/src/main/handlers/messages/server-node-delete.ts b/desktop/src/main/handlers/messages/server-node-delete.ts
new file mode 100644
index 00000000..f0bba25a
--- /dev/null
+++ b/desktop/src/main/handlers/messages/server-node-delete.ts
@@ -0,0 +1,19 @@
+import { MessageContext, MessageHandler } from '@/operations/messages';
+import { ServerNodeDeleteMessageInput } from '@/operations/messages/server-node-delete';
+import { mediator } from '@/main/mediator';
+
+export class ServerNodeDeleteMessageHandler
+ implements MessageHandler<ServerNodeDeleteMessageInput>
+{
+ public async handleMessage(
+ context: MessageContext,
+ input: ServerNodeDeleteMessageInput,
+ ): Promise<void> {
+ await mediator.executeMutation({
+ type: 'server_node_delete',
+ accountId: context.accountId,
+ id: input.id,
+ workspaceId: input.workspaceId,
+ });
+ }
+}
diff --git a/desktop/src/main/handlers/messages/server-node-sync.ts b/desktop/src/main/handlers/messages/server-node-sync.ts
new file mode 100644
index 00000000..678d1e02
--- /dev/null
+++ b/desktop/src/main/handlers/messages/server-node-sync.ts
@@ -0,0 +1,27 @@
+import { MessageContext, MessageHandler } from '@/operations/messages';
+import { ServerNodeSyncMessageInput } from '@/operations/messages/server-node-sync';
+import { mediator } from '@/main/mediator';
+
+export class ServerNodeSyncMessageHandler
+ implements MessageHandler<ServerNodeSyncMessageInput>
+{
+ public async handleMessage(
+ context: MessageContext,
+ input: ServerNodeSyncMessageInput,
+ ): Promise<void> {
+ await mediator.executeMutation({
+ type: 'server_node_sync',
+ accountId: context.accountId,
+ id: input.id,
+ workspaceId: input.workspaceId,
+ state: input.state,
+ createdAt: input.createdAt,
+ createdBy: input.createdBy,
+ updatedAt: input.updatedAt,
+ updatedBy: input.updatedBy,
+ serverCreatedAt: input.serverCreatedAt,
+ serverUpdatedAt: input.serverUpdatedAt,
+ versionId: input.versionId,
+ });
+ }
+}
diff --git a/desktop/src/main/handlers/messages/server-nodes-sync.ts b/desktop/src/main/handlers/messages/server-nodes-sync.ts
deleted file mode 100644
index 9c4d607f..00000000
--- a/desktop/src/main/handlers/messages/server-nodes-sync.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { MessageContext, MessageHandler } from '@/operations/messages';
-import { ServerNodesSyncMessageInput } from '@/operations/messages/server-nodes-sync';
-import { mediator } from '@/main/mediator';
-
-export class ServerNodesSyncMessageHandler
- implements MessageHandler<ServerNodesSyncMessageInput>
-{
- public async handleMessage(
- context: MessageContext,
- input: ServerNodesSyncMessageInput,
- ): Promise<void> {
- await mediator.executeMutation({
- type: 'node_sync',
- nodes: input.nodes,
- accountId: context.accountId,
- });
- }
-}
diff --git a/desktop/src/main/handlers/mutations/index.ts b/desktop/src/main/handlers/mutations/index.ts
index c4752fdf..8395d54e 100644
--- a/desktop/src/main/handlers/mutations/index.ts
+++ b/desktop/src/main/handlers/mutations/index.ts
@@ -29,7 +29,8 @@ import { WorkspaceUpdateMutationHandler } from '@/main/handlers/mutations/worksp
import { DocumentSaveMutationHandler } from '@/main/handlers/mutations/document-save';
import { AvatarUploadMutationHandler } from '@/main/handlers/mutations/avatar-upload';
import { LogoutMutationHandler } from '@/main/handlers/mutations/logout';
-import { NodeSyncMutationHandler } from '@/main/handlers/mutations/node-sync';
+import { ServerNodeSyncMutationHandler } from '@/main/handlers/mutations/server-node-sync';
+import { ServerNodeDeleteMutationHandler } from '@/main/handlers/mutations/server-node-delete';
import { FolderCreateMutationHandler } from '@/main/handlers/mutations/folder-create';
import { FileCreateMutationHandler } from '@/main/handlers/mutations/file-create';
import { FileDownloadMutationHandler } from '@/main/handlers/mutations/file-download';
@@ -70,7 +71,8 @@ export const mutationHandlerMap: MutationHandlerMap = {
document_save: new DocumentSaveMutationHandler(),
avatar_upload: new AvatarUploadMutationHandler(),
logout: new LogoutMutationHandler(),
- node_sync: new NodeSyncMutationHandler(),
+ server_node_sync: new ServerNodeSyncMutationHandler(),
+ server_node_delete: new ServerNodeDeleteMutationHandler(),
folder_create: new FolderCreateMutationHandler(),
file_create: new FileCreateMutationHandler(),
file_download: new FileDownloadMutationHandler(),
diff --git a/desktop/src/main/handlers/mutations/node-sync.ts b/desktop/src/main/handlers/mutations/node-sync.ts
deleted file mode 100644
index 44c06c8e..00000000
--- a/desktop/src/main/handlers/mutations/node-sync.ts
+++ /dev/null
@@ -1,134 +0,0 @@
-import { databaseManager } from '@/main/data/database-manager';
-import { socketManager } from '@/main/sockets/socket-manager';
-import {
- MutationChange,
- MutationHandler,
- MutationResult,
-} from '@/operations/mutations';
-import { NodeSyncMutationInput } from '@/operations/mutations/node-sync';
-import { toUint8Array } from 'js-base64';
-import * as Y from 'yjs';
-
-export class NodeSyncMutationHandler
- implements MutationHandler<NodeSyncMutationInput>
-{
- public async handleMutation(
- input: NodeSyncMutationInput,
- ): Promise<MutationResult<NodeSyncMutationInput>> {
- const changes: MutationChange[] = [];
-
- for (const node of input.nodes) {
- const workspace = await databaseManager.appDatabase
- .selectFrom('workspaces')
- .selectAll()
- .where((eb) =>
- eb.and([
- eb('account_id', '=', input.accountId),
- eb('workspace_id', '=', node.workspaceId),
- ]),
- )
- .executeTakeFirst();
-
- if (!workspace) {
- continue;
- }
-
- const userId = workspace.user_id;
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- if (node.deletedAt) {
- await workspaceDatabase
- .deleteFrom('nodes')
- .where('id', '=', node.id)
- .execute();
-
- continue;
- }
-
- const existingNode = await workspaceDatabase
- .selectFrom('nodes')
- .where('id', '=', node.id)
- .selectAll()
- .executeTakeFirst();
-
- if (!existingNode) {
- const doc = new Y.Doc({
- guid: node.id,
- });
-
- Y.applyUpdate(doc, toUint8Array(node.state));
-
- const attributesMap = doc.getMap('attributes');
- const attributes = JSON.stringify(attributesMap.toJSON());
-
- await workspaceDatabase
- .insertInto('nodes')
- .values({
- id: node.id,
- attributes: attributes,
- state: node.state,
- created_at: node.createdAt,
- created_by: node.createdBy,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_version_id: node.versionId,
- })
- .onConflict((cb) =>
- cb
- .doUpdateSet({
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.versionId,
- })
- .where('version_id', '=', node.versionId),
- )
- .execute();
- } else {
- const doc = new Y.Doc({
- guid: node.id,
- });
-
- Y.applyUpdate(doc, toUint8Array(existingNode.state));
- Y.applyUpdate(doc, toUint8Array(node.state));
-
- const attributesMap = doc.getMap('attributes');
- const attributes = JSON.stringify(attributesMap.toJSON());
-
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- state: node.state,
- attributes: attributes,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.versionId,
- updated_at: node.updatedAt,
- updated_by: node.updatedBy,
- version_id: node.versionId,
- })
- .where('id', '=', node.id)
- .execute();
- }
-
- changes.push({
- type: 'workspace',
- table: 'nodes',
- userId: userId,
- });
-
- socketManager.sendMessage(workspace.account_id, {
- type: 'local_node_sync',
- nodeId: node.id,
- versionId: node.versionId,
- });
- }
-
- return {
- output: {
- success: true,
- },
- changes: changes,
- };
- }
-}
diff --git a/desktop/src/main/handlers/mutations/server-node-delete.ts b/desktop/src/main/handlers/mutations/server-node-delete.ts
new file mode 100644
index 00000000..08effa17
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/server-node-delete.ts
@@ -0,0 +1,65 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { socketManager } from '@/main/sockets/socket-manager';
+import {
+ MutationChange,
+ MutationHandler,
+ MutationResult,
+} from '@/operations/mutations';
+import { ServerNodeDeleteMutationInput } from '@/operations/mutations/server-node-delete';
+
+export class ServerNodeDeleteMutationHandler
+ implements MutationHandler<ServerNodeDeleteMutationInput>
+{
+ public async handleMutation(
+ input: ServerNodeDeleteMutationInput,
+ ): Promise<MutationResult<ServerNodeDeleteMutationInput>> {
+ const changes: MutationChange[] = [];
+
+ const workspace = await databaseManager.appDatabase
+ .selectFrom('workspaces')
+ .selectAll()
+ .where((eb) =>
+ eb.and([
+ eb('account_id', '=', input.accountId),
+ eb('workspace_id', '=', input.workspaceId),
+ ]),
+ )
+ .executeTakeFirst();
+
+ if (!workspace) {
+ return {
+ output: {
+ success: false,
+ },
+ };
+ }
+
+ const userId = workspace.user_id;
+ const workspaceDatabase =
+ await databaseManager.getWorkspaceDatabase(userId);
+
+ await workspaceDatabase
+ .deleteFrom('nodes')
+ .where('id', '=', input.id)
+ .execute();
+
+ changes.push({
+ type: 'workspace',
+ table: 'nodes',
+ userId: userId,
+ });
+
+ socketManager.sendMessage(workspace.account_id, {
+ type: 'local_node_delete',
+ nodeId: input.id,
+ workspaceId: input.workspaceId,
+ });
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: changes,
+ };
+ }
+}
diff --git a/desktop/src/main/handlers/mutations/server-node-sync.ts b/desktop/src/main/handlers/mutations/server-node-sync.ts
new file mode 100644
index 00000000..e71585be
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/server-node-sync.ts
@@ -0,0 +1,127 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { socketManager } from '@/main/sockets/socket-manager';
+import {
+ MutationChange,
+ MutationHandler,
+ MutationResult,
+} from '@/operations/mutations';
+import { ServerNodeSyncMutationInput } from '@/operations/mutations/server-node-sync';
+import { toUint8Array } from 'js-base64';
+import * as Y from 'yjs';
+
+export class ServerNodeSyncMutationHandler
+ implements MutationHandler<ServerNodeSyncMutationInput>
+{
+ public async handleMutation(
+ input: ServerNodeSyncMutationInput,
+ ): Promise<MutationResult<ServerNodeSyncMutationInput>> {
+ const changes: MutationChange[] = [];
+
+ const workspace = await databaseManager.appDatabase
+ .selectFrom('workspaces')
+ .selectAll()
+ .where((eb) =>
+ eb.and([
+ eb('account_id', '=', input.accountId),
+ eb('workspace_id', '=', input.workspaceId),
+ ]),
+ )
+ .executeTakeFirst();
+
+ if (!workspace) {
+ return {
+ output: {
+ success: false,
+ },
+ };
+ }
+
+ const userId = workspace.user_id;
+ const workspaceDatabase =
+ await databaseManager.getWorkspaceDatabase(userId);
+
+ const existingNode = await workspaceDatabase
+ .selectFrom('nodes')
+ .where('id', '=', input.id)
+ .selectAll()
+ .executeTakeFirst();
+
+ if (!existingNode) {
+ const doc = new Y.Doc({
+ guid: input.id,
+ });
+
+ Y.applyUpdate(doc, toUint8Array(input.state));
+
+ const attributesMap = doc.getMap('attributes');
+ const attributes = JSON.stringify(attributesMap.toJSON());
+
+ await workspaceDatabase
+ .insertInto('nodes')
+ .values({
+ id: input.id,
+ attributes: attributes,
+ state: input.state,
+ created_at: input.createdAt,
+ created_by: input.createdBy,
+ version_id: input.versionId,
+ server_created_at: input.serverCreatedAt,
+ server_version_id: input.versionId,
+ })
+ .onConflict((cb) =>
+ cb
+ .doUpdateSet({
+ server_created_at: input.serverCreatedAt,
+ server_updated_at: input.serverUpdatedAt,
+ server_version_id: input.versionId,
+ })
+ .where('version_id', '=', input.versionId),
+ )
+ .execute();
+ } else {
+ const doc = new Y.Doc({
+ guid: input.id,
+ });
+
+ Y.applyUpdate(doc, toUint8Array(existingNode.state));
+ Y.applyUpdate(doc, toUint8Array(input.state));
+
+ const attributesMap = doc.getMap('attributes');
+ const attributes = JSON.stringify(attributesMap.toJSON());
+
+ await workspaceDatabase
+ .updateTable('nodes')
+ .set({
+ state: input.state,
+ attributes: attributes,
+ server_created_at: input.serverCreatedAt,
+ server_updated_at: input.serverUpdatedAt,
+ server_version_id: input.versionId,
+ updated_at: input.updatedAt,
+ updated_by: input.updatedBy,
+ version_id: input.versionId,
+ })
+ .where('id', '=', input.id)
+ .execute();
+ }
+
+ changes.push({
+ type: 'workspace',
+ table: 'nodes',
+ userId: userId,
+ });
+
+ socketManager.sendMessage(workspace.account_id, {
+ type: 'local_node_sync',
+ nodeId: input.id,
+ versionId: input.versionId,
+ });
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: changes,
+ };
+ }
+}
diff --git a/desktop/src/operations/messages/local-node-delete.ts b/desktop/src/operations/messages/local-node-delete.ts
new file mode 100644
index 00000000..59ec3124
--- /dev/null
+++ b/desktop/src/operations/messages/local-node-delete.ts
@@ -0,0 +1,11 @@
+export type LocalNodeDeleteMessageInput = {
+ type: 'local_node_delete';
+ nodeId: string;
+ workspaceId: string;
+};
+
+declare module '@/operations/messages' {
+ interface MessageMap {
+ local_node_delete: LocalNodeDeleteMessageInput;
+ }
+}
diff --git a/desktop/src/operations/messages/server-node-delete.ts b/desktop/src/operations/messages/server-node-delete.ts
new file mode 100644
index 00000000..00e96953
--- /dev/null
+++ b/desktop/src/operations/messages/server-node-delete.ts
@@ -0,0 +1,11 @@
+export type ServerNodeDeleteMessageInput = {
+ type: 'server_node_delete';
+ id: string;
+ workspaceId: string;
+};
+
+declare module '@/operations/messages' {
+ interface MessageMap {
+ server_node_delete: ServerNodeDeleteMessageInput;
+ }
+}
diff --git a/desktop/src/operations/messages/server-node-sync.ts b/desktop/src/operations/messages/server-node-sync.ts
new file mode 100644
index 00000000..a44b3b39
--- /dev/null
+++ b/desktop/src/operations/messages/server-node-sync.ts
@@ -0,0 +1,19 @@
+export type ServerNodeSyncMessageInput = {
+ type: 'server_node_sync';
+ id: string;
+ workspaceId: string;
+ state: string;
+ createdAt: string;
+ createdBy: string;
+ updatedAt: string | null;
+ updatedBy: string | null;
+ serverCreatedAt: string;
+ serverUpdatedAt: string | null;
+ versionId: string;
+};
+
+declare module '@/operations/messages' {
+ interface MessageMap {
+ server_node_sync: ServerNodeSyncMessageInput;
+ }
+}
diff --git a/desktop/src/operations/messages/server-nodes-sync.ts b/desktop/src/operations/messages/server-nodes-sync.ts
deleted file mode 100644
index 1adb3080..00000000
--- a/desktop/src/operations/messages/server-nodes-sync.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import { ServerNodeSyncData } from '@/types/sync';
-
-export type ServerNodesSyncMessageInput = {
- type: 'server_nodes_sync';
- nodes: ServerNodeSyncData[];
-};
-
-declare module '@/operations/messages' {
- interface MessageMap {
- server_nodes_sync: ServerNodesSyncMessageInput;
- }
-}
diff --git a/desktop/src/operations/mutations/node-sync.ts b/desktop/src/operations/mutations/node-sync.ts
deleted file mode 100644
index dfb67fb5..00000000
--- a/desktop/src/operations/mutations/node-sync.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { ServerNodeSyncData } from '@/types/sync';
-
-export type NodeSyncMutationInput = {
- type: 'node_sync';
- accountId: string;
- nodes: ServerNodeSyncData[];
-};
-
-export type NodeSyncMutationOutput = {
- success: boolean;
-};
-
-declare module '@/operations/mutations' {
- interface MutationMap {
- node_sync: {
- input: NodeSyncMutationInput;
- output: NodeSyncMutationOutput;
- };
- }
-}
diff --git a/desktop/src/operations/mutations/server-node-delete.ts b/desktop/src/operations/mutations/server-node-delete.ts
new file mode 100644
index 00000000..cfe79701
--- /dev/null
+++ b/desktop/src/operations/mutations/server-node-delete.ts
@@ -0,0 +1,19 @@
+export type ServerNodeDeleteMutationInput = {
+ type: 'server_node_delete';
+ accountId: string;
+ id: string;
+ workspaceId: string;
+};
+
+export type ServerNodeDeleteMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ server_node_delete: {
+ input: ServerNodeDeleteMutationInput;
+ output: ServerNodeDeleteMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/server-node-sync.ts b/desktop/src/operations/mutations/server-node-sync.ts
new file mode 100644
index 00000000..b2014aa8
--- /dev/null
+++ b/desktop/src/operations/mutations/server-node-sync.ts
@@ -0,0 +1,27 @@
+export type ServerNodeSyncMutationInput = {
+ type: 'server_node_sync';
+ accountId: string;
+ id: string;
+ workspaceId: string;
+ state: string;
+ createdAt: string;
+ createdBy: string;
+ updatedAt: string | null;
+ updatedBy: string | null;
+ serverCreatedAt: string;
+ serverUpdatedAt: string | null;
+ versionId: string;
+};
+
+export type ServerNodeSyncMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ server_node_sync: {
+ input: ServerNodeSyncMutationInput;
+ output: ServerNodeSyncMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/types/sync.ts b/desktop/src/types/sync.ts
index 5244ef1d..4bbf4401 100644
--- a/desktop/src/types/sync.ts
+++ b/desktop/src/types/sync.ts
@@ -9,22 +9,6 @@ export type ServerSyncChangeResult = {
export type ServerSyncChangeStatus = 'success' | 'error';
-export type ServerNodeSyncData = {
- id: string;
- workspaceId: string;
- state: string;
- createdAt: string;
- updatedAt?: string | null;
- createdBy: string;
- updatedBy?: string | null;
- deletedAt?: string | null;
- deletedBy?: string | null;
- versionId: string;
- serverCreatedAt: string;
- serverUpdatedAt?: string | null;
- serverDeletedAt?: string | null;
-};
-
export type LocalCreateNodeChangeData = {
type: 'node_create';
id: string;
diff --git a/server/package-lock.json b/server/package-lock.json
index 66f46c54..fc36eca8 100644
--- a/server/package-lock.json
+++ b/server/package-lock.json
@@ -20,6 +20,7 @@
"js-sha256": "^0.11.0",
"kafkajs": "^2.2.4",
"kysely": "^0.27.4",
+ "lodash": "^4.17.21",
"multer": "^1.4.5-lts.1",
"pg": "^8.13.0",
"postgres": "^3.4.4",
@@ -34,6 +35,7 @@
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.7",
+ "@types/lodash": "^4.17.12",
"@types/multer": "^1.4.12",
"@types/node": "^22.7.7",
"@types/pg": "^8.11.10",
@@ -2368,6 +2370,13 @@
"@types/node": "*"
}
},
+ "node_modules/@types/lodash": {
+ "version": "4.17.12",
+ "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.12.tgz",
+ "integrity": "sha512-sviUmCE8AYdaF/KIHLDJBQgeYzPBI0vf/17NaYehBJfYD1j6/L95Slh07NlyK2iNyBNaEkb3En2jRt+a8y3xZQ==",
+ "dev": true,
+ "license": "MIT"
+ },
"node_modules/@types/mime": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz",
@@ -3994,7 +4003,6 @@
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
- "dev": true,
"license": "MIT"
},
"node_modules/lodash.defaults": {
diff --git a/server/package.json b/server/package.json
index 0c352346..d58218d9 100644
--- a/server/package.json
+++ b/server/package.json
@@ -17,6 +17,7 @@
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.7",
+ "@types/lodash": "^4.17.12",
"@types/multer": "^1.4.12",
"@types/node": "^22.7.7",
"@types/pg": "^8.11.10",
@@ -40,6 +41,7 @@
"js-sha256": "^0.11.0",
"kafkajs": "^2.2.4",
"kysely": "^0.27.4",
+ "lodash": "^4.17.21",
"multer": "^1.4.5-lts.1",
"pg": "^8.13.0",
"postgres": "^3.4.4",
diff --git a/server/src/consumers/changes-subcriber.ts b/server/src/consumers/changes-subcriber.ts
index 1dacc29a..635b08a2 100644
--- a/server/src/consumers/changes-subcriber.ts
+++ b/server/src/consumers/changes-subcriber.ts
@@ -1,9 +1,7 @@
import { database } from '@/data/database';
import { redis, CHANNEL_NAMES } from '@/data/redis';
-import { SelectNode } from '@/data/schema';
-import { NodeTypes } from '@/lib/constants';
import { socketManager } from '@/sockets/socket-manager';
-import { ServerNodeChangeEvent, ServerNodeSyncData } from '@/types/sync';
+import { ServerNodeChangeEvent } from '@/types/sync';
export const initChangesSubscriber = async () => {
const subscriber = redis.duplicate();
@@ -14,104 +12,72 @@ export const initChangesSubscriber = async () => {
const handleEvent = async (event: string) => {
const data: ServerNodeChangeEvent = JSON.parse(event);
- let hasWorkspaceConnection = false;
+ const deviceIds: string[] = [];
for (const connection of socketManager.getConnections()) {
- const workspaces = connection.getWorkspaces();
- if (workspaces.has(data.workspaceId)) {
- hasWorkspaceConnection = true;
- break;
- }
+ deviceIds.push(connection.getDeviceId());
}
- if (!hasWorkspaceConnection) {
+ if (deviceIds.length === 0) {
return;
}
- const node = await database
- .selectFrom('nodes')
- .where('id', '=', data.nodeId)
+ const nodeVersions = await database
+ .selectFrom('device_node_versions')
.selectAll()
- .executeTakeFirst();
+ .where((eb) =>
+ eb.and([
+ eb('device_id', 'in', deviceIds),
+ eb('node_id', '=', data.nodeId),
+ ]),
+ )
+ .execute();
- if (!node) {
+ if (nodeVersions.length === 0) {
return;
}
- const ancestorIds = await fetchAncestors(node);
- const collaborators = extractCollaborators(node);
- const nodeSync: ServerNodeSyncData = {
- id: node.id,
- workspaceId: node.workspace_id,
- state: node.state,
- createdAt: node.created_at.toISOString(),
- updatedAt: node.updated_at?.toISOString(),
- createdBy: node.created_by,
- updatedBy: node.updated_by,
- deletedAt: node.deleted_at?.toISOString(),
- deletedBy: node.deleted_by,
- versionId: node.version_id,
- serverCreatedAt: node.server_created_at.toISOString(),
- serverUpdatedAt: node.server_updated_at?.toISOString(),
- serverDeletedAt: node.server_deleted_at?.toISOString(),
- };
-
- for (const connection of socketManager.getConnections()) {
- const workspaces = connection.getWorkspaces();
- if (!workspaces.has(data.workspaceId)) {
- continue;
- }
+ const node = await database
+ .selectFrom('nodes')
+ .select([
+ 'id',
+ 'state',
+ 'created_at',
+ 'created_by',
+ 'updated_at',
+ 'updated_by',
+ 'server_created_at',
+ 'server_updated_at',
+ 'version_id',
+ ])
+ .where('id', '=', data.nodeId)
+ .executeTakeFirst();
- const workspaceUser = workspaces.get(data.workspaceId);
- if (!workspaceUser) {
+ for (const nodeVersion of nodeVersions) {
+ const socketConnection = socketManager.getConnection(nodeVersion.device_id);
+ if (!socketConnection) {
continue;
}
- const isCollaborator = collaborators.includes(workspaceUser.userId);
- const shouldSend =
- node.type === NodeTypes.User ||
- isCollaborator ||
- ancestorIds.some((id) => workspaceUser.nodeIds.includes(id));
-
- if (shouldSend) {
- connection.send({
- type: 'server_nodes_sync',
- nodes: [nodeSync],
+ if (nodeVersion.access_removed_at !== null || !node) {
+ socketConnection.send({
+ type: 'server_node_delete',
+ id: data.nodeId,
+ workspaceId: data.workspaceId,
+ });
+ } else {
+ socketConnection.send({
+ type: 'server_node_sync',
+ id: node.id,
+ workspaceId: data.workspaceId,
+ state: node.state!,
+ createdAt: node.created_at.toISOString(),
+ createdBy: node.created_by,
+ updatedAt: node.updated_at?.toISOString() ?? null,
+ updatedBy: node.updated_by ?? null,
+ serverCreatedAt: node.server_created_at.toISOString(),
+ serverUpdatedAt: node.server_updated_at?.toISOString() ?? null,
+ versionId: node.version_id,
});
}
-
- if (isCollaborator) {
- workspaceUser.nodeIds.push(node.id);
- } else if (workspaceUser.nodeIds.includes(node.id)) {
- workspaceUser.nodeIds = workspaceUser.nodeIds.filter(
- (id) => id !== node.id,
- );
- }
- }
-};
-
-const fetchAncestors = async (node: SelectNode): Promise<string[]> => {
- if (node.type === NodeTypes.User || node.type === NodeTypes.Space) {
- return [];
}
-
- const ancestors = await database
- .selectFrom('node_paths')
- .where('descendant_id', '=', node.id)
- .select(['ancestor_id'])
- .execute();
-
- if (!ancestors) {
- return [];
- }
-
- const ancestorIds = ancestors.map((ancestor) => ancestor.ancestor_id);
- return ancestorIds;
-};
-
-const extractCollaborators = (node: SelectNode): string[] => {
- if (!node.attributes || !node.attributes.collaborators) {
- return [];
- }
-
- return Object.keys(node.attributes.collaborators);
};
diff --git a/server/src/data/migrations.ts b/server/src/data/migrations.ts
index 65a77b02..c3663d56 100644
--- a/server/src/data/migrations.ts
+++ b/server/src/data/migrations.ts
@@ -95,12 +95,9 @@ const createNodesTable: Migration = {
.addColumn('updated_at', 'timestamptz')
.addColumn('created_by', 'varchar(30)', (col) => col.notNull())
.addColumn('updated_by', 'varchar(30)')
- .addColumn('deleted_at', 'timestamptz')
- .addColumn('deleted_by', 'varchar(30)')
.addColumn('version_id', 'varchar(30)', (col) => col.notNull())
.addColumn('server_created_at', 'timestamptz', (col) => col.notNull())
.addColumn('server_updated_at', 'timestamptz')
- .addColumn('server_deleted_at', 'timestamptz')
.execute();
},
down: async (db) => {
@@ -292,8 +289,10 @@ const createDeviceNodeVersionTable: Migration = {
col.notNull().references('devices.id').onDelete('cascade'),
)
.addColumn('node_id', 'varchar(30)', (col) => col.notNull())
+ .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())
.addColumn('version_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('synced_at', 'timestamptz', (col) => col.notNull())
+ .addColumn('synced_at', 'timestamptz')
+ .addColumn('access_removed_at', 'timestamptz')
.addPrimaryKeyConstraint('device_node_versions_pkey', [
'device_id',
'node_id',
diff --git a/server/src/data/schema.ts b/server/src/data/schema.ts
index df104398..3c856bf1 100644
--- a/server/src/data/schema.ts
+++ b/server/src/data/schema.ts
@@ -75,12 +75,9 @@ interface NodeTable {
updated_at: ColumnType<Date | null, Date | null, Date>;
created_by: ColumnType<string, string, never>;
updated_by: ColumnType<string | null, string | null, string>;
- deleted_at: ColumnType<Date | null, Date | null, Date>;
- deleted_by: ColumnType<string | null, string | null, string>;
version_id: ColumnType<string, string, string>;
server_created_at: ColumnType<Date, Date, never>;
server_updated_at: ColumnType<Date | null, Date | null, Date>;
- server_deleted_at: ColumnType<Date | null, Date | null, Date>;
}
export type SelectNode = Selectable<NodeTable>;
@@ -135,7 +132,9 @@ interface DeviceNodeVersionTable {
device_id: ColumnType<string, string, never>;
node_id: ColumnType<string, string, never>;
version_id: ColumnType<string, string, string>;
- synced_at: ColumnType<Date, Date, Date>;
+ workspace_id: ColumnType<string, string, never>;
+ synced_at: ColumnType<Date | null, Date | null, Date>;
+ access_removed_at: ColumnType<Date | null, Date | null, Date>;
}
export type SelectDeviceNodeVersion = Selectable<DeviceNodeVersionTable>;
diff --git a/server/src/index.ts b/server/src/index.ts
index 4f26ee4b..a9eadf6a 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -2,6 +2,7 @@ import { initApi } from '@/api';
import { initRedis } from '@/data/redis';
import { initChangesSubscriber } from '@/consumers/changes-subcriber';
import { migrate } from '@/data/database';
+import { initEventWorker } from '@/queues/events';
migrate().then(() => {
initApi();
@@ -9,6 +10,8 @@ migrate().then(() => {
initRedis().then(() => {
console.log('Redis initialized');
+ initEventWorker();
+
initChangesSubscriber().then(() => {
console.log('Change subscriber started');
});
diff --git a/server/src/lib/nodes.ts b/server/src/lib/nodes.ts
index 22e6acc0..55a63847 100644
--- a/server/src/lib/nodes.ts
+++ b/server/src/lib/nodes.ts
@@ -1,6 +1,6 @@
import { database } from '@/data/database';
import { SelectNode } from '@/data/schema';
-import { ServerNode } from '@/types/nodes';
+import { NodeCollaborator, ServerNode } from '@/types/nodes';
export const mapNode = (node: SelectNode): ServerNode => {
return {
@@ -72,3 +72,38 @@ export const fetchNodeDescendants = async (
return result.map((row) => row.descendant_id);
};
+
+export const fetchNodeCollaborators = async (
+ nodeId: string,
+): Promise<NodeCollaborator[]> => {
+ const result = await database
+ .selectFrom('node_paths as np')
+ .innerJoin('node_collaborators as nc', 'np.ancestor_id', 'nc.node_id')
+ .select([
+ 'np.ancestor_id as node_id',
+ 'np.level as node_level',
+ 'nc.collaborator_id',
+ 'nc.role',
+ ])
+ .where('np.descendant_id', '=', nodeId)
+ .execute();
+
+ return result.map((row) => ({
+ nodeId: row.node_id,
+ level: row.node_level,
+ collaboratorId: row.collaborator_id,
+ role: row.role,
+ }));
+};
+
+export const fetchWorkspaceUsers = async (
+ workspaceId: string,
+): Promise<string[]> => {
+ const result = await database
+ .selectFrom('workspace_users')
+ .select('id')
+ .where('workspace_id', '=', workspaceId)
+ .execute();
+
+ return result.map((row) => row.id);
+};
diff --git a/server/src/messages/index.ts b/server/src/messages/index.ts
index 7049c90b..b40bc970 100644
--- a/server/src/messages/index.ts
+++ b/server/src/messages/index.ts
@@ -1,8 +1,13 @@
import { LocalNodeSyncMessageInput } from '@/messages/local-node-sync';
-import { ServerNodesSyncMessageInput } from '@/messages/server-nodes-sync';
+import { LocalNodeDeleteMessageInput } from '@/messages/local-node-delete';
+import { ServerNodeSyncMessageInput } from '@/messages/server-node-sync';
+import { ServerNodeDeleteMessageInput } from '@/messages/server-node-delete';
+
export interface MessageMap {
local_node_sync: LocalNodeSyncMessageInput;
- server_nodes_sync: ServerNodesSyncMessageInput;
+ local_node_delete: LocalNodeDeleteMessageInput;
+ server_node_sync: ServerNodeSyncMessageInput;
+ server_node_delete: ServerNodeDeleteMessageInput;
}
export type MessageInput = MessageMap[keyof MessageMap];
diff --git a/server/src/messages/local-node-delete.ts b/server/src/messages/local-node-delete.ts
new file mode 100644
index 00000000..94ab698a
--- /dev/null
+++ b/server/src/messages/local-node-delete.ts
@@ -0,0 +1,5 @@
+export type LocalNodeDeleteMessageInput = {
+ type: 'local_node_delete';
+ nodeId: string;
+ workspaceId: string;
+};
diff --git a/server/src/messages/local-node-sync.ts b/server/src/messages/local-node-sync.ts
index 15652965..f021cd6f 100644
--- a/server/src/messages/local-node-sync.ts
+++ b/server/src/messages/local-node-sync.ts
@@ -2,4 +2,5 @@ export type LocalNodeSyncMessageInput = {
type: 'local_node_sync';
nodeId: string;
versionId: string;
+ workspaceId: string;
};
diff --git a/server/src/messages/server-node-delete.ts b/server/src/messages/server-node-delete.ts
new file mode 100644
index 00000000..1c91b0bd
--- /dev/null
+++ b/server/src/messages/server-node-delete.ts
@@ -0,0 +1,5 @@
+export type ServerNodeDeleteMessageInput = {
+ type: 'server_node_delete';
+ id: string;
+ workspaceId: string;
+};
diff --git a/server/src/messages/server-node-sync.ts b/server/src/messages/server-node-sync.ts
new file mode 100644
index 00000000..ee6045fe
--- /dev/null
+++ b/server/src/messages/server-node-sync.ts
@@ -0,0 +1,13 @@
+export type ServerNodeSyncMessageInput = {
+ type: 'server_node_sync';
+ id: string;
+ workspaceId: string;
+ state: string;
+ createdAt: string;
+ createdBy: string;
+ updatedAt: string | null;
+ updatedBy: string | null;
+ serverCreatedAt: string;
+ serverUpdatedAt: string | null;
+ versionId: string;
+};
diff --git a/server/src/messages/server-nodes-sync.ts b/server/src/messages/server-nodes-sync.ts
deleted file mode 100644
index 0031494d..00000000
--- a/server/src/messages/server-nodes-sync.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { ServerNodeSyncData } from '@/types/sync';
-
-export type ServerNodesSyncMessageInput = {
- type: 'server_nodes_sync';
- nodes: ServerNodeSyncData[];
-};
diff --git a/server/src/queues/events.ts b/server/src/queues/events.ts
new file mode 100644
index 00000000..8a943644
--- /dev/null
+++ b/server/src/queues/events.ts
@@ -0,0 +1,213 @@
+import { database } from '@/data/database';
+import { CHANNEL_NAMES, redis } from '@/data/redis';
+import { CreateDeviceNodeVersion } from '@/data/schema';
+import { NodeTypes } from '@/lib/constants';
+import { fetchNodeCollaborators, fetchWorkspaceUsers } from '@/lib/nodes';
+import {
+ NodeCreatedEvent,
+ NodeDeletedEvent,
+ NodeEvent,
+ NodeUpdatedEvent,
+} from '@/types/events';
+import { ServerNodeAttributes } from '@/types/nodes';
+import { Job, Queue, Worker } from 'bullmq';
+import { difference, isEqual } from 'lodash';
+
+const REDIS_HOST = process.env.REDIS_HOST;
+const REDIS_PASSWORD = process.env.REDIS_PASSWORD;
+const REDIS_PORT = process.env.REDIS_PORT;
+const REDIS_DB = process.env.REDIS_DB;
+
+if (!REDIS_HOST || !REDIS_PASSWORD || !REDIS_PORT || !REDIS_DB) {
+ throw new Error('Redis configuration is missing');
+}
+
+const eventQueue = new Queue('events', {
+ connection: {
+ host: REDIS_HOST,
+ password: REDIS_PASSWORD,
+ port: parseInt(REDIS_PORT),
+ db: parseInt(REDIS_DB),
+ },
+ defaultJobOptions: {
+ removeOnComplete: true,
+ },
+});
+
+export const enqueueEvent = async (event: NodeEvent): Promise<void> => {
+ await eventQueue.add('event', event);
+};
+
+export const initEventWorker = () => {
+ return new Worker('events', handleEventJob, {
+ connection: {
+ host: REDIS_HOST,
+ password: REDIS_PASSWORD,
+ port: parseInt(REDIS_PORT),
+ db: parseInt(REDIS_DB),
+ },
+ });
+};
+
+const handleEventJob = async (job: Job) => {
+ const event = job.data as NodeEvent;
+
+ switch (event.type) {
+ case 'node_created':
+ return handleNodeCreatedEvent(event);
+ case 'node_updated':
+ return handleNodeUpdatedEvent(event);
+ case 'node_deleted':
+ return handleNodeDeletedEvent(event);
+ }
+};
+
+const handleNodeCreatedEvent = async (
+ event: NodeCreatedEvent,
+): Promise<void> => {
+ const collaboratorIds: string[] = [];
+
+ if (event.attributes.type === NodeTypes.User) {
+ const userIds = await fetchWorkspaceUsers(event.workspaceId);
+ collaboratorIds.push(...userIds);
+ } else {
+ const collaborators = await fetchNodeCollaborators(event.id);
+ collaboratorIds.push(...collaborators.map((c) => c.collaboratorId));
+ }
+
+ if (collaboratorIds.length === 0) {
+ return;
+ }
+
+ const deviceIds = await getDeviceIdsForCollaborators(collaboratorIds);
+ await database
+ .insertInto('device_node_versions')
+ .values(
+ deviceIds.map((deviceId) => ({
+ device_id: deviceId,
+ node_id: event.id,
+ version_id: 'null',
+ workspace_id: event.workspaceId,
+ synced_at: null,
+ access_removed_at: null,
+ })),
+ )
+ .onConflict((cb) => cb.doNothing())
+ .execute();
+
+ await publishChange(event.id, event.workspaceId);
+};
+
+const handleNodeUpdatedEvent = async (
+ event: NodeUpdatedEvent,
+): Promise<void> => {
+ const beforeCollaborators = extractCollaboratorIds(event.beforeAttributes);
+ const afterCollaborators = extractCollaboratorIds(event.afterAttributes);
+
+ const addedCollaborators = difference(
+ afterCollaborators,
+ beforeCollaborators,
+ );
+
+ const removedCollaborators = difference(
+ beforeCollaborators,
+ afterCollaborators,
+ );
+
+ if (addedCollaborators.length > 0) {
+ const deviceIds = await getDeviceIdsForCollaborators(addedCollaborators);
+ const descendants = await database
+ .selectFrom('node_paths')
+ .select('descendant_id')
+ .where('ancestor_id', '=', event.id)
+ .execute();
+
+ const descendantIds = descendants.map((d) => d.descendant_id);
+ const deviceNodeVersionsToCreated: CreateDeviceNodeVersion[] = [];
+ for (const deviceId of deviceIds) {
+ for (const descendantId of descendantIds) {
+ deviceNodeVersionsToCreated.push({
+ device_id: deviceId,
+ node_id: descendantId,
+ version_id: 'null',
+ workspace_id: event.workspaceId,
+ synced_at: null,
+ access_removed_at: null,
+ });
+ }
+ }
+
+ if (deviceNodeVersionsToCreated.length > 0) {
+ await database
+ .insertInto('device_node_versions')
+ .values(deviceNodeVersionsToCreated)
+ .onConflict((cb) => cb.doNothing())
+ .execute();
+ }
+ }
+
+ if (removedCollaborators.length > 0) {
+ const deviceIds = await getDeviceIdsForCollaborators(removedCollaborators);
+
+ await database
+ .updateTable('device_node_versions')
+ .set({
+ access_removed_at: new Date(),
+ })
+ .where((eb) =>
+ eb.and([
+ eb('device_id', 'in', deviceIds),
+ eb(
+ 'node_id',
+ 'in',
+ eb
+ .selectFrom('node_paths')
+ .select('descendant_id')
+ .where('ancestor_id', '=', event.id),
+ ),
+ ]),
+ )
+ .execute();
+ }
+
+ await publishChange(event.id, event.workspaceId);
+};
+
+const extractCollaboratorIds = (collaborators: ServerNodeAttributes) => {
+ if (!collaborators.collaborators) {
+ return [];
+ }
+
+ return Object.keys(collaborators.collaborators).sort();
+};
+
+const handleNodeDeletedEvent = async (
+ event: NodeDeletedEvent,
+): Promise<void> => {
+ await publishChange(event.id, event.workspaceId);
+};
+
+const getDeviceIdsForCollaborators = async (
+ collaboratorIds: string[],
+): Promise<string[]> => {
+ const devices = await database
+ .selectFrom('devices as d')
+ .select('d.id')
+ .innerJoin('workspace_users as wu', 'd.account_id', 'wu.account_id')
+ .where('wu.id', 'in', collaboratorIds)
+ .execute();
+
+ return devices.map((d) => d.id);
+};
+
+const publishChange = async (
+ nodeId: string,
+ workspaceId: string,
+): Promise<void> => {
+ const changeJson = JSON.stringify({
+ nodeId,
+ workspaceId,
+ });
+
+ await redis.publish(CHANNEL_NAMES.CHANGES, changeJson);
+};
diff --git a/server/src/routes/sync.ts b/server/src/routes/sync.ts
index d49e9121..b4127fdd 100644
--- a/server/src/routes/sync.ts
+++ b/server/src/routes/sync.ts
@@ -16,7 +16,12 @@ import { fetchCollaboratorRole } from '@/lib/nodes';
import { ServerNodeAttributes } from '@/types/nodes';
import { fromUint8Array, toUint8Array } from 'js-base64';
import * as Y from 'yjs';
-import { CHANNEL_NAMES, redis } from '@/data/redis';
+import {
+ NodeCreatedEvent,
+ NodeDeletedEvent,
+ NodeUpdatedEvent,
+} from '@/types/events';
+import { enqueueEvent } from '@/queues/events';
export const syncRouter = Router();
@@ -158,7 +163,18 @@ const handleCreateNodeChange = async (
})
.execute();
- await publishChange(changeData.id, workspaceUser.workspace_id);
+ const event: NodeCreatedEvent = {
+ type: 'node_created',
+ id: changeData.id,
+ workspaceId: workspaceUser.workspace_id,
+ attributes: attributes,
+ createdBy: changeData.createdBy,
+ createdAt: changeData.createdAt,
+ serverCreatedAt: new Date().toISOString(),
+ versionId: changeData.versionId,
+ };
+
+ await enqueueEvent(event);
return {
status: 'success',
@@ -171,7 +187,7 @@ const handleUpdateNodeChange = async (
): Promise<SyncLocalChangeResult> => {
const existingNode = await database
.selectFrom('nodes')
- .select(['id', 'workspace_id', 'state'])
+ .select(['id', 'workspace_id', 'attributes', 'state'])
.where('id', '=', changeData.id)
.executeTakeFirst();
@@ -202,13 +218,14 @@ const handleUpdateNodeChange = async (
}
const attributesMap = doc.getMap('attributes');
- const attributes = JSON.stringify(attributesMap.toJSON());
+ const attributes = attributesMap.toJSON() as ServerNodeAttributes;
+ const attributesJson = JSON.stringify(attributes);
const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
await database
.updateTable('nodes')
.set({
- attributes: attributes,
+ attributes: attributesJson,
state: encodedState,
updated_at: new Date(changeData.updatedAt),
updated_by: changeData.updatedBy,
@@ -218,7 +235,19 @@ const handleUpdateNodeChange = async (
.where('id', '=', changeData.id)
.execute();
- await publishChange(changeData.id, workspaceUser.workspace_id);
+ 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);
return {
status: 'success',
@@ -255,18 +284,15 @@ const handleDeleteNodeChange = async (
}
await database.deleteFrom('nodes').where('id', '=', changeData.id).execute();
- await publishChange(changeData.id, workspaceUser.workspace_id);
+ const event: NodeDeletedEvent = {
+ type: 'node_deleted',
+ id: changeData.id,
+ workspaceId: workspaceUser.workspace_id,
+ };
+
+ await enqueueEvent(event);
return {
status: 'success',
};
};
-
-const publishChange = async (nodeId: string, workspaceId: string) => {
- const changeJson = JSON.stringify({
- nodeId,
- workspaceId,
- });
-
- await redis.publish(CHANNEL_NAMES.CHANGES, changeJson);
-};
diff --git a/server/src/routes/workspaces.ts b/server/src/routes/workspaces.ts
index c85f7991..686b2b17 100644
--- a/server/src/routes/workspaces.ts
+++ b/server/src/routes/workspaces.ts
@@ -24,6 +24,8 @@ import { getNameFromEmail } from '@/lib/utils';
import { AccountStatus } from '@/types/accounts';
import { ServerNode } from '@/types/nodes';
import { mapNode } from '@/lib/nodes';
+import { NodeCreatedEvent } from '@/types/events';
+import { enqueueEvent } from '@/queues/events';
export const workspacesRouter = Router();
@@ -124,6 +126,19 @@ workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {
.execute();
});
+ const userEvent: NodeCreatedEvent = {
+ type: 'node_created',
+ id: userId,
+ workspaceId: workspaceId,
+ attributes: JSON.parse(userAttributes),
+ createdBy: account.id,
+ createdAt: createdAt.toISOString(),
+ versionId: userVersionId,
+ serverCreatedAt: createdAt.toISOString(),
+ };
+
+ await enqueueEvent(userEvent);
+
const output: WorkspaceOutput = {
id: workspaceId,
name: input.name,
@@ -671,6 +686,21 @@ workspacesRouter.post(
if (usersToCreate.length > 0) {
await trx.insertInto('nodes').values(usersToCreate).execute();
+
+ for (const user of usersToCreate) {
+ const userEvent: NodeCreatedEvent = {
+ type: 'node_created',
+ id: user.id,
+ workspaceId: user.workspace_id,
+ attributes: JSON.parse(user.attributes ?? '{}'),
+ createdBy: user.created_by,
+ createdAt: user.created_at.toISOString(),
+ serverCreatedAt: user.server_created_at.toISOString(),
+ versionId: user.version_id,
+ };
+
+ await enqueueEvent(userEvent);
+ }
}
});
}
diff --git a/server/src/sockets/socket-connection.ts b/server/src/sockets/socket-connection.ts
index 0be55e9a..958e4cb8 100644
--- a/server/src/sockets/socket-connection.ts
+++ b/server/src/sockets/socket-connection.ts
@@ -1,106 +1,87 @@
import { database } from '@/data/database';
-import { NodeTypes } from '@/lib/constants';
import { MessageInput } from '@/messages';
import { NeuronRequestAccount } from '@/types/api';
-import { ServerNodeSyncData } from '@/types/sync';
import { WebSocket } from 'ws';
-interface WorkspaceUser {
- userId: string;
- nodeIds: string[];
-}
-
export class SocketConnection {
private readonly socket: WebSocket;
private readonly accountId: string;
private readonly deviceId: string;
- private readonly workspaces: Map<string, WorkspaceUser>;
constructor(socket: WebSocket, account: NeuronRequestAccount) {
this.socket = socket;
this.accountId = account.id;
this.deviceId = account.deviceId;
- this.workspaces = new Map();
socket.on('message', (message) => {
this.handleMessage(message.toString());
});
}
- public send(message: MessageInput) {
+ public send(message: MessageInput): void {
this.socket.send(JSON.stringify(message));
}
- public init() {
- this.fetchWorkspaceUsers().then(() => {
- this.sendPendingChanges();
- });
+ public init(): void {
+ this.sendPendingChanges();
}
- public getWorkspaces(): Map<string, WorkspaceUser> {
- return this.workspaces;
+ public getDeviceId(): string {
+ return this.deviceId;
}
private async handleMessage(message: string): Promise<void> {
const messageInput: MessageInput = JSON.parse(message);
+ console.log(messageInput);
if (messageInput.type === 'local_node_sync') {
await database
- .insertInto('device_node_versions')
- .values({
- device_id: this.deviceId,
- node_id: messageInput.nodeId,
+ .updateTable('device_node_versions')
+ .set({
version_id: messageInput.versionId,
synced_at: new Date(),
})
- .onConflict((ob) =>
- ob.columns(['device_id', 'node_id']).doUpdateSet({
- synced_at: new Date(),
- version_id: messageInput.versionId,
- }),
- )
+ .where('device_id', '=', this.deviceId)
+ .where('node_id', '=', messageInput.nodeId)
+ .execute();
+ } else if (messageInput.type === 'local_node_delete') {
+ await database
+ .deleteFrom('device_node_versions')
+ .where('device_id', '=', this.deviceId)
+ .where('node_id', '=', messageInput.nodeId)
+ .where('workspace_id', '=', messageInput.workspaceId)
.execute();
}
}
private async sendPendingChanges() {
- const workspaceIds = Array.from(this.workspaces.keys());
- const nodeIds = Array.from(this.workspaces.values())
- .map((user) => user.nodeIds)
- .flat();
-
const unsyncedNodes = await database
- .selectFrom('nodes')
- .leftJoin('device_node_versions', (join) =>
- join
- .onRef('nodes.id', '=', 'device_node_versions.node_id')
- .on('device_node_versions.device_id', '=', this.deviceId),
- )
- .selectAll('nodes')
+ .selectFrom('device_node_versions as dnv')
+ .leftJoin('nodes as n', 'n.id', 'dnv.node_id')
+ .select([
+ 'n.id',
+ 'n.state',
+ 'n.created_at',
+ 'n.created_by',
+ 'n.updated_at',
+ 'n.updated_by',
+ 'n.server_created_at',
+ 'n.server_updated_at',
+ 'dnv.access_removed_at',
+ 'n.version_id',
+ 'dnv.node_id',
+ 'dnv.workspace_id',
+ ])
.where((eb) =>
eb.and([
- eb('nodes.workspace_id', 'in', workspaceIds),
- eb.or([
- eb('nodes.type', '=', NodeTypes.User),
- eb(
- 'nodes.id',
- 'in',
- database
- .selectFrom('node_paths')
- .select('descendant_id')
- .where('ancestor_id', 'in', nodeIds),
- ),
- ]),
+ eb('dnv.device_id', '=', this.deviceId),
eb.or([
- eb('device_node_versions.node_id', 'is', null),
- eb(
- 'nodes.version_id',
- '!=',
- eb.ref('device_node_versions.version_id'),
- ),
+ eb('n.id', 'is', null),
+ eb('dnv.version_id', '!=', eb.ref('n.version_id')),
+ eb('dnv.access_removed_at', 'is not', null),
]),
]),
)
- .orderBy('nodes.id', 'asc')
+ .orderBy('n.id', 'asc')
.limit(100)
.execute();
@@ -108,54 +89,28 @@ export class SocketConnection {
return;
}
- const nodes: ServerNodeSyncData[] = [];
- for (const node of unsyncedNodes) {
- nodes.push({
- id: node.id,
- workspaceId: node.workspace_id,
- state: node.state,
- createdAt: node.created_at.toISOString(),
- createdBy: node.created_by,
- versionId: node.version_id,
- serverCreatedAt: node.created_at.toISOString(),
- serverUpdatedAt: node.updated_at?.toISOString() ?? null,
- serverDeletedAt: node.deleted_at?.toISOString() ?? null,
- });
- }
-
- this.send({
- type: 'server_nodes_sync',
- nodes,
- });
- }
-
- private async fetchWorkspaceUsers(): Promise<void> {
- const users = await database
- .selectFrom('workspace_users')
- .select(['id', 'workspace_id'])
- .where('account_id', '=', this.accountId)
- .execute();
-
- if (users.length === 0) {
- return;
- }
-
- const userIds = users.map((user) => user.id);
- const nodes = await database
- .selectFrom('node_collaborators')
- .select(['collaborator_id', 'node_id'])
- .where('collaborator_id', 'in', userIds)
- .execute();
-
- for (const user of users) {
- const userNodeIds = nodes
- .filter((node) => node.collaborator_id === user.id)
- .map((node) => node.node_id);
-
- this.workspaces.set(user.workspace_id, {
- userId: user.id,
- nodeIds: userNodeIds,
- });
+ for (const row of unsyncedNodes) {
+ if (row.id === null) {
+ this.send({
+ type: 'server_node_delete',
+ id: row.node_id,
+ workspaceId: row.workspace_id,
+ });
+ } else {
+ this.send({
+ type: 'server_node_sync',
+ id: row.id,
+ workspaceId: row.workspace_id,
+ state: row.state!,
+ createdAt: row.created_at!.toISOString(),
+ createdBy: row.created_by!,
+ updatedAt: row.updated_at?.toISOString() ?? null,
+ updatedBy: row.updated_by ?? null,
+ serverCreatedAt: row.server_created_at!.toISOString(),
+ serverUpdatedAt: row.server_updated_at?.toISOString() ?? null,
+ versionId: row.version_id!,
+ });
+ }
}
}
}
diff --git a/server/src/sockets/socket-manager.ts b/server/src/sockets/socket-manager.ts
index 73543ad0..a3c85ed1 100644
--- a/server/src/sockets/socket-manager.ts
+++ b/server/src/sockets/socket-manager.ts
@@ -53,6 +53,10 @@ class SocketManager {
return this.sockets.values();
}
+ public getConnection(deviceId: string): SocketConnection | undefined {
+ return this.sockets.get(deviceId);
+ }
+
public send(deviceId: string, message: MessageInput) {
const connection = this.sockets.get(deviceId);
if (!connection) {
diff --git a/server/src/types/events.ts b/server/src/types/events.ts
new file mode 100644
index 00000000..7d574c78
--- /dev/null
+++ b/server/src/types/events.ts
@@ -0,0 +1,32 @@
+import { ServerNodeAttributes } from '@/types/nodes';
+
+export type NodeEvent = NodeCreatedEvent | NodeUpdatedEvent | NodeDeletedEvent;
+
+export type NodeCreatedEvent = {
+ type: 'node_created';
+ id: string;
+ workspaceId: string;
+ attributes: ServerNodeAttributes;
+ createdBy: string;
+ createdAt: string;
+ serverCreatedAt: string;
+ versionId: string;
+};
+
+export type NodeUpdatedEvent = {
+ type: 'node_updated';
+ id: string;
+ workspaceId: string;
+ beforeAttributes: ServerNodeAttributes;
+ afterAttributes: ServerNodeAttributes;
+ updatedBy: string;
+ updatedAt: string;
+ serverUpdatedAt: string;
+ versionId: string;
+};
+
+export type NodeDeletedEvent = {
+ type: 'node_deleted';
+ id: string;
+ workspaceId: string;
+};
diff --git a/server/src/types/nodes.ts b/server/src/types/nodes.ts
index 2b86dcf9..61a258cc 100644
--- a/server/src/types/nodes.ts
+++ b/server/src/types/nodes.ts
@@ -23,29 +23,6 @@ export type ServerNodeAttributes = {
[key: string]: any;
};
-export type ServerNodeCollaborator = {
- nodeId: string;
- collaboratorId: string;
- role: string;
- workspaceId: string;
- createdAt: Date;
- createdBy: string;
- updatedAt?: Date | null;
- updatedBy?: string | null;
- versionId: string;
- serverCreatedAt: Date;
- serverUpdatedAt?: Date | null;
-};
-
-export type ServerNodeReaction = {
- nodeId: string;
- actorId: string;
- reaction: string;
- workspaceId: string;
- createdAt: Date;
- serverCreatedAt: Date;
-};
-
export type NodeBlock = {
type: string;
text?: string | null;
@@ -56,3 +33,10 @@ export type NodeBlockMark = {
type: string;
attrs: any;
};
+
+export type NodeCollaborator = {
+ nodeId: string;
+ level: number;
+ collaboratorId: string;
+ role: string;
+};
diff --git a/server/src/types/sync.ts b/server/src/types/sync.ts
index ac9b5685..06e418cb 100644
--- a/server/src/types/sync.ts
+++ b/server/src/types/sync.ts
@@ -49,22 +49,6 @@ export type LocalNodeChangeData =
| LocalUpdateNodeChangeData
| LocalDeleteNodeChangeData;
-export type ServerNodeSyncData = {
- id: string;
- workspaceId: string;
- state: string;
- createdAt: string;
- updatedAt?: string | null;
- createdBy: string;
- updatedBy?: string | null;
- deletedAt?: string | null;
- deletedBy?: string | null;
- versionId: string;
- serverCreatedAt: string;
- serverUpdatedAt?: string | null;
- serverDeletedAt?: string | null;
-};
-
export type ServerNodeChangeEvent = {
workspaceId: string;
nodeId: string;