Skip to content

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

Refactor collaborations

Hakan Shehub4d42a6cf4b6
diff --git a/apps/desktop/src/main/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.ts
index 389833fc..55a67fa4 100644
--- a/apps/desktop/src/main/data/workspace/migrations.ts
+++ b/apps/desktop/src/main/data/workspace/migrations.ts
@@ -83,12 +83,12 @@ const createCollaborationsTable: Migration = {
up: async (db) => {
await db.schema
.createTable('collaborations')
- .addColumn('node_id', 'text', (col) => col.notNull())
- .addColumn('roles', 'text')
+ .addColumn('node_id', 'text', (col) => col.notNull().primaryKey())
+ .addColumn('role', 'text')
.addColumn('created_at', 'text', (col) => col.notNull())
.addColumn('updated_at', 'text')
+ .addColumn('deleted_at', 'text')
.addColumn('version', 'integer')
- .addPrimaryKeyConstraint('collaborations_pkey', ['node_id'])
.execute();
},
down: async (db) => {
diff --git a/apps/desktop/src/main/data/workspace/schema.ts b/apps/desktop/src/main/data/workspace/schema.ts
index ad4356bd..961360a0 100644
--- a/apps/desktop/src/main/data/workspace/schema.ts
+++ b/apps/desktop/src/main/data/workspace/schema.ts
@@ -69,9 +69,10 @@ export type UpdateTransaction = Updateable<TransactionTable>;
interface CollaborationTable {
node_id: ColumnType<string, string, never>;
- roles: ColumnType<string, string, string>;
+ role: ColumnType<string, string, string>;
created_at: ColumnType<string, string, never>;
updated_at: ColumnType<string | null, string | null, string | null>;
+ deleted_at: ColumnType<string | null, string | null, string | null>;
version: ColumnType<bigint, bigint, bigint>;
}
diff --git a/apps/desktop/src/main/jobs/sync-missing-nodes.ts b/apps/desktop/src/main/jobs/sync-missing-nodes.ts
deleted file mode 100644
index dbace58d..00000000
--- a/apps/desktop/src/main/jobs/sync-missing-nodes.ts
+++ /dev/null
@@ -1,95 +0,0 @@
-import { GetTransactionsOutput } from '@colanode/core';
-
-import { nodeService } from '@/main/services/node-service';
-import { fetchCursor, fetchWorkspaceCredentials } from '@/main/utils';
-import { createDebugger } from '@/main/debugger';
-import { databaseService } from '@/main/data/database-service';
-import { serverService } from '@/main/services/server-service';
-import { JobHandler } from '@/main/jobs';
-import { httpClient } from '@/shared/lib/http-client';
-
-export type SyncMissingNodesInput = {
- type: 'sync_missing_nodes';
- userId: string;
-};
-
-declare module '@/main/jobs' {
- interface JobMap {
- sync_missing_nodes: {
- input: SyncMissingNodesInput;
- };
- }
-}
-
-export class SyncMissingNodesJobHandler
- implements JobHandler<SyncMissingNodesInput>
-{
- public triggerDebounce = 100;
- public interval = 1000 * 60;
-
- private readonly debug = createDebugger('job:sync-missing-nodes');
-
- public async handleJob(input: SyncMissingNodesInput) {
- this.debug(`Syncing missing nodes for user ${input.userId}`);
-
- const workspaceDatabase = await databaseService.getWorkspaceDatabase(
- input.userId
- );
-
- const missingNodes = await workspaceDatabase
- .selectFrom('collaborations')
- .leftJoin('nodes', 'collaborations.node_id', 'nodes.id')
- .select('collaborations.node_id')
- .where('nodes.id', 'is', null)
- .execute();
-
- if (missingNodes.length === 0) {
- this.debug(`No missing nodes found for user ${input.userId}, skipping`);
- return;
- }
-
- const credentials = await fetchWorkspaceCredentials(input.userId);
- if (!credentials) {
- this.debug(
- `No workspace credentials found for user ${input.userId}, skipping`
- );
- return;
- }
-
- if (!serverService.isAvailable(credentials.serverDomain)) {
- this.debug(
- `Server ${credentials.serverDomain} is not available, skipping`
- );
- return;
- }
-
- for (const node of missingNodes) {
- try {
- this.debug(
- `Syncing missing node ${node.node_id} for user ${input.userId}`
- );
-
- const { data } = await httpClient.get<GetTransactionsOutput>(
- `/v1/workspaces/${credentials.workspaceId}/transactions/${node.node_id}`,
- {
- domain: credentials.serverDomain,
- token: credentials.token,
- }
- );
-
- const cursor = await fetchCursor(input.userId, 'transactions');
- await nodeService.replaceTransactions(
- input.userId,
- node.node_id,
- data.transactions,
- cursor
- );
- } catch (error) {
- this.debug(
- error,
- `Error syncing missing node ${node.node_id} for user ${input.userId}`
- );
- }
- }
- }
-}
diff --git a/apps/desktop/src/main/scheduler.ts b/apps/desktop/src/main/scheduler.ts
index 4949ca90..568d8895 100644
--- a/apps/desktop/src/main/scheduler.ts
+++ b/apps/desktop/src/main/scheduler.ts
@@ -12,7 +12,6 @@ import { RevertInvalidTransactionsJobHandler } from '@/main/jobs/revert-invalid-
import { SyncPendingTransactionsJobHandler } from '@/main/jobs/sync-pending-transactions';
import { SyncIncompleteTransactionsJobHandler } from '@/main/jobs/sync-incomplete-transactions';
import { SyncPendingInteractionsJobHandler } from '@/main/jobs/sync-pending-interactions';
-import { SyncMissingNodesJobHandler } from '@/main/jobs/sync-missing-nodes';
import { SyncDeletedTokensJobHandler } from '@/main/jobs/sync-deleted-tokens';
import { ConnectSocketJobHandler } from '@/main/jobs/connect-socket';
import { UploadFilesJobHandler } from '@/main/jobs/upload-files';
@@ -33,7 +32,6 @@ export const jobHandlerMap: JobHandlerMap = {
sync_pending_transactions: new SyncPendingTransactionsJobHandler(),
sync_incomplete_transactions: new SyncIncompleteTransactionsJobHandler(),
sync_pending_interactions: new SyncPendingInteractionsJobHandler(),
- sync_missing_nodes: new SyncMissingNodesJobHandler(),
sync_deleted_tokens: new SyncDeletedTokensJobHandler(),
connect_socket: new ConnectSocketJobHandler(),
upload_files: new UploadFilesJobHandler(),
@@ -246,11 +244,6 @@ class Scheduler {
userId,
});
- this.schedule({
- type: 'sync_missing_nodes',
- userId,
- });
-
this.schedule({
type: 'revert_invalid_transactions',
userId,
@@ -321,13 +314,6 @@ class Scheduler {
return true;
}
- if (
- state.input.type === 'sync_missing_nodes' &&
- state.input.userId === userId
- ) {
- return true;
- }
-
if (
state.input.type === 'revert_invalid_transactions' &&
state.input.userId === userId
diff --git a/apps/desktop/src/main/services/collaboration-service.ts b/apps/desktop/src/main/services/collaboration-service.ts
index b53a9ab4..951789da 100644
--- a/apps/desktop/src/main/services/collaboration-service.ts
+++ b/apps/desktop/src/main/services/collaboration-service.ts
@@ -1,7 +1,4 @@
-import {
- ServerCollaboration,
- ServerDeletedCollaboration,
-} from '@colanode/core';
+import { ServerCollaboration } from '@colanode/core';
import { createDebugger } from '@/main/debugger';
import { databaseService } from '@/main/data/database-service';
@@ -10,7 +7,7 @@ import { eventBus } from '@/shared/lib/event-bus';
class CollaborationService {
private readonly debug = createDebugger('service:collaboration');
- public async applyServerCollaboration(
+ public async syncServerCollaboration(
userId: string,
collaboration: ServerCollaboration
) {
@@ -25,22 +22,49 @@ class CollaborationService {
.insertInto('collaborations')
.values({
node_id: collaboration.nodeId,
- roles: JSON.stringify(collaboration.roles),
+ role: collaboration.role,
created_at: collaboration.createdAt,
+ updated_at: collaboration.updatedAt,
+ deleted_at: collaboration.deletedAt,
version: BigInt(collaboration.version),
})
.onConflict((oc) =>
oc
.columns(['node_id'])
.doUpdateSet({
- roles: JSON.stringify(collaboration.roles),
+ role: collaboration.role,
version: BigInt(collaboration.version),
updated_at: collaboration.updatedAt,
+ deleted_at: collaboration.deletedAt,
})
.where('version', '<', BigInt(collaboration.version))
)
.execute();
+ if (collaboration.deletedAt) {
+ await workspaceDatabase.transaction().execute(async (tx) => {
+ await tx
+ .deleteFrom('nodes')
+ .where('id', '=', collaboration.nodeId)
+ .execute();
+
+ await tx
+ .deleteFrom('transactions')
+ .where('node_id', '=', collaboration.nodeId)
+ .execute();
+
+ await tx
+ .deleteFrom('interaction_events')
+ .where('node_id', '=', collaboration.nodeId)
+ .execute();
+
+ await tx
+ .deleteFrom('interactions')
+ .where('node_id', '=', collaboration.nodeId)
+ .execute();
+ });
+ }
+
eventBus.publish({
type: 'collaboration_synced',
userId,
@@ -48,45 +72,6 @@ class CollaborationService {
createdAt: new Date(collaboration.createdAt),
});
}
-
- public async applyServerDeletedCollaboration(
- userId: string,
- deletedCollaboration: ServerDeletedCollaboration
- ) {
- this.debug(
- `Applying server deleted collaboration: ${deletedCollaboration.nodeId} for user ${userId}`
- );
-
- const workspaceDatabase =
- await databaseService.getWorkspaceDatabase(userId);
-
- await workspaceDatabase.transaction().execute(async (tx) => {
- await tx
- .deleteFrom('nodes')
- .where('id', '=', deletedCollaboration.nodeId)
- .execute();
-
- await tx
- .deleteFrom('transactions')
- .where('node_id', '=', deletedCollaboration.nodeId)
- .execute();
-
- await tx
- .deleteFrom('collaborations')
- .where('node_id', '=', deletedCollaboration.nodeId)
- .execute();
-
- await tx
- .deleteFrom('interaction_events')
- .where('node_id', '=', deletedCollaboration.nodeId)
- .execute();
-
- await tx
- .deleteFrom('interactions')
- .where('node_id', '=', deletedCollaboration.nodeId)
- .execute();
- });
- }
}
export const collaborationService = new CollaborationService();
diff --git a/apps/desktop/src/main/services/socket-connection.ts b/apps/desktop/src/main/services/socket-connection.ts
index 931196e7..7f30e4e3 100644
--- a/apps/desktop/src/main/services/socket-connection.ts
+++ b/apps/desktop/src/main/services/socket-connection.ts
@@ -60,8 +60,6 @@ export class SocketConnection {
if (message.type === 'transactions_batch') {
syncService.syncServerTransactions(message);
- } else if (message.type === 'deleted_collaborations_batch') {
- syncService.syncServerDeletedCollaborations(message);
} else if (message.type === 'collaborations_batch') {
syncService.syncServerCollaborations(message);
} else if (message.type === 'interactions_batch') {
diff --git a/apps/desktop/src/main/services/sync-consumer.ts b/apps/desktop/src/main/services/sync-consumer.ts
index 1cd3f2fd..b15ffe06 100644
--- a/apps/desktop/src/main/services/sync-consumer.ts
+++ b/apps/desktop/src/main/services/sync-consumer.ts
@@ -1,6 +1,5 @@
import {
CollaborationsBatchMessage,
- DeletedCollaborationsBatchMessage,
InitSyncConsumerMessage,
InteractionsBatchMessage,
SyncConsumerType,
@@ -121,13 +120,13 @@ export class SyncConsumer {
return;
}
- this.debug(`Syncing server collaborations for user ${message.userId}`);
+ 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.applyServerCollaboration(
+ await collaborationService.syncServerCollaboration(
message.userId,
collaboration
);
@@ -136,7 +135,7 @@ export class SyncConsumer {
} catch (error) {
this.debug(
error,
- `Error syncing server collaborations for user ${message.userId}`
+ `Error syncing server collaborators for user ${message.userId}`
);
} finally {
if (cursor) {
@@ -151,59 +150,6 @@ export class SyncConsumer {
}
}
- public async syncDeletedCollaborations(
- message: DeletedCollaborationsBatchMessage
- ) {
- if (this.type !== 'deleted_collaborations') {
- this.debug(
- `Syncing of server deleted collaborations not supported for consumer type ${this.type}, skipping`
- );
- return;
- }
-
- if (this.status === 'syncing') {
- this.debug(
- `Syncing of server deleted collaborations already in progress for user ${message.userId}, skipping`
- );
- return;
- }
-
- this.debug(
- `Syncing server deleted collaborations for user ${message.userId}`
- );
-
- this.status = 'syncing';
- let cursor: bigint | null = null;
- try {
- for (const deletedCollaboration of message.deletedCollaborations) {
- await collaborationService.applyServerDeletedCollaboration(
- message.userId,
- deletedCollaboration
- );
- cursor = BigInt(deletedCollaboration.version);
- }
- } catch (error) {
- this.debug(
- error,
- `Error syncing server deleted collaborations for user ${message.userId}`
- );
- } finally {
- this.debug(
- `Syncing of server deleted collaborations completed for user ${message.userId}`
- );
-
- if (cursor) {
- await updateCursor(message.userId, 'deleted_collaborations', cursor);
- this.cursor = cursor;
- this.status = 'idle';
- this.initConsumer(cursor);
- } else {
- this.status = 'idle';
- this.ping();
- }
- }
- }
-
public async syncInteractions(message: InteractionsBatchMessage) {
if (this.type !== 'interactions') {
this.debug(
diff --git a/apps/desktop/src/main/services/sync-service.ts b/apps/desktop/src/main/services/sync-service.ts
index 112f0ca2..460d359e 100644
--- a/apps/desktop/src/main/services/sync-service.ts
+++ b/apps/desktop/src/main/services/sync-service.ts
@@ -1,6 +1,5 @@
import {
CollaborationsBatchMessage,
- DeletedCollaborationsBatchMessage,
InteractionsBatchMessage,
SyncConsumerType,
TransactionsBatchMessage,
@@ -16,7 +15,6 @@ class SyncService {
public async initUserConsumers(accountId: string, userId: string) {
await this.createConsumer(accountId, userId, 'transactions');
- await this.createConsumer(accountId, userId, 'deleted_collaborations');
await this.createConsumer(accountId, userId, 'collaborations');
await this.createConsumer(accountId, userId, 'interactions');
await this.createConsumer(accountId, userId, 'users');
@@ -60,17 +58,6 @@ class SyncService {
}
}
- public async syncServerDeletedCollaborations(
- message: DeletedCollaborationsBatchMessage
- ) {
- const consumer = this.consumers.get(
- this.getConsumerKey(message.userId, 'deleted_collaborations')
- );
- if (consumer) {
- consumer.syncDeletedCollaborations(message);
- }
- }
-
public async syncServerCollaborations(message: CollaborationsBatchMessage) {
const consumer = this.consumers.get(
this.getConsumerKey(message.userId, 'collaborations')
diff --git a/apps/server/src/data/migrations.ts b/apps/server/src/data/migrations.ts
index b38e42ea..b8963aa3 100644
--- a/apps/server/src/data/migrations.ts
+++ b/apps/server/src/data/migrations.ts
@@ -174,6 +174,47 @@ const createTransactionsTable: Migration = {
},
};
+const createCollaborationsTable: Migration = {
+ up: async (db) => {
+ await sql`
+ CREATE SEQUENCE IF NOT EXISTS collaborations_version_sequence
+ START WITH 1000000000
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+ `.execute(db);
+
+ await db.schema
+ .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())
+ .addColumn('created_by', 'varchar(30)', (col) => col.notNull())
+ .addColumn('updated_at', 'timestamptz')
+ .addColumn('updated_by', 'varchar(30)')
+ .addColumn('deleted_at', 'timestamptz')
+ .addColumn('deleted_by', 'varchar(30)')
+ .addColumn('version', 'bigint', (col) =>
+ col.notNull().defaultTo(sql`nextval('collaborations_version_sequence')`)
+ )
+ .addPrimaryKeyConstraint('collaborations_pkey', [
+ 'node_id',
+ 'collaborator_id',
+ ])
+ .execute();
+ },
+ down: async (db) => {
+ await db.schema.dropTable('collaborations').execute();
+ await sql`DROP SEQUENCE IF EXISTS collaborations_version_sequence`.execute(
+ db
+ );
+ },
+};
+
const createNodePathsTable: Migration = {
up: async (db) => {
await db.schema
@@ -248,114 +289,6 @@ const createNodePathsTable: Migration = {
},
};
-const createCollaborationsTable: Migration = {
- up: async (db) => {
- // Create sequence first
- await sql`
- CREATE SEQUENCE IF NOT EXISTS collaborations_version_seq
- START WITH 1000000000
- INCREMENT BY 1
- NO MINVALUE
- NO MAXVALUE
- CACHE 1;
- `.execute(db);
-
- // Create table with version column
- await db.schema
- .createTable('collaborations')
- .addColumn('user_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('node_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('roles', 'jsonb', (col) => col.notNull().defaultTo('{}'))
- .addColumn('created_at', 'timestamptz', (col) => col.notNull())
- .addColumn('updated_at', 'timestamptz')
- .addColumn('version', 'bigint', (col) =>
- col.notNull().defaultTo(sql`nextval('collaborations_version_seq')`)
- )
- .addPrimaryKeyConstraint('collaborations_pkey', ['user_id', 'node_id'])
- .execute();
-
- // Add trigger to update version on update
- await sql`
- CREATE OR REPLACE FUNCTION update_collaboration_version() RETURNS TRIGGER AS $$
- BEGIN
- NEW.version = nextval('collaborations_version_seq');
- RETURN NEW;
- END;
- $$ LANGUAGE plpgsql;
-
- CREATE TRIGGER trg_update_collaboration_version
- BEFORE UPDATE ON collaborations
- FOR EACH ROW
- EXECUTE FUNCTION update_collaboration_version();
- `.execute(db);
- },
- down: async (db) => {
- await sql`
- DROP TRIGGER IF EXISTS trg_update_collaboration_version ON collaborations;
- DROP FUNCTION IF EXISTS update_collaboration_version();
- DROP SEQUENCE IF EXISTS collaborations_version_seq;
- `.execute(db);
- await db.schema.dropTable('collaborations').execute();
- },
-};
-
-const createDeletedCollaborationsTable: Migration = {
- up: async (db) => {
- await sql`
- CREATE SEQUENCE IF NOT EXISTS deleted_collaborations_version_seq
- START WITH 1000000000
- INCREMENT BY 1
- NO MINVALUE
- NO MAXVALUE
- CACHE 1;
- `.execute(db);
-
- await db.schema
- .createTable('deleted_collaborations')
- .addColumn('user_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('node_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())
- .addColumn('created_at', 'timestamptz', (col) => col.notNull())
- .addColumn('version', 'bigint', (col) =>
- col
- .notNull()
- .defaultTo(sql`nextval('deleted_collaborations_version_seq')`)
- )
- .addPrimaryKeyConstraint('deleted_collaborations_pkey', [
- 'user_id',
- 'node_id',
- ])
- .execute();
-
- await sql`
- CREATE OR REPLACE FUNCTION handle_collaboration_update() RETURNS TRIGGER AS $$
- BEGIN
- IF NEW.roles = '{}' THEN
- DELETE FROM collaborations WHERE user_id = NEW.user_id AND node_id = NEW.node_id;
- INSERT INTO deleted_collaborations (user_id, node_id, workspace_id, created_at)
- VALUES (NEW.user_id, NEW.node_id, NEW.workspace_id, NOW());
- ELSE
- DELETE FROM deleted_collaborations WHERE user_id = NEW.user_id AND node_id = NEW.node_id;
- END IF;
- RETURN NULL;
- END;
- $$ LANGUAGE plpgsql;
-
- CREATE TRIGGER after_collaboration_insert
- AFTER INSERT ON collaborations
- FOR EACH ROW EXECUTE FUNCTION handle_collaboration_update();
-
- CREATE TRIGGER after_collaboration_update
- AFTER UPDATE ON collaborations
- FOR EACH ROW EXECUTE FUNCTION handle_collaboration_update();
- `.execute(db);
- },
- down: async (db) => {
- await db.schema.dropTable('deleted_collaborations').execute();
- },
-};
-
const createUploadsTable: Migration = {
up: async (db) => {
await db.schema
@@ -438,9 +371,8 @@ export const databaseMigrations: Record<string, Migration> = {
'00004_create_users_table': createUsersTable,
'00005_create_nodes_table': createNodesTable,
'00006_create_transactions_table': createTransactionsTable,
- '00007_create_node_paths_table': createNodePathsTable,
- '00008_create_collaborations_table': createCollaborationsTable,
- '00009_create_deleted_collaborations_table': createDeletedCollaborationsTable,
- '00010_create_uploads_table': createUploadsTable,
- '00011_create_interactions_table': createInteractionsTable,
+ '00007_create_collaborations_table': createCollaborationsTable,
+ '00008_create_node_paths_table': createNodePathsTable,
+ '00009_create_uploads_table': createUploadsTable,
+ '00010_create_interactions_table': createInteractionsTable,
};
diff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.ts
index 15bd56a2..47818c7a 100644
--- a/apps/server/src/data/schema.ts
+++ b/apps/server/src/data/schema.ts
@@ -109,30 +109,18 @@ export type SelectNode = Selectable<NodeTable>;
export type CreateNode = Insertable<NodeTable>;
export type UpdateNode = Updateable<NodeTable>;
-interface TransactionTable {
- id: ColumnType<string, string, never>;
+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>;
- operation: ColumnType<string, string, never>;
- data: ColumnType<Uint8Array | null, Uint8Array | null, Uint8Array | null>;
+ role: ColumnType<NodeRole, NodeRole, NodeRole>;
created_at: ColumnType<Date, Date, never>;
created_by: ColumnType<string, string, never>;
- server_created_at: ColumnType<Date, Date, never>;
- version: ColumnType<bigint, never, never>;
-}
-
-export type SelectTransaction = Selectable<TransactionTable>;
-export type CreateTransaction = Insertable<TransactionTable>;
-export type UpdateTransaction = Updateable<TransactionTable>;
-
-interface CollaborationTable {
- user_id: ColumnType<string, string, never>;
- node_id: ColumnType<string, string, never>;
- workspace_id: ColumnType<string, string, never>;
- roles: JSONColumnType<Record<string, NodeRole>, string, string>;
- created_at: ColumnType<Date, Date, never>;
- updated_at: ColumnType<Date | null, Date | null, Date>;
+ updated_at: ColumnType<Date | null, Date | null, Date | null>;
+ updated_by: ColumnType<string | null, string | null, string | null>;
+ deleted_at: ColumnType<Date | null, Date | null, Date | null>;
+ deleted_by: ColumnType<string | null, string | null, string | null>;
version: ColumnType<bigint, never, never>;
}
@@ -140,17 +128,22 @@ export type SelectCollaboration = Selectable<CollaborationTable>;
export type CreateCollaboration = Insertable<CollaborationTable>;
export type UpdateCollaboration = Updateable<CollaborationTable>;
-interface DeletedCollaborationTable {
- user_id: ColumnType<string, never, never>;
- node_id: ColumnType<string, never, never>;
+interface TransactionTable {
+ id: ColumnType<string, string, never>;
+ node_id: ColumnType<string, string, never>;
+ root_id: ColumnType<string, string, never>;
workspace_id: ColumnType<string, string, never>;
- created_at: ColumnType<Date, never, never>;
+ operation: ColumnType<string, string, never>;
+ data: ColumnType<Uint8Array | null, Uint8Array | null, Uint8Array | null>;
+ created_at: ColumnType<Date, Date, never>;
+ created_by: ColumnType<string, string, never>;
+ server_created_at: ColumnType<Date, Date, never>;
version: ColumnType<bigint, never, never>;
}
-export type SelectDeletedCollaboration = Selectable<DeletedCollaborationTable>;
-export type CreateDeletedCollaboration = Insertable<DeletedCollaborationTable>;
-export type UpdateDeletedCollaboration = Updateable<DeletedCollaborationTable>;
+export type SelectTransaction = Selectable<TransactionTable>;
+export type CreateTransaction = Insertable<TransactionTable>;
+export type UpdateTransaction = Updateable<TransactionTable>;
interface NodePathTable {
ancestor_id: ColumnType<string, string, never>;
@@ -208,7 +201,6 @@ export interface DatabaseSchema {
nodes: NodeTable;
transactions: TransactionTable;
collaborations: CollaborationTable;
- deleted_collaborations: DeletedCollaborationTable;
node_paths: NodePathTable;
uploads: UploadTable;
interactions: InteractionTable;
diff --git a/apps/server/src/jobs/clean-node-data.ts b/apps/server/src/jobs/clean-node-data.ts
index 5d0912bd..53e4f6b6 100644
--- a/apps/server/src/jobs/clean-node-data.ts
+++ b/apps/server/src/jobs/clean-node-data.ts
@@ -142,8 +142,8 @@ const deleteChildren = async (
await trx
.updateTable('collaborations')
.set({
- roles: '{}',
- updated_at: new Date(),
+ deleted_at: new Date(),
+ deleted_by: userId,
})
.where('node_id', 'in', nodeIds)
.execute();
@@ -169,6 +169,7 @@ const deleteChildren = async (
type: 'node_deleted',
nodeId: node.id,
nodeType: node.type,
+ rootId: node.root_id,
workspaceId: workspaceId,
});
diff --git a/apps/server/src/lib/nodes.ts b/apps/server/src/lib/nodes.ts
index 10a3a74b..55e4e929 100644
--- a/apps/server/src/lib/nodes.ts
+++ b/apps/server/src/lib/nodes.ts
@@ -6,7 +6,6 @@ import {
NodeRole,
NodeType,
ServerCollaboration,
- ServerDeletedCollaboration,
ServerInteraction,
ServerTransaction,
ServerUser,
@@ -15,10 +14,9 @@ import { encodeState } from '@colanode/crdt';
import { database } from '@/data/database';
import {
- SelectCollaboration,
- SelectDeletedCollaboration,
SelectInteraction,
SelectNode,
+ SelectCollaboration,
SelectTransaction,
SelectUser,
} from '@/data/schema';
@@ -44,6 +42,7 @@ export const mapNode = (node: SelectNode): Node => {
return {
id: node.id,
parentId: node.parent_id,
+ rootId: node.root_id,
type: node.type as NodeType,
attributes: node.attributes,
createdAt: node.created_at.toISOString(),
@@ -104,32 +103,6 @@ export const mapTransaction = (
throw new Error('Unknown transaction type');
};
-export const mapDeletedCollaboration = (
- deletedCollaboration: SelectDeletedCollaboration
-): ServerDeletedCollaboration => {
- return {
- userId: deletedCollaboration.user_id,
- nodeId: deletedCollaboration.node_id,
- workspaceId: deletedCollaboration.workspace_id,
- createdAt: deletedCollaboration.created_at.toISOString(),
- version: deletedCollaboration.version.toString(),
- };
-};
-
-export const mapCollaboration = (
- collaboration: SelectCollaboration
-): ServerCollaboration => {
- return {
- userId: collaboration.user_id,
- nodeId: collaboration.node_id,
- workspaceId: collaboration.workspace_id,
- roles: collaboration.roles,
- createdAt: collaboration.created_at.toISOString(),
- updatedAt: collaboration.updated_at?.toISOString() ?? null,
- version: collaboration.version.toString(),
- };
-};
-
export const mapInteraction = (
interaction: SelectInteraction
): ServerInteraction => {
@@ -162,6 +135,25 @@ export const mapUser = (user: SelectUser): ServerUser => {
};
};
+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 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.ts
index 1e54e6d7..78e22e76 100644
--- a/apps/server/src/services/node-service.ts
+++ b/apps/server/src/services/node-service.ts
@@ -8,17 +8,18 @@ import {
registry,
} from '@colanode/core';
import { decodeState, YDoc } from '@colanode/crdt';
-import { sql, Transaction } from 'kysely';
+import { Transaction } from 'kysely';
import { cloneDeep } from 'lodash-es';
import { jobService } from '@/services/job-service';
import { database } from '@/data/database';
import {
- CreateCollaboration,
CreateNode,
+ CreateCollaboration,
CreateTransaction,
DatabaseSchema,
SelectUser,
+ SelectCollaboration,
} from '@/data/schema';
import { eventBus } from '@/lib/event-bus';
import { fetchNodeAncestors, mapNode } from '@/lib/nodes';
@@ -87,7 +88,7 @@ class NodeService {
};
try {
- const { createdNode, createdTransaction } =
+ const { createdNode, createdTransaction, createdCollaborations } =
await this.applyDatabaseCreateTransaction(
attributes,
createNode,
@@ -98,9 +99,19 @@ class NodeService {
type: 'node_created',
nodeId: input.nodeId,
nodeType: input.attributes.type,
+ rootId: input.rootId,
workspaceId: input.workspaceId,
});
+ for (const createdCollaboration of createdCollaborations) {
+ eventBus.publish({
+ type: 'collaboration_created',
+ collaboratorId: createdCollaboration.collaborator_id,
+ nodeId: input.nodeId,
+ workspaceId: input.workspaceId,
+ });
+ }
+
return {
node: createdNode,
transaction: createdTransaction,
@@ -177,65 +188,93 @@ class NodeService {
);
try {
- const { updatedNode, createdTransaction } = await database
- .transaction()
- .execute(async (trx) => {
- const updatedNode = await trx
- .updateTable('nodes')
- .returningAll()
- .set({
- attributes: attributesJson,
- updated_at: date,
- updated_by: input.userId,
- transaction_id: transactionId,
- })
- .where('id', '=', input.nodeId)
- .where('transaction_id', '=', node.transactionId)
- .executeTakeFirst();
-
- if (!updatedNode) {
- throw new Error('Failed to update node');
- }
-
- const createdTransaction = await trx
- .insertInto('transactions')
- .returningAll()
- .values({
- id: transactionId,
- node_id: input.nodeId,
- root_id: node.rootId,
- workspace_id: input.workspaceId,
- operation: 'update',
- data: update,
- created_at: date,
- created_by: input.userId,
- server_created_at: date,
- })
- .executeTakeFirst();
-
- if (!createdTransaction) {
- throw new Error('Failed to create transaction');
- }
-
- await this.applyCollaborationUpdates(
+ const {
+ updatedNode,
+ createdTransaction,
+ createdCollaborations,
+ updatedCollaborations,
+ } = await database.transaction().execute(async (trx) => {
+ const updatedNode = await trx
+ .updateTable('nodes')
+ .returningAll()
+ .set({
+ attributes: attributesJson,
+ updated_at: date,
+ updated_by: input.userId,
+ transaction_id: transactionId,
+ })
+ .where('id', '=', input.nodeId)
+ .where('transaction_id', '=', node.transactionId)
+ .executeTakeFirst();
+
+ if (!updatedNode) {
+ throw new Error('Failed to update node');
+ }
+
+ const createdTransaction = await trx
+ .insertInto('transactions')
+ .returningAll()
+ .values({
+ id: transactionId,
+ node_id: input.nodeId,
+ root_id: node.rootId,
+ workspace_id: input.workspaceId,
+ operation: 'update',
+ data: update,
+ created_at: date,
+ created_by: input.userId,
+ server_created_at: date,
+ })
+ .executeTakeFirst();
+
+ if (!createdTransaction) {
+ throw new Error('Failed to create transaction');
+ }
+
+ const { createdCollaborations, updatedCollaborations } =
+ await this.applyCollaboratorUpdates(
trx,
input.nodeId,
+ node.rootId,
+ input.userId,
+ input.workspaceId,
collaboratorChanges
);
- return {
- updatedNode,
- createdTransaction,
- };
- });
+ return {
+ updatedNode,
+ createdTransaction,
+ createdCollaborations,
+ updatedCollaborations,
+ };
+ });
eventBus.publish({
type: 'node_updated',
nodeId: input.nodeId,
nodeType: node.type,
+ rootId: node.rootId,
workspaceId: input.workspaceId,
});
+ for (const createdCollaboration of createdCollaborations) {
+ eventBus.publish({
+ type: 'collaboration_created',
+ collaboratorId: createdCollaboration.collaborator_id,
+ nodeId: input.nodeId,
+ workspaceId: input.workspaceId,
+ });
+ }
+
+ for (const updatedCollaboration of updatedCollaborations) {
+ eventBus.publish({
+ type: 'collaboration_updated',
+ collaboratorId: updatedCollaboration.collaborator_id,
+ nodeId: input.nodeId,
+ workspaceId: input.workspaceId,
+ });
+ }
+
return {
type: 'success',
output: {
@@ -302,7 +341,7 @@ class NodeService {
};
try {
- const { createdNode, createdTransaction } =
+ const { createdNode, createdTransaction, createdCollaborations } =
await this.applyDatabaseCreateTransaction(
attributes,
createNode,
@@ -313,9 +352,19 @@ class NodeService {
type: 'node_created',
nodeId: input.nodeId,
nodeType: attributes.type,
+ rootId: input.rootId,
workspaceId: context.workspaceId,
});
+ for (const createdCollaboration of createdCollaborations) {
+ eventBus.publish({
+ type: 'collaboration_created',
+ collaboratorId: createdCollaboration.collaborator_id,
+ nodeId: input.nodeId,
+ workspaceId: context.workspaceId,
+ });
+ }
+
return {
node: createdNode,
transaction: createdTransaction,
@@ -400,94 +449,94 @@ class NodeService {
);
try {
- const { updatedNode, createdTransaction } = await database
- .transaction()
- .execute(async (trx) => {
- const updatedNode = await trx
- .updateTable('nodes')
- .returningAll()
- .set({
- attributes: attributesJson,
- updated_at: input.createdAt,
- updated_by: input.userId,
- transaction_id: input.id,
- })
- .where('id', '=', input.nodeId)
- .where('transaction_id', '=', node.transactionId)
- .executeTakeFirst();
-
- if (!updatedNode) {
- throw new Error('Failed to update node');
- }
-
- const createdTransaction = await trx
- .insertInto('transactions')
- .returningAll()
- .values({
- id: input.id,
- node_id: input.nodeId,
- root_id: input.rootId,
- workspace_id: context.workspaceId,
- operation: 'update',
- data:
- typeof input.data === 'string'
- ? decodeState(input.data)
- : input.data,
- created_at: input.createdAt,
- created_by: input.userId,
- server_created_at: new Date(),
- })
- .executeTakeFirst();
-
- if (!createdTransaction) {
- throw new Error('Failed to create transaction');
- }
-
- await this.applyCollaborationUpdates(
+ const {
+ updatedNode,
+ createdTransaction,
+ createdCollaborations,
+ updatedCollaborations,
+ } = await database.transaction().execute(async (trx) => {
+ const updatedNode = await trx
+ .updateTable('nodes')
+ .returningAll()
+ .set({
+ attributes: attributesJson,
+ updated_at: input.createdAt,
+ updated_by: input.userId,
+ transaction_id: input.id,
+ })
+ .where('id', '=', input.nodeId)
+ .where('transaction_id', '=', node.transactionId)
+ .executeTakeFirst();
+
+ if (!updatedNode) {
+ throw new Error('Failed to update node');
+ }
+
+ const createdTransaction = await trx
+ .insertInto('transactions')
+ .returningAll()
+ .values({
+ id: input.id,
+ node_id: input.nodeId,
+ root_id: input.rootId,
+ workspace_id: context.workspaceId,
+ operation: 'update',
+ data:
+ typeof input.data === 'string'
+ ? decodeState(input.data)
+ : input.data,
+ created_at: input.createdAt,
+ created_by: input.userId,
+ server_created_at: new Date(),
+ })
+ .executeTakeFirst();
+
+ if (!createdTransaction) {
+ throw new Error('Failed to create transaction');
+ }
+
+ const { createdCollaborations, updatedCollaborations } =
+ await this.applyCollaboratorUpdates(
trx,
input.nodeId,
+ input.rootId,
+ input.userId,
+ context.workspaceId,
collaboratorChanges
);
- return {
- updatedNode,
- createdTransaction,
- };
- });
+ return {
+ updatedNode,
+ createdTransaction,
+ createdCollaborations,
+ updatedCollaborations,
+ };
+ });
eventBus.publish({
type: 'node_updated',
nodeId: input.nodeId,
nodeType: node.type,
+ rootId: node.rootId,
workspaceId: context.workspaceId,
});
- const addedCollaboratorIds = Object.keys(
- collaboratorChanges.addedCollaborators
- );
-
- if (addedCollaboratorIds.length > 0) {
- for (const userId of addedCollaboratorIds) {
- eventBus.publish({
- type: 'collaborator_added',
- userId,
- nodeId: input.nodeId,
- });
- }
+ for (const createdCollaboration of createdCollaborations) {
+ eventBus.publish({
+ type: 'collaboration_created',
+ collaboratorId: createdCollaboration.collaborator_id,
+ nodeId: input.nodeId,
+ workspaceId: context.workspaceId,
+ });
}
- const removedCollaboratorIds = Object.keys(
- collaboratorChanges.removedCollaborators
- );
-
- if (removedCollaboratorIds.length > 0) {
- for (const userId of removedCollaboratorIds) {
- eventBus.publish({
- type: 'collaborator_removed',
- userId,
- nodeId: input.nodeId,
- });
- }
+ for (const updatedCollaboration of updatedCollaborations) {
+ eventBus.publish({
+ type: 'collaboration_updated',
+ collaboratorId: updatedCollaboration.collaborator_id,
+ nodeId: input.nodeId,
+ workspaceId: context.workspaceId,
+ });
}
return {
@@ -526,9 +575,8 @@ class NodeService {
return null;
}
- const { deletedNode, createdTransaction } = await database
- .transaction()
- .execute(async (trx) => {
+ const { deletedNode, createdTransaction, updatedCollaborations } =
+ await database.transaction().execute(async (trx) => {
const deletedNode = await trx
.deleteFrom('nodes')
.returningAll()
@@ -563,18 +611,20 @@ class NodeService {
throw new Error('Failed to create transaction');
}
- await trx
+ const updatedCollaborations = await trx
.updateTable('collaborations')
.set({
- roles: '{}',
- updated_at: new Date(),
+ deleted_at: new Date(),
+ deleted_by: user.id,
})
+ .returningAll()
.where('node_id', '=', input.nodeId)
.execute();
return {
deletedNode,
createdTransaction,
+ updatedCollaborations,
};
});
@@ -582,9 +632,19 @@ class NodeService {
type: 'node_deleted',
nodeId: input.nodeId,
nodeType: node.type,
+ rootId: node.rootId,
workspaceId: user.workspace_id,
});
+ for (const updatedCollaboration of updatedCollaborations) {
+ eventBus.publish({
+ type: 'collaboration_updated',
+ collaboratorId: updatedCollaboration.collaborator_id,
+ nodeId: input.nodeId,
+ workspaceId: user.workspace_id,
+ });
+ }
+
await jobService.addJob({
type: 'clean_node_data',
nodeId: input.nodeId,
@@ -605,11 +665,13 @@ class NodeService {
const collaborationsToCreate: CreateCollaboration[] = Object.entries(
extractNodeCollaborators(attributes)
).map(([userId, role]) => ({
- user_id: userId,
+ collaborator_id: userId,
node_id: node.id,
+ root_id: node.root_id,
workspace_id: node.workspace_id,
- roles: JSON.stringify({ [node.id]: role }),
+ role,
created_at: new Date(),
+ created_by: transaction.created_by,
}));
return await database.transaction().execute(async (trx) => {
@@ -643,73 +705,80 @@ class NodeService {
if (createdCollaborations.length !== collaborationsToCreate.length) {
throw new Error('Failed to create collaborations');
}
+
+ return { createdNode, createdTransaction, createdCollaborations };
}
- await sql`
- INSERT INTO collaborations (user_id, node_id, workspace_id, roles, created_at)
- SELECT
- c.user_id,
- ${node.id} as node_id,
- ${node.workspace_id} as workspace_id,
- c.roles,
- ${new Date()} as created_at
- FROM collaborations as c
- WHERE c.node_id = ${attributes.parentId}
- ON CONFLICT (user_id, node_id) DO UPDATE
- SET
- roles = collaborations.roles || EXCLUDED.roles,
- updated_at = NOW()
- `.execute(trx);
-
- return { createdNode, createdTransaction };
+ return { createdNode, createdTransaction, createdCollaborations: [] };
});
}
- private async applyCollaborationUpdates(
+ private async applyCollaboratorUpdates(
transaction: Transaction<DatabaseSchema>,
nodeId: string,
+ rootId: string,
+ userId: string,
+ workspaceId: string,
updateResult: CollaboratorChangeResult
) {
- for (const [userId, role] of Object.entries(
+ const createdCollaborations: SelectCollaboration[] = [];
+ const updatedCollaborations: SelectCollaboration[] = [];
+
+ for (const [collaboratorId, role] of Object.entries(
updateResult.addedCollaborators
)) {
- const roles = JSON.stringify({ [nodeId]: role });
- await sql`
- INSERT INTO collaborations (user_id, node_id, workspace_id, roles, created_at)
- SELECT
- ${userId} as user_id,
- np.descendant_id as node_id,
- np.workspace_id as workspace_id,
- ${roles},
- ${new Date()} as created_at
- FROM node_paths as np
- WHERE np.ancestor_id = ${nodeId}
- ON CONFLICT (user_id, node_id) DO UPDATE
- SET
- roles = collaborations.roles || EXCLUDED.roles,
- updated_at = NOW()
- `.execute(transaction);
+ const createdCollaboration = await transaction
+ .insertInto('collaborations')
+ .returningAll()
+ .values({
+ collaborator_id: collaboratorId,
+ node_id: nodeId,
+ root_id: rootId,
+ workspace_id: workspaceId,
+ role,
+ created_at: new Date(),
+ created_by: userId,
+ })
+ .onConflict((oc) =>
+ oc.columns(['collaborator_id', 'node_id']).doUpdateSet({
+ role,
+ updated_at: new Date(),
+ updated_by: userId,
+ deleted_at: null,
+ deleted_by: null,
+ })
+ )
+ .executeTakeFirst();
+
+ if (!createdCollaboration) {
+ throw new Error('Failed to create collaboration');
+ }
+
+ createdCollaborations.push(createdCollaboration);
}
- for (const [userId, role] of Object.entries(
+ for (const [collaboratorId, role] of Object.entries(
updateResult.updatedCollaborators
)) {
- const roles = JSON.stringify({ [nodeId]: role });
- await sql`
- INSERT INTO collaborations (user_id, node_id, workspace_id, roles, created_at)
- SELECT
- ${userId} as user_id,
- np.descendant_id as node_id,
- np.workspace_id as workspace_id,
- ${roles},
- ${new Date()} as created_at
- FROM node_paths as np
- WHERE np.ancestor_id = ${nodeId}
- ON CONFLICT (user_id, node_id) DO UPDATE
- SET
- roles = collaborations.roles || EXCLUDED.roles,
- updated_at = NOW()
- `.execute(transaction);
+ const updatedCollaboration = await transaction
+ .updateTable('collaborations')
+ .returningAll()
+ .set({
+ role,
+ updated_at: new Date(),
+ updated_by: userId,
+ deleted_at: null,
+ deleted_by: null,
+ })
+ .where('collaborator_id', '=', collaboratorId)
+ .where('node_id', '=', nodeId)
+ .executeTakeFirst();
+
+ if (!updatedCollaboration) {
+ throw new Error('Failed to update collaboration');
+ }
+
+ updatedCollaborations.push(updatedCollaboration);
}
const removedCollaboratorIds = Object.keys(
@@ -717,20 +786,25 @@ class NodeService {
);
if (removedCollaboratorIds.length > 0) {
- await sql`
- UPDATE collaborations
- SET
- roles = collaborations.roles - ${nodeId},
- updated_at = NOW()
- WHERE user_id IN (${sql.join(removedCollaboratorIds, sql`, `)})
- AND node_id IN
- (
- SELECT np.descendant_id
- FROM node_paths as np
- WHERE np.ancestor_id = ${nodeId}
- )
- `.execute(transaction);
+ const removedCollaborations = await transaction
+ .updateTable('collaborations')
+ .returningAll()
+ .set({
+ deleted_at: new Date(),
+ deleted_by: userId,
+ })
+ .where('collaborator_id', 'in', removedCollaboratorIds)
+ .where('node_id', '=', nodeId)
+ .execute();
+
+ if (removedCollaborations.length !== removedCollaboratorIds.length) {
+ throw new Error('Failed to remove collaborations');
+ }
+
+ updatedCollaborations.push(...removedCollaborations);
}
+
+ return { createdCollaborations, updatedCollaborations };
}
private checkCollaboratorChanges(
diff --git a/apps/server/src/services/socket-connection.ts b/apps/server/src/services/socket-connection.ts
index 26b2d744..410f4a2d 100644
--- a/apps/server/src/services/socket-connection.ts
+++ b/apps/server/src/services/socket-connection.ts
@@ -1,14 +1,13 @@
import { WebSocket } from 'ws';
import {
- CollaborationsBatchMessage,
InitSyncConsumerMessage,
InteractionsBatchMessage,
Message,
TransactionsBatchMessage,
SyncConsumerType,
WorkspaceStatus,
- DeletedCollaborationsBatchMessage,
UsersBatchMessage,
+ CollaborationsBatchMessage,
} from '@colanode/core';
import { interactionService } from '@/services/interaction-service';
@@ -17,15 +16,14 @@ import { RequestAccount } from '@/types/api';
import { database } from '@/data/database';
import {
mapCollaboration,
- mapDeletedCollaboration,
mapInteraction,
mapTransaction,
mapUser,
} from '@/lib/nodes';
import {
AccountUpdatedEvent,
- CollaboratorAddedEvent,
- CollaboratorRemovedEvent,
+ CollaborationCreatedEvent,
+ CollaborationUpdatedEvent,
Event,
InteractionUpdatedEvent,
NodeCreatedEvent,
@@ -46,6 +44,7 @@ type SocketSyncConsumer = {
type SocketUser = {
userId: string;
workspaceId: string;
+ rootIds: string[];
consumers: Map<SyncConsumerType, SocketSyncConsumer>;
};
@@ -91,10 +90,10 @@ export class SocketConnection {
this.handleNodeUpdatedEvent(event);
} else if (event.type === 'node_deleted') {
this.handleNodeDeletedEvent(event);
- } else if (event.type === 'collaborator_added') {
- this.handleCollaboratorAddedEvent(event);
- } else if (event.type === 'collaborator_removed') {
- this.handleCollaboratorRemovedEvent(event);
+ } else if (event.type === 'collaboration_created') {
+ this.handleCollaborationCreatedEvent(event);
+ } else if (event.type === 'collaboration_updated') {
+ this.handleCollaborationUpdatedEvent(event);
} else if (event.type === 'interaction_updated') {
this.handleInteractionUpdatedEvent(event);
} else if (event.type === 'account_updated') {
@@ -151,8 +150,6 @@ export class SocketConnection {
) {
if (consumer.type === 'transactions') {
this.consumeTransactions(user, consumer);
- } else if (consumer.type === 'deleted_collaborations') {
- this.consumeDeletedCollaborations(user, consumer);
} else if (consumer.type === 'collaborations') {
this.consumeCollaborations(user, consumer);
} else if (consumer.type === 'interactions') {
@@ -170,22 +167,21 @@ export class SocketConnection {
return;
}
+ if (user.rootIds.length === 0) {
+ return;
+ }
+
consumer.fetching = true;
this.logger.trace(
`Checking for pending node transactions for ${this.account.id} and user ${user.userId} with cursor ${consumer.cursor}`
);
const unsyncedTransactions = await database
- .selectFrom('transactions as t')
- .leftJoin('collaborations as c', (join) =>
- join
- .on('c.user_id', '=', user.userId)
- .onRef('c.node_id', '=', 't.node_id')
- )
- .selectAll('t')
- .whereRef('c.node_id', '=', 't.node_id')
- .where('t.version', '>', BigInt(consumer.cursor))
- .orderBy('t.version', 'asc')
+ .selectFrom('transactions')
+ .selectAll()
+ .where('root_id', 'in', user.rootIds)
+ .where('version', '>', BigInt(consumer.cursor))
+ .orderBy('version', 'asc')
.limit(20)
.execute();
@@ -208,49 +204,6 @@ export class SocketConnection {
this.sendMessage(message);
}
- private async consumeDeletedCollaborations(
- user: SocketUser,
- consumer: SocketSyncConsumer
- ) {
- if (consumer.fetching) {
- return;
- }
-
- consumer.fetching = true;
- this.logger.trace(
- `Checking for pending deleted collaborations for ${this.account.id} and user ${user.userId} with cursor ${consumer.cursor}`
- );
-
- const unsyncedDeletedCollaborations = await database
- .selectFrom('deleted_collaborations as dc')
- .selectAll()
- .where('dc.user_id', '=', user.userId)
- .where('dc.version', '>', BigInt(consumer.cursor))
- .orderBy('dc.version', 'asc')
- .limit(50)
- .execute();
-
- if (unsyncedDeletedCollaborations.length === 0) {
- this.logger.trace(
- `No pending deleted collaborations found for ${this.account.id} and user ${user.userId} with cursor ${consumer.cursor}`
- );
- consumer.fetching = false;
- return;
- }
-
- const deletedCollaborations = unsyncedDeletedCollaborations.map(
- mapDeletedCollaboration
- );
- const message: DeletedCollaborationsBatchMessage = {
- type: 'deleted_collaborations_batch',
- userId: user.userId,
- deletedCollaborations,
- };
-
- user.consumers.delete(consumer.type);
- this.sendMessage(message);
- }
-
private async consumeCollaborations(
user: SocketUser,
consumer: SocketSyncConsumer
@@ -265,11 +218,11 @@ export class SocketConnection {
);
const unsyncedCollaborations = await database
- .selectFrom('collaborations as c')
+ .selectFrom('collaborations')
.selectAll()
- .where('c.user_id', '=', user.userId)
- .where('c.version', '>', BigInt(consumer.cursor))
- .orderBy('c.version', 'asc')
+ .where('collaborator_id', '=', user.userId)
+ .where('version', '>', BigInt(consumer.cursor))
+ .orderBy('version', 'asc')
.limit(50)
.execute();
@@ -306,16 +259,11 @@ export class SocketConnection {
);
const unsyncedInteractions = await database
- .selectFrom('interactions as i')
- .leftJoin('collaborations as c', (join) =>
- join
- .on('c.user_id', '=', user.userId)
- .onRef('c.node_id', '=', 'i.node_id')
- )
- .whereRef('c.node_id', '=', 'i.node_id')
- .selectAll('i')
- .where('i.version', '>', BigInt(consumer.cursor))
- .orderBy('i.version', 'asc')
+ .selectFrom('interactions')
+ .selectAll()
+ .where('node_id', 'in', user.rootIds)
+ .where('version', '>', BigInt(consumer.cursor))
+ .orderBy('version', 'asc')
.limit(20)
.execute();
@@ -379,16 +327,15 @@ export class SocketConnection {
continue;
}
+ if (!user.rootIds.includes(event.rootId)) {
+ continue;
+ }
+
const transactionsConsumer = user.consumers.get('transactions');
if (transactionsConsumer) {
this.consumePendingSync(user, transactionsConsumer);
}
- const collaborationsConsumer = user.consumers.get('collaborations');
- if (collaborationsConsumer) {
- this.consumePendingSync(user, collaborationsConsumer);
- }
-
const interactionsConsumer = user.consumers.get('interactions');
if (interactionsConsumer) {
this.consumePendingSync(user, interactionsConsumer);
@@ -402,6 +349,10 @@ export class SocketConnection {
continue;
}
+ if (!user.rootIds.includes(event.rootId)) {
+ continue;
+ }
+
const transactionsConsumer = user.consumers.get('transactions');
if (transactionsConsumer) {
this.consumePendingSync(user, transactionsConsumer);
@@ -415,26 +366,29 @@ export class SocketConnection {
continue;
}
+ if (!user.rootIds.includes(event.rootId)) {
+ continue;
+ }
+
const transactionsConsumer = user.consumers.get('transactions');
if (transactionsConsumer) {
this.consumePendingSync(user, transactionsConsumer);
}
-
- const deletedCollaborationsConsumer = user.consumers.get(
- 'deleted_collaborations'
- );
- if (deletedCollaborationsConsumer) {
- this.consumePendingSync(user, deletedCollaborationsConsumer);
- }
}
}
- private async handleCollaboratorAddedEvent(event: CollaboratorAddedEvent) {
+ private async handleCollaborationCreatedEvent(
+ event: CollaborationCreatedEvent
+ ) {
for (const user of this.users.values()) {
- if (user.userId !== event.userId) {
+ if (user.userId !== event.collaboratorId) {
continue;
}
+ if (!user.rootIds.includes(event.nodeId)) {
+ user.rootIds.push(event.nodeId);
+ }
+
const collaborationsConsumer = user.consumers.get('collaborations');
if (collaborationsConsumer) {
this.consumePendingSync(user, collaborationsConsumer);
@@ -442,19 +396,21 @@ export class SocketConnection {
}
}
- private async handleCollaboratorRemovedEvent(
- event: CollaboratorRemovedEvent
+ private async handleCollaborationUpdatedEvent(
+ event: CollaborationUpdatedEvent
) {
for (const user of this.users.values()) {
- if (user.userId !== event.userId) {
+ if (user.userId !== event.collaboratorId) {
continue;
}
- const deletedCollaborationsConsumer = user.consumers.get(
- 'deleted_collaborations'
- );
- if (deletedCollaborationsConsumer) {
- this.consumePendingSync(user, deletedCollaborationsConsumer);
+ if (!user.rootIds.includes(event.nodeId)) {
+ user.rootIds.push(event.nodeId);
+ }
+
+ const collaborationsConsumer = user.consumers.get('collaborations');
+ if (collaborationsConsumer) {
+ this.consumePendingSync(user, collaborationsConsumer);
}
}
}
@@ -535,6 +491,17 @@ export class SocketConnection {
return null;
}
+ const collaborations = await database
+ .selectFrom('collaborations')
+ .select(['node_id'])
+ .where('collaborator_id', '=', userId)
+ .where('deleted_at', 'is', null)
+ .execute();
+
+ const rootIds = collaborations.map(
+ (collaboration) => collaboration.node_id
+ );
+
const addedSocketUser = this.users.get(userId);
if (addedSocketUser) {
return addedSocketUser;
@@ -544,6 +511,7 @@ export class SocketConnection {
const newSocketUser: SocketUser = {
userId,
workspaceId: user.workspace_id,
+ rootIds,
consumers: new Map(),
};
diff --git a/apps/server/src/types/events.ts b/apps/server/src/types/events.ts
index d501055b..e71725f1 100644
--- a/apps/server/src/types/events.ts
+++ b/apps/server/src/types/events.ts
@@ -4,6 +4,7 @@ export type NodeCreatedEvent = {
type: 'node_created';
nodeId: string;
nodeType: NodeType;
+ rootId: string;
workspaceId: string;
};
@@ -11,6 +12,7 @@ export type NodeUpdatedEvent = {
type: 'node_updated';
nodeId: string;
nodeType: NodeType;
+ rootId: string;
workspaceId: string;
};
@@ -18,19 +20,22 @@ export type NodeDeletedEvent = {
type: 'node_deleted';
nodeId: string;
nodeType: NodeType;
+ rootId: string;
workspaceId: string;
};
-export type CollaboratorAddedEvent = {
- type: 'collaborator_added';
- userId: string;
+export type CollaborationCreatedEvent = {
+ type: 'collaboration_created';
+ collaboratorId: string;
nodeId: string;
+ workspaceId: string;
};
-export type CollaboratorRemovedEvent = {
- type: 'collaborator_removed';
- userId: string;
+export type CollaborationUpdatedEvent = {
+ type: 'collaboration_updated';
+ collaboratorId: string;
nodeId: string;
+ workspaceId: string;
};
export type InteractionUpdatedEvent = {
@@ -84,8 +89,8 @@ export type Event =
| NodeCreatedEvent
| NodeUpdatedEvent
| NodeDeletedEvent
- | CollaboratorAddedEvent
- | CollaboratorRemovedEvent
+ | CollaborationCreatedEvent
+ | CollaborationUpdatedEvent
| InteractionUpdatedEvent
| UserCreatedEvent
| UserUpdatedEvent
diff --git a/packages/core/src/types/messages.ts b/packages/core/src/types/messages.ts
index 37799eee..3cf3ed34 100644
--- a/packages/core/src/types/messages.ts
+++ b/packages/core/src/types/messages.ts
@@ -1,7 +1,6 @@
import { InteractionEvent } from './interactions';
import {
ServerCollaboration,
- ServerDeletedCollaboration,
ServerInteraction,
ServerTransaction,
ServerUser,
@@ -14,12 +13,6 @@ export type TransactionsBatchMessage = {
transactions: ServerTransaction[];
};
-export type DeletedCollaborationsBatchMessage = {
- type: 'deleted_collaborations_batch';
- userId: string;
- deletedCollaborations: ServerDeletedCollaboration[];
-};
-
export type CollaborationsBatchMessage = {
type: 'collaborations_batch';
userId: string;
@@ -70,7 +63,6 @@ export type WorkspaceDeletedMessage = {
export type Message =
| TransactionsBatchMessage
| CollaborationsBatchMessage
- | DeletedCollaborationsBatchMessage
| InteractionsBatchMessage
| UsersBatchMessage
| SyncInteractionsMessage
diff --git a/packages/core/src/types/sync.ts b/packages/core/src/types/sync.ts
index 6b6f2b36..f5d921ab 100644
--- a/packages/core/src/types/sync.ts
+++ b/packages/core/src/types/sync.ts
@@ -104,13 +104,18 @@ export type ServerDeletedCollaboration = {
};
export type ServerCollaboration = {
- userId: string;
+ collaboratorId: string;
nodeId: string;
+ rootId: string;
workspaceId: string;
- roles: Record<string, NodeRole>;
+ role: NodeRole;
createdAt: string;
+ createdBy: string;
updatedAt: string | null;
- version: string;
+ updatedBy: string | null;
+ deletedAt: string | null;
+ deletedBy: string | null;
+ version: bigint;
};
export type ServerInteraction = {
@@ -142,6 +147,5 @@ export type ServerUser = {
export type SyncConsumerType =
| 'transactions'
| 'collaborations'
- | 'deleted_collaborations'
| 'interactions'
| 'users';