Open-source and local-first Slack and Notion alternative that puts you in control of your data
TypeScript chatchat-applicationcrdteditorelectronknowledge-baselocal-firstnotionnotion-alternativerealtime-collaborationself-hostedslacksqliteteam-collaborationwikiyjs
Implement new approach for synchronization
Hakan Shehuf8c9b551fa0e
diff --git a/desktop/src/main/data/workspace/migrations.ts b/desktop/src/main/data/workspace/migrations.tsindex 376d66cc..c9569622 100644--- a/desktop/src/main/data/workspace/migrations.ts+++ b/desktop/src/main/data/workspace/migrations.ts@@ -34,20 +34,13 @@ const createNodesTable: Migration = {.addColumn('server_updated_at', 'text').addColumn('server_version_id', 'text').execute();- },- down: async (db) => {- await db.schema.dropTable('nodes').execute();- },-};-const createNodesParentIdAndTypeIndex: Migration = {- up: async (db) => {await sql`CREATE INDEX IF NOT EXISTS "nodes_parent_id_type_index" ON "nodes" ("parent_id", "type");`.execute(db);},down: async (db) => {- await sql`DROP INDEX IF EXISTS "nodes_parent_id_type_index"`.execute(db);+ await db.schema.dropTable('nodes').execute();},};@@ -64,61 +57,6 @@ const createNodeNamesTable: Migration = {},};-const createNodeCollaboratorsTable: Migration = {- up: async (db) => {- await db.schema- .createTable('node_collaborators')- .addColumn('node_id', 'text', (col) =>- col.notNull().references('nodes.id').onDelete('cascade'),- )- .addColumn('collaborator_id', 'text', (col) =>- col.notNull().references('nodes.id').onDelete('cascade'),- )- .addColumn('role', 'text', (col) => col.notNull())- .addColumn('created_at', 'text', (col) => col.notNull())- .addColumn('updated_at', 'text')- .addColumn('created_by', 'text', (col) => col.notNull())- .addColumn('updated_by', 'text')- .addColumn('version_id', 'text', (col) => col.notNull())- .addColumn('server_created_at', 'text')- .addColumn('server_updated_at', 'text')- .addColumn('server_version_id', 'text')- .addPrimaryKeyConstraint('node_reactions_pkey', [- 'node_id',- 'collaborator_id',- ])- .execute();- },- down: async (db) => {- await db.schema.dropTable('node_collaborators').execute();- },-};--const createNodeReactionsTable: Migration = {- up: async (db) => {- await db.schema- .createTable('node_reactions')- .addColumn('node_id', 'text', (col) =>- col.notNull().references('nodes.id').onDelete('cascade'),- )- .addColumn('actor_id', 'text', (col) =>- col.notNull().references('nodes.id').onDelete('cascade'),- )- .addColumn('reaction', 'text', (col) => col.notNull())- .addColumn('created_at', 'text', (col) => col.notNull())- .addColumn('server_created_at', 'text')- .addPrimaryKeyConstraint('node_reactions_pkey', [- 'node_id',- 'actor_id',- 'reaction',- ])- .execute();- },- down: async (db) => {- await db.schema.dropTable('node_reactions').execute();- },-};-const createChangesTable: Migration = {up: async (db) => {await db.schema@@ -329,202 +267,14 @@ const createNodeDeleteNameTrigger: Migration = {},};-const createNodeCollaboratorInsertChangeTrigger: Migration = {- up: async (db) => {- await sql`- CREATE TRIGGER node_collaborator_insert_change- AFTER INSERT ON node_collaborators- FOR EACH ROW- WHEN NEW.server_version_id IS NULL- BEGIN- INSERT INTO changes ('action', 'table', 'after', 'created_at')- VALUES (- 'insert',- 'node_collaborators',- json_object(- 'node_id', NEW.'node_id',- 'collaborator_id', NEW.'collaborator_id',- 'role', NEW.'role',- 'created_at', NEW.'created_at',- 'updated_at', NEW.'updated_at',- 'created_by', NEW.'created_by',- 'updated_by', NEW.'updated_by',- 'version_id', NEW.'version_id',- 'server_created_at', NEW.'server_created_at',- 'server_updated_at', NEW.'server_updated_at',- 'server_version_id', NEW.'server_version_id'- ),- datetime('now')- );- END;- `.execute(db);- },- down: async (db) => {- await sql`DROP TRIGGER node_collaborator_insert_change`.execute(db);- },-};--const createNodeCollaboratorUpdateChangeTrigger: Migration = {- up: async (db) => {- await sql`- CREATE TRIGGER node_collaborator_update_change- AFTER UPDATE ON node_collaborators- FOR EACH ROW- WHEN NEW.server_version_id IS NULL OR NEW.version_id != NEW.server_version_id- BEGIN- INSERT INTO changes ('action', 'table', 'before', 'after', 'created_at')- VALUES (- 'update',- 'node_collaborators',- json_object(- 'node_id', OLD.'node_id',- 'collaborator_id', OLD.'collaborator_id',- 'role', OLD.'role',- 'created_at', OLD.'created_at',- 'updated_at', OLD.'updated_at',- 'created_by', OLD.'created_by',- 'updated_by', OLD.'updated_by',- 'version_id', OLD.'version_id',- 'server_created_at', OLD.'server_created_at',- 'server_updated_at', OLD.'server_updated_at',- 'server_version_id', OLD.'server_version_id'- ),- json_object(- 'node_id', NEW.'node_id',- 'collaborator_id', NEW.'collaborator_id',- 'role', NEW.'role',- 'created_at', NEW.'created_at',- 'updated_at', NEW.'updated_at',- 'created_by', NEW.'created_by',- 'updated_by', NEW.'updated_by',- 'version_id', NEW.'version_id',- 'server_created_at', NEW.'server_created_at',- 'server_updated_at', NEW.'server_updated_at',- 'server_version_id', NEW.'server_version_id'- ),- datetime('now')- );- END;- `.execute(db);- },- down: async (db) => {- await sql`DROP TRIGGER node_collaborator_update_change`.execute(db);- },-};--const createNodeCollaboratorDeleteChangeTrigger: Migration = {- up: async (db) => {- await sql`- CREATE TRIGGER node_collaborator_delete_change- AFTER DELETE ON node_collaborators- FOR EACH ROW- BEGIN- INSERT INTO changes ('action', 'table', 'before', 'created_at')- VALUES (- 'delete',- 'node_collaborators',- json_object(- 'node_id', OLD.'node_id',- 'collaborator_id', OLD.'collaborator_id',- 'role', OLD.'role',- 'created_at', OLD.'created_at',- 'updated_at', OLD.'updated_at',- 'created_by', OLD.'created_by',- 'updated_by', OLD.'updated_by',- 'version_id', OLD.'version_id',- 'server_created_at', OLD.'server_created_at',- 'server_updated_at', OLD.'server_updated_at',- 'server_version_id', OLD.'server_version_id'- ),- datetime('now')- );- END;- `.execute(db);- },- down: async (db) => {- await sql`DROP TRIGGER node_collaborator_delete_change`.execute(db);- },-};--const createNodeReactionInsertChangeTrigger: Migration = {- up: async (db) => {- await sql`- CREATE TRIGGER node_reaction_insert_change- AFTER INSERT ON node_reactions- FOR EACH ROW- WHEN NEW.server_created_at IS NULL- BEGIN- INSERT INTO changes ('action', 'table', 'after', 'created_at')- VALUES (- 'insert',- 'node_reactions',- json_object(- 'node_id', NEW.'node_id',- 'actor_id', NEW.'actor_id',- 'reaction', NEW.'reaction',- 'created_at', NEW.'created_at',- 'server_created_at', NEW.'server_created_at'- ),- datetime('now')- );- END;- `.execute(db);- },- down: async (db) => {- await sql`DROP TRIGGER after_insert_node_reactions`.execute(db);- },-};--const createNodeReactionDeleteChangeTrigger: Migration = {- up: async (db) => {- await sql`- CREATE TRIGGER node_reaction_delete_change- AFTER DELETE ON node_reactions- FOR EACH ROW- BEGIN- INSERT INTO changes ('action', 'table', 'before', 'created_at')- VALUES (- 'delete',- 'node_reactions',- json_object(- 'node_id', OLD.'node_id',- 'actor_id', OLD.'actor_id',- 'reaction', OLD.'reaction',- 'created_at', OLD.'created_at',- 'server_created_at', OLD.'server_created_at'- ),- datetime('now')- );- END;- `.execute(db);- },- down: async (db) => {- await sql`DROP TRIGGER node_reaction_delete_change`.execute(db);- },-};-export const workspaceDatabaseMigrations: Record<string, Migration> = {'00001_create_nodes_table': createNodesTable,- '00002_create_nodes_parent_id_and_type_index':- createNodesParentIdAndTypeIndex,- '00003_create_node_reactions_table': createNodeReactionsTable,- '00004_create_node_names_table': createNodeNamesTable,- '00005_create_node_collaborators_table': createNodeCollaboratorsTable,- '00006_create_changes_table': createChangesTable,- '00007_create_node_insert_change_trigger': createNodeInsertChangeTrigger,- '00008_create_node_update_change_trigger': createNodeUpdateChangeTrigger,- '00009_create_node_delete_change_trigger': createNodeDeleteChangeTrigger,- '00010_create_node_insert_name_trigger': createNodeInsertNameTrigger,- '00011_create_node_update_name_trigger': createNodeUpdateNameTrigger,- '00012_create_node_delete_name_trigger': createNodeDeleteNameTrigger,- '00013_create_node_collaborator_insert_change_trigger':- createNodeCollaboratorInsertChangeTrigger,- '00014_create_node_collaborator_update_change_trigger':- createNodeCollaboratorUpdateChangeTrigger,- '00015_create_node_collaborator_delete_change_trigger':- createNodeCollaboratorDeleteChangeTrigger,- '00016_create_node_reaction_insert_change_trigger':- createNodeReactionInsertChangeTrigger,- '00017_create_node_reaction_delete_change_trigger':- createNodeReactionDeleteChangeTrigger,+ '00002_create_node_names_table': createNodeNamesTable,+ '00003_create_changes_table': createChangesTable,+ '00004_create_node_insert_change_trigger': createNodeInsertChangeTrigger,+ '00005_create_node_update_change_trigger': createNodeUpdateChangeTrigger,+ '00006_create_node_delete_change_trigger': createNodeDeleteChangeTrigger,+ '00007_create_node_insert_name_trigger': createNodeInsertNameTrigger,+ '00008_create_node_update_name_trigger': createNodeUpdateNameTrigger,+ '00009_create_node_delete_name_trigger': createNodeDeleteNameTrigger,};diff --git a/desktop/src/main/data/workspace/schema.ts b/desktop/src/main/data/workspace/schema.tsindex 847052bf..ad91d8ee 100644--- a/desktop/src/main/data/workspace/schema.ts+++ b/desktop/src/main/data/workspace/schema.ts@@ -21,36 +21,6 @@ export type SelectNode = Selectable<NodeTable>;export type CreateNode = Insertable<NodeTable>;export type UpdateNode = Updateable<NodeTable>;-interface NodeCollaboratorTable {- node_id: ColumnType<string, string, never>;- collaborator_id: ColumnType<string, string, never>;- role: ColumnType<string, string, string>;- created_at: ColumnType<string, string, never>;- updated_at: ColumnType<string | null, string | null, string | null>;- created_by: ColumnType<string, string, never>;- updated_by: ColumnType<string | null, string | null, string | null>;- version_id: ColumnType<string, string, string>;- server_created_at: ColumnType<string | null, string | null, string | null>;- server_updated_at: ColumnType<string | null, string | null, string | null>;- server_version_id: ColumnType<string | null, string | null, string | null>;-}--export type SelectNodeCollaborator = Selectable<NodeCollaboratorTable>;-export type CreateNodeCollaborator = Insertable<NodeCollaboratorTable>;-export type UpdateNodeCollaborator = Updateable<NodeCollaboratorTable>;--interface NodeReactionTable {- node_id: ColumnType<string, string, never>;- actor_id: ColumnType<string, string, never>;- reaction: ColumnType<string, string, never>;- created_at: ColumnType<string, string, never>;- server_created_at: ColumnType<string | null, string | null, string | null>;-}--export type SelectNodeReaction = Selectable<NodeReactionTable>;-export type CreateNodeReaction = Insertable<NodeReactionTable>;-export type UpdateNodeReaction = Updateable<NodeReactionTable>;-interface ChangeTable {id: ColumnType<number, never, never>;table: ColumnType<string, string, never>;@@ -68,6 +38,4 @@ export type UpdateChange = Updateable<ChangeTable>;export interface WorkspaceDatabaseSchema {nodes: NodeTable;changes: ChangeTable;- node_collaborators: NodeCollaboratorTable;- node_reactions: NodeReactionTable;}diff --git a/desktop/src/main/handlers/messages/index.ts b/desktop/src/main/handlers/messages/index.tsindex 0df054f7..ff2c2063 100644--- a/desktop/src/main/handlers/messages/index.ts+++ b/desktop/src/main/handlers/messages/index.ts@@ -1,14 +1,12 @@import { MessageHandler, MessageMap } from '@/operations/messages';-import { ServerChangeMessageHandler } from '@/main/handlers/messages/server-change';-import { ServerChangeResultMessageHandler } from '@/main/handlers/messages/server-change-result';-import { ServerChangeBatchMessageHandler } from '@/main/handlers/messages/server-change-batch';+import { LocalNodeSyncMessageHandler } from '@/main/handlers/messages/local-node-sync';+import { ServerNodesSyncMessageHandler } from '@/main/handlers/messages/server-nodes-sync';type MessageHandlerMap = {[K in keyof MessageMap]: MessageHandler<MessageMap[K]>;};export const messageHandlerMap: MessageHandlerMap = {- server_change: new ServerChangeMessageHandler(),- server_change_result: new ServerChangeResultMessageHandler(),- server_change_batch: new ServerChangeBatchMessageHandler(),+ local_node_sync: new LocalNodeSyncMessageHandler(),+ server_nodes_sync: new ServerNodesSyncMessageHandler(),};diff --git a/desktop/src/main/handlers/messages/server-change-result.ts b/desktop/src/main/handlers/messages/local-node-sync.tssimilarity index 53%rename from desktop/src/main/handlers/messages/server-change-result.tsrename to desktop/src/main/handlers/messages/local-node-sync.tsindex 4ddeef9c..6cff77ee 100644--- a/desktop/src/main/handlers/messages/server-change-result.ts+++ b/desktop/src/main/handlers/messages/local-node-sync.ts@@ -1,13 +1,13 @@import { MessageContext, MessageHandler } from '@/operations/messages';-import { ServerChangeResultMessageInput } from '@/operations/messages/server-change-result';+import { LocalNodeSyncMessageInput } from '@/operations/messages/local-node-sync';import { socketManager } from '@/main/sockets/socket-manager';-export class ServerChangeResultMessageHandler- implements MessageHandler<ServerChangeResultMessageInput>+export class LocalNodeSyncMessageHandler+ implements MessageHandler<LocalNodeSyncMessageInput>{public async handleMessage(context: MessageContext,- input: ServerChangeResultMessageInput,+ input: LocalNodeSyncMessageInput,): Promise<void> {socketManager.sendMessage(context.accountId, input);}diff --git a/desktop/src/main/handlers/messages/server-change-batch.ts b/desktop/src/main/handlers/messages/server-change-batch.tsdeleted file mode 100644index ad38af38..00000000--- a/desktop/src/main/handlers/messages/server-change-batch.ts+++ /dev/null@@ -1,16 +0,0 @@-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.ts b/desktop/src/main/handlers/messages/server-change.tsdeleted file mode 100644index b20e5975..00000000--- a/desktop/src/main/handlers/messages/server-change.ts+++ /dev/null@@ -1,14 +0,0 @@-import { MessageContext, MessageHandler } from '@/operations/messages';-import { ServerChangeMessageInput } from '@/operations/messages/server-change';-import { synchronizer } from '@/main/synchronizer';--export class ServerChangeMessageHandler- implements MessageHandler<ServerChangeMessageInput>-{- public async handleMessage(- context: MessageContext,- input: ServerChangeMessageInput,- ): Promise<void> {- await synchronizer.handleServerChange(context.accountId, input.change);- }-}diff --git a/desktop/src/main/handlers/messages/server-nodes-sync.ts b/desktop/src/main/handlers/messages/server-nodes-sync.tsnew file mode 100644index 00000000..9c4d607f--- /dev/null+++ b/desktop/src/main/handlers/messages/server-nodes-sync.ts@@ -0,0 +1,18 @@+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/chat-create.ts b/desktop/src/main/handlers/mutations/chat-create.tsindex e570527d..dfd0ac0f 100644--- a/desktop/src/main/handlers/mutations/chat-create.ts+++ b/desktop/src/main/handlers/mutations/chat-create.ts@@ -1,9 +1,15 @@import { databaseManager } from '@/main/data/database-manager';import { NodeTypes } from '@/lib/constants';import { generateId, IdType } from '@/lib/id';-import { buildCreateNode } from '@/lib/nodes';import { MutationHandler, MutationResult } from '@/operations/mutations';import { ChatCreateMutationInput } from '@/operations/mutations/chat-create';+import { sql } from 'kysely';+import * as Y from 'yjs';+import { fromUint8Array } from 'js-base64';++interface ChatRow {+ id: string;+}export class ChatCreateMutationHandlerimplements MutationHandler<ChatCreateMutationInput>@@ -15,24 +21,17 @@ export class ChatCreateMutationHandlerinput.userId,);- const existingChats = await workspaceDatabase- .selectFrom('nodes')- .selectAll()- .where('type', '=', NodeTypes.Chat)- .where(- 'id',- 'in',- workspaceDatabase- .selectFrom('node_collaborators')- .select('node_id')- .where('collaborator_id', 'in', [input.userId, input.otherUserId])- .groupBy('node_id')- .having(workspaceDatabase.fn.count('collaborator_id'), '=', 2),- )- .execute();+ const query = sql<ChatRow>`+ SELECT id+ FROM nodes+ WHERE type = ${NodeTypes.Chat}+ AND json_extract(attributes, '$.collaborators.${input.userId}') = 'owner'+ AND json_extract(attributes, '$.collaborators.${input.otherUserId}') = 'owner'+ `.compile(workspaceDatabase);- if (existingChats.length > 0) {- const chat = existingChats[0];+ const existingChats = await workspaceDatabase.executeQuery(query);+ if (existingChats.rows?.length > 0) {+ const chat = existingChats.rows[0];return {output: {id: chat.id,@@ -41,45 +40,36 @@ export class ChatCreateMutationHandler}const id = generateId(IdType.Chat);- await workspaceDatabase.transaction().execute(async (trx) => {- await trx- .insertInto('nodes')- .values(- buildCreateNode(- {- id: id,- attributes: {- type: NodeTypes.Chat,- },- },- input.userId,- ),- )- .execute();+ const doc = new Y.Doc({+ guid: id,+ });- await trx- .insertInto('node_collaborators')- .values([- {- node_id: id,- collaborator_id: input.userId,- role: 'owner',- created_at: new Date().toISOString(),- created_by: input.userId,- version_id: generateId(IdType.Version),- },- {- node_id: id,- collaborator_id: input.otherUserId,- role: 'owner',- created_at: new Date().toISOString(),- created_by: input.userId,- version_id: generateId(IdType.Version),- },- ])- .execute();+ const attributesMap = doc.getMap('attributes');++ doc.transact(() => {+ attributesMap.set('type', NodeTypes.Chat);+ attributesMap.set('collaborators', new Y.Map());++ const collaboratorsMap = attributesMap.get('collaborators') as Y.Map<any>;+ collaboratorsMap.set(input.userId, 'owner');+ collaboratorsMap.set(input.otherUserId, 'owner');});+ const attributes = JSON.stringify(attributesMap.toJSON());+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));++ await workspaceDatabase+ .insertInto('nodes')+ .values({+ id: id,+ attributes: attributes,+ state: encodedState,+ created_at: new Date().toISOString(),+ created_by: input.userId,+ version_id: generateId(IdType.Version),+ })+ .execute();+return {output: {id: id,@@ -90,11 +80,6 @@ export class ChatCreateMutationHandlertable: 'nodes',userId: input.userId,},- {- type: 'workspace',- table: 'node_collaborators',- userId: input.userId,- },],};}diff --git a/desktop/src/main/handlers/mutations/index.ts b/desktop/src/main/handlers/mutations/index.tsindex 8f7621b0..e9ed8258 100644--- a/desktop/src/main/handlers/mutations/index.ts+++ b/desktop/src/main/handlers/mutations/index.ts@@ -31,13 +31,8 @@ import { AvatarUploadMutationHandler } from '@/main/handlers/mutations/avatar-upimport { 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';import { LogoutMutationHandler } from '@/main/handlers/mutations/logout';-import { NodeBatchSyncMutationHandler } from '@/main/handlers/mutations/node-batch-sync';+import { NodeSyncMutationHandler } from '@/main/handlers/mutations/node-sync';type MutationHandlerMap = {[K in keyof MutationMap]: MutationHandler<MutationMap[K]['input']>;@@ -77,14 +72,6 @@ export const mutationHandlerMap: MutationHandlerMap = {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(),logout: new LogoutMutationHandler(),- node_batch_sync: new NodeBatchSyncMutationHandler(),+ node_sync: new NodeSyncMutationHandler(),};diff --git a/desktop/src/main/handlers/mutations/node-collaborator-create.ts b/desktop/src/main/handlers/mutations/node-collaborator-create.tsindex 77f2e489..0b4abb0f 100644--- a/desktop/src/main/handlers/mutations/node-collaborator-create.ts+++ b/desktop/src/main/handlers/mutations/node-collaborator-create.ts@@ -2,6 +2,8 @@ import { databaseManager } from '@/main/data/database-manager';import { generateId, IdType } from '@/lib/id';import { MutationHandler, MutationResult } from '@/operations/mutations';import { NodeCollaboratorCreateMutationInput } from '@/operations/mutations/node-collaborator-create';+import * as Y from 'yjs';+import { fromUint8Array } from 'js-base64';export class NodeCollaboratorCreateMutationHandlerimplements MutationHandler<NodeCollaboratorCreateMutationInput>@@ -13,21 +15,44 @@ export class NodeCollaboratorCreateMutationHandlerinput.userId,);+ const node = await workspaceDatabase+ .selectFrom('nodes')+ .where('id', '=', input.nodeId)+ .selectAll()+ .executeTakeFirst();++ if (!node) {+ throw new Error('Node not found');+ }++ const doc = new Y.Doc({+ guid: node.id,+ });++ const attributesMap = doc.getMap('attributes');++ doc.transact(() => {+ if (!attributesMap.has('collaborators')) {+ attributesMap.set('collaborators', new Y.Map());+ }++ const collaboratorsMap = attributesMap.get('collaborators') as Y.Map<any>;+ collaboratorsMap.set(input.userId, input.role);+ });++ const attributes = JSON.stringify(attributesMap.toJSON());+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));+await workspaceDatabase- .insertInto('node_collaborators')- .values([- input.collaboratorIds.map((collaboratorId) => {- return {- node_id: input.nodeId,- collaborator_id: collaboratorId,- role: input.role,- created_at: new Date().toISOString(),- created_by: input.userId,- version_id: generateId(IdType.Version),- };- }),- ])- .onConflict((cb) => cb.doNothing())+ .updateTable('nodes')+ .set({+ attributes: attributes,+ state: encodedState,+ version_id: generateId(IdType.Version),+ updated_at: new Date().toISOString(),+ updated_by: input.userId,+ })+ .where('id', '=', node.id).execute();return {@@ -37,7 +62,7 @@ export class NodeCollaboratorCreateMutationHandlerchanges: [{type: 'workspace',- table: 'node_collaborators',+ table: 'nodes',userId: input.userId,},],diff --git a/desktop/src/main/handlers/mutations/node-collaborator-delete.ts b/desktop/src/main/handlers/mutations/node-collaborator-delete.tsindex 58838e7a..1d7ae852 100644--- a/desktop/src/main/handlers/mutations/node-collaborator-delete.ts+++ b/desktop/src/main/handlers/mutations/node-collaborator-delete.ts@@ -1,6 +1,8 @@import { databaseManager } from '@/main/data/database-manager';import { MutationHandler, MutationResult } from '@/operations/mutations';import { NodeCollaboratorDeleteMutationInput } from '@/operations/mutations/node-collaborator-delete';+import { fromUint8Array } from 'js-base64';+import * as Y from 'yjs';export class NodeCollaboratorDeleteMutationHandlerimplements MutationHandler<NodeCollaboratorDeleteMutationInput>@@ -12,14 +14,39 @@ export class NodeCollaboratorDeleteMutationHandlerinput.userId,);+ const node = await workspaceDatabase+ .selectFrom('nodes')+ .where('id', '=', input.nodeId)+ .selectAll()+ .executeTakeFirst();++ if (!node) {+ throw new Error('Node not found');+ }++ const doc = new Y.Doc({+ guid: node.id,+ });++ const attributesMap = doc.getMap('attributes');++ doc.transact(() => {+ const collaboratorsMap = attributesMap.get('collaborators') as Y.Map<any>;+ collaboratorsMap.delete(input.collaboratorId);+ });++ const attributes = JSON.stringify(attributesMap.toJSON());+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));+await workspaceDatabase- .deleteFrom('node_collaborators')- .where((eb) =>- eb.and([- eb('node_id', '=', input.nodeId),- eb('collaborator_id', '=', input.collaboratorId),- ]),- )+ .updateTable('nodes')+ .set({+ attributes: attributes,+ state: encodedState,+ updated_at: new Date().toISOString(),+ updated_by: input.userId,+ })+ .where('id', '=', node.id).execute();return {@@ -29,7 +56,7 @@ export class NodeCollaboratorDeleteMutationHandlerchanges: [{type: 'workspace',- table: 'node_collaborators',+ table: 'nodes',userId: input.userId,},],diff --git a/desktop/src/main/handlers/mutations/node-collaborator-server-create.ts b/desktop/src/main/handlers/mutations/node-collaborator-server-create.tsdeleted file mode 100644index 33f38e13..00000000--- a/desktop/src/main/handlers/mutations/node-collaborator-server-create.ts+++ /dev/null@@ -1,69 +0,0 @@-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.tsdeleted file mode 100644index ada5cad8..00000000--- a/desktop/src/main/handlers/mutations/node-collaborator-server-delete.ts+++ /dev/null@@ -1,57 +0,0 @@-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.tsdeleted file mode 100644index 8cac7e03..00000000--- a/desktop/src/main/handlers/mutations/node-collaborator-server-update.ts+++ /dev/null@@ -1,96 +0,0 @@-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-update.ts b/desktop/src/main/handlers/mutations/node-collaborator-update.tsindex 4e800ab7..b5e073b9 100644--- a/desktop/src/main/handlers/mutations/node-collaborator-update.ts+++ b/desktop/src/main/handlers/mutations/node-collaborator-update.ts@@ -2,7 +2,8 @@ import { databaseManager } from '@/main/data/database-manager';import { generateId, IdType } from '@/lib/id';import { MutationHandler, MutationResult } from '@/operations/mutations';import { NodeCollaboratorUpdateMutationInput } from '@/operations/mutations/node-collaborator-update';-+import * as Y from 'yjs';+import { fromUint8Array } from 'js-base64';export class NodeCollaboratorUpdateMutationHandlerimplements MutationHandler<NodeCollaboratorUpdateMutationInput>{@@ -13,20 +14,44 @@ export class NodeCollaboratorUpdateMutationHandlerinput.userId,);+ const node = await workspaceDatabase+ .selectFrom('nodes')+ .where('id', '=', input.nodeId)+ .selectAll()+ .executeTakeFirst();++ if (!node) {+ throw new Error('Node not found');+ }++ const doc = new Y.Doc({+ guid: node.id,+ });++ const attributesMap = doc.getMap('attributes');++ doc.transact(() => {+ if (!attributesMap.has('collaborators')) {+ attributesMap.set('collaborators', new Y.Map());+ }++ const collaboratorsMap = attributesMap.get('collaborators') as Y.Map<any>;+ collaboratorsMap.set(input.collaboratorId, input.role);+ });++ const attributes = JSON.stringify(attributesMap.toJSON());+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));+await workspaceDatabase- .updateTable('node_collaborators')+ .updateTable('nodes').set({- role: input.role,+ attributes: attributes,+ state: encodedState,updated_at: new Date().toISOString(),updated_by: input.userId,version_id: generateId(IdType.Version),})- .where((eb) =>- eb.and([- eb('node_id', '=', input.nodeId),- eb('collaborator_id', '=', input.collaboratorId),- ]),- )+ .where('id', '=', node.id).execute();return {@@ -36,7 +61,7 @@ export class NodeCollaboratorUpdateMutationHandlerchanges: [{type: 'workspace',- table: 'node_collaborators',+ table: 'nodes',userId: input.userId,},],diff --git a/desktop/src/main/handlers/mutations/node-reaction-create.ts b/desktop/src/main/handlers/mutations/node-reaction-create.tsindex 27f0861e..1b71e195 100644--- a/desktop/src/main/handlers/mutations/node-reaction-create.ts+++ b/desktop/src/main/handlers/mutations/node-reaction-create.ts@@ -1,6 +1,9 @@+import { generateId, IdType } from '@/lib/id';import { databaseManager } from '@/main/data/database-manager';import { MutationHandler, MutationResult } from '@/operations/mutations';import { NodeReactionCreateMutationInput } from '@/operations/mutations/node-reaction-create';+import { fromUint8Array, toUint8Array } from 'js-base64';+import * as Y from 'yjs';export class NodeReactionCreateMutationHandlerimplements MutationHandler<NodeReactionCreateMutationInput>@@ -12,15 +15,59 @@ export class NodeReactionCreateMutationHandlerinput.userId,);+ const node = await workspaceDatabase+ .selectFrom('nodes')+ .where('id', '=', input.nodeId)+ .selectAll()+ .executeTakeFirst();++ if (!node) {+ throw new Error('Node not found');+ }++ const doc = new Y.Doc({+ guid: node.id,+ });++ Y.applyUpdate(doc, toUint8Array(node.state));++ const attributesMap = doc.getMap('attributes');++ doc.transact(() => {+ if (!attributesMap.has('reactions')) {+ attributesMap.set('reactions', new Y.Map());+ }++ const reactionsMap = attributesMap.get('reactions') as Y.Map<any>;+ if (!reactionsMap.has(input.reaction)) {+ reactionsMap.set(input.reaction, new Y.Array());+ }++ const reactionArray = reactionsMap.get(input.reaction) as Y.Array<string>;+ if (reactionArray.toArray().includes(input.userId)) {+ return;+ }++ reactionArray.push([input.userId]);+ });++ const attributes = JSON.stringify(attributesMap.toJSON());+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));++ if (encodedState === node.state) {+ return;+ }+await workspaceDatabase- .insertInto('node_reactions')- .values({- node_id: input.nodeId,- actor_id: input.userId,- reaction: input.reaction,- created_at: new Date().toISOString(),+ .updateTable('nodes')+ .set({+ attributes: attributes,+ state: encodedState,+ updated_at: new Date().toISOString(),+ updated_by: input.userId,+ version_id: generateId(IdType.Version),})- .onConflict((b) => b.doNothing())+ .where('id', '=', node.id).execute();return {@@ -30,7 +77,7 @@ export class NodeReactionCreateMutationHandlerchanges: [{type: 'workspace',- table: 'node_reactions',+ table: 'nodes',userId: input.userId,},],diff --git a/desktop/src/main/handlers/mutations/node-reaction-delete.ts b/desktop/src/main/handlers/mutations/node-reaction-delete.tsindex bbf37d90..3b24a50c 100644--- a/desktop/src/main/handlers/mutations/node-reaction-delete.ts+++ b/desktop/src/main/handlers/mutations/node-reaction-delete.ts@@ -1,6 +1,9 @@import { databaseManager } from '@/main/data/database-manager';import { MutationHandler, MutationResult } from '@/operations/mutations';import { NodeReactionDeleteMutationInput } from '@/operations/mutations/node-reaction-delete';+import * as Y from 'yjs';+import { fromUint8Array, toUint8Array } from 'js-base64';+import { generateId, IdType } from '@/lib/id';export class NodeReactionDeleteMutationHandlerimplements MutationHandler<NodeReactionDeleteMutationInput>@@ -12,15 +15,61 @@ export class NodeReactionDeleteMutationHandlerinput.userId,);+ const node = await workspaceDatabase+ .selectFrom('nodes')+ .where('id', '=', input.nodeId)+ .selectAll()+ .executeTakeFirst();++ if (!node) {+ throw new Error('Node not found');+ }++ const doc = new Y.Doc({+ guid: node.id,+ });++ Y.applyUpdate(doc, toUint8Array(node.state));++ const attributesMap = doc.getMap('attributes');++ doc.transact(() => {+ if (!attributesMap.has('reactions')) {+ attributesMap.set('reactions', new Y.Map());+ }++ const reactionsMap = attributesMap.get('reactions') as Y.Map<any>;+ if (!reactionsMap.has(input.reaction)) {+ reactionsMap.set(input.reaction, new Y.Array());+ }++ const reactionArray = reactionsMap.get(input.reaction) as Y.Array<string>;++ const index = reactionArray.toArray().indexOf(input.userId);+ if (index === -1) {+ return;+ }++ reactionArray.delete(index);+ });++ const attributes = JSON.stringify(attributesMap.toJSON());+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));++ if (encodedState === node.state) {+ return;+ }+await workspaceDatabase- .deleteFrom('node_reactions')- .where((eb) =>- eb.and([- eb('node_id', '=', input.nodeId),- eb('actor_id', '=', input.userId),- eb('reaction', '=', input.reaction),- ]),- )+ .updateTable('nodes')+ .set({+ attributes: attributes,+ state: encodedState,+ updated_at: new Date().toISOString(),+ updated_by: input.userId,+ version_id: generateId(IdType.Version),+ })+ .where('id', '=', node.id).execute();return {@@ -30,7 +79,7 @@ export class NodeReactionDeleteMutationHandlerchanges: [{type: 'workspace',- table: 'node_reactions',+ table: 'nodes',userId: input.userId,},],diff --git a/desktop/src/main/handlers/mutations/node-reaction-server-create.ts b/desktop/src/main/handlers/mutations/node-reaction-server-create.tsdeleted file mode 100644index 0796c438..00000000--- a/desktop/src/main/handlers/mutations/node-reaction-server-create.ts+++ /dev/null@@ -1,65 +0,0 @@-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.tsdeleted file mode 100644index cb0b53b6..00000000--- a/desktop/src/main/handlers/mutations/node-reaction-server-delete.ts+++ /dev/null@@ -1,58 +0,0 @@-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-batch-sync.ts b/desktop/src/main/handlers/mutations/node-sync.tssimilarity index 63%rename from desktop/src/main/handlers/mutations/node-batch-sync.tsrename to desktop/src/main/handlers/mutations/node-sync.tsindex 4ccc60a8..f1cc914f 100644--- a/desktop/src/main/handlers/mutations/node-batch-sync.ts+++ b/desktop/src/main/handlers/mutations/node-sync.ts@@ -1,39 +1,42 @@import { databaseManager } from '@/main/data/database-manager';-import { MutationHandler, MutationResult } from '@/operations/mutations';-import { NodeBatchSyncMutationInput } from '@/operations/mutations/node-batch-sync';+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 NodeBatchSyncMutationHandler- implements MutationHandler<NodeBatchSyncMutationInput>+export class NodeSyncMutationHandler+ implements MutationHandler<NodeSyncMutationInput>{public async handleMutation(- input: NodeBatchSyncMutationInput,- ): Promise<MutationResult<NodeBatchSyncMutationInput>> {- const workspace = await databaseManager.appDatabase- .selectFrom('workspaces')- .selectAll()- .where((eb) =>- eb.and([- eb('account_id', '=', input.accountId),- eb('workspace_id', '=', input.workspaceId),- ]),- )- .executeTakeFirst();+ input: NodeSyncMutationInput,+ ): Promise<MutationResult<NodeSyncMutationInput>> {+ const changes: MutationChange[] = [];- if (!workspace) {- return {- output: {- success: false,- },- };- }+ 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);+ const userId = workspace.user_id;+ const workspaceDatabase =+ await databaseManager.getWorkspaceDatabase(userId);- for (const node of input.nodes) {const existingNode = await workspaceDatabase.selectFrom('nodes').where('id', '=', node.id)@@ -72,7 +75,7 @@ export class NodeBatchSyncMutationHandler.where('version_id', '=', node.versionId),).execute();- } else if (existingNode.version_id !== node.versionId) {+ } else {const doc = new Y.Doc({guid: node.id,});@@ -88,6 +91,7 @@ export class NodeBatchSyncMutationHandler.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,@@ -97,19 +101,25 @@ export class NodeBatchSyncMutationHandler.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: [- {- type: 'workspace',- table: 'nodes',- userId: userId,- },- ],+ changes: changes,};}}diff --git a/desktop/src/main/handlers/mutations/space-create.ts b/desktop/src/main/handlers/mutations/space-create.tsindex 7a4cde01..74079979 100644--- a/desktop/src/main/handlers/mutations/space-create.ts+++ b/desktop/src/main/handlers/mutations/space-create.ts@@ -1,9 +1,11 @@import { databaseManager } from '@/main/data/database-manager';-import { NodeTypes } from '@/lib/constants';import { generateId, IdType } from '@/lib/id';-import { buildCreateNode, generateNodeIndex } from '@/lib/nodes';+import { generateNodeIndex } from '@/lib/nodes';import { MutationHandler, MutationResult } from '@/operations/mutations';import { SpaceCreateMutationInput } from '@/operations/mutations/space-create';+import * as Y from 'yjs';+import { NodeTypes } from '@/lib/constants';+import { fromUint8Array } from 'js-base64';export class SpaceCreateMutationHandlerimplements MutationHandler<SpaceCreateMutationInput>@@ -16,65 +18,97 @@ export class SpaceCreateMutationHandler);const spaceId = generateId(IdType.Space);- const pageId = generateId(IdType.Page);- const channelId = generateId(IdType.Channel);+ const spaceDoc = new Y.Doc({+ guid: spaceId,+ });++ const spaceAttributesMap = spaceDoc.getMap('attributes');+ spaceDoc.transact(() => {+ spaceAttributesMap.set('type', NodeTypes.Space);+ spaceAttributesMap.set('name', input.name);++ if (input.description) {+ spaceAttributesMap.set('description', input.description);+ }+ if (!spaceAttributesMap.has('collaborators')) {+ spaceAttributesMap.set('collaborators', new Y.Map<string>());+ }++ const collaboratorsMap = spaceAttributesMap.get(+ 'collaborators',+ ) as Y.Map<string>;++ collaboratorsMap.set(input.userId, 'owner');+ });++ const spaceAttributes = JSON.stringify(spaceAttributesMap.toJSON());+ const spaceState = fromUint8Array(Y.encodeStateAsUpdate(spaceDoc));++ const pageId = generateId(IdType.Page);const pageIndex = generateNodeIndex(null, null);+ const pageDoc = new Y.Doc({+ guid: pageId,+ });++ const pageAttributesMap = pageDoc.getMap('attributes');+ pageDoc.transact(() => {+ pageAttributesMap.set('type', NodeTypes.Page);+ pageAttributesMap.set('name', 'Home');+ pageAttributesMap.set('parentId', spaceId);+ pageAttributesMap.set('index', pageIndex);+ });++ const pageAttributes = JSON.stringify(pageAttributesMap.toJSON());+ const pageState = fromUint8Array(Y.encodeStateAsUpdate(pageDoc));++ const channelId = generateId(IdType.Channel);const channelIndex = generateNodeIndex(pageIndex, null);+ const channelDoc = new Y.Doc({+ guid: channelId,+ });++ const channelAttributesMap = channelDoc.getMap('attributes');+ channelDoc.transact(() => {+ channelAttributesMap.set('type', NodeTypes.Channel);+ channelAttributesMap.set('name', 'Discussions');+ channelAttributesMap.set('parentId', spaceId);+ channelAttributesMap.set('index', channelIndex);+ });++ const channelAttributes = JSON.stringify(channelAttributesMap.toJSON());+ const channelState = fromUint8Array(Y.encodeStateAsUpdate(channelDoc));await workspaceDatabase.transaction().execute(async (trx) => {await trx.insertInto('nodes').values([- buildCreateNode(- {- id: spaceId,- attributes: {- type: NodeTypes.Space,- name: input.name,- description: input.description,- },- },- input.userId,- ),- buildCreateNode(- {- id: pageId,- attributes: {- type: NodeTypes.Page,- parentId: spaceId,- index: pageIndex,- name: 'Home',- },- },- input.userId,- ),- buildCreateNode(- {- id: channelId,- attributes: {- type: NodeTypes.Channel,- parentId: spaceId,- index: channelIndex,- name: 'Discussions',- },- },- input.userId,- ),+ {+ id: spaceId,+ attributes: spaceAttributes,+ state: spaceState,+ created_at: new Date().toISOString(),+ created_by: input.userId,+ version_id: generateId(IdType.Version),+ },+ {+ id: pageId,+ attributes: pageAttributes,+ state: pageState,+ created_at: new Date().toISOString(),+ created_by: input.userId,+ version_id: generateId(IdType.Version),+ },+ {+ id: channelId,+ attributes: channelAttributes,+ state: channelState,+ created_at: new Date().toISOString(),+ created_by: input.userId,+ version_id: generateId(IdType.Version),+ },]).execute();-- await trx- .insertInto('node_collaborators')- .values({- node_id: spaceId,- collaborator_id: input.userId,- role: 'owner',- created_at: new Date().toISOString(),- created_by: input.userId,- version_id: generateId(IdType.Version),- })- .execute();});return {diff --git a/desktop/src/main/handlers/queries/chat-get.ts b/desktop/src/main/handlers/queries/chat-get.tsindex 030bf64a..4895acfe 100644--- a/desktop/src/main/handlers/queries/chat-get.ts+++ b/desktop/src/main/handlers/queries/chat-get.ts@@ -6,7 +6,6 @@ import {QueryResult,} from '@/operations/queries';import { mapNode } from '@/lib/nodes';-import { sql } from 'kysely';import { SelectNode } from '@/main/data/workspace/schema';import { MutationChange } from '@/operations/mutations';import { isEqual } from 'lodash';@@ -16,11 +15,20 @@ export class ChatGetQueryHandler implements QueryHandler<ChatGetQueryInput> {async handleQuery(input: ChatGetQueryInput,): Promise<QueryResult<ChatGetQueryInput>> {- const rows = await this.fetchNodes(input);+ const chat = await this.fetchChat(input);+ if (!chat) {+ return {+ output: null,+ state: {},+ };+ }++ const collaborators = await this.fetchCollaborators(input, chat);return {- output: this.buildChat(input.chatId, rows),+ output: this.buildChat(chat, collaborators),state: {- rows,+ chat,+ collaborators,},};}@@ -34,7 +42,7 @@ export class ChatGetQueryHandler implements QueryHandler<ChatGetQueryInput> {!changes.some((change) =>change.type === 'workspace' &&- (change.table === 'nodes' || change.table === 'node_collaborators') &&+ change.table === 'nodes' &&change.userId === input.userId,)) {@@ -43,8 +51,13 @@ export class ChatGetQueryHandler implements QueryHandler<ChatGetQueryInput> {};}- const rows = await this.fetchNodes(input);- if (isEqual(rows, state.rows)) {+ const chat = await this.fetchChat(input);+ const collaborators = await this.fetchCollaborators(input, chat);++ if (+ isEqual(chat, state.chat) &&+ isEqual(collaborators, state.collaborators)+ ) {return {hasChanges: false,};@@ -53,59 +66,80 @@ export class ChatGetQueryHandler implements QueryHandler<ChatGetQueryInput> {return {hasChanges: true,result: {- output: this.buildChat(input.chatId, rows),+ output: this.buildChat(chat, collaborators),state: {- rows,+ chat,+ collaborators,},},};}- private async fetchNodes(input: ChatGetQueryInput): Promise<SelectNode[]> {+ private async fetchChat(+ input: ChatGetQueryInput,+ ): Promise<SelectNode | null> {const workspaceDatabase = await databaseManager.getWorkspaceDatabase(input.userId,);- const query = sql<SelectNode>`- WITH chat_node AS (- SELECT *- FROM nodes- WHERE id = ${input.chatId}- ),- collaborator_nodes AS (- SELECT *- FROM nodes- WHERE id IN- (- SELECT DISTINCT collaborator_id- FROM node_collaborators- WHERE node_id = ${input.chatId} AND collaborator_id != ${input.userId}- )- )- SELECT * FROM chat_node- UNION ALL- SELECT * FROM collaborator_nodes- `.compile(workspaceDatabase);+ const chat = await workspaceDatabase+ .selectFrom('nodes')+ .where('id', '=', input.chatId)+ .selectAll()+ .executeTakeFirst();- const result = await workspaceDatabase.executeQuery(query);- return result.rows;+ return chat;}- private buildChat = (chatId: string, rows: SelectNode[]): ChatNode | null => {- const nodes = rows.map(mapNode);- const chatNode = nodes.find((node) => node.id === chatId);- if (!chatNode) {- return null;+ private async fetchCollaborators(+ input: ChatGetQueryInput,+ chat: SelectNode,+ ): Promise<SelectNode[]> {+ const workspaceDatabase = await databaseManager.getWorkspaceDatabase(+ input.userId,+ );++ if (!chat.attributes) {+ return [];+ }++ const attributes = JSON.parse(chat.attributes);+ if (!attributes.collaborators) {+ return [];}- const collaborators = rows.filter((node) => node.id !== chatId);- if (collaborators.length === 0) {- return null;+ const collaboratorIds = Object.keys(attributes.collaborators);+ if (attributes.collaborators) {+ for (const collaboratorId of collaboratorIds) {+ if (collaboratorId === input.userId) {+ continue;+ }++ if (collaboratorIds.includes(collaboratorId)) {+ continue;+ }++ collaboratorIds.push(collaboratorId);+ }}+ const collaborators = await workspaceDatabase+ .selectFrom('nodes')+ .where('id', 'in', collaboratorIds)+ .selectAll()+ .execute();++ const nodes = [chat, ...collaborators];+ return nodes;+ }++ private buildChat = (+ chat: SelectNode,+ collaborators: SelectNode[],+ ): ChatNode | null => {const collaborator = mapNode(collaborators[0]);return {- id: chatNode.id,+ id: chat.id,name: collaborator.attributes.name ?? 'Unknown',avatar: collaborator.attributes.avatar ?? null,};diff --git a/desktop/src/main/handlers/queries/message-list.ts b/desktop/src/main/handlers/queries/message-list.tsindex dae48974..7ab9a584 100644--- a/desktop/src/main/handlers/queries/message-list.ts+++ b/desktop/src/main/handlers/queries/message-list.ts@@ -7,7 +7,6 @@ import {} from '@/operations/queries';import { MutationChange } from '@/operations/mutations';import { SelectNode } from '@/main/data/workspace/schema';-import { sql } from 'kysely';import { NodeTypes } from '@/lib/constants';import { MessageNode, MessageReactionCount } from '@/types/messages';import { mapNode } from '@/lib/nodes';@@ -15,23 +14,20 @@ import { UserNode } from '@/types/users';import { compareString } from '@/lib/utils';import { isEqual } from 'lodash';-type MessageRow = SelectNode & {- reaction_counts: string | null;- user_reactions: string | null;-};-export class MessageListQueryHandlerimplements QueryHandler<MessageListQueryInput>{public async handleQuery(input: MessageListQueryInput,): Promise<QueryResult<MessageListQueryInput>> {- const rows = await this.fetchMesssages(input);+ const messages = await this.fetchMesssages(input);+ const authors = await this.fetchAuthors(input, messages);return {- output: this.buildMessages(rows),+ output: this.buildMessages(input.userId, messages, authors),state: {- rows,+ messages,+ authors,},};}@@ -45,7 +41,7 @@ export class MessageListQueryHandler!changes.some((change) =>change.type === 'workspace' &&- (change.table === 'nodes' || change.table === 'node_reactions') &&+ change.table === 'nodes' &&change.userId === input.userId,)) {@@ -54,8 +50,10 @@ export class MessageListQueryHandler};}- const rows = await this.fetchMesssages(input);- if (isEqual(rows, state.rows)) {+ const messages = await this.fetchMesssages(input);+ const authors = await this.fetchAuthors(input, messages);++ if (isEqual(messages, state.messages) && isEqual(authors, state.authors)) {return {hasChanges: false,};@@ -64,9 +62,10 @@ export class MessageListQueryHandlerreturn {hasChanges: true,result: {- output: this.buildMessages(rows),+ output: this.buildMessages(input.userId, messages, authors),state: {- rows,+ messages,+ authors,},},};@@ -74,73 +73,56 @@ export class MessageListQueryHandlerprivate async fetchMesssages(input: MessageListQueryInput,- ): Promise<MessageRow[]> {+ ): Promise<SelectNode[]> {const workspaceDatabase = await databaseManager.getWorkspaceDatabase(input.userId,);const offset = input.page * input.count;- const query = sql<MessageRow>`- WITH message_nodes AS (- SELECT *- FROM nodes- WHERE parent_id = ${input.conversationId} AND type = ${NodeTypes.Message}- ORDER BY id DESC- LIMIT ${sql.lit(input.count)}- OFFSET ${sql.lit(offset)}- ),- author_nodes AS (- SELECT *- FROM nodes- WHERE id IN (SELECT DISTINCT created_by FROM message_nodes)- ),- all_nodes AS (- SELECT * FROM message_nodes- UNION ALL- SELECT * FROM author_nodes- ),- message_reaction_counts AS (- SELECT- mrc.node_id,- json_group_array(- json_object(- 'reaction', mrc.'reaction',- 'count', mrc.'count'- )- ) AS reaction_counts- FROM (- SELECT node_id, reaction, COUNT(*) as count- FROM node_reactions- WHERE node_id IN (SELECT id FROM message_nodes)- GROUP BY node_id, reaction- ) mrc- GROUP BY mrc.node_id- ),- user_reactions AS (- SELECT- ur.node_id,- json_group_array(ur.reaction) AS user_reactions- FROM node_reactions ur- WHERE ur.node_id IN (SELECT id FROM message_nodes) AND ur.actor_id = ${input.userId}- GROUP BY ur.node_id+ const messages = await workspaceDatabase+ .selectFrom('nodes')+ .selectAll()+ .where((eb) =>+ eb.and([+ eb('parent_id', '=', input.conversationId),+ eb('type', '=', NodeTypes.Message),+ ]),)- SELECT- n.*,- COALESCE(mrc.reaction_counts, json('[]')) AS reaction_counts,- COALESCE(ur.user_reactions, json('[]')) AS user_reactions- FROM all_nodes n- LEFT JOIN message_reaction_counts mrc ON n.id = mrc.node_id- LEFT JOIN user_reactions ur ON n.id = ur.node_id;- `.compile(workspaceDatabase);-- const result = await workspaceDatabase.executeQuery(query);- return result.rows;+ .orderBy('id', 'desc')+ .limit(input.count)+ .offset(offset)+ .execute();++ return messages;}- private buildMessages = (rows: MessageRow[]): MessageNode[] => {- const messageRows = rows.filter((row) => row.type === NodeTypes.Message);- const authorRows = rows.filter((row) => row.type === NodeTypes.User);+ private async fetchAuthors(+ input: MessageListQueryInput,+ messages: SelectNode[],+ ): Promise<SelectNode[]> {+ if (messages.length === 0) {+ return [];+ }++ const workspaceDatabase = await databaseManager.getWorkspaceDatabase(+ input.userId,+ );++ const authorIds = messages.map((message) => message.created_by);+ const authors = await workspaceDatabase+ .selectFrom('nodes')+ .selectAll()+ .where('id', 'in', authorIds)+ .execute();++ return authors;+ }+ private buildMessages = (+ userId: string,+ messageRows: SelectNode[],+ authorRows: SelectNode[],+ ): MessageNode[] => {const messages: MessageNode[] = [];const authorMap = new Map<string, UserNode>();for (const authorRow of authorRows) {@@ -160,11 +142,16 @@ export class MessageListQueryHandlerfor (const messageRow of messageRows) {const messageNode = mapNode(messageRow);const author = authorMap.get(messageNode.createdBy);+ const reactions =+ (messageNode.attributes.reactions as Record<string, string[]>) ?? {};- const reactionCounts: MessageReactionCount[] = JSON.parse(- messageRow.reaction_counts,- );- const userReactions: string[] = JSON.parse(messageRow.user_reactions);+ const reactionCounts: MessageReactionCount[] = Object.entries(reactions)+ .map(([reaction, users]) => ({+ reaction,+ count: users.length,+ isReactedTo: users.includes(userId),+ }))+ .filter((reactionCount) => reactionCount.count > 0);const message: MessageNode = {id: messageNode.id,@@ -177,7 +164,6 @@ export class MessageListQueryHandler},content: messageNode.attributes.content,reactionCounts,- userReactions,};messages.push(message);diff --git a/desktop/src/main/handlers/queries/node-collaborator-list.ts b/desktop/src/main/handlers/queries/node-collaborator-list.tsindex ae6876cc..e935c009 100644--- a/desktop/src/main/handlers/queries/node-collaborator-list.ts+++ b/desktop/src/main/handlers/queries/node-collaborator-list.ts@@ -49,7 +49,7 @@ export class NodeCollaboratorListQueryHandler!changes.some((change) =>change.type === 'workspace' &&- (change.table === 'nodes' || change.table === 'node_collaborators') &&+ change.table === 'nodes' &&change.userId === input.userId,)) {diff --git a/desktop/src/main/handlers/queries/sidebar-chat-list.ts b/desktop/src/main/handlers/queries/sidebar-chat-list.tsindex d25e9254..dfdd7c62 100644--- a/desktop/src/main/handlers/queries/sidebar-chat-list.ts+++ b/desktop/src/main/handlers/queries/sidebar-chat-list.ts@@ -5,7 +5,6 @@ import {QueryHandler,QueryResult,} from '@/operations/queries';-import { sql } from 'kysely';import { SelectNode } from '@/main/data/workspace/schema';import { NodeTypes } from '@/lib/constants';import { SidebarChatNode } from '@/types/workspaces';@@ -13,21 +12,20 @@ import { mapNode } from '@/lib/nodes';import { MutationChange } from '@/operations/mutations';import { isEqual } from 'lodash';-type ChatRow = SelectNode & {- collaborators: string;-};-export class SidebarChatListQueryHandlerimplements QueryHandler<SidebarChatListQueryInput>{public async handleQuery(input: SidebarChatListQueryInput,): Promise<QueryResult<SidebarChatListQueryInput>> {- const rows = await this.fetchChats(input);+ const chats = await this.fetchChats(input);+ const collaborators = await this.fetchChatCollaborators(input, chats);+return {- output: this.buildSidebarChatNodes(rows),+ output: this.buildSidebarChatNodes(input.userId, chats, collaborators),state: {- rows,+ chats,+ collaborators,},};}@@ -41,7 +39,7 @@ export class SidebarChatListQueryHandler!changes.some((change) =>change.type === 'workspace' &&- (change.table === 'nodes' || change.table === 'node_collaborators') &&+ change.table === 'nodes' &&change.userId === input.userId,)) {@@ -50,8 +48,12 @@ export class SidebarChatListQueryHandler};}- const rows = await this.fetchChats(input);- if (isEqual(rows, state.rows)) {+ const chats = await this.fetchChats(input);+ const collaborators = await this.fetchChatCollaborators(input, chats);+ if (+ isEqual(chats, state.chats) &&+ isEqual(collaborators, state.collaborators)+ ) {return {hasChanges: false,};@@ -60,9 +62,10 @@ export class SidebarChatListQueryHandlerreturn {hasChanges: true,result: {- output: this.buildSidebarChatNodes(rows),+ output: this.buildSidebarChatNodes(input.userId, chats, collaborators),state: {- rows,+ chats,+ collaborators,},},};@@ -70,86 +73,99 @@ export class SidebarChatListQueryHandlerprivate async fetchChats(input: SidebarChatListQueryInput,- ): Promise<ChatRow[]> {+ ): Promise<SelectNode[]> {const workspaceDatabase = await databaseManager.getWorkspaceDatabase(input.userId,);- const query = sql<ChatRow>`- WITH chat_nodes AS (- SELECT *- FROM nodes- WHERE parent_id IS NULL AND type = ${NodeTypes.Chat}- ),- collaborator_nodes AS (- SELECT *- FROM nodes- WHERE id IN- (- SELECT collaborator_id- FROM node_collaborators- WHERE collaborator_id != ${input.userId}- AND node_id IN- (- SELECT id- FROM chat_nodes- )- )- ),- all_nodes AS (- SELECT * FROM chat_nodes- UNION ALL- SELECT * FROM collaborator_nodes- ),- chat_collaborators AS (- SELECT- nc.node_id,- json_group_array(nc.collaborator_id) AS collaborators- FROM node_collaborators nc- WHERE nc.node_id IN (SELECT id FROM chat_nodes)- AND nc.collaborator_id != ${input.userId}- GROUP BY nc.node_id- )- SELECT- n.*,- COALESCE(cc.collaborators, json('[]')) AS collaborators- FROM all_nodes n- LEFT JOIN chat_collaborators cc ON n.id = cc.node_id- `.compile(workspaceDatabase);-- const result = await workspaceDatabase.executeQuery(query);- return result.rows;+ const chats = await workspaceDatabase+ .selectFrom('nodes')+ .where('type', '=', NodeTypes.Chat)+ .selectAll()+ .execute();++ return chats;+ }++ private async fetchChatCollaborators(+ input: SidebarChatListQueryInput,+ chats: SelectNode[],+ ): Promise<SelectNode[]> {+ const workspaceDatabase = await databaseManager.getWorkspaceDatabase(+ input.userId,+ );++ const collaboratorIds: string[] = [];+ for (const chat of chats) {+ if (!chat.attributes) {+ continue;+ }++ const attributes = JSON.parse(chat.attributes);+ if (!attributes.collaborators) {+ continue;+ }++ const keys = Object.keys(attributes.collaborators);+ for (const key of keys) {+ if (key === input.userId) {+ continue;+ }++ collaboratorIds.push(key);+ }+ }++ const collaborators = await workspaceDatabase+ .selectFrom('nodes')+ .where('id', 'in', collaboratorIds)+ .selectAll()+ .execute();++ return collaborators;}- private buildSidebarChatNodes = (rows: ChatRow[]): SidebarChatNode[] => {- const chats: SidebarChatNode[] = [];+ private buildSidebarChatNodes = (+ userId: string,+ chats: SelectNode[],+ collaborators: SelectNode[],+ ): SidebarChatNode[] => {+ const sidebarChatNodes: SidebarChatNode[] = [];- for (const row of rows) {- if (row.type !== NodeTypes.Chat) {+ for (const chat of chats) {+ const chatNode = mapNode(chat);+ if (!chatNode.attributes || !chatNode.attributes.collaborators) {continue;}- const chatNode = mapNode(row);- const collaboratorIds = JSON.parse(row.collaborators) as string[];+ const collaboratorIds = Object.keys(chatNode.attributes.collaborators);if (!collaboratorIds || collaboratorIds.length === 0) {continue;}- const collaboratorId = collaboratorIds[0];- const collaboratorRow = rows.find((r) => r.id === collaboratorId);+ const collaboratorId = collaboratorIds.find((id) => id !== userId);++ if (!collaboratorId) {+ continue;+ }++ const collaboratorRow = collaborators.find(+ (r) => r.id === collaboratorId,+ );+if (!collaboratorRow) {continue;}const collaboratorNode = mapNode(collaboratorRow);- chats.push({+ sidebarChatNodes.push({id: chatNode.id,type: chatNode.type,- name: collaboratorNode.attributes.name,- avatar: chatNode.attributes.avatar,+ name: collaboratorNode.attributes.name ?? 'Unknown',+ avatar: collaboratorNode.attributes.avatar ?? null,});}- return chats;+ return sidebarChatNodes;};}diff --git a/desktop/src/main/synchronizer.ts b/desktop/src/main/synchronizer.tsindex b7f4683b..a53ef017 100644--- a/desktop/src/main/synchronizer.ts+++ b/desktop/src/main/synchronizer.ts@@ -1,12 +1,7 @@import { BackoffCalculator } from '@/lib/backoff-calculator';import { buildAxiosInstance } from '@/lib/servers';import { databaseManager } from '@/main/data/database-manager';-import {- ServerChange,- ServerChangeData,- ServerSyncResponse,-} from '@/types/sync';-import { mediator } from '@/main/mediator';+import { ServerSyncResponse } from '@/types/sync';const EVENT_LOOP_INTERVAL = 100;@@ -38,157 +33,6 @@ class Synchronizer {setTimeout(this.executeEventLoop, EVENT_LOOP_INTERVAL);}- public async handleServerChange(accountId: string, change: ServerChange) {- const executed = await this.executeServerChange(- accountId,- change.workspaceId,- change.data,- );-- await mediator.executeMessage(- {- accountId,- deviceId: change.deviceId,- },- {- type: 'server_change_result',- changeId: change.id,- success: executed,- },- );- }-- private async executeServerChange(- accountId: string,- workspaceId: string,- data: ServerChangeData,- ): Promise<boolean> {- switch (data.type) {- case 'node_create': {- const result = await mediator.executeMutation({- type: 'node_server_create',- accountId: accountId,- workspaceId: 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: 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: workspaceId,- id: data.id,- });-- return result.success;- }- case 'node_collaborator_create': {- const result = await mediator.executeMutation({- type: 'node_collaborator_server_create',- accountId: accountId,- workspaceId: 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: 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: 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: 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: workspaceId,- nodeId: data.nodeId,- actorId: data.actorId,- reaction: data.reaction,- });-- return result.success;- }- case 'node_batch_sync': {- const result = await mediator.executeMutation({- type: 'node_batch_sync',- accountId: accountId,- workspaceId: workspaceId,- nodes: data.nodes,- });-- return result.success;- }- default: {- return false;- }- }- }-private async checkForLoggedOutAccount() {const accounts = await databaseManager.appDatabase.selectFrom('accounts')diff --git a/desktop/src/operations/messages/local-node-sync.ts b/desktop/src/operations/messages/local-node-sync.tsnew file mode 100644index 00000000..374636b1--- /dev/null+++ b/desktop/src/operations/messages/local-node-sync.ts@@ -0,0 +1,11 @@+export type LocalNodeSyncMessageInput = {+ type: 'local_node_sync';+ nodeId: string;+ versionId: string;+};++declare module '@/operations/messages' {+ interface MessageMap {+ local_node_sync: LocalNodeSyncMessageInput;+ }+}diff --git a/desktop/src/operations/messages/server-change-batch.ts b/desktop/src/operations/messages/server-change-batch.tsdeleted file mode 100644index 9ca0074e..00000000--- a/desktop/src/operations/messages/server-change-batch.ts+++ /dev/null@@ -1,12 +0,0 @@-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.tsdeleted file mode 100644index b9e1140b..00000000--- a/desktop/src/operations/messages/server-change-result.ts+++ /dev/null@@ -1,11 +0,0 @@-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/messages/server-change.ts b/desktop/src/operations/messages/server-change.tsdeleted file mode 100644index ad08e66e..00000000--- a/desktop/src/operations/messages/server-change.ts+++ /dev/null@@ -1,12 +0,0 @@-import { ServerChange } from '@/types/sync';--export type ServerChangeMessageInput = {- type: 'server_change';- change: ServerChange;-};--declare module '@/operations/messages' {- interface MessageMap {- server_change: ServerChangeMessageInput;- }-}diff --git a/desktop/src/operations/messages/server-nodes-sync.ts b/desktop/src/operations/messages/server-nodes-sync.tsnew file mode 100644index 00000000..1adb3080--- /dev/null+++ b/desktop/src/operations/messages/server-nodes-sync.ts@@ -0,0 +1,12 @@+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-batch-sync.ts b/desktop/src/operations/mutations/node-batch-sync.tsdeleted file mode 100644index 9501bbdb..00000000--- a/desktop/src/operations/mutations/node-batch-sync.ts+++ /dev/null@@ -1,21 +0,0 @@-import { ServerNodeBatchSyncData } from '@/types/sync';--export type NodeBatchSyncMutationInput = {- type: 'node_batch_sync';- accountId: string;- workspaceId: string;- nodes: ServerNodeBatchSyncData[];-};--export type NodeBatchSyncMutationOutput = {- success: boolean;-};--declare module '@/operations/mutations' {- interface MutationMap {- node_batch_sync: {- input: NodeBatchSyncMutationInput;- output: NodeBatchSyncMutationOutput;- };- }-}diff --git a/desktop/src/operations/mutations/node-collaborator-server-create.ts b/desktop/src/operations/mutations/node-collaborator-server-create.tsdeleted file mode 100644index df802dda..00000000--- a/desktop/src/operations/mutations/node-collaborator-server-create.ts+++ /dev/null@@ -1,25 +0,0 @@-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.tsdeleted file mode 100644index 4065c596..00000000--- a/desktop/src/operations/mutations/node-collaborator-server-delete.ts+++ /dev/null@@ -1,20 +0,0 @@-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.tsdeleted file mode 100644index c0a535b8..00000000--- a/desktop/src/operations/mutations/node-collaborator-server-update.ts+++ /dev/null@@ -1,25 +0,0 @@-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-reaction-server-create.ts b/desktop/src/operations/mutations/node-reaction-server-create.tsdeleted file mode 100644index 3e8a8614..00000000--- a/desktop/src/operations/mutations/node-reaction-server-create.ts+++ /dev/null@@ -1,23 +0,0 @@-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.tsdeleted file mode 100644index 20b41683..00000000--- a/desktop/src/operations/mutations/node-reaction-server-delete.ts+++ /dev/null@@ -1,21 +0,0 @@-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-sync.ts b/desktop/src/operations/mutations/node-sync.tsnew file mode 100644index 00000000..dfb67fb5--- /dev/null+++ b/desktop/src/operations/mutations/node-sync.ts@@ -0,0 +1,20 @@+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/renderer/components/messages/message-reactions.tsx b/desktop/src/renderer/components/messages/message-reactions.tsxindex da5516f5..1fbd9a18 100644--- a/desktop/src/renderer/components/messages/message-reactions.tsx+++ b/desktop/src/renderer/components/messages/message-reactions.tsx@@ -19,8 +19,11 @@ export const MessageReactions = ({return (<div className="my-1 flex flex-row gap-2">{message.reactionCounts.map((reaction) => {- const hasReacted = message.userReactions.includes(reaction.reaction);+ if (reaction.count === 0) {+ return null;+ }+ const hasReacted = reaction.isReactedTo;return (<divkey={reaction.reaction}diff --git a/desktop/src/renderer/components/messages/message.tsx b/desktop/src/renderer/components/messages/message.tsxindex d2d9240a..4d5db711 100644--- a/desktop/src/renderer/components/messages/message.tsx+++ b/desktop/src/renderer/components/messages/message.tsx@@ -137,7 +137,7 @@ export const Message = ({ message, previousMessage }: MessageProps) => {/>))}</div>- {message.reactionCounts.length > 0 && (+ {message.reactionCounts?.length > 0 && (<MessageReactionsmessage={message}onReactionClick={(reaction) => {@@ -145,7 +145,13 @@ export const Message = ({ message, previousMessage }: MessageProps) => {return;}- if (message.userReactions.includes(reaction)) {+ if (+ message.reactionCounts.some(+ (reactionCount) =>+ reactionCount.reaction === reaction &&+ reactionCount.isReactedTo,+ )+ ) {mutate({input: {type: 'node_reaction_delete',diff --git a/desktop/src/types/messages.ts b/desktop/src/types/messages.tsindex 38e73664..13876601 100644--- a/desktop/src/types/messages.ts+++ b/desktop/src/types/messages.ts@@ -7,10 +7,10 @@ export type MessageNode = {createdAt: string;author: UserNode;reactionCounts: MessageReactionCount[];- userReactions: string[];};export type MessageReactionCount = {reaction: string;count: number;+ isReactedTo: boolean;};diff --git a/desktop/src/types/nodes.ts b/desktop/src/types/nodes.tsindex bebc579b..9fa78ab8 100644--- a/desktop/src/types/nodes.ts+++ b/desktop/src/types/nodes.ts@@ -64,29 +64,6 @@ export type ServerNodeAttributes = {[key: string]: any;};-export type ServerNodeCollaborator = {- nodeId: string;- collaboratorId: string;- role: string;- workspaceId: string;- createdAt: string;- createdBy: string;- updatedAt?: string | null;- updatedBy?: string | null;- versionId: string;- serverCreatedAt: string;- serverUpdatedAt?: string | null;-};--export type ServerNodeReaction = {- nodeId: string;- actorId: string;- reaction: string;- workspaceId: string;- createdAt: string;- serverCreatedAt: string;-};-export type NodeInsertInput = {id: string;attributes: LocalNodeAttributes;diff --git a/desktop/src/types/sync.ts b/desktop/src/types/sync.tsindex fe8180dd..773e0148 100644--- a/desktop/src/types/sync.ts+++ b/desktop/src/types/sync.ts@@ -1,9 +1,3 @@-import {- ServerNode,- ServerNodeCollaborator,- ServerNodeReaction,-} from '@/types/nodes';-export type ServerSyncResponse = {results: ServerSyncChangeResult[];};@@ -15,113 +9,18 @@ export type ServerSyncChangeResult = {export type ServerSyncChangeStatus = 'success' | 'error';-export type WorkspaceSyncData = {- nodes: ServerNode[];- nodeReactions: ServerNodeReaction[];- nodeCollaborators: ServerNodeCollaborator[];-};--export type ServerChange = {+export type ServerNodeSyncData = {id: string;workspaceId: string;- deviceId: string;- data: ServerChangeData;- createdAt: string;-};--export type ServerChangeData =- | ServerNodeCreateChangeData- | ServerNodeUpdateChangeData- | ServerNodeDeleteChangeData- | ServerNodeCollaboratorCreateChangeData- | ServerNodeCollaboratorUpdateChangeData- | ServerNodeCollaboratorDeleteChangeData- | ServerNodeReactionCreateChangeData- | ServerNodeReactionDeleteChangeData- | ServerNodeBatchSyncChangeData;--export type ServerNodeCreateChangeData = {- type: 'node_create';- id: string;- state: string;- createdAt: string;- createdBy: string;- versionId: string;- serverCreatedAt: string;-};--export type ServerNodeUpdateChangeData = {- type: 'node_update';- id: string;- update: string;- updatedAt: string;- updatedBy: string;- versionId: string;- serverUpdatedAt: string;-};--export type ServerNodeDeleteChangeData = {- type: 'node_delete';- id: string;-};--export type ServerNodeCollaboratorCreateChangeData = {- type: 'node_collaborator_create';- nodeId: string;- collaboratorId: string;- role: string;- createdAt: string;- createdBy: string;- versionId: string;- serverCreatedAt: string;-};--export type ServerNodeCollaboratorUpdateChangeData = {- type: 'node_collaborator_update';- nodeId: string;- collaboratorId: string;- role: string;- updatedAt: string;- updatedBy: string;- versionId: string;- serverUpdatedAt: string;-};--export type ServerNodeCollaboratorDeleteChangeData = {- type: 'node_collaborator_delete';- nodeId: string;- collaboratorId: string;-};--export type ServerNodeReactionCreateChangeData = {- type: 'node_reaction_create';- nodeId: string;- actorId: string;- reaction: string;- createdAt: string;- serverCreatedAt: string;-};--export type ServerNodeReactionDeleteChangeData = {- type: 'node_reaction_delete';- nodeId: string;- actorId: string;- reaction: string;-};--export type ServerNodeBatchSyncChangeData = {- type: 'node_batch_sync';- nodes: ServerNodeBatchSyncData[];-};--export type ServerNodeBatchSyncData = {- id: string;state: string;createdAt: string;- createdBy: string;- versionId: 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;};diff --git a/desktop/src/types/workspaces.ts b/desktop/src/types/workspaces.tsindex a3e1229c..a9d273fd 100644--- a/desktop/src/types/workspaces.ts+++ b/desktop/src/types/workspaces.ts@@ -1,4 +1,4 @@-import { ServerNode, ServerNodeReaction } from '@/types/nodes';+import { ServerNode } from '@/types/nodes';export enum WorkspaceRole {Owner = 'owner',diff --git a/server/src/consumers/changes-subcriber.ts b/server/src/consumers/changes-subcriber.tsindex 1fd7f019..1dacc29a 100644--- a/server/src/consumers/changes-subcriber.ts+++ b/server/src/consumers/changes-subcriber.ts@@ -1,54 +1,117 @@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 {- ServerChange,- ServerChangeBroadcastMessage,- ServerChangeData,-} from '@/types/sync';+import { ServerNodeChangeEvent, ServerNodeSyncData } from '@/types/sync';export const initChangesSubscriber = async () => {const subscriber = redis.duplicate();await subscriber.connect();- await subscriber.subscribe(CHANNEL_NAMES.CHANGES, handleMessage);+ await subscriber.subscribe(CHANNEL_NAMES.CHANGES, handleEvent);};-const handleMessage = async (message: string) => {- const data: ServerChangeBroadcastMessage = JSON.parse(message);- if (!data.deviceIds) {- return;- }+const handleEvent = async (event: string) => {+ const data: ServerNodeChangeEvent = JSON.parse(event);- if (!data.changeId) {- return;+ let hasWorkspaceConnection = false;+ for (const connection of socketManager.getConnections()) {+ const workspaces = connection.getWorkspaces();+ if (workspaces.has(data.workspaceId)) {+ hasWorkspaceConnection = true;+ break;+ }}- const connections = socketManager.getConnections(data.deviceIds);- if (connections.length === 0) {+ if (!hasWorkspaceConnection) {return;}- const change = await database- .selectFrom('changes')+ const node = await database+ .selectFrom('nodes')+ .where('id', '=', data.nodeId).selectAll()- .where('id', '=', data.changeId).executeTakeFirst();- if (!change) {+ if (!node) {return;}- const serverChange: ServerChange = {- id: change.id,- workspaceId: change.workspace_id,- data: change.data as ServerChangeData,- createdAt: change.created_at.toISOString(),+ 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(),};- connections.forEach((connection) => {- connection.send({- type: 'server_change',- change: serverChange,- });- });+ for (const connection of socketManager.getConnections()) {+ const workspaces = connection.getWorkspaces();+ if (!workspaces.has(data.workspaceId)) {+ continue;+ }++ const workspaceUser = workspaces.get(data.workspaceId);+ if (!workspaceUser) {+ 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 (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.tsindex 9cc6feb6..65a77b02 100644--- a/server/src/data/migrations.ts+++ b/server/src/data/migrations.ts@@ -95,9 +95,12 @@ 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')+ .addColumn('server_created_at', 'timestamptz', (col) => col.notNull()).addColumn('server_updated_at', 'timestamptz')+ .addColumn('server_deleted_at', 'timestamptz').execute();},down: async (db) => {@@ -105,7 +108,7 @@ const createNodesTable: Migration = {},};-const createClosureTable: Migration = {+const createNodePathsTable: Migration = {up: async (db) => {await db.schema.createTable('node_paths')@@ -163,8 +166,7 @@ const createClosureTable: Migration = {$$ LANGUAGE plpgsql;CREATE TRIGGER trg_update_node_path- AFTER UPDATE OF parent_id ON nodes- FOR EACH ROW+ AFTER UPDATE ON nodesEXECUTE FUNCTION fn_update_node_path();`.execute(db);},@@ -176,7 +178,6 @@ const createClosureTable: Migration = {DROP FUNCTION IF EXISTS fn_update_node_path();`.execute(db);- // Drop closure tableawait db.schema.dropTable('node_paths').execute();},};@@ -192,55 +193,75 @@ const createNodeCollaboratorsTable: Migration = {col.notNull().references('nodes.id').onDelete('cascade'),).addColumn('role', 'varchar(30)', (col) => col.notNull())- .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull()).addColumn('created_at', 'timestamptz', (col) => col.notNull())- .addColumn('updated_at', 'timestamptz')- .addColumn('created_by', 'varchar(30)', (col) => col.notNull())- .addColumn('updated_by', 'varchar(30)')- .addColumn('version_id', 'varchar(30)', (col) => col.notNull())- .addColumn('server_created_at', 'timestamptz')- .addColumn('server_updated_at', 'timestamptz').addPrimaryKeyConstraint('node_collaborators_pkey', ['node_id','collaborator_id',]).execute();- },- down: async (db) => {- await db.schema.dropTable('node_collaborators').execute();- },-};-const createNodeReactionsTable: Migration = {- up: async (db) => {- await db.schema- .createTable('node_reactions')- .addColumn('node_id', 'varchar(30)', (col) =>- col.notNull().references('nodes.id').onDelete('cascade'),- )- .addColumn('actor_id', 'varchar(30)', (col) =>- col.notNull().references('nodes.id').onDelete('cascade'),- )- .addColumn('reaction', 'varchar(30)', (col) => col.notNull())- .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())- .addColumn('created_at', 'timestamptz', (col) => col.notNull())- .addColumn('server_created_at', 'timestamptz', (col) => col.notNull())- .addPrimaryKeyConstraint('node_reactions_pkey', [- 'node_id',- 'actor_id',- 'reaction',- ])- .execute();+ await sql`+ CREATE OR REPLACE FUNCTION fn_insert_node_collaborators() RETURNS TRIGGER AS $$+ DECLARE+ collaborator_key text;+ collaborator_role text;+ BEGIN+ FOR collaborator_key, collaborator_role IN+ SELECT * FROM jsonb_each_text(NEW.attributes->'collaborators')+ LOOP+ INSERT INTO node_collaborators (node_id, collaborator_id, role, created_at)+ VALUES (NEW.id, collaborator_key, collaborator_role, NOW());+ END LOOP;++ RETURN NEW;+ END;+ $$ LANGUAGE plpgsql;++ CREATE TRIGGER trg_insert_node_collaborators+ AFTER INSERT ON nodes+ FOR EACH ROW+ EXECUTE FUNCTION fn_insert_node_collaborators();++ CREATE OR REPLACE FUNCTION fn_update_node_collaborators() RETURNS TRIGGER AS $$+ DECLARE+ collaborator_key text;+ collaborator_role text;+ BEGIN+ DELETE FROM node_collaborators WHERE node_id = NEW.id;++ FOR collaborator_key, collaborator_role IN+ SELECT * FROM jsonb_each_text(NEW.attributes->'collaborators')+ LOOP+ INSERT INTO node_collaborators (node_id, collaborator_id, role, created_at)+ VALUES (NEW.id, collaborator_key, collaborator_role, NOW());+ END LOOP;++ RETURN NEW;+ END;+ $$ LANGUAGE plpgsql;++ CREATE TRIGGER trg_update_node_collaborators+ AFTER UPDATE ON nodes+ FOR EACH ROW+ EXECUTE FUNCTION fn_update_node_collaborators();+ `.execute(db);},down: async (db) => {- await db.schema.dropTable('node_reactions').execute();+ await sql`+ DROP TRIGGER IF EXISTS trg_insert_node_collaborators ON nodes;+ DROP TRIGGER IF EXISTS trg_update_node_collaborators ON nodes;+ DROP FUNCTION IF EXISTS fn_insert_node_collaborators();+ DROP FUNCTION IF EXISTS fn_update_node_collaborators();+ `.execute(db);++ await db.schema.dropTable('node_collaborators').execute();},};-const createAccountDevicesTable: Migration = {+const createDevicesTable: Migration = {up: async (db) => {await db.schema- .createTable('account_devices')+ .createTable('devices').addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey()).addColumn('account_id', 'varchar(30)', (col) => col.notNull()).addColumn('token_hash', 'varchar(100)', (col) => col.notNull())@@ -259,43 +280,28 @@ const createAccountDevicesTable: Migration = {.execute();},down: async (db) => {- await db.schema.dropTable('account_devices').execute();+ await db.schema.dropTable('devices').execute();},};-const createChangesTable: Migration = {+const createDeviceNodeVersionTable: Migration = {up: async (db) => {await db.schema- .createTable('changes')- .addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey())- .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())- .addColumn('data', 'jsonb')- .addColumn('created_at', 'timestamptz', (col) => col.notNull())- .addColumn('notified_at', 'timestamptz')- .execute();- },- down: async (db) => {- await db.schema.dropTable('changes').execute();- },-};--const createChangeDevicesTable: Migration = {- up: async (db) => {- await db.schema- .createTable('change_devices')- .addColumn('change_id', 'varchar(30)', (col) =>- col.notNull().references('changes.id').onDelete('cascade'),+ .createTable('device_node_versions')+ .addColumn('device_id', 'varchar(30)', (col) =>+ col.notNull().references('devices.id').onDelete('cascade'),)- .addColumn('device_id', 'varchar(30)', (col) => col.notNull())- .addColumn('retry_count', 'integer', (col) => col.notNull().defaultTo(0))- .addPrimaryKeyConstraint('change_devices_pkey', [- 'change_id',+ .addColumn('node_id', 'varchar(30)', (col) => col.notNull())+ .addColumn('version_id', 'varchar(30)', (col) => col.notNull())+ .addColumn('synced_at', 'timestamptz', (col) => col.notNull())+ .addPrimaryKeyConstraint('device_node_versions_pkey', ['device_id',+ 'node_id',]).execute();},down: async (db) => {- await db.schema.dropTable('change_devices').execute();+ await db.schema.dropTable('device_node_versions').execute();},};@@ -304,10 +310,8 @@ export const databaseMigrations: Record<string, Migration> = {'00002_create_workspaces_table': createWorkspacesTable,'00003_create_workspace_users_table': createWorkspaceUsersTable,'00004_create_nodes_table': createNodesTable,- '00005_create_closure_table': createClosureTable,+ '00005_create_node_paths_table': createNodePathsTable,'00006_create_node_collaborators_table': createNodeCollaboratorsTable,- '00007_create_node_reactions_table': createNodeReactionsTable,- '00008_create_account_devices_table': createAccountDevicesTable,- '00009_create_changes_table': createChangesTable,- '00010_create_change_devices_table': createChangeDevicesTable,+ '00007_create_devices_table': createDevicesTable,+ '00008_create_device_node_version_table': createDeviceNodeVersionTable,};diff --git a/server/src/data/schema.ts b/server/src/data/schema.tsindex 8c483139..df104398 100644--- a/server/src/data/schema.ts+++ b/server/src/data/schema.ts@@ -59,28 +59,6 @@ export type SelectWorkspaceUser = Selectable<WorkspaceUserTable>;export type CreateWorkspaceUser = Insertable<WorkspaceUserTable>;export type UpdateWorkspaceUser = Updateable<WorkspaceUserTable>;-interface AccountDeviceTable {- id: ColumnType<string, string, never>;- account_id: ColumnType<string, string, never>;- token_hash: ColumnType<string, string, string>;- token_salt: ColumnType<string, string, string>;- token_generated_at: ColumnType<Date, Date, Date>;- previous_token_hash: ColumnType<string | null, string | null, string | null>;- previous_token_salt: ColumnType<string | null, string | null, string | null>;- type: ColumnType<number, number, number>;- version: ColumnType<string, string, string>;- platform: ColumnType<string | null, string | null, string | null>;- cpu: ColumnType<string | null, string | null, string | null>;- hostname: ColumnType<string | null, string | null, string | null>;- created_at: ColumnType<Date, Date, never>;- last_online_at: ColumnType<Date | null, Date | null, Date>;- last_active_at: ColumnType<Date | null, Date | null, Date>;-}--export type SelectAccountDevice = Selectable<AccountDeviceTable>;-export type CreateAccountDevice = Insertable<AccountDeviceTable>;-export type UpdateAccountDevice = Updateable<AccountDeviceTable>;-interface NodeTable {id: ColumnType<string, string, never>;workspace_id: ColumnType<string, string, never>;@@ -97,9 +75,12 @@ 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>;@@ -121,64 +102,53 @@ interface NodeCollaboratorTable {node_id: ColumnType<string, string, never>;collaborator_id: ColumnType<string, string, never>;role: ColumnType<string, string, string>;- workspace_id: ColumnType<string, string, never>;created_at: ColumnType<Date, Date, never>;- updated_at: ColumnType<Date | null, Date | null, Date>;- created_by: ColumnType<string, string, never>;- updated_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>;}export type SelectNodeCollaborator = Selectable<NodeCollaboratorTable>;export type CreateNodeCollaborator = Insertable<NodeCollaboratorTable>;export type UpdateNodeCollaborator = Updateable<NodeCollaboratorTable>;-interface NodeReactionTable {- node_id: ColumnType<string, string, never>;- actor_id: ColumnType<string, string, never>;- reaction: ColumnType<string, string, never>;- workspace_id: ColumnType<string, string, never>;- created_at: ColumnType<Date, Date, never>;- server_created_at: ColumnType<Date, Date, never>;-}--export type SelectNodeReaction = Selectable<NodeReactionTable>;-export type CreateNodeReaction = Insertable<NodeReactionTable>;-export type UpdateNodeReaction = Updateable<NodeReactionTable>;--interface ChangeTable {+interface DeviceTable {id: ColumnType<string, string, never>;- workspace_id: ColumnType<string, string, never>;- data: JSONColumnType<any, string | null, string | null>;+ account_id: ColumnType<string, string, never>;+ token_hash: ColumnType<string, string, string>;+ token_salt: ColumnType<string, string, string>;+ token_generated_at: ColumnType<Date, Date, Date>;+ previous_token_hash: ColumnType<string | null, string | null, string | null>;+ previous_token_salt: ColumnType<string | null, string | null, string | null>;+ type: ColumnType<number, number, number>;+ version: ColumnType<string, string, string>;+ platform: ColumnType<string | null, string | null, string | null>;+ cpu: ColumnType<string | null, string | null, string | null>;+ hostname: ColumnType<string | null, string | null, string | null>;created_at: ColumnType<Date, Date, never>;- notified_at: ColumnType<Date | null, Date | null, Date>;+ last_online_at: ColumnType<Date | null, Date | null, Date>;+ last_active_at: ColumnType<Date | null, Date | null, Date>;}-export type SelectChange = Selectable<ChangeTable>;-export type CreateChange = Insertable<ChangeTable>;-export type UpdateChange = Updateable<ChangeTable>;+export type SelectDevice = Selectable<DeviceTable>;+export type CreateDevice = Insertable<DeviceTable>;+export type UpdateDevice = Updateable<DeviceTable>;-interface ChangeDeviceTable {- change_id: ColumnType<string, string, never>;+interface DeviceNodeVersionTable {device_id: ColumnType<string, string, never>;- retry_count: ColumnType<number, number, number>;+ node_id: ColumnType<string, string, never>;+ version_id: ColumnType<string, string, string>;+ synced_at: ColumnType<Date, Date, Date>;}-export type SelectChangeDevice = Selectable<ChangeDeviceTable>;-export type CreateChangeDevice = Insertable<ChangeDeviceTable>;-export type UpdateChangeDevice = Updateable<ChangeDeviceTable>;+export type SelectDeviceNodeVersion = Selectable<DeviceNodeVersionTable>;+export type CreateDeviceNodeVersion = Insertable<DeviceNodeVersionTable>;+export type UpdateDeviceNodeVersion = Updateable<DeviceNodeVersionTable>;export interface DatabaseSchema {accounts: AccountTable;workspaces: WorkspaceTable;workspace_users: WorkspaceUserTable;- account_devices: AccountDeviceTable;nodes: NodeTable;node_paths: NodePathTable;node_collaborators: NodeCollaboratorTable;- node_reactions: NodeReactionTable;- changes: ChangeTable;- change_devices: ChangeDeviceTable;+ devices: DeviceTable;+ device_node_versions: DeviceNodeVersionTable;}diff --git a/server/src/index.ts b/server/src/index.tsindex 76c58433..4f26ee4b 100644--- a/server/src/index.ts+++ b/server/src/index.ts@@ -2,8 +2,6 @@ import { initApi } from '@/api';import { initRedis } from '@/data/redis';import { initChangesSubscriber } from '@/consumers/changes-subcriber';import { migrate } from '@/data/database';-import { initChangeWorker } from '@/queues/changes';-import { initSyncWorker } from '@/queues/sync';migrate().then(() => {initApi();@@ -11,9 +9,6 @@ migrate().then(() => {initRedis().then(() => {console.log('Redis initialized');- initChangeWorker();- initSyncWorker();-initChangesSubscriber().then(() => {console.log('Change subscriber started');});diff --git a/server/src/lib/nodes.ts b/server/src/lib/nodes.tsindex 4b288459..22e6acc0 100644--- a/server/src/lib/nodes.ts+++ b/server/src/lib/nodes.ts@@ -21,16 +21,6 @@ export const mapNode = (node: SelectNode): ServerNode => {};};-type NodeCollaboratorRow = {- node_id: string;- node_level: number;- role: string;-};--type NodeIdRow = {- id: string;-};-export const fetchCollaboratorRole = async (nodeId: string,collaboratorId: string,diff --git a/server/src/lib/tokens.ts b/server/src/lib/tokens.tsindex 7380608b..1f3ea7f9 100644--- a/server/src/lib/tokens.ts+++ b/server/src/lib/tokens.ts@@ -37,21 +37,19 @@ export const verifyToken = async (const id = token.slice(0, 28);const secret = token.slice(28);- const accountDevice = await database- .selectFrom('account_devices')+ const device = await database+ .selectFrom('devices').selectAll().where('id', '=', id).executeTakeFirst();- if (!accountDevice) {+ if (!device) {return {authenticated: false,};}- if (- !verifySecret(secret, accountDevice.token_salt, accountDevice.token_hash)- ) {+ if (!verifySecret(secret, device.token_salt, device.token_hash)) {return {authenticated: false,};@@ -60,8 +58,8 @@ export const verifyToken = async (return {authenticated: true,account: {- id: accountDevice.account_id,- deviceId: accountDevice.id,+ id: device.account_id,+ deviceId: device.id,},};};diff --git a/server/src/messages/index.ts b/server/src/messages/index.tsindex 628aec93..7049c90b 100644--- a/server/src/messages/index.ts+++ b/server/src/messages/index.ts@@ -1,11 +1,8 @@-import { ServerChangeMessageInput } from '@/messages/server-change';-import { ServerChangeResultMessageInput } from '@/messages/server-change-result';-import { ServerChangeBatchMessageInput } from '@/messages/server-change-batch';-+import { LocalNodeSyncMessageInput } from '@/messages/local-node-sync';+import { ServerNodesSyncMessageInput } from '@/messages/server-nodes-sync';export interface MessageMap {- server_change: ServerChangeMessageInput;- server_change_result: ServerChangeResultMessageInput;- server_change_batch: ServerChangeBatchMessageInput;+ local_node_sync: LocalNodeSyncMessageInput;+ server_nodes_sync: ServerNodesSyncMessageInput;}export type MessageInput = MessageMap[keyof MessageMap];diff --git a/server/src/messages/local-node-sync.ts b/server/src/messages/local-node-sync.tsnew file mode 100644index 00000000..15652965--- /dev/null+++ b/server/src/messages/local-node-sync.ts@@ -0,0 +1,5 @@+export type LocalNodeSyncMessageInput = {+ type: 'local_node_sync';+ nodeId: string;+ versionId: string;+};diff --git a/server/src/messages/server-change-batch.ts b/server/src/messages/server-change-batch.tsdeleted file mode 100644index f8c93d9d..00000000--- a/server/src/messages/server-change-batch.ts+++ /dev/null@@ -1,6 +0,0 @@-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.tsdeleted file mode 100644index 6c542433..00000000--- a/server/src/messages/server-change-result.ts+++ /dev/null@@ -1,5 +0,0 @@-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.tsdeleted file mode 100644index 0056e82b..00000000--- a/server/src/messages/server-change.ts+++ /dev/null@@ -1,6 +0,0 @@-import { ServerChange } from '@/types/sync';--export type ServerChangeMessageInput = {- type: 'server_change';- change: ServerChange;-};diff --git a/server/src/messages/server-nodes-sync.ts b/server/src/messages/server-nodes-sync.tsnew file mode 100644index 00000000..0031494d--- /dev/null+++ b/server/src/messages/server-nodes-sync.ts@@ -0,0 +1,6 @@+import { ServerNodeSyncData } from '@/types/sync';++export type ServerNodesSyncMessageInput = {+ type: 'server_nodes_sync';+ nodes: ServerNodeSyncData[];+};diff --git a/server/src/queues/changes.ts b/server/src/queues/changes.tsindex bdccae92..234602f2 100644--- a/server/src/queues/changes.ts+++ b/server/src/queues/changes.ts@@ -1,171 +1,171 @@-import { database } from '@/data/database';-import { CHANNEL_NAMES, redis } from '@/data/redis';-import { getIdType, IdType } from '@/lib/id';-import { fetchNodeAscendants } from '@/lib/nodes';-import { ServerChangeData } from '@/types/sync';-import { Job, Queue, Worker } from 'bullmq';--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');-}--export const queue = new Queue('changes', {- connection: {- host: REDIS_HOST,- password: REDIS_PASSWORD,- port: parseInt(REDIS_PORT),- db: parseInt(REDIS_DB),- },- defaultJobOptions: {- removeOnComplete: true,- },-});--export const enqueueChange = async (id: string): Promise<void> => {- await queue.add('change', { id }, { jobId: id });-};--export const enqueueChanges = async (ids: string[]): Promise<void> => {- await queue.addBulk(ids.map((id) => ({ name: 'change', data: { id } })));-};--export const initChangeWorker = () => {- return new Worker('changes', handleChangeJob, {- connection: {- host: REDIS_HOST,- password: REDIS_PASSWORD,- port: parseInt(REDIS_PORT),- db: parseInt(REDIS_DB),- },- });-};--const handleChangeJob = async (job: Job) => {- try {- const changeId = job.data.id;- const change = await database- .selectFrom('changes')- .selectAll()- .where('id', '=', changeId)- .executeTakeFirst();-- if (!change) {- return;- }-- if (change.notified_at) {- console.log('change already notified');- return;- }-- const changeData = change.data as ServerChangeData;- const nodeId = getNodeId(changeData);- if (!nodeId) {- return;- }-- const deviceIds = await fetchDevicesForNode(change.workspace_id, nodeId);- if (deviceIds.length === 0) {- return;- }-- await database.transaction().execute(async (trx) => {- await trx- .insertInto('change_devices')- .values(- deviceIds.map((deviceId) => ({- change_id: changeId,- device_id: deviceId,- retry_count: 0,- })),- )- .execute();-- await trx- .updateTable('changes')- .set({ notified_at: new Date() })- .where('id', '=', changeId)- .execute();- });-- redis.publish(- CHANNEL_NAMES.CHANGES,- JSON.stringify({- changeId,- deviceIds,- }),- );- } catch (error) {- console.error('error', error);- }-};--const getNodeId = (changeData: ServerChangeData): string | null => {- switch (changeData.type) {- case 'node_create':- return changeData.id;- case 'node_update':- return changeData.id;- case 'node_delete':- return changeData.id;- case 'node_collaborator_create':- return changeData.nodeId;- case 'node_collaborator_update':- return changeData.nodeId;- case 'node_collaborator_delete':- return changeData.nodeId;- case 'node_reaction_create':- return changeData.nodeId;- case 'node_reaction_delete':- return changeData.nodeId;- default:- return null;- }-};--const fetchDevicesForNode = async (- workspaceId: string,- nodeId: string,-): Promise<string[]> => {- const idType = getIdType(nodeId);- if (idType === IdType.User) {- return fetchAllWorkspaceDevices(workspaceId);- }-- const nodeTree = await fetchNodeAscendants(nodeId);- return fetchDevicesForNodes(nodeTree);-};--const fetchAllWorkspaceDevices = async (- workspaceId: string,-): Promise<string[]> => {- const deviceIds = await database- .selectFrom('workspace_users as wu')- .innerJoin('account_devices as ad', 'wu.account_id', 'ad.account_id')- .select('ad.id')- .where('wu.workspace_id', '=', workspaceId)- .execute();-- return deviceIds- .map((row) => row.id)- .filter((id): id is string => id !== null);-};--const fetchDevicesForNodes = async (nodeIds: string[]): Promise<string[]> => {- const deviceIds = await database- .selectFrom('node_collaborators as nc')- .innerJoin('workspace_users as wu', 'nc.collaborator_id', 'wu.id')- .innerJoin('account_devices as ad', 'wu.account_id', 'ad.account_id')- .select('ad.id')- .where('nc.node_id', 'in', nodeIds)- .execute();-- return deviceIds- .map((row) => row.id)- .filter((id): id is string => id !== null);-};+// import { database } from '@/data/database';+// import { CHANNEL_NAMES, redis } from '@/data/redis';+// import { getIdType, IdType } from '@/lib/id';+// import { fetchNodeAscendants } from '@/lib/nodes';+// import { ServerChangeData } from '@/types/sync';+// import { Job, Queue, Worker } from 'bullmq';++// 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');+// }++// export const queue = new Queue('changes', {+// connection: {+// host: REDIS_HOST,+// password: REDIS_PASSWORD,+// port: parseInt(REDIS_PORT),+// db: parseInt(REDIS_DB),+// },+// defaultJobOptions: {+// removeOnComplete: true,+// },+// });++// export const enqueueChange = async (id: string): Promise<void> => {+// await queue.add('change', { id }, { jobId: id });+// };++// export const enqueueChanges = async (ids: string[]): Promise<void> => {+// await queue.addBulk(ids.map((id) => ({ name: 'change', data: { id } })));+// };++// export const initChangeWorker = () => {+// return new Worker('changes', handleChangeJob, {+// connection: {+// host: REDIS_HOST,+// password: REDIS_PASSWORD,+// port: parseInt(REDIS_PORT),+// db: parseInt(REDIS_DB),+// },+// });+// };++// const handleChangeJob = async (job: Job) => {+// try {+// const changeId = job.data.id;+// const change = await database+// .selectFrom('changes')+// .selectAll()+// .where('id', '=', changeId)+// .executeTakeFirst();++// if (!change) {+// return;+// }++// if (change.notified_at) {+// console.log('change already notified');+// return;+// }++// const changeData = change.data as ServerChangeData;+// const nodeId = getNodeId(changeData);+// if (!nodeId) {+// return;+// }++// const deviceIds = await fetchDevicesForNode(change.workspace_id, nodeId);+// if (deviceIds.length === 0) {+// return;+// }++// await database.transaction().execute(async (trx) => {+// await trx+// .insertInto('change_devices')+// .values(+// deviceIds.map((deviceId) => ({+// change_id: changeId,+// device_id: deviceId,+// retry_count: 0,+// })),+// )+// .execute();++// await trx+// .updateTable('changes')+// .set({ notified_at: new Date() })+// .where('id', '=', changeId)+// .execute();+// });++// redis.publish(+// CHANNEL_NAMES.CHANGES,+// JSON.stringify({+// changeId,+// deviceIds,+// }),+// );+// } catch (error) {+// console.error('error', error);+// }+// };++// const getNodeId = (changeData: ServerChangeData): string | null => {+// switch (changeData.type) {+// case 'node_create':+// return changeData.id;+// case 'node_update':+// return changeData.id;+// case 'node_delete':+// return changeData.id;+// case 'node_collaborator_create':+// return changeData.nodeId;+// case 'node_collaborator_update':+// return changeData.nodeId;+// case 'node_collaborator_delete':+// return changeData.nodeId;+// case 'node_reaction_create':+// return changeData.nodeId;+// case 'node_reaction_delete':+// return changeData.nodeId;+// default:+// return null;+// }+// };++// const fetchDevicesForNode = async (+// workspaceId: string,+// nodeId: string,+// ): Promise<string[]> => {+// const idType = getIdType(nodeId);+// if (idType === IdType.User) {+// return fetchAllWorkspaceDevices(workspaceId);+// }++// const nodeTree = await fetchNodeAscendants(nodeId);+// return fetchDevicesForNodes(nodeTree);+// };++// const fetchAllWorkspaceDevices = async (+// workspaceId: string,+// ): Promise<string[]> => {+// const deviceIds = await database+// .selectFrom('workspace_users as wu')+// .innerJoin('account_devices as ad', 'wu.account_id', 'ad.account_id')+// .select('ad.id')+// .where('wu.workspace_id', '=', workspaceId)+// .execute();++// return deviceIds+// .map((row) => row.id)+// .filter((id): id is string => id !== null);+// };++// const fetchDevicesForNodes = async (nodeIds: string[]): Promise<string[]> => {+// const deviceIds = await database+// .selectFrom('node_collaborators as nc')+// .innerJoin('workspace_users as wu', 'nc.collaborator_id', 'wu.id')+// .innerJoin('account_devices as ad', 'wu.account_id', 'ad.account_id')+// .select('ad.id')+// .where('nc.node_id', 'in', nodeIds)+// .execute();++// return deviceIds+// .map((row) => row.id)+// .filter((id): id is string => id !== null);+// };diff --git a/server/src/queues/sync.ts b/server/src/queues/sync.tsindex 87b01c91..ef027b26 100644--- a/server/src/queues/sync.ts+++ b/server/src/queues/sync.ts@@ -1,407 +1,407 @@-import { database } from '@/data/database';-import { CHANNEL_NAMES, redis } from '@/data/redis';-import { generateId, IdType } from '@/lib/id';-import { ServerChange, ServerNodeBatchSyncData } from '@/types/sync';-import { Job, Queue, Worker } from 'bullmq';-import { NodeTypes } from '@/lib/constants';--const SYNC_BATCH_SIZE = 100;--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');-}--export const queue = new Queue('sync', {- connection: {- host: REDIS_HOST,- password: REDIS_PASSWORD,- port: parseInt(REDIS_PORT),- db: parseInt(REDIS_DB),- },- defaultJobOptions: {- removeOnComplete: true,- },-});--export const enqueueCollaboratorSync = async (- nodeId: string,- collaboratorId: string,-): Promise<void> => {- await queue.add('collaborator-sync', { nodeId, collaboratorId });-};--export const enqueueWorkspaceUserSync = async (- workspaceId: string,- userId: string,-): Promise<void> => {- await queue.add('workspace-user-sync', { workspaceId, userId });-};--export const enqueueAccountDeviceSync = async (- accountId: string,- deviceId: string,-): Promise<void> => {- await queue.add('account-device-sync', { accountId, deviceId });-};--export const initSyncWorker = () => {- return new Worker('sync', handleSyncJob, {- connection: {- host: REDIS_HOST,- password: REDIS_PASSWORD,- port: parseInt(REDIS_PORT),- db: parseInt(REDIS_DB),- },- });-};--const handleSyncJob = async (job: Job) => {- switch (job.name) {- case 'collaborator-sync':- return handleCollaboratorSync(job.data);- case 'workspace-user-sync':- return handleWorkspaceUserSync(job.data);- case 'account-device-sync':- return handleAccountDeviceSync(job.data);- }-};--const handleCollaboratorSync = async (data: {- nodeId: string;- collaboratorId: string;-}) => {- const { nodeId, collaboratorId } = data;- const collaboratorNode = await database- .selectFrom('nodes')- .where('id', '=', nodeId)- .selectAll()- .executeTakeFirst();-- if (!collaboratorNode) {- return;- }-- const workspaceUser = await database- .selectFrom('workspace_users')- .where('id', '=', collaboratorId)- .selectAll()- .executeTakeFirst();-- if (!workspaceUser) {- return;- }-- const accountDevices = await database- .selectFrom('account_devices')- .where('account_id', '=', workspaceUser.account_id)- .selectAll()- .execute();-- const deviceIds = accountDevices.map((device) => device.id);- if (deviceIds.length === 0) {- return;- }-- await processDescendantsSync(- collaboratorNode.workspace_id,- [collaboratorNode.id],- deviceIds,- );-};--const handleWorkspaceUserSync = async (data: {- workspaceId: string;- userId: string;-}) => {- const { workspaceId, userId } = data;-- const userNode = await database- .selectFrom('nodes')- .where('id', '=', userId)- .selectAll()- .executeTakeFirst();-- if (!userNode) {- return;- }-- const workspaceUser = await database- .selectFrom('workspace_users')- .where('id', '=', userId)- .selectAll()- .executeTakeFirst();-- if (!workspaceUser) {- return;- }-- const accountDevices = await database- .selectFrom('account_devices')- .where('account_id', '=', workspaceUser.account_id)- .selectAll()- .execute();-- const deviceIds = accountDevices.map((device) => device.id);- if (deviceIds.length === 0) {- return;- }-- const nodesWithAccess = await database- .selectFrom('node_collaborators')- .where('collaborator_id', '=', userNode.id)- .select('node_id')- .execute();-- const nodeIds = nodesWithAccess.map((node) => node.node_id);- if (nodeIds.length === 0) {- return;- }-- await processUsersSync(workspaceId, deviceIds);- await processDescendantsSync(workspaceId, nodeIds, deviceIds);-};--const handleAccountDeviceSync = async (data: {- accountId: string;- deviceId: string;-}) => {- const { accountId, deviceId } = data;-- const workspaceUsers = await database- .selectFrom('workspace_users')- .where('account_id', '=', accountId)- .selectAll()- .execute();-- for (const workspaceUser of workspaceUsers) {- await processUsersSync(workspaceUser.workspace_id, [deviceId]);-- const nodesWithAccess = await database- .selectFrom('node_collaborators')- .where('collaborator_id', '=', workspaceUser.id)- .select('node_id')- .execute();-- const nodeIds = nodesWithAccess.map((node) => node.node_id);- if (nodeIds.length === 0) {- continue;- }-- await processDescendantsSync(workspaceUser.workspace_id, nodeIds, [- deviceId,- ]);- }-};--const processUsersSync = async (workspaceId: string, deviceIds: string[]) => {- let lastProcessedId = '';- let hasMoreUsers = true;-- while (hasMoreUsers) {- const users = await database- .selectFrom('nodes')- .select([- 'id',- 'state',- 'created_at',- 'created_by',- 'version_id',- 'updated_at',- 'updated_by',- 'server_created_at',- 'server_updated_at',- ])- .where((eb) =>- eb.and([- eb('workspace_id', '=', workspaceId),- eb('type', '=', NodeTypes.User),- eb('id', '>', lastProcessedId),- ]),- )- .orderBy('id', 'asc')- .limit(SYNC_BATCH_SIZE)- .execute();-- if (users.length === 0) {- hasMoreUsers = false;- break;- }-- lastProcessedId = users[users.length - 1].id;-- const nodeSyncData: ServerNodeBatchSyncData[] = [];- for (const user of users) {- nodeSyncData.push({- id: user.id,- state: user.state,- createdAt: user.created_at.toISOString(),- createdBy: user.created_by,- versionId: user.version_id,- updatedAt: user.updated_at?.toISOString(),- updatedBy: user.updated_by,- serverCreatedAt: user.server_created_at.toISOString(),- serverUpdatedAt: user.server_updated_at?.toISOString(),- });- }-- if (nodeSyncData.length === 0) {- continue;- }-- const change: ServerChange = {- id: generateId(IdType.Change),- workspaceId,- data: {- type: 'node_batch_sync',- nodes: nodeSyncData,- },- createdAt: new Date().toISOString(),- };-- await database.transaction().execute(async (tx) => {- await tx- .insertInto('changes')- .values({- id: change.id,- workspace_id: change.workspaceId,- created_at: new Date(change.createdAt),- data: JSON.stringify(change.data),- })- .execute();-- await tx- .insertInto('change_devices')- .values(- deviceIds.map((deviceId) => ({- change_id: change.id,- device_id: deviceId,- retry_count: 0,- })),- )- .execute();- });-- await redis.publish(- CHANNEL_NAMES.CHANGES,- JSON.stringify({- changeId: change.id,- deviceIds,- }),- );-- if (users.length < SYNC_BATCH_SIZE) {- hasMoreUsers = false;- }- }-};--const processDescendantsSync = async (- workspaceId: string,- nodeIds: string[],- deviceIds: string[],-) => {- let lastProcessedId = '';- let hasMoreDescendants = true;-- while (hasMoreDescendants) {- const descendantNodes = await database- .selectFrom('node_paths as np')- .innerJoin('nodes as n', 'np.descendant_id', 'n.id')- .select([- 'n.id',- 'n.state',- 'n.created_at',- 'n.created_by',- 'n.version_id',- 'n.updated_at',- 'n.updated_by',- 'n.server_created_at',- 'n.server_updated_at',- ])- .where((eb) =>- eb.and([- eb('np.ancestor_id', 'in', nodeIds),- eb('np.descendant_id', '>', lastProcessedId),- ]),- )- .orderBy('np.descendant_id', 'asc')- .limit(SYNC_BATCH_SIZE)- .execute();-- if (descendantNodes.length === 0) {- hasMoreDescendants = false;- break;- }-- const nodeSyncData: ServerNodeBatchSyncData[] = [];- for (const node of descendantNodes) {- nodeSyncData.push({- id: node.id,- state: node.state,- createdAt: node.created_at.toISOString(),- createdBy: node.created_by,- versionId: node.version_id,- updatedAt: node.updated_at?.toISOString(),- updatedBy: node.updated_by,- serverCreatedAt: node.server_created_at.toISOString(),- serverUpdatedAt: node.server_updated_at?.toISOString(),- });- }-- lastProcessedId = descendantNodes[descendantNodes.length - 1].id;-- if (nodeSyncData.length === 0) {- continue;- }-- const change: ServerChange = {- id: generateId(IdType.Change),- workspaceId,- data: {- type: 'node_batch_sync',- nodes: nodeSyncData,- },- createdAt: new Date().toISOString(),- };-- await database.transaction().execute(async (tx) => {- await tx- .insertInto('changes')- .values({- id: change.id,- workspace_id: change.workspaceId,- created_at: new Date(change.createdAt),- data: JSON.stringify(change.data),- })- .execute();-- await tx- .insertInto('change_devices')- .values(- deviceIds.map((deviceId) => ({- change_id: change.id,- device_id: deviceId,- retry_count: 0,- })),- )- .execute();- });-- await redis.publish(- CHANNEL_NAMES.CHANGES,- JSON.stringify({- changeId: change.id,- deviceIds,- }),- );-- if (descendantNodes.length < SYNC_BATCH_SIZE) {- hasMoreDescendants = false;- }- }-};+// import { database } from '@/data/database';+// import { CHANNEL_NAMES, redis } from '@/data/redis';+// import { generateId, IdType } from '@/lib/id';+// import { ServerChange, ServerNodeBatchSyncData } from '@/types/sync';+// import { Job, Queue, Worker } from 'bullmq';+// import { NodeTypes } from '@/lib/constants';++// const SYNC_BATCH_SIZE = 100;++// 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');+// }++// export const queue = new Queue('sync', {+// connection: {+// host: REDIS_HOST,+// password: REDIS_PASSWORD,+// port: parseInt(REDIS_PORT),+// db: parseInt(REDIS_DB),+// },+// defaultJobOptions: {+// removeOnComplete: true,+// },+// });++// export const enqueueCollaboratorSync = async (+// nodeId: string,+// collaboratorId: string,+// ): Promise<void> => {+// await queue.add('collaborator-sync', { nodeId, collaboratorId });+// };++// export const enqueueWorkspaceUserSync = async (+// workspaceId: string,+// userId: string,+// ): Promise<void> => {+// await queue.add('workspace-user-sync', { workspaceId, userId });+// };++// export const enqueueAccountDeviceSync = async (+// accountId: string,+// deviceId: string,+// ): Promise<void> => {+// await queue.add('account-device-sync', { accountId, deviceId });+// };++// export const initSyncWorker = () => {+// return new Worker('sync', handleSyncJob, {+// connection: {+// host: REDIS_HOST,+// password: REDIS_PASSWORD,+// port: parseInt(REDIS_PORT),+// db: parseInt(REDIS_DB),+// },+// });+// };++// const handleSyncJob = async (job: Job) => {+// switch (job.name) {+// case 'collaborator-sync':+// return handleCollaboratorSync(job.data);+// case 'workspace-user-sync':+// return handleWorkspaceUserSync(job.data);+// case 'account-device-sync':+// return handleAccountDeviceSync(job.data);+// }+// };++// const handleCollaboratorSync = async (data: {+// nodeId: string;+// collaboratorId: string;+// }) => {+// const { nodeId, collaboratorId } = data;+// const collaboratorNode = await database+// .selectFrom('nodes')+// .where('id', '=', nodeId)+// .selectAll()+// .executeTakeFirst();++// if (!collaboratorNode) {+// return;+// }++// const workspaceUser = await database+// .selectFrom('workspace_users')+// .where('id', '=', collaboratorId)+// .selectAll()+// .executeTakeFirst();++// if (!workspaceUser) {+// return;+// }++// const devices = await database+// .selectFrom('devices')+// .where('account_id', '=', workspaceUser.account_id)+// .selectAll()+// .execute();++// const deviceIds = devices.map((device) => device.id);+// if (deviceIds.length === 0) {+// return;+// }++// await processDescendantsSync(+// collaboratorNode.workspace_id,+// [collaboratorNode.id],+// deviceIds,+// );+// };++// const handleWorkspaceUserSync = async (data: {+// workspaceId: string;+// userId: string;+// }) => {+// const { workspaceId, userId } = data;++// const userNode = await database+// .selectFrom('nodes')+// .where('id', '=', userId)+// .selectAll()+// .executeTakeFirst();++// if (!userNode) {+// return;+// }++// const workspaceUser = await database+// .selectFrom('workspace_users')+// .where('id', '=', userId)+// .selectAll()+// .executeTakeFirst();++// if (!workspaceUser) {+// return;+// }++// const devices = await database+// .selectFrom('devices')+// .where('account_id', '=', workspaceUser.account_id)+// .selectAll()+// .execute();++// const deviceIds = devices.map((device) => device.id);+// if (deviceIds.length === 0) {+// return;+// }++// const nodesWithAccess = await database+// .selectFrom('node_collaborators')+// .where('collaborator_id', '=', userNode.id)+// .select('node_id')+// .execute();++// const nodeIds = nodesWithAccess.map((node) => node.node_id);+// if (nodeIds.length === 0) {+// return;+// }++// await processUsersSync(workspaceId, deviceIds);+// await processDescendantsSync(workspaceId, nodeIds, deviceIds);+// };++// const handleAccountDeviceSync = async (data: {+// accountId: string;+// deviceId: string;+// }) => {+// const { accountId, deviceId } = data;++// const workspaceUsers = await database+// .selectFrom('workspace_users')+// .where('account_id', '=', accountId)+// .selectAll()+// .execute();++// for (const workspaceUser of workspaceUsers) {+// await processUsersSync(workspaceUser.workspace_id, [deviceId]);++// const nodesWithAccess = await database+// .selectFrom('node_collaborators')+// .where('collaborator_id', '=', workspaceUser.id)+// .select('node_id')+// .execute();++// const nodeIds = nodesWithAccess.map((node) => node.node_id);+// if (nodeIds.length === 0) {+// continue;+// }++// await processDescendantsSync(workspaceUser.workspace_id, nodeIds, [+// deviceId,+// ]);+// }+// };++// const processUsersSync = async (workspaceId: string, deviceIds: string[]) => {+// let lastProcessedId = '';+// let hasMoreUsers = true;++// while (hasMoreUsers) {+// const users = await database+// .selectFrom('nodes')+// .select([+// 'id',+// 'state',+// 'created_at',+// 'created_by',+// 'version_id',+// 'updated_at',+// 'updated_by',+// 'server_created_at',+// 'server_updated_at',+// ])+// .where((eb) =>+// eb.and([+// eb('workspace_id', '=', workspaceId),+// eb('type', '=', NodeTypes.User),+// eb('id', '>', lastProcessedId),+// ]),+// )+// .orderBy('id', 'asc')+// .limit(SYNC_BATCH_SIZE)+// .execute();++// if (users.length === 0) {+// hasMoreUsers = false;+// break;+// }++// lastProcessedId = users[users.length - 1].id;++// const nodeSyncData: ServerNodeBatchSyncData[] = [];+// for (const user of users) {+// nodeSyncData.push({+// id: user.id,+// state: user.state,+// createdAt: user.created_at.toISOString(),+// createdBy: user.created_by,+// versionId: user.version_id,+// updatedAt: user.updated_at?.toISOString(),+// updatedBy: user.updated_by,+// serverCreatedAt: user.server_created_at.toISOString(),+// serverUpdatedAt: user.server_updated_at?.toISOString(),+// });+// }++// if (nodeSyncData.length === 0) {+// continue;+// }++// const change: ServerChange = {+// id: generateId(IdType.Change),+// workspaceId,+// data: {+// type: 'node_batch_sync',+// nodes: nodeSyncData,+// },+// createdAt: new Date().toISOString(),+// };++// await database.transaction().execute(async (tx) => {+// await tx+// .insertInto('changes')+// .values({+// id: change.id,+// workspace_id: change.workspaceId,+// created_at: new Date(change.createdAt),+// data: JSON.stringify(change.data),+// })+// .execute();++// await tx+// .insertInto('change_devices')+// .values(+// deviceIds.map((deviceId) => ({+// change_id: change.id,+// device_id: deviceId,+// retry_count: 0,+// })),+// )+// .execute();+// });++// await redis.publish(+// CHANNEL_NAMES.CHANGES,+// JSON.stringify({+// changeId: change.id,+// deviceIds,+// }),+// );++// if (users.length < SYNC_BATCH_SIZE) {+// hasMoreUsers = false;+// }+// }+// };++// const processDescendantsSync = async (+// workspaceId: string,+// nodeIds: string[],+// deviceIds: string[],+// ) => {+// let lastProcessedId = '';+// let hasMoreDescendants = true;++// while (hasMoreDescendants) {+// const descendantNodes = await database+// .selectFrom('node_paths as np')+// .innerJoin('nodes as n', 'np.descendant_id', 'n.id')+// .select([+// 'n.id',+// 'n.state',+// 'n.created_at',+// 'n.created_by',+// 'n.version_id',+// 'n.updated_at',+// 'n.updated_by',+// 'n.server_created_at',+// 'n.server_updated_at',+// ])+// .where((eb) =>+// eb.and([+// eb('np.ancestor_id', 'in', nodeIds),+// eb('np.descendant_id', '>', lastProcessedId),+// ]),+// )+// .orderBy('np.descendant_id', 'asc')+// .limit(SYNC_BATCH_SIZE)+// .execute();++// if (descendantNodes.length === 0) {+// hasMoreDescendants = false;+// break;+// }++// const nodeSyncData: ServerNodeBatchSyncData[] = [];+// for (const node of descendantNodes) {+// nodeSyncData.push({+// id: node.id,+// state: node.state,+// createdAt: node.created_at.toISOString(),+// createdBy: node.created_by,+// versionId: node.version_id,+// updatedAt: node.updated_at?.toISOString(),+// updatedBy: node.updated_by,+// serverCreatedAt: node.server_created_at.toISOString(),+// serverUpdatedAt: node.server_updated_at?.toISOString(),+// });+// }++// lastProcessedId = descendantNodes[descendantNodes.length - 1].id;++// if (nodeSyncData.length === 0) {+// continue;+// }++// const change: ServerChange = {+// id: generateId(IdType.Change),+// workspaceId,+// data: {+// type: 'node_batch_sync',+// nodes: nodeSyncData,+// },+// createdAt: new Date().toISOString(),+// };++// await database.transaction().execute(async (tx) => {+// await tx+// .insertInto('changes')+// .values({+// id: change.id,+// workspace_id: change.workspaceId,+// created_at: new Date(change.createdAt),+// data: JSON.stringify(change.data),+// })+// .execute();++// await tx+// .insertInto('change_devices')+// .values(+// deviceIds.map((deviceId) => ({+// change_id: change.id,+// device_id: deviceId,+// retry_count: 0,+// })),+// )+// .execute();+// });++// await redis.publish(+// CHANNEL_NAMES.CHANGES,+// JSON.stringify({+// changeId: change.id,+// deviceIds,+// }),+// );++// if (descendantNodes.length < SYNC_BATCH_SIZE) {+// hasMoreDescendants = false;+// }+// }+// };diff --git a/server/src/routes/accounts.ts b/server/src/routes/accounts.tsindex 3de1d600..d0042596 100644--- a/server/src/routes/accounts.ts+++ b/server/src/routes/accounts.ts@@ -16,7 +16,6 @@ import { WorkspaceOutput, WorkspaceRole } from '@/types/workspaces';import { authMiddleware } from '@/middlewares/auth';import { generateToken } from '@/lib/tokens';import { mapNode } from '@/lib/nodes';-import { enqueueAccountDeviceSync } from '@/queues/sync';const GoogleUserInfoUrl = 'https://www.googleapis.com/oauth2/v1/userinfo';const SaltRounds = 10;@@ -215,7 +214,7 @@ accountsRouter.delete(}await database- .deleteFrom('account_devices')+ .deleteFrom('devices').where('id', '=', req.account.deviceId).execute();@@ -283,8 +282,8 @@ const buildLoginOutput = async (const deviceId = generateId(IdType.Device);const { token, salt, hash } = generateToken(deviceId);- const accountDevice = await database- .insertInto('account_devices')+ const device = await database+ .insertInto('devices').values({id: deviceId,account_id: id,@@ -298,19 +297,17 @@ const buildLoginOutput = async (.returningAll().executeTakeFirst();- if (!accountDevice) {- throw new Error('Failed to create account device.');+ if (!device) {+ throw new Error('Failed to create device.');}- await enqueueAccountDeviceSync(id, deviceId);-return {account: {token,id,name,email,- deviceId: accountDevice.id,+ deviceId: device.id,},workspaces: workspaceOutputs,};diff --git a/server/src/routes/sync.ts b/server/src/routes/sync.tsindex b40607ea..d6c67621 100644--- a/server/src/routes/sync.ts+++ b/server/src/routes/sync.ts@@ -1,90 +1,22 @@import { ApiError, NeuronRequest, NeuronResponse } from '@/types/api';import { database } from '@/data/database';import { Router } from 'express';-import {- ServerNode,- ServerNodeCollaborator,- ServerNodeReaction,-} from '@/types/nodes';-import { compareString } from '@/lib/utils';-import { mapNode } from '@/lib/nodes';import {LocalChange,+ LocalNodeChangeData,ServerSyncChangeResult,SyncLocalChangeResult,SyncLocalChangesInput,} from '@/types/sync';import { SelectWorkspaceUser } from '@/data/schema';-import { handleNodeChange } from '@/sync/nodes';-import { handleNodeCollaboratorChange } from '@/sync/node-collaborators';-import { handleNodeReactionChange } from '@/sync/node-reactions';+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';export const syncRouter = Router();-syncRouter.get(- '/:workspaceId',- async (req: NeuronRequest, res: NeuronResponse) => {- const workspaceId = req.params.workspaceId as string;- const nodeRows = await database- .selectFrom('nodes')- .selectAll()- .where('workspace_id', '=', workspaceId)- .execute();-- const nodeReactionRows = await database- .selectFrom('node_reactions')- .selectAll()- .where('workspace_id', '=', workspaceId)- .execute();-- const nodeCollaboratorRows = await database- .selectFrom('node_collaborators')- .selectAll()- .where('workspace_id', '=', workspaceId)- .execute();-- const nodes: ServerNode[] = nodeRows- .map((node) => mapNode(node))- .sort((a, b) => compareString(a.id, b.id));-- const nodeReactions: ServerNodeReaction[] = nodeReactionRows.map(- (nodeReaction) => {- return {- nodeId: nodeReaction.node_id,- actorId: nodeReaction.actor_id,- reaction: nodeReaction.reaction,- workspaceId: nodeReaction.workspace_id,- createdAt: nodeReaction.created_at,- serverCreatedAt: nodeReaction.server_created_at,- };- },- );-- const nodeCollaborators: ServerNodeCollaborator[] =- nodeCollaboratorRows.map((nodeCollaborator) => {- return {- nodeId: nodeCollaborator.node_id,- collaboratorId: nodeCollaborator.collaborator_id,- role: nodeCollaborator.role,- workspaceId: nodeCollaborator.workspace_id,- createdAt: nodeCollaborator.created_at,- createdBy: nodeCollaborator.created_by,- updatedAt: nodeCollaborator.updated_at,- updatedBy: nodeCollaborator.updated_by,- versionId: nodeCollaborator.version_id,- serverCreatedAt: nodeCollaborator.server_created_at,- serverUpdatedAt: nodeCollaborator.server_created_at,- };- });-- res.status(200).json({- nodes,- nodeReactions,- nodeCollaborators,- });- },-);-syncRouter.post('/:workspaceId',async (req: NeuronRequest, res: NeuronResponse) => {@@ -133,6 +65,7 @@ syncRouter.post(status: result.status,});} catch (error) {+ console.error('Error handling local change', error);results.push({id: change.id,status: 'error',@@ -147,21 +80,214 @@ syncRouter.post(const handleLocalChange = async (workspaceUser: SelectWorkspaceUser,- mutation: LocalChange,+ change: LocalChange,): Promise<SyncLocalChangeResult> => {- switch (mutation.table) {- case 'nodes': {- return handleNodeChange(workspaceUser, mutation);+ switch (change.action) {+ case 'insert': {+ return handleCreateNodeChange(workspaceUser, change);+ }+ case 'update': {+ return handleUpdateNodeChange(workspaceUser, change);+ }+ case 'delete': {+ return handleDeleteNodeChange(workspaceUser, change);}- case 'node_collaborators': {- return handleNodeCollaboratorChange(workspaceUser, mutation);+ default: {+ return {+ status: 'error',+ };}- case 'node_reactions': {- return handleNodeReactionChange(workspaceUser, mutation);+ }+};++const handleCreateNodeChange = async (+ workspaceUser: SelectWorkspaceUser,+ change: LocalChange,+): Promise<SyncLocalChangeResult> => {+ if (!change.after) {+ return {+ status: 'error',+ };+ }++ const nodeData = JSON.parse(change.after) as LocalNodeChangeData;+ const existingNode = await database+ .selectFrom('nodes')+ .where('id', '=', nodeData.id)+ .executeTakeFirst();++ if (existingNode) {+ return {+ status: 'success',+ };+ }++ const attributes: ServerNodeAttributes = JSON.parse(nodeData.attributes);+ if (attributes.parentId) {+ const parentRole = await fetchCollaboratorRole(+ attributes.parentId,+ workspaceUser.id,+ );++ if (+ parentRole === null ||+ (parentRole !== 'owner' && parentRole !== 'admin')+ ) {+ return {+ status: 'error',+ };}}+ await database+ .insertInto('nodes')+ .values({+ id: nodeData.id,+ attributes: nodeData.attributes,+ workspace_id: workspaceUser.workspace_id,+ state: nodeData.state,+ created_at: new Date(nodeData.created_at),+ created_by: nodeData.created_by,+ version_id: nodeData.version_id,+ server_created_at: new Date(),+ })+ .execute();++ await publishChange(nodeData.id, workspaceUser.workspace_id);+return {- status: 'error',+ status: 'success',};};++const handleUpdateNodeChange = async (+ workspaceUser: SelectWorkspaceUser,+ change: LocalChange,+): Promise<SyncLocalChangeResult> => {+ if (!change.after) {+ return {+ status: 'error',+ };+ }++ const nodeData = JSON.parse(change.after) as LocalNodeChangeData;+ const existingNode = await database+ .selectFrom('nodes')+ .select(['id', 'workspace_id', 'state'])+ .where('id', '=', nodeData.id)+ .executeTakeFirst();++ if (+ !existingNode ||+ existingNode.workspace_id != workspaceUser.workspace_id+ ) {+ return {+ status: 'error',+ };+ }++ const role = await fetchCollaboratorRole(nodeData.id, workspaceUser.id);+ if (role === null) {+ return {+ status: 'error',+ };+ }++ const updatedAt = nodeData.updated_at+ ? new Date(nodeData.updated_at)+ : new Date();+ const updatedBy = nodeData.updated_by ?? workspaceUser.id;++ const doc = new Y.Doc({+ guid: nodeData.id,+ });++ Y.applyUpdate(doc, toUint8Array(existingNode.state));+ Y.applyUpdate(doc, toUint8Array(nodeData.state));++ const attributesMap = doc.getMap('attributes');+ const attributes = JSON.stringify(attributesMap.toJSON());+ const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));++ await database+ .updateTable('nodes')+ .set({+ attributes: attributes,+ state: encodedState,+ updated_at: updatedAt,+ updated_by: updatedBy,+ version_id: nodeData.version_id,+ server_updated_at: new Date(),+ })+ .where('id', '=', nodeData.id)+ .execute();++ await publishChange(nodeData.id, workspaceUser.workspace_id);++ return {+ status: 'success',+ };+};++const handleDeleteNodeChange = async (+ workspaceUser: SelectWorkspaceUser,+ change: LocalChange,+): Promise<SyncLocalChangeResult> => {+ if (!change.before) {+ return {+ status: 'error',+ };+ }++ const nodeData = JSON.parse(change.before) as LocalNodeChangeData;+ const existingNode = await database+ .selectFrom('nodes')+ .where('id', '=', nodeData.id)+ .select(['id', 'workspace_id'])+ .executeTakeFirst();++ if (!existingNode) {+ return {+ status: 'success',+ };+ }++ if (existingNode.workspace_id !== workspaceUser.workspace_id) {+ return {+ status: 'error',+ };+ }++ const role = await fetchCollaboratorRole(nodeData.id, workspaceUser.id);+ if (role === null) {+ return {+ status: 'error',+ };+ }++ await database+ .updateTable('nodes')+ .set({+ deleted_at: new Date(),+ deleted_by: workspaceUser.id,+ version_id: nodeData.version_id,+ server_deleted_at: new Date(),+ })+ .where('id', '=', nodeData.id)+ .execute();++ await publishChange(nodeData.id, workspaceUser.workspace_id);++ 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.tsindex 43ad89a7..c85f7991 100644--- a/server/src/routes/workspaces.ts+++ b/server/src/routes/workspaces.ts@@ -15,7 +15,6 @@ import * as Y from 'yjs';import { fromUint8Array, toUint8Array } from 'js-base64';import {CreateAccount,- CreateChange,CreateNode,CreateWorkspaceUser,SelectNode,@@ -25,12 +24,6 @@ import { getNameFromEmail } from '@/lib/utils';import { AccountStatus } from '@/types/accounts';import { ServerNode } from '@/types/nodes';import { mapNode } from '@/lib/nodes';-import {- ServerNodeCreateChangeData,- ServerNodeUpdateChangeData,-} from '@/types/sync';-import { enqueueChange, enqueueChanges } from '@/queues/changes';-import { enqueueWorkspaceUserSync } from '@/queues/sync';export const workspacesRouter = Router();@@ -131,8 +124,6 @@ workspacesRouter.post('/', async (req: NeuronRequest, res: NeuronResponse) => {.execute();});- await enqueueWorkspaceUserSync(workspaceId, userId);-const output: WorkspaceOutput = {id: workspaceId,name: input.name,@@ -558,7 +549,6 @@ workspacesRouter.post(const accountsToCreate: CreateAccount[] = [];const workspaceUsersToCreate: CreateWorkspaceUser[] = [];const usersToCreate: CreateNode[] = [];- const changesToCreate: CreateChange[] = [];const users: ServerNode[] = [];for (const email of input.emails) {@@ -623,7 +613,6 @@ workspacesRouter.post(const userAttributes = JSON.stringify(userAttributesMap.toJSON());const userState = fromUint8Array(Y.encodeStateAsUpdate(userDoc));- const userChangeId = generateId(IdType.Change);workspaceUsersToCreate.push({id: userId,@@ -660,23 +649,6 @@ workspacesRouter.post(workspace_id: user.workspaceId,});- const changeData: ServerNodeCreateChangeData = {- type: 'node_create',- id: user.id,- state: user.state,- createdAt: user.createdAt.toISOString(),- createdBy: user.createdBy,- versionId: user.versionId,- serverCreatedAt: user.serverCreatedAt.toISOString(),- };-- changesToCreate.push({- id: userChangeId,- workspace_id: workspace.id,- data: JSON.stringify(changeData),- created_at: new Date(),- });-users.push(user);}@@ -695,28 +667,12 @@ workspacesRouter.post(.insertInto('workspace_users').values(workspaceUsersToCreate).execute();-- for (const workspaceUser of workspaceUsersToCreate) {- await enqueueWorkspaceUserSync(- workspaceUser.workspace_id,- workspaceUser.id,- );- }}if (usersToCreate.length > 0) {await trx.insertInto('nodes').values(usersToCreate).execute();}-- if (changesToCreate.length > 0) {- await trx.insertInto('changes').values(changesToCreate).execute();- }});-- if (changesToCreate.length > 0) {- const changeIds = changesToCreate.map((change) => change.id);- await enqueueChanges(changeIds);- }}return res.status(200).json({@@ -819,7 +775,6 @@ workspacesRouter.put(const userAttributes = JSON.stringify(userAttributesMap.toJSON());const encodedState = fromUint8Array(Y.encodeStateAsUpdate(userDoc));const updatedAt = new Date();- const userChangeId = generateId(IdType.Change);const userNode: ServerNode = {id: user.id,@@ -838,18 +793,8 @@ workspacesRouter.put(updatedBy: currentWorkspaceUser.id,};- const changeData: ServerNodeUpdateChangeData = {- type: 'node_update',- id: userNode.id,- updates: userUpdates,- updatedAt: updatedAt.toISOString(),- updatedBy: currentWorkspaceUser.id,- versionId: userNode.versionId,- serverUpdatedAt: updatedAt.toISOString(),- };-await database.transaction().execute(async (trx) => {- await database+ await trx.updateTable('workspace_users').set({role: input.role,@@ -860,7 +805,7 @@ workspacesRouter.put(.where('id', '=', userId).execute();- await database+ await trx.updateTable('nodes').set({attributes: userAttributes,@@ -872,20 +817,8 @@ workspacesRouter.put(}).where('id', '=', userNode.id).execute();-- await trx- .insertInto('changes')- .values({- id: userChangeId,- workspace_id: workspace.id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();});- await enqueueChange(userChangeId);-return res.status(200).json({user: userNode,});diff --git a/server/src/sockets/socket-connection.ts b/server/src/sockets/socket-connection.tsindex 5f9732ec..f9bd1040 100644--- a/server/src/sockets/socket-connection.ts+++ b/server/src/sockets/socket-connection.ts@@ -1,20 +1,26 @@import { database } from '@/data/database';+import { NodeTypes } from '@/lib/constants';import { MessageInput } from '@/messages';import { NeuronRequestAccount } from '@/types/api';-import { ServerChange } from '@/types/sync';+import { ServerNodeSyncData } from '@/types/sync';import { WebSocket } from 'ws';+interface WorkspaceUser {+ userId: string;+ nodeIds: string[];+}+export class SocketConnection {private readonly socket: WebSocket;- private readonly pendingChanges: Set<string> = new Set();-- public accountId: string;- public deviceId: string;+ 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());@@ -22,104 +28,130 @@ export class SocketConnection {}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();+ this.fetchWorkspaceUsers().then(() => {+ this.sendPendingChanges();+ });+ }++ public getWorkspaces(): Map<string, WorkspaceUser> {+ return this.workspaces;}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('change_devices')- .where((eb) =>- eb.and([- eb('change_id', '=', messageInput.changeId),- eb('device_id', '=', this.deviceId),- ]),- )- .execute();- } else {- await database- .updateTable('change_devices')- .set((eb) => ({ retry_count: eb('retry_count', '+', 1) }))- .where((eb) =>- eb.and([- eb('change_id', '=', messageInput.changeId),- eb('device_id', '=', this.deviceId),- ]),- )- .execute();- }-- this.pendingChanges.delete(messageInput.changeId);- if (this.pendingChanges.size === 0) {- this.sendPendingChanges();- }+ if (messageInput.type === 'local_node_sync') {+ await database+ .insertInto('device_node_versions')+ .values({+ device_id: this.deviceId,+ node_id: messageInput.nodeId,+ 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,+ }),+ )+ .execute();}}private async sendPendingChanges() {- const changes = await database- .selectFrom('changes as c')- .innerJoin('change_devices as cd', 'c.id', 'cd.change_id')- .select([- 'c.id',- 'c.workspace_id',- 'cd.device_id',- 'c.created_at',- 'c.data',- ])- .where('cd.device_id', '=', this.deviceId)- .orderBy('id', 'asc')+ 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')+ .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.or([+ eb('device_node_versions.node_id', 'is', null),+ eb(+ 'nodes.version_id',+ '!=',+ eb.ref('device_node_versions.version_id'),+ ),+ ]),+ ]),+ )+ .orderBy('nodes.id', 'asc').limit(100).execute();- if (changes.length === 0) {+ if (unsyncedNodes.length === 0) {return;}- const serverChanges: ServerChange[] = [];- for (const change of changes) {- if (- !change.id ||- !change.workspace_id ||- !change.created_at ||- !change.data- ) {- continue;- }-- serverChanges.push({- id: change.id,- workspaceId: change.workspace_id,- createdAt: change.created_at.toISOString(),- data: change.data,+ 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_change_batch',- changes: serverChanges,+ 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();++ 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,+ });+ }+ }}diff --git a/server/src/sockets/socket-manager.ts b/server/src/sockets/socket-manager.tsindex cced814b..73543ad0 100644--- a/server/src/sockets/socket-manager.ts+++ b/server/src/sockets/socket-manager.ts@@ -49,16 +49,8 @@ class SocketManager {});}- public getConnections(deviceIds: string[]): SocketConnection[] {- const result: SocketConnection[] = [];- for (const deviceId of deviceIds) {- const connection = this.sockets.get(deviceId);- if (connection) {- result.push(connection);- }- }-- return result;+ public getConnections(): IterableIterator<SocketConnection> {+ return this.sockets.values();}public send(deviceId: string, message: MessageInput) {diff --git a/server/src/sync/node-collaborators.ts b/server/src/sync/node-collaborators.tsdeleted file mode 100644index a238c5cb..00000000--- a/server/src/sync/node-collaborators.ts+++ /dev/null@@ -1,456 +0,0 @@-import {- database,- hasDeleteChanges,- hasInsertChanges,- hasUpdateChanges,-} from '@/data/database';-import { SelectWorkspaceUser } from '@/data/schema';-import { generateId, IdType } from '@/lib/id';-import { fetchCollaboratorRole } from '@/lib/nodes';-import { enqueueChange } from '@/queues/changes';-import { enqueueCollaboratorSync } from '@/queues/sync';-import {- SyncLocalChangeResult,- LocalChange,- LocalNodeCollaboratorChangeData,- ServerNodeCollaboratorCreateChangeData,- ServerNodeCollaboratorUpdateChangeData,- ServerNodeCollaboratorDeleteChangeData,-} from '@/types/sync';--export const handleNodeCollaboratorChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- switch (change.action) {- case 'insert': {- return handleCreateNodeCollaboratorChange(workspaceUser, change);- }- case 'update': {- return handleUpdateNodeCollaboratorChange(workspaceUser, change);- }- case 'delete': {- return handleDeleteNodeCollaboratorChange(workspaceUser, change);- }- }-};--const handleCreateNodeCollaboratorChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- if (!change.after) {- return {- status: 'error',- };- }-- const nodeCollaboratorData = JSON.parse(- change.after,- ) as LocalNodeCollaboratorChangeData;-- const canCreate = await canCreateNodeCollaborator(- workspaceUser,- nodeCollaboratorData,- );-- if (!canCreate) {- return {- status: 'error',- };- }-- const serverCreatedAt = new Date();- const changeId = generateId(IdType.Change);- const changeData: ServerNodeCollaboratorCreateChangeData = {- type: 'node_collaborator_create',- nodeId: nodeCollaboratorData.node_id,- collaboratorId: nodeCollaboratorData.collaborator_id,- role: nodeCollaboratorData.role,- createdAt: nodeCollaboratorData.created_at,- serverCreatedAt: serverCreatedAt.toISOString(),- createdBy: nodeCollaboratorData.created_by,- versionId: nodeCollaboratorData.version_id,- };-- await database.transaction().execute(async (trx) => {- const result = await trx- .insertInto('node_collaborators')- .values({- node_id: nodeCollaboratorData.node_id,- collaborator_id: nodeCollaboratorData.collaborator_id,- role: nodeCollaboratorData.role,- workspace_id: workspaceUser.workspace_id,- created_at: new Date(nodeCollaboratorData.created_at),- created_by: nodeCollaboratorData.created_by,- server_created_at: new Date(),- version_id: nodeCollaboratorData.version_id,- })- .onConflict((ob) => ob.doNothing())- .execute();-- if (!hasInsertChanges(result)) {- return;- }-- await trx- .insertInto('changes')- .values({- id: changeId,- workspace_id: workspaceUser.workspace_id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();- });-- await enqueueChange(changeId);- await enqueueCollaboratorSync(- nodeCollaboratorData.node_id,- nodeCollaboratorData.collaborator_id,- );-- return {- status: 'success',- };-};--const canCreateNodeCollaborator = async (- workspaceUser: SelectWorkspaceUser,- data: LocalNodeCollaboratorChangeData,-): Promise<boolean> => {- const node = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', data.node_id)- .executeTakeFirst();-- if (node === null || node === undefined) {- return false;- }-- // If the node is a root node and created by the current user- if (- node.parent_id === null &&- node.created_by === workspaceUser.id &&- data.collaborator_id === workspaceUser.id- ) {- return true;- }-- // Get the current user's role for the node or its ancestors- const currentUserRole = await fetchCollaboratorRole(- data.node_id,- workspaceUser.id,- );-- if (currentUserRole === null) {- return false; // User has no access to the node- }-- if (currentUserRole === 'owner') {- // Owners can add any role- return true;- }-- if (currentUserRole === 'admin') {- // Admins can add admins and collaborators, but not owners- if (data.role === 'owner') {- return false;- }-- return true;- }-- // Collaborators cannot add other collaborators- return false;-};--const handleUpdateNodeCollaboratorChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- if (!change.after) {- return {- status: 'error',- };- }-- const nodeCollaboratorData = JSON.parse(- change.after,- ) as LocalNodeCollaboratorChangeData;-- const existingNodeCollaborator = await database- .selectFrom('node_collaborators')- .selectAll()- .where((eb) =>- eb.and([- eb('node_id', '=', nodeCollaboratorData.node_id),- eb('collaborator_id', '=', nodeCollaboratorData.collaborator_id),- ]),- )- .executeTakeFirst();-- if (- !existingNodeCollaborator ||- existingNodeCollaborator.workspace_id != workspaceUser.workspace_id ||- existingNodeCollaborator.updated_at === null ||- existingNodeCollaborator.updated_by === null- ) {- return {- status: 'error',- };- }-- const canUpdate = await canUpdateNodeCollaborator(- workspaceUser,- nodeCollaboratorData,- );-- if (!canUpdate) {- return {- status: 'error',- };- }-- if (existingNodeCollaborator.role === nodeCollaboratorData.role) {- return {- status: 'success',- };- }-- const updatedAt = new Date(existingNodeCollaborator.updated_at);- if (existingNodeCollaborator.server_updated_at !== null) {- const serverUpdatedAt = new Date(- existingNodeCollaborator.server_updated_at,- );- if (serverUpdatedAt > updatedAt) {- return {- status: 'success',- };- }- }-- const serverUpdatedAt = new Date();- const changeId = generateId(IdType.Change);- const changeData: ServerNodeCollaboratorUpdateChangeData = {- type: 'node_collaborator_update',- nodeId: nodeCollaboratorData.node_id,- collaboratorId: nodeCollaboratorData.collaborator_id,- role: nodeCollaboratorData.role,- serverUpdatedAt: serverUpdatedAt.toISOString(),- versionId: nodeCollaboratorData.version_id,- updatedAt: updatedAt.toISOString(),- updatedBy:- nodeCollaboratorData.updated_by ?? existingNodeCollaborator.created_by,- };-- await database.transaction().execute(async (trx) => {- const result = await trx- .updateTable('node_collaborators')- .set({- role: nodeCollaboratorData.role,- updated_at: updatedAt,- updated_by:- nodeCollaboratorData.updated_by ??- existingNodeCollaborator.created_by,- version_id: nodeCollaboratorData.version_id,- server_updated_at: new Date(),- })- .where((eb) =>- eb.and([- eb('node_id', '=', nodeCollaboratorData.node_id),- eb('collaborator_id', '=', nodeCollaboratorData.collaborator_id),- ]),- )- .execute();-- if (!hasUpdateChanges(result)) {- return;- }-- await trx- .insertInto('changes')- .values({- id: changeId,- workspace_id: workspaceUser.workspace_id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();- });-- await enqueueChange(changeId);-- return {- status: 'success',- };-};--const canUpdateNodeCollaborator = async (- workspaceUser: SelectWorkspaceUser,- data: LocalNodeCollaboratorChangeData,-): Promise<boolean> => {- const node = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', data.node_id)- .executeTakeFirst();-- if (node === null || node === undefined) {- return false;- }-- // Get the current user's role for the node or its ancestors- const currentUserRole = await fetchCollaboratorRole(- data.node_id,- workspaceUser.id,- );-- if (currentUserRole === null) {- return false; // User has no access to the node- }-- if (currentUserRole === 'owner') {- // Owners can add any role- return true;- }-- if (currentUserRole === 'admin') {- // Admins can add admins and collaborators, but not owners- if (data.role === 'owner') {- return false;- }-- return true;- }-- // Collaborators cannot add other collaborators- return false;-};--const handleDeleteNodeCollaboratorChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- if (!change.before) {- return {- status: 'error',- };- }-- const nodeCollaboratorData = JSON.parse(- change.before,- ) as LocalNodeCollaboratorChangeData;-- const existingNodeCollaborator = await database- .selectFrom('node_collaborators')- .selectAll()- .where((eb) =>- eb.and([- eb('node_id', '=', nodeCollaboratorData.node_id),- eb('collaborator_id', '=', nodeCollaboratorData.collaborator_id),- ]),- )- .executeTakeFirst();-- if (- !existingNodeCollaborator ||- existingNodeCollaborator.workspace_id != workspaceUser.workspace_id- ) {- return {- status: 'error',- };- }-- const canDelete = await canDeleteNodeCollaborator(- workspaceUser,- nodeCollaboratorData,- );- if (!canDelete) {- return {- status: 'error',- };- }-- const changeId = generateId(IdType.Change);- const changeData: ServerNodeCollaboratorDeleteChangeData = {- type: 'node_collaborator_delete',- nodeId: nodeCollaboratorData.node_id,- collaboratorId: nodeCollaboratorData.collaborator_id,- };-- await database.transaction().execute(async (trx) => {- const result = await trx- .deleteFrom('node_collaborators')- .where((eb) =>- eb.and([- eb('node_id', '=', nodeCollaboratorData.node_id),- eb('collaborator_id', '=', nodeCollaboratorData.collaborator_id),- ]),- )- .execute();-- if (!hasDeleteChanges(result)) {- return;- }-- await trx- .insertInto('changes')- .values({- id: changeId,- workspace_id: workspaceUser.workspace_id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();- });-- await enqueueChange(changeId);-- return {- status: 'success',- };-};--const canDeleteNodeCollaborator = async (- workspaceUser: SelectWorkspaceUser,- data: LocalNodeCollaboratorChangeData,-): Promise<boolean> => {- const node = await database- .selectFrom('nodes')- .selectAll()- .where('id', '=', data.node_id)- .executeTakeFirst();-- if (node === null || node === undefined) {- return false;- }-- // Get the current user's role for the node or its ancestors- const currentUserRole = await fetchCollaboratorRole(- data.node_id,- workspaceUser.id,- );-- if (currentUserRole === null) {- return false; // User has no access to the node- }-- if (currentUserRole === 'owner') {- // Owners can add any role- return true;- }-- if (currentUserRole === 'admin') {- // Admins can add admins and collaborators, but not owners- if (data.role === 'owner') {- return false;- }-- return true;- }-- // Collaborators cannot add other collaborators- return false;-};diff --git a/server/src/sync/node-reactions.ts b/server/src/sync/node-reactions.tsdeleted file mode 100644index 17e5b314..00000000--- a/server/src/sync/node-reactions.ts+++ /dev/null@@ -1,170 +0,0 @@-import { database, hasInsertChanges, hasDeleteChanges } from '@/data/database';-import { SelectWorkspaceUser } from '@/data/schema';-import { generateId, IdType } from '@/lib/id';-import { fetchCollaboratorRole } from '@/lib/nodes';-import { enqueueChange } from '@/queues/changes';-import {- SyncLocalChangeResult,- LocalChange,- LocalNodeReactionChangeData,- ServerNodeReactionCreateChangeData,- ServerNodeReactionDeleteChangeData,-} from '@/types/sync';--export const handleNodeReactionChange = async (- workspaceUser: SelectWorkspaceUser,- mutation: LocalChange,-): Promise<SyncLocalChangeResult> => {- switch (mutation.action) {- case 'insert': {- return handleCreateNodeReactionChange(workspaceUser, mutation);- }- case 'delete': {- return handleDeleteNodeReactionChange(workspaceUser, mutation);- }- }-- return {- status: 'error',- };-};--const handleCreateNodeReactionChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- if (!change.after) {- return {- status: 'error',- };- }-- const nodeReactionData = JSON.parse(- change.after,- ) as LocalNodeReactionChangeData;-- if (nodeReactionData.actor_id !== workspaceUser.id) {- return {- status: 'error',- };- }-- const nodeRole = await fetchCollaboratorRole(- nodeReactionData.node_id,- workspaceUser.id,- );-- if (nodeRole === null) {- return {- status: 'error',- };- }-- const serverCreatedAt = new Date();- const changeId = generateId(IdType.Change);- const changeData: ServerNodeReactionCreateChangeData = {- type: 'node_reaction_create',- nodeId: nodeReactionData.node_id,- actorId: nodeReactionData.actor_id,- reaction: nodeReactionData.reaction,- createdAt: nodeReactionData.created_at,- serverCreatedAt: serverCreatedAt.toISOString(),- };-- await database.transaction().execute(async (trx) => {- const result = await database- .insertInto('node_reactions')- .values({- node_id: nodeReactionData.node_id,- actor_id: nodeReactionData.actor_id,- reaction: nodeReactionData.reaction,- created_at: new Date(nodeReactionData.created_at),- workspace_id: workspaceUser.workspace_id,- server_created_at: new Date(),- })- .onConflict((ob) => ob.doNothing())- .execute();-- if (!hasInsertChanges(result)) {- return;- }-- await trx- .insertInto('changes')- .values({- id: changeId,- workspace_id: workspaceUser.workspace_id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();- });-- await enqueueChange(changeId);-- return {- status: 'success',- };-};--const handleDeleteNodeReactionChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- if (!change.before) {- return {- status: 'error',- };- }-- const nodeReactionData = JSON.parse(- change.before,- ) as LocalNodeReactionChangeData;-- if (nodeReactionData.actor_id !== workspaceUser.id) {- return {- status: 'error',- };- }-- const changeId = generateId(IdType.Change);- const changeData: ServerNodeReactionDeleteChangeData = {- type: 'node_reaction_delete',- nodeId: nodeReactionData.node_id,- actorId: nodeReactionData.actor_id,- reaction: nodeReactionData.reaction,- };-- await database.transaction().execute(async (trx) => {- const result = await trx- .deleteFrom('node_reactions')- .where((eb) =>- eb.and([- eb('node_id', '=', nodeReactionData.node_id),- eb('actor_id', '=', nodeReactionData.actor_id),- eb('reaction', '=', nodeReactionData.reaction),- ]),- )- .execute();-- if (!hasDeleteChanges(result)) {- return;- }-- await trx- .insertInto('changes')- .values({- id: changeId,- workspace_id: workspaceUser.workspace_id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();- });-- await enqueueChange(changeId);-- return {- status: 'success',- };-};diff --git a/server/src/sync/nodes.ts b/server/src/sync/nodes.tsdeleted file mode 100644index 1a342870..00000000--- a/server/src/sync/nodes.ts+++ /dev/null@@ -1,299 +0,0 @@-import {- database,- hasInsertChanges,- hasUpdateChanges,- hasDeleteChanges,-} from '@/data/database';-import { SelectWorkspaceUser } from '@/data/schema';-import { fetchCollaboratorRole } from '@/lib/nodes';-import {- SyncLocalChangeResult,- LocalChange,- LocalNodeChangeData,- ServerNodeCreateChangeData,- ServerNodeUpdateChangeData,- ServerNodeDeleteChangeData,-} from '@/types/sync';-import { ServerNodeAttributes } from '@/types/nodes';-import { fromUint8Array, toUint8Array } from 'js-base64';-import * as Y from 'yjs';-import { generateId, IdType } from '@/lib/id';-import { enqueueChange } from '@/queues/changes';--export const handleNodeChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- switch (change.action) {- case 'insert': {- return handleCreateNodeChange(workspaceUser, change);- }- case 'update': {- return handleUpdateNodeChange(workspaceUser, change);- }- case 'delete': {- return handleDeleteNodeChange(workspaceUser, change);- }- }-};--const handleCreateNodeChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- if (!change.after) {- return {- status: 'error',- };- }-- const nodeData = JSON.parse(change.after) as LocalNodeChangeData;- const existingNode = await database- .selectFrom('nodes')- .where('id', '=', nodeData.id)- .executeTakeFirst();-- if (existingNode) {- return {- status: 'success',- };- }-- const attributes: ServerNodeAttributes = JSON.parse(nodeData.attributes);- if (attributes.parentId) {- const parentRole = await fetchCollaboratorRole(- attributes.parentId,- workspaceUser.id,- );-- if (- parentRole === null ||- (parentRole !== 'owner' && parentRole !== 'admin')- ) {- return {- status: 'error',- };- }- }-- const serverCreatedAt = new Date();- const changeId = generateId(IdType.Change);- const changeData: ServerNodeCreateChangeData = {- type: 'node_create',- id: nodeData.id,- state: nodeData.state,- createdAt: nodeData.created_at,- createdBy: nodeData.created_by,- serverCreatedAt: serverCreatedAt.toISOString(),- versionId: nodeData.version_id,- };-- await database.transaction().execute(async (trx) => {- const result = await trx- .insertInto('nodes')- .values({- id: nodeData.id,- attributes: nodeData.attributes,- workspace_id: workspaceUser.workspace_id,- state: nodeData.state,- created_at: new Date(nodeData.created_at),- created_by: nodeData.created_by,- version_id: nodeData.version_id,- server_created_at: serverCreatedAt,- })- .execute();-- if (!hasInsertChanges(result)) {- return;- }-- await trx- .insertInto('changes')- .values({- id: changeId,- workspace_id: workspaceUser.workspace_id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();- });-- await enqueueChange(changeId);-- return {- status: 'success',- };-};--const handleUpdateNodeChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- if (!change.after) {- return {- status: 'error',- };- }-- const nodeData = JSON.parse(change.after) as LocalNodeChangeData;- const existingNode = await database- .selectFrom('nodes')- .select(['id', 'workspace_id', 'state'])- .where('id', '=', nodeData.id)- .executeTakeFirst();-- if (- !existingNode ||- existingNode.workspace_id != workspaceUser.workspace_id- ) {- return {- status: 'error',- };- }-- const role = await fetchCollaboratorRole(nodeData.id, workspaceUser.id);- if (role === null) {- return {- status: 'error',- };- }-- const updatedAt = nodeData.updated_at- ? new Date(nodeData.updated_at)- : new Date();- const updatedBy = nodeData.updated_by ?? workspaceUser.id;- const serverUpdatedAt = new Date();-- const doc = new Y.Doc({- guid: nodeData.id,- });-- Y.applyUpdate(doc, toUint8Array(existingNode.state));-- const updates: string[] = [];- doc.on('update', (update) => {- updates.push(fromUint8Array(update));- });-- Y.applyUpdate(doc, toUint8Array(nodeData.state));-- const attributesMap = doc.getMap('attributes');- const attributes = JSON.stringify(attributesMap.toJSON());- const encodedState = fromUint8Array(Y.encodeStateAsUpdate(doc));-- const changeId = generateId(IdType.Change);- const changeData: ServerNodeUpdateChangeData = {- type: 'node_update',- id: nodeData.id,- updates: updates,- updatedAt: updatedAt.toISOString(),- updatedBy: updatedBy,- serverUpdatedAt: serverUpdatedAt.toISOString(),- versionId: nodeData.version_id,- };-- await database.transaction().execute(async (trx) => {- const result = await trx- .updateTable('nodes')- .set({- attributes: attributes,- state: encodedState,- updated_at: updatedAt,- updated_by: updatedBy,- version_id: nodeData.version_id,- server_updated_at: new Date(),- })- .where('id', '=', nodeData.id)- .execute();-- if (!hasUpdateChanges(result)) {- return;- }-- await trx- .insertInto('changes')- .values({- id: changeId,- workspace_id: workspaceUser.workspace_id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();- });-- await enqueueChange(changeId);-- return {- status: 'success',- };-};--const handleDeleteNodeChange = async (- workspaceUser: SelectWorkspaceUser,- change: LocalChange,-): Promise<SyncLocalChangeResult> => {- if (!change.before) {- return {- status: 'error',- };- }-- const nodeData = JSON.parse(change.before) as LocalNodeChangeData;- const existingNode = await database- .selectFrom('nodes')- .where('id', '=', nodeData.id)- .select(['id', 'workspace_id'])- .executeTakeFirst();-- if (!existingNode) {- return {- status: 'success',- };- }-- if (existingNode.workspace_id !== workspaceUser.workspace_id) {- return {- status: 'error',- };- }-- const role = await fetchCollaboratorRole(nodeData.id, workspaceUser.id);- if (role === null) {- return {- status: 'error',- };- }-- const changeId = generateId(IdType.Change);- const changeData: ServerNodeDeleteChangeData = {- type: 'node_delete',- id: nodeData.id,- };-- await database.transaction().execute(async (trx) => {- const result = await trx- .deleteFrom('nodes')- .where('id', '=', nodeData.id)- .execute();-- if (!hasDeleteChanges(result)) {- return;- }-- await trx- .insertInto('changes')- .values({- id: changeId,- workspace_id: workspaceUser.workspace_id,- data: JSON.stringify(changeData),- created_at: new Date(),- })- .execute();- });-- await enqueueChange(changeId);-- return {- status: 'success',- };-};diff --git a/server/src/types/sync.ts b/server/src/types/sync.tsindex 4afa8358..b916db4d 100644--- a/server/src/types/sync.ts+++ b/server/src/types/sync.ts@@ -36,132 +36,23 @@ export type LocalNodeChangeData = {server_version_id: string;};-export type LocalNodeCollaboratorChangeData = {- node_id: string;- collaborator_id: string;- role: string;- created_at: string;- updated_at?: string | null;- created_by: string;- updated_by?: string | null;- version_id: string;- server_created_at: string;- server_updated_at: string;- server_version_id: string;-};--export type LocalNodeReactionChangeData = {- node_id: string;- actor_id: string;- reaction: string;- created_at: string;-};--export type ServerChange = {+export type ServerNodeSyncData = {id: string;workspaceId: string;- data: ServerChangeData;- createdAt: string;-};--export type ServerChangeBroadcastMessage = {- changeId: string;- deviceIds: string[];-};--export type ServerChangeData =- | ServerNodeCreateChangeData- | ServerNodeUpdateChangeData- | ServerNodeDeleteChangeData- | ServerNodeCollaboratorCreateChangeData- | ServerNodeCollaboratorUpdateChangeData- | ServerNodeCollaboratorDeleteChangeData- | ServerNodeReactionCreateChangeData- | ServerNodeReactionDeleteChangeData- | ServerNodeBatchSyncChangeData;--export type ServerNodeCreateChangeData = {- type: 'node_create';- id: 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 ServerNodeUpdateChangeData = {- type: 'node_update';- id: string;- updates: string[];- updatedAt: string;- updatedBy: string;- versionId: string;- serverUpdatedAt: string;-};--export type ServerNodeDeleteChangeData = {- type: 'node_delete';- id: string;-};--export type ServerNodeCollaboratorCreateChangeData = {- type: 'node_collaborator_create';- nodeId: string;- collaboratorId: string;- role: string;- createdAt: string;- createdBy: string;- versionId: string;- serverCreatedAt: string;-};--export type ServerNodeCollaboratorUpdateChangeData = {- type: 'node_collaborator_update';- nodeId: string;- collaboratorId: string;- role: string;- updatedAt: string;- updatedBy: string;- versionId: string;- serverUpdatedAt: string;-};--export type ServerNodeCollaboratorDeleteChangeData = {- type: 'node_collaborator_delete';- nodeId: string;- collaboratorId: string;-};--export type ServerNodeReactionCreateChangeData = {- type: 'node_reaction_create';- nodeId: string;- actorId: string;- reaction: string;- createdAt: string;- serverCreatedAt: string;-};--export type ServerNodeReactionDeleteChangeData = {- type: 'node_reaction_delete';+export type ServerNodeChangeEvent = {+ workspaceId: string;nodeId: string;- actorId: string;- reaction: string;-};--export type ServerNodeBatchSyncChangeData = {- type: 'node_batch_sync';- nodes: ServerNodeBatchSyncData[];-};--export type ServerNodeBatchSyncData = {- id: string;- state: string;- createdAt: string;- createdBy: string;- versionId: string;- updatedAt?: string | null;- updatedBy?: string | null;- serverCreatedAt: string;- serverUpdatedAt?: string | null;};