Skip to content

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

Improve synchronization flow

Hakan Shehu4df3aefc41f2
diff --git a/desktop/package-lock.json b/desktop/package-lock.json
index 1045d34d..47a7d9dc 100644
--- a/desktop/package-lock.json
+++ b/desktop/package-lock.json
@@ -118,7 +118,7 @@
"prettier-plugin-tailwindcss": "^0.6.8",
"tailwindcss": "^3.4.13",
"ts-node": "^10.9.2",
- "typescript": "^5.6.2",
+ "typescript": "^5.6.3",
"vite": "^5.4.8"
}
},
@@ -13590,9 +13590,9 @@
}
},
"node_modules/typescript": {
- "version": "5.6.2",
- "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz",
- "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==",
+ "version": "5.6.3",
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz",
+ "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==",
"devOptional": true,
"license": "Apache-2.0",
"bin": {
diff --git a/desktop/package.json b/desktop/package.json
index 8670de8e..033246a2 100644
--- a/desktop/package.json
+++ b/desktop/package.json
@@ -40,7 +40,7 @@
"prettier-plugin-tailwindcss": "^0.6.8",
"tailwindcss": "^3.4.13",
"ts-node": "^10.9.2",
- "typescript": "^5.6.2",
+ "typescript": "^5.6.3",
"vite": "^5.4.8"
},
"keywords": [],
diff --git a/desktop/src/main/handlers/messages/index.ts b/desktop/src/main/handlers/messages/index.ts
index fb24d8ba..0df054f7 100644
--- a/desktop/src/main/handlers/messages/index.ts
+++ b/desktop/src/main/handlers/messages/index.ts
@@ -1,6 +1,7 @@
import { MessageHandler, MessageMap } from '@/operations/messages';
import { ServerChangeMessageHandler } from '@/main/handlers/messages/server-change';
-import { ServerChangeAckMessageHandler } from '@/main/handlers/messages/server-change-ack';
+import { ServerChangeResultMessageHandler } from '@/main/handlers/messages/server-change-result';
+import { ServerChangeBatchMessageHandler } from '@/main/handlers/messages/server-change-batch';
type MessageHandlerMap = {
[K in keyof MessageMap]: MessageHandler<MessageMap[K]>;
@@ -8,5 +9,6 @@ type MessageHandlerMap = {
export const messageHandlerMap: MessageHandlerMap = {
server_change: new ServerChangeMessageHandler(),
- server_change_ack: new ServerChangeAckMessageHandler(),
+ server_change_result: new ServerChangeResultMessageHandler(),
+ server_change_batch: new ServerChangeBatchMessageHandler(),
};
diff --git a/desktop/src/main/handlers/messages/server-change-batch.ts b/desktop/src/main/handlers/messages/server-change-batch.ts
new file mode 100644
index 00000000..ad38af38
--- /dev/null
+++ b/desktop/src/main/handlers/messages/server-change-batch.ts
@@ -0,0 +1,16 @@
+import { MessageContext, MessageHandler } from '@/operations/messages';
+import { ServerChangeBatchMessageInput } from '@/operations/messages/server-change-batch';
+import { synchronizer } from '@/main/synchronizer';
+
+export class ServerChangeBatchMessageHandler
+ implements MessageHandler<ServerChangeBatchMessageInput>
+{
+ public async handleMessage(
+ context: MessageContext,
+ input: ServerChangeBatchMessageInput,
+ ): Promise<void> {
+ for (const change of input.changes) {
+ await synchronizer.handleServerChange(context.accountId, change);
+ }
+ }
+}
diff --git a/desktop/src/main/handlers/messages/server-change-ack.ts b/desktop/src/main/handlers/messages/server-change-result.ts
similarity index 53%
rename from desktop/src/main/handlers/messages/server-change-ack.ts
rename to desktop/src/main/handlers/messages/server-change-result.ts
index 4afd8bb0..4ddeef9c 100644
--- a/desktop/src/main/handlers/messages/server-change-ack.ts
+++ b/desktop/src/main/handlers/messages/server-change-result.ts
@@ -1,13 +1,13 @@
import { MessageContext, MessageHandler } from '@/operations/messages';
-import { ServerChangeAckMessageInput } from '@/operations/messages/server-change-ack';
+import { ServerChangeResultMessageInput } from '@/operations/messages/server-change-result';
import { socketManager } from '@/main/sockets/socket-manager';
-export class ServerChangeAckMessageHandler
- implements MessageHandler<ServerChangeAckMessageInput>
+export class ServerChangeResultMessageHandler
+ implements MessageHandler<ServerChangeResultMessageInput>
{
public async handleMessage(
context: MessageContext,
- input: ServerChangeAckMessageInput,
+ input: ServerChangeResultMessageInput,
): Promise<void> {
socketManager.sendMessage(context.accountId, input);
}
diff --git a/desktop/src/main/handlers/messages/server-change.ts b/desktop/src/main/handlers/messages/server-change.ts
index ddc59e3b..b20e5975 100644
--- a/desktop/src/main/handlers/messages/server-change.ts
+++ b/desktop/src/main/handlers/messages/server-change.ts
@@ -1,6 +1,6 @@
import { MessageContext, MessageHandler } from '@/operations/messages';
import { ServerChangeMessageInput } from '@/operations/messages/server-change';
-import { mediator } from '@/main/mediator';
+import { synchronizer } from '@/main/synchronizer';
export class ServerChangeMessageHandler
implements MessageHandler<ServerChangeMessageInput>
@@ -9,53 +9,6 @@ export class ServerChangeMessageHandler
context: MessageContext,
input: ServerChangeMessageInput,
): Promise<void> {
- if (input.change.table === 'nodes' && input.change.workspaceId) {
- await mediator.executeMutation({
- type: 'node_sync',
- id: input.change.id,
- accountId: context.accountId,
- workspaceId: input.change.workspaceId,
- action: input.change.action,
- after: input.change.after,
- before: input.change.before,
- });
- } else if (
- input.change.table === 'node_reactions' &&
- input.change.workspaceId
- ) {
- await mediator.executeMutation({
- type: 'node_reaction_sync',
- id: input.change.id,
- accountId: context.accountId,
- workspaceId: input.change.workspaceId,
- action: input.change.action,
- after: input.change.after,
- before: input.change.before,
- });
- } else if (
- input.change.table === 'node_collaborator' &&
- input.change.workspaceId
- ) {
- await mediator.executeMutation({
- type: 'node_collaborator_sync',
- id: input.change.id,
- accountId: context.accountId,
- workspaceId: input.change.workspaceId,
- action: input.change.action,
- after: input.change.after,
- before: input.change.before,
- });
- }
-
- await mediator.executeMessage(
- {
- accountId: context.accountId,
- deviceId: context.deviceId,
- },
- {
- type: 'server_change_ack',
- changeId: input.change.id,
- },
- );
+ await synchronizer.handleServerChange(context.accountId, input.change);
}
}
diff --git a/desktop/src/main/handlers/mutations/index.ts b/desktop/src/main/handlers/mutations/index.ts
index 9a00cfd1..0c715062 100644
--- a/desktop/src/main/handlers/mutations/index.ts
+++ b/desktop/src/main/handlers/mutations/index.ts
@@ -26,11 +26,16 @@ import { WorkspaceAccountRoleUpdateMutationHandler } from '@/main/handlers/mutat
import { WorkspaceAccountsInviteMutationHandler } from '@/main/handlers/mutations/workspace-accounts-invite';
import { WorkspaceCreateMutationHandler } from '@/main/handlers/mutations/workspace-create';
import { WorkspaceUpdateMutationHandler } from '@/main/handlers/mutations/workspace-update';
-import { NodeSyncMutationHandler } from '@/main/handlers/mutations/node-sync';
-import { NodeReactionSyncMutationHandler } from '@/main/handlers/mutations/node-reaction-sync';
-import { NodeCollaboratorSyncMutationHandler } from '@/main/handlers/mutations/node-collaborator-sync';
import { DocumentSaveMutationHandler } from '@/main/handlers/mutations/document-save';
import { AvatarUploadMutationHandler } from '@/main/handlers/mutations/avatar-upload';
+import { NodeServerCreateMutationHandler } from '@/main/handlers/mutations/node-server-create';
+import { NodeServerUpdateMutationHandler } from '@/main/handlers/mutations/node-server-update';
+import { NodeServerDeleteMutationHandler } from '@/main/handlers/mutations/node-server-delete';
+import { NodeCollaboratorServerCreateMutationHandler } from '@/main/handlers/mutations/node-collaborator-server-create';
+import { NodeCollaboratorServerUpdateMutationHandler } from '@/main/handlers/mutations/node-collaborator-server-update';
+import { NodeCollaboratorServerDeleteMutationHandler } from '@/main/handlers/mutations/node-collaborator-server-delete';
+import { NodeReactionServerCreateMutationHandler } from '@/main/handlers/mutations/node-reaction-server-create';
+import { NodeReactionServerDeleteMutationHandler } from '@/main/handlers/mutations/node-reaction-server-delete';
type MutationHandlerMap = {
[K in keyof MutationMap]: MutationHandler<MutationMap[K]['input']>;
@@ -65,9 +70,17 @@ export const mutationHandlerMap: MutationHandlerMap = {
workspace_accounts_invite: new WorkspaceAccountsInviteMutationHandler(),
workspace_create: new WorkspaceCreateMutationHandler(),
workspace_update: new WorkspaceUpdateMutationHandler(),
- node_sync: new NodeSyncMutationHandler(),
- node_reaction_sync: new NodeReactionSyncMutationHandler(),
- node_collaborator_sync: new NodeCollaboratorSyncMutationHandler(),
document_save: new DocumentSaveMutationHandler(),
avatar_upload: new AvatarUploadMutationHandler(),
+ node_server_create: new NodeServerCreateMutationHandler(),
+ node_server_update: new NodeServerUpdateMutationHandler(),
+ node_server_delete: new NodeServerDeleteMutationHandler(),
+ node_collaborator_server_create:
+ new NodeCollaboratorServerCreateMutationHandler(),
+ node_collaborator_server_update:
+ new NodeCollaboratorServerUpdateMutationHandler(),
+ node_collaborator_server_delete:
+ new NodeCollaboratorServerDeleteMutationHandler(),
+ node_reaction_server_create: new NodeReactionServerCreateMutationHandler(),
+ node_reaction_server_delete: new NodeReactionServerDeleteMutationHandler(),
};
diff --git a/desktop/src/main/handlers/mutations/node-collaborator-server-create.ts b/desktop/src/main/handlers/mutations/node-collaborator-server-create.ts
new file mode 100644
index 00000000..33f38e13
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/node-collaborator-server-create.ts
@@ -0,0 +1,69 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
+import { NodeCollaboratorServerCreateMutationInput } from '@/operations/mutations/node-collaborator-server-create';
+
+export class NodeCollaboratorServerCreateMutationHandler
+ implements MutationHandler<NodeCollaboratorServerCreateMutationInput>
+{
+ public async handleMutation(
+ input: NodeCollaboratorServerCreateMutationInput,
+ ): Promise<MutationResult<NodeCollaboratorServerCreateMutationInput>> {
+ 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
+ .insertInto('node_collaborators')
+ .values({
+ node_id: input.nodeId,
+ collaborator_id: input.collaboratorId,
+ role: input.role,
+ 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_version_id: input.versionId,
+ })
+ .where('server_version_id', 'is', null),
+ )
+ .execute();
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'nodes',
+ userId: userId,
+ },
+ ],
+ };
+ }
+}
diff --git a/desktop/src/main/handlers/mutations/node-collaborator-server-delete.ts b/desktop/src/main/handlers/mutations/node-collaborator-server-delete.ts
new file mode 100644
index 00000000..ada5cad8
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/node-collaborator-server-delete.ts
@@ -0,0 +1,57 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
+import { NodeCollaboratorServerDeleteMutationInput } from '@/operations/mutations/node-collaborator-server-delete';
+
+export class NodeCollaboratorServerDeleteMutationHandler
+ implements MutationHandler<NodeCollaboratorServerDeleteMutationInput>
+{
+ public async handleMutation(
+ input: NodeCollaboratorServerDeleteMutationInput,
+ ): Promise<MutationResult<NodeCollaboratorServerDeleteMutationInput>> {
+ 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('node_collaborators')
+ .where((eb) =>
+ eb.and([
+ eb('node_id', '=', input.nodeId),
+ eb('collaborator_id', '=', input.collaboratorId),
+ ]),
+ )
+ .execute();
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'node_collaborators',
+ userId: userId,
+ },
+ ],
+ };
+ }
+}
diff --git a/desktop/src/main/handlers/mutations/node-collaborator-server-update.ts b/desktop/src/main/handlers/mutations/node-collaborator-server-update.ts
new file mode 100644
index 00000000..8cac7e03
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/node-collaborator-server-update.ts
@@ -0,0 +1,96 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
+import { NodeCollaboratorServerUpdateMutationInput } from '@/operations/mutations/node-collaborator-server-update';
+
+export class NodeCollaboratorServerUpdateMutationHandler
+ implements MutationHandler<NodeCollaboratorServerUpdateMutationInput>
+{
+ public async handleMutation(
+ input: NodeCollaboratorServerUpdateMutationInput,
+ ): Promise<MutationResult<NodeCollaboratorServerUpdateMutationInput>> {
+ 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 nodeCollaborator = await workspaceDatabase
+ .selectFrom('node_collaborators')
+ .selectAll()
+ .where((eb) =>
+ eb.and([
+ eb('node_id', '=', input.nodeId),
+ eb('collaborator_id', '=', input.collaboratorId),
+ ]),
+ )
+ .executeTakeFirst();
+
+ if (nodeCollaborator.server_version_id === input.versionId) {
+ return {
+ output: {
+ success: true,
+ },
+ };
+ }
+
+ if (nodeCollaborator.updated_at) {
+ const localUpdatedAt = new Date(nodeCollaborator.updated_at);
+ const serverUpdatedAt = new Date(input.updatedAt);
+ if (localUpdatedAt > serverUpdatedAt) {
+ return {
+ output: {
+ success: true,
+ },
+ };
+ }
+ }
+
+ await workspaceDatabase
+ .updateTable('node_collaborators')
+ .set({
+ role: input.role,
+ updated_at: input.updatedAt,
+ updated_by: input.updatedBy,
+ version_id: input.versionId,
+ server_updated_at: input.serverUpdatedAt,
+ server_version_id: input.versionId,
+ })
+ .where((eb) =>
+ eb.and([
+ eb('node_id', '=', input.nodeId),
+ eb('collaborator_id', '=', input.collaboratorId),
+ ]),
+ )
+ .execute();
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'node_collaborators',
+ userId: userId,
+ },
+ ],
+ };
+ }
+}
diff --git a/desktop/src/main/handlers/mutations/node-collaborator-sync.ts b/desktop/src/main/handlers/mutations/node-collaborator-sync.ts
deleted file mode 100644
index a52ead33..00000000
--- a/desktop/src/main/handlers/mutations/node-collaborator-sync.ts
+++ /dev/null
@@ -1,160 +0,0 @@
-import { databaseManager } from '@/main/data/database-manager';
-import { MutationHandler, MutationResult } from '@/operations/mutations';
-import { NodeCollaboratorSyncMutationInput } from '@/operations/mutations/node-collaborator-sync';
-import { ServerNodeCollaborator, ServerNodeReaction } from '@/types/nodes';
-
-export class NodeCollaboratorSyncMutationHandler
- implements MutationHandler<NodeCollaboratorSyncMutationInput>
-{
- public async handleMutation(
- input: NodeCollaboratorSyncMutationInput,
- ): Promise<MutationResult<NodeCollaboratorSyncMutationInput>> {
- 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;
- if (input.action === 'insert' && input.after) {
- await this.insertNodeCollaborator(userId, input.after);
- } else if (input.action === 'update' && input.after) {
- await this.updateNodeCollaborator(userId, input.after);
- } else if (input.action === 'delete' && input.before) {
- await this.deleteNodeReaction(userId, input.before);
- }
-
- return {
- output: {
- success: true,
- },
- changes: [
- {
- type: 'workspace',
- table: 'node_collaborators',
- userId: userId,
- },
- ],
- };
- }
-
- private async insertNodeCollaborator(
- userId: string,
- nodeCollaborator: ServerNodeCollaborator,
- ): Promise<void> {
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- await workspaceDatabase
- .insertInto('node_collaborators')
- .values({
- node_id: nodeCollaborator.nodeId,
- collaborator_id: nodeCollaborator.collaboratorId,
- role: nodeCollaborator.role,
- created_at: nodeCollaborator.createdAt,
- created_by: nodeCollaborator.createdBy,
- updated_by: nodeCollaborator.updatedBy,
- updated_at: nodeCollaborator.updatedAt,
- version_id: nodeCollaborator.versionId,
- server_created_at: nodeCollaborator.serverCreatedAt,
- server_updated_at: nodeCollaborator.serverUpdatedAt,
- server_version_id: nodeCollaborator.versionId,
- })
- .onConflict((cb) =>
- cb
- .doUpdateSet({
- server_created_at: nodeCollaborator.serverCreatedAt,
- server_updated_at: nodeCollaborator.serverUpdatedAt,
- server_version_id: nodeCollaborator.versionId,
- })
- .where('version_id', '=', nodeCollaborator.versionId),
- )
- .execute();
- }
-
- private async updateNodeCollaborator(
- userId: string,
- nodeCollaborator: ServerNodeCollaborator,
- ): Promise<void> {
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- const existingNodeCollaborator = await workspaceDatabase
- .selectFrom('node_collaborators')
- .selectAll()
- .where((eb) =>
- eb.and([
- eb('node_id', '=', nodeCollaborator.nodeId),
- eb('collaborator_id', '=', nodeCollaborator.collaboratorId),
- ]),
- )
- .executeTakeFirst();
-
- if (
- existingNodeCollaborator.server_version_id === nodeCollaborator.versionId
- ) {
- return;
- }
-
- if (existingNodeCollaborator.updated_at) {
- if (!nodeCollaborator.updatedAt) {
- return;
- }
- const localUpdatedAt = new Date(existingNodeCollaborator.updated_at);
- const serverUpdatedAt = new Date(nodeCollaborator.updatedAt);
- if (localUpdatedAt > serverUpdatedAt) {
- return;
- }
- }
-
- await workspaceDatabase
- .updateTable('node_collaborators')
- .set({
- role: nodeCollaborator.role,
- updated_at: nodeCollaborator.updatedAt,
- updated_by: nodeCollaborator.updatedBy,
- version_id: nodeCollaborator.versionId,
- server_created_at: nodeCollaborator.serverCreatedAt,
- server_updated_at: nodeCollaborator.serverUpdatedAt,
- server_version_id: nodeCollaborator.versionId,
- })
- .where((eb) =>
- eb.and([
- eb('node_id', '=', nodeCollaborator.nodeId),
- eb('collaborator_id', '=', nodeCollaborator.collaboratorId),
- ]),
- )
- .execute();
- }
-
- private async deleteNodeReaction(
- userId: string,
- nodeCollaborator: ServerNodeCollaborator,
- ): Promise<void> {
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- await workspaceDatabase
- .deleteFrom('node_collaborators')
- .where((eb) =>
- eb.and([
- eb('node_id', '=', nodeCollaborator.nodeId),
- eb('collaborator_id', '=', nodeCollaborator.collaboratorId),
- ]),
- )
- .execute();
- }
-}
diff --git a/desktop/src/main/handlers/mutations/node-reaction-server-create.ts b/desktop/src/main/handlers/mutations/node-reaction-server-create.ts
new file mode 100644
index 00000000..0796c438
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/node-reaction-server-create.ts
@@ -0,0 +1,65 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
+import { NodeReactionServerCreateMutationInput } from '@/operations/mutations/node-reaction-server-create';
+
+export class NodeReactionServerCreateMutationHandler
+ implements MutationHandler<NodeReactionServerCreateMutationInput>
+{
+ public async handleMutation(
+ input: NodeReactionServerCreateMutationInput,
+ ): Promise<MutationResult<NodeReactionServerCreateMutationInput>> {
+ 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
+ .insertInto('node_reactions')
+ .values({
+ node_id: input.nodeId,
+ actor_id: input.actorId,
+ reaction: input.reaction,
+ created_at: input.createdAt,
+ server_created_at: input.serverCreatedAt,
+ })
+ .onConflict((cb) =>
+ cb
+ .doUpdateSet({
+ server_created_at: input.serverCreatedAt,
+ })
+ .where('server_created_at', 'is', null),
+ )
+ .execute();
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'node_reactions',
+ userId: userId,
+ },
+ ],
+ };
+ }
+}
diff --git a/desktop/src/main/handlers/mutations/node-reaction-server-delete.ts b/desktop/src/main/handlers/mutations/node-reaction-server-delete.ts
new file mode 100644
index 00000000..cb0b53b6
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/node-reaction-server-delete.ts
@@ -0,0 +1,58 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
+import { NodeReactionServerDeleteMutationInput } from '@/operations/mutations/node-reaction-server-delete';
+
+export class NodeReactionServerDeleteMutationHandler
+ implements MutationHandler<NodeReactionServerDeleteMutationInput>
+{
+ public async handleMutation(
+ input: NodeReactionServerDeleteMutationInput,
+ ): Promise<MutationResult<NodeReactionServerDeleteMutationInput>> {
+ 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('node_reactions')
+ .where((eb) =>
+ eb.and([
+ eb('node_id', '=', input.nodeId),
+ eb('actor_id', '=', input.actorId),
+ eb('reaction', '=', input.reaction),
+ ]),
+ )
+ .execute();
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'node_reactions',
+ userId: userId,
+ },
+ ],
+ };
+ }
+}
diff --git a/desktop/src/main/handlers/mutations/node-reaction-sync.ts b/desktop/src/main/handlers/mutations/node-reaction-sync.ts
deleted file mode 100644
index d97a1af6..00000000
--- a/desktop/src/main/handlers/mutations/node-reaction-sync.ts
+++ /dev/null
@@ -1,94 +0,0 @@
-import { databaseManager } from '@/main/data/database-manager';
-import { MutationHandler, MutationResult } from '@/operations/mutations';
-import { NodeReactionSyncMutationInput } from '@/operations/mutations/node-reaction-sync';
-import { ServerNodeReaction } from '@/types/nodes';
-
-export class NodeReactionSyncMutationHandler
- implements MutationHandler<NodeReactionSyncMutationInput>
-{
- public async handleMutation(
- input: NodeReactionSyncMutationInput,
- ): Promise<MutationResult<NodeReactionSyncMutationInput>> {
- 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;
- if (input.action === 'insert' && input.after) {
- await this.insertNodeReaction(userId, input.after);
- } else if (input.action === 'delete' && input.before) {
- await this.deleteNodeReaction(userId, input.before);
- }
-
- return {
- output: {
- success: true,
- },
- changes: [
- {
- type: 'workspace',
- table: 'node_reactions',
- userId: userId,
- },
- ],
- };
- }
-
- private async insertNodeReaction(
- userId: string,
- nodeReaction: ServerNodeReaction,
- ): Promise<void> {
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- await workspaceDatabase
- .insertInto('node_reactions')
- .values({
- node_id: nodeReaction.nodeId,
- actor_id: nodeReaction.actorId,
- reaction: nodeReaction.reaction,
- created_at: nodeReaction.createdAt,
- server_created_at: nodeReaction.serverCreatedAt,
- })
- .onConflict((ob) =>
- ob.doUpdateSet({
- server_created_at: nodeReaction.serverCreatedAt,
- }),
- )
- .execute();
- }
-
- private async deleteNodeReaction(
- userId: string,
- nodeReaction: ServerNodeReaction,
- ): Promise<void> {
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- await workspaceDatabase
- .deleteFrom('node_reactions')
- .where((eb) =>
- eb.and([
- eb('node_id', '=', nodeReaction.nodeId),
- eb('actor_id', '=', nodeReaction.actorId),
- eb('reaction', '=', nodeReaction.reaction),
- ]),
- )
- .execute();
- }
-}
diff --git a/desktop/src/main/handlers/mutations/node-server-create.ts b/desktop/src/main/handlers/mutations/node-server-create.ts
new file mode 100644
index 00000000..ec1a901e
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/node-server-create.ts
@@ -0,0 +1,79 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
+import { NodeServerCreateMutationInput } from '@/operations/mutations/node-server-create';
+import { toUint8Array } from 'js-base64';
+import * as Y from 'yjs';
+
+export class NodeServerCreateMutationHandler
+ implements MutationHandler<NodeServerCreateMutationInput>
+{
+ public async handleMutation(
+ input: NodeServerCreateMutationInput,
+ ): Promise<MutationResult<NodeServerCreateMutationInput>> {
+ 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 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_version_id: input.versionId,
+ })
+ .where('version_id', '=', input.versionId),
+ )
+ .execute();
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'nodes',
+ userId: userId,
+ },
+ ],
+ };
+ }
+}
diff --git a/desktop/src/main/handlers/mutations/node-server-delete.ts b/desktop/src/main/handlers/mutations/node-server-delete.ts
new file mode 100644
index 00000000..0413d2ba
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/node-server-delete.ts
@@ -0,0 +1,52 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
+import { NodeServerDeleteMutationInput } from '@/operations/mutations/node-server-delete';
+
+export class NodeServerDeleteMutationHandler
+ implements MutationHandler<NodeServerDeleteMutationInput>
+{
+ public async handleMutation(
+ input: NodeServerDeleteMutationInput,
+ ): Promise<MutationResult<NodeServerDeleteMutationInput>> {
+ 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();
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'nodes',
+ userId: userId,
+ },
+ ],
+ };
+ }
+}
diff --git a/desktop/src/main/handlers/mutations/node-server-update.ts b/desktop/src/main/handlers/mutations/node-server-update.ts
new file mode 100644
index 00000000..55d3757b
--- /dev/null
+++ b/desktop/src/main/handlers/mutations/node-server-update.ts
@@ -0,0 +1,88 @@
+import { databaseManager } from '@/main/data/database-manager';
+import { MutationHandler, MutationResult } from '@/operations/mutations';
+import { NodeServerUpdateMutationInput } from '@/operations/mutations/node-server-update';
+import { fromUint8Array, toUint8Array } from 'js-base64';
+import * as Y from 'yjs';
+
+export class NodeServerUpdateMutationHandler
+ implements MutationHandler<NodeServerUpdateMutationInput>
+{
+ public async handleMutation(
+ input: NodeServerUpdateMutationInput,
+ ): Promise<MutationResult<NodeServerUpdateMutationInput>> {
+ 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 node = await workspaceDatabase
+ .selectFrom('nodes')
+ .selectAll()
+ .where('id', '=', input.id)
+ .executeTakeFirst();
+
+ if (!node) {
+ return {
+ output: {
+ success: false,
+ },
+ };
+ }
+
+ const doc = new Y.Doc({
+ guid: input.id,
+ });
+
+ Y.applyUpdate(doc, toUint8Array(node.state));
+ Y.applyUpdate(doc, toUint8Array(input.update));
+
+ const attributesMap = doc.getMap('attributes');
+ const attributes = JSON.stringify(attributesMap.toJSON());
+ const state = fromUint8Array(Y.encodeStateAsUpdate(doc));
+
+ await workspaceDatabase
+ .updateTable('nodes')
+ .set({
+ attributes: attributes,
+ state: state,
+ updated_at: input.updatedAt,
+ updated_by: input.updatedBy,
+ version_id: input.versionId,
+ server_version_id: input.versionId,
+ server_updated_at: input.serverUpdatedAt,
+ })
+ .where('id', '=', input.id)
+ .execute();
+
+ return {
+ output: {
+ success: true,
+ },
+ changes: [
+ {
+ type: 'workspace',
+ table: 'nodes',
+ userId: userId,
+ },
+ ],
+ };
+ }
+}
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 338fb83a..00000000
--- a/desktop/src/main/handlers/mutations/node-sync.ts
+++ /dev/null
@@ -1,137 +0,0 @@
-import { databaseManager } from '@/main/data/database-manager';
-import { MutationHandler, MutationResult } from '@/operations/mutations';
-import { NodeSyncMutationInput } from '@/operations/mutations/node-sync';
-import { ServerNode } from '@/types/nodes';
-import { fromUint8Array, toUint8Array } from 'js-base64';
-import * as Y from 'yjs';
-
-export class NodeSyncMutationHandler
- implements MutationHandler<NodeSyncMutationInput>
-{
- public async handleMutation(
- input: NodeSyncMutationInput,
- ): Promise<MutationResult<NodeSyncMutationInput>> {
- 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;
- if (input.action === 'insert' && input.after) {
- await this.insertNode(userId, input.after);
- } else if (input.action === 'update' && input.after) {
- await this.updateNode(userId, input.after);
- } else if (input.action === 'delete' && input.before) {
- await this.deleteNode(userId, input.before);
- }
-
- return {
- output: {
- success: true,
- },
- changes: [
- {
- type: 'workspace',
- table: 'nodes',
- userId: userId,
- },
- ],
- };
- }
-
- private async insertNode(userId: string, node: ServerNode): Promise<void> {
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- await workspaceDatabase
- .insertInto('nodes')
- .values({
- id: node.id,
- attributes: JSON.stringify(node.attributes),
- state: node.state,
- created_at: node.createdAt,
- created_by: node.createdBy,
- updated_by: node.updatedBy,
- updated_at: node.updatedAt,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- 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();
- }
-
- private async updateNode(userId: string, node: ServerNode): Promise<void> {
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- const existingNode = await workspaceDatabase
- .selectFrom('nodes')
- .selectAll()
- .where('id', '=', node.id)
- .executeTakeFirst();
-
- if (existingNode.version_id === node.versionId) {
- return;
- }
-
- 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());
- const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));
-
- await workspaceDatabase
- .updateTable('nodes')
- .set({
- attributes: attributes,
- state: encodedState,
- updated_at: node.updatedAt,
- updated_by: node.updatedBy,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.versionId,
- })
- .where('id', '=', node.id)
- .execute();
- }
-
- private async deleteNode(userId: string, node: ServerNode): Promise<void> {
- const workspaceDatabase =
- await databaseManager.getWorkspaceDatabase(userId);
-
- await workspaceDatabase
- .deleteFrom('nodes')
- .where('id', '=', node.id)
- .execute();
- }
-}
diff --git a/desktop/src/main/synchronizer.ts b/desktop/src/main/synchronizer.ts
index 7702a8e0..6cd1adec 100644
--- a/desktop/src/main/synchronizer.ts
+++ b/desktop/src/main/synchronizer.ts
@@ -1,8 +1,12 @@
import { BackoffCalculator } from '@/lib/backoff-calculator';
import { buildAxiosInstance } from '@/lib/servers';
-import { SelectWorkspace } from '@/main/data/app/schema';
import { databaseManager } from '@/main/data/database-manager';
-import { ServerSyncResponse, WorkspaceSyncData } from '@/types/sync';
+import {
+ ServerChange,
+ ServerChangeData,
+ ServerSyncResponse,
+} from '@/types/sync';
+import { mediator } from '@/main/mediator';
const EVENT_LOOP_INTERVAL = 100;
@@ -26,7 +30,6 @@ class Synchronizer {
private async executeEventLoop() {
try {
- await this.checkForWorkspaceSyncs();
await this.checkForWorkspaceChanges();
} catch (error) {
console.log('error', error);
@@ -35,19 +38,140 @@ class Synchronizer {
setTimeout(this.executeEventLoop, EVENT_LOOP_INTERVAL);
}
- private async checkForWorkspaceSyncs(): Promise<void> {
- const unsyncedWorkspaces = await databaseManager.appDatabase
- .selectFrom('workspaces')
- .selectAll()
- .where('synced', '=', 0)
- .execute();
-
- if (unsyncedWorkspaces.length === 0) {
- return;
+ public async handleServerChange(accountId: string, change: ServerChange) {
+ const executed = await this.executeServerChange(accountId, change.data);
+ if (executed) {
+ await mediator.executeMessage(
+ {
+ accountId,
+ deviceId: change.deviceId,
+ },
+ {
+ type: 'server_change_result',
+ changeId: change.id,
+ success: executed,
+ },
+ );
}
+ }
- for (const workspace of unsyncedWorkspaces) {
- await this.syncWorkspace(workspace);
+ private async executeServerChange(
+ accountId: string,
+ data: ServerChangeData,
+ ): Promise<boolean> {
+ switch (data.type) {
+ case 'node_create': {
+ const result = await mediator.executeMutation({
+ type: 'node_server_create',
+ accountId: accountId,
+ workspaceId: data.workspaceId,
+ id: data.id,
+ state: data.state,
+ createdAt: data.createdAt,
+ serverCreatedAt: data.serverCreatedAt,
+ createdBy: data.createdBy,
+ versionId: data.versionId,
+ });
+
+ return result.success;
+ }
+ case 'node_update': {
+ const result = await mediator.executeMutation({
+ type: 'node_server_update',
+ accountId: accountId,
+ workspaceId: data.workspaceId,
+ id: data.id,
+ update: data.update,
+ updatedAt: data.updatedAt,
+ updatedBy: data.updatedBy,
+ versionId: data.versionId,
+ serverUpdatedAt: data.serverUpdatedAt,
+ });
+
+ return result.success;
+ }
+ case 'node_delete': {
+ const result = await mediator.executeMutation({
+ type: 'node_server_delete',
+ accountId: accountId,
+ workspaceId: data.workspaceId,
+ id: data.id,
+ });
+
+ return result.success;
+ }
+ case 'node_collaborator_create': {
+ const result = await mediator.executeMutation({
+ type: 'node_collaborator_server_create',
+ accountId: accountId,
+ workspaceId: data.workspaceId,
+ nodeId: data.nodeId,
+ collaboratorId: data.collaboratorId,
+ role: data.role,
+ createdAt: data.createdAt,
+ createdBy: data.createdBy,
+ versionId: data.versionId,
+ serverCreatedAt: data.serverCreatedAt,
+ });
+
+ return result.success;
+ }
+ case 'node_collaborator_update': {
+ const result = await mediator.executeMutation({
+ type: 'node_collaborator_server_update',
+ accountId: accountId,
+ workspaceId: data.workspaceId,
+ nodeId: data.nodeId,
+ collaboratorId: data.collaboratorId,
+ role: data.role,
+ updatedAt: data.updatedAt,
+ updatedBy: data.updatedBy,
+ versionId: data.versionId,
+ serverUpdatedAt: data.serverUpdatedAt,
+ });
+
+ return result.success;
+ }
+ case 'node_collaborator_delete': {
+ const result = await mediator.executeMutation({
+ type: 'node_collaborator_server_delete',
+ accountId: accountId,
+ workspaceId: data.workspaceId,
+ nodeId: data.nodeId,
+ collaboratorId: data.collaboratorId,
+ });
+
+ return result.success;
+ }
+ case 'node_reaction_create': {
+ const result = await mediator.executeMutation({
+ type: 'node_reaction_server_create',
+ accountId: accountId,
+ workspaceId: data.workspaceId,
+ nodeId: data.nodeId,
+ actorId: data.actorId,
+ reaction: data.reaction,
+ createdAt: data.createdAt,
+ serverCreatedAt: data.serverCreatedAt,
+ });
+
+ return result.success;
+ }
+ case 'node_reaction_delete': {
+ const result = await mediator.executeMutation({
+ type: 'node_reaction_server_delete',
+ accountId: accountId,
+ workspaceId: data.workspaceId,
+ nodeId: data.nodeId,
+ actorId: data.actorId,
+ reaction: data.reaction,
+ });
+
+ return result.success;
+ }
+ default: {
+ return false;
+ }
}
}
@@ -63,7 +187,6 @@ class Synchronizer {
'servers.domain',
'servers.attributes',
])
- .where('workspaces.synced', '=', 1)
.execute();
for (const workspace of workspaces) {
@@ -147,127 +270,6 @@ class Synchronizer {
}
}
}
-
- private async syncWorkspace(workspace: SelectWorkspace): Promise<void> {
- if (this.workspaceBackoffs.has(workspace.user_id)) {
- const backoff = this.workspaceBackoffs.get(workspace.user_id);
- if (!backoff.canRetry()) {
- return;
- }
- }
-
- try {
- const credentials = await databaseManager.appDatabase
- .selectFrom('accounts')
- .innerJoin('servers', 'accounts.server', 'servers.domain')
- .select(['domain', 'attributes', 'token'])
- .where('id', '=', workspace.account_id)
- .executeTakeFirst();
-
- if (!credentials) {
- return;
- }
-
- const axios = buildAxiosInstance(
- credentials.domain,
- credentials.attributes,
- credentials.token,
- );
- const { data } = await axios.get<WorkspaceSyncData>(
- `/v1/sync/${workspace.workspace_id}`,
- );
-
- await databaseManager.deleteWorkspaceDatabase(
- workspace.account_id,
- workspace.workspace_id,
- workspace.user_id,
- );
-
- const workspaceDatabase = await databaseManager.getWorkspaceDatabase(
- workspace.user_id,
- );
-
- await workspaceDatabase.transaction().execute(async (trx) => {
- if (data.nodes.length > 0) {
- await trx
- .insertInto('nodes')
- .values(
- data.nodes.map((node) => {
- return {
- id: node.id,
- attributes: JSON.stringify(node.attributes),
- state: node.state,
- created_at: node.createdAt,
- created_by: node.createdBy,
- updated_at: node.updatedAt,
- updated_by: node.updatedBy,
- version_id: node.versionId,
- server_created_at: node.serverCreatedAt,
- server_updated_at: node.serverUpdatedAt,
- server_version_id: node.versionId,
- };
- }),
- )
- .execute();
- }
-
- if (data.nodeReactions.length > 0) {
- await trx
- .insertInto('node_reactions')
- .values(
- data.nodeReactions.map((nodeReaction) => {
- return {
- node_id: nodeReaction.nodeId,
- actor_id: nodeReaction.actorId,
- reaction: nodeReaction.reaction,
- created_at: nodeReaction.createdAt,
- server_created_at: nodeReaction.serverCreatedAt,
- };
- }),
- )
- .execute();
- }
-
- if (data.nodeCollaborators.length > 0) {
- await trx
- .insertInto('node_collaborators')
- .values(
- data.nodeCollaborators.map((nodeCollaborator) => {
- return {
- node_id: nodeCollaborator.nodeId,
- collaborator_id: nodeCollaborator.collaboratorId,
- role: nodeCollaborator.role,
- created_at: nodeCollaborator.createdAt,
- created_by: nodeCollaborator.createdBy,
- updated_at: nodeCollaborator.updatedAt,
- updated_by: nodeCollaborator.updatedBy,
- version_id: nodeCollaborator.versionId,
- server_created_at: nodeCollaborator.serverCreatedAt,
- server_updated_at: nodeCollaborator.serverUpdatedAt,
- server_version_id: nodeCollaborator.versionId,
- };
- }),
- )
- .execute();
- }
- });
-
- await databaseManager.appDatabase
- .updateTable('workspaces')
- .set({
- synced: 1,
- })
- .where('user_id', '=', workspace.user_id)
- .execute();
- } catch (error) {
- if (!this.workspaceBackoffs.has(workspace.user_id)) {
- this.workspaceBackoffs.set(workspace.user_id, new BackoffCalculator());
- }
-
- const backoff = this.workspaceBackoffs.get(workspace.user_id);
- backoff.increaseError();
- }
- }
}
export const synchronizer = new Synchronizer();
diff --git a/desktop/src/operations/messages/server-change-ack.ts b/desktop/src/operations/messages/server-change-ack.ts
deleted file mode 100644
index 67157770..00000000
--- a/desktop/src/operations/messages/server-change-ack.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-export type ServerChangeAckMessageInput = {
- type: 'server_change_ack';
- changeId: string;
-};
-
-declare module '@/operations/messages' {
- interface MessageMap {
- server_change_ack: ServerChangeAckMessageInput;
- }
-}
diff --git a/desktop/src/operations/messages/server-change-batch.ts b/desktop/src/operations/messages/server-change-batch.ts
new file mode 100644
index 00000000..9ca0074e
--- /dev/null
+++ b/desktop/src/operations/messages/server-change-batch.ts
@@ -0,0 +1,12 @@
+import { ServerChange } from '@/types/sync';
+
+export type ServerChangeBatchMessageInput = {
+ type: 'server_change_batch';
+ changes: ServerChange[];
+};
+
+declare module '@/operations/messages' {
+ interface MessageMap {
+ server_change_batch: ServerChangeBatchMessageInput;
+ }
+}
diff --git a/desktop/src/operations/messages/server-change-result.ts b/desktop/src/operations/messages/server-change-result.ts
new file mode 100644
index 00000000..b9e1140b
--- /dev/null
+++ b/desktop/src/operations/messages/server-change-result.ts
@@ -0,0 +1,11 @@
+export type ServerChangeResultMessageInput = {
+ type: 'server_change_result';
+ changeId: string;
+ success: boolean;
+};
+
+declare module '@/operations/messages' {
+ interface MessageMap {
+ server_change_result: ServerChangeResultMessageInput;
+ }
+}
diff --git a/desktop/src/operations/mutations/node-collaborator-server-create.ts b/desktop/src/operations/mutations/node-collaborator-server-create.ts
new file mode 100644
index 00000000..df802dda
--- /dev/null
+++ b/desktop/src/operations/mutations/node-collaborator-server-create.ts
@@ -0,0 +1,25 @@
+export type NodeCollaboratorServerCreateMutationInput = {
+ type: 'node_collaborator_server_create';
+ accountId: string;
+ workspaceId: string;
+ nodeId: string;
+ collaboratorId: string;
+ role: string;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+ serverCreatedAt: string;
+};
+
+export type NodeCollaboratorServerCreateMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ node_collaborator_server_create: {
+ input: NodeCollaboratorServerCreateMutationInput;
+ output: NodeCollaboratorServerCreateMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/node-collaborator-server-delete.ts b/desktop/src/operations/mutations/node-collaborator-server-delete.ts
new file mode 100644
index 00000000..4065c596
--- /dev/null
+++ b/desktop/src/operations/mutations/node-collaborator-server-delete.ts
@@ -0,0 +1,20 @@
+export type NodeCollaboratorServerDeleteMutationInput = {
+ type: 'node_collaborator_server_delete';
+ accountId: string;
+ workspaceId: string;
+ nodeId: string;
+ collaboratorId: string;
+};
+
+export type NodeCollaboratorServerDeleteMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ node_collaborator_server_delete: {
+ input: NodeCollaboratorServerDeleteMutationInput;
+ output: NodeCollaboratorServerDeleteMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/node-collaborator-server-update.ts b/desktop/src/operations/mutations/node-collaborator-server-update.ts
new file mode 100644
index 00000000..c0a535b8
--- /dev/null
+++ b/desktop/src/operations/mutations/node-collaborator-server-update.ts
@@ -0,0 +1,25 @@
+export type NodeCollaboratorServerUpdateMutationInput = {
+ type: 'node_collaborator_server_update';
+ accountId: string;
+ workspaceId: string;
+ nodeId: string;
+ collaboratorId: string;
+ role: string;
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+ serverUpdatedAt: string;
+};
+
+export type NodeCollaboratorServerUpdateMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ node_collaborator_server_update: {
+ input: NodeCollaboratorServerUpdateMutationInput;
+ output: NodeCollaboratorServerUpdateMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/node-collaborator-sync.ts b/desktop/src/operations/mutations/node-collaborator-sync.ts
deleted file mode 100644
index 6625cc99..00000000
--- a/desktop/src/operations/mutations/node-collaborator-sync.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-export type NodeCollaboratorSyncMutationInput = {
- type: 'node_collaborator_sync';
- accountId: string;
- workspaceId: string;
- id: string;
- action: string;
- before: any;
- after: any;
-};
-
-export type NodeCollaboratorSyncMutationOutput = {
- success: boolean;
-};
-
-declare module '@/operations/mutations' {
- interface MutationMap {
- node_collaborator_sync: {
- input: NodeCollaboratorSyncMutationInput;
- output: NodeCollaboratorSyncMutationOutput;
- };
- }
-}
diff --git a/desktop/src/operations/mutations/node-reaction-server-create.ts b/desktop/src/operations/mutations/node-reaction-server-create.ts
new file mode 100644
index 00000000..3e8a8614
--- /dev/null
+++ b/desktop/src/operations/mutations/node-reaction-server-create.ts
@@ -0,0 +1,23 @@
+export type NodeReactionServerCreateMutationInput = {
+ type: 'node_reaction_server_create';
+ accountId: string;
+ workspaceId: string;
+ nodeId: string;
+ actorId: string;
+ reaction: string;
+ createdAt: string;
+ serverCreatedAt: string;
+};
+
+export type NodeReactionServerCreateMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ node_reaction_server_create: {
+ input: NodeReactionServerCreateMutationInput;
+ output: NodeReactionServerCreateMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/node-reaction-server-delete.ts b/desktop/src/operations/mutations/node-reaction-server-delete.ts
new file mode 100644
index 00000000..20b41683
--- /dev/null
+++ b/desktop/src/operations/mutations/node-reaction-server-delete.ts
@@ -0,0 +1,21 @@
+export type NodeReactionServerDeleteMutationInput = {
+ type: 'node_reaction_server_delete';
+ accountId: string;
+ workspaceId: string;
+ nodeId: string;
+ actorId: string;
+ reaction: string;
+};
+
+export type NodeReactionServerDeleteMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ node_reaction_server_delete: {
+ input: NodeReactionServerDeleteMutationInput;
+ output: NodeReactionServerDeleteMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/node-reaction-sync.ts b/desktop/src/operations/mutations/node-reaction-sync.ts
deleted file mode 100644
index 4d234b05..00000000
--- a/desktop/src/operations/mutations/node-reaction-sync.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-export type NodeReactionSyncMutationInput = {
- type: 'node_reaction_sync';
- accountId: string;
- workspaceId: string;
- id: string;
- action: string;
- before: any;
- after: any;
-};
-
-export type NodeReactionSyncMutationOutput = {
- success: boolean;
-};
-
-declare module '@/operations/mutations' {
- interface MutationMap {
- node_reaction_sync: {
- input: NodeReactionSyncMutationInput;
- output: NodeReactionSyncMutationOutput;
- };
- }
-}
diff --git a/desktop/src/operations/mutations/node-server-create.ts b/desktop/src/operations/mutations/node-server-create.ts
new file mode 100644
index 00000000..db59dd8e
--- /dev/null
+++ b/desktop/src/operations/mutations/node-server-create.ts
@@ -0,0 +1,24 @@
+export type NodeServerCreateMutationInput = {
+ type: 'node_server_create';
+ accountId: string;
+ workspaceId: string;
+ id: string;
+ state: string;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+ serverCreatedAt: string;
+};
+
+export type NodeServerCreateMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ node_server_create: {
+ input: NodeServerCreateMutationInput;
+ output: NodeServerCreateMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/node-server-delete.ts b/desktop/src/operations/mutations/node-server-delete.ts
new file mode 100644
index 00000000..4b120dfd
--- /dev/null
+++ b/desktop/src/operations/mutations/node-server-delete.ts
@@ -0,0 +1,19 @@
+export type NodeServerDeleteMutationInput = {
+ type: 'node_server_delete';
+ accountId: string;
+ workspaceId: string;
+ id: string;
+};
+
+export type NodeServerDeleteMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ node_server_delete: {
+ input: NodeServerDeleteMutationInput;
+ output: NodeServerDeleteMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/node-server-update.ts b/desktop/src/operations/mutations/node-server-update.ts
new file mode 100644
index 00000000..0e70bec9
--- /dev/null
+++ b/desktop/src/operations/mutations/node-server-update.ts
@@ -0,0 +1,24 @@
+export type NodeServerUpdateMutationInput = {
+ type: 'node_server_update';
+ accountId: string;
+ workspaceId: string;
+ id: string;
+ update: string;
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+ serverUpdatedAt: string;
+};
+
+export type NodeServerUpdateMutationOutput = {
+ success: boolean;
+};
+
+declare module '@/operations/mutations' {
+ interface MutationMap {
+ node_server_update: {
+ input: NodeServerUpdateMutationInput;
+ output: NodeServerUpdateMutationOutput;
+ };
+ }
+}
diff --git a/desktop/src/operations/mutations/node-sync.ts b/desktop/src/operations/mutations/node-sync.ts
deleted file mode 100644
index f28f5c03..00000000
--- a/desktop/src/operations/mutations/node-sync.ts
+++ /dev/null
@@ -1,22 +0,0 @@
-export type NodeSyncMutationInput = {
- type: 'node_sync';
- accountId: string;
- workspaceId: string;
- id: string;
- action: string;
- before: any;
- after: any;
-};
-
-export type NodeSyncMutationOutput = {
- success: boolean;
-};
-
-declare module '@/operations/mutations' {
- interface MutationMap {
- node_sync: {
- input: NodeSyncMutationInput;
- output: NodeSyncMutationOutput;
- };
- }
-}
diff --git a/desktop/src/types/sync.ts b/desktop/src/types/sync.ts
index 4bb87e76..dd4de437 100644
--- a/desktop/src/types/sync.ts
+++ b/desktop/src/types/sync.ts
@@ -4,15 +4,6 @@ import {
ServerNodeReaction,
} from '@/types/nodes';
-export type ServerChange = {
- id: string;
- table: string;
- action: string;
- workspaceId: string | null;
- before: any | null;
- after: any | null;
-};
-
export type ServerSyncResponse = {
results: ServerSyncChangeResult[];
};
@@ -29,3 +20,98 @@ export type WorkspaceSyncData = {
nodeReactions: ServerNodeReaction[];
nodeCollaborators: ServerNodeCollaborator[];
};
+
+export type ServerChange = {
+ id: string;
+ workspaceId: string;
+ deviceId: string;
+ data: ServerChangeData;
+ createdAt: string;
+};
+
+export type ServerChangeData =
+ | ServerNodeCreateChangeData
+ | ServerNodeUpdateChangeData
+ | ServerNodeDeleteChangeData
+ | ServerNodeCollaboratorCreateChangeData
+ | ServerNodeCollaboratorUpdateChangeData
+ | ServerNodeCollaboratorDeleteChangeData
+ | ServerNodeReactionCreateChangeData
+ | ServerNodeReactionDeleteChangeData;
+
+export type ServerNodeCreateChangeData = {
+ type: 'node_create';
+ id: string;
+ workspaceId: string;
+ state: string;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+ serverCreatedAt: string;
+};
+
+export type ServerNodeUpdateChangeData = {
+ type: 'node_update';
+ id: string;
+ workspaceId: string;
+ update: string;
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+ serverUpdatedAt: string;
+};
+
+export type ServerNodeDeleteChangeData = {
+ type: 'node_delete';
+ id: string;
+ workspaceId: string;
+};
+
+export type ServerNodeCollaboratorCreateChangeData = {
+ type: 'node_collaborator_create';
+ nodeId: string;
+ collaboratorId: string;
+ role: string;
+ workspaceId: string;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+ serverCreatedAt: string;
+};
+
+export type ServerNodeCollaboratorUpdateChangeData = {
+ type: 'node_collaborator_update';
+ nodeId: string;
+ collaboratorId: string;
+ workspaceId: string;
+ role: string;
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+ serverUpdatedAt: string;
+};
+
+export type ServerNodeCollaboratorDeleteChangeData = {
+ type: 'node_collaborator_delete';
+ nodeId: string;
+ collaboratorId: string;
+ workspaceId: string;
+};
+
+export type ServerNodeReactionCreateChangeData = {
+ type: 'node_reaction_create';
+ nodeId: string;
+ actorId: string;
+ reaction: string;
+ workspaceId: string;
+ createdAt: string;
+ serverCreatedAt: string;
+};
+
+export type ServerNodeReactionDeleteChangeData = {
+ type: 'node_reaction_delete';
+ nodeId: string;
+ actorId: string;
+ reaction: string;
+ workspaceId: string;
+};
diff --git a/server/package-lock.json b/server/package-lock.json
index ea174915..84f01394 100644
--- a/server/package-lock.json
+++ b/server/package-lock.json
@@ -41,7 +41,7 @@
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
- "typescript": "^5.6.2"
+ "typescript": "^5.6.3"
}
},
"node_modules/@aws-crypto/crc32": {
@@ -4974,9 +4974,9 @@
"license": "MIT"
},
"node_modules/typescript": {
- "version": "5.6.2",
- "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.2.tgz",
- "integrity": "sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==",
+ "version": "5.6.3",
+ "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.6.3.tgz",
+ "integrity": "sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==",
"dev": true,
"license": "Apache-2.0",
"bin": {
diff --git a/server/package.json b/server/package.json
index 80eaf2b0..392aaa52 100644
--- a/server/package.json
+++ b/server/package.json
@@ -26,7 +26,7 @@
"prettier": "^3.3.3",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
- "typescript": "^5.6.2"
+ "typescript": "^5.6.3"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.665.0",
diff --git a/server/src/api.ts b/server/src/api.ts
index 66213ba4..214112c4 100644
--- a/server/src/api.ts
+++ b/server/src/api.ts
@@ -8,7 +8,7 @@ import { authMiddleware } from '@/middlewares/auth';
import { syncRouter } from '@/routes/sync';
import { configRouter } from '@/routes/config';
import { avatarsRouter } from '@/routes/avatars';
-import { synapse } from '@/synapse';
+import { socketManager } from '@/sockets/socket-manager';
export const initApi = () => {
const app = express();
@@ -28,7 +28,7 @@ export const initApi = () => {
app.use('/v1/avatars', authMiddleware, avatarsRouter);
const server = http.createServer(app);
- synapse.init(server);
+ socketManager.init(server);
server.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
diff --git a/server/src/consumers/change-cdc.ts b/server/src/consumers/change-cdc.ts
index c8ef9d55..7324c7f6 100644
--- a/server/src/consumers/change-cdc.ts
+++ b/server/src/consumers/change-cdc.ts
@@ -2,7 +2,6 @@ import { kafka, TOPIC_NAMES, CONSUMER_IDS } from '@/data/kafka';
import { CdcMessage, ChangeCdcData } from '@/types/cdc';
import { redis, CHANNEL_NAMES } from '@/data/redis';
import { PostgresOperation } from '@/lib/constants';
-import { database } from '@/data/database';
export const initChangeCdcConsumer = async () => {
const consumer = kafka.consumer({ groupId: CONSUMER_IDS.CHANGE_CDC });
@@ -52,18 +51,7 @@ const handleChangeCreate = async (change: CdcMessage<ChangeCdcData>) => {
};
const handleChangeUpdate = async (change: CdcMessage<ChangeCdcData>) => {
- const changeData = change.after;
- if (!changeData) {
- return;
- }
-
- // if all devices have acknowledged the mutation, delete it
- if (changeData.device_ids == null || changeData.device_ids.length == 0) {
- await database
- .deleteFrom('changes')
- .where('id', '=', changeData.id)
- .execute();
- }
+ console.log('Change update:', change.after?.id);
};
const handleChangeDelete = async (change: CdcMessage<ChangeCdcData>) => {
diff --git a/server/src/consumers/changes-subcriber.ts b/server/src/consumers/changes-subcriber.ts
index c194a62b..5680e51d 100644
--- a/server/src/consumers/changes-subcriber.ts
+++ b/server/src/consumers/changes-subcriber.ts
@@ -1,5 +1,5 @@
import { redis, CHANNEL_NAMES } from '@/data/redis';
-import { synapse } from '@/synapse';
+import { socketManager } from '@/sockets/socket-manager';
import { ServerChange } from '@/types/sync';
import { ChangeCdcData } from '@/types/cdc';
import { ServerChangeMessageInput } from '@/messages/server-change';
@@ -12,17 +12,13 @@ export const initChangesSubscriber = async () => {
const handleMessage = async (message: string) => {
const changeData = JSON.parse(message) as ChangeCdcData;
- if (!changeData.device_ids || !changeData.device_ids.length) {
- return;
- }
const serverChange: ServerChange = {
id: changeData.id,
- action: changeData.action as 'insert' | 'update' | 'delete',
- table: changeData.table,
workspaceId: changeData.workspace_id,
- before: changeData.before ? JSON.parse(changeData.before) : null,
- after: changeData.after ? JSON.parse(changeData.after) : null,
+ deviceId: changeData.device_id,
+ data: JSON.parse(changeData.data),
+ createdAt: changeData.created_at,
};
const input: ServerChangeMessageInput = {
@@ -30,7 +26,5 @@ const handleMessage = async (message: string) => {
change: serverChange,
};
- for (const deviceId of changeData.device_ids) {
- synapse.send(deviceId, input);
- }
+ socketManager.send(changeData.device_id, input);
};
diff --git a/server/src/consumers/node-cdc.ts b/server/src/consumers/node-cdc.ts
index 033015c4..732503aa 100644
--- a/server/src/consumers/node-cdc.ts
+++ b/server/src/consumers/node-cdc.ts
@@ -3,7 +3,11 @@ import { CdcMessage, NodeCdcData } from '@/types/cdc';
import { PostgresOperation } from '@/lib/constants';
import { database } from '@/data/database';
import { generateId, IdType } from '@/lib/id';
-import { ServerNode } from '@/types/nodes';
+import {
+ ServerNodeCreateChangeData,
+ ServerNodeDeleteChangeData,
+ ServerNodeUpdateChangeData,
+} from '@/types/sync';
export const initNodeChangesConsumer = async () => {
const consumer = kafka.consumer({ groupId: CONSUMER_IDS.NODE_CDC });
@@ -54,18 +58,31 @@ const handleNodeCreate = async (change: CdcMessage<NodeCdcData>) => {
return;
}
- const serverNode: ServerNode = mapNode(node);
+ const data: ServerNodeCreateChangeData = {
+ type: 'node_create',
+ id: node.id,
+ workspaceId: node.workspace_id,
+ state: node.state,
+ createdAt: node.created_at,
+ createdBy: node.created_by,
+ serverCreatedAt: node.server_created_at,
+ versionId: node.version_id,
+ };
+
await database
.insertInto('changes')
- .values({
- id: generateId(IdType.Change),
- table: 'nodes',
- action: 'insert',
- workspace_id: node.workspace_id,
- created_at: new Date(),
- after: JSON.stringify(serverNode),
- device_ids: deviceIds,
- })
+ .values(
+ deviceIds.map((deviceId) => {
+ return {
+ id: generateId(IdType.Change),
+ device_id: deviceId,
+ workspace_id: node.workspace_id,
+ data: JSON.stringify(data),
+ created_at: new Date(),
+ retry_count: 0,
+ };
+ }),
+ )
.execute();
};
@@ -80,19 +97,31 @@ const handleNodeUpdate = async (change: CdcMessage<NodeCdcData>) => {
return;
}
- const serverNode: ServerNode = mapNode(node);
+ const data: ServerNodeUpdateChangeData = {
+ type: 'node_update',
+ id: node.id,
+ workspaceId: node.workspace_id,
+ update: node.state,
+ updatedAt: node.updated_at ?? new Date().toISOString(),
+ updatedBy: node.updated_by ?? node.created_by,
+ serverUpdatedAt: node.server_updated_at ?? new Date().toISOString(),
+ versionId: node.version_id,
+ };
+
await database
.insertInto('changes')
- .values({
- id: generateId(IdType.Change),
- table: 'nodes',
- action: 'update',
- workspace_id: node.workspace_id,
- created_at: new Date(),
- before: change.before ? JSON.stringify(change.before) : null,
- after: JSON.stringify(serverNode),
- device_ids: deviceIds,
- })
+ .values(
+ deviceIds.map((deviceId) => {
+ return {
+ id: generateId(IdType.Change),
+ device_id: deviceId,
+ workspace_id: node.workspace_id,
+ data: JSON.stringify(data),
+ created_at: new Date(),
+ retry_count: 0,
+ };
+ }),
+ )
.execute();
};
@@ -107,19 +136,26 @@ const handleNodeDelete = async (change: CdcMessage<NodeCdcData>) => {
return;
}
- const serverNode: ServerNode = mapNode(node);
+ const data: ServerNodeDeleteChangeData = {
+ type: 'node_delete',
+ id: node.id,
+ workspaceId: node.workspace_id,
+ };
+
await database
.insertInto('changes')
- .values({
- id: generateId(IdType.Change),
- table: 'nodes',
- action: 'delete',
- workspace_id: node.workspace_id,
- created_at: new Date(),
- before: JSON.stringify(serverNode),
- after: null,
- device_ids: deviceIds,
- })
+ .values(
+ deviceIds.map((deviceId) => {
+ return {
+ id: generateId(IdType.Change),
+ device_id: deviceId,
+ workspace_id: node.workspace_id,
+ data: JSON.stringify(data),
+ created_at: new Date(),
+ retry_count: 0,
+ };
+ }),
+ )
.execute();
};
@@ -140,24 +176,3 @@ const getDeviceIds = async (workspaceId: string) => {
const deviceIds = accountDevices.map((account) => account.id);
return deviceIds;
};
-
-const mapNode = (node: NodeCdcData): ServerNode => {
- return {
- id: node.id,
- workspaceId: node.workspace_id,
- parentId: node.parent_id,
- type: node.type,
- index: node.index,
- attributes: JSON.parse(node.attributes),
- state: node.state,
- createdAt: new Date(node.created_at),
- createdBy: node.created_by,
- updatedAt: node.updated_at ? new Date(node.updated_at) : null,
- updatedBy: node.updated_by,
- versionId: node.version_id,
- serverCreatedAt: new Date(node.server_created_at),
- serverUpdatedAt: node.server_updated_at
- ? new Date(node.server_updated_at)
- : null,
- };
-};
diff --git a/server/src/consumers/node-collaborator-cdc.ts b/server/src/consumers/node-collaborator-cdc.ts
index ddd5d65e..075897cd 100644
--- a/server/src/consumers/node-collaborator-cdc.ts
+++ b/server/src/consumers/node-collaborator-cdc.ts
@@ -3,7 +3,11 @@ import { CdcMessage, NodeCollaboratorCdcData } from '@/types/cdc';
import { PostgresOperation } from '@/lib/constants';
import { database } from '@/data/database';
import { generateId, IdType } from '@/lib/id';
-import { ServerNodeCollaborator } from '@/types/nodes';
+import {
+ ServerNodeCollaboratorCreateChangeData,
+ ServerNodeCollaboratorDeleteChangeData,
+ ServerNodeCollaboratorUpdateChangeData,
+} from '@/types/sync';
export const initNodeCollaboratorChangesConsumer = async () => {
const consumer = kafka.consumer({
@@ -60,19 +64,32 @@ const handleNodeCollaboratorCreate = async (
return;
}
- const serverNodeCollaborator: ServerNodeCollaborator =
- mapNodeCollaborator(nodeCollaborator);
+ const data: ServerNodeCollaboratorCreateChangeData = {
+ type: 'node_collaborator_create',
+ nodeId: nodeCollaborator.node_id,
+ collaboratorId: nodeCollaborator.collaborator_id,
+ role: nodeCollaborator.role,
+ workspaceId: nodeCollaborator.workspace_id,
+ createdAt: nodeCollaborator.created_at,
+ createdBy: nodeCollaborator.created_by,
+ versionId: nodeCollaborator.version_id,
+ serverCreatedAt: nodeCollaborator.server_created_at,
+ };
+
await database
.insertInto('changes')
- .values({
- id: generateId(IdType.Change),
- table: 'node_collaborators',
- action: 'insert',
- workspace_id: nodeCollaborator.workspace_id,
- created_at: new Date(),
- after: JSON.stringify(serverNodeCollaborator),
- device_ids: deviceIds,
- })
+ .values(
+ deviceIds.map((deviceId) => {
+ return {
+ id: generateId(IdType.Change),
+ device_id: deviceId,
+ workspace_id: nodeCollaborator.workspace_id,
+ data: JSON.stringify(data),
+ created_at: new Date(),
+ retry_count: 0,
+ };
+ }),
+ )
.execute();
};
@@ -89,19 +106,33 @@ const handleNodeCollaboratorUpdate = async (
return;
}
- const serverNodeCollaborator: ServerNodeCollaborator =
- mapNodeCollaborator(nodeCollaborator);
+ const data: ServerNodeCollaboratorUpdateChangeData = {
+ type: 'node_collaborator_update',
+ nodeId: nodeCollaborator.node_id,
+ collaboratorId: nodeCollaborator.collaborator_id,
+ role: nodeCollaborator.role,
+ workspaceId: nodeCollaborator.workspace_id,
+ updatedAt: nodeCollaborator.updated_at ?? new Date().toISOString(),
+ updatedBy: nodeCollaborator.updated_by ?? nodeCollaborator.created_by,
+ versionId: nodeCollaborator.version_id,
+ serverUpdatedAt:
+ nodeCollaborator.server_updated_at ?? new Date().toISOString(),
+ };
+
await database
.insertInto('changes')
- .values({
- id: generateId(IdType.Change),
- table: 'node_collaborators',
- action: 'update',
- workspace_id: nodeCollaborator.workspace_id,
- created_at: new Date(),
- after: JSON.stringify(serverNodeCollaborator),
- device_ids: deviceIds,
- })
+ .values(
+ deviceIds.map((deviceId) => {
+ return {
+ id: generateId(IdType.Change),
+ device_id: deviceId,
+ workspace_id: nodeCollaborator.workspace_id,
+ data: JSON.stringify(data),
+ created_at: new Date(),
+ retry_count: 0,
+ };
+ }),
+ )
.execute();
};
@@ -118,20 +149,27 @@ const handleNodeCollaboratorDelete = async (
return;
}
- const serverNodeCollaborator: ServerNodeCollaborator =
- mapNodeCollaborator(nodeCollaborator);
+ const data: ServerNodeCollaboratorDeleteChangeData = {
+ type: 'node_collaborator_delete',
+ nodeId: nodeCollaborator.node_id,
+ collaboratorId: nodeCollaborator.collaborator_id,
+ workspaceId: nodeCollaborator.workspace_id,
+ };
+
await database
.insertInto('changes')
- .values({
- id: generateId(IdType.Change),
- table: 'node_collaborators',
- action: 'delete',
- workspace_id: nodeCollaborator.workspace_id,
- created_at: new Date(),
- before: JSON.stringify(serverNodeCollaborator),
- after: null,
- device_ids: deviceIds,
- })
+ .values(
+ deviceIds.map((deviceId) => {
+ return {
+ id: generateId(IdType.Change),
+ device_id: deviceId,
+ workspace_id: nodeCollaborator.workspace_id,
+ data: JSON.stringify(data),
+ created_at: new Date(),
+ retry_count: 0,
+ };
+ }),
+ )
.execute();
};
@@ -152,25 +190,3 @@ const getDeviceIds = async (workspaceId: string) => {
const deviceIds = accountDevices.map((account) => account.id);
return deviceIds;
};
-
-const mapNodeCollaborator = (
- nodeCollaborator: NodeCollaboratorCdcData,
-): ServerNodeCollaborator => {
- return {
- nodeId: nodeCollaborator.node_id,
- collaboratorId: nodeCollaborator.collaborator_id,
- role: nodeCollaborator.role,
- workspaceId: nodeCollaborator.workspace_id,
- createdAt: new Date(nodeCollaborator.created_at),
- createdBy: nodeCollaborator.created_by,
- updatedAt: nodeCollaborator.updated_at
- ? new Date(nodeCollaborator.updated_at)
- : null,
- updatedBy: nodeCollaborator.updated_by,
- versionId: nodeCollaborator.version_id,
- serverCreatedAt: new Date(nodeCollaborator.server_created_at),
- serverUpdatedAt: nodeCollaborator.server_updated_at
- ? new Date(nodeCollaborator.server_updated_at)
- : null,
- };
-};
diff --git a/server/src/consumers/node-reaction-cdc.ts b/server/src/consumers/node-reaction-cdc.ts
index 249cf31b..f408c6f3 100644
--- a/server/src/consumers/node-reaction-cdc.ts
+++ b/server/src/consumers/node-reaction-cdc.ts
@@ -3,7 +3,10 @@ import { CdcMessage, NodeReactionCdcData } from '@/types/cdc';
import { PostgresOperation } from '@/lib/constants';
import { database } from '@/data/database';
import { generateId, IdType } from '@/lib/id';
-import { ServerNodeReaction } from '@/types/nodes';
+import {
+ ServerNodeReactionCreateChangeData,
+ ServerNodeReactionDeleteChangeData,
+} from '@/types/sync';
export const initNodeReactionChangesConsumer = async () => {
const consumer = kafka.consumer({
@@ -56,18 +59,30 @@ const handleNodeReactionCreate = async (
return;
}
- const serverNodeReaction: ServerNodeReaction = mapNodeReaction(reaction);
+ const data: ServerNodeReactionCreateChangeData = {
+ type: 'node_reaction_create',
+ nodeId: reaction.node_id,
+ actorId: reaction.actor_id,
+ reaction: reaction.reaction,
+ workspaceId: reaction.workspace_id,
+ createdAt: reaction.created_at,
+ serverCreatedAt: reaction.server_created_at,
+ };
+
await database
.insertInto('changes')
- .values({
- id: generateId(IdType.Change),
- table: 'node_reactions',
- action: 'insert',
- workspace_id: reaction.workspace_id,
- created_at: new Date(),
- after: JSON.stringify(serverNodeReaction),
- device_ids: deviceIds,
- })
+ .values(
+ deviceIds.map((deviceId) => {
+ return {
+ id: generateId(IdType.Change),
+ device_id: deviceId,
+ workspace_id: reaction.workspace_id,
+ data: JSON.stringify(data),
+ created_at: new Date(),
+ retry_count: 0,
+ };
+ }),
+ )
.execute();
};
@@ -84,19 +99,28 @@ const handleNodeReactionDelete = async (
return;
}
- const serverNodeReaction: ServerNodeReaction = mapNodeReaction(reaction);
+ const data: ServerNodeReactionDeleteChangeData = {
+ type: 'node_reaction_delete',
+ nodeId: reaction.node_id,
+ actorId: reaction.actor_id,
+ reaction: reaction.reaction,
+ workspaceId: reaction.workspace_id,
+ };
+
await database
.insertInto('changes')
- .values({
- id: generateId(IdType.Change),
- table: 'node_reactions',
- action: 'delete',
- workspace_id: reaction.workspace_id,
- created_at: new Date(),
- before: JSON.stringify(serverNodeReaction),
- after: null,
- device_ids: deviceIds,
- })
+ .values(
+ deviceIds.map((deviceId) => {
+ return {
+ id: generateId(IdType.Change),
+ device_id: deviceId,
+ workspace_id: reaction.workspace_id,
+ data: JSON.stringify(data),
+ created_at: new Date(),
+ retry_count: 0,
+ };
+ }),
+ )
.execute();
};
@@ -117,14 +141,3 @@ const getDeviceIds = async (workspaceId: string) => {
const deviceIds = accountDevices.map((account) => account.id);
return deviceIds;
};
-
-const mapNodeReaction = (reaction: NodeReactionCdcData): ServerNodeReaction => {
- return {
- nodeId: reaction.node_id,
- actorId: reaction.actor_id,
- reaction: reaction.reaction,
- workspaceId: reaction.workspace_id,
- createdAt: new Date(reaction.created_at),
- serverCreatedAt: new Date(reaction.server_created_at),
- };
-};
diff --git a/server/src/data/migrations.ts b/server/src/data/migrations.ts
index 3316f8e6..efa07310 100644
--- a/server/src/data/migrations.ts
+++ b/server/src/data/migrations.ts
@@ -192,13 +192,17 @@ const createChangesTable: Migration = {
await db.schema
.createTable('changes')
.addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey())
+ .addColumn('device_id', 'varchar(30)', (col) => col.notNull())
.addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('table', 'varchar(30)', (col) => col.notNull())
- .addColumn('action', 'varchar(30)', (col) => col.notNull())
- .addColumn('after', 'jsonb')
- .addColumn('before', 'jsonb')
+ .addColumn('data', 'jsonb')
.addColumn('created_at', 'timestamptz', (col) => col.notNull())
- .addColumn('device_ids', sql`text[]`, (col) => col.notNull())
+ .addColumn('retry_count', 'integer', (col) => col.notNull().defaultTo(0))
+ .execute();
+
+ await db.schema
+ .createIndex('changes_device_id_index')
+ .on('changes')
+ .column('device_id')
.execute();
},
down: async (db) => {
diff --git a/server/src/data/schema.ts b/server/src/data/schema.ts
index f95bdcff..4e66cc58 100644
--- a/server/src/data/schema.ts
+++ b/server/src/data/schema.ts
@@ -139,13 +139,11 @@ export type UpdateNodeReaction = Updateable<NodeReactionTable>;
interface ChangeTable {
id: ColumnType<string, string, never>;
+ device_id: ColumnType<string, string, never>;
workspace_id: ColumnType<string, string, never>;
- table: ColumnType<string, string, never>;
- action: ColumnType<string, string, never>;
- after: ColumnType<string | null, string | null, never>;
- before: ColumnType<string | null, string | null, never>;
+ data: JSONColumnType<any, string | null, string | null>;
created_at: ColumnType<Date, Date, never>;
- device_ids: ColumnType<string[], string[], string[]>;
+ retry_count: ColumnType<number, number, number>;
}
export type SelectChange = Selectable<ChangeTable>;
diff --git a/server/src/messages/index.ts b/server/src/messages/index.ts
index 58965cfe..628aec93 100644
--- a/server/src/messages/index.ts
+++ b/server/src/messages/index.ts
@@ -1,4 +1,12 @@
-export interface MessageMap {}
+import { ServerChangeMessageInput } from '@/messages/server-change';
+import { ServerChangeResultMessageInput } from '@/messages/server-change-result';
+import { ServerChangeBatchMessageInput } from '@/messages/server-change-batch';
+
+export interface MessageMap {
+ server_change: ServerChangeMessageInput;
+ server_change_result: ServerChangeResultMessageInput;
+ server_change_batch: ServerChangeBatchMessageInput;
+}
export type MessageInput = MessageMap[keyof MessageMap];
diff --git a/server/src/messages/server-change-ack.ts b/server/src/messages/server-change-ack.ts
deleted file mode 100644
index c32a5e27..00000000
--- a/server/src/messages/server-change-ack.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import { database } from '@/data/database';
-import { MessageContext } from '@/messages';
-import { sql } from 'kysely';
-
-export type ServerChangeAckMessageInput = {
- type: 'server_change_ack';
- changeId: string;
-};
-
-declare module '@/messages' {
- interface MessageMap {
- server_change_ack: ServerChangeAckMessageInput;
- }
-}
-
-export const handleChangeAck = async (
- context: MessageContext,
- input: ServerChangeAckMessageInput,
-) => {
- await database
- .updateTable('changes')
- .set({
- device_ids: sql`array_remove(device_ids, ${context.deviceId})`,
- })
- .where('id', '=', input.changeId)
- .execute();
-};
diff --git a/server/src/messages/server-change-batch.ts b/server/src/messages/server-change-batch.ts
new file mode 100644
index 00000000..f8c93d9d
--- /dev/null
+++ b/server/src/messages/server-change-batch.ts
@@ -0,0 +1,6 @@
+import { ServerChange } from '@/types/sync';
+
+export type ServerChangeBatchMessageInput = {
+ type: 'server_change_batch';
+ changes: ServerChange[];
+};
diff --git a/server/src/messages/server-change-result.ts b/server/src/messages/server-change-result.ts
new file mode 100644
index 00000000..6c542433
--- /dev/null
+++ b/server/src/messages/server-change-result.ts
@@ -0,0 +1,5 @@
+export type ServerChangeResultMessageInput = {
+ type: 'server_change_result';
+ changeId: string;
+ success: boolean;
+};
diff --git a/server/src/messages/server-change.ts b/server/src/messages/server-change.ts
index 7cb8e922..0056e82b 100644
--- a/server/src/messages/server-change.ts
+++ b/server/src/messages/server-change.ts
@@ -4,9 +4,3 @@ export type ServerChangeMessageInput = {
type: 'server_change';
change: ServerChange;
};
-
-declare module '@/messages' {
- interface MessageMap {
- server_change: ServerChangeMessageInput;
- }
-}
diff --git a/server/src/sockets/socket-connection.ts b/server/src/sockets/socket-connection.ts
new file mode 100644
index 00000000..336e7c6b
--- /dev/null
+++ b/server/src/sockets/socket-connection.ts
@@ -0,0 +1,91 @@
+import { database } from '@/data/database';
+import { MessageInput } from '@/messages';
+import { NeuronRequestAccount } from '@/types/api';
+import { WebSocket } from 'ws';
+
+export class SocketConnection {
+ private readonly socket: WebSocket;
+ private readonly account: NeuronRequestAccount;
+ private readonly pendingChanges: Set<string> = new Set();
+
+ constructor(socket: WebSocket, account: NeuronRequestAccount) {
+ this.socket = socket;
+ this.account = account;
+
+ socket.on('message', (message) => {
+ this.handleMessage(message.toString());
+ });
+ }
+
+ public send(message: MessageInput) {
+ if (message.type === 'server_change') {
+ const changeId = message.change.id;
+ if (this.pendingChanges.size > 0) {
+ return;
+ }
+
+ this.pendingChanges.add(changeId);
+ } else if (message.type === 'server_change_batch') {
+ if (this.pendingChanges.size > 0) {
+ return;
+ }
+
+ message.changes.forEach((change) => {
+ this.pendingChanges.add(change.id);
+ });
+ }
+ this.socket.send(JSON.stringify(message));
+ }
+
+ public init() {
+ this.sendPendingChanges();
+ }
+
+ private async handleMessage(message: string): Promise<void> {
+ const messageInput: MessageInput = JSON.parse(message);
+ if (messageInput.type === 'server_change_result') {
+ if (messageInput.success) {
+ await database
+ .deleteFrom('changes')
+ .where('id', '=', messageInput.changeId)
+ .execute();
+ } else {
+ await database
+ .updateTable('changes')
+ .set((eb) => ({ retry_count: eb('retry_count', '+', 1) }))
+ .where('id', '=', messageInput.changeId)
+ .execute();
+ }
+
+ this.pendingChanges.delete(messageInput.changeId);
+ if (this.pendingChanges.size === 0) {
+ this.sendPendingChanges();
+ }
+ }
+ }
+
+ private async sendPendingChanges() {
+ const changes = await database
+ .selectFrom('changes')
+ .selectAll()
+ .where('device_id', '=', this.account.deviceId)
+ .orderBy('id', 'asc')
+ .limit(100)
+ .execute();
+
+ if (changes.length === 0) {
+ return;
+ }
+
+ this.send({
+ type: 'server_change_batch',
+ changes: changes.map((change) => ({
+ id: change.id,
+ workspaceId: change.workspace_id,
+ deviceId: change.device_id,
+ createdAt: change.created_at.toISOString(),
+ data: change.data,
+ })),
+ });
+ }
+}
diff --git a/server/src/sockets/socket-manager.ts b/server/src/sockets/socket-manager.ts
new file mode 100644
index 00000000..b6f70346
--- /dev/null
+++ b/server/src/sockets/socket-manager.ts
@@ -0,0 +1,62 @@
+import http from 'http';
+import { WebSocketServer } from 'ws';
+import { verifyToken } from '@/lib/tokens';
+import { SocketConnection } from '@/sockets/socket-connection';
+import { MessageInput } from '@/messages';
+
+class SocketManager {
+ private readonly sockets: Map<string, SocketConnection> = new Map();
+
+ public init(server: http.Server) {
+ const wss = new WebSocketServer({
+ server,
+ path: '/v1/synapse',
+ verifyClient: async (info, callback) => {
+ const req = info.req;
+ const token = req.headers['authorization'];
+
+ if (!token) {
+ return callback(false, 401, 'Unauthorized');
+ }
+
+ callback(true);
+ },
+ });
+
+ wss.on('connection', async (socket, req) => {
+ const token = req.headers['authorization'];
+ if (!token) {
+ socket.close();
+ return;
+ }
+
+ const result = await verifyToken(token);
+ if (!result.authenticated) {
+ socket.close();
+ return;
+ }
+
+ const account = result.account;
+
+ socket.on('close', () => {
+ this.sockets.delete(account.deviceId);
+ });
+
+ const connection = new SocketConnection(socket, account);
+ connection.init();
+
+ this.sockets.set(account.deviceId, connection);
+ });
+ }
+
+ public send(deviceId: string, message: MessageInput) {
+ const connection = this.sockets.get(deviceId);
+ if (!connection) {
+ return;
+ }
+
+ connection.send(message);
+ }
+}
+
+export const socketManager = new SocketManager();
diff --git a/server/src/synapse.ts b/server/src/synapse.ts
deleted file mode 100644
index c473aa43..00000000
--- a/server/src/synapse.ts
+++ /dev/null
@@ -1,128 +0,0 @@
-import http from 'http';
-import { WebSocket, WebSocketServer } from 'ws';
-import { database } from '@/data/database';
-import { sql } from 'kysely';
-import { SelectChange } from '@/data/schema';
-import { ServerChange } from '@/types/sync';
-import { MessageInput } from '@/messages';
-import { handleChangeAck } from '@/messages/server-change-ack';
-import { verifyToken } from '@/lib/tokens';
-import { NeuronRequestAccount } from '@/types/api';
-
-interface SynapseConnection {
- socket: WebSocket;
- account: NeuronRequestAccount;
-}
-
-class SynapseManager {
- private readonly sockets: Map<string, SynapseConnection> = new Map();
-
- public init(server: http.Server) {
- const wss = new WebSocketServer({
- server,
- path: '/v1/synapse',
- verifyClient: async (info, callback) => {
- const req = info.req;
- const token = req.headers['authorization'];
-
- if (!token) {
- return callback(false, 401, 'Unauthorized');
- }
-
- callback(true);
- },
- });
-
- wss.on('connection', async (socket, req) => {
- const token = req.headers['authorization'];
- if (!token) {
- socket.close();
- return;
- }
-
- const result = await verifyToken(token);
- if (!result.authenticated) {
- socket.close();
- return;
- }
-
- const account = result.account;
- socket.on('message', (message) => {
- this.handleMessage(account, message.toString());
- });
-
- socket.on('close', () => {
- this.sockets.delete(account.deviceId);
- });
-
- this.sockets.set(account.deviceId, {
- socket,
- account,
- });
-
- await this.sendPendingChanges(account.deviceId);
- });
- }
-
- public send(deviceId: string, message: MessageInput) {
- const connection = this.sockets.get(deviceId);
- if (!connection || !connection.socket) {
- return;
- }
-
- connection.socket.send(JSON.stringify(message));
- }
-
- private async handleMessage(
- account: NeuronRequestAccount,
- message: string,
- ): Promise<void> {
- const messageInput: MessageInput = JSON.parse(message);
- if (messageInput.type === 'server_change_ack') {
- await handleChangeAck(
- {
- accountId: account.id,
- deviceId: account.deviceId,
- },
- messageInput,
- );
- }
- }
-
- private async sendPendingChanges(deviceId: string) {
- let lastId = '0';
- let hasMore = true;
-
- while (hasMore) {
- const pendingChanges = await sql<SelectChange>`
- SELECT *
- FROM changes
- WHERE ${deviceId} = ANY(device_ids)
- AND id > ${lastId}
- ORDER BY id ASC
- LIMIT 50
- `.execute(database);
-
- for (const change of pendingChanges.rows) {
- const serverChange: ServerChange = {
- id: change.id,
- action: change.action as 'insert' | 'update' | 'delete',
- table: change.table,
- workspaceId: change.workspace_id,
- before: change.before,
- after: change.after,
- };
-
- this.send(deviceId, {
- type: 'server_change',
- change: serverChange,
- });
- lastId = change.id;
- }
-
- hasMore = pendingChanges.rows.length === 50;
- }
- }
-}
-
-export const synapse = new SynapseManager();
diff --git a/server/src/types/cdc.ts b/server/src/types/cdc.ts
index 661ab46a..ec4de10c 100644
--- a/server/src/types/cdc.ts
+++ b/server/src/types/cdc.ts
@@ -27,13 +27,10 @@ type CdcSource = {
export type ChangeCdcData = {
id: string;
+ device_id: string;
workspace_id: string;
- table: string;
- action: string;
- after: string | null;
- before: string | null;
+ data: string;
created_at: string;
- device_ids: string[];
};
export type NodeCdcData = {
diff --git a/server/src/types/sync.ts b/server/src/types/sync.ts
index 13eb99cf..70d96a14 100644
--- a/server/src/types/sync.ts
+++ b/server/src/types/sync.ts
@@ -59,9 +59,95 @@ export type LocalNodeReactionChangeData = {
export type ServerChange = {
id: string;
- table: string;
- action: 'insert' | 'update' | 'delete';
workspaceId: string;
- before: any | null;
- after: any | null;
+ deviceId: string;
+ data: ServerChangeData;
+ createdAt: string;
+};
+
+export type ServerChangeData =
+ | ServerNodeCreateChangeData
+ | ServerNodeUpdateChangeData
+ | ServerNodeDeleteChangeData
+ | ServerNodeCollaboratorCreateChangeData
+ | ServerNodeCollaboratorUpdateChangeData
+ | ServerNodeCollaboratorDeleteChangeData
+ | ServerNodeReactionCreateChangeData
+ | ServerNodeReactionDeleteChangeData;
+
+export type ServerNodeCreateChangeData = {
+ type: 'node_create';
+ id: string;
+ workspaceId: string;
+ state: string;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+ serverCreatedAt: string;
+};
+
+export type ServerNodeUpdateChangeData = {
+ type: 'node_update';
+ id: string;
+ workspaceId: string;
+ update: string;
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+ serverUpdatedAt: string;
+};
+
+export type ServerNodeDeleteChangeData = {
+ type: 'node_delete';
+ id: string;
+ workspaceId: string;
+};
+
+export type ServerNodeCollaboratorCreateChangeData = {
+ type: 'node_collaborator_create';
+ nodeId: string;
+ collaboratorId: string;
+ role: string;
+ workspaceId: string;
+ createdAt: string;
+ createdBy: string;
+ versionId: string;
+ serverCreatedAt: string;
+};
+
+export type ServerNodeCollaboratorUpdateChangeData = {
+ type: 'node_collaborator_update';
+ nodeId: string;
+ collaboratorId: string;
+ workspaceId: string;
+ role: string;
+ updatedAt: string;
+ updatedBy: string;
+ versionId: string;
+ serverUpdatedAt: string;
+};
+
+export type ServerNodeCollaboratorDeleteChangeData = {
+ type: 'node_collaborator_delete';
+ nodeId: string;
+ collaboratorId: string;
+ workspaceId: string;
+};
+
+export type ServerNodeReactionCreateChangeData = {
+ type: 'node_reaction_create';
+ nodeId: string;
+ actorId: string;
+ reaction: string;
+ workspaceId: string;
+ createdAt: string;
+ serverCreatedAt: string;
+};
+
+export type ServerNodeReactionDeleteChangeData = {
+ type: 'node_reaction_delete';
+ nodeId: string;
+ actorId: string;
+ reaction: string;
+ workspaceId: string;
};