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
Treat files as a separate entity from nodes
Hakan Shehu69dce7b05297
diff --git a/apps/desktop/src/main/consumers/files.ts b/apps/desktop/src/main/consumers/files.tsnew file mode 100644index 00000000..e603201c--- /dev/null+++ b/apps/desktop/src/main/consumers/files.ts@@ -0,0 +1,78 @@+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/data/workspace/migrations.ts b/apps/desktop/src/main/data/workspace/migrations.tsindex d9211ab0..53b33a68 100644--- a/apps/desktop/src/main/data/workspace/migrations.ts+++ b/apps/desktop/src/main/data/workspace/migrations.ts@@ -69,9 +69,7 @@ const createTransactionsTable: Migration = {.addColumn('created_at', 'text', (col) => col.notNull()).addColumn('created_by', 'text', (col) => col.notNull()).addColumn('server_created_at', 'text')- .addColumn('retry_count', 'integer', (col) => col.defaultTo(0))- .addColumn('status', 'text', (col) => col.defaultTo('pending'))- .addColumn('version', 'integer')+ .addColumn('version', 'integer', (col) => col.notNull()).execute();},down: async (db) => {@@ -96,42 +94,67 @@ const createCollaborationsTable: Migration = {},};-const createDownloadsTable: Migration = {+const createFilesTable: Migration = {up: async (db) => {await db.schema- .createTable('downloads')- .addColumn('node_id', 'text', (col) =>- col.notNull().primaryKey().references('nodes.id').onDelete('cascade')- )- .addColumn('upload_id', 'text', (col) => col.notNull())+ .createTable('files')+ .addColumn('id', 'text', (col) => col.notNull().primaryKey())+ .addColumn('type', 'text', (col) => col.notNull())+ .addColumn('parent_id', 'text', (col) => col.notNull())+ .addColumn('root_id', 'text', (col) => col.notNull())+ .addColumn('name', 'text', (col) => col.notNull())+ .addColumn('original_name', 'text', (col) => col.notNull())+ .addColumn('mime_type', 'text', (col) => col.notNull())+ .addColumn('extension', 'text', (col) => col.notNull())+ .addColumn('size', 'integer', (col) => col.notNull()).addColumn('created_at', 'text', (col) => col.notNull())+ .addColumn('created_by', 'text', (col) => col.notNull()).addColumn('updated_at', 'text')- .addColumn('completed_at', 'text')- .addColumn('progress', 'integer', (col) => col.defaultTo(0))- .addColumn('retry_count', 'integer', (col) => col.defaultTo(0))+ .addColumn('updated_by', 'text')+ .addColumn('status', 'integer', (col) => col.notNull())+ .addColumn('version', 'integer').execute();},down: async (db) => {- await db.schema.dropTable('downloads').execute();+ await db.schema.dropTable('files').execute();},};-const createUploadsTable: Migration = {+const createFileStatesTable: Migration = {up: async (db) => {await db.schema- .createTable('uploads')- .addColumn('node_id', 'text', (col) =>- col.notNull().primaryKey().references('nodes.id').onDelete('cascade')- )- .addColumn('upload_id', 'text', (col) => col.notNull())+ .createTable('file_states')+ .addColumn('file_id', 'text', (col) => col.notNull().primaryKey())+ .addColumn('download_status', 'text', (col) => col.notNull())+ .addColumn('download_progress', 'integer', (col) => col.notNull())+ .addColumn('download_retries', 'integer', (col) => col.notNull())+ .addColumn('upload_status', 'text', (col) => col.notNull())+ .addColumn('upload_progress', 'integer', (col) => col.notNull())+ .addColumn('upload_retries', 'integer', (col) => col.notNull()).addColumn('created_at', 'text', (col) => col.notNull()).addColumn('updated_at', 'text')- .addColumn('progress', 'integer', (col) => col.defaultTo(0))- .addColumn('retry_count', 'integer', (col) => col.defaultTo(0)).execute();},down: async (db) => {- await db.schema.dropTable('uploads').execute();+ await db.schema.dropTable('file_states').execute();+ },+};++const createMutationsTable: Migration = {+ up: async (db) => {+ await db.schema+ .createTable('mutations')+ .addColumn('id', 'text', (col) => col.notNull().primaryKey())+ .addColumn('type', 'text', (col) => col.notNull())+ .addColumn('node_id', 'text', (col) => col.notNull())+ .addColumn('key', 'text', (col) => col.notNull())+ .addColumn('data', 'text', (col) => col.notNull())+ .addColumn('created_at', 'text', (col) => col.notNull())+ .addColumn('retries', 'integer', (col) => col.notNull())+ .execute();+ },+ down: async (db) => {+ await db.schema.dropTable('mutations').execute();},};@@ -295,12 +318,13 @@ export const workspaceDatabaseMigrations: Record<string, Migration> = {'00002_create_nodes_table': createNodesTable,'00003_create_transactions_table': createTransactionsTable,'00004_create_collaborations_table': createCollaborationsTable,- '00005_create_uploads_table': createUploadsTable,- '00006_create_downloads_table': createDownloadsTable,+ '00005_create_files_table': createFilesTable,+ '00006_create_file_states_table': createFileStatesTable,'00007_create_interactions_table': createInteractionsTable,'00008_create_interaction_events_table': createInteractionEventsTable,- '00009_create_node_paths_table': createNodePathsTable,- '00010_create_node_names_table': createNodeNamesTable,- '00011_create_node_texts_table': createNodeTextsTable,- '00012_create_cursors_table': createCursorsTable,+ '00009_create_mutations_table': createMutationsTable,+ '00010_create_node_paths_table': createNodePathsTable,+ '00011_create_node_names_table': createNodeNamesTable,+ '00012_create_node_texts_table': createNodeTextsTable,+ '00013_create_cursors_table': createCursorsTable,};diff --git a/apps/desktop/src/main/data/workspace/schema.ts b/apps/desktop/src/main/data/workspace/schema.tsindex 8cd12c12..b93b824e 100644--- a/apps/desktop/src/main/data/workspace/schema.ts+++ b/apps/desktop/src/main/data/workspace/schema.ts@@ -1,6 +1,15 @@-import { InteractionAttribute, NodeType, WorkspaceRole } from '@colanode/core';+import {+ FileStatus,+ FileType,+ InteractionAttribute,+ MutationType,+ NodeType,+ WorkspaceRole,+} from '@colanode/core';import { ColumnType, Insertable, Selectable, Updateable } from 'kysely';+import { DownloadStatus, UploadStatus } from '@/shared/types/files';+interface UserTable {id: ColumnType<string, string, never>;email: ColumnType<string, string, never>;@@ -53,9 +62,7 @@ interface TransactionTable {created_at: ColumnType<string, string, never>;created_by: ColumnType<string, string, never>;server_created_at: ColumnType<string | null, string | null, string | null>;- retry_count: ColumnType<number, number, number>;- status: ColumnType<string, string, string>;- version: ColumnType<bigint | null, bigint | null, bigint | null>;+ version: ColumnType<bigint, bigint, bigint>;}export type SelectTransaction = Selectable<TransactionTable>;@@ -75,33 +82,6 @@ export type SelectCollaboration = Selectable<CollaborationTable>;export type CreateCollaboration = Insertable<CollaborationTable>;export type UpdateCollaboration = Updateable<CollaborationTable>;-interface UploadTable {- node_id: ColumnType<string, string, never>;- upload_id: ColumnType<string, string, never>;- created_at: ColumnType<string, string, never>;- updated_at: ColumnType<string | null, string | null, string | null>;- progress: ColumnType<number, number, number>;- retry_count: ColumnType<number, number, number>;-}--export type SelectUpload = Selectable<UploadTable>;-export type CreateUpload = Insertable<UploadTable>;-export type UpdateUpload = Updateable<UploadTable>;--interface DownloadTable {- node_id: ColumnType<string, string, never>;- upload_id: ColumnType<string, string, never>;- created_at: ColumnType<string, string, never>;- updated_at: ColumnType<string | null, string | null, string | null>;- completed_at: ColumnType<string | null, string | null, string | null>;- progress: ColumnType<number, number, number>;- retry_count: ColumnType<number, number, number>;-}--export type SelectDownload = Selectable<DownloadTable>;-export type CreateDownload = Insertable<DownloadTable>;-export type UpdateDownload = Updateable<DownloadTable>;-interface InteractionTable {user_id: ColumnType<string, string, never>;node_id: ColumnType<string, string, never>;@@ -119,6 +99,44 @@ export type SelectInteraction = Selectable<InteractionTable>;export type CreateInteraction = Insertable<InteractionTable>;export type UpdateInteraction = Updateable<InteractionTable>;+interface FileTable {+ id: ColumnType<string, string, never>;+ type: ColumnType<FileType, FileType, FileType>;+ parent_id: ColumnType<string, string, string>;+ root_id: ColumnType<string, string, string>;+ name: ColumnType<string, string, string>;+ original_name: ColumnType<string, string, string>;+ mime_type: ColumnType<string, string, string>;+ extension: ColumnType<string, string, string>;+ size: ColumnType<number, number, number>;+ created_at: ColumnType<string, string, never>;+ created_by: ColumnType<string, string, never>;+ updated_at: ColumnType<string | null, string | null, string | null>;+ updated_by: ColumnType<string | null, string | null, string | null>;+ status: ColumnType<FileStatus, FileStatus, FileStatus>;+ version: ColumnType<bigint, bigint, bigint>;+}++export type SelectFile = Selectable<FileTable>;+export type CreateFile = Insertable<FileTable>;+export type UpdateFile = Updateable<FileTable>;++interface FileStateTable {+ file_id: ColumnType<string, string, never>;+ download_status: ColumnType<DownloadStatus, DownloadStatus, DownloadStatus>;+ download_progress: ColumnType<number, number, number>;+ download_retries: ColumnType<number, number, number>;+ upload_status: ColumnType<UploadStatus, UploadStatus, UploadStatus>;+ upload_progress: ColumnType<number, number, number>;+ upload_retries: ColumnType<number, number, number>;+ created_at: ColumnType<string, string, never>;+ updated_at: ColumnType<string | null, string | null, string>;+}++export type SelectFileState = Selectable<FileStateTable>;+export type CreateFileState = Insertable<FileStateTable>;+export type UpdateFileState = Updateable<FileStateTable>;+interface InteractionEventTable {node_id: ColumnType<string, string, never>;attribute: ColumnType<InteractionAttribute, string, string>;@@ -133,6 +151,20 @@ export type SelectInteractionEvent = Selectable<InteractionEventTable>;export type CreateInteractionEvent = Insertable<InteractionEventTable>;export type UpdateInteractionEvent = Updateable<InteractionEventTable>;+interface MutationTable {+ id: ColumnType<string, string, never>;+ type: ColumnType<MutationType, MutationType, never>;+ node_id: ColumnType<string, string, never>;+ key: ColumnType<string, string, string>;+ data: ColumnType<string, string, never>;+ created_at: ColumnType<string, string, never>;+ retries: ColumnType<number, number, number>;+}++export type SelectMutation = Selectable<MutationTable>;+export type CreateMutation = Insertable<MutationTable>;+export type UpdateMutation = Updateable<MutationTable>;+interface NodeNameTable {id: ColumnType<string, string, never>;name: ColumnType<string, string, string>;@@ -164,10 +196,11 @@ export interface WorkspaceDatabaseSchema {transactions: TransactionTable;node_paths: NodePathTable;collaborations: CollaborationTable;- uploads: UploadTable;- downloads: DownloadTable;interactions: InteractionTable;interaction_events: InteractionEventTable;+ files: FileTable;+ file_states: FileStateTable;+ mutations: MutationTable;node_names: NodeNameTable;node_texts: NodeTextTable;cursors: CursorTable;diff --git a/apps/desktop/src/main/jobs/revert-invalid-transactions.ts b/apps/desktop/src/main/jobs/revert-invalid-transactions.tsindex 85cf89a9..d1acaf98 100644--- a/apps/desktop/src/main/jobs/revert-invalid-transactions.ts+++ b/apps/desktop/src/main/jobs/revert-invalid-transactions.ts@@ -1,61 +1,61 @@-import { nodeService } from '@/main/services/node-service';-import { createDebugger } from '@/main/debugger';-import { databaseService } from '@/main/data/database-service';-import { JobHandler } from '@/main/jobs';-import { mapTransaction } from '@/main/utils';--export type RevertInvalidTransactionsInput = {- type: 'revert_invalid_transactions';- userId: string;-};--declare module '@/main/jobs' {- interface JobMap {- revert_invalid_transactions: {- input: RevertInvalidTransactionsInput;- };- }-}--export class RevertInvalidTransactionsJobHandler- implements JobHandler<RevertInvalidTransactionsInput>-{- public triggerDebounce = 100;- public interval = 1000 * 60 * 5;-- private readonly debug = createDebugger('job:revert-invalid-transactions');-- public async handleJob(input: RevertInvalidTransactionsInput) {- this.debug(`Reverting invalid transactions for user ${input.userId}`);-- const workspaceDatabase = await databaseService.getWorkspaceDatabase(- input.userId- );-- const invalidTransactions = await workspaceDatabase- .selectFrom('transactions')- .selectAll()- .where('status', '=', 'pending')- .where('retry_count', '>=', 10)- .execute();-- if (invalidTransactions.length === 0) {- this.debug(- `No invalid transactions found for user ${input.userId}, skipping`- );- return;- }-- for (const transactionRow of invalidTransactions) {- const transaction = mapTransaction(transactionRow);-- if (transaction.operation === 'create') {- await nodeService.revertCreateTransaction(input.userId, transaction);- } else if (transaction.operation === 'update') {- await nodeService.revertUpdateTransaction(input.userId, transaction);- } else if (transaction.operation === 'delete') {- await nodeService.revertDeleteTransaction(input.userId, transaction);- }- }- }-}+// import { nodeService } from '@/main/services/node-service';+// import { createDebugger } from '@/main/debugger';+// import { databaseService } from '@/main/data/database-service';+// import { JobHandler } from '@/main/jobs';+// import { mapTransaction } from '@/main/utils';++// export type RevertInvalidTransactionsInput = {+// type: 'revert_invalid_transactions';+// userId: string;+// };++// declare module '@/main/jobs' {+// interface JobMap {+// revert_invalid_transactions: {+// input: RevertInvalidTransactionsInput;+// };+// }+// }++// export class RevertInvalidTransactionsJobHandler+// implements JobHandler<RevertInvalidTransactionsInput>+// {+// public triggerDebounce = 100;+// public interval = 1000 * 60 * 5;++// private readonly debug = createDebugger('job:revert-invalid-transactions');++// public async handleJob(input: RevertInvalidTransactionsInput) {+// this.debug(`Reverting invalid transactions for user ${input.userId}`);++// const workspaceDatabase = await databaseService.getWorkspaceDatabase(+// input.userId+// );++// const invalidTransactions = await workspaceDatabase+// .selectFrom('transactions')+// .selectAll()+// .where('status', '=', 'pending')+// .where('retry_count', '>=', 10)+// .execute();++// if (invalidTransactions.length === 0) {+// this.debug(+// `No invalid transactions found for user ${input.userId}, skipping`+// );+// return;+// }++// for (const transactionRow of invalidTransactions) {+// const transaction = mapTransaction(transactionRow);++// if (transaction.operation === 'create') {+// await nodeService.revertCreateTransaction(input.userId, transaction);+// } else if (transaction.operation === 'update') {+// await nodeService.revertUpdateTransaction(input.userId, transaction);+// } else if (transaction.operation === 'delete') {+// await nodeService.revertDeleteTransaction(input.userId, transaction);+// }+// }+// }+// }diff --git a/apps/desktop/src/main/jobs/sync-pending-mutations.ts b/apps/desktop/src/main/jobs/sync-pending-mutations.tsnew file mode 100644index 00000000..47a1b081--- /dev/null+++ b/apps/desktop/src/main/jobs/sync-pending-mutations.ts@@ -0,0 +1,114 @@+import { Mutation, SyncMutationsOutput } from '@colanode/core';++import { fetchWorkspaceCredentials, mapMutation } 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 SyncPendingMutationsInput = {+ type: 'sync_pending_mutations';+ userId: string;+};++declare module '@/main/jobs' {+ interface JobMap {+ sync_pending_mutations: {+ input: SyncPendingMutationsInput;+ };+ }+}++export class SyncPendingMutationsJobHandler+ implements JobHandler<SyncPendingMutationsInput>+{+ public triggerDebounce = 0;+ public interval = 1000 * 60;++ private readonly debug = createDebugger('job:sync-pending-mutations');++ public async handleJob(input: SyncPendingMutationsInput) {+ this.debug(`Sending local pending mutations for user ${input.userId}`);++ const workspaceDatabase = await databaseService.getWorkspaceDatabase(+ input.userId+ );++ const unsyncedMutations = await workspaceDatabase+ .selectFrom('mutations')+ .selectAll()+ .orderBy('id', 'asc')+ .limit(20)+ .execute();++ if (unsyncedMutations.length === 0) {+ return;+ }++ this.debug(+ `Sending ${unsyncedMutations.length} local pending mutations for user ${input.userId}`+ );++ const credentials = await fetchWorkspaceCredentials(input.userId);+ if (!credentials) {+ this.debug(+ `No workspace credentials found for user ${input.userId}, skipping sending local pending mutations`+ );+ return;+ }++ if (!serverService.isAvailable(credentials.serverDomain)) {+ this.debug(+ `Server ${credentials.serverDomain} is not available, skipping sending local pending mutations`+ );+ return;+ }++ const mutations: Mutation[] = unsyncedMutations.map(mapMutation);+ const { data } = await httpClient.post<SyncMutationsOutput>(+ `/v1/workspaces/${credentials.workspaceId}/mutations`,+ {+ mutations,+ },+ {+ domain: credentials.serverDomain,+ token: credentials.token,+ }+ );++ const syncedMutationIds: string[] = [];+ const unsyncedMutationIds: string[] = [];++ for (const result of data.results) {+ if (result.status === 'success') {+ syncedMutationIds.push(result.id);+ } else {+ unsyncedMutationIds.push(result.id);+ }+ }++ if (syncedMutationIds.length > 0) {+ this.debug(+ `Marking ${syncedMutationIds.length} local pending mutations as sent for user ${input.userId}`+ );++ await workspaceDatabase+ .deleteFrom('mutations')+ .where('id', 'in', syncedMutationIds)+ .execute();+ }++ if (unsyncedMutationIds.length > 0) {+ this.debug(+ `Marking ${unsyncedMutationIds.length} local pending mutations as failed for user ${input.userId}`+ );++ await workspaceDatabase+ .updateTable('mutations')+ .set((eb) => ({ retries: eb('retries', '+', 1) }))+ .where('id', 'in', unsyncedMutationIds)+ .execute();+ }+ }+}diff --git a/apps/desktop/src/main/jobs/sync-pending-transactions.ts b/apps/desktop/src/main/jobs/sync-pending-transactions.tsdeleted file mode 100644index 555ad9ea..00000000--- a/apps/desktop/src/main/jobs/sync-pending-transactions.ts+++ /dev/null@@ -1,119 +0,0 @@-import { SyncTransactionsOutput, LocalTransaction } from '@colanode/core';--import { fetchWorkspaceCredentials, mapTransaction } 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 SyncPendingTransactionsInput = {- type: 'sync_pending_transactions';- userId: string;-};--declare module '@/main/jobs' {- interface JobMap {- sync_pending_transactions: {- input: SyncPendingTransactionsInput;- };- }-}--export class SyncPendingTransactionsJobHandler- implements JobHandler<SyncPendingTransactionsInput>-{- public triggerDebounce = 0;- public interval = 1000 * 60;-- private readonly debug = createDebugger('job:sync-pending-transactions');-- public async handleJob(input: SyncPendingTransactionsInput) {- this.debug(`Sending local pending transactions for user ${input.userId}`);-- const workspaceDatabase = await databaseService.getWorkspaceDatabase(- input.userId- );-- const unsyncedTransactions = await workspaceDatabase- .selectFrom('transactions')- .selectAll()- .where('status', '=', 'pending')- .orderBy('id', 'asc')- .limit(20)- .execute();-- if (unsyncedTransactions.length === 0) {- return;- }-- this.debug(- `Sending ${unsyncedTransactions.length} local pending transactions for user ${input.userId}`- );-- const credentials = await fetchWorkspaceCredentials(input.userId);- if (!credentials) {- this.debug(- `No workspace credentials found for user ${input.userId}, skipping sending local pending transactions`- );- return;- }-- if (!serverService.isAvailable(credentials.serverDomain)) {- this.debug(- `Server ${credentials.serverDomain} is not available, skipping sending local pending transactions`- );- return;- }-- const transactions: LocalTransaction[] =- unsyncedTransactions.map(mapTransaction);- const { data } = await httpClient.post<SyncTransactionsOutput>(- `/v1/workspaces/${credentials.workspaceId}/transactions`,- {- transactions,- },- {- domain: credentials.serverDomain,- token: credentials.token,- }- );-- const syncedTransactionIds: string[] = [];- const unsyncedTransactionIds: string[] = [];-- for (const result of data.results) {- if (result.status === 'success') {- syncedTransactionIds.push(result.id);- } else {- unsyncedTransactionIds.push(result.id);- }- }-- if (syncedTransactionIds.length > 0) {- this.debug(- `Marking ${syncedTransactionIds.length} local pending transactions as sent for user ${input.userId}`- );-- await workspaceDatabase- .updateTable('transactions')- .set({ status: 'sent' })- .where('id', 'in', syncedTransactionIds)- .where('status', '=', 'pending')- .execute();- }-- if (unsyncedTransactionIds.length > 0) {- this.debug(- `Marking ${unsyncedTransactionIds.length} local pending transactions as failed for user ${input.userId}`- );-- await workspaceDatabase- .updateTable('transactions')- .set((eb) => ({ retry_count: eb('retry_count', '+', 1) }))- .where('id', 'in', unsyncedTransactionIds)- .where('status', '=', 'pending')- .execute();- }- }-}diff --git a/apps/desktop/src/main/mutations/files/file-create.ts b/apps/desktop/src/main/mutations/files/file-create.tsindex 6d90c465..e4c1966d 100644--- a/apps/desktop/src/main/mutations/files/file-create.ts+++ b/apps/desktop/src/main/mutations/files/file-create.ts@@ -1,18 +1,20 @@import {- extractFileType,- FileAttributes,+ CreateFileMutationData,+ FileStatus,generateId,IdType,} from '@colanode/core';import { fileService } from '@/main/services/file-service';-import { nodeService } from '@/main/services/node-service';import { MutationHandler } from '@/main/types';import {FileCreateMutationInput,FileCreateMutationOutput,} from '@/shared/mutations/files/file-create';import { MutationError } from '@/shared/mutations';+import { databaseService } from '@/main/data/database-service';+import { eventBus } from '@/shared/lib/event-bus';+import { mapFile } from '@/main/utils';export class FileCreateMutationHandlerimplements MutationHandler<FileCreateMutationInput>@@ -28,47 +30,112 @@ export class FileCreateMutationHandler);}+ const workspaceDatabase = await databaseService.getWorkspaceDatabase(+ input.userId+ );+const fileId = generateId(IdType.File);- const uploadId = generateId(IdType.Upload);fileService.copyFileToWorkspace(input.filePath,fileId,- uploadId,metadata.extension,input.userId);- const attributes: FileAttributes = {- type: 'file',- subtype: extractFileType(metadata.mimeType),+ const mutationData: CreateFileMutationData = {+ id: fileId,+ fileType: metadata.type,parentId: input.parentId,+ rootId: input.rootId,name: metadata.name,- fileName: metadata.name,+ originalName: metadata.name,extension: metadata.extension,- size: metadata.size,mimeType: metadata.mimeType,- uploadId,- uploadStatus: 'pending',+ size: metadata.size,+ createdAt: new Date().toISOString(),};- await nodeService.createNode(input.userId, {- id: fileId,- attributes,- upload: {- node_id: fileId,- created_at: new Date().toISOString(),- progress: 0,- retry_count: 0,- upload_id: uploadId,- },- download: {- node_id: fileId,- upload_id: uploadId,- created_at: new Date().toISOString(),- progress: 100,- retry_count: 0,- completed_at: new Date().toISOString(),+ const createdFile = await workspaceDatabase+ .transaction()+ .execute(async (tx) => {+ const createdFile = await tx+ .insertInto('files')+ .returningAll()+ .values({+ id: fileId,+ type: metadata.type,+ parent_id: input.parentId,+ root_id: input.rootId,+ name: metadata.name,+ original_name: metadata.name,+ mime_type: metadata.mimeType,+ size: metadata.size,+ extension: metadata.extension,+ created_at: new Date().toISOString(),+ created_by: input.userId,+ status: FileStatus.Pending,+ version: 0n,+ })+ .executeTakeFirst();++ if (!createdFile) {+ throw new Error('Failed to create file.');+ }++ await tx+ .insertInto('file_states')+ .values({+ file_id: fileId,+ created_at: new Date().toISOString(),+ download_progress: 100,+ download_status: 'completed',+ download_retries: 0,+ upload_progress: 0,+ upload_status: 'pending',+ upload_retries: 0,+ })+ .execute();++ await tx+ .insertInto('mutations')+ .values({+ id: generateId(IdType.Mutation),+ type: 'create_file',+ node_id: fileId,+ key: `create_${fileId}`,+ data: JSON.stringify(mutationData),+ created_at: new Date().toISOString(),+ retries: 0,+ })+ .execute();++ return createdFile;+ });++ if (!createdFile) {+ throw new Error('Failed to create file.');+ }++ eventBus.publish({+ type: 'file_created',+ userId: input.userId,+ file: mapFile(createdFile),+ });++ eventBus.publish({+ type: 'file_state_created',+ userId: input.userId,+ fileState: {+ fileId: fileId,+ downloadProgress: 100,+ downloadStatus: 'completed',+ downloadRetries: 0,+ uploadProgress: 10,+ uploadStatus: 'pending',+ uploadRetries: 0,+ createdAt: new Date().toISOString(),+ updatedAt: null,},});diff --git a/apps/desktop/src/main/mutations/files/file-download.ts b/apps/desktop/src/main/mutations/files/file-download.tsindex fdbc1b1a..542012a8 100644--- a/apps/desktop/src/main/mutations/files/file-download.ts+++ b/apps/desktop/src/main/mutations/files/file-download.ts@@ -1,6 +1,6 @@import { databaseService } from '@/main/data/database-service';import { MutationHandler } from '@/main/types';-import { mapNode } from '@/main/utils';+import { mapFileState } from '@/main/utils';import { eventBus } from '@/shared/lib/event-bus';import { MutationError } from '@/shared/mutations';import {@@ -18,62 +18,65 @@ export class FileDownloadMutationHandlerinput.userId);- const node = await workspaceDatabase- .selectFrom('nodes')+ const file = await workspaceDatabase+ .selectFrom('files').selectAll().where('id', '=', input.fileId).executeTakeFirst();- if (!node) {+ if (!file) {throw new MutationError('node_not_found','The file you are trying to download does not exist.');}- const file = mapNode(node);- if (file.attributes.type !== 'file') {- throw new MutationError(- 'invalid_attributes',- 'The node you are trying to download is not a file.'- );- }-- const download = await workspaceDatabase- .selectFrom('downloads')+ const existingFileState = await workspaceDatabase+ .selectFrom('file_states').selectAll()- .where('node_id', '=', input.fileId)+ .where('file_id', '=', input.fileId).executeTakeFirst();- if (download) {+ if (+ existingFileState &&+ existingFileState.download_status === 'completed'+ ) {return {success: true,};}- const createdAt = new Date();- await workspaceDatabase- .insertInto('downloads')+ const fileState = await workspaceDatabase+ .insertInto('file_states')+ .returningAll().values({- node_id: input.fileId,- upload_id: file.attributes.uploadId,- created_at: createdAt.toISOString(),- progress: 0,- retry_count: 0,+ file_id: input.fileId,+ download_status: 'pending',+ download_progress: 0,+ download_retries: 0,+ upload_status: 'none',+ upload_progress: 0,+ upload_retries: 0,+ created_at: new Date().toISOString(),})- .execute();+ .onConflict((oc) =>+ oc.doUpdateSet(() => ({+ download_status: 'pending',+ download_progress: 0,+ download_retries: 0,+ updated_at: new Date().toISOString(),+ }))+ )+ .executeTakeFirst();++ if (!fileState) {+ throw new Error('Failed to create file state.');+ }eventBus.publish({- type: 'download_created',+ type: 'file_state_created',userId: input.userId,- download: {- nodeId: node.id,- uploadId: file.attributes.uploadId,- createdAt: createdAt.toISOString(),- updatedAt: null,- progress: 0,- retryCount: 0,- },+ fileState: mapFileState(fileState),});return {diff --git a/apps/desktop/src/main/mutations/messages/message-create.ts b/apps/desktop/src/main/mutations/messages/message-create.tsindex ab49a5d5..abc4bb41 100644--- a/apps/desktop/src/main/mutations/messages/message-create.ts+++ b/apps/desktop/src/main/mutations/messages/message-create.ts@@ -1,8 +1,7 @@import {Block,EditorNodeTypes,- extractFileType,- FileAttributes,+ FileStatus,generateId,IdType,MessageAttributes,@@ -18,6 +17,10 @@ import {MessageCreateMutationOutput,} from '@/shared/mutations/messages/message-create';import { MutationError } from '@/shared/mutations';+import { CreateFile, CreateFileState } from '@/main/data/workspace/schema';+import { databaseService } from '@/main/data/database-service';+import { eventBus } from '@/shared/lib/event-bus';+import { mapFile, mapFileState } from '@/main/utils';export class MessageCreateMutationHandlerimplements MutationHandler<MessageCreateMutationInput>@@ -31,6 +34,8 @@ export class MessageCreateMutationHandlerconst messageId = generateId(IdType.Message);const createdAt = new Date().toISOString();const blocks = mapContentsToBlocks(messageId, messageContent, new Map());+ const files: CreateFile[] = [];+ const fileStates: CreateFileState[] = [];// check if there are nested nodes (files, pages, folders etc.)for (const block of blocks) {@@ -45,8 +50,6 @@ export class MessageCreateMutationHandler}const fileId = generateId(IdType.File);- const uploadId = generateId(IdType.Upload);-block.id = fileId;block.type = NodeTypes.File;block.attrs = null;@@ -54,42 +57,35 @@ export class MessageCreateMutationHandlerfileService.copyFileToWorkspace(path,fileId,- uploadId,metadata.extension,input.userId);- const fileAttributes: FileAttributes = {- type: 'file',- subtype: extractFileType(metadata.mimeType),- parentId: messageId,+ files.push({+ id: fileId,+ type: metadata.type,+ status: FileStatus.Pending,+ parent_id: messageId,+ root_id: messageId,name: metadata.name,- fileName: metadata.name,- mimeType: metadata.mimeType,- size: metadata.size,+ original_name: metadata.name,+ mime_type: metadata.mimeType,extension: metadata.extension,- uploadId,- uploadStatus: 'pending',- };+ size: metadata.size,+ created_at: createdAt,+ created_by: input.userId,+ version: 0n,+ });- inputs.push({- id: fileId,- attributes: fileAttributes,- download: {- node_id: fileId,- upload_id: uploadId,- created_at: createdAt,- progress: 100,- retry_count: 0,- completed_at: new Date().toISOString(),- },- upload: {- node_id: fileId,- upload_id: uploadId,- created_at: createdAt,- progress: 0,- retry_count: 0,- },+ fileStates.push({+ file_id: fileId,+ upload_status: 'pending',+ upload_progress: 0,+ upload_retries: 0,+ download_status: 'pending',+ download_progress: 0,+ download_retries: 0,+ created_at: createdAt,});}}@@ -139,6 +135,57 @@ export class MessageCreateMutationHandlerawait nodeService.createNode(input.userId, inputs);+ if (files.length > 0) {+ const workspaceDatabase = await databaseService.getWorkspaceDatabase(+ input.userId+ );++ const { createdFiles, createdFileStates } = await workspaceDatabase+ .transaction()+ .execute(async (tx) => {+ const createdFiles = await tx+ .insertInto('files')+ .returningAll()+ .values(files)+ .execute();++ if (createdFiles.length !== files.length) {+ throw new Error('Failed to create files.');+ }++ const createdFileStates = await tx+ .insertInto('file_states')+ .returningAll()+ .values(fileStates)+ .execute();++ if (createdFileStates.length !== fileStates.length) {+ throw new Error('Failed to create file states.');+ }++ return {+ createdFiles,+ createdFileStates,+ };+ });++ for (const file of createdFiles) {+ eventBus.publish({+ type: 'file_created',+ userId: input.userId,+ file: mapFile(file),+ });+ }++ for (const fileState of createdFileStates) {+ eventBus.publish({+ type: 'file_state_created',+ userId: input.userId,+ fileState: mapFileState(fileState),+ });+ }+ }+return {id: messageId,};diff --git a/apps/desktop/src/main/queries/files/download-get.ts b/apps/desktop/src/main/queries/files/download-get.tsdeleted file mode 100644index 4c76a8f0..00000000--- a/apps/desktop/src/main/queries/files/download-get.ts+++ /dev/null@@ -1,87 +0,0 @@-import { databaseService } from '@/main/data/database-service';-import { SelectDownload } from '@/main/data/workspace/schema';-import { ChangeCheckResult, QueryHandler } from '@/main/types';-import { mapDownload } from '@/main/utils';-import { DownloadGetQueryInput } from '@/shared/queries/files/download-get';-import { Event } from '@/shared/types/events';-import { Download } from '@/shared/types/nodes';--export class DownloadGetQueryHandler- implements QueryHandler<DownloadGetQueryInput>-{- public async handleQuery(- input: DownloadGetQueryInput- ): Promise<Download | null> {- const row = await this.fetchDownload(input);- return row ? mapDownload(row) : null;- }-- public async checkForChanges(- event: Event,- input: DownloadGetQueryInput,- _: Download | null- ): Promise<ChangeCheckResult<DownloadGetQueryInput>> {- if (- event.type === 'workspace_deleted' &&- event.workspace.userId === input.userId- ) {- return {- hasChanges: true,- result: null,- };- }-- if (- event.type === 'download_created' &&- event.userId === input.userId &&- event.download.nodeId === input.nodeId- ) {- return {- hasChanges: true,- result: event.download,- };- }-- if (- event.type === 'download_updated' &&- event.userId === input.userId &&- event.download.nodeId === input.nodeId- ) {- return {- hasChanges: true,- result: event.download,- };- }-- if (- event.type === 'download_deleted' &&- event.userId === input.userId &&- event.download.nodeId === input.nodeId- ) {- return {- hasChanges: true,- result: null,- };- }-- return {- hasChanges: false,- };- }-- private async fetchDownload(- input: DownloadGetQueryInput- ): Promise<SelectDownload | undefined> {- const workspaceDatabase = await databaseService.getWorkspaceDatabase(- input.userId- );-- const row = await workspaceDatabase- .selectFrom('downloads')- .selectAll()- .where('node_id', '=', input.nodeId)- .executeTakeFirst();-- return row;- }-}diff --git a/apps/desktop/src/main/queries/files/file-get.ts b/apps/desktop/src/main/queries/files/file-get.tsnew file mode 100644index 00000000..f96f85f6--- /dev/null+++ b/apps/desktop/src/main/queries/files/file-get.ts@@ -0,0 +1,123 @@+import { databaseService } from '@/main/data/database-service';+import { ChangeCheckResult, QueryHandler } from '@/main/types';+import { mapFile } from '@/main/utils';+import { FileGetQueryInput } from '@/shared/queries/files/file-get';+import { Event } from '@/shared/types/events';+import { FileWithState } from '@/shared/types/files';++export class FileGetQueryHandler implements QueryHandler<FileGetQueryInput> {+ public async handleQuery(+ input: FileGetQueryInput+ ): Promise<FileWithState | null> {+ return await this.fetchFile(input);+ }++ public async checkForChanges(+ event: Event,+ input: FileGetQueryInput,+ output: FileWithState | null+ ): Promise<ChangeCheckResult<FileGetQueryInput>> {+ if (+ event.type === 'workspace_deleted' &&+ event.workspace.userId === input.userId+ ) {+ return {+ hasChanges: true,+ result: null,+ };+ }++ if (+ event.type === 'file_created' &&+ event.userId === input.userId &&+ event.file.id === input.id+ ) {+ const output = await this.handleQuery(input);+ return {+ hasChanges: true,+ result: output,+ };+ }++ if (+ event.type === 'file_updated' &&+ event.userId === input.userId &&+ event.file.id === input.id+ ) {+ return {+ hasChanges: true,+ result: {+ ...event.file,+ downloadProgress: output?.downloadProgress ?? 0,+ downloadStatus: output?.downloadStatus ?? 'none',+ uploadProgress: output?.uploadProgress ?? 0,+ uploadStatus: output?.uploadStatus ?? 'none',+ },+ };+ }++ if (+ event.type === 'file_deleted' &&+ event.userId === input.userId &&+ event.file.id === input.id+ ) {+ return {+ hasChanges: true,+ result: null,+ };+ }++ return {+ hasChanges: false,+ };+ }++ private async fetchFile(+ input: FileGetQueryInput+ ): Promise<FileWithState | null> {+ const workspaceDatabase = await databaseService.getWorkspaceDatabase(+ input.userId+ );++ const file = await workspaceDatabase+ .selectFrom('files as f')+ .leftJoin('file_states as fs', 'f.id', 'fs.file_id')+ .select([+ 'f.id',+ 'f.type',+ 'f.parent_id',+ 'f.root_id',+ 'f.name',+ 'f.original_name',+ 'f.mime_type',+ 'f.extension',+ 'f.size',+ 'f.created_at',+ 'f.created_by',+ 'f.updated_at',+ 'f.updated_by',+ 'f.status',+ 'f.version',+ 'fs.download_status',+ 'fs.download_progress',+ 'fs.upload_status',+ 'fs.upload_progress',+ ])+ .where('f.id', '=', input.id)+ .executeTakeFirst();++ if (!file) {+ return null;+ }++ const fileWithState: FileWithState = {+ ...mapFile(file),+ downloadProgress: file.download_progress ?? 0,+ downloadStatus: file.download_status ?? 'none',+ uploadProgress: file.upload_progress ?? 0,+ uploadStatus: file.upload_status ?? 'none',+ };++ return fileWithState;+ }+}diff --git a/apps/desktop/src/main/queries/files/file-list.ts b/apps/desktop/src/main/queries/files/file-list.tsindex 35ff604e..d3674de0 100644--- a/apps/desktop/src/main/queries/files/file-list.ts+++ b/apps/desktop/src/main/queries/files/file-list.ts@@ -1,22 +1,21 @@-import { compareString, FileNode } from '@colanode/core';-import { databaseService } from '@/main/data/database-service';-import { SelectNode } from '@/main/data/workspace/schema';import { ChangeCheckResult, QueryHandler } from '@/main/types';-import { mapNode } from '@/main/utils';+import { mapFile } from '@/main/utils';import { FileListQueryInput } from '@/shared/queries/files/file-list';import { Event } from '@/shared/types/events';+import { FileWithState } from '@/shared/types/files';export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {- public async handleQuery(input: FileListQueryInput): Promise<FileNode[]> {- const files = await this.fetchFiles(input);- return this.buildFiles(files);+ public async handleQuery(+ input: FileListQueryInput+ ): Promise<FileWithState[]> {+ return await this.fetchFiles(input);}public async checkForChanges(event: Event,input: FileListQueryInput,- output: FileNode[]+ output: FileWithState[]): Promise<ChangeCheckResult<FileListQueryInput>> {if (event.type === 'workspace_deleted' &&@@ -29,10 +28,9 @@ export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {}if (- event.type === 'node_created' &&+ event.type === 'file_created' &&event.userId === input.userId &&- event.node.type === 'file' &&- event.node.parentId === input.parentId+ event.file.parentId === input.parentId) {const output = await this.handleQuery(input);return {@@ -42,16 +40,21 @@ export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {}if (- event.type === 'node_updated' &&+ event.type === 'file_updated' &&event.userId === input.userId &&- event.node.type === 'file' &&- event.node.parentId === input.parentId+ event.file.parentId === input.parentId) {- const file = output.find((file) => file.id === event.node.id);+ const file = output.find((file) => file.id === event.file.id);if (file) {const newResult = output.map((file) => {- if (file.id === event.node.id) {- return event.node as FileNode;+ if (file.id === event.file.id) {+ return {+ ...event.file,+ downloadProgress: file.downloadProgress,+ downloadStatus: file.downloadStatus,+ uploadProgress: file.uploadProgress,+ uploadStatus: file.uploadStatus,+ };}return file;@@ -65,17 +68,16 @@ export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {}if (- event.type === 'node_deleted' &&+ event.type === 'file_deleted' &&event.userId === input.userId &&- event.node.type === 'file' &&- event.node.parentId === input.parentId+ event.file.parentId === input.parentId) {- const file = output.find((file) => file.id === event.node.id);+ const file = output.find((file) => file.id === event.file.id);if (file) {- const newResult = await this.handleQuery(input);+ const output = await this.handleQuery(input);return {hasChanges: true,- result: newResult,+ result: output,};}}@@ -85,38 +87,52 @@ export class FileListQueryHandler implements QueryHandler<FileListQueryInput> {};}- private async fetchFiles(input: FileListQueryInput): Promise<SelectNode[]> {+ private async fetchFiles(+ input: FileListQueryInput+ ): Promise<FileWithState[]> {const workspaceDatabase = await databaseService.getWorkspaceDatabase(input.userId);const offset = (input.page - 1) * input.count;const files = await workspaceDatabase- .selectFrom('nodes')- .selectAll()- .where((eb) =>- eb.and([eb('parent_id', '=', input.parentId), eb('type', '=', 'file')])- )- .orderBy('id', 'asc')+ .selectFrom('files as f')+ .leftJoin('file_states as fs', 'f.id', 'fs.file_id')+ .select([+ 'f.id',+ 'f.type',+ 'f.parent_id',+ 'f.root_id',+ 'f.name',+ 'f.original_name',+ 'f.mime_type',+ 'f.extension',+ 'f.size',+ 'f.created_at',+ 'f.created_by',+ 'f.updated_at',+ 'f.updated_by',+ 'f.status',+ 'f.version',+ 'fs.download_status',+ 'fs.download_progress',+ 'fs.upload_status',+ 'fs.upload_progress',+ ])+ .where('f.parent_id', '=', input.parentId)+ .orderBy('f.id', 'asc').limit(input.count).offset(offset).execute();- return files;- }+ const filesWithState: FileWithState[] = files.map((file) => ({+ ...mapFile(file),+ downloadProgress: file.download_progress ?? 0,+ downloadStatus: file.download_status ?? 'none',+ uploadProgress: file.upload_progress ?? 0,+ uploadStatus: file.upload_status ?? 'none',+ }));- private buildFiles = (rows: SelectNode[]): FileNode[] => {- const nodes = rows.map(mapNode);- const files: FileNode[] = [];-- for (const node of nodes) {- if (node.type !== 'file') {- continue;- }-- files.push(node);- }-- return files.sort((a, b) => compareString(a.id, b.id));- };+ return filesWithState;+ }}diff --git a/apps/desktop/src/main/queries/index.ts b/apps/desktop/src/main/queries/index.tsindex 24e2d63c..dd33cc41 100644--- a/apps/desktop/src/main/queries/index.ts+++ b/apps/desktop/src/main/queries/index.ts@@ -1,8 +1,8 @@import { AccountGetQueryHandler } from '@/main/queries/accounts/account-get';import { AccountListQueryHandler } from '@/main/queries/accounts/accounts-list';-import { DownloadGetQueryHandler } from '@/main/queries/files/download-get';import { EmojisGetQueryHandler } from '@/main/queries/emojis/emojis-get';import { FileListQueryHandler } from '@/main/queries/files/file-list';+import { FileGetQueryHandler } from '@/main/queries/files/file-get';import { FileMetadataGetQueryHandler } from '@/main/queries/files/file-metadata-get';import { IconsGetQueryHandler } from '@/main/queries/icons/icons-get';import { MessageListQueryHandler } from '@/main/queries/messages/message-list';@@ -44,8 +44,8 @@ export const queryHandlerMap: QueryHandlerMap = {file_metadata_get: new FileMetadataGetQueryHandler(),account_get: new AccountGetQueryHandler(),workspace_get: new WorkspaceGetQueryHandler(),- download_get: new DownloadGetQueryHandler(),database_list: new DatabaseListQueryHandler(),record_search: new RecordSearchQueryHandler(),user_get: new UserGetQueryHandler(),+ file_get: new FileGetQueryHandler(),};diff --git a/apps/desktop/src/main/scheduler.ts b/apps/desktop/src/main/scheduler.tsindex 48f0592d..e98e42e1 100644--- a/apps/desktop/src/main/scheduler.ts+++ b/apps/desktop/src/main/scheduler.ts@@ -8,8 +8,8 @@ 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 { RevertInvalidTransactionsJobHandler } from '@/main/jobs/revert-invalid-transactions';-import { SyncPendingTransactionsJobHandler } from '@/main/jobs/sync-pending-transactions';+// import { RevertInvalidTransactionsJobHandler } from '@/main/jobs/revert-invalid-transactions';+import { SyncPendingMutationsJobHandler } from '@/main/jobs/sync-pending-mutations';import { SyncPendingInteractionsJobHandler } from '@/main/jobs/sync-pending-interactions';import { SyncDeletedTokensJobHandler } from '@/main/jobs/sync-deleted-tokens';import { ConnectSocketJobHandler } from '@/main/jobs/connect-socket';@@ -27,8 +27,8 @@ export const jobHandlerMap: JobHandlerMap = {sync_servers: new SyncServersJobHandler(),sync_account: new SyncAccountJobHandler(),init_sync_consumers: new InitSyncConsumersJobHandler(),- revert_invalid_transactions: new RevertInvalidTransactionsJobHandler(),- sync_pending_transactions: new SyncPendingTransactionsJobHandler(),+ // revert_invalid_transactions: new RevertInvalidTransactionsJobHandler(),+ sync_pending_mutations: new SyncPendingMutationsJobHandler(),sync_pending_interactions: new SyncPendingInteractionsJobHandler(),sync_deleted_tokens: new SyncDeletedTokensJobHandler(),connect_socket: new ConnectSocketJobHandler(),@@ -228,7 +228,7 @@ class Scheduler {}this.schedule({- type: 'sync_pending_transactions',+ type: 'sync_pending_mutations',userId,});@@ -237,10 +237,10 @@ class Scheduler {userId,});- this.schedule({- type: 'revert_invalid_transactions',- userId,- });+ // this.schedule({+ // type: 'revert_invalid_transactions',+ // userId,+ // });this.schedule({type: 'init_sync_consumers',@@ -287,7 +287,7 @@ class Scheduler {}if (- state.input.type === 'sync_pending_transactions' &&+ state.input.type === 'sync_pending_mutations' &&state.input.userId === userId) {return true;@@ -300,12 +300,12 @@ class Scheduler {return true;}- if (- state.input.type === 'revert_invalid_transactions' &&- state.input.userId === userId- ) {- return true;- }+ // if (+ // state.input.type === 'revert_invalid_transactions' &&+ // state.input.userId === userId+ // ) {+ // return true;+ // }if (state.input.type === 'upload_files' && state.input.userId === userId) {return true;@@ -353,9 +353,9 @@ class Scheduler {this.deleteWorkspaceJobs(event.workspace.userId);} else if (event.type === 'socket_connection_opened') {this.scheduleAccountWorkspacesJobs(event.accountId);- } else if (event.type === 'transaction_created') {+ } else if (event.type === 'mutation_created') {this.trigger({- type: 'sync_pending_transactions',+ type: 'sync_pending_mutations',userId: event.userId,});} else if (event.type === 'interaction_event_created') {@@ -363,16 +363,7 @@ class Scheduler {type: 'sync_pending_interactions',userId: event.userId,});- } else if (event.type === 'node_created' && event.node.type === 'file') {- this.trigger({- type: 'upload_files',- userId: event.userId,- });- this.trigger({- type: 'download_files',- userId: event.userId,- });- } else if (event.type === 'node_updated' && event.node.type === 'file') {+ } else if (event.type === 'file_state_created') {this.trigger({type: 'upload_files',userId: event.userId,@@ -381,21 +372,11 @@ class Scheduler {type: 'download_files',userId: event.userId,});- } else if (event.type === 'node_deleted' && event.node.type === 'file') {+ } else if (event.type === 'file_deleted') {this.trigger({type: 'clean_deleted_files',userId: event.userId,});- } else if (event.type === 'download_created') {- this.trigger({- type: 'download_files',- userId: event.userId,- });- } else if (event.type === 'upload_created') {- this.trigger({- type: 'upload_files',- userId: event.userId,- });}}}diff --git a/apps/desktop/src/main/services/file-service.ts b/apps/desktop/src/main/services/file-service.tsindex cd7be659..19370d7d 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,- FileAttributes,+ ServerFile,} from '@colanode/core';import axios from 'axios';import mime from 'mime-types';@@ -18,6 +18,8 @@ import { serverService } from '@/main/services/server-service';import {fetchWorkspaceCredentials,getWorkspaceFilesDirectoryPath,+ mapFile,+ mapFileState,} from '@/main/utils';import { eventBus } from '@/shared/lib/event-bus';import { httpClient } from '@/shared/lib/http-client';@@ -54,7 +56,6 @@ class FileService {public copyFileToWorkspace(filePath: string,fileId: string,- uploadId: string,fileExtension: string,userId: string): void {@@ -66,7 +67,7 @@ class FileService {const destinationFilePath = path.join(filesDir,- `${fileId}_${uploadId}${fileExtension}`+ `${fileId}${fileExtension}`);this.debug(@@ -123,9 +124,9 @@ class FileService {await databaseService.getWorkspaceDatabase(userId);const uploads = await workspaceDatabase- .selectFrom('uploads')+ .selectFrom('file_states').selectAll()- .where('progress', '=', 0)+ .where('upload_status', '=', 'pending').execute();if (uploads.length === 0) {@@ -140,54 +141,40 @@ class FileService {const filesDir = getWorkspaceFilesDirectoryPath(userId);for (const upload of uploads) {- if (upload.retry_count >= 5) {+ if (upload.upload_retries >= 5) {await workspaceDatabase- .deleteFrom('uploads')- .where('node_id', '=', upload.node_id)+ .updateTable('file_states')+ .set({+ upload_status: 'failed',+ updated_at: new Date().toISOString(),+ })+ .where('file_id', '=', upload.file_id).execute();continue;}const file = await workspaceDatabase- .selectFrom('nodes')+ .selectFrom('files').selectAll()- .where('id', '=', upload.node_id)+ .where('id', '=', upload.file_id).executeTakeFirst();if (!file) {await workspaceDatabase- .deleteFrom('uploads')- .where('node_id', '=', upload.node_id)+ .deleteFrom('file_states')+ .where('file_id', '=', upload.file_id).execute();continue;}- const transactions = await workspaceDatabase- .selectFrom('transactions')- .selectAll()- .where('node_id', '=', upload.node_id)- .where('status', '=', 'pending')- .execute();-- if (transactions.length > 0) {- this.debug(- `Transactions found for node ${upload.node_id}, skipping upload until transactions are completed`- );- continue;- }-- const attributes: FileAttributes = JSON.parse(file.attributes);- const filePath = path.join(- filesDir,- `${upload.node_id}_${upload.upload_id}${attributes.extension}`- );+ const filePath = path.join(filesDir, `${file.id}${file.extension}`);if (!fs.existsSync(filePath)) {await workspaceDatabase- .deleteFrom('uploads')- .where('node_id', '=', upload.node_id)+ .deleteFrom('file_states')+ .where('file_id', '=', file.id).execute();continue;@@ -199,10 +186,9 @@ class FileService {try {const { data } = await httpClient.post<CreateUploadOutput>(- `/v1/workspaces/${credentials.workspaceId}/uploads`,+ `/v1/workspaces/${credentials.workspaceId}/files`,{fileId: file.id,- uploadId: upload.upload_id,},{domain: credentials.serverDomain,@@ -214,13 +200,13 @@ class FileService {const fileStream = fs.createReadStream(filePath);await axios.put(presignedUrl, fileStream, {headers: {- 'Content-Type': attributes.mimeType,- 'Content-Length': attributes.size,+ 'Content-Type': file.mime_type,+ 'Content-Length': file.size,},});const { status } = await httpClient.put<CompleteUploadOutput>(- `/v1/workspaces/${credentials.workspaceId}/uploads/${data.uploadId}`,+ `/v1/workspaces/${credentials.workspaceId}/files/${file.id}`,{},{domain: credentials.serverDomain,@@ -233,15 +219,19 @@ class FileService {}await workspaceDatabase- .deleteFrom('uploads')- .where('node_id', '=', upload.node_id)+ .updateTable('file_states')+ .set({+ upload_status: 'completed',+ upload_progress: 100,+ updated_at: new Date().toISOString(),+ })+ .where('file_id', '=', file.id).execute();- } catch (error) {- console.log('error', error);+ } catch {await workspaceDatabase- .updateTable('uploads')- .set((eb) => ({ retry_count: eb('retry_count', '+', 1) }))- .where('node_id', '=', upload.node_id)+ .updateTable('file_states')+ .set((eb) => ({ upload_retries: eb('upload_retries', '+', 1) }))+ .where('file_id', '=', file.id).execute();}}@@ -252,9 +242,9 @@ class FileService {await databaseService.getWorkspaceDatabase(userId);const downloads = await workspaceDatabase- .selectFrom('downloads')+ .selectFrom('file_states').selectAll()- .where('progress', '=', 0)+ .where('download_status', '=', 'pending').execute();if (downloads.length === 0) {@@ -274,60 +264,53 @@ class FileService {for (const download of downloads) {const file = await workspaceDatabase- .selectFrom('nodes')+ .selectFrom('files').selectAll()- .where('id', '=', download.node_id)+ .where('id', '=', download.file_id).executeTakeFirst();if (!file) {- await workspaceDatabase- .deleteFrom('downloads')- .where('node_id', '=', download.node_id)- .execute();+ const deletedFileState = await workspaceDatabase+ .deleteFrom('file_states')+ .returningAll()+ .where('file_id', '=', download.file_id)+ .executeTakeFirst();++ if (!deletedFileState) {+ continue;+ }eventBus.publish({- type: 'download_deleted',+ type: 'file_state_deleted',userId,- download: {- nodeId: download.node_id,- uploadId: download.upload_id,- createdAt: download.created_at,- updatedAt: download.updated_at,- progress: download.progress,- retryCount: download.retry_count,- },+ fileState: mapFileState(deletedFileState),});continue;}- const attributes: FileAttributes = JSON.parse(file.attributes);- const filePath = path.join(- filesDir,- `${download.node_id}_${download.upload_id}${attributes.extension}`- );+ const filePath = path.join(filesDir, `${file.id}${file.extension}`);if (fs.existsSync(filePath)) {- await workspaceDatabase- .updateTable('downloads')+ const updatedFileState = await workspaceDatabase+ .updateTable('file_states')+ .returningAll().set({- progress: 100,- completed_at: new Date().toISOString(),+ download_status: 'completed',+ download_progress: 100,+ updated_at: new Date().toISOString(),})- .where('node_id', '=', download.node_id)- .execute();+ .where('file_id', '=', file.id)+ .executeTakeFirst();++ if (!updatedFileState) {+ continue;+ }eventBus.publish({- type: 'download_updated',+ type: 'file_state_updated',userId,- download: {- nodeId: download.node_id,- uploadId: download.upload_id,- createdAt: download.created_at,- updatedAt: download.updated_at,- progress: 100,- retryCount: download.retry_count,- },+ fileState: mapFileState(updatedFileState),});continue;@@ -339,7 +322,7 @@ class FileService {try {const { data } = await httpClient.get<CreateDownloadOutput>(- `/v1/workspaces/${credentials.workspaceId}/downloads/${download.node_id}`,+ `/v1/workspaces/${credentials.workspaceId}/downloads/${file.id}`,{domain: credentials.serverDomain,token: credentials.token,@@ -354,44 +337,42 @@ class FileService {response.data.pipe(fileStream);});- await workspaceDatabase- .updateTable('downloads')- .set({ progress: 100 })- .where('node_id', '=', download.node_id)- .execute();+ const updatedFileState = await workspaceDatabase+ .updateTable('file_states')+ .returningAll()+ .set({+ download_status: 'completed',+ download_progress: 100,+ updated_at: new Date().toISOString(),+ })+ .where('file_id', '=', file.id)+ .executeTakeFirst();++ if (!updatedFileState) {+ continue;+ }eventBus.publish({- type: 'download_updated',+ type: 'file_state_updated',userId,- download: {- nodeId: download.node_id,- uploadId: download.upload_id,- createdAt: download.created_at,- updatedAt: download.updated_at,- progress: 100,- retryCount: download.retry_count,- },+ fileState: mapFileState(updatedFileState),});- } catch (error) {- console.log('error', error);-- await workspaceDatabase- .updateTable('downloads')- .set((eb) => ({ retry_count: eb('retry_count', '+', 1) }))- .where('node_id', '=', download.node_id)- .execute();+ } catch {+ const updatedFileState = await workspaceDatabase+ .updateTable('file_states')+ .returningAll()+ .set((eb) => ({ download_retries: eb('download_retries', '+', 1) }))+ .where('file_id', '=', file.id)+ .executeTakeFirst();++ if (!updatedFileState) {+ continue;+ }eventBus.publish({- type: 'download_updated',+ type: 'file_state_updated',userId,- download: {- nodeId: download.node_id,- uploadId: download.upload_id,- createdAt: download.created_at,- updatedAt: download.updated_at,- progress: download.progress,- retryCount: download.retry_count + 1,- },+ fileState: mapFileState(updatedFileState),});}}@@ -418,23 +399,125 @@ class FileService {}const fileIds = Object.keys(fileIdMap);- const downloads = await workspaceDatabase- .selectFrom('downloads')- .select(['node_id'])- .where('node_id', 'in', fileIds)+ const fileStates = await workspaceDatabase+ .selectFrom('file_states')+ .select(['file_id'])+ .where('file_id', 'in', fileIds).execute();for (const fileId of fileIds) {- if (!downloads.some((d) => d.node_id === fileId)) {+ if (!fileStates.some((f) => f.file_id === fileId)) {const filePath = path.join(getWorkspaceFilesDirectoryPath(userId),fileIdMap[fileId]!);fs.rmSync(filePath, { force: true });++ const deletedFileState = await workspaceDatabase+ .deleteFrom('file_states')+ .returningAll()+ .where('file_id', '=', fileId)+ .executeTakeFirst();++ if (!deletedFileState) {+ continue;+ }++ eventBus.publish({+ type: 'file_state_deleted',+ userId,+ fileState: mapFileState(deletedFileState),+ });}}}}++ public async syncServerFile(userId: string, file: ServerFile): Promise<void> {+ const workspaceDatabase =+ await databaseService.getWorkspaceDatabase(userId);++ const existingFile = await workspaceDatabase+ .selectFrom('files')+ .selectAll()+ .where('id', '=', file.id)+ .executeTakeFirst();++ const version = BigInt(file.version);+ if (existingFile) {+ if (existingFile.version === version) {+ this.debug(`Server file ${file.id} is already synced`);+ return;+ }++ const updatedFile = await workspaceDatabase+ .updateTable('files')+ .returningAll()+ .set({+ name: file.name,+ original_name: file.originalName,+ mime_type: file.mimeType,+ extension: file.extension,+ size: file.size,+ parent_id: file.parentId,+ root_id: file.rootId,+ status: file.status,+ type: file.type,+ updated_at: file.updatedAt,+ updated_by: file.updatedBy,+ version,+ })+ .where('id', '=', file.id)+ .executeTakeFirst();++ if (!updatedFile) {+ return;+ }++ eventBus.publish({+ type: 'file_updated',+ userId,+ file: mapFile(updatedFile),+ });++ this.debug(`Server file ${file.id} has been synced`);+ return;+ }++ const createdFile = await workspaceDatabase+ .insertInto('files')+ .returningAll()+ .values({+ id: file.id,+ version,+ type: file.type,+ parent_id: file.parentId,+ root_id: file.rootId,+ name: file.name,+ original_name: file.originalName,+ mime_type: file.mimeType,+ extension: file.extension,+ size: file.size,+ created_at: file.createdAt,+ created_by: file.createdBy,+ updated_at: file.updatedAt,+ updated_by: file.updatedBy,+ status: file.status,+ })+ .executeTakeFirst();++ if (!createdFile) {+ return;+ }++ eventBus.publish({+ type: 'file_created',+ userId,+ file: mapFile(createdFile),+ });++ this.debug(`Server file ${file.id} has been synced`);+ }}export const fileService = new FileService();diff --git a/apps/desktop/src/main/services/node-service.ts b/apps/desktop/src/main/services/node-service.tsindex af676776..442d4b8b 100644--- a/apps/desktop/src/main/services/node-service.ts+++ b/apps/desktop/src/main/services/node-service.ts@@ -19,21 +19,12 @@ import { createDebugger } from '@/main/debugger';import { SelectWorkspace } from '@/main/data/app/schema';import { databaseService } from '@/main/data/database-service';import {- CreateDownload,- CreateUpload,- SelectDownload,+ SelectMutation,SelectNode,SelectTransaction,- SelectUpload,} from '@/main/data/workspace/schema';import { interactionService } from '@/main/services/interaction-service';-import {- fetchNodeAncestors,- mapDownload,- mapNode,- mapTransaction,- mapUpload,-} from '@/main/utils';+import { fetchNodeAncestors, mapNode, mapTransaction } from '@/main/utils';import { eventBus } from '@/shared/lib/event-bus';const UPDATE_RETRIES_LIMIT = 20;@@ -41,8 +32,6 @@ const UPDATE_RETRIES_LIMIT = 20;export type CreateNodeInput = {id: string;attributes: NodeAttributes;- upload?: CreateUpload;- download?: CreateDownload;};export type UpdateNodeResult =@@ -81,9 +70,7 @@ class NodeService {const inputs = Array.isArray(input) ? input : [input];const createdNodes: SelectNode[] = [];- const createdTransactions: SelectTransaction[] = [];- const createdUploads: SelectUpload[] = [];- const createdDownloads: SelectDownload[] = [];+ const createdMutations: SelectMutation[] = [];const workspaceDatabase =await databaseService.getWorkspaceDatabase(userId);@@ -159,8 +146,7 @@ class NodeService {data: update,created_at: createdAt,created_by: context.userId,- retry_count: 0,- status: 'pending',+ version: 0n,}).executeTakeFirst();@@ -168,35 +154,25 @@ class NodeService {throw new Error('Failed to create transaction');}- createdTransactions.push(createdTransaction);-- if (inputItem.upload) {- const createdUpload = await transaction- .insertInto('uploads')- .returningAll()- .values(inputItem.upload)- .executeTakeFirst();-- if (!createdUpload) {- throw new Error('Failed to create upload');- }+ const mutation = await transaction+ .insertInto('mutations')+ .returningAll()+ .values({+ id: generateId(IdType.Mutation),+ node_id: inputItem.id,+ type: 'apply_create_transaction',+ key: `transaction_${createdTransaction.id}`,+ data: JSON.stringify(mapTransaction(createdTransaction)),+ created_at: createdAt,+ retries: 0,+ })+ .executeTakeFirst();- createdUploads.push(createdUpload);+ if (!mutation) {+ throw new Error('Failed to create mutation');}- if (inputItem.download) {- const createdDownload = await transaction- .insertInto('downloads')- .returningAll()- .values(inputItem.download)- .executeTakeFirst();-- if (!createdDownload) {- throw new Error('Failed to create download');- }-- createdDownloads.push(createdDownload);- }+ createdMutations.push(mutation);if (name) {await transaction@@ -226,48 +202,13 @@ class NodeService {});}- for (const createdTransaction of createdTransactions) {- this.debug(- `Created transaction ${createdTransaction.id} for node ${createdTransaction.node_id} with operation ${createdTransaction.operation}`- );-- eventBus.publish({- type: 'transaction_created',- userId,- transaction: mapTransaction(createdTransaction),- });-- await interactionService.setInteraction(- userId,- createdTransaction.node_id,- {- attribute: 'lastReceivedTransactionId',- value: createdTransaction.id,- }- );- }-- for (const createdUpload of createdUploads) {- this.debug(- `Created upload ${createdUpload.upload_id} for node ${createdUpload.node_id}`- );-- eventBus.publish({- type: 'upload_created',- userId,- upload: mapUpload(createdUpload),- });- }-- for (const createdDownload of createdDownloads) {- this.debug(- `Created download ${createdDownload.upload_id} for node ${createdDownload.node_id}`- );+ for (const createdMutation of createdMutations) {+ this.debug(`Created mutation ${createdMutation.id}`);eventBus.publish({- type: 'download_created',+ type: 'mutation_created',userId,- download: mapDownload(createdDownload),+ mutationId: createdMutation.id,});}}@@ -352,7 +293,7 @@ class NodeService {const name = model.getName(nodeId, updatedAttributes);const text = model.getText(nodeId, updatedAttributes);- const { updatedNode, createdTransaction } = await workspaceDatabase+ const { updatedNode, createdMutation } = await workspaceDatabase.transaction().execute(async (trx) => {const updatedNode = await trx@@ -368,24 +309,44 @@ class NodeService {.where('transaction_id', '=', node.transactionId).executeTakeFirst();- if (updatedNode) {- const createdTransaction = await trx- .insertInto('transactions')- .returningAll()- .values({- id: transactionId,- node_id: nodeId,- root_id: node.rootId,- operation: 'update',- data: update,- created_at: updatedAt,- created_by: context.userId,- retry_count: 0,- status: 'pending',- })- .executeTakeFirst();+ if (!updatedNode) {+ return { updatedNode: undefined };+ }+ const createdTransaction = await trx+ .insertInto('transactions')+ .returningAll()+ .values({+ id: transactionId,+ node_id: nodeId,+ root_id: node.rootId,+ operation: 'update',+ data: update,+ created_at: updatedAt,+ created_by: context.userId,+ version: 0n,+ })+ .executeTakeFirst();++ if (!createdTransaction) {+ throw new Error('Failed to create transaction');+ }++ const createdMutation = await trx+ .insertInto('mutations')+ .returningAll()+ .values({+ id: generateId(IdType.Mutation),+ node_id: nodeId,+ type: 'apply_update_transaction',+ key: `transaction_${createdTransaction.id}`,+ data: JSON.stringify(mapTransaction(createdTransaction)),+ created_at: updatedAt,+ retries: 0,+ })+ .executeTakeFirst();- return { updatedNode, createdTransaction };+ if (!createdMutation) {+ throw new Error('Failed to create mutation');}if (name !== undefined) {@@ -411,8 +372,8 @@ class NodeService {}return {- updatedNode: undefined,- createdTransaction: undefined,+ updatedNode,+ createdMutation,};});@@ -430,27 +391,16 @@ class NodeService {this.debug(`Failed to update node ${nodeId}`);}- if (createdTransaction) {- this.debug(- `Created transaction ${createdTransaction.id} for node ${nodeId}`- );+ if (createdMutation) {+ this.debug(`Created mutation ${createdMutation.id} for node ${nodeId}`);eventBus.publish({- type: 'transaction_created',+ type: 'mutation_created',userId,- transaction: mapTransaction(createdTransaction),+ mutationId: createdMutation.id,});-- await interactionService.setInteraction(- userId,- createdTransaction.node_id,- {- attribute: 'lastReceivedTransactionId',- value: createdTransaction.id,- }- );} else {- this.debug(`Failed to create transaction for node ${nodeId}`);+ this.debug(`Failed to create mutation for node ${nodeId}`);}if (updatedNode) {@@ -486,7 +436,7 @@ class NodeService {throw new Error('Insufficient permissions');}- const { deletedNode, createdTransaction } = await workspaceDatabase+ const { deletedNode, createdMutation } = await workspaceDatabase.transaction().execute(async (trx) => {const deletedNode = await trx@@ -496,7 +446,7 @@ class NodeService {.executeTakeFirst();if (!deletedNode) {- return { deletedNode: undefined, createdTransaction: undefined };+ return { deletedNode: undefined };}await trx@@ -533,12 +483,29 @@ class NodeService {data: null,created_at: new Date().toISOString(),created_by: context.userId,- retry_count: 0,- status: 'pending',+ version: 0n,+ })+ .executeTakeFirst();++ if (!createdTransaction) {+ throw new Error('Failed to create transaction');+ }++ const createdMutation = await trx+ .insertInto('mutations')+ .returningAll()+ .values({+ id: generateId(IdType.Mutation),+ node_id: nodeId,+ type: 'apply_delete_transaction',+ key: `transaction_${createdTransaction.id}`,+ data: JSON.stringify(mapTransaction(createdTransaction)),+ created_at: new Date().toISOString(),+ retries: 0,}).executeTakeFirst();- return { deletedNode, createdTransaction };+ return { deletedNode, createdMutation };});if (deletedNode) {@@ -555,18 +522,16 @@ class NodeService {this.debug(`Failed to delete node ${nodeId}`);}- if (createdTransaction) {- this.debug(- `Created transaction ${createdTransaction.id} for node ${nodeId}`- );+ if (createdMutation) {+ this.debug(`Created mutation ${createdMutation.id} for node ${nodeId}`);eventBus.publish({- type: 'transaction_created',+ type: 'mutation_created',userId,- transaction: mapTransaction(createdTransaction),+ mutationId: createdMutation.id,});} else {- this.debug(`Failed to create transaction for node ${nodeId}`);+ this.debug(`Failed to create mutation for node ${nodeId}`);}}@@ -838,13 +803,12 @@ class NodeService {const version = BigInt(transaction.version);const existingTransaction = await workspaceDatabase.selectFrom('transactions')- .select(['id', 'status', 'version', 'server_created_at'])+ .select(['id', 'version', 'server_created_at']).where('id', '=', transaction.id).executeTakeFirst();if (existingTransaction) {if (- existingTransaction.status === 'synced' &&existingTransaction.version === version &&existingTransaction.server_created_at === transaction.serverCreatedAt) {@@ -857,7 +821,6 @@ class NodeService {await workspaceDatabase.updateTable('transactions').set({- status: 'synced',version,server_created_at: transaction.serverCreatedAt,})@@ -908,8 +871,6 @@ class NodeService {data: decodeState(transaction.data),created_at: transaction.createdAt,created_by: transaction.createdBy,- retry_count: 0,- status: createdNode ? 'synced' : 'incomplete',version,server_created_at: transaction.serverCreatedAt,})@@ -965,13 +926,12 @@ class NodeService {const version = BigInt(transaction.version);const existingTransaction = await workspaceDatabase.selectFrom('transactions')- .select(['id', 'status', 'version', 'server_created_at'])+ .select(['id', 'version', 'server_created_at']).where('id', '=', transaction.id).executeTakeFirst();if (existingTransaction) {if (- existingTransaction.status === 'synced' &&existingTransaction.version === version &&existingTransaction.server_created_at === transaction.serverCreatedAt) {@@ -984,7 +944,6 @@ class NodeService {await workspaceDatabase.updateTable('transactions').set({- status: 'synced',version,server_created_at: transaction.serverCreatedAt,})@@ -1048,8 +1007,6 @@ class NodeService {data: decodeState(transaction.data),created_at: transaction.createdAt,created_by: transaction.createdBy,- retry_count: 0,- status: updatedNode ? 'synced' : 'incomplete',version,server_created_at: transaction.serverCreatedAt,})@@ -1229,16 +1186,6 @@ class NodeService {.deleteFrom('interactions').where('node_id', '=', transaction.nodeId).execute();-- await tx- .deleteFrom('uploads')- .where('node_id', '=', transaction.nodeId)- .execute();-- await tx- .deleteFrom('downloads')- .where('node_id', '=', transaction.nodeId)- .execute();});eventBus.publish({diff --git a/apps/desktop/src/main/services/socket-connection.ts b/apps/desktop/src/main/services/socket-connection.tsindex 4df9a159..d2663929 100644--- a/apps/desktop/src/main/services/socket-connection.ts+++ b/apps/desktop/src/main/services/socket-connection.ts@@ -78,6 +78,12 @@ export class SocketConnection {} 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 === '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 925de278..7212e914 100644--- a/apps/desktop/src/main/services/sync-service.ts+++ b/apps/desktop/src/main/services/sync-service.ts@@ -3,11 +3,13 @@ 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 { createDebugger } from '@/main/debugger';export type NodeConsumersWrapper = {transactions: TransactionsConsumer;interactions: InteractionsConsumer;+ files: FilesConsumer;};export type ConsumersWrapper = {@@ -77,9 +79,18 @@ class SyncService {);await interactionsConsumer.init();+ const filesConsumer = new FilesConsumer(+ userId,+ accountId,+ rootId,+ workspaceDatabase+ );+ await filesConsumer.init();+consumers.nodes[rootId] = {transactions: transactionsConsumer,interactions: interactionsConsumer,+ files: filesConsumer,};}@@ -114,6 +125,13 @@ class SyncService {): InteractionsConsumer | undefined {return this.users.get(userId)?.nodes[rootId]?.interactions;}++ public getFilesConsumer(+ userId: string,+ rootId: string+ ): FilesConsumer | undefined {+ return this.users.get(userId)?.nodes[rootId]?.files;+ }}export const syncService = new SyncService();diff --git a/apps/desktop/src/main/utils.ts b/apps/desktop/src/main/utils.tsindex c1b1c460..da9d1135 100644--- a/apps/desktop/src/main/utils.ts+++ b/apps/desktop/src/main/utils.ts@@ -1,4 +1,4 @@-import { LocalTransaction, Node } from '@colanode/core';+import { LocalTransaction, Mutation, Node } from '@colanode/core';import { encodeState } from '@colanode/crdt';import {DeleteResult,@@ -19,19 +19,20 @@ import {SelectWorkspace,} from '@/main/data/app/schema';import {- SelectDownload,+ SelectFile,+ SelectFileState,SelectInteraction,+ SelectMutation,SelectNode,SelectTransaction,- SelectUpload,SelectUser,WorkspaceDatabaseSchema,} from '@/main/data/workspace/schema';import { Account } from '@/shared/types/accounts';import { Interaction } from '@/shared/types/interactions';-import { Download, Upload } from '@/shared/types/nodes';import { Server } from '@/shared/types/servers';import { User } from '@/shared/types/users';+import { File, FileState } from '@/shared/types/files';import { Workspace, WorkspaceCredentials } from '@/shared/types/workspaces';export const appPath = app.getPath('userData');@@ -230,6 +231,46 @@ export const mapTransaction = (row: SelectTransaction): LocalTransaction => {throw new Error('Invalid transaction type');};+export const mapMutation = (row: SelectMutation): Mutation => {+ if (row.type === 'apply_create_transaction') {+ return {+ id: row.id,+ type: 'apply_create_transaction',+ data: JSON.parse(row.data),+ createdAt: row.created_at,+ };+ }++ if (row.type === 'apply_update_transaction') {+ return {+ id: row.id,+ type: 'apply_update_transaction',+ data: JSON.parse(row.data),+ createdAt: row.created_at,+ };+ }++ if (row.type === 'apply_delete_transaction') {+ return {+ id: row.id,+ type: 'apply_delete_transaction',+ data: JSON.parse(row.data),+ createdAt: row.created_at,+ };+ }++ if (row.type === 'create_file') {+ return {+ id: row.id,+ type: 'create_file',+ data: JSON.parse(row.data),+ createdAt: row.created_at,+ };+ }++ throw new Error('Invalid mutation type');+};+export const mapServer = (row: SelectServer): Server => {return {domain: row.domain,@@ -242,24 +283,37 @@ export const mapServer = (row: SelectServer): Server => {};};-export const mapUpload = (row: SelectUpload): Upload => {+export const mapFile = (row: SelectFile): File => {return {- nodeId: row.node_id,+ id: row.id,+ type: row.type,+ parentId: row.parent_id,+ rootId: row.root_id,+ name: row.name,+ originalName: row.original_name,+ extension: row.extension,+ mimeType: row.mime_type,+ size: row.size,createdAt: row.created_at,+ createdBy: row.created_by,updatedAt: row.updated_at,- progress: row.progress,- retryCount: row.retry_count,+ updatedBy: row.updated_by,+ status: row.status,+ version: row.version,};};-export const mapDownload = (row: SelectDownload): Download => {+export const mapFileState = (row: SelectFileState): FileState => {return {- nodeId: row.node_id,- uploadId: row.upload_id,- progress: row.progress,+ fileId: row.file_id,+ downloadProgress: row.download_progress,+ downloadStatus: row.download_status,+ downloadRetries: row.download_retries,+ uploadProgress: row.upload_progress,+ uploadStatus: row.upload_status,+ uploadRetries: row.upload_retries,createdAt: row.created_at,updatedAt: row.updated_at,- retryCount: row.retry_count,};};diff --git a/apps/desktop/src/renderer/components/documents/document-editor.tsx b/apps/desktop/src/renderer/components/documents/document-editor.tsxindex 027bdbf1..1303f055 100644--- a/apps/desktop/src/renderer/components/documents/document-editor.tsx+++ b/apps/desktop/src/renderer/components/documents/document-editor.tsx@@ -64,6 +64,7 @@ import { EditorBubbleMenu } from '@/renderer/editor/menu/bubble-menu';interface DocumentEditorProps {documentId: string;+ rootId: string;content: JSONContent;transactionId: string;canEdit: boolean;@@ -73,6 +74,7 @@ interface DocumentEditorProps {export const DocumentEditor = ({documentId,+ rootId,content,transactionId,canEdit,@@ -141,6 +143,7 @@ export const DocumentEditor = ({context: {documentId,userId: workspace.userId,+ rootId,},}),BoldMark,diff --git a/apps/desktop/src/renderer/components/documents/document.tsx b/apps/desktop/src/renderer/components/documents/document.tsxindex cd57ebd0..aed24bce 100644--- a/apps/desktop/src/renderer/components/documents/document.tsx+++ b/apps/desktop/src/renderer/components/documents/document.tsx@@ -6,6 +6,7 @@ import { mapBlocksToContents } from '@/shared/lib/editor';interface DocumentProps {nodeId: string;+ rootId: string;content?: Record<string, Block> | null;transactionId: string;canEdit: boolean;@@ -15,6 +16,7 @@ interface DocumentProps {export const Document = ({nodeId,+ rootId,content,transactionId,canEdit,@@ -39,6 +41,7 @@ export const Document = ({<DocumentEditorkey={nodeId}documentId={nodeId}+ rootId={rootId}content={tiptapContent}transactionId={transactionId}canEdit={canEdit}diff --git a/apps/desktop/src/renderer/components/files/file-block.tsx b/apps/desktop/src/renderer/components/files/file-block.tsxindex 439fd645..86695375 100644--- a/apps/desktop/src/renderer/components/files/file-block.tsx+++ b/apps/desktop/src/renderer/components/files/file-block.tsx@@ -10,12 +10,12 @@ export const FileBlock = ({ id }: FileBlockProps) => {const workspace = useWorkspace();const { data } = useQuery({- type: 'node_get',- nodeId: id,+ type: 'file_get',+ id,userId: workspace.userId,});- if (!data || data.type !== 'file') {+ if (!data) {return null;}diff --git a/apps/desktop/src/renderer/components/files/file-body.tsx b/apps/desktop/src/renderer/components/files/file-body.tsxindex 2ab8bb11..4052611b 100644--- a/apps/desktop/src/renderer/components/files/file-body.tsx+++ b/apps/desktop/src/renderer/components/files/file-body.tsx@@ -1,40 +1,40 @@-import { FileNode } from '@colanode/core';import { SquareArrowOutUpRight } from 'lucide-react';-import { useEffect } from 'react';+// import { useEffect } from 'react';import { FilePreview } from '@/renderer/components/files/file-preview';import { FileSidebar } from '@/renderer/components/files/file-sidebar';import { Button } from '@/renderer/components/ui/button';import { useWorkspace } from '@/renderer/contexts/workspace';-import { useRadar } from '@/renderer/contexts/radar';+// import { useRadar } from '@/renderer/contexts/radar';+import { FileWithState } from '@/shared/types/files';interface FileBodyProps {- file: FileNode;+ file: FileWithState;}export const FileBody = ({ file }: FileBodyProps) => {const workspace = useWorkspace();- const radar = useRadar();+ // const radar = useRadar();- useEffect(() => {- radar.markAsOpened(- workspace.userId,- file.id,- file.type,- file.transactionId- );+ // useEffect(() => {+ // radar.markAsOpened(+ // workspace.userId,+ // file.id,+ // 'file',+ // file.transactionId+ // );- const interval = setInterval(() => {- radar.markAsOpened(- workspace.userId,- file.id,- file.type,- file.transactionId- );- }, 60000);+ // const interval = setInterval(() => {+ // radar.markAsOpened(+ // workspace.userId,+ // file.id,+ // file.type,+ // file.transactionId+ // );+ // }, 60000);- return () => clearInterval(interval);- }, [file.id, file.type, file.transactionId]);+ // return () => clearInterval(interval);+ // }, [file.id, file.type, file.transactionId]);return (<div className="flex h-full max-h-full w-full flex-row items-center gap-2">@@ -48,7 +48,7 @@ export const FileBody = ({ file }: FileBodyProps) => {type: 'file_open',userId: workspace.userId,fileId: file.id,- extension: file.attributes.extension,+ extension: file.extension,})}>diff --git a/apps/desktop/src/renderer/components/files/file-breadcrumb-item.tsx b/apps/desktop/src/renderer/components/files/file-breadcrumb-item.tsxdeleted file mode 100644index 904dcd99..00000000--- a/apps/desktop/src/renderer/components/files/file-breadcrumb-item.tsx+++ /dev/null@@ -1,16 +0,0 @@-import { FileNode } from '@colanode/core';--import { Avatar } from '@/renderer/components/avatars/avatar';--interface FileBreadcrumbItemProps {- node: FileNode;-}--export const FileBreadcrumbItem = ({ node }: FileBreadcrumbItemProps) => {- return (- <div className="flex items-center space-x-2">- <Avatar size="small" id={node.id} name={node.attributes.name} />- <span>{node.attributes.name}</span>- </div>- );-};diff --git a/apps/desktop/src/renderer/components/files/file-container.tsx b/apps/desktop/src/renderer/components/files/file-container.tsxindex 7497f42f..69d32bb5 100644--- a/apps/desktop/src/renderer/components/files/file-container.tsx+++ b/apps/desktop/src/renderer/components/files/file-container.tsx@@ -11,27 +11,36 @@ interface FileContainerProps {export const FileContainer = ({ nodeId }: FileContainerProps) => {const workspace = useWorkspace();- const { data, isPending } = useQuery({- type: 'node_tree_get',- nodeId,++ const { data: file, isPending: isFilePending } = useQuery({+ type: 'file_get',+ id: nodeId,userId: workspace.userId,});- if (isPending) {+ const { data: nodes, isPending: isNodesPending } = useQuery(+ {+ type: 'node_tree_get',+ nodeId: file?.parentId ?? '',+ userId: workspace.userId,+ },+ {+ enabled: !!file,+ }+ );++ if (isFilePending || isNodesPending) {return null;}- const nodes = data ?? [];- const file = nodes.find((node) => node.id === nodeId);- const role = extractNodeRole(nodes, workspace.userId);-- if (!file || file.type !== 'file' || !role) {+ const role = extractNodeRole(nodes ?? [], workspace.userId);+ if (!file || !role) {return null;}return (<div className="flex h-full w-full flex-col">- <FileHeader nodes={nodes} file={file} role={role} />+ <FileHeader nodes={nodes ?? []} file={file} role={role} /><FileBody file={file} /></div>);diff --git a/apps/desktop/src/renderer/components/files/file-header.tsx b/apps/desktop/src/renderer/components/files/file-header.tsxindex ee95e3bb..7f3075d7 100644--- a/apps/desktop/src/renderer/components/files/file-header.tsx+++ b/apps/desktop/src/renderer/components/files/file-header.tsx@@ -1,4 +1,4 @@-import { FileNode, Node, NodeRole } from '@colanode/core';+import { Node, NodeRole } from '@colanode/core';import { NodeCollaboratorsPopover } from '@/renderer/components/collaborators/node-collaborators-popover';import { FileSettings } from '@/renderer/components/files/file-settings';@@ -6,10 +6,11 @@ import { NodeBreadcrumb } from '@/renderer/components/layouts/node-breadcrumb';import { NodeFullscreenButton } from '@/renderer/components/layouts/node-fullscreen-button';import { Header } from '@/renderer/components/ui/header';import { useContainer } from '@/renderer/contexts/container';+import { FileWithState } from '@/shared/types/files';interface FileHeaderProps {nodes: Node[];- file: FileNode;+ file: FileWithState;role: NodeRole;}diff --git a/apps/desktop/src/renderer/components/files/file-preview.tsx b/apps/desktop/src/renderer/components/files/file-preview.tsxindex 9673f3bf..c4aa31b3 100644--- a/apps/desktop/src/renderer/components/files/file-preview.tsx+++ b/apps/desktop/src/renderer/components/files/file-preview.tsx@@ -1,4 +1,3 @@-import { FileNode } from '@colanode/core';import { match } from 'ts-pattern';import { FileDownload } from '@/renderer/components/files/file-download';@@ -6,39 +5,26 @@ import { FilePreviewImage } from '@/renderer/components/files/previews/file-previmport { FilePreviewOther } from '@/renderer/components/files/previews/file-preview-other';import { FilePreviewVideo } from '@/renderer/components/files/previews/file-preview-video';import { useWorkspace } from '@/renderer/contexts/workspace';-import { useQuery } from '@/renderer/hooks/use-query';import { getFileUrl } from '@/shared/lib/files';+import { FileWithState } from '@/shared/types/files';interface FilePreviewProps {- file: FileNode;+ file: FileWithState;}export const FilePreview = ({ file }: FilePreviewProps) => {const workspace = useWorkspace();- const { data } = useQuery({- type: 'download_get',- nodeId: file.id,- userId: workspace.userId,- });- if (!data || data.progress !== 100) {- return <FileDownload id={file.id} downloadProgress={data?.progress} />;+ if (file.downloadProgress !== 100) {+ return (+ <FileDownload id={file.id} downloadProgress={file.downloadProgress} />+ );}- const url = getFileUrl(- workspace.userId,- file.id,- file.attributes.uploadId,- file.attributes.extension- );+ const url = getFileUrl(workspace.userId, file.id, file.extension);- return match(file.attributes.subtype)- .with('image', () => (- <FilePreviewImage url={url} name={file.attributes.name} />- ))+ return match(file.type)+ .with('image', () => <FilePreviewImage url={url} name={file.name} />).with('video', () => <FilePreviewVideo url={url} />)- .with('other', () => (- <FilePreviewOther mimeType={file.attributes.mimeType} />- ))- .otherwise(() => null);+ .otherwise(() => <FilePreviewOther mimeType={file.mimeType} />);};diff --git a/apps/desktop/src/renderer/components/files/file-sidebar.tsx b/apps/desktop/src/renderer/components/files/file-sidebar.tsxindex 18fa0daf..3570795c 100644--- a/apps/desktop/src/renderer/components/files/file-sidebar.tsx+++ b/apps/desktop/src/renderer/components/files/file-sidebar.tsx@@ -1,4 +1,4 @@-import { FileNode, formatDate } from '@colanode/core';+import { formatDate } from '@colanode/core';import React from 'react';import { Avatar } from '@/renderer/components/avatars/avatar';@@ -6,9 +6,10 @@ import { FileThumbnail } from '@/renderer/components/files/file-thumbnail';import { useWorkspace } from '@/renderer/contexts/workspace';import { useQuery } from '@/renderer/hooks/use-query';import { formatBytes } from '@/shared/lib/files';+import { FileWithState } from '@/shared/types/files';interface FileSidebarProps {- file: FileNode;+ file: FileWithState;}const FileMeta = ({ title, value }: { title: string; value: string }) => {@@ -39,16 +40,16 @@ export const FileSidebar = ({ file }: FileSidebarProps) => {/><divclassName="line-clamp-3 break-words text-base font-medium"- title={file.attributes.name}+ title={file.name}>- {file.attributes.name}+ {file.name}</div></div><div className="mt-5 flex flex-col gap-4">- <FileMeta title="Name" value={file.attributes.name} />- <FileMeta title="Type" value={file.attributes.mimeType} />- <FileMeta title="Size" value={formatBytes(file.attributes.size)} />+ <FileMeta title="Name" value={file.name} />+ <FileMeta title="Type" value={file.mimeType} />+ <FileMeta title="Size" value={formatBytes(file.size)} /><FileMeta title="Created at" value={formatDate(file.createdAt)} />{user && (diff --git a/apps/desktop/src/renderer/components/files/file-thumbnail.tsx b/apps/desktop/src/renderer/components/files/file-thumbnail.tsxindex 7da30e6e..f8a2f3e6 100644--- a/apps/desktop/src/renderer/components/files/file-thumbnail.tsx+++ b/apps/desktop/src/renderer/components/files/file-thumbnail.tsx@@ -1,44 +1,27 @@-import { FileNode } from '@colanode/core';-import { FileIcon } from '@/renderer/components/files/file-icon';import { useWorkspace } from '@/renderer/contexts/workspace';-import { useQuery } from '@/renderer/hooks/use-query';import { getFileUrl } from '@/shared/lib/files';import { cn } from '@/shared/lib/utils';+import { FileWithState } from '@/shared/types/files';interface FileThumbnailProps {- file: FileNode;+ file: FileWithState;className?: string;}export const FileThumbnail = ({ file, className }: FileThumbnailProps) => {const workspace = useWorkspace();- const { data } = useQuery({- type: 'download_get',- nodeId: file.id,- userId: workspace.userId,- });- if (- file.attributes.subtype === 'image' &&- data &&- data.progress === 100 &&- data.uploadId === file.attributes.uploadId- ) {- const url = getFileUrl(- workspace.userId,- file.id,- file.attributes.uploadId,- file.attributes.extension- );+ if (file.type === 'image' && file.downloadProgress === 100) {+ const url = getFileUrl(workspace.userId, file.id, file.extension);return (<imgsrc={url}- alt={file.attributes.name}+ alt={file.name}className={cn('object-contain object-center', className)}/>);}- return <FileIcon mimeType={file.attributes.mimeType} className="size-10" />;+ return <FileIcon mimeType={file.mimeType} className="size-10" />;};diff --git a/apps/desktop/src/renderer/components/folders/folder-body.tsx b/apps/desktop/src/renderer/components/folders/folder-body.tsxindex 7e9bbde1..f122bd3d 100644--- a/apps/desktop/src/renderer/components/folders/folder-body.tsx+++ b/apps/desktop/src/renderer/components/folders/folder-body.tsx@@ -100,11 +100,10 @@ export const FolderBody = ({ folder }: FolderBodyProps) => {mutate({input: {type: 'file_create',- accountId: workspace.accountId,- workspaceId: workspace.id,userId: workspace.userId,filePath,parentId: folder.id,+ rootId: folder.rootId,},onError(error) {toast({diff --git a/apps/desktop/src/renderer/components/folders/grids/grid-file.tsx b/apps/desktop/src/renderer/components/folders/grids/grid-file.tsxindex 88db7656..4315a6e9 100644--- a/apps/desktop/src/renderer/components/folders/grids/grid-file.tsx+++ b/apps/desktop/src/renderer/components/folders/grids/grid-file.tsx@@ -1,11 +1,10 @@-import { FileNode } from '@colanode/core';-+import { FileWithState } from '@/shared/types/files';import { FileContextMenu } from '@/renderer/components/files/file-context-menu';import { FileThumbnail } from '@/renderer/components/files/file-thumbnail';import { GridItem } from '@/renderer/components/folders/grids/grid-item';interface GridFileProps {- file: FileNode;+ file: FileWithState;}export const GridFile = ({ file }: GridFileProps) => {@@ -17,9 +16,9 @@ export const GridFile = ({ file }: GridFileProps) => {</div><pclassName="line-clamp-2 w-full break-words text-center text-xs text-foreground/80"- title={file.attributes.name}+ title={file.name}>- {file.attributes.name}+ {file.name}</p></GridItem></FileContextMenu>diff --git a/apps/desktop/src/renderer/components/layouts/node-breadcrumb-item.tsx b/apps/desktop/src/renderer/components/layouts/node-breadcrumb-item.tsxindex e50434bd..66d6b2ec 100644--- a/apps/desktop/src/renderer/components/layouts/node-breadcrumb-item.tsx+++ b/apps/desktop/src/renderer/components/layouts/node-breadcrumb-item.tsx@@ -3,7 +3,6 @@ import { Node } from '@colanode/core';import { ChannelBreadcrumbItem } from '@/renderer/components/channels/channel-breadcrumb-item';import { ChatBreadcrumbItem } from '@/renderer/components/chats/chat-breadcrumb-item';import { DatabaseBreadcrumbItem } from '@/renderer/components/databases/database-breadcrumb-item';-import { FileBreadcrumbItem } from '@/renderer/components/files/file-breadcrumb-item';import { FolderBreadcrumbItem } from '@/renderer/components/folders/folder-breadcrumb-item';import { MessageBreadcrumbItem } from '@/renderer/components/messages/message-breadcrumb-item';import { PageBreadcrumbItem } from '@/renderer/components/pages/page-breadcrumb-item';@@ -30,8 +29,6 @@ export const NodeBreadcrumbItem = ({ node }: NodeBreadcrumbItemProps) => {return <RecordBreadcrumbItem node={node} />;case 'folder':return <FolderBreadcrumbItem node={node} />;- case 'file':- return <FileBreadcrumbItem node={node} />;case 'message':return <MessageBreadcrumbItem node={node} />;default:diff --git a/apps/desktop/src/renderer/components/pages/page-body.tsx b/apps/desktop/src/renderer/components/pages/page-body.tsxindex 76742453..330c361d 100644--- a/apps/desktop/src/renderer/components/pages/page-body.tsx+++ b/apps/desktop/src/renderer/components/pages/page-body.tsx@@ -65,6 +65,7 @@ export const PageBody = ({ page, role }: PageBodyProps) => {<ScrollArea className="h-full max-h-full w-full overflow-y-auto px-10 pb-12"><DocumentnodeId={page.id}+ rootId={page.rootId}content={page.attributes.content}transactionId={page.transactionId}canEdit={canEdit}diff --git a/apps/desktop/src/renderer/components/records/record-body.tsx b/apps/desktop/src/renderer/components/records/record-body.tsxindex 34960649..9fcb3376 100644--- a/apps/desktop/src/renderer/components/records/record-body.tsx+++ b/apps/desktop/src/renderer/components/records/record-body.tsx@@ -88,6 +88,7 @@ export const RecordBody = ({<Separator className="my-4 w-full" /><DocumentnodeId={record.id}+ rootId={record.rootId}content={record.attributes.content}transactionId={record.transactionId}canEdit={canEdit}diff --git a/apps/desktop/src/renderer/contexts/folder.ts b/apps/desktop/src/renderer/contexts/folder.tsindex 58b6f3d9..1b5bdee1 100644--- a/apps/desktop/src/renderer/contexts/folder.ts+++ b/apps/desktop/src/renderer/contexts/folder.ts@@ -1,10 +1,11 @@-import { FileNode } from '@colanode/core';import { createContext, useContext } from 'react';+import { FileWithState } from '@/shared/types/files';+interface FolderContext {id: string;name: string;- files: FileNode[];+ files: FileWithState[];onClick: (event: React.MouseEvent<HTMLElement>, id: string) => void;onDoubleClick: (event: React.MouseEvent<HTMLElement>, id: string) => void;onMove: (nodeId: string, targetId: string) => void;diff --git a/apps/desktop/src/renderer/editor/commands/file.tsx b/apps/desktop/src/renderer/editor/commands/file.tsxindex 96ffa8e4..518ad9b4 100644--- a/apps/desktop/src/renderer/editor/commands/file.tsx+++ b/apps/desktop/src/renderer/editor/commands/file.tsx@@ -33,13 +33,13 @@ export const FileCommand: EditorCommand = {return;}- const { userId, documentId } = context;+ const { userId, documentId, rootId } = context;const output = await window.colanode.executeMutation({type: 'file_create',filePath,userId,parentId: documentId,- generateIndex: false,+ rootId,});if (!output.success) {diff --git a/apps/desktop/src/shared/lib/files.ts b/apps/desktop/src/shared/lib/files.tsindex 61847d5e..865dad21 100644--- a/apps/desktop/src/shared/lib/files.ts+++ b/apps/desktop/src/shared/lib/files.ts@@ -13,10 +13,9 @@ export const formatBytes = (bytes: number, decimals?: number): string => {export const getFileUrl = (userId: string,fileId: string,- uploadId: string,extension: string) => {- return `local-file://${userId}/${fileId}_${uploadId}${extension}`;+ return `local-file://${userId}/${fileId}${extension}`;};export const getFilePlaceholderUrl = (path: string) => {diff --git a/apps/desktop/src/shared/mutations/files/file-create.ts b/apps/desktop/src/shared/mutations/files/file-create.tsindex 639e7d88..09e2922e 100644--- a/apps/desktop/src/shared/mutations/files/file-create.ts+++ b/apps/desktop/src/shared/mutations/files/file-create.ts@@ -2,6 +2,7 @@ export type FileCreateMutationInput = {type: 'file_create';userId: string;parentId: string;+ rootId: string;filePath: string;};diff --git a/apps/desktop/src/shared/queries/files/download-get.ts b/apps/desktop/src/shared/queries/files/download-get.tsdeleted file mode 100644index 8d0bf2f3..00000000--- a/apps/desktop/src/shared/queries/files/download-get.ts+++ /dev/null@@ -1,16 +0,0 @@-import { Download } from '@/shared/types/nodes';--export type DownloadGetQueryInput = {- type: 'download_get';- nodeId: string;- userId: string;-};--declare module '@/shared/queries' {- interface QueryMap {- download_get: {- input: DownloadGetQueryInput;- output: Download | null;- };- }-}diff --git a/apps/desktop/src/shared/queries/files/file-get.ts b/apps/desktop/src/shared/queries/files/file-get.tsnew file mode 100644index 00000000..e5b9243e--- /dev/null+++ b/apps/desktop/src/shared/queries/files/file-get.ts@@ -0,0 +1,16 @@+import { FileWithState } from '@/shared/types/files';++export type FileGetQueryInput = {+ type: 'file_get';+ id: string;+ userId: string;+};++declare module '@/shared/queries' {+ interface QueryMap {+ file_get: {+ input: FileGetQueryInput;+ output: FileWithState | null;+ };+ }+}diff --git a/apps/desktop/src/shared/queries/files/file-list.ts b/apps/desktop/src/shared/queries/files/file-list.tsindex 0ab46d21..80a4da32 100644--- a/apps/desktop/src/shared/queries/files/file-list.ts+++ b/apps/desktop/src/shared/queries/files/file-list.ts@@ -1,4 +1,4 @@-import { FileNode } from '@colanode/core';+import { FileWithState } from '@/shared/types/files';export type FileListQueryInput = {type: 'file_list';@@ -12,7 +12,7 @@ declare module '@/shared/queries' {interface QueryMap {file_list: {input: FileListQueryInput;- output: FileNode[];+ output: FileWithState[];};}}diff --git a/apps/desktop/src/shared/types/editor.ts b/apps/desktop/src/shared/types/editor.tsindex c4d6e7e0..4c3fdeaf 100644--- a/apps/desktop/src/shared/types/editor.ts+++ b/apps/desktop/src/shared/types/editor.ts@@ -10,6 +10,7 @@ export type EditorCommandProps = {export type EditorCommandContext = {documentId: string;userId: string;+ rootId: string;};export type EditorCommand = {diff --git a/apps/desktop/src/shared/types/events.ts b/apps/desktop/src/shared/types/events.tsindex 6dbe20b1..92966231 100644--- a/apps/desktop/src/shared/types/events.ts+++ b/apps/desktop/src/shared/types/events.ts@@ -1,11 +1,11 @@-import { LocalTransaction, Node } from '@colanode/core';+import { Node } from '@colanode/core';import { Account } from '@/shared/types/accounts';import { Interaction } from '@/shared/types/interactions';-import { Download, Upload } from '@/shared/types/nodes';import { Server } from '@/shared/types/servers';import { Workspace } from '@/shared/types/workspaces';import { User } from '@/shared/types/users';+import { File, FileState } from '@/shared/types/files';export type UserCreatedEvent = {type: 'user_created';@@ -43,6 +43,42 @@ export type NodeDeletedEvent = {node: Node;};+export type FileCreatedEvent = {+ type: 'file_created';+ userId: string;+ file: File;+};++export type FileUpdatedEvent = {+ type: 'file_updated';+ userId: string;+ file: File;+};++export type FileDeletedEvent = {+ type: 'file_deleted';+ userId: string;+ file: File;+};++export type FileStateCreatedEvent = {+ type: 'file_state_created';+ userId: string;+ fileState: FileState;+};++export type FileStateUpdatedEvent = {+ type: 'file_state_updated';+ userId: string;+ fileState: FileState;+};++export type FileStateDeletedEvent = {+ type: 'file_state_deleted';+ userId: string;+ fileState: FileState;+};+export type AccountCreatedEvent = {type: 'account_created';account: Account;@@ -83,42 +119,6 @@ export type ServerUpdatedEvent = {server: Server;};-export type DownloadCreatedEvent = {- type: 'download_created';- userId: string;- download: Download;-};--export type DownloadUpdatedEvent = {- type: 'download_updated';- userId: string;- download: Download;-};--export type DownloadDeletedEvent = {- type: 'download_deleted';- userId: string;- download: Download;-};--export type UploadCreatedEvent = {- type: 'upload_created';- userId: string;- upload: Upload;-};--export type UploadUpdatedEvent = {- type: 'upload_updated';- userId: string;- upload: Upload;-};--export type UploadDeletedEvent = {- type: 'upload_deleted';- userId: string;- upload: Upload;-};-export type QueryResultUpdatedEvent = {type: 'query_result_updated';id: string;@@ -129,10 +129,10 @@ export type RadarDataUpdatedEvent = {type: 'radar_data_updated';};-export type TransactionCreatedEvent = {- type: 'transaction_created';+export type MutationCreatedEvent = {+ type: 'mutation_created';userId: string;- transaction: LocalTransaction;+ mutationId: string;};export type CollaborationCreatedEvent = {@@ -185,15 +185,15 @@ export type Event =| WorkspaceDeletedEvent| ServerCreatedEvent| ServerUpdatedEvent- | DownloadCreatedEvent- | DownloadUpdatedEvent- | DownloadDeletedEvent- | UploadCreatedEvent- | UploadUpdatedEvent- | UploadDeletedEvent+ | FileCreatedEvent+ | FileUpdatedEvent+ | FileDeletedEvent+ | FileStateCreatedEvent+ | FileStateUpdatedEvent+ | FileStateDeletedEvent| QueryResultUpdatedEvent| RadarDataUpdatedEvent- | TransactionCreatedEvent+ | MutationCreatedEvent| ServerAvailabilityChangedEvent| SocketConnectionOpenedEvent| CollaborationCreatedEventdiff --git a/apps/desktop/src/shared/types/files.ts b/apps/desktop/src/shared/types/files.tsindex 5c9624f8..ccd37341 100644--- a/apps/desktop/src/shared/types/files.ts+++ b/apps/desktop/src/shared/types/files.ts@@ -1,4 +1,4 @@-import { FileType } from '@colanode/core';+import { FileStatus, FileType } from '@colanode/core';export type FileMetadata = {path: string;@@ -8,3 +8,43 @@ export type FileMetadata = {size: number;type: FileType;};++export type File = {+ id: string;+ type: FileType;+ parentId: string;+ rootId: string;+ name: string;+ originalName: string;+ extension: string;+ mimeType: string;+ size: number;+ createdAt: string;+ createdBy: string;+ updatedAt: string | null;+ updatedBy: string | null;+ status: FileStatus;+ version: bigint;+};++export type DownloadStatus = 'none' | 'pending' | 'completed' | 'failed';+export type UploadStatus = 'none' | 'pending' | 'completed' | 'failed';++export type FileState = {+ fileId: string;+ downloadProgress: number;+ downloadStatus: DownloadStatus;+ downloadRetries: number;+ uploadProgress: number;+ uploadStatus: UploadStatus;+ uploadRetries: number;+ createdAt: string;+ updatedAt: string | null;+};++export type FileWithState = File & {+ downloadProgress: number;+ downloadStatus: DownloadStatus;+ uploadProgress: number;+ uploadStatus: UploadStatus;+};diff --git a/apps/desktop/src/shared/types/nodes.ts b/apps/desktop/src/shared/types/nodes.tsindex 966ad721..5ab2d9d6 100644--- a/apps/desktop/src/shared/types/nodes.ts+++ b/apps/desktop/src/shared/types/nodes.ts@@ -5,20 +5,3 @@ export type NodeCollaborator = {collaboratorId: string;role: NodeRole;};--export type Download = {- nodeId: string;- uploadId: string;- createdAt: string;- updatedAt: string | null;- progress: number;- retryCount: number;-};--export type Upload = {- nodeId: string;- createdAt: string;- updatedAt: string | null;- progress: number;- retryCount: number;-};diff --git a/apps/server/src/consumers/files.ts b/apps/server/src/consumers/files.tsnew file mode 100644index 00000000..05105845--- /dev/null+++ b/apps/server/src/consumers/files.ts@@ -0,0 +1,78 @@+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/controllers/client/workspaces/files/file-download-get.ts b/apps/server/src/controllers/client/workspaces/files/file-download-get.tsindex c7d14521..3623a449 100644--- a/apps/server/src/controllers/client/workspaces/files/file-download-get.ts+++ b/apps/server/src/controllers/client/workspaces/files/file-download-get.ts@@ -1,5 +1,5 @@import { Request, Response } from 'express';-import { CreateDownloadOutput, hasCollaboratorAccess } from '@colanode/core';+import { CreateDownloadOutput, hasViewerAccess } from '@colanode/core';import { GetObjectCommand } from '@aws-sdk/client-s3';import { getSignedUrl } from '@aws-sdk/s3-request-presigner';@@ -14,22 +14,13 @@ export const fileDownloadGetHandler = async (): Promise<void> => {const fileId = req.params.fileId as string;- const role = await fetchNodeRole(fileId, res.locals.user.id);- if (role === null || !hasCollaboratorAccess(role)) {- res.status(403).json({- code: ApiError.Forbidden,- message: 'Forbidden.',- });- return;- }-- const node = await database- .selectFrom('nodes')+ const file = await database+ .selectFrom('files').selectAll().where('id', '=', fileId).executeTakeFirst();- if (!node) {+ if (!file) {res.status(404).json({code: ApiError.ResourceNotFound,message: 'File not found.',@@ -37,33 +28,20 @@ export const fileDownloadGetHandler = async (return;}- if (node.attributes.type !== 'file') {- res.status(400).json({- code: ApiError.BadRequest,- message: 'File not found.',- });- return;- }-- // check if the upload is completed- const upload = await database- .selectFrom('uploads')- .selectAll()- .where('node_id', '=', fileId)- .executeTakeFirst();-- if (!upload) {- res.status(400).json({- code: ApiError.BadRequest,- message: 'Upload not completed.',+ const role = await fetchNodeRole(file.root_id, res.locals.user.id);+ if (role === null || !hasViewerAccess(role)) {+ res.status(403).json({+ code: ApiError.Forbidden,+ message: 'Forbidden.',});return;}//generate presigned url for download+ const path = `files/${file.workspace_id}/${file.id}${file.extension}`;const command = new GetObjectCommand({Bucket: BUCKET_NAMES.FILES,- Key: upload.path,+ Key: path,});const presignedUrl = await getSignedUrl(filesStorage, command, {diff --git a/apps/server/src/controllers/client/workspaces/files/file-upload-complete.ts b/apps/server/src/controllers/client/workspaces/files/file-upload-complete.tsindex e21a0d63..f7a0fcb3 100644--- a/apps/server/src/controllers/client/workspaces/files/file-upload-complete.ts+++ b/apps/server/src/controllers/client/workspaces/files/file-upload-complete.ts@@ -1,34 +1,23 @@import { Request, Response } from 'express';-import { extractFileType, UploadMetadata } from '@colanode/core';import { HeadObjectCommand } from '@aws-sdk/client-s3';+import { FileStatus } from '@colanode/core';import { ApiError } from '@/types/api';import { database } from '@/data/database';-import { redis } from '@/data/redis';import { BUCKET_NAMES, filesStorage } from '@/data/storage';-import { nodeService } from '@/services/node-service';+import { eventBus } from '@/lib/event-bus';export const fileUploadCompleteHandler = async (req: Request,res: Response): Promise<void> => {const workspaceId = req.params.workspaceId as string;- const uploadId = req.params.uploadId as string;+ const fileId = req.params.fileId as string;- const metadataJson = await redis.get(uploadId);- if (!metadataJson) {- res.status(404).json({- code: ApiError.ResourceNotFound,- message: 'Upload not found.',- });- return;- }-- const metadata: UploadMetadata = JSON.parse(metadataJson);const file = await database- .selectFrom('nodes')+ .selectFrom('files').selectAll()- .where('id', '=', metadata.fileId)+ .where('id', '=', fileId).executeTakeFirst();if (!file) {@@ -39,23 +28,31 @@ export const fileUploadCompleteHandler = async (return;}- if (file.attributes.type !== 'file') {+ if (file.created_by !== res.locals.user.id) {+ res.status(403).json({+ code: ApiError.Forbidden,+ message: 'Forbidden.',+ });+ return;+ }++ if (file.workspace_id !== workspaceId) {res.status(400).json({code: ApiError.BadRequest,- message: 'File not found.',+ message: 'File does not belong to this workspace.',});return;}- if (file.attributes.size !== metadata.size) {+ if (file.status !== FileStatus.Pending) {res.status(400).json({code: ApiError.BadRequest,- message: 'Size mismatch.',+ message: 'File is not pending.',});return;}- const path = metadata.path;+ const path = `files/${file.workspace_id}/${file.id}${file.extension}`;// check if the file exists in the bucketconst command = new HeadObjectCommand({Bucket: BUCKET_NAMES.FILES,@@ -66,7 +63,7 @@ export const fileUploadCompleteHandler = async (const headObject = await filesStorage.send(command);// Verify file size matches expected size- if (headObject.ContentLength !== metadata.size) {+ if (headObject.ContentLength !== file.size) {res.status(400).json({code: ApiError.BadRequest,message: 'Uploaded file size does not match expected size',@@ -75,7 +72,7 @@ export const fileUploadCompleteHandler = async (}// Verify mime type matches expected type- if (headObject.ContentType !== metadata.mimeType) {+ if (headObject.ContentType !== file.mime_type) {res.status(400).json({code: ApiError.BadRequest,message: 'Uploaded file type does not match expected type',@@ -90,33 +87,31 @@ export const fileUploadCompleteHandler = async (return;}- await database- .insertInto('uploads')- .values({- node_id: file.id,- upload_id: uploadId,- workspace_id: workspaceId,- path: metadata.path,- mime_type: metadata.mimeType,- size: metadata.size,- type: extractFileType(metadata.mimeType),- created_by: res.locals.user.id,- created_at: new Date(metadata.createdAt),- completed_at: new Date(),+ const updatedFile = await database+ .updateTable('files')+ .returningAll()+ .set({+ status: FileStatus.Ready,+ updated_by: res.locals.user.id,+ updated_at: new Date(),})- .execute();+ .where('id', '=', fileId)+ .executeTakeFirst();++ if (!updatedFile) {+ res.status(500).json({+ code: ApiError.InternalServerError,+ message: 'Failed to update file status.',+ });+ return;+ }- await nodeService.updateNode({- nodeId: file.id,- userId: res.locals.user.id,- workspaceId: workspaceId,- updater: (attributes) => ({- ...attributes,- uploadStatus: 'completed',- uploadId: metadata.uploadId,- }),+ eventBus.publish({+ type: 'file_updated',+ fileId: updatedFile.id,+ rootId: updatedFile.root_id,+ workspaceId: updatedFile.workspace_id,});- await redis.del(uploadId);res.status(200).json({ success: true });};diff --git a/apps/server/src/controllers/client/workspaces/files/file-upload-init.ts b/apps/server/src/controllers/client/workspaces/files/file-upload-init.tsindex a6616149..aa553693 100644--- a/apps/server/src/controllers/client/workspaces/files/file-upload-init.ts+++ b/apps/server/src/controllers/client/workspaces/files/file-upload-init.ts@@ -1,16 +1,11 @@import { Request, Response } from 'express';-import {- CreateUploadInput,- CreateUploadOutput,- UploadMetadata,-} from '@colanode/core';+import { CreateUploadInput, CreateUploadOutput } from '@colanode/core';import { PutObjectCommand } from '@aws-sdk/client-s3';import { getSignedUrl } from '@aws-sdk/s3-request-presigner';import { ApiError } from '@/types/api';import { database } from '@/data/database';import { BUCKET_NAMES, filesStorage } from '@/data/storage';-import { redis } from '@/data/redis';export const fileUploadInitHandler = async (req: Request,@@ -19,13 +14,13 @@ export const fileUploadInitHandler = async (const workspaceId = req.params.workspaceId as string;const input = req.body as CreateUploadInput;- const node = await database- .selectFrom('nodes')+ const file = await database+ .selectFrom('files').selectAll().where('id', '=', input.fileId).executeTakeFirst();- if (!node) {+ if (!file) {res.status(404).json({code: ApiError.ResourceNotFound,message: 'File not found.',@@ -33,15 +28,7 @@ export const fileUploadInitHandler = async (return;}- if (node.attributes.type !== 'file') {- res.status(400).json({- code: ApiError.BadRequest,- message: 'File not found.',- });- return;- }-- if (node.created_by !== res.locals.user.id) {+ if (file.created_by !== res.locals.user.id) {res.status(403).json({code: ApiError.Forbidden,message: 'Forbidden.',@@ -50,12 +37,12 @@ export const fileUploadInitHandler = async (}//generate presigned url for upload- const path = `files/${workspaceId}/${input.fileId}_${input.uploadId}${node.attributes.extension}`;+ const path = `files/${workspaceId}/${input.fileId}${file.extension}`;const command = new PutObjectCommand({Bucket: BUCKET_NAMES.FILES,Key: path,- ContentLength: node.attributes.size,- ContentType: node.attributes.mimeType,+ ContentLength: file.size,+ ContentType: file.mime_type,});const expiresIn = 60 * 60 * 4; // 4 hours@@ -63,21 +50,7 @@ export const fileUploadInitHandler = async (expiresIn,});- const data: UploadMetadata = {- fileId: input.fileId,- path,- mimeType: node.attributes.mimeType,- size: node.attributes.size,- uploadId: input.uploadId,- createdAt: new Date().toISOString(),- };-- await redis.set(input.uploadId, JSON.stringify(data), {- EX: expiresIn,- });-const output: CreateUploadOutput = {- uploadId: input.uploadId,url: presignedUrl,};diff --git a/apps/server/src/controllers/client/workspaces/index.ts b/apps/server/src/controllers/client/workspaces/index.tsindex 8666cd69..3d95cb42 100644--- a/apps/server/src/controllers/client/workspaces/index.ts+++ b/apps/server/src/controllers/client/workspaces/index.ts@@ -4,4 +4,4 @@ export * from './workspace-delete';export * from './workspace-get';export * from './users';export * from './files';-export * from './transactions';+export * from './mutations';diff --git a/apps/server/src/controllers/client/workspaces/mutations/index.ts b/apps/server/src/controllers/client/workspaces/mutations/index.tsnew file mode 100644index 00000000..3481ac42--- /dev/null+++ b/apps/server/src/controllers/client/workspaces/mutations/index.ts@@ -0,0 +1 @@+export * from './mutations-sync';diff --git a/apps/server/src/controllers/client/workspaces/mutations/mutations-sync.ts b/apps/server/src/controllers/client/workspaces/mutations/mutations-sync.tsnew file mode 100644index 00000000..dcf4c396--- /dev/null+++ b/apps/server/src/controllers/client/workspaces/mutations/mutations-sync.ts@@ -0,0 +1,124 @@+import { Request, Response } from 'express';+import {+ SyncMutationsInput,+ SyncMutationResult,+ SyncMutationStatus,+ Mutation,+ ApplyCreateTransactionMutation,+ ApplyUpdateTransactionMutation,+ ApplyDeleteTransactionMutation,+ CreateFileMutation,+} from '@colanode/core';++import { SelectUser } from '@/data/schema';+import { nodeService } from '@/services/node-service';+import { fileService } from '@/services/file-service';++export const mutationsSyncHandler = async (+ req: Request,+ res: Response+): Promise<void> => {+ const input = req.body as SyncMutationsInput;++ const results: SyncMutationResult[] = [];+ for (const mutation of input.mutations) {+ try {+ const status = await handleMutation(res.locals.user, mutation);+ results.push({+ id: mutation.id,+ status: status,+ });+ } catch (error) {+ console.error('Error handling local mutation', error);+ results.push({+ id: mutation.id,+ status: 'error',+ });+ }+ }++ console.log('executed mutations', results);+ res.status(200).json({ results });+};++const handleMutation = async (+ user: SelectUser,+ mutation: Mutation+): Promise<SyncMutationStatus> => {+ if (mutation.type === 'apply_create_transaction') {+ return await handleCreateTransaction(user, mutation);+ } else if (mutation.type === 'apply_update_transaction') {+ return await handleUpdateTransaction(user, mutation);+ } else if (mutation.type === 'apply_delete_transaction') {+ return await handleDeleteTransaction(user, mutation);+ } else if (mutation.type === 'create_file') {+ return await handleCreateFile(user, mutation);+ } else {+ return 'error';+ }+};++const handleCreateTransaction = async (+ user: SelectUser,+ mutation: ApplyCreateTransactionMutation+): Promise<SyncMutationStatus> => {+ const output = await nodeService.applyCreateTransaction(user, {+ id: mutation.data.id,+ nodeId: mutation.data.nodeId,+ rootId: mutation.data.rootId,+ data: mutation.data.data,+ createdAt: new Date(mutation.data.createdAt),+ });++ if (!output) {+ return 'error';+ }++ return 'success';+};++const handleUpdateTransaction = async (+ user: SelectUser,+ mutation: ApplyUpdateTransactionMutation+): Promise<SyncMutationStatus> => {+ const output = await nodeService.applyUpdateTransaction(user, {+ id: mutation.data.id,+ nodeId: mutation.data.nodeId,+ rootId: mutation.data.rootId,+ userId: mutation.data.createdBy,+ data: mutation.data.data,+ createdAt: new Date(mutation.data.createdAt),+ });++ if (!output) {+ return 'error';+ }++ return 'success';+};++const handleDeleteTransaction = async (+ user: SelectUser,+ mutation: ApplyDeleteTransactionMutation+): Promise<SyncMutationStatus> => {+ const output = await nodeService.applyDeleteTransaction(user, {+ id: mutation.data.id,+ nodeId: mutation.data.nodeId,+ rootId: mutation.data.rootId,+ createdAt: new Date(mutation.data.createdAt),+ });++ if (!output) {+ return 'error';+ }++ return 'success';+};++const handleCreateFile = async (+ user: SelectUser,+ mutation: CreateFileMutation+): Promise<SyncMutationStatus> => {+ const output = await fileService.createFile(user, mutation);+ return output ? 'success' : 'error';+};diff --git a/apps/server/src/controllers/client/workspaces/transactions/index.ts b/apps/server/src/controllers/client/workspaces/transactions/index.tsdeleted file mode 100644index a978e1b3..00000000--- a/apps/server/src/controllers/client/workspaces/transactions/index.ts+++ /dev/null@@ -1,2 +0,0 @@-export * from './transactions-get';-export * from './transactions-sync';diff --git a/apps/server/src/controllers/client/workspaces/transactions/transactions-get.ts b/apps/server/src/controllers/client/workspaces/transactions/transactions-get.tsdeleted file mode 100644index 197a3fc9..00000000--- a/apps/server/src/controllers/client/workspaces/transactions/transactions-get.ts+++ /dev/null@@ -1,35 +0,0 @@-import { Request, Response } from 'express';-import { GetTransactionsOutput, hasViewerAccess } from '@colanode/core';--import { ApiError } from '@/types/api';-import { database } from '@/data/database';-import { fetchNodeRole, mapTransaction } from '@/lib/nodes';--export const transactionsGetHandler = async (- req: Request,- res: Response-): Promise<void> => {- const nodeId = req.params.nodeId as string;-- const role = await fetchNodeRole(nodeId, res.locals.user.id);- if (role === null || !hasViewerAccess(role)) {- res.status(403).json({- code: ApiError.Forbidden,- message: 'Forbidden.',- });- return;- }-- const transactions = await database- .selectFrom('transactions')- .selectAll()- .where('node_id', '=', nodeId)- .orderBy('version', 'desc')- .execute();-- const output: GetTransactionsOutput = {- transactions: transactions.map(mapTransaction),- };-- res.status(200).json(output);-};diff --git a/apps/server/src/controllers/client/workspaces/transactions/transactions-sync.ts b/apps/server/src/controllers/client/workspaces/transactions/transactions-sync.tsdeleted file mode 100644index c8486341..00000000--- a/apps/server/src/controllers/client/workspaces/transactions/transactions-sync.ts+++ /dev/null@@ -1,112 +0,0 @@-import { Request, Response } from 'express';-import {- LocalCreateTransaction,- LocalDeleteTransaction,- LocalTransaction,- LocalUpdateTransaction,- SyncTransactionResult,- SyncTransactionsInput,- SyncTransactionStatus,-} from '@colanode/core';--import { SelectUser } from '@/data/schema';-import { nodeService } from '@/services/node-service';--export const transactionsSyncHandler = async (- req: Request,- res: Response-): Promise<void> => {- const input = req.body as SyncTransactionsInput;-- const results: SyncTransactionResult[] = [];- for (const transaction of input.transactions) {- try {- const status = await handleLocalTransaction(res.locals.user, transaction);- results.push({- id: transaction.id,- status: status,- });- } catch (error) {- console.error('Error handling local transaction', error);- results.push({- id: transaction.id,- status: 'error',- });- }- }-- console.log('executed mutations', results);- res.status(200).json({ results });-};--const handleLocalTransaction = async (- user: SelectUser,- transaction: LocalTransaction-): Promise<SyncTransactionStatus> => {- if (transaction.operation === 'create') {- return await handleCreateTransaction(user, transaction);- } else if (transaction.operation === 'update') {- return await handleUpdateTransaction(user, transaction);- } else if (transaction.operation === 'delete') {- return await handleDeleteTransaction(user, transaction);- } else {- return 'error';- }-};--const handleCreateTransaction = async (- user: SelectUser,- transaction: LocalCreateTransaction-): Promise<SyncTransactionStatus> => {- const output = await nodeService.applyCreateTransaction(user, {- id: transaction.id,- nodeId: transaction.nodeId,- rootId: transaction.rootId,- data: transaction.data,- createdAt: new Date(transaction.createdAt),- });-- if (!output) {- return 'error';- }-- return 'success';-};--const handleUpdateTransaction = async (- user: SelectUser,- transaction: LocalUpdateTransaction-): Promise<SyncTransactionStatus> => {- const output = await nodeService.applyUpdateTransaction(user, {- id: transaction.id,- nodeId: transaction.nodeId,- rootId: transaction.rootId,- userId: transaction.createdBy,- data: transaction.data,- createdAt: new Date(transaction.createdAt),- });-- if (!output) {- return 'error';- }-- return 'success';-};--const handleDeleteTransaction = async (- user: SelectUser,- transaction: LocalDeleteTransaction-): Promise<SyncTransactionStatus> => {- const output = await nodeService.applyDeleteTransaction(user, {- id: transaction.id,- nodeId: transaction.nodeId,- rootId: transaction.rootId,- createdAt: new Date(transaction.createdAt),- });-- if (!output) {- return 'error';- }-- return 'success';-};diff --git a/apps/server/src/data/migrations.ts b/apps/server/src/data/migrations.tsindex b8963aa3..cc246928 100644--- a/apps/server/src/data/migrations.ts+++ b/apps/server/src/data/migrations.ts@@ -215,6 +215,47 @@ const createCollaborationsTable: Migration = {},};+const createFilesTable: Migration = {+ up: async (db) => {+ await sql`+ CREATE SEQUENCE IF NOT EXISTS files_version_sequence+ START WITH 1000000000+ INCREMENT BY 1+ NO MINVALUE+ NO MAXVALUE+ CACHE 1;+ `.execute(db);++ await db.schema+ .createTable('files')+ .addColumn('id', 'varchar(30)', (col) => col.notNull().primaryKey())+ .addColumn('type', 'varchar(30)', (col) => col.notNull())+ .addColumn('parent_id', 'varchar(30)', (col) => col.notNull())+ .addColumn('root_id', 'varchar(30)', (col) => col.notNull())+ .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())+ .addColumn('name', 'varchar(256)', (col) => col.notNull())+ .addColumn('original_name', 'varchar(256)', (col) => col.notNull())+ .addColumn('mime_type', 'varchar(256)', (col) => col.notNull())+ .addColumn('extension', 'varchar(256)', (col) => col.notNull())+ .addColumn('size', 'integer', (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('status', 'integer', (col) => col.notNull())+ .addColumn('version', 'bigint', (col) =>+ col.notNull().defaultTo(sql`nextval('files_version_sequence')`)+ )+ .execute();+ },+ down: async (db) => {+ await db.schema.dropTable('files').execute();+ await sql`DROP SEQUENCE IF EXISTS files_version_sequence`.execute(db);+ },+};+const createNodePathsTable: Migration = {up: async (db) => {await db.schema@@ -289,29 +330,6 @@ const createNodePathsTable: Migration = {},};-const createUploadsTable: Migration = {- up: async (db) => {- await db.schema- .createTable('uploads')- .addColumn('node_id', 'varchar(30)', (col) =>- col.notNull().references('nodes.id').onDelete('no action').primaryKey()- )- .addColumn('upload_id', 'varchar(30)', (col) => col.notNull())- .addColumn('workspace_id', 'varchar(30)', (col) => col.notNull())- .addColumn('path', 'varchar(256)', (col) => col.notNull())- .addColumn('size', 'integer', (col) => col.notNull())- .addColumn('mime_type', 'varchar(256)', (col) => col.notNull())- .addColumn('type', 'varchar(30)', (col) => col.notNull())- .addColumn('created_by', 'varchar(30)', (col) => col.notNull())- .addColumn('created_at', 'timestamptz', (col) => col.notNull())- .addColumn('completed_at', 'timestamptz', (col) => col.notNull())- .execute();- },- down: async (db) => {- await db.schema.dropTable('uploads').execute();- },-};-const createInteractionsTable: Migration = {up: async (db) => {await sql`@@ -372,7 +390,7 @@ export const databaseMigrations: Record<string, Migration> = {'00005_create_nodes_table': createNodesTable,'00006_create_transactions_table': createTransactionsTable,'00007_create_collaborations_table': createCollaborationsTable,- '00008_create_node_paths_table': createNodePathsTable,- '00009_create_uploads_table': createUploadsTable,+ '00008_create_files_table': createFilesTable,+ '00009_create_node_paths_table': createNodePathsTable,'00010_create_interactions_table': createInteractionsTable,};diff --git a/apps/server/src/data/schema.ts b/apps/server/src/data/schema.tsindex 47818c7a..3c94e1d9 100644--- a/apps/server/src/data/schema.ts+++ b/apps/server/src/data/schema.ts@@ -1,4 +1,6 @@import {+ FileStatus,+ FileType,InteractionAttributes,NodeAttributes,NodeRole,@@ -145,6 +147,31 @@ export type SelectTransaction = Selectable<TransactionTable>;export type CreateTransaction = Insertable<TransactionTable>;export type UpdateTransaction = Updateable<TransactionTable>;+interface FileTable {+ id: ColumnType<string, string, never>;+ type: ColumnType<FileType, FileType, FileType>;+ parent_id: ColumnType<string, string, never>;+ root_id: ColumnType<string, string, never>;+ workspace_id: ColumnType<string, string, never>;+ name: ColumnType<string, string, never>;+ original_name: ColumnType<string, string, never>;+ mime_type: ColumnType<string, string, never>;+ extension: ColumnType<string, string, never>;+ size: ColumnType<number, number, never>;+ created_at: ColumnType<Date, Date, never>;+ created_by: ColumnType<string, string, never>;+ 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>;+ status: ColumnType<FileStatus, FileStatus, FileStatus>;+ version: ColumnType<bigint, never, never>;+}++export type SelectFile = Selectable<FileTable>;+export type CreateFile = Insertable<FileTable>;+export type UpdateFile = Updateable<FileTable>;+interface NodePathTable {ancestor_id: ColumnType<string, string, never>;descendant_id: ColumnType<string, string, never>;@@ -156,23 +183,6 @@ export type SelectNodePath = Selectable<NodePathTable>;export type CreateNodePath = Insertable<NodePathTable>;export type UpdateNodePath = Updateable<NodePathTable>;-interface UploadTable {- node_id: ColumnType<string, string, never>;- upload_id: ColumnType<string, string, never>;- workspace_id: ColumnType<string, string, never>;- path: ColumnType<string, string, string>;- size: ColumnType<number, number, number>;- mime_type: ColumnType<string, string, string>;- type: ColumnType<string, string, string>;- created_by: ColumnType<string, string, never>;- created_at: ColumnType<Date, Date, never>;- completed_at: ColumnType<Date, Date, never>;-}--export type SelectUpload = Selectable<UploadTable>;-export type CreateUpload = Insertable<UploadTable>;-export type UpdateUpload = Updateable<UploadTable>;-interface InteractionTable {user_id: ColumnType<string, string, never>;node_id: ColumnType<string, string, never>;@@ -201,7 +211,7 @@ export interface DatabaseSchema {nodes: NodeTable;transactions: TransactionTable;collaborations: CollaborationTable;+ files: FileTable;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.tsindex 53e4f6b6..c42f3d6a 100644--- a/apps/server/src/jobs/clean-node-data.ts+++ b/apps/server/src/jobs/clean-node-data.ts@@ -1,10 +1,10 @@import { generateId, IdType } from '@colanode/core';-import { DeleteObjectCommand } from '@aws-sdk/client-s3';+// import { DeleteObjectCommand } from '@aws-sdk/client-s3';import { database } from '@/data/database';-import { CreateTransaction, SelectUpload } from '@/data/schema';+import { CreateTransaction } from '@/data/schema';import { JobHandler } from '@/types/jobs';-import { filesStorage, BUCKET_NAMES } from '@/data/storage';+// import { filesStorage, BUCKET_NAMES } from '@/data/storage';import { eventBus } from '@/lib/event-bus';import { createLogger } from '@/lib/logger';@@ -87,18 +87,18 @@ const deleteChildren = async (break;}- const fileIds: string[] = descendants- .filter((d) => d.type === 'file')- .map((d) => d.id);+ // const fileIds: string[] = descendants+ // .filter((d) => d.type === 'file')+ // .map((d) => d.id);- const uploads: SelectUpload[] =- fileIds.length > 0- ? await database- .selectFrom('uploads')- .selectAll()- .where('node_id', 'in', fileIds)- .execute()- : [];+ // const uploads: SelectUpload[] =+ // fileIds.length > 0+ // ? await database+ // .selectFrom('uploads')+ // .selectAll()+ // .where('node_id', 'in', fileIds)+ // .execute()+ // : [];const nodeIds: string[] = descendants.map((d) => d.id);const transactionsToCreate: CreateTransaction[] = descendants.map(@@ -113,7 +113,7 @@ const deleteChildren = async (server_created_at: new Date(),}));- const uploadsToDelete: string[] = uploads.map((u) => u.node_id);+ // const uploadsToDelete: string[] = uploads.map((u) => u.node_id);await database.transaction().execute(async (trx) => {await trx@@ -131,12 +131,12 @@ const deleteChildren = async (throw new Error('Failed to create transactions');}- if (uploadsToDelete.length > 0) {- await trx- .deleteFrom('uploads')- .where('node_id', 'in', uploadsToDelete)- .execute();- }+ // if (uploadsToDelete.length > 0) {+ // await trx+ // .deleteFrom('uploads')+ // .where('node_id', 'in', uploadsToDelete)+ // .execute();+ // }await trx.deleteFrom('nodes').where('id', 'in', nodeIds).execute();await trx@@ -149,18 +149,18 @@ const deleteChildren = async (.execute();});- for (const upload of uploads) {- const command = new DeleteObjectCommand({- Bucket: BUCKET_NAMES.FILES,- Key: upload.path,- });+ // for (const upload of uploads) {+ // const command = new DeleteObjectCommand({+ // Bucket: BUCKET_NAMES.FILES,+ // Key: upload.path,+ // });- logger.trace(- `Deleting file as a descendant of ${parentIds}: ${upload.path}`- );+ // logger.trace(+ // `Deleting file as a descendant of ${parentIds}: ${upload.path}`+ // );- await filesStorage.send(command);- }+ // await filesStorage.send(command);+ // }for (const node of descendants) {logger.trace(`Publishing node deleted event for ${node.id}`);diff --git a/apps/server/src/lib/nodes.ts b/apps/server/src/lib/nodes.tsindex 55e4e929..70f8826e 100644--- a/apps/server/src/lib/nodes.ts+++ b/apps/server/src/lib/nodes.ts@@ -6,6 +6,7 @@ import {NodeRole,NodeType,ServerCollaboration,+ ServerFile,ServerInteraction,ServerTransaction,ServerUser,@@ -19,6 +20,7 @@ import {SelectCollaboration,SelectTransaction,SelectUser,+ SelectFile,} from '@/data/schema';import { NodeCollaborator } from '@/types/nodes';@@ -154,6 +156,29 @@ export const mapCollaboration = (};};+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 fetchNode = async (nodeId: string): Promise<SelectNode | null> => {const result = await database.selectFrom('nodes')diff --git a/apps/server/src/routes/client.ts b/apps/server/src/routes/client.tsindex 916893a6..49002afe 100644--- a/apps/server/src/routes/client.ts+++ b/apps/server/src/routes/client.ts@@ -19,9 +19,8 @@ import {fileDownloadGetHandler,fileUploadInitHandler,fileUploadCompleteHandler,- transactionsGetHandler,- transactionsSyncHandler,avatarUploadParameter,+ mutationsSyncHandler,} from '@/controllers/client';import { workspaceMiddleware } from '@/middlewares/workspace';import { authMiddleware } from '@/middlewares/auth';@@ -104,29 +103,22 @@ clientRouter.get();clientRouter.post(- '/v1/workspaces/:workspaceId/uploads',+ '/v1/workspaces/:workspaceId/files',authMiddleware,workspaceMiddleware,fileUploadInitHandler);clientRouter.put(- '/v1/workspaces/:workspaceId/uploads/:uploadId',+ '/v1/workspaces/:workspaceId/files/:fileId',authMiddleware,workspaceMiddleware,fileUploadCompleteHandler);-clientRouter.get(- '/v1/workspaces/:workspaceId/transactions/:nodeId',- authMiddleware,- workspaceMiddleware,- transactionsGetHandler-);-clientRouter.post(- '/v1/workspaces/:workspaceId/transactions',+ '/v1/workspaces/:workspaceId/mutations',authMiddleware,workspaceMiddleware,- transactionsSyncHandler+ mutationsSyncHandler);diff --git a/apps/server/src/services/file-service.ts b/apps/server/src/services/file-service.tsnew file mode 100644index 00000000..430b0179--- /dev/null+++ b/apps/server/src/services/file-service.ts@@ -0,0 +1,78 @@+import {+ CreateFileMutation,+ extractNodeRole,+ FileStatus,+ hasCollaboratorAccess,+} from '@colanode/core';++import { database } from '@/data/database';+import { SelectUser } from '@/data/schema';+import { mapNode } from '@/lib/nodes';+import { eventBus } from '@/lib/event-bus';++class FileService {+ public async createFile(+ user: SelectUser,+ mutation: CreateFileMutation+ ): Promise<boolean> {+ const existingFile = await database+ .selectFrom('files')+ .where('id', '=', mutation.data.id)+ .executeTakeFirst();++ if (existingFile) {+ return true;+ }++ const root = await database+ .selectFrom('nodes')+ .selectAll()+ .where('id', '=', mutation.data.rootId)+ .executeTakeFirst();++ if (!root) {+ return false;+ }++ const rootNode = mapNode(root);+ const role = extractNodeRole(rootNode, user.id);+ if (!hasCollaboratorAccess(role)) {+ return false;+ }++ const createdFile = await database+ .insertInto('files')+ .returningAll()+ .values({+ id: mutation.data.id,+ type: mutation.data.fileType,+ parent_id: mutation.data.parentId,+ root_id: mutation.data.rootId,+ workspace_id: root.workspace_id,+ name: mutation.data.name,+ original_name: mutation.data.originalName,+ extension: mutation.data.extension,+ mime_type: mutation.data.mimeType,+ size: mutation.data.size,+ created_by: user.id,+ created_at: new Date(mutation.data.createdAt),+ status: FileStatus.Pending,+ })+ .executeTakeFirst();++ if (!createdFile) {+ return false;+ }++ eventBus.publish({+ type: 'file_created',+ fileId: createdFile.id,+ rootId: createdFile.root_id,+ workspaceId: createdFile.workspace_id,+ });++ return true;+ }+}++export const fileService = new FileService();diff --git a/apps/server/src/services/socket-connection.ts b/apps/server/src/services/socket-connection.tsindex aa39211d..bbab2d7c 100644--- a/apps/server/src/services/socket-connection.ts+++ b/apps/server/src/services/socket-connection.ts@@ -6,6 +6,7 @@ import {ConsumeCollaborationsMessage,ConsumeTransactionsMessage,ConsumeInteractionsMessage,+ ConsumeFilesMessage,} from '@colanode/core';import { interactionService } from '@/services/interaction-service';@@ -23,10 +24,12 @@ 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';type NodeConsumersWrapper = {transactions: TransactionsConsumer;interactions: InteractionsConsumer;+ files: FilesConsumer;};type ConsumersWrapper = {@@ -105,6 +108,8 @@ export class SocketConnection {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 === 'sync_interactions') {interactionService.syncLocalInteractions(this.account.id, message);}@@ -153,6 +158,7 @@ export class SocketConnection {nodeConsumers = {transactions: new TransactionsConsumer(user.user, message.rootId),interactions: new InteractionsConsumer(user.user, message.rootId),+ files: new FilesConsumer(user.user, message.rootId),};user.consumers.nodes.set(message.rootId, nodeConsumers);@@ -176,6 +182,7 @@ export class SocketConnection {nodeConsumers = {transactions: new TransactionsConsumer(user.user, message.rootId),interactions: new InteractionsConsumer(user.user, message.rootId),+ files: new FilesConsumer(user.user, message.rootId),};user.consumers.nodes.set(message.rootId, nodeConsumers);@@ -184,6 +191,30 @@ export class SocketConnection {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) {+ 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),+ };++ user.consumers.nodes.set(message.rootId, nodeConsumers);+ }++ await nodeConsumers.files.consume(message);+ }+private async getOrCreateUser(userId: string): Promise<SocketUser | null> {const existingUser = this.users.get(userId);if (existingUser) {diff --git a/apps/server/src/types/events.ts b/apps/server/src/types/events.tsindex d3b5c34f..c11d6e8a 100644--- a/apps/server/src/types/events.ts+++ b/apps/server/src/types/events.ts@@ -38,6 +38,27 @@ export type CollaborationUpdatedEvent = {workspaceId: string;};+export type FileCreatedEvent = {+ type: 'file_created';+ fileId: string;+ rootId: string;+ workspaceId: string;+};++export type FileUpdatedEvent = {+ type: 'file_updated';+ fileId: string;+ rootId: string;+ workspaceId: string;+};++export type FileDeletedEvent = {+ type: 'file_deleted';+ fileId: string;+ rootId: string;+ workspaceId: string;+};+export type InteractionUpdatedEvent = {type: 'interaction_updated';userId: string;@@ -99,4 +120,7 @@ export type Event =| WorkspaceCreatedEvent| WorkspaceUpdatedEvent| WorkspaceDeletedEvent- | DeviceDeletedEvent;+ | DeviceDeletedEvent+ | FileCreatedEvent+ | FileUpdatedEvent+ | FileDeletedEvent;diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex ab888137..54ea8126 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -10,7 +10,6 @@ export * from './registry/chat';export * from './registry/core';export * from './registry/database';export * from './registry/fields';-export * from './registry/file';export * from './registry/folder';export * from './registry/index';export * from './registry/message';@@ -27,3 +26,4 @@ export * from './types/servers';export * from './types/sync';export * from './types/workspaces';export * from './lib/blocks';+export * from './types/mutations';diff --git a/packages/core/src/lib/files.ts b/packages/core/src/lib/files.tsindex 186a66a0..9aba4912 100644--- a/packages/core/src/lib/files.ts+++ b/packages/core/src/lib/files.ts@@ -14,7 +14,7 @@ export const extractFileType = (mimeType: string): FileType => {}if (mimeType.startsWith('application/pdf')) {- return 'document';+ return 'pdf';}return 'other';diff --git a/packages/core/src/registry/file.ts b/packages/core/src/registry/file.tsdeleted file mode 100644index 88468279..00000000--- a/packages/core/src/registry/file.ts+++ /dev/null@@ -1,44 +0,0 @@-import { z } from 'zod';--import { NodeModel } from './core';--export const fileAttributesSchema = z.object({- type: z.literal('file'),- subtype: z.enum(['image', 'video', 'audio', 'document', 'other']),- name: z.string(),- parentId: z.string(),- mimeType: z.string(),- size: z.number(),- extension: z.string(),- fileName: z.string(),- uploadStatus: z- .enum(['pending', 'completed', 'failed', 'no_space'])- .default('pending'),- uploadId: z.string(),-});--export type FileAttributes = z.infer<typeof fileAttributesSchema>;--export const fileModel: NodeModel = {- type: 'file',- schema: fileAttributesSchema,- getName: (_, attributes) => {- if (attributes.type !== 'file') {- return undefined;- }-- return attributes.name;- },- getText: () => {- return undefined;- },- canCreate: async (_, __) => {- return true;- },- canUpdate: async (_, __, ___) => {- return true;- },- canDelete: async (_, __) => {- return true;- },-};diff --git a/packages/core/src/registry/index.ts b/packages/core/src/registry/index.tsindex ca85ef80..58dddd26 100644--- a/packages/core/src/registry/index.ts+++ b/packages/core/src/registry/index.ts@@ -2,7 +2,6 @@ import { ChannelAttributes, channelModel } from './channel';import { ChatAttributes, chatModel } from './chat';import { NodeModel } from './core';import { DatabaseAttributes, databaseModel } from './database';-import { FileAttributes, fileModel } from './file';import { FolderAttributes, folderModel } from './folder';import { MessageAttributes, messageModel } from './message';import { PageAttributes, pageModel } from './page';@@ -44,11 +43,6 @@ export type DatabaseNode = NodeBase & {attributes: DatabaseAttributes;};-export type FileNode = NodeBase & {- type: 'file';- attributes: FileAttributes;-};-export type FolderNode = NodeBase & {type: 'folder';attributes: FolderAttributes;@@ -78,7 +72,6 @@ export type NodeType =| 'channel'| 'chat'| 'database'- | 'file'| 'folder'| 'message'| 'page'@@ -90,7 +83,6 @@ export type NodeAttributes =| DatabaseAttributes| ChannelAttributes| ChatAttributes- | FileAttributes| FolderAttributes| MessageAttributes| PageAttributes@@ -100,7 +92,6 @@ export type Node =| ChannelNode| ChatNode| DatabaseNode- | FileNode| FolderNode| MessageNode| PageNode@@ -114,7 +105,6 @@ class Registry {this.models.set('channel', channelModel);this.models.set('chat', chatModel);this.models.set('database', databaseModel);- this.models.set('file', fileModel);this.models.set('folder', folderModel);this.models.set('message', messageModel);this.models.set('page', pageModel);diff --git a/packages/core/src/types/files.ts b/packages/core/src/types/files.tsindex bc883472..21229a18 100644--- a/packages/core/src/types/files.ts+++ b/packages/core/src/types/files.ts@@ -1,19 +1,8 @@export type CreateUploadInput = {fileId: string;- uploadId: string;-};--export type UploadMetadata = {- fileId: string;- uploadId: string;- path: string;- mimeType: string;- size: number;- createdAt: string;};export type CreateUploadOutput = {- uploadId: string;url: string;};@@ -25,4 +14,10 @@ export type CompleteUploadOutput = {success: boolean;};-export type FileType = 'image' | 'video' | 'audio' | 'document' | 'other';+export type FileType = 'image' | 'video' | 'audio' | 'pdf' | 'other';++export enum FileStatus {+ Pending = 0,+ Ready = 1,+ Error = 2,+}diff --git a/packages/core/src/types/messages.ts b/packages/core/src/types/messages.tsindex 568b0d21..67851d70 100644--- a/packages/core/src/types/messages.ts+++ b/packages/core/src/types/messages.ts@@ -1,6 +1,7 @@import { InteractionEvent } from './interactions';import {ServerCollaboration,+ ServerFile,ServerInteraction,ServerTransaction,ServerUser,@@ -32,6 +33,13 @@ export type UsersBatchMessage = {users: ServerUser[];};+export type FilesBatchMessage = {+ type: 'files_batch';+ userId: string;+ rootId: string;+ files: ServerFile[];+};+export type SyncInteractionsMessage = {type: 'sync_interactions';userId: string;@@ -66,6 +74,13 @@ export type ConsumeTransactionsMessage = {cursor: string;};+export type ConsumeFilesMessage = {+ type: 'consume_files';+ userId: string;+ rootId: string;+ cursor: string;+};+export type AccountUpdatedMessage = {type: 'account_updated';accountId: string;@@ -93,4 +108,6 @@ export type Message =| ConsumeTransactionsMessage| AccountUpdatedMessage| WorkspaceUpdatedMessage- | WorkspaceDeletedMessage;+ | WorkspaceDeletedMessage+ | FilesBatchMessage+ | ConsumeFilesMessage;diff --git a/packages/core/src/types/mutations.ts b/packages/core/src/types/mutations.tsnew file mode 100644index 00000000..fe012b52--- /dev/null+++ b/packages/core/src/types/mutations.ts@@ -0,0 +1,67 @@+import { FileType } from './files';+import {+ LocalCreateTransaction,+ LocalDeleteTransaction,+ LocalUpdateTransaction,+} from './sync';++export type SyncMutationsInput = {+ mutations: Mutation[];+};++export type SyncMutationsOutput = {+ results: SyncMutationResult[];+};++export type SyncMutationStatus = 'success' | 'error';++export type SyncMutationResult = {+ id: string;+ status: SyncMutationStatus;+};++export type MutationBase = {+ id: string;+ createdAt: string;+};++export type CreateFileMutationData = {+ id: string;+ fileType: FileType;+ parentId: string;+ rootId: string;+ name: string;+ originalName: string;+ extension: string;+ mimeType: string;+ size: number;+ createdAt: string;+};++export type CreateFileMutation = MutationBase & {+ type: 'create_file';+ data: CreateFileMutationData;+};++export type ApplyCreateTransactionMutation = MutationBase & {+ type: 'apply_create_transaction';+ data: LocalCreateTransaction;+};++export type ApplyUpdateTransactionMutation = MutationBase & {+ type: 'apply_update_transaction';+ data: LocalUpdateTransaction;+};++export type ApplyDeleteTransactionMutation = MutationBase & {+ type: 'apply_delete_transaction';+ data: LocalDeleteTransaction;+};++export type Mutation =+ | CreateFileMutation+ | ApplyCreateTransactionMutation+ | ApplyUpdateTransactionMutation+ | ApplyDeleteTransactionMutation;++export type MutationType = Mutation['type'];diff --git a/packages/core/src/types/sync.ts b/packages/core/src/types/sync.tsindex 3fcdb1ed..df02dc5c 100644--- a/packages/core/src/types/sync.ts+++ b/packages/core/src/types/sync.ts@@ -1,23 +1,9 @@import { InteractionAttributes } from './interactions';import { WorkspaceRole } from './workspaces';+import { FileStatus, FileType } from './files';import { NodeRole } from '../registry/core';-export type SyncTransactionsInput = {- transactions: LocalTransaction[];-};--export type SyncTransactionsOutput = {- results: SyncTransactionResult[];-};--export type SyncTransactionStatus = 'success' | 'error';--export type SyncTransactionResult = {- id: string;- status: SyncTransactionStatus;-};-export type LocalTransaction =| LocalCreateTransaction| LocalUpdateTransaction@@ -143,3 +129,24 @@ export type ServerUser = {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;+};