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
Fix issues with sync
Hakan Shehu633133b65c22
diff --git a/desktop/src/main/data/database-manager.ts b/desktop/src/main/data/database-manager.tsindex 2318a804..1d941bd3 100644--- a/desktop/src/main/data/database-manager.ts+++ b/desktop/src/main/data/database-manager.ts@@ -57,7 +57,7 @@ class DatabaseManager {}const workspaceDatabase = await this.initWorkspaceDatabase(- userId,+ workspace.account_id,workspace.workspace_id,);@@ -65,6 +65,29 @@ class DatabaseManager {return workspaceDatabase;}+ public async deleteWorkspaceDatabase(+ accountId: string,+ workspaceId: string,+ userId: string,+ ): Promise<void> {+ await this.waitForInit();++ if (this.workspaceDatabases.has(userId)) {+ this.workspaceDatabases.delete(userId);+ }++ const workspaceDir = path.join(+ this.appPath,+ accountId,+ 'workspaces',+ workspaceId,+ );++ if (fs.existsSync(workspaceDir)) {+ fs.rmSync(workspaceDir, { recursive: true, force: true });+ }+ }+private async waitForInit(): Promise<void> {if (!this.initPromise) {this.initPromise = this.executeInit();@@ -83,7 +106,7 @@ class DatabaseManager {for (const workspace of workspaces) {const workspaceDatabase = await this.initWorkspaceDatabase(- workspace.user_id,+ workspace.account_id,workspace.workspace_id,);diff --git a/desktop/src/main/synchronizer.ts b/desktop/src/main/synchronizer.tsindex 33418a99..7702a8e0 100644--- a/desktop/src/main/synchronizer.ts+++ b/desktop/src/main/synchronizer.ts@@ -67,8 +67,8 @@ class Synchronizer {.execute();for (const workspace of workspaces) {- if (this.workspaceBackoffs.has(workspace.workspace_id)) {- const backoff = this.workspaceBackoffs.get(workspace.workspace_id);+ if (this.workspaceBackoffs.has(workspace.user_id)) {+ const backoff = this.workspaceBackoffs.get(workspace.user_id);if (!backoff.canRetry()) {return;}@@ -135,22 +135,22 @@ class Synchronizer {.execute();}} catch (error) {- if (!this.workspaceBackoffs.has(workspace.workspace_id)) {+ if (!this.workspaceBackoffs.has(workspace.user_id)) {this.workspaceBackoffs.set(- workspace.workspace_id,+ workspace.user_id,new BackoffCalculator(),);}- const backoff = this.workspaceBackoffs.get(workspace.workspace_id);+ const backoff = this.workspaceBackoffs.get(workspace.user_id);backoff.increaseError();}}}private async syncWorkspace(workspace: SelectWorkspace): Promise<void> {- if (this.workspaceBackoffs.has(workspace.workspace_id)) {- const backoff = this.workspaceBackoffs.get(workspace.workspace_id);+ if (this.workspaceBackoffs.has(workspace.user_id)) {+ const backoff = this.workspaceBackoffs.get(workspace.user_id);if (!backoff.canRetry()) {return;}@@ -177,15 +177,17 @@ class Synchronizer {`/v1/sync/${workspace.workspace_id}`,);+ await databaseManager.deleteWorkspaceDatabase(+ workspace.account_id,+ workspace.workspace_id,+ workspace.user_id,+ );+const workspaceDatabase = await databaseManager.getWorkspaceDatabase(workspace.user_id,);await workspaceDatabase.transaction().execute(async (trx) => {- await trx.deleteFrom('nodes').execute();- await trx.deleteFrom('node_reactions').execute();- await trx.deleteFrom('node_collaborators').execute();-if (data.nodes.length > 0) {await trx.insertInto('nodes')@@ -225,6 +227,29 @@ class Synchronizer {).execute();}++ if (data.nodeCollaborators.length > 0) {+ await trx+ .insertInto('node_collaborators')+ .values(+ data.nodeCollaborators.map((nodeCollaborator) => {+ return {+ node_id: nodeCollaborator.nodeId,+ collaborator_id: nodeCollaborator.collaboratorId,+ role: nodeCollaborator.role,+ created_at: nodeCollaborator.createdAt,+ created_by: nodeCollaborator.createdBy,+ updated_at: nodeCollaborator.updatedAt,+ updated_by: nodeCollaborator.updatedBy,+ version_id: nodeCollaborator.versionId,+ server_created_at: nodeCollaborator.serverCreatedAt,+ server_updated_at: nodeCollaborator.serverUpdatedAt,+ server_version_id: nodeCollaborator.versionId,+ };+ }),+ )+ .execute();+ }});await databaseManager.appDatabase@@ -235,14 +260,11 @@ class Synchronizer {.where('user_id', '=', workspace.user_id).execute();} catch (error) {- if (!this.workspaceBackoffs.has(workspace.workspace_id)) {- this.workspaceBackoffs.set(- workspace.workspace_id,- new BackoffCalculator(),- );+ if (!this.workspaceBackoffs.has(workspace.user_id)) {+ this.workspaceBackoffs.set(workspace.user_id, new BackoffCalculator());}- const backoff = this.workspaceBackoffs.get(workspace.workspace_id);+ const backoff = this.workspaceBackoffs.get(workspace.user_id);backoff.increaseError();}}diff --git a/server/src/routes/sync.ts b/server/src/routes/sync.tsindex 7bcfbe5c..0f6de5fd 100644--- a/server/src/routes/sync.ts+++ b/server/src/routes/sync.ts@@ -80,7 +80,7 @@ syncRouter.get(res.status(200).json({nodes,nodeReactions,- nodeCollabors: nodeCollaborators,+ nodeCollaborators,});},);