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
Refactor and improve synchronizers
Hakan Shehu51c832ebe115
diff --git a/apps/desktop/src/main/consumers/base.ts b/apps/desktop/src/main/consumers/base.tsdeleted file mode 100644index c22530cd..00000000--- a/apps/desktop/src/main/consumers/base.ts+++ /dev/null@@ -1,64 +0,0 @@-import { Kysely } from 'kysely';-import { Message } from '@colanode/core';--import { socketService } from '@/main/services/socket-service';-import { WorkspaceDatabaseSchema } from '@/main/data/workspace/schema';--export type ConsumerStatus = 'idle' | 'fetching' | 'syncing';--export abstract class BaseConsumer {- protected status: ConsumerStatus = 'idle';- protected userId: string;- protected accountId: string;- protected workspaceDatabase: Kysely<WorkspaceDatabaseSchema>;-- private cursorValue: bigint = 0n;-- constructor(- userId: string,- accountId: string,- workspaceDatabase: Kysely<WorkspaceDatabaseSchema>- ) {- this.userId = userId;- this.accountId = accountId;- this.workspaceDatabase = workspaceDatabase;- }-- protected abstract get cursorKey(): string;-- protected get cursor() {- return this.cursorValue;- }-- protected async initCursor() {- const cursor = await this.workspaceDatabase- .selectFrom('cursors')- .select('value')- .where('key', '=', this.cursorKey)- .executeTakeFirst();-- this.cursorValue = cursor?.value ?? 0n;- }-- protected async setCursor(cursor: bigint) {- this.cursorValue = cursor;- await this.workspaceDatabase- .insertInto('cursors')- .values({- key: this.cursorKey,- value: cursor,- created_at: new Date().toISOString(),- })- .onConflict((eb) =>- eb.column('key').doUpdateSet({- value: cursor,- updated_at: new Date().toISOString(),- })- )- .execute();- }-- protected sendMessage(message: Message) {- return socketService.sendMessage(this.accountId, message);- }-}diff --git a/apps/desktop/src/main/consumers/collaborations.ts b/apps/desktop/src/main/consumers/collaborations.tsdeleted file mode 100644index 728dd55b..00000000--- a/apps/desktop/src/main/consumers/collaborations.ts+++ /dev/null@@ -1,66 +0,0 @@-import {- CollaborationsBatchMessage,- ConsumeCollaborationsMessage,-} from '@colanode/core';--import { collaborationService } from '@/main/services/collaboration-service';-import { BaseConsumer } from '@/main/consumers/base';-import { createDebugger } from '@/main/debugger';--export class CollaborationsConsumer extends BaseConsumer {- private readonly debug = createDebugger('consumer:collaborations');-- protected get cursorKey(): string {- return 'collaborations';- }-- public async init() {- await super.initCursor();- this.requestCollaborations();- }-- public async processCollaborations(message: CollaborationsBatchMessage) {- if (this.status === 'syncing') {- this.debug(- `Syncing of server collaborations already in progress for user ${message.userId}, skipping`- );- return;- }-- this.debug(`Syncing server collaborators for user ${message.userId}`);-- this.status = 'syncing';- let cursor: bigint | null = null;- try {- for (const collaboration of message.collaborations) {- await collaborationService.syncServerCollaboration(- message.userId,- collaboration- );- cursor = BigInt(collaboration.version);- }- } catch (error) {- this.debug(- error,- `Error syncing server collaborators for user ${message.userId}`- );- } finally {- if (cursor) {- await this.setCursor(cursor);- }-- this.status = 'idle';- this.requestCollaborations();- }- }-- private async requestCollaborations() {- const message: ConsumeCollaborationsMessage = {- type: 'consume_collaborations',- userId: this.userId,- cursor: this.cursor.toString(),- };-- this.sendMessage(message);- }-}diff --git a/apps/desktop/src/main/consumers/files.ts b/apps/desktop/src/main/consumers/files.tsdeleted file mode 100644index e603201c..00000000--- a/apps/desktop/src/main/consumers/files.ts+++ /dev/null@@ -1,78 +0,0 @@-import { Kysely } from 'kysely';-import { ConsumeFilesMessage, FilesBatchMessage } from '@colanode/core';--import { fileService } from '@/main/services/file-service';-import { createDebugger } from '@/main/debugger';-import { WorkspaceDatabaseSchema } from '@/main/data/workspace/schema';-import { BaseConsumer } from '@/main/consumers/base';--export class FilesConsumer extends BaseConsumer {- private readonly debug = createDebugger('consumer:files');- private readonly rootId: string;-- constructor(- userId: string,- accountId: string,- rootId: string,- workspaceDatabase: Kysely<WorkspaceDatabaseSchema>- ) {- super(userId, accountId, workspaceDatabase);- this.rootId = rootId;- }-- protected get cursorKey(): string {- return `files:${this.rootId}`;- }-- public async init() {- await super.initCursor();- this.requestFiles();- }-- public async processFiles(message: FilesBatchMessage) {- if (this.status === 'syncing') {- this.debug(- `Syncing of server files already in progress for user ${message.userId}, skipping`- );- return;- }-- this.debug(`Syncing server files for user ${message.userId}`);-- this.status = 'syncing';- let cursor: bigint | null = null;- try {- for (const file of message.files) {- await fileService.syncServerFile(message.userId, file);- cursor = BigInt(file.version);- }- } catch (error) {- this.debug(- error,- `Error syncing server files for user ${message.userId}`- );- } finally {- this.debug(- `Syncing of server files completed for user ${message.userId}`- );-- if (cursor) {- await this.setCursor(cursor);- }-- this.status = 'idle';- this.requestFiles();- }- }-- private async requestFiles() {- const message: ConsumeFilesMessage = {- type: 'consume_files',- userId: this.userId,- rootId: this.rootId,- cursor: this.cursor.toString(),- };-- this.sendMessage(message);- }-}diff --git a/apps/desktop/src/main/consumers/interactions.ts b/apps/desktop/src/main/consumers/interactions.tsdeleted file mode 100644index cefa734d..00000000--- a/apps/desktop/src/main/consumers/interactions.ts+++ /dev/null@@ -1,84 +0,0 @@-import { Kysely } from 'kysely';-import {- ConsumeInteractionsMessage,- InteractionsBatchMessage,-} from '@colanode/core';--import { interactionService } from '@/main/services/interaction-service';-import { createDebugger } from '@/main/debugger';-import { WorkspaceDatabaseSchema } from '@/main/data/workspace/schema';-import { BaseConsumer } from '@/main/consumers/base';--export class InteractionsConsumer extends BaseConsumer {- private readonly debug = createDebugger('consumer:interactions');- private readonly rootId: string;-- constructor(- userId: string,- accountId: string,- rootId: string,- workspaceDatabase: Kysely<WorkspaceDatabaseSchema>- ) {- super(userId, accountId, workspaceDatabase);- this.rootId = rootId;- }-- protected get cursorKey(): string {- return `interactions:${this.rootId}`;- }-- public async init() {- await super.initCursor();- this.requestInteractions();- }-- public async processInteractions(message: InteractionsBatchMessage) {- if (this.status === 'syncing') {- this.debug(- `Syncing of server interactions already in progress for user ${message.userId}, skipping`- );- return;- }-- this.debug(`Syncing server interactions for user ${message.userId}`);-- this.status = 'syncing';- let cursor: bigint | null = null;- try {- for (const interaction of message.interactions) {- await interactionService.applyServerInteraction(- message.userId,- interaction- );- cursor = BigInt(interaction.version);- }- } catch (error) {- this.debug(- error,- `Error syncing server interactions for user ${message.userId}`- );- } finally {- this.debug(- `Syncing of server interactions completed for user ${message.userId}`- );-- if (cursor) {- await this.setCursor(cursor);- }-- this.status = 'idle';- this.requestInteractions();- }- }-- private async requestInteractions() {- const message: ConsumeInteractionsMessage = {- type: 'consume_interactions',- userId: this.userId,- rootId: this.rootId,- cursor: this.cursor.toString(),- };-- this.sendMessage(message);- }-}diff --git a/apps/desktop/src/main/consumers/message-reactions.ts b/apps/desktop/src/main/consumers/message-reactions.tsdeleted file mode 100644index 3f98fab2..00000000--- a/apps/desktop/src/main/consumers/message-reactions.ts+++ /dev/null@@ -1,81 +0,0 @@-import { Kysely } from 'kysely';-import {- MessageReactionsBatchMessage,- ConsumeMessageReactionsMessage,-} from '@colanode/core';--import { messageService } from '@/main/services/message-service';-import { createDebugger } from '@/main/debugger';-import { WorkspaceDatabaseSchema } from '@/main/data/workspace/schema';-import { BaseConsumer } from '@/main/consumers/base';--export class MessageReactionsConsumer extends BaseConsumer {- private readonly debug = createDebugger('consumer:message-reactions');- private readonly rootId: string;-- constructor(- userId: string,- accountId: string,- rootId: string,- workspaceDatabase: Kysely<WorkspaceDatabaseSchema>- ) {- super(userId, accountId, workspaceDatabase);- this.rootId = rootId;- }-- protected get cursorKey(): string {- return `message-reactions:${this.rootId}`;- }-- public async init() {- await super.initCursor();- this.requestMessageReactions();- }-- public async processMessageReactions(message: MessageReactionsBatchMessage) {- if (this.status === 'syncing') {- this.debug(- `Syncing of server messages already in progress for user ${message.userId}, skipping`- );- return;- }-- this.debug(`Syncing server messages for user ${message.userId}`);-- this.status = 'syncing';- let cursor: bigint | null = null;- try {- for (const item of message.messageReactions) {- await messageService.syncServerMessageReaction(message.userId, item);- cursor = BigInt(item.version);- }- } catch (error) {- this.debug(- error,- `Error syncing server messages for user ${message.userId}`- );- } finally {- this.debug(- `Syncing of server messages completed for user ${message.userId}`- );-- if (cursor) {- await this.setCursor(cursor);- }-- this.status = 'idle';- this.requestMessageReactions();- }- }-- private async requestMessageReactions() {- const message: ConsumeMessageReactionsMessage = {- type: 'consume_message_reactions',- userId: this.userId,- rootId: this.rootId,- cursor: this.cursor.toString(),- };-- this.sendMessage(message);- }-}diff --git a/apps/desktop/src/main/consumers/messages.ts b/apps/desktop/src/main/consumers/messages.tsdeleted file mode 100644index 2fbd742b..00000000--- a/apps/desktop/src/main/consumers/messages.ts+++ /dev/null@@ -1,78 +0,0 @@-import { Kysely } from 'kysely';-import { ConsumeMessagesMessage, MessagesBatchMessage } from '@colanode/core';--import { messageService } from '@/main/services/message-service';-import { createDebugger } from '@/main/debugger';-import { WorkspaceDatabaseSchema } from '@/main/data/workspace/schema';-import { BaseConsumer } from '@/main/consumers/base';--export class MessagesConsumer extends BaseConsumer {- private readonly debug = createDebugger('consumer:messages');- private readonly rootId: string;-- constructor(- userId: string,- accountId: string,- rootId: string,- workspaceDatabase: Kysely<WorkspaceDatabaseSchema>- ) {- super(userId, accountId, workspaceDatabase);- this.rootId = rootId;- }-- protected get cursorKey(): string {- return `messages:${this.rootId}`;- }-- public async init() {- await super.initCursor();- this.requestMessages();- }-- public async processMessages(message: MessagesBatchMessage) {- if (this.status === 'syncing') {- this.debug(- `Syncing of server messages already in progress for user ${message.userId}, skipping`- );- return;- }-- this.debug(`Syncing server messages for user ${message.userId}`);-- this.status = 'syncing';- let cursor: bigint | null = null;- try {- for (const item of message.messages) {- await messageService.syncServerMessage(message.userId, item);- cursor = BigInt(item.version);- }- } catch (error) {- this.debug(- error,- `Error syncing server messages for user ${message.userId}`- );- } finally {- this.debug(- `Syncing of server messages completed for user ${message.userId}`- );-- if (cursor) {- await this.setCursor(cursor);- }-- this.status = 'idle';- this.requestMessages();- }- }-- private async requestMessages() {- const message: ConsumeMessagesMessage = {- type: 'consume_messages',- userId: this.userId,- rootId: this.rootId,- cursor: this.cursor.toString(),- };-- this.sendMessage(message);- }-}diff --git a/apps/desktop/src/main/consumers/transactions.ts b/apps/desktop/src/main/consumers/transactions.tsdeleted file mode 100644index 6427872c..00000000--- a/apps/desktop/src/main/consumers/transactions.ts+++ /dev/null@@ -1,81 +0,0 @@-import { Kysely } from 'kysely';-import {- ConsumeTransactionsMessage,- TransactionsBatchMessage,-} from '@colanode/core';--import { nodeService } from '@/main/services/node-service';-import { createDebugger } from '@/main/debugger';-import { WorkspaceDatabaseSchema } from '@/main/data/workspace/schema';-import { BaseConsumer } from '@/main/consumers/base';--export class TransactionsConsumer extends BaseConsumer {- private readonly debug = createDebugger('consumer:transactions');- private readonly rootId: string;-- constructor(- userId: string,- accountId: string,- rootId: string,- workspaceDatabase: Kysely<WorkspaceDatabaseSchema>- ) {- super(userId, accountId, workspaceDatabase);- this.rootId = rootId;- }-- protected get cursorKey(): string {- return `transactions:${this.rootId}`;- }-- public async init() {- await super.initCursor();- this.requestTransactions();- }-- public async processTransactions(message: TransactionsBatchMessage) {- if (this.status === 'syncing') {- this.debug(- `Syncing of server transactions already in progress for user ${message.userId}, skipping`- );- return;- }-- this.debug(`Syncing server transactions for user ${message.userId}`);-- this.status = 'syncing';- let cursor: bigint | null = null;- try {- for (const transaction of message.transactions) {- await nodeService.applyServerTransaction(message.userId, transaction);- cursor = BigInt(transaction.version);- }- } catch (error) {- this.debug(- error,- `Error syncing server transactions for user ${message.userId}`- );- } finally {- this.debug(- `Syncing of server transactions completed for user ${message.userId}`- );-- if (cursor) {- await this.setCursor(cursor);- }-- this.status = 'idle';- this.requestTransactions();- }- }-- private async requestTransactions() {- const message: ConsumeTransactionsMessage = {- type: 'consume_transactions',- userId: this.userId,- rootId: this.rootId,- cursor: this.cursor.toString(),- };-- this.sendMessage(message);- }-}diff --git a/apps/desktop/src/main/consumers/users.ts b/apps/desktop/src/main/consumers/users.tsdeleted file mode 100644index 173441dd..00000000--- a/apps/desktop/src/main/consumers/users.ts+++ /dev/null@@ -1,55 +0,0 @@-import { ConsumeUsersMessage, UsersBatchMessage } from '@colanode/core';--import { userService } from '@/main/services/user-service';-import { BaseConsumer } from '@/main/consumers/base';-import { createDebugger } from '@/main/debugger';--export class UsersConsumer extends BaseConsumer {- private readonly debug = createDebugger('consumer:users');-- protected get cursorKey(): string {- return 'users';- }-- public async init() {- await super.initCursor();- this.requestUsers();- }-- public async processUsers(message: UsersBatchMessage) {- this.status = 'syncing';- let cursor: bigint | null = null;- try {- for (const user of message.users) {- await userService.syncServerUser(message.userId, user);- cursor = BigInt(user.version);- }- } catch (error) {- this.debug(- error,- `Error syncing server users for user ${message.userId}`- );- } finally {- this.debug(- `Syncing of server users completed for user ${message.userId}`- );-- if (cursor) {- await this.setCursor(cursor);- }-- this.status = 'idle';- this.requestUsers();- }- }-- private async requestUsers() {- const message: ConsumeUsersMessage = {- type: 'consume_users',- userId: this.userId,- cursor: this.cursor.toString(),- };-- this.sendMessage(message);- }-}diff --git a/apps/desktop/src/main/jobs/init-sync-consumers.ts b/apps/desktop/src/main/jobs/init-sync-consumers.tsdeleted file mode 100644index 8027f75e..00000000--- a/apps/desktop/src/main/jobs/init-sync-consumers.ts+++ /dev/null@@ -1,27 +0,0 @@-import { syncService } from '@/main/services/sync-service';-import { JobHandler } from '@/main/jobs';--export type InitSyncConsumersInput = {- type: 'init_sync_consumers';- accountId: string;- userId: string;-};--declare module '@/main/jobs' {- interface JobMap {- init_sync_consumers: {- input: InitSyncConsumersInput;- };- }-}--export class InitSyncConsumersJobHandler- implements JobHandler<InitSyncConsumersInput>-{- public triggerDebounce = 100;- public interval = 1000 * 60;-- public async handleJob(input: InitSyncConsumersInput) {- syncService.initUserConsumers(input.accountId, input.userId);- }-}diff --git a/apps/desktop/src/main/jobs/init-synchronizers.ts b/apps/desktop/src/main/jobs/init-synchronizers.tsnew file mode 100644index 00000000..17b8691d--- /dev/null+++ b/apps/desktop/src/main/jobs/init-synchronizers.ts@@ -0,0 +1,26 @@+import { syncService } from '@/main/services/sync-service';+import { JobHandler } from '@/main/jobs';++export type InitSynchronizersInput = {+ type: 'init_synchronizers';+ userId: string;+};++declare module '@/main/jobs' {+ interface JobMap {+ init_synchronizers: {+ input: InitSynchronizersInput;+ };+ }+}++export class InitSynchronizersJobHandler+ implements JobHandler<InitSynchronizersInput>+{+ public triggerDebounce = 100;+ public interval = 1000 * 60;++ public async handleJob(input: InitSynchronizersInput) {+ syncService.initSynchronizers(input.userId);+ }+}diff --git a/apps/desktop/src/main/scheduler.ts b/apps/desktop/src/main/scheduler.tsindex e98e42e1..72e677af 100644--- a/apps/desktop/src/main/scheduler.ts+++ b/apps/desktop/src/main/scheduler.ts@@ -7,7 +7,7 @@ import { createDebugger } from '@/main/debugger';import { JobHandler, JobInput, JobMap } from '@/main/jobs';import { SyncServersJobHandler } from '@/main/jobs/sync-servers';import { SyncAccountJobHandler } from '@/main/jobs/sync-account';-import { InitSyncConsumersJobHandler } from '@/main/jobs/init-sync-consumers';+import { InitSynchronizersJobHandler } from '@/main/jobs/init-synchronizers';// import { RevertInvalidTransactionsJobHandler } from '@/main/jobs/revert-invalid-transactions';import { SyncPendingMutationsJobHandler } from '@/main/jobs/sync-pending-mutations';import { SyncPendingInteractionsJobHandler } from '@/main/jobs/sync-pending-interactions';@@ -26,7 +26,7 @@ type JobHandlerMap = {export const jobHandlerMap: JobHandlerMap = {sync_servers: new SyncServersJobHandler(),sync_account: new SyncAccountJobHandler(),- init_sync_consumers: new InitSyncConsumersJobHandler(),+ init_synchronizers: new InitSynchronizersJobHandler(),// revert_invalid_transactions: new RevertInvalidTransactionsJobHandler(),sync_pending_mutations: new SyncPendingMutationsJobHandler(),sync_pending_interactions: new SyncPendingInteractionsJobHandler(),@@ -218,15 +218,15 @@ class Scheduler {.execute();for (const workspace of workspaces) {- this.scheduleWorkspaceJobs(accountId, workspace.user_id);- }- }+ if (!socketService.isConnected(accountId)) {+ return;+ }- private scheduleWorkspaceJobs(accountId: string, userId: string) {- if (!socketService.isConnected(accountId)) {- return;+ this.scheduleWorkspaceJobs(workspace.user_id);}+ }+ private scheduleWorkspaceJobs(userId: string) {this.schedule({type: 'sync_pending_mutations',userId,@@ -243,9 +243,8 @@ class Scheduler {// });this.schedule({- type: 'init_sync_consumers',+ type: 'init_synchronizers',userId,- accountId,});this.schedule({@@ -280,7 +279,7 @@ class Scheduler {private isWorkspaceJob(state: JobState, userId: string) {if (- state.input.type === 'init_sync_consumers' &&+ state.input.type === 'init_synchronizers' &&state.input.userId === userId) {return true;@@ -345,10 +344,7 @@ class Scheduler {});this.deleteAccountJobs(event.account.id);} else if (event.type === 'workspace_created') {- this.scheduleWorkspaceJobs(- event.workspace.accountId,- event.workspace.userId- );+ this.scheduleWorkspaceJobs(event.workspace.userId);} else if (event.type === 'workspace_deleted') {this.deleteWorkspaceJobs(event.workspace.userId);} else if (event.type === 'socket_connection_opened') {@@ -377,6 +373,11 @@ class Scheduler {type: 'clean_deleted_files',userId: event.userId,});+ } else if (event.type === 'collaboration_created') {+ this.trigger({+ type: 'init_synchronizers',+ userId: event.userId,+ });}}}diff --git a/apps/desktop/src/main/services/collaboration-service.ts b/apps/desktop/src/main/services/collaboration-service.tsindex df7afd41..4d62d67d 100644--- a/apps/desktop/src/main/services/collaboration-service.ts+++ b/apps/desktop/src/main/services/collaboration-service.ts@@ -1,4 +1,4 @@-import { ServerCollaboration } from '@colanode/core';+import { SyncCollaborationData } from '@colanode/core';import { createDebugger } from '@/main/debugger';import { databaseService } from '@/main/data/database-service';@@ -9,7 +9,7 @@ class CollaborationService {public async syncServerCollaboration(userId: string,- collaboration: ServerCollaboration+ collaboration: SyncCollaborationData) {this.debug(`Applying server collaboration: ${collaboration.nodeId} for user ${userId}`diff --git a/apps/desktop/src/main/services/file-service.ts b/apps/desktop/src/main/services/file-service.tsindex 19370d7d..214f1a98 100644--- a/apps/desktop/src/main/services/file-service.ts+++ b/apps/desktop/src/main/services/file-service.ts@@ -3,7 +3,7 @@ import {CreateDownloadOutput,CreateUploadOutput,extractFileType,- ServerFile,+ SyncFileData,} from '@colanode/core';import axios from 'axios';import mime from 'mime-types';@@ -433,7 +433,10 @@ class FileService {}}- public async syncServerFile(userId: string, file: ServerFile): Promise<void> {+ public async syncServerFile(+ userId: string,+ file: SyncFileData+ ): Promise<void> {const workspaceDatabase =await databaseService.getWorkspaceDatabase(userId);diff --git a/apps/desktop/src/main/services/message-service.ts b/apps/desktop/src/main/services/message-service.tsindex 503ac43c..dd0eea88 100644--- a/apps/desktop/src/main/services/message-service.ts+++ b/apps/desktop/src/main/services/message-service.ts@@ -1,4 +1,4 @@-import { ServerMessage, ServerMessageReaction } from '@colanode/core';+import { SyncMessageData, SyncMessageReactionData } from '@colanode/core';import { mapMessage, mapMessageReaction } from '@/main/utils';import { databaseService } from '@/main/data/database-service';@@ -8,7 +8,7 @@ import { eventBus } from '@/shared/lib/event-bus';class MessageService {private readonly debug = createDebugger('service:message');- public async syncServerMessage(userId: string, message: ServerMessage) {+ public async syncServerMessage(userId: string, message: SyncMessageData) {const workspaceDatabase =await databaseService.getWorkspaceDatabase(userId);@@ -86,7 +86,7 @@ class MessageService {public async syncServerMessageReaction(userId: string,- messageReaction: ServerMessageReaction+ messageReaction: SyncMessageReactionData) {const workspaceDatabase =await databaseService.getWorkspaceDatabase(userId);diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.tsindex 63ee156c..2adf519f 100644--- a/apps/desktop/src/main/services/node-service.ts+++ b/apps/desktop/src/main/services/node-service.ts@@ -8,10 +8,10 @@ import {NodeAttributes,NodeMutationContext,registry,- ServerCreateTransaction,- ServerDeleteTransaction,- ServerTransaction,- ServerUpdateTransaction,+ SyncCreateTransactionData,+ SyncDeleteTransactionData,+ SyncTransactionData,+ SyncUpdateTransactionData,} from '@colanode/core';import { decodeState, YDoc } from '@colanode/crdt';@@ -531,7 +531,7 @@ class NodeService {public async applyServerTransaction(userId: string,- transaction: ServerTransaction+ transaction: SyncTransactionData) {if (transaction.operation === 'create') {await this.applyServerCreateTransaction(userId, transaction);@@ -545,7 +545,7 @@ class NodeService {public async replaceTransactions(userId: string,nodeId: string,- transactions: ServerTransaction[],+ transactions: SyncTransactionData[],transactionCursor: bigint): Promise<boolean> {for (let count = 0; count < UPDATE_RETRIES_LIMIT; count++) {@@ -567,7 +567,7 @@ class NodeService {public async tryReplaceTransactions(userId: string,nodeId: string,- transactions: ServerTransaction[],+ transactions: SyncTransactionData[],transactionCursor: bigint): Promise<boolean | null> {const workspaceDatabase =@@ -785,7 +785,7 @@ class NodeService {private async applyServerCreateTransaction(userId: string,- transaction: ServerCreateTransaction+ transaction: SyncCreateTransactionData) {this.debug(`Applying server create transaction ${transaction.id} for node ${transaction.nodeId}`@@ -912,7 +912,7 @@ class NodeService {private async applyServerUpdateTransaction(userId: string,- transaction: ServerUpdateTransaction+ transaction: SyncUpdateTransactionData) {const workspaceDatabase =await databaseService.getWorkspaceDatabase(userId);@@ -1062,7 +1062,7 @@ class NodeService {private async applyServerDeleteTransaction(userId: string,- transaction: ServerDeleteTransaction+ transaction: SyncDeleteTransactionData) {this.debug(`Applying server delete transaction ${transaction.id} for node ${transaction.nodeId}`diff --git a/apps/desktop/src/main/services/socket-connection.ts b/apps/desktop/src/main/services/socket-connection.tsindex 397951b4..a03c5085 100644--- a/apps/desktop/src/main/services/socket-connection.ts+++ b/apps/desktop/src/main/services/socket-connection.ts@@ -58,45 +58,8 @@ export class SocketConnection {`Received message of type ${message.type} for account ${this.account.id}`);- if (message.type === 'transactions_batch') {- const transactionsConsumer = syncService.getTransactionsConsumer(- message.userId,- message.rootId- );- transactionsConsumer?.processTransactions(message);- } else if (message.type === 'collaborations_batch') {- const collaborationsConsumer = syncService.getCollaborationsConsumer(- message.userId- );- collaborationsConsumer?.processCollaborations(message);- } else if (message.type === 'interactions_batch') {- const interactionsConsumer = syncService.getInteractionsConsumer(- message.userId,- message.rootId- );- interactionsConsumer?.processInteractions(message);- } else if (message.type === 'users_batch') {- const usersConsumer = syncService.getUsersConsumer(message.userId);- usersConsumer?.processUsers(message);- } else if (message.type === 'files_batch') {- const filesConsumer = syncService.getFilesConsumer(- message.userId,- message.rootId- );- filesConsumer?.processFiles(message);- } else if (message.type === 'messages_batch') {- const messagesConsumer = syncService.getMessagesConsumer(- message.userId,- message.rootId- );- messagesConsumer?.processMessages(message);- } else if (message.type === 'message_reactions_batch') {- const messageReactionsConsumer =- syncService.getMessageReactionsConsumer(- message.userId,- message.rootId- );- messageReactionsConsumer?.processMessageReactions(message);+ if (message.type === 'synchronizer_output') {+ syncService.processSyncMessage(message);} else if (message.type === 'account_updated') {scheduler.trigger({type: 'sync_account',diff --git a/apps/desktop/src/main/services/sync-service.ts b/apps/desktop/src/main/services/sync-service.tsindex e17cf588..bacc71fb 100644--- a/apps/desktop/src/main/services/sync-service.ts+++ b/apps/desktop/src/main/services/sync-service.ts@@ -1,59 +1,55 @@+import { SynchronizerInput, SynchronizerOutputMessage } from '@colanode/core';+import { Kysely } from 'kysely';+import { databaseService } from '@/main/data/database-service';-import { UsersConsumer } from '@/main/consumers/users';-import { TransactionsConsumer } from '@/main/consumers/transactions';-import { CollaborationsConsumer } from '@/main/consumers/collaborations';-import { InteractionsConsumer } from '@/main/consumers/interactions';-import { FilesConsumer } from '@/main/consumers/files';+import { BaseSynchronizer } from '@/main/synchronizers/base';import { createDebugger } from '@/main/debugger';-import { MessagesConsumer } from '@/main/consumers/messages';-import { MessageReactionsConsumer } from '@/main/consumers/message-reactions';--export type NodeConsumersWrapper = {- transactions: TransactionsConsumer;- interactions: InteractionsConsumer;- files: FilesConsumer;- messages: MessagesConsumer;- messageReactions: MessageReactionsConsumer;-};--export type ConsumersWrapper = {- users: UsersConsumer;- collaborations: CollaborationsConsumer;- nodes: Record<string, NodeConsumersWrapper>;-};+import { UserSynchronizer } from '@/main/synchronizers/users';+import { WorkspaceDatabaseSchema } from '@/main/data/workspace/schema';+import { CollaborationSynchronizer } from '@/main/synchronizers/collaborations';+import { TransactionSynchronizer } from '@/main/synchronizers/transactions';+import { MessageSynchronizer } from '@/main/synchronizers/messages';+import { MessageReactionSynchronizer } from '@/main/synchronizers/message-reactions';+import { FileSynchronizer } from '@/main/synchronizers/files';class SyncService {private readonly debug = createDebugger('service:sync');- private readonly users: Map<string, ConsumersWrapper> = new Map();+ private readonly synchronizers: Map<+ string,+ BaseSynchronizer<SynchronizerInput>+ > = new Map();++ public async initSynchronizers(userId: string) {+ const workspace = await databaseService.appDatabase+ .selectFrom('workspaces')+ .selectAll()+ .where('user_id', '=', userId)+ .executeTakeFirst();++ if (!workspace) {+ return;+ }- public async initUserConsumers(accountId: string, userId: string) {const workspaceDatabase =await databaseService.getWorkspaceDatabase(userId);- let consumers = this.users.get(userId);- if (!consumers) {- const usersConsumer = new UsersConsumer(- userId,- accountId,- workspaceDatabase- );- await usersConsumer.init();-- const collaborationsConsumer = new CollaborationsConsumer(- userId,- accountId,- workspaceDatabase- );- await collaborationsConsumer.init();-- consumers = {- users: usersConsumer,- collaborations: collaborationsConsumer,- nodes: {},- };-- this.users.set(userId, consumers);- }+ await this.initSynchronizer(+ userId,+ workspace.account_id,+ workspaceDatabase,+ {+ type: 'users',+ }+ );++ await this.initSynchronizer(+ userId,+ workspace.account_id,+ workspaceDatabase,+ {+ type: 'collaborations',+ }+ );const collaborations = await workspaceDatabase.selectFrom('collaborations')@@ -62,111 +58,179 @@ class SyncService {for (const collaboration of collaborations) {const rootId = collaboration.node_id;-- if (consumers.nodes[rootId]) {- continue;+ if (collaboration.deleted_at) {+ this.removeRootNodeSynchronizers(userId, rootId);+ } else {+ await this.initRootNodeSynchronizers(+ userId,+ workspace.account_id,+ rootId,+ workspaceDatabase+ );}+ }+ }- const transactionsConsumer = new TransactionsConsumer(- userId,- accountId,- rootId,- workspaceDatabase+ public processSyncMessage(+ message: SynchronizerOutputMessage<SynchronizerInput>+ ) {+ const synchronizer = this.synchronizers.get(message.id);+ if (!synchronizer) {+ return;+ }++ synchronizer.sync(message);+ }++ private async initSynchronizer(+ userId: string,+ accountId: string,+ workspaceDatabase: Kysely<WorkspaceDatabaseSchema>,+ input: SynchronizerInput+ ) {+ this.debug('Initializing synchronizer', input);+ const synchronizer = this.buildSynchronizer(+ userId,+ accountId,+ workspaceDatabase,+ input+ );++ if (!synchronizer) {+ return;+ }++ const existingSynchronizer = this.synchronizers.get(synchronizer.id);+ if (existingSynchronizer) {+ this.debug(+ 'Synchronizer already exists, pinging',+ existingSynchronizer.id);- await transactionsConsumer.init();+ existingSynchronizer.ping();+ return;+ }++ this.synchronizers.set(synchronizer.id, synchronizer);+ await synchronizer.init();+ }++ private buildSynchronizer(+ userId: string,+ accountId: string,+ workspaceDatabase: Kysely<WorkspaceDatabaseSchema>,+ input: SynchronizerInput+ ): BaseSynchronizer<SynchronizerInput> | null {+ if (input.type === 'users') {+ return new UserSynchronizer(userId, accountId, input, workspaceDatabase);+ }- const interactionsConsumer = new InteractionsConsumer(+ if (input.type === 'collaborations') {+ return new CollaborationSynchronizer(userId,accountId,- rootId,+ input,workspaceDatabase);- await interactionsConsumer.init();+ }++ if (input.type === 'files') {+ return new FileSynchronizer(userId, accountId, input, workspaceDatabase);+ }- const filesConsumer = new FilesConsumer(+ if (input.type === 'messages') {+ return new MessageSynchronizer(userId,accountId,- rootId,+ input,workspaceDatabase);- await filesConsumer.init();+ }- const messagesConsumer = new MessagesConsumer(+ if (input.type === 'message_reactions') {+ return new MessageReactionSynchronizer(userId,accountId,- rootId,+ input,workspaceDatabase);- await messagesConsumer.init();+ }- const messageReactionsConsumer = new MessageReactionsConsumer(+ if (input.type === 'transactions') {+ return new TransactionSynchronizer(userId,accountId,- rootId,+ input,workspaceDatabase);- await messageReactionsConsumer.init();-- consumers.nodes[rootId] = {- transactions: transactionsConsumer,- interactions: interactionsConsumer,- files: filesConsumer,- messages: messagesConsumer,- messageReactions: messageReactionsConsumer,- };}- // check for deleted collaborations and remove them from the consumers- for (const nodeId of Object.keys(consumers.nodes)) {- if (!collaborations.some((c) => c.node_id === nodeId)) {- delete consumers.nodes[nodeId];- }- }+ return null;}- public getUsersConsumer(userId: string): UsersConsumer | undefined {- return this.users.get(userId)?.users;+ private async initRootNodeSynchronizers(+ userId: string,+ accountId: string,+ rootId: string,+ workspaceDatabase: Kysely<WorkspaceDatabaseSchema>+ ) {+ await this.initSynchronizer(userId, accountId, workspaceDatabase, {+ type: 'transactions',+ rootId,+ });++ await this.initSynchronizer(userId, accountId, workspaceDatabase, {+ type: 'messages',+ rootId,+ });++ await this.initSynchronizer(userId, accountId, workspaceDatabase, {+ type: 'message_reactions',+ rootId,+ });++ await this.initSynchronizer(userId, accountId, workspaceDatabase, {+ type: 'files',+ rootId,+ });}- public getCollaborationsConsumer(- userId: string- ): CollaborationsConsumer | undefined {- return this.users.get(userId)?.collaborations;- }+ private removeRootNodeSynchronizers(userId: string, rootId: string) {+ const keys = Array.from(this.synchronizers.keys());- public getTransactionsConsumer(- userId: string,- rootId: string- ): TransactionsConsumer | undefined {- return this.users.get(userId)?.nodes[rootId]?.transactions;- }+ for (const key of keys) {+ const synchronizer = this.synchronizers.get(key);+ if (!synchronizer) {+ continue;+ }- public getInteractionsConsumer(- userId: string,- rootId: string- ): InteractionsConsumer | undefined {- return this.users.get(userId)?.nodes[rootId]?.interactions;- }+ if (+ synchronizer.input.type === 'transactions' &&+ synchronizer.input.rootId === rootId+ ) {+ this.synchronizers.delete(key);+ }- public getFilesConsumer(- userId: string,- rootId: string- ): FilesConsumer | undefined {- return this.users.get(userId)?.nodes[rootId]?.files;- }+ if (+ synchronizer.input.type === 'messages' &&+ synchronizer.input.rootId === rootId+ ) {+ this.synchronizers.delete(key);+ }- public getMessagesConsumer(- userId: string,- rootId: string- ): MessagesConsumer | undefined {- return this.users.get(userId)?.nodes[rootId]?.messages;- }+ if (+ synchronizer.input.type === 'message_reactions' &&+ synchronizer.input.rootId === rootId+ ) {+ this.synchronizers.delete(key);+ }- public getMessageReactionsConsumer(- userId: string,- rootId: string- ): MessageReactionsConsumer | undefined {- return this.users.get(userId)?.nodes[rootId]?.messageReactions;+ if (+ synchronizer.input.type === 'files' &&+ synchronizer.input.rootId === rootId+ ) {+ this.synchronizers.delete(key);+ }+ }}}diff --git a/apps/desktop/src/main/services/user-service.ts b/apps/desktop/src/main/services/user-service.tsindex be1dc5af..0bbd9b5b 100644--- a/apps/desktop/src/main/services/user-service.ts+++ b/apps/desktop/src/main/services/user-service.ts@@ -1,9 +1,9 @@-import { ServerUser } from '@colanode/core';+import { SyncUserData } from '@colanode/core';-import { databaseService } from '../data/database-service';+import { databaseService } from '@/main/data/database-service';class UserService {- public async syncServerUser(userId: string, user: ServerUser) {+ public async syncServerUser(userId: string, user: SyncUserData) {const workspaceDatabase =await databaseService.getWorkspaceDatabase(userId);diff --git a/apps/desktop/src/main/synchronizers/base.ts b/apps/desktop/src/main/synchronizers/base.tsnew file mode 100644index 00000000..65e25710--- /dev/null+++ b/apps/desktop/src/main/synchronizers/base.ts@@ -0,0 +1,140 @@+import { Kysely } from 'kysely';+import {+ SynchronizerOutputMessage,+ SynchronizerInputMessage,+ SynchronizerInput,+ SynchronizerMap,+} from '@colanode/core';+import { sha256 } from 'js-sha256';++import { socketService } from '@/main/services/socket-service';+import { createDebugger } from '@/main/debugger';+import { WorkspaceDatabaseSchema } from '@/main/data/workspace/schema';++export type SynchronizerStatus = 'idle' | 'waiting' | 'processing';++export abstract class BaseSynchronizer<TInput extends SynchronizerInput> {+ public readonly id: string;+ public readonly userId: string;+ public readonly accountId: string;+ public readonly input: TInput;++ protected readonly database: Kysely<WorkspaceDatabaseSchema>;+ protected readonly debug = createDebugger('consumer');++ private status: SynchronizerStatus = 'idle';+ private cursor: bigint = 0n;++ constructor(+ userId: string,+ accountId: string,+ input: TInput,+ database: Kysely<WorkspaceDatabaseSchema>+ ) {+ this.id = this.generateId(userId, input);+ this.userId = userId;+ this.accountId = accountId;+ this.input = input;+ this.database = database;+ }++ protected abstract process(+ data: SynchronizerMap[TInput['type']]['data']+ ): Promise<void>;++ protected abstract get cursorKey(): string;++ public async init() {+ this.cursor = await this.fetchCursor();+ this.initConsumer();+ }++ public async ping() {+ this.initConsumer();+ }++ public async sync(message: SynchronizerOutputMessage<TInput>) {+ if (message.id !== this.id) {+ return;+ }++ if (this.status === 'processing') {+ return;+ }++ this.status = 'processing';+ let lastCursor: bigint | null = null;++ try {+ for (const item of message.items) {+ await this.process(item.data);+ lastCursor = BigInt(item.cursor);+ }+ } catch (error) {+ this.debug('Error consuming items', error);+ } finally {+ if (lastCursor !== null) {+ this.cursor = lastCursor;+ await this.saveCursor(lastCursor);+ }++ this.status = 'idle';+ this.initConsumer();+ }+ }++ private initConsumer() {+ if (this.status === 'processing') {+ return;+ }++ const message: SynchronizerInputMessage = {+ type: 'synchronizer_input',+ userId: this.userId,+ id: this.id,+ input: this.input,+ cursor: this.cursor.toString(),+ };++ const sent = socketService.sendMessage(this.accountId, message);+ if (!sent) {+ this.status = 'waiting';+ }+ }++ private async fetchCursor() {+ const cursor = await this.database+ .selectFrom('cursors')+ .select('value')+ .where('key', '=', this.cursorKey)+ .executeTakeFirst();++ return cursor?.value ?? 0n;+ }++ private async saveCursor(cursor: bigint) {+ await this.database+ .insertInto('cursors')+ .values({+ key: this.cursorKey,+ value: cursor,+ created_at: new Date().toISOString(),+ })+ .onConflict((eb) =>+ eb.column('key').doUpdateSet({+ value: cursor,+ updated_at: new Date().toISOString(),+ })+ )+ .execute();+ }++ private generateId(userId: string, input: TInput) {+ const idObj = {+ userId,+ inputId: input,+ };++ return sha256(JSON.stringify(idObj));+ }+}diff --git a/apps/desktop/src/main/synchronizers/collaborations.ts b/apps/desktop/src/main/synchronizers/collaborations.tsnew file mode 100644index 00000000..3dcc69b8--- /dev/null+++ b/apps/desktop/src/main/synchronizers/collaborations.ts@@ -0,0 +1,14 @@+import { SyncCollaborationData, SyncCollaborationsInput } from '@colanode/core';++import { BaseSynchronizer } from '@/main/synchronizers/base';+import { collaborationService } from '@/main/services/collaboration-service';++export class CollaborationSynchronizer extends BaseSynchronizer<SyncCollaborationsInput> {+ protected async process(data: SyncCollaborationData): Promise<void> {+ await collaborationService.syncServerCollaboration(this.userId, data);+ }++ protected get cursorKey(): string {+ return `collaborations`;+ }+}diff --git a/apps/desktop/src/main/synchronizers/files.ts b/apps/desktop/src/main/synchronizers/files.tsnew file mode 100644index 00000000..26fb8f91--- /dev/null+++ b/apps/desktop/src/main/synchronizers/files.ts@@ -0,0 +1,14 @@+import { SyncFilesInput, SyncFileData } from '@colanode/core';++import { BaseSynchronizer } from '@/main/synchronizers/base';+import { fileService } from '@/main/services/file-service';++export class FileSynchronizer extends BaseSynchronizer<SyncFilesInput> {+ protected async process(data: SyncFileData): Promise<void> {+ await fileService.syncServerFile(this.userId, data);+ }++ protected get cursorKey(): string {+ return `files:${this.input.rootId}`;+ }+}diff --git a/apps/desktop/src/main/synchronizers/message-reactions.ts b/apps/desktop/src/main/synchronizers/message-reactions.tsnew file mode 100644index 00000000..3e2ecea0--- /dev/null+++ b/apps/desktop/src/main/synchronizers/message-reactions.ts@@ -0,0 +1,17 @@+import {+ SyncMessageReactionsInput,+ SyncMessageReactionData,+} from '@colanode/core';++import { BaseSynchronizer } from '@/main/synchronizers/base';+import { messageService } from '@/main/services/message-service';++export class MessageReactionSynchronizer extends BaseSynchronizer<SyncMessageReactionsInput> {+ protected async process(data: SyncMessageReactionData): Promise<void> {+ await messageService.syncServerMessageReaction(this.userId, data);+ }++ protected get cursorKey(): string {+ return `message_reactions:${this.input.rootId}`;+ }+}diff --git a/apps/desktop/src/main/synchronizers/messages.ts b/apps/desktop/src/main/synchronizers/messages.tsnew file mode 100644index 00000000..93502ea7--- /dev/null+++ b/apps/desktop/src/main/synchronizers/messages.ts@@ -0,0 +1,14 @@+import { SyncMessagesInput, SyncMessageData } from '@colanode/core';++import { BaseSynchronizer } from '@/main/synchronizers/base';+import { messageService } from '@/main/services/message-service';++export class MessageSynchronizer extends BaseSynchronizer<SyncMessagesInput> {+ protected async process(data: SyncMessageData): Promise<void> {+ await messageService.syncServerMessage(this.userId, data);+ }++ protected get cursorKey(): string {+ return `messages:${this.input.rootId}`;+ }+}diff --git a/apps/desktop/src/main/synchronizers/transactions.ts b/apps/desktop/src/main/synchronizers/transactions.tsnew file mode 100644index 00000000..5bdc10d4--- /dev/null+++ b/apps/desktop/src/main/synchronizers/transactions.ts@@ -0,0 +1,14 @@+import { SyncTransactionData, SyncTransactionsInput } from '@colanode/core';++import { BaseSynchronizer } from '@/main/synchronizers/base';+import { nodeService } from '@/main/services/node-service';++export class TransactionSynchronizer extends BaseSynchronizer<SyncTransactionsInput> {+ protected async process(data: SyncTransactionData): Promise<void> {+ await nodeService.applyServerTransaction(this.userId, data);+ }++ protected get cursorKey(): string {+ return `transactions:${this.input.rootId}`;+ }+}diff --git a/apps/desktop/src/main/synchronizers/users.ts b/apps/desktop/src/main/synchronizers/users.tsnew file mode 100644index 00000000..1bd8a056--- /dev/null+++ b/apps/desktop/src/main/synchronizers/users.ts@@ -0,0 +1,14 @@+import { SyncUserData, SyncUsersInput } from '@colanode/core';++import { BaseSynchronizer } from '@/main/synchronizers/base';+import { userService } from '@/main/services/user-service';++export class UserSynchronizer extends BaseSynchronizer<SyncUsersInput> {+ protected async process(data: SyncUserData): Promise<void> {+ await userService.syncServerUser(this.userId, data);+ }++ protected get cursorKey(): string {+ return `users`;+ }+}diff --git a/apps/server/src/consumers/base.ts b/apps/server/src/consumers/base.tsdeleted file mode 100644index c9ca15fb..00000000--- a/apps/server/src/consumers/base.ts+++ /dev/null@@ -1,16 +0,0 @@-import { ConnectedUser } from '@/types/users';-import { Event } from '@/types/events';--export type ConsumerStatus = 'idle' | 'pending' | 'fetching';--export abstract class BaseConsumer {- protected status: ConsumerStatus = 'idle';- protected user: ConnectedUser;- protected cursor: bigint | null = null;-- constructor(user: ConnectedUser) {- this.user = user;- }-- public abstract processEvent(event: Event): void;-}diff --git a/apps/server/src/consumers/collaborations.ts b/apps/server/src/consumers/collaborations.tsdeleted file mode 100644index ccb275fb..00000000--- a/apps/server/src/consumers/collaborations.ts+++ /dev/null@@ -1,76 +0,0 @@-import {- CollaborationsBatchMessage,- ConsumeCollaborationsMessage,-} from '@colanode/core';--import { BaseConsumer } from '@/consumers/base';-import { Event } from '@/types/events';-import { database } from '@/data/database';-import { mapCollaboration } from '@/lib/nodes';--export class CollaborationsConsumer extends BaseConsumer {- public processEvent(event: Event): void {- if (- event.type === 'collaboration_created' &&- event.collaboratorId === this.user.userId- ) {- this.fetchCollaborations();- } else if (- event.type === 'collaboration_updated' &&- event.collaboratorId === this.user.userId- ) {- this.fetchCollaborations();- }- }-- public async consume(message: ConsumeCollaborationsMessage) {- if (this.status === 'fetching') {- return;- }-- const cursor = BigInt(message.cursor);- if (this.status === 'idle') {- this.cursor = cursor;- this.status = 'pending';- await this.fetchCollaborations();- } else if (this.status === 'pending' && this.cursor !== cursor) {- this.cursor = cursor;- await this.fetchCollaborations();- }- }-- private async fetchCollaborations() {- if (this.cursor === null) {- return;- }-- if (this.status !== 'pending') {- return;- }-- this.status = 'fetching';- const unsyncedCollaborations = await database- .selectFrom('collaborations')- .selectAll()- .where('workspace_id', '=', this.user.workspaceId)- .where('version', '>', this.cursor)- .orderBy('version', 'asc')- .limit(50)- .execute();-- if (unsyncedCollaborations.length === 0) {- this.status = 'pending';- return;- }-- const collaborations = unsyncedCollaborations.map(mapCollaboration);- const message: CollaborationsBatchMessage = {- type: 'collaborations_batch',- userId: this.user.userId,- collaborations,- };-- this.status = 'idle';- this.user.sendMessage(message);- }-}diff --git a/apps/server/src/consumers/files.ts b/apps/server/src/consumers/files.tsdeleted file mode 100644index 05105845..00000000--- a/apps/server/src/consumers/files.ts+++ /dev/null@@ -1,78 +0,0 @@-import { ConsumeFilesMessage, FilesBatchMessage } from '@colanode/core';--import { BaseConsumer } from '@/consumers/base';-import { Event } from '@/types/events';-import { database } from '@/data/database';-import { mapFile } from '@/lib/nodes';-import { ConnectedUser } from '@/types/users';--export class FilesConsumer extends BaseConsumer {- private readonly rootId: string;-- constructor(user: ConnectedUser, rootId: string) {- super(user);- this.rootId = rootId;- }-- public processEvent(event: Event): void {- if (event.type === 'file_created' && event.rootId === this.rootId) {- this.fetchFiles();- } else if (event.type === 'file_updated' && event.rootId === this.rootId) {- this.fetchFiles();- } else if (event.type === 'file_deleted' && event.rootId === this.rootId) {- this.fetchFiles();- }- }-- public async consume(message: ConsumeFilesMessage) {- if (this.status === 'fetching') {- return;- }-- const cursor = BigInt(message.cursor);- if (this.status === 'idle') {- this.cursor = cursor;- this.status = 'pending';- await this.fetchFiles();- } else if (this.status === 'pending' && this.cursor !== cursor) {- this.cursor = cursor;- await this.fetchFiles();- }- }-- private async fetchFiles() {- if (this.cursor === null) {- return;- }-- if (this.status !== 'pending') {- return;- }-- this.status = 'fetching';- const unsyncedFiles = await database- .selectFrom('files')- .selectAll()- .where('root_id', '=', this.rootId)- .where('version', '>', this.cursor)- .orderBy('version', 'asc')- .limit(20)- .execute();-- if (unsyncedFiles.length === 0) {- this.status = 'pending';- return;- }-- const files = unsyncedFiles.map(mapFile);- const message: FilesBatchMessage = {- type: 'files_batch',- userId: this.user.userId,- rootId: this.rootId,- files,- };-- this.status = 'idle';- this.user.sendMessage(message);- }-}diff --git a/apps/server/src/consumers/interactions.ts b/apps/server/src/consumers/interactions.tsdeleted file mode 100644index d08ab0c8..00000000--- a/apps/server/src/consumers/interactions.ts+++ /dev/null@@ -1,77 +0,0 @@-import {- ConsumeInteractionsMessage,- InteractionsBatchMessage,-} from '@colanode/core';--import { BaseConsumer } from '@/consumers/base';-import { Event } from '@/types/events';-import { database } from '@/data/database';-import { mapInteraction } from '@/lib/nodes';-import { ConnectedUser } from '@/types/users';--export class InteractionsConsumer extends BaseConsumer {- private readonly rootId: string;-- constructor(user: ConnectedUser, rootId: string) {- super(user);- this.rootId = rootId;- }-- public processEvent(event: Event): void {- if (event.type === 'interaction_updated' && event.rootId === this.rootId) {- this.fetchInteractions();- }- }-- public async consume(message: ConsumeInteractionsMessage) {- if (this.status === 'fetching') {- return;- }-- const cursor = BigInt(message.cursor);- if (this.status === 'idle') {- this.cursor = cursor;- this.status = 'pending';- await this.fetchInteractions();- } else if (this.status === 'pending' && this.cursor !== cursor) {- this.cursor = cursor;- await this.fetchInteractions();- }- }-- private async fetchInteractions() {- if (this.cursor === null) {- return;- }-- if (this.status !== 'pending') {- return;- }-- this.status = 'fetching';- const unsyncedInteractions = await database- .selectFrom('interactions')- .selectAll()- .where('node_id', '=', this.rootId)- .where('version', '>', this.cursor)- .orderBy('version', 'asc')- .limit(20)- .execute();-- if (unsyncedInteractions.length === 0) {- this.status = 'pending';- return;- }-- const interactions = unsyncedInteractions.map(mapInteraction);- const message: InteractionsBatchMessage = {- type: 'interactions_batch',- userId: this.user.userId,- rootId: this.rootId,- interactions,- };-- this.status = 'idle';- this.user.sendMessage(message);- }-}diff --git a/apps/server/src/consumers/message-reactions.ts b/apps/server/src/consumers/message-reactions.tsdeleted file mode 100644index 8f8e32cf..00000000--- a/apps/server/src/consumers/message-reactions.ts+++ /dev/null@@ -1,85 +0,0 @@-import {- ConsumeMessageReactionsMessage,- MessageReactionsBatchMessage,-} from '@colanode/core';--import { BaseConsumer } from '@/consumers/base';-import { Event } from '@/types/events';-import { database } from '@/data/database';-import { mapMessageReaction } from '@/lib/nodes';-import { ConnectedUser } from '@/types/users';--export class MessageReactionsConsumer extends BaseConsumer {- private readonly rootId: string;-- constructor(user: ConnectedUser, rootId: string) {- super(user);- this.rootId = rootId;- }-- public processEvent(event: Event): void {- if (- event.type === 'message_reaction_created' &&- event.rootId === this.rootId- ) {- this.fetchMessageReactions();- } else if (- event.type === 'message_reaction_deleted' &&- event.rootId === this.rootId- ) {- this.fetchMessageReactions();- }- }-- public async consume(message: ConsumeMessageReactionsMessage) {- if (this.status === 'fetching') {- return;- }-- const cursor = BigInt(message.cursor);- if (this.status === 'idle') {- this.cursor = cursor;- this.status = 'pending';- await this.fetchMessageReactions();- } else if (this.status === 'pending' && this.cursor !== cursor) {- this.cursor = cursor;- await this.fetchMessageReactions();- }- }-- private async fetchMessageReactions() {- if (this.cursor === null) {- return;- }-- if (this.status !== 'pending') {- return;- }-- this.status = 'fetching';- const unsyncedMessageReactions = await database- .selectFrom('message_reactions')- .selectAll()- .where('root_id', '=', this.rootId)- .where('version', '>', this.cursor)- .orderBy('version', 'asc')- .limit(20)- .execute();-- if (unsyncedMessageReactions.length === 0) {- this.status = 'pending';- return;- }-- const messageReactions = unsyncedMessageReactions.map(mapMessageReaction);- const message: MessageReactionsBatchMessage = {- type: 'message_reactions_batch',- userId: this.user.userId,- rootId: this.rootId,- messageReactions,- };-- this.status = 'idle';- this.user.sendMessage(message);- }-}diff --git a/apps/server/src/consumers/messages.ts b/apps/server/src/consumers/messages.tsdeleted file mode 100644index c57a232b..00000000--- a/apps/server/src/consumers/messages.ts+++ /dev/null@@ -1,84 +0,0 @@-import { ConsumeMessagesMessage, MessagesBatchMessage } from '@colanode/core';--import { BaseConsumer } from '@/consumers/base';-import { Event } from '@/types/events';-import { database } from '@/data/database';-import { mapMessage } from '@/lib/nodes';-import { ConnectedUser } from '@/types/users';--export class MessagesConsumer extends BaseConsumer {- private readonly rootId: string;-- constructor(user: ConnectedUser, rootId: string) {- super(user);- this.rootId = rootId;- }-- public processEvent(event: Event): void {- if (event.type === 'message_created' && event.rootId === this.rootId) {- this.fetchMessages();- } else if (- event.type === 'message_updated' &&- event.rootId === this.rootId- ) {- this.fetchMessages();- } else if (- event.type === 'message_deleted' &&- event.rootId === this.rootId- ) {- this.fetchMessages();- }- }-- public async consume(message: ConsumeMessagesMessage) {- if (this.status === 'fetching') {- return;- }-- const cursor = BigInt(message.cursor);- if (this.status === 'idle') {- this.cursor = cursor;- this.status = 'pending';- await this.fetchMessages();- } else if (this.status === 'pending' && this.cursor !== cursor) {- this.cursor = cursor;- await this.fetchMessages();- }- }-- private async fetchMessages() {- if (this.cursor === null) {- return;- }-- if (this.status !== 'pending') {- return;- }-- this.status = 'fetching';- const unsyncedMessages = await database- .selectFrom('messages')- .selectAll()- .where('root_id', '=', this.rootId)- .where('version', '>', this.cursor)- .orderBy('version', 'asc')- .limit(20)- .execute();-- if (unsyncedMessages.length === 0) {- this.status = 'pending';- return;- }-- const messages = unsyncedMessages.map(mapMessage);- const message: MessagesBatchMessage = {- type: 'messages_batch',- userId: this.user.userId,- rootId: this.rootId,- messages,- };-- this.status = 'idle';- this.user.sendMessage(message);- }-}diff --git a/apps/server/src/consumers/transactions.ts b/apps/server/src/consumers/transactions.tsdeleted file mode 100644index c3f8783f..00000000--- a/apps/server/src/consumers/transactions.ts+++ /dev/null@@ -1,81 +0,0 @@-import {- ConsumeTransactionsMessage,- TransactionsBatchMessage,-} from '@colanode/core';--import { BaseConsumer } from '@/consumers/base';-import { Event } from '@/types/events';-import { database } from '@/data/database';-import { mapTransaction } from '@/lib/nodes';-import { ConnectedUser } from '@/types/users';--export class TransactionsConsumer extends BaseConsumer {- private readonly rootId: string;-- constructor(user: ConnectedUser, rootId: string) {- super(user);- this.rootId = rootId;- }-- public processEvent(event: Event): void {- if (event.type === 'node_created' && event.rootId === this.rootId) {- this.fetchTransactions();- } else if (event.type === 'node_updated' && event.rootId === this.rootId) {- this.fetchTransactions();- } else if (event.type === 'node_deleted' && event.rootId === this.rootId) {- this.fetchTransactions();- }- }-- public async consume(message: ConsumeTransactionsMessage) {- if (this.status === 'fetching') {- return;- }-- const cursor = BigInt(message.cursor);- if (this.status === 'idle') {- this.cursor = cursor;- this.status = 'pending';- await this.fetchTransactions();- } else if (this.status === 'pending' && this.cursor !== cursor) {- this.cursor = cursor;- await this.fetchTransactions();- }- }-- private async fetchTransactions() {- if (this.cursor === null) {- return;- }-- if (this.status !== 'pending') {- return;- }-- this.status = 'fetching';- const unsyncedTransactions = await database- .selectFrom('transactions')- .selectAll()- .where('root_id', '=', this.rootId)- .where('version', '>', this.cursor)- .orderBy('version', 'asc')- .limit(20)- .execute();-- if (unsyncedTransactions.length === 0) {- this.status = 'pending';- return;- }-- const transactions = unsyncedTransactions.map(mapTransaction);- const message: TransactionsBatchMessage = {- type: 'transactions_batch',- userId: this.user.userId,- rootId: this.rootId,- transactions,- };-- this.status = 'idle';- this.user.sendMessage(message);- }-}diff --git a/apps/server/src/consumers/users.ts b/apps/server/src/consumers/users.tsdeleted file mode 100644index 361de3d9..00000000--- a/apps/server/src/consumers/users.ts+++ /dev/null@@ -1,73 +0,0 @@-import { ConsumeUsersMessage, UsersBatchMessage } from '@colanode/core';--import { BaseConsumer } from '@/consumers/base';-import { Event } from '@/types/events';-import { database } from '@/data/database';-import { mapUser } from '@/lib/nodes';--export class UsersConsumer extends BaseConsumer {- public processEvent(event: Event): void {- if (- event.type === 'user_created' &&- event.workspaceId === this.user.workspaceId- ) {- this.fetchUsers();- } else if (- event.type === 'user_updated' &&- event.workspaceId === this.user.workspaceId- ) {- this.fetchUsers();- }- }-- public async consume(message: ConsumeUsersMessage) {- if (this.status === 'fetching') {- return;- }-- const cursor = BigInt(message.cursor);- if (this.status === 'idle') {- this.cursor = cursor;- this.status = 'pending';- await this.fetchUsers();- } else if (this.status === 'pending' && this.cursor !== cursor) {- this.cursor = cursor;- await this.fetchUsers();- }- }-- private async fetchUsers() {- if (this.cursor === null) {- return;- }-- if (this.status !== 'pending') {- return;- }-- this.status = 'fetching';- const unsyncedUsers = await database- .selectFrom('users')- .selectAll()- .where('workspace_id', '=', this.user.workspaceId)- .where('version', '>', this.cursor)- .orderBy('version', 'asc')- .limit(50)- .execute();-- if (unsyncedUsers.length === 0) {- this.status = 'pending';- return;- }-- const users = unsyncedUsers.map(mapUser);- const message: UsersBatchMessage = {- type: 'users_batch',- userId: this.user.userId,- users,- };-- this.status = 'idle';- this.user.sendMessage(message);- }-}diff --git a/apps/server/src/data/migrations.ts b/apps/server/src/data/migrations.tsindex 61401745..3ccd7772 100644--- a/apps/server/src/data/migrations.ts+++ b/apps/server/src/data/migrations.ts@@ -208,7 +208,6 @@ const createCollaborationsTable: Migration = {.createTable('collaborations').addColumn('node_id', 'varchar(30)', (col) => col.notNull()).addColumn('collaborator_id', 'varchar(30)', (col) => col.notNull())- .addColumn('root_id', 'varchar(30)', (col) => col.notNull()).addColumn('workspace_id', 'varchar(30)', (col) => col.notNull()).addColumn('role', 'varchar(30)', (col) => col.notNull()).addColumn('created_at', 'timestamptz', (col) => col.notNull())diff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.tsindex 0bf12342..31f22d4b 100644--- a/apps/server/src/data/schema.ts+++ b/apps/server/src/data/schema.ts@@ -115,7 +115,6 @@ export type UpdateNode = Updateable<NodeTable>;interface CollaborationTable {node_id: ColumnType<string, string, never>;collaborator_id: ColumnType<string, string, never>;- root_id: ColumnType<string, string, never>;workspace_id: ColumnType<string, string, never>;role: ColumnType<NodeRole, NodeRole, NodeRole>;created_at: ColumnType<Date, Date, never>;diff --git a/apps/server/src/lib/nodes.ts b/apps/server/src/lib/nodes.tsindex 34b189bc..5af9e78f 100644--- a/apps/server/src/lib/nodes.ts+++ b/apps/server/src/lib/nodes.ts@@ -5,27 +5,11 @@ import {NodeOutput,NodeRole,NodeType,- ServerCollaboration,- ServerFile,ServerInteraction,- ServerMessage,- ServerMessageReaction,- ServerTransaction,- ServerUser,} from '@colanode/core';-import { encodeState } from '@colanode/crdt';import { database } from '@/data/database';-import {- SelectInteraction,- SelectNode,- SelectCollaboration,- SelectTransaction,- SelectUser,- SelectFile,- SelectMessage,- SelectMessageReaction,-} from '@/data/schema';+import { SelectInteraction, SelectNode } from '@/data/schema';import { NodeCollaborator } from '@/types/nodes';export const mapNodeOutput = (node: SelectNode): NodeOutput => {@@ -59,56 +43,6 @@ export const mapNode = (node: SelectNode): Node => {} as Node;};-export const mapTransaction = (- transaction: SelectTransaction-): ServerTransaction => {- if (transaction.operation === 'create' && transaction.data) {- return {- id: transaction.id,- operation: 'create',- nodeId: transaction.node_id,- rootId: transaction.root_id,- workspaceId: transaction.workspace_id,- data: encodeState(transaction.data),- createdAt: transaction.created_at.toISOString(),- createdBy: transaction.created_by,- serverCreatedAt: transaction.server_created_at.toISOString(),- version: transaction.version.toString(),- };- }-- if (transaction.operation === 'update' && transaction.data) {- return {- id: transaction.id,- operation: 'update',- nodeId: transaction.node_id,- rootId: transaction.root_id,- workspaceId: transaction.workspace_id,- data: encodeState(transaction.data),- createdAt: transaction.created_at.toISOString(),- createdBy: transaction.created_by,- serverCreatedAt: transaction.server_created_at.toISOString(),- version: transaction.version.toString(),- };- }-- if (transaction.operation === 'delete') {- return {- id: transaction.id,- operation: 'delete',- nodeId: transaction.node_id,- rootId: transaction.root_id,- workspaceId: transaction.workspace_id,- createdAt: transaction.created_at.toISOString(),- createdBy: transaction.created_by,- serverCreatedAt: transaction.server_created_at.toISOString(),- version: transaction.version.toString(),- };- }-- throw new Error('Unknown transaction type');-};-export const mapInteraction = (interaction: SelectInteraction): ServerInteraction => {@@ -125,98 +59,6 @@ export const mapInteraction = (};};-export const mapUser = (user: SelectUser): ServerUser => {- return {- id: user.id,- workspaceId: user.workspace_id,- email: user.email,- role: user.role,- name: user.name,- avatar: user.avatar,- customName: user.custom_name,- customAvatar: user.custom_avatar,- createdAt: user.created_at.toISOString(),- updatedAt: user.updated_at?.toISOString() ?? null,- version: user.version.toString(),- };-};--export const mapCollaboration = (- collaboration: SelectCollaboration-): ServerCollaboration => {- return {- nodeId: collaboration.node_id,- rootId: collaboration.root_id,- workspaceId: collaboration.workspace_id,- collaboratorId: collaboration.collaborator_id,- role: collaboration.role,- createdBy: collaboration.created_by,- updatedBy: collaboration.updated_by,- createdAt: collaboration.created_at.toISOString(),- updatedAt: collaboration.updated_at?.toISOString() ?? null,- deletedAt: collaboration.deleted_at?.toISOString() ?? null,- deletedBy: collaboration.deleted_by ?? null,- version: collaboration.version,- };-};--export const mapFile = (file: SelectFile): ServerFile => {- return {- id: file.id,- type: file.type,- parentId: file.parent_id,- rootId: file.root_id,- workspaceId: file.workspace_id,- name: file.name,- originalName: file.original_name,- mimeType: file.mime_type,- size: file.size,- extension: file.extension,- createdAt: file.created_at.toISOString(),- createdBy: file.created_by,- updatedAt: file.updated_at?.toISOString() ?? null,- updatedBy: file.updated_by ?? null,- deletedAt: file.deleted_at?.toISOString() ?? null,- deletedBy: file.deleted_by ?? null,- version: file.version.toString(),- status: file.status,- };-};--export const mapMessage = (message: SelectMessage): ServerMessage => {- return {- id: message.id,- type: message.type,- nodeId: message.node_id,- parentId: message.parent_id,- rootId: message.root_id,- workspaceId: message.workspace_id,- content: JSON.parse(message.content),- createdAt: message.created_at.toISOString(),- createdBy: message.created_by,- updatedAt: message.updated_at?.toISOString() ?? null,- updatedBy: message.updated_by ?? null,- deletedAt: message.deleted_at?.toISOString() ?? null,- deletedBy: message.deleted_by ?? null,- version: message.version.toString(),- };-};--export const mapMessageReaction = (- messageReaction: SelectMessageReaction-): ServerMessageReaction => {- return {- messageId: messageReaction.message_id,- collaboratorId: messageReaction.collaborator_id,- reaction: messageReaction.reaction,- rootId: messageReaction.root_id,- workspaceId: messageReaction.workspace_id,- createdAt: messageReaction.created_at.toISOString(),- deletedAt: messageReaction.deleted_at?.toISOString() ?? null,- version: messageReaction.version.toString(),- };-};-export const fetchNode = async (nodeId: string): Promise<SelectNode | null> => {const result = await database.selectFrom('nodes')diff --git a/apps/server/src/services/node-service.ts b/apps/server/src/services/node-service.tsindex 78e22e76..74fa17ce 100644--- a/apps/server/src/services/node-service.ts+++ b/apps/server/src/services/node-service.ts@@ -667,7 +667,6 @@ class NodeService {).map(([userId, role]) => ({collaborator_id: userId,node_id: node.id,- root_id: node.root_id,workspace_id: node.workspace_id,role,created_at: new Date(),@@ -733,7 +732,6 @@ class NodeService {.values({collaborator_id: collaboratorId,node_id: nodeId,- root_id: rootId,workspace_id: workspaceId,role,created_at: new Date(),diff --git a/apps/server/src/services/socket-connection.ts b/apps/server/src/services/socket-connection.tsindex 99475889..c504de4d 100644--- a/apps/server/src/services/socket-connection.ts+++ b/apps/server/src/services/socket-connection.ts@@ -1,14 +1,9 @@import { WebSocket } from 'ws';import {Message,+ SynchronizerInput,+ SynchronizerInputMessage,WorkspaceStatus,- ConsumeUsersMessage,- ConsumeCollaborationsMessage,- ConsumeTransactionsMessage,- ConsumeInteractionsMessage,- ConsumeFilesMessage,- ConsumeMessagesMessage,- ConsumeMessageReactionsMessage,} from '@colanode/core';import { interactionService } from '@/services/interaction-service';@@ -17,36 +12,25 @@ import { RequestAccount } from '@/types/api';import { database } from '@/data/database';import {AccountUpdatedEvent,+ CollaborationCreatedEvent,+ CollaborationUpdatedEvent,Event,WorkspaceDeletedEvent,WorkspaceUpdatedEvent,} from '@/types/events';import { ConnectedUser } from '@/types/users';-import { UsersConsumer } from '@/consumers/users';-import { CollaborationsConsumer } from '@/consumers/collaborations';-import { TransactionsConsumer } from '@/consumers/transactions';-import { InteractionsConsumer } from '@/consumers/interactions';-import { FilesConsumer } from '@/consumers/files';-import { MessagesConsumer } from '@/consumers/messages';-import { MessageReactionsConsumer } from '@/consumers/message-reactions';--type NodeConsumersWrapper = {- transactions: TransactionsConsumer;- interactions: InteractionsConsumer;- files: FilesConsumer;- messages: MessagesConsumer;- messageReactions: MessageReactionsConsumer;-};--type ConsumersWrapper = {- users: UsersConsumer;- collaborations: CollaborationsConsumer;- nodes: Map<string, NodeConsumersWrapper>;-};+import { BaseSynchronizer } from '@/synchronizers/base';+import { UserSynchronizer } from '@/synchronizers/users';+import { CollaborationSynchronizer } from '@/synchronizers/collaborations';+import { FileSynchronizer } from '@/synchronizers/files';+import { MessageSynchronizer } from '@/synchronizers/messages';+import { MessageReactionSynchronizer } from '@/synchronizers/message-reactions';+import { TransactionSynchronizer } from '@/synchronizers/transactions';type SocketUser = {user: ConnectedUser;- consumers: ConsumersWrapper;+ rootIds: Set<string>;+ synchronizers: Map<string, BaseSynchronizer<SynchronizerInput>>;};export class SocketConnection {@@ -84,81 +68,43 @@ export class SocketConnection {this.socket.close();}- public handleEvent(event: Event) {- if (event.type === 'account_updated') {- this.handleAccountUpdatedEvent(event);- } else if (event.type === 'workspace_updated') {- this.handleWorkspaceUpdatedEvent(event);- } else if (event.type === 'workspace_deleted') {- this.handleWorkspaceDeletedEvent(event);- } else {- for (const user of this.users.values()) {- user.consumers.users.processEvent(event);- user.consumers.collaborations.processEvent(event);- for (const node of user.consumers.nodes.values()) {- node.transactions.processEvent(event);- node.interactions.processEvent(event);- node.files.processEvent(event);- node.messages.processEvent(event);- node.messageReactions.processEvent(event);- }- }- }- }-private async handleMessage(message: Message) {this.logger.trace(message, `Socket message from ${this.account.id}`);- if (message.type === 'consume_users') {- this.handleConsumeUsers(message);- } else if (message.type === 'consume_collaborations') {- this.handleConsumeCollaborations(message);- } else if (message.type === 'consume_transactions') {- this.handleConsumeTransactions(message);- } else if (message.type === 'consume_interactions') {- this.handleConsumeInteractions(message);- } else if (message.type === 'consume_files') {- this.handleConsumeFiles(message);- } else if (message.type === 'consume_messages') {- this.handleConsumeMessages(message);- } else if (message.type === 'consume_message_reactions') {- this.handleConsumeMessageReactions(message);+ if (message.type === 'synchronizer_input') {+ this.handleSynchronizerInput(message);} else if (message.type === 'sync_interactions') {interactionService.syncLocalInteractions(this.account.id, message);}}- private async handleConsumeUsers(message: ConsumeUsersMessage) {- this.logger.info(- `Consume users from ${this.account.id} and user ${message.userId}`- );-- const user = await this.getOrCreateUser(message.userId);- if (user === null) {- return;+ public async handleEvent(event: Event) {+ if (event.type === 'account_updated') {+ this.handleAccountUpdatedEvent(event);+ } else if (event.type === 'workspace_updated') {+ this.handleWorkspaceUpdatedEvent(event);+ } else if (event.type === 'workspace_deleted') {+ this.handleWorkspaceDeletedEvent(event);+ } else if (event.type === 'collaboration_created') {+ this.handleCollaborationCreatedEvent(event);+ } else if (event.type === 'collaboration_updated') {+ this.handleCollaborationUpdatedEvent(event);}- await user.consumers.users.consume(message);- }-- private async handleConsumeCollaborations(- message: ConsumeCollaborationsMessage- ) {- this.logger.info(- `Consume collaborations from ${this.account.id} and user ${message.userId}`- );-- const user = await this.getOrCreateUser(message.userId);- if (user === null) {- return;+ for (const user of this.users.values()) {+ for (const synchronizer of user.synchronizers.values()) {+ const output = await synchronizer.fetchDataFromEvent(event);+ if (output) {+ user.synchronizers.delete(synchronizer.id);+ this.sendMessage(output);+ }+ }}-- await user.consumers.collaborations.consume(message);}- private async handleConsumeTransactions(message: ConsumeTransactionsMessage) {+ private async handleSynchronizerInput(message: SynchronizerInputMessage) {this.logger.info(- `Consume transactions from ${this.account.id} and user ${message.userId}`+ `Synchronizer input from ${this.account.id} and user ${message.userId} and input ${message.input}`);const user = await this.getOrCreateUser(message.userId);@@ -166,141 +112,68 @@ export class SocketConnection {return;}- let nodeConsumers = user.consumers.nodes.get(message.rootId);- if (!nodeConsumers) {- nodeConsumers = {- transactions: new TransactionsConsumer(user.user, message.rootId),- interactions: new InteractionsConsumer(user.user, message.rootId),- files: new FilesConsumer(user.user, message.rootId),- messages: new MessagesConsumer(user.user, message.rootId),- messageReactions: new MessageReactionsConsumer(- user.user,- message.rootId- ),- };-- user.consumers.nodes.set(message.rootId, nodeConsumers);- }-- await nodeConsumers.transactions.consume(message);- }-- private async handleConsumeInteractions(message: ConsumeInteractionsMessage) {- this.logger.info(- `Consume interactions from ${this.account.id} and user ${message.userId}`- );-- const user = await this.getOrCreateUser(message.userId);- if (user === null) {+ const synchronizer = this.buildSynchronizer(message, user);+ if (synchronizer === null) {return;}- let nodeConsumers = user.consumers.nodes.get(message.rootId);- if (!nodeConsumers) {- nodeConsumers = {- transactions: new TransactionsConsumer(user.user, message.rootId),- interactions: new InteractionsConsumer(user.user, message.rootId),- files: new FilesConsumer(user.user, message.rootId),- messages: new MessagesConsumer(user.user, message.rootId),- messageReactions: new MessageReactionsConsumer(- user.user,- message.rootId- ),- };-- user.consumers.nodes.set(message.rootId, nodeConsumers);- }-- await nodeConsumers.interactions.consume(message);- }-- private async handleConsumeFiles(message: ConsumeFilesMessage) {- this.logger.info(- `Consume files from ${this.account.id} and user ${message.userId}`- );-- const user = await this.getOrCreateUser(message.userId);- if (user === null) {+ const output = await synchronizer.fetchData();+ if (output === null) {+ user.synchronizers.set(synchronizer.id, synchronizer);return;}- let nodeConsumers = user.consumers.nodes.get(message.rootId);- if (!nodeConsumers) {- nodeConsumers = {- transactions: new TransactionsConsumer(user.user, message.rootId),- interactions: new InteractionsConsumer(user.user, message.rootId),- files: new FilesConsumer(user.user, message.rootId),- messages: new MessagesConsumer(user.user, message.rootId),- messageReactions: new MessageReactionsConsumer(- user.user,- message.rootId- ),- };-- user.consumers.nodes.set(message.rootId, nodeConsumers);- }-- await nodeConsumers.files.consume(message);+ this.sendMessage(output);}- private async handleConsumeMessages(message: ConsumeMessagesMessage) {- this.logger.info(- `Consume messages from ${this.account.id} and user ${message.userId}`- );-- const user = await this.getOrCreateUser(message.userId);- if (user === null) {- return;- }-- let nodeConsumers = user.consumers.nodes.get(message.rootId);- if (!nodeConsumers) {- nodeConsumers = {- transactions: new TransactionsConsumer(user.user, message.rootId),- interactions: new InteractionsConsumer(user.user, message.rootId),- files: new FilesConsumer(user.user, message.rootId),- messages: new MessagesConsumer(user.user, message.rootId),- messageReactions: new MessageReactionsConsumer(- user.user,- message.rootId- ),- };-- user.consumers.nodes.set(message.rootId, nodeConsumers);- }-- await nodeConsumers.messages.consume(message);- }-- private async handleConsumeMessageReactions(- message: ConsumeMessageReactionsMessage- ) {- this.logger.info(- `Consume message reactions from ${this.account.id} and user ${message.userId}`- );+ private buildSynchronizer(+ message: SynchronizerInputMessage,+ user: SocketUser+ ): BaseSynchronizer<SynchronizerInput> | null {+ const cursor = BigInt(message.cursor);+ if (message.input.type === 'users') {+ return new UserSynchronizer(message.id, user.user, message.input, cursor);+ } else if (message.input.type === 'collaborations') {+ return new CollaborationSynchronizer(+ message.id,+ user.user,+ message.input,+ cursor+ );+ } else if (message.input.type === 'files') {+ if (!user.rootIds.has(message.input.rootId)) {+ return null;+ }- const user = await this.getOrCreateUser(message.userId);- if (user === null) {- return;- }+ return new FileSynchronizer(message.id, user.user, message.input, cursor);+ } else if (message.input.type === 'messages') {+ if (!user.rootIds.has(message.input.rootId)) {+ return null;+ }- let nodeConsumers = user.consumers.nodes.get(message.rootId);- if (!nodeConsumers) {- nodeConsumers = {- transactions: new TransactionsConsumer(user.user, message.rootId),- interactions: new InteractionsConsumer(user.user, message.rootId),- files: new FilesConsumer(user.user, message.rootId),- messages: new MessagesConsumer(user.user, message.rootId),- messageReactions: new MessageReactionsConsumer(- user.user,- message.rootId- ),- };-- user.consumers.nodes.set(message.rootId, nodeConsumers);+ return new MessageSynchronizer(+ message.id,+ user.user,+ message.input,+ cursor+ );+ } else if (message.input.type === 'message_reactions') {+ return new MessageReactionSynchronizer(+ message.id,+ user.user,+ message.input,+ cursor+ );+ } else if (message.input.type === 'transactions') {+ return new TransactionSynchronizer(+ message.id,+ user.user,+ message.input,+ cursor+ );}- await nodeConsumers.messageReactions.consume(message);+ return null;}private async getOrCreateUser(userId: string): Promise<SocketUser | null> {@@ -340,6 +213,12 @@ export class SocketConnection {return null;}+ const collaborations = await database+ .selectFrom('collaborations')+ .selectAll()+ .where('collaborator_id', '=', userId)+ .execute();+const addedSocketUser = this.users.get(userId);if (addedSocketUser) {return addedSocketUser;@@ -351,24 +230,25 @@ export class SocketConnection {workspaceId: user.workspace_id,accountId: this.account.id,deviceId: this.account.deviceId,- sendMessage: this.sendMessage.bind(this),};- const usersConsumer = new UsersConsumer(connectedUser);- const collaborationsConsumer = new CollaborationsConsumer(connectedUser);- const nodesConsumers = new Map<string, NodeConsumersWrapper>();+ const rootIds = new Set<string>();+ for (const collaboration of collaborations) {+ if (collaboration.deleted_at) {+ continue;+ }- const newSocketUser: SocketUser = {+ rootIds.add(collaboration.node_id);+ }++ const socketUser: SocketUser = {user: connectedUser,- consumers: {- users: usersConsumer,- collaborations: collaborationsConsumer,- nodes: nodesConsumers,- },+ rootIds,+ synchronizers: new Map(),};- this.users.set(userId, newSocketUser);- return newSocketUser;+ this.users.set(userId, socketUser);+ return socketUser;}private handleAccountUpdatedEvent(event: AccountUpdatedEvent) {@@ -411,4 +291,35 @@ export class SocketConnection {accountId: this.account.id,});}++ private handleCollaborationCreatedEvent(event: CollaborationCreatedEvent) {+ const user = this.users.get(event.collaboratorId);+ if (!user) {+ return;+ }++ user.rootIds.add(event.nodeId);+ }++ private async handleCollaborationUpdatedEvent(+ event: CollaborationUpdatedEvent+ ) {+ const user = this.users.get(event.collaboratorId);+ if (!user) {+ return;+ }++ const collaboration = await database+ .selectFrom('collaborations')+ .selectAll()+ .where('collaborator_id', '=', event.collaboratorId)+ .where('node_id', '=', event.nodeId)+ .executeTakeFirst();++ if (!collaboration || collaboration.deleted_at) {+ user.rootIds.delete(event.nodeId);+ } else {+ user.rootIds.add(event.nodeId);+ }+ }}diff --git a/apps/server/src/synchronizers/base.ts b/apps/server/src/synchronizers/base.tsnew file mode 100644index 00000000..1df110b0--- /dev/null+++ b/apps/server/src/synchronizers/base.ts@@ -0,0 +1,27 @@+import { SynchronizerInput, SynchronizerOutputMessage } from '@colanode/core';++import { ConnectedUser } from '@/types/users';+import { Event } from '@/types/events';++export type SynchronizerStatus = 'pending' | 'fetching';++export abstract class BaseSynchronizer<T extends SynchronizerInput> {+ public readonly id: string;+ public readonly user: ConnectedUser;+ public readonly input: T;+ public readonly cursor: bigint;++ protected status: SynchronizerStatus = 'pending';++ constructor(id: string, user: ConnectedUser, input: T, cursor: bigint) {+ this.id = id;+ this.user = user;+ this.input = input;+ this.cursor = cursor;+ }++ public abstract fetchData(): Promise<SynchronizerOutputMessage<T> | null>;+ public abstract fetchDataFromEvent(+ event: Event+ ): Promise<SynchronizerOutputMessage<T> | null>;+}diff --git a/apps/server/src/synchronizers/collaborations.ts b/apps/server/src/synchronizers/collaborations.tsnew file mode 100644index 00000000..aeea534e--- /dev/null+++ b/apps/server/src/synchronizers/collaborations.ts@@ -0,0 +1,105 @@+import {+ SynchronizerOutputMessage,+ SyncCollaborationsInput,+ SyncCollaborationData,+} from '@colanode/core';++import { BaseSynchronizer } from '@/synchronizers/base';+import { Event } from '@/types/events';+import { database } from '@/data/database';+import { SelectCollaboration } from '@/data/schema';++export class CollaborationSynchronizer extends BaseSynchronizer<SyncCollaborationsInput> {+ public async fetchData(): Promise<SynchronizerOutputMessage<SyncCollaborationsInput> | null> {+ const collaborations = await this.fetchCollaborations();+ if (collaborations.length === 0) {+ return null;+ }++ return this.buildMessage(collaborations);+ }++ public async fetchDataFromEvent(+ event: Event+ ): Promise<SynchronizerOutputMessage<SyncCollaborationsInput> | null> {+ if (!this.shouldFetch(event)) {+ return null;+ }++ const collaborations = await this.fetchCollaborations();+ if (collaborations.length === 0) {+ return null;+ }++ return this.buildMessage(collaborations);+ }++ private async fetchCollaborations() {+ if (this.status === 'fetching') {+ return [];+ }++ this.status = 'fetching';+ const collaborations = await database+ .selectFrom('collaborations')+ .selectAll()+ .where('workspace_id', '=', this.user.workspaceId)+ .where('version', '>', this.cursor)+ .orderBy('version', 'asc')+ .limit(50)+ .execute();++ this.status = 'pending';+ return collaborations;+ }++ private buildMessage(+ unsyncedCollaborations: SelectCollaboration[]+ ): SynchronizerOutputMessage<SyncCollaborationsInput> {+ const items: SyncCollaborationData[] = unsyncedCollaborations.map(+ (collaboration) => ({+ nodeId: collaboration.node_id,+ workspaceId: collaboration.workspace_id,+ collaboratorId: collaboration.collaborator_id,+ role: collaboration.role,+ createdBy: collaboration.created_by,+ updatedBy: collaboration.updated_by,+ createdAt: collaboration.created_at.toISOString(),+ updatedAt: collaboration.updated_at?.toISOString() ?? null,+ deletedAt: collaboration.deleted_at?.toISOString() ?? null,+ deletedBy: collaboration.deleted_by ?? null,+ version: collaboration.version.toString(),+ })+ );++ return {+ type: 'synchronizer_output',+ userId: this.user.userId,+ id: this.id,+ items: items.map((item) => ({+ cursor: item.version,+ data: item,+ })),+ };+ }++ private shouldFetch(event: Event) {+ if (+ event.type === 'collaboration_created' &&+ event.workspaceId === this.user.workspaceId &&+ event.collaboratorId === this.user.userId+ ) {+ return true;+ }++ if (+ event.type === 'collaboration_updated' &&+ event.workspaceId === this.user.workspaceId &&+ event.collaboratorId === this.user.userId+ ) {+ return true;+ }++ return false;+ }+}diff --git a/apps/server/src/synchronizers/files.ts b/apps/server/src/synchronizers/files.tsnew file mode 100644index 00000000..c91cf1b8--- /dev/null+++ b/apps/server/src/synchronizers/files.ts@@ -0,0 +1,106 @@+import {+ SynchronizerOutputMessage,+ SyncFilesInput,+ SyncFileData,+} from '@colanode/core';++import { BaseSynchronizer } from '@/synchronizers/base';+import { Event } from '@/types/events';+import { database } from '@/data/database';+import { SelectFile } from '@/data/schema';++export class FileSynchronizer extends BaseSynchronizer<SyncFilesInput> {+ public async fetchData(): Promise<SynchronizerOutputMessage<SyncFilesInput> | null> {+ const files = await this.fetchFiles();+ if (files.length === 0) {+ return null;+ }++ return this.buildMessage(files);+ }++ public async fetchDataFromEvent(+ event: Event+ ): Promise<SynchronizerOutputMessage<SyncFilesInput> | null> {+ if (!this.shouldFetch(event)) {+ return null;+ }++ const files = await this.fetchFiles();+ if (files.length === 0) {+ return null;+ }++ return this.buildMessage(files);+ }++ private async fetchFiles() {+ if (this.status === 'fetching') {+ return [];+ }++ this.status = 'fetching';+ const files = await database+ .selectFrom('files')+ .selectAll()+ .where('root_id', '=', this.input.rootId)+ .where('version', '>', this.cursor)+ .orderBy('version', 'asc')+ .limit(20)+ .execute();++ this.status = 'pending';+ return files;+ }++ private buildMessage(+ unsyncedFiles: SelectFile[]+ ): SynchronizerOutputMessage<SyncFilesInput> {+ const items: SyncFileData[] = unsyncedFiles.map((file) => ({+ id: file.id,+ type: file.type,+ parentId: file.parent_id,+ rootId: file.root_id,+ workspaceId: file.workspace_id,+ name: file.name,+ originalName: file.original_name,+ mimeType: file.mime_type,+ size: file.size,+ extension: file.extension,+ createdAt: file.created_at.toISOString(),+ createdBy: file.created_by,+ updatedAt: file.updated_at?.toISOString() ?? null,+ updatedBy: file.updated_by ?? null,+ deletedAt: file.deleted_at?.toISOString() ?? null,+ deletedBy: file.deleted_by ?? null,+ version: file.version.toString(),+ status: file.status,+ }));++ return {+ type: 'synchronizer_output',+ userId: this.user.userId,+ id: this.id,+ items: items.map((item) => ({+ cursor: item.version,+ data: item,+ })),+ };+ }++ private shouldFetch(event: Event) {+ if (event.type === 'file_created' && event.rootId === this.input.rootId) {+ return true;+ }++ if (event.type === 'file_updated' && event.rootId === this.input.rootId) {+ return true;+ }++ if (event.type === 'file_deleted' && event.rootId === this.input.rootId) {+ return true;+ }++ return false;+ }+}diff --git a/apps/server/src/synchronizers/message-reactions.ts b/apps/server/src/synchronizers/message-reactions.tsnew file mode 100644index 00000000..db9e2237--- /dev/null+++ b/apps/server/src/synchronizers/message-reactions.ts@@ -0,0 +1,100 @@+import {+ SynchronizerOutputMessage,+ SyncMessageReactionsInput,+ SyncMessageReactionData,+} from '@colanode/core';++import { BaseSynchronizer } from '@/synchronizers/base';+import { Event } from '@/types/events';+import { database } from '@/data/database';+import { SelectMessageReaction } from '@/data/schema';++export class MessageReactionSynchronizer extends BaseSynchronizer<SyncMessageReactionsInput> {+ public async fetchData(): Promise<SynchronizerOutputMessage<SyncMessageReactionsInput> | null> {+ const messageReactions = await this.fetchMessageReactions();+ if (messageReactions.length === 0) {+ return null;+ }++ return this.buildMessage(messageReactions);+ }++ public async fetchDataFromEvent(+ event: Event+ ): Promise<SynchronizerOutputMessage<SyncMessageReactionsInput> | null> {+ if (!this.shouldFetch(event)) {+ return null;+ }++ const messageReactions = await this.fetchMessageReactions();+ if (messageReactions.length === 0) {+ return null;+ }++ return this.buildMessage(messageReactions);+ }++ private async fetchMessageReactions() {+ if (this.status === 'fetching') {+ return [];+ }++ this.status = 'fetching';+ const messageReactions = await database+ .selectFrom('message_reactions')+ .selectAll()+ .where('root_id', '=', this.input.rootId)+ .where('version', '>', this.cursor)+ .orderBy('version', 'asc')+ .limit(20)+ .execute();++ this.status = 'pending';+ return messageReactions;+ }++ private buildMessage(+ unsyncedMessageReactions: SelectMessageReaction[]+ ): SynchronizerOutputMessage<SyncMessageReactionsInput> {+ const items: SyncMessageReactionData[] = unsyncedMessageReactions.map(+ (messageReaction) => ({+ messageId: messageReaction.message_id,+ collaboratorId: messageReaction.collaborator_id,+ reaction: messageReaction.reaction,+ rootId: messageReaction.root_id,+ workspaceId: messageReaction.workspace_id,+ createdAt: messageReaction.created_at.toISOString(),+ deletedAt: messageReaction.deleted_at?.toISOString() ?? null,+ version: messageReaction.version.toString(),+ })+ );++ return {+ type: 'synchronizer_output',+ userId: this.user.userId,+ id: this.id,+ items: items.map((item) => ({+ cursor: item.version,+ data: item,+ })),+ };+ }++ private shouldFetch(event: Event) {+ if (+ event.type === 'message_reaction_created' &&+ event.rootId === this.input.rootId+ ) {+ return true;+ }++ if (+ event.type === 'message_reaction_deleted' &&+ event.rootId === this.input.rootId+ ) {+ return true;+ }++ return false;+ }+}diff --git a/apps/server/src/synchronizers/messages.ts b/apps/server/src/synchronizers/messages.tsnew file mode 100644index 00000000..aa459d14--- /dev/null+++ b/apps/server/src/synchronizers/messages.ts@@ -0,0 +1,111 @@+import {+ SynchronizerOutputMessage,+ SyncMessagesInput,+ SyncMessageData,+} from '@colanode/core';++import { BaseSynchronizer } from '@/synchronizers/base';+import { Event } from '@/types/events';+import { database } from '@/data/database';+import { SelectMessage } from '@/data/schema';++export class MessageSynchronizer extends BaseSynchronizer<SyncMessagesInput> {+ public async fetchData(): Promise<SynchronizerOutputMessage<SyncMessagesInput> | null> {+ const messages = await this.fetchMessages();+ if (messages.length === 0) {+ return null;+ }++ return this.buildMessage(messages);+ }++ public async fetchDataFromEvent(+ event: Event+ ): Promise<SynchronizerOutputMessage<SyncMessagesInput> | null> {+ if (!this.shouldFetch(event)) {+ return null;+ }++ const messages = await this.fetchMessages();+ if (messages.length === 0) {+ return null;+ }++ return this.buildMessage(messages);+ }++ private async fetchMessages() {+ if (this.status === 'fetching') {+ return [];+ }++ this.status = 'fetching';+ const messages = await database+ .selectFrom('messages')+ .selectAll()+ .where('root_id', '=', this.input.rootId)+ .where('version', '>', this.cursor)+ .orderBy('version', 'asc')+ .limit(20)+ .execute();++ this.status = 'pending';+ return messages;+ }++ private buildMessage(+ unsyncedMessages: SelectMessage[]+ ): SynchronizerOutputMessage<SyncMessagesInput> {+ const items: SyncMessageData[] = unsyncedMessages.map((message) => ({+ id: message.id,+ type: message.type,+ nodeId: message.node_id,+ parentId: message.parent_id,+ rootId: message.root_id,+ workspaceId: message.workspace_id,+ content: JSON.parse(message.content),+ createdAt: message.created_at.toISOString(),+ createdBy: message.created_by,+ updatedAt: message.updated_at?.toISOString() ?? null,+ updatedBy: message.updated_by ?? null,+ deletedAt: message.deleted_at?.toISOString() ?? null,+ deletedBy: message.deleted_by ?? null,+ version: message.version.toString(),+ }));++ return {+ type: 'synchronizer_output',+ userId: this.user.userId,+ id: this.id,+ items: items.map((item) => ({+ cursor: item.version,+ data: item,+ })),+ };+ }++ private shouldFetch(event: Event) {+ if (+ event.type === 'message_created' &&+ event.rootId === this.input.rootId+ ) {+ return true;+ }++ if (+ event.type === 'message_updated' &&+ event.rootId === this.input.rootId+ ) {+ return true;+ }++ if (+ event.type === 'message_deleted' &&+ event.rootId === this.input.rootId+ ) {+ return true;+ }++ return false;+ }+}diff --git a/apps/server/src/synchronizers/transactions.ts b/apps/server/src/synchronizers/transactions.tsnew file mode 100644index 00000000..e512c838--- /dev/null+++ b/apps/server/src/synchronizers/transactions.ts@@ -0,0 +1,145 @@+import {+ SynchronizerOutputMessage,+ SyncTransactionsInput,+ SyncTransactionData,+} from '@colanode/core';+import { encodeState } from '@colanode/crdt';++import { BaseSynchronizer } from '@/synchronizers/base';+import { Event } from '@/types/events';+import { database } from '@/data/database';+import { SelectTransaction } from '@/data/schema';++export class TransactionSynchronizer extends BaseSynchronizer<SyncTransactionsInput> {+ public async fetchData(): Promise<SynchronizerOutputMessage<SyncTransactionsInput> | null> {+ const transactions = await this.fetchTransactions();+ if (transactions.length === 0) {+ return null;+ }++ return this.buildMessage(transactions);+ }++ public async fetchDataFromEvent(+ event: Event+ ): Promise<SynchronizerOutputMessage<SyncTransactionsInput> | null> {+ if (!this.shouldFetch(event)) {+ return null;+ }++ const transactions = await this.fetchTransactions();+ if (transactions.length === 0) {+ return null;+ }++ return this.buildMessage(transactions);+ }++ private async fetchTransactions() {+ if (this.status === 'fetching') {+ return [];+ }++ this.status = 'fetching';+ const transactions = await database+ .selectFrom('transactions')+ .selectAll()+ .where('root_id', '=', this.input.rootId)+ .where('version', '>', this.cursor)+ .orderBy('version', 'asc')+ .limit(20)+ .execute();++ this.status = 'pending';+ return transactions;+ }++ private buildMessage(+ unsyncedTransactions: SelectTransaction[]+ ): SynchronizerOutputMessage<SyncTransactionsInput> {+ const items: SyncTransactionData[] = unsyncedTransactions.map(+ (transaction) => {+ if (transaction.operation === 'create' && transaction.data) {+ return {+ id: transaction.id,+ operation: 'create',+ nodeId: transaction.node_id,+ rootId: transaction.root_id,+ workspaceId: transaction.workspace_id,+ data: encodeState(transaction.data),+ createdAt: transaction.created_at.toISOString(),+ createdBy: transaction.created_by,+ serverCreatedAt: transaction.server_created_at.toISOString(),+ version: transaction.version.toString(),+ };+ }++ if (transaction.operation === 'update' && transaction.data) {+ return {+ id: transaction.id,+ operation: 'update',+ nodeId: transaction.node_id,+ rootId: transaction.root_id,+ workspaceId: transaction.workspace_id,+ data: encodeState(transaction.data),+ createdAt: transaction.created_at.toISOString(),+ createdBy: transaction.created_by,+ serverCreatedAt: transaction.server_created_at.toISOString(),+ version: transaction.version.toString(),+ };+ }++ if (transaction.operation === 'delete') {+ return {+ id: transaction.id,+ operation: 'delete',+ nodeId: transaction.node_id,+ rootId: transaction.root_id,+ workspaceId: transaction.workspace_id,+ createdAt: transaction.created_at.toISOString(),+ createdBy: transaction.created_by,+ serverCreatedAt: transaction.server_created_at.toISOString(),+ version: transaction.version.toString(),+ };+ }++ throw new Error('Unknown transaction type');+ }+ );++ return {+ type: 'synchronizer_output',+ userId: this.user.userId,+ id: this.id,+ items: items.map((item) => ({+ cursor: item.version,+ data: item,+ })),+ };+ }++ private shouldFetch(event: Event) {+ if (+ event.type === 'message_created' &&+ event.rootId === this.input.rootId+ ) {+ return true;+ }++ if (+ event.type === 'message_updated' &&+ event.rootId === this.input.rootId+ ) {+ return true;+ }++ if (+ event.type === 'message_deleted' &&+ event.rootId === this.input.rootId+ ) {+ return true;+ }++ return false;+ }+}diff --git a/apps/server/src/synchronizers/users.ts b/apps/server/src/synchronizers/users.tsnew file mode 100644index 00000000..89d3a7ce--- /dev/null+++ b/apps/server/src/synchronizers/users.ts@@ -0,0 +1,101 @@+import {+ SynchronizerOutputMessage,+ SyncUserData,+ SyncUsersInput,+} from '@colanode/core';++import { BaseSynchronizer } from '@/synchronizers/base';+import { Event } from '@/types/events';+import { database } from '@/data/database';+import { SelectUser } from '@/data/schema';++export class UserSynchronizer extends BaseSynchronizer<SyncUsersInput> {+ public async fetchData(): Promise<SynchronizerOutputMessage<SyncUsersInput> | null> {+ const users = await this.fetchUsers();+ if (users.length === 0) {+ return null;+ }++ return this.buildMessage(users);+ }++ public async fetchDataFromEvent(+ event: Event+ ): Promise<SynchronizerOutputMessage<SyncUsersInput> | null> {+ if (!this.shouldFetch(event)) {+ return null;+ }++ const users = await this.fetchUsers();+ if (users.length === 0) {+ return null;+ }++ return this.buildMessage(users);+ }++ private async fetchUsers() {+ if (this.status === 'fetching') {+ return [];+ }++ this.status = 'fetching';+ const users = await database+ .selectFrom('users')+ .selectAll()+ .where('workspace_id', '=', this.user.workspaceId)+ .where('version', '>', this.cursor)+ .orderBy('version', 'asc')+ .limit(50)+ .execute();++ this.status = 'pending';+ return users;+ }++ private buildMessage(+ unsyncedUsers: SelectUser[]+ ): SynchronizerOutputMessage<SyncUsersInput> {+ const items: SyncUserData[] = unsyncedUsers.map((user) => ({+ id: user.id,+ workspaceId: user.workspace_id,+ email: user.email,+ role: user.role,+ name: user.name,+ avatar: user.avatar,+ customName: user.custom_name,+ customAvatar: user.custom_avatar,+ createdAt: user.created_at.toISOString(),+ updatedAt: user.updated_at?.toISOString() ?? null,+ version: user.version.toString(),+ }));++ return {+ type: 'synchronizer_output',+ userId: this.user.userId,+ id: this.id,+ items: items.map((item) => ({+ cursor: item.version,+ data: item,+ })),+ };+ }++ private shouldFetch(event: Event) {+ if (+ event.type === 'user_created' &&+ event.workspaceId === this.user.workspaceId+ ) {+ return true;+ }++ if (+ event.type === 'user_updated' &&+ event.workspaceId === this.user.workspaceId+ ) {+ return true;+ }++ return false;+ }+}diff --git a/apps/server/src/types/users.ts b/apps/server/src/types/users.tsindex 44ae49d2..562314f3 100644--- a/apps/server/src/types/users.ts+++ b/apps/server/src/types/users.ts@@ -1,9 +1,6 @@-import { Message } from '@colanode/core';-export type ConnectedUser = {userId: string;workspaceId: string;accountId: string;deviceId: string;- sendMessage: (message: Message) => void;};diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex e67340d6..22197cdc 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -27,3 +27,4 @@ export * from './types/workspaces';export * from './lib/blocks';export * from './types/mutations';export * from './types/messages';+export * from './synchronizers';diff --git a/packages/core/src/synchronizers/collaborations.ts b/packages/core/src/synchronizers/collaborations.tsnew file mode 100644index 00000000..3224f94d--- /dev/null+++ b/packages/core/src/synchronizers/collaborations.ts@@ -0,0 +1,28 @@+import { NodeRole } from '../registry/core';++export type SyncCollaborationsInput = {+ type: 'collaborations';+};++export type SyncCollaborationData = {+ collaboratorId: string;+ nodeId: string;+ workspaceId: string;+ role: NodeRole;+ createdAt: string;+ createdBy: string;+ updatedAt: string | null;+ updatedBy: string | null;+ deletedAt: string | null;+ deletedBy: string | null;+ version: string;+};++declare module '@colanode/core' {+ interface SynchronizerMap {+ collaborations: {+ input: SyncCollaborationsInput;+ data: SyncCollaborationData;+ };+ }+}diff --git a/packages/core/src/synchronizers/files.ts b/packages/core/src/synchronizers/files.tsnew file mode 100644index 00000000..ff59220e--- /dev/null+++ b/packages/core/src/synchronizers/files.ts@@ -0,0 +1,36 @@+import { FileStatus, FileType } from '../types/files';++export type SyncFilesInput = {+ type: 'files';+ rootId: string;+};++export type SyncFileData = {+ id: string;+ type: FileType;+ parentId: string;+ rootId: string;+ workspaceId: string;+ name: string;+ originalName: string;+ mimeType: string;+ size: number;+ extension: string;+ status: FileStatus;+ createdAt: string;+ createdBy: string;+ updatedAt: string | null;+ updatedBy: string | null;+ deletedAt: string | null;+ deletedBy: string | null;+ version: string;+};++declare module '@colanode/core' {+ interface SynchronizerMap {+ files: {+ input: SyncFilesInput;+ data: SyncFileData;+ };+ }+}diff --git a/packages/core/src/synchronizers/index.ts b/packages/core/src/synchronizers/index.tsnew file mode 100644index 00000000..9233402a--- /dev/null+++ b/packages/core/src/synchronizers/index.ts@@ -0,0 +1,11 @@+export * from './messages';+export * from './transactions';+export * from './users';+export * from './files';+export * from './message-reactions';+export * from './collaborations';++// eslint-disable-next-line @typescript-eslint/no-empty-object-type+export interface SynchronizerMap {}++export type SynchronizerInput = SynchronizerMap[keyof SynchronizerMap]['input'];diff --git a/packages/core/src/synchronizers/message-reactions.ts b/packages/core/src/synchronizers/message-reactions.tsnew file mode 100644index 00000000..d6aa3f3d--- /dev/null+++ b/packages/core/src/synchronizers/message-reactions.ts@@ -0,0 +1,24 @@+export type SyncMessageReactionsInput = {+ type: 'message_reactions';+ rootId: string;+};++export type SyncMessageReactionData = {+ messageId: string;+ collaboratorId: string;+ reaction: string;+ rootId: string;+ workspaceId: string;+ createdAt: string;+ deletedAt: string | null;+ version: string;+};++declare module '@colanode/core' {+ interface SynchronizerMap {+ message_reactions: {+ input: SyncMessageReactionsInput;+ data: SyncMessageReactionData;+ };+ }+}diff --git a/packages/core/src/synchronizers/messages.ts b/packages/core/src/synchronizers/messages.tsnew file mode 100644index 00000000..85301331--- /dev/null+++ b/packages/core/src/synchronizers/messages.ts@@ -0,0 +1,32 @@+import { MessageContent, MessageType } from '../types/messages';++export type SyncMessagesInput = {+ type: 'messages';+ rootId: string;+};++export type SyncMessageData = {+ id: string;+ type: MessageType;+ parentId: string;+ nodeId: string;+ rootId: string;+ workspaceId: string;+ content: MessageContent;+ createdAt: string;+ createdBy: string;+ updatedAt: string | null;+ updatedBy: string | null;+ deletedAt: string | null;+ deletedBy: string | null;+ version: string;+};++declare module '@colanode/core' {+ interface SynchronizerMap {+ messages: {+ input: SyncMessagesInput;+ data: SyncMessageData;+ };+ }+}diff --git a/packages/core/src/synchronizers/transactions.ts b/packages/core/src/synchronizers/transactions.tsnew file mode 100644index 00000000..b7305b15--- /dev/null+++ b/packages/core/src/synchronizers/transactions.ts@@ -0,0 +1,56 @@+export type SyncTransactionsInput = {+ type: 'transactions';+ rootId: string;+};++export type SyncCreateTransactionData = {+ id: string;+ operation: 'create';+ nodeId: string;+ rootId: string;+ workspaceId: string;+ data: string;+ createdAt: string;+ createdBy: string;+ serverCreatedAt: string;+ version: string;+};++export type SyncUpdateTransactionData = {+ id: string;+ operation: 'update';+ nodeId: string;+ rootId: string;+ workspaceId: string;+ data: string;+ createdAt: string;+ createdBy: string;+ serverCreatedAt: string;+ version: string;+};++export type SyncDeleteTransactionData = {+ id: string;+ operation: 'delete';+ nodeId: string;+ rootId: string;+ workspaceId: string;+ createdAt: string;+ createdBy: string;+ serverCreatedAt: string;+ version: string;+};++export type SyncTransactionData =+ | SyncCreateTransactionData+ | SyncUpdateTransactionData+ | SyncDeleteTransactionData;++declare module '@colanode/core' {+ interface SynchronizerMap {+ transactions: {+ input: SyncTransactionsInput;+ data: SyncTransactionData;+ };+ }+}diff --git a/packages/core/src/synchronizers/users.ts b/packages/core/src/synchronizers/users.tsnew file mode 100644index 00000000..f6ff90d3--- /dev/null+++ b/packages/core/src/synchronizers/users.ts@@ -0,0 +1,28 @@+import { WorkspaceRole } from '../types/workspaces';++export type SyncUsersInput = {+ type: 'users';+};++export type SyncUserData = {+ id: string;+ workspaceId: string;+ email: string;+ name: string;+ avatar: string | null;+ role: WorkspaceRole;+ customName: string | null;+ customAvatar: string | null;+ createdAt: string;+ updatedAt: string | null;+ version: string;+};++declare module '@colanode/core' {+ interface SynchronizerMap {+ users: {+ input: SyncUsersInput;+ data: SyncUserData;+ };+ }+}diff --git a/packages/core/src/types/nodes.ts b/packages/core/src/types/nodes.tsindex 0854c36a..25421fb0 100644--- a/packages/core/src/types/nodes.ts+++ b/packages/core/src/types/nodes.ts@@ -1,5 +1,3 @@-import { ServerTransaction } from './sync';-import { NodeAttributes } from '../registry';export type NodeOutput = {@@ -15,7 +13,3 @@ export type NodeOutput = {updatedBy?: string | null;transactionId: string;};--export type GetTransactionsOutput = {- transactions: ServerTransaction[];-};diff --git a/packages/core/src/types/sockets.ts b/packages/core/src/types/sockets.tsindex bbbb8c82..af7a4770 100644--- a/packages/core/src/types/sockets.ts+++ b/packages/core/src/types/sockets.ts@@ -1,59 +1,23 @@import { InteractionEvent } from './interactions';-import {- ServerCollaboration,- ServerFile,- ServerInteraction,- ServerMessage,- ServerMessageReaction,- ServerTransaction,- ServerUser,-} from './sync';-export type TransactionsBatchMessage = {- type: 'transactions_batch';- userId: string;- rootId: string;- transactions: ServerTransaction[];-};--export type CollaborationsBatchMessage = {- type: 'collaborations_batch';- userId: string;- collaborations: ServerCollaboration[];-};--export type InteractionsBatchMessage = {- type: 'interactions_batch';- userId: string;- rootId: string;- interactions: ServerInteraction[];-};+import { SynchronizerInput, SynchronizerMap } from '../synchronizers';-export type UsersBatchMessage = {- type: 'users_batch';+export type SynchronizerInputMessage = {+ type: 'synchronizer_input';userId: string;- users: ServerUser[];-};--export type FilesBatchMessage = {- type: 'files_batch';- userId: string;- rootId: string;- files: ServerFile[];-};--export type MessagesBatchMessage = {- type: 'messages_batch';- userId: string;- rootId: string;- messages: ServerMessage[];+ id: string;+ input: SynchronizerInput;+ cursor: string;};-export type MessageReactionsBatchMessage = {- type: 'message_reactions_batch';+export type SynchronizerOutputMessage<TInput extends SynchronizerInput> = {+ type: 'synchronizer_output';userId: string;- rootId: string;- messageReactions: ServerMessageReaction[];+ id: string;+ items: {+ cursor: string;+ data: SynchronizerMap[TInput['type']]['data'];+ }[];};export type SyncInteractionsMessage = {@@ -64,53 +28,6 @@ export type SyncInteractionsMessage = {events: InteractionEvent[];};-export type ConsumeUsersMessage = {- type: 'consume_users';- userId: string;- cursor: string;-};--export type ConsumeCollaborationsMessage = {- type: 'consume_collaborations';- userId: string;- cursor: string;-};--export type ConsumeInteractionsMessage = {- type: 'consume_interactions';- userId: string;- rootId: string;- cursor: string;-};--export type ConsumeTransactionsMessage = {- type: 'consume_transactions';- userId: string;- rootId: string;- cursor: string;-};--export type ConsumeFilesMessage = {- type: 'consume_files';- userId: string;- rootId: string;- cursor: string;-};--export type ConsumeMessagesMessage = {- type: 'consume_messages';- userId: string;- rootId: string;- cursor: string;-};--export type ConsumeMessageReactionsMessage = {- type: 'consume_message_reactions';- userId: string;- rootId: string;- cursor: string;-};-export type AccountUpdatedMessage = {type: 'account_updated';accountId: string;@@ -127,21 +44,9 @@ export type WorkspaceDeletedMessage = {};export type Message =- | TransactionsBatchMessage- | CollaborationsBatchMessage- | InteractionsBatchMessage- | UsersBatchMessage| SyncInteractionsMessage- | ConsumeUsersMessage- | ConsumeCollaborationsMessage- | ConsumeInteractionsMessage- | ConsumeTransactionsMessage| AccountUpdatedMessage| WorkspaceUpdatedMessage| WorkspaceDeletedMessage- | FilesBatchMessage- | ConsumeFilesMessage- | ConsumeMessagesMessage- | MessagesBatchMessage- | MessageReactionsBatchMessage- | ConsumeMessageReactionsMessage;+ | SynchronizerInputMessage+ | SynchronizerOutputMessage<SynchronizerInput>;diff --git a/packages/core/src/types/sync.ts b/packages/core/src/types/sync.tsindex 4169222a..1adfc7d2 100644--- a/packages/core/src/types/sync.ts+++ b/packages/core/src/types/sync.ts@@ -1,9 +1,4 @@import { InteractionAttributes } from './interactions';-import { WorkspaceRole } from './workspaces';-import { FileStatus, FileType } from './files';-import { MessageContent, MessageType } from './messages';--import { NodeRole } from '../registry/core';export type LocalTransaction =| LocalCreateTransaction@@ -39,72 +34,6 @@ export type LocalDeleteTransaction = {createdBy: string;};-export type ServerCreateTransaction = {- id: string;- operation: 'create';- nodeId: string;- rootId: string;- workspaceId: string;- data: string;- createdAt: string;- createdBy: string;- serverCreatedAt: string;- version: string;-};--export type ServerUpdateTransaction = {- id: string;- operation: 'update';- nodeId: string;- rootId: string;- workspaceId: string;- data: string;- createdAt: string;- createdBy: string;- serverCreatedAt: string;- version: string;-};--export type ServerDeleteTransaction = {- id: string;- operation: 'delete';- nodeId: string;- rootId: string;- workspaceId: string;- createdAt: string;- createdBy: string;- serverCreatedAt: string;- version: string;-};--export type ServerTransaction =- | ServerCreateTransaction- | ServerUpdateTransaction- | ServerDeleteTransaction;--export type ServerDeletedCollaboration = {- userId: string;- nodeId: string;- workspaceId: string;- createdAt: string;- version: string;-};--export type ServerCollaboration = {- collaboratorId: string;- nodeId: string;- rootId: string;- workspaceId: string;- role: NodeRole;- createdAt: string;- createdBy: string;- updatedAt: string | null;- updatedBy: string | null;- deletedAt: string | null;- deletedBy: string | null;- version: bigint;-};-export type ServerInteraction = {userId: string;nodeId: string;@@ -116,66 +45,3 @@ export type ServerInteraction = {serverUpdatedAt: string | null;version: string;};--export type ServerUser = {- id: string;- workspaceId: string;- email: string;- name: string;- avatar: string | null;- role: WorkspaceRole;- customName: string | null;- customAvatar: string | null;- createdAt: string;- updatedAt: string | null;- version: string;-};--export type ServerFile = {- id: string;- type: FileType;- parentId: string;- rootId: string;- workspaceId: string;- name: string;- originalName: string;- mimeType: string;- size: number;- extension: string;- status: FileStatus;- createdAt: string;- createdBy: string;- updatedAt: string | null;- updatedBy: string | null;- deletedAt: string | null;- deletedBy: string | null;- version: string;-};--export type ServerMessage = {- id: string;- type: MessageType;- parentId: string;- nodeId: string;- rootId: string;- workspaceId: string;- content: MessageContent;- createdAt: string;- createdBy: string;- updatedAt: string | null;- updatedBy: string | null;- deletedAt: string | null;- deletedBy: string | null;- version: string;-};--export type ServerMessageReaction = {- messageId: string;- collaboratorId: string;- reaction: string;- rootId: string;- workspaceId: string;- createdAt: string;- deletedAt: string | null;- version: string;-};