From 47e976b0bba8a6b228062a2cd69ee5d429ab30e8 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 14 Dec 2024 15:01:26 +0000 Subject: [PATCH] Simplify change handlers --- plugin/src/services/types.ts | 61 +++------------- .../sync-locally-deleted-file.ts | 15 ++-- .../sync-locally-renamed-file.ts | 57 --------------- .../sync-locally-updated-file.ts | 73 +++++++++++++++++-- .../src/sync-functions/sync-new-local-file.ts | 48 ------------ 5 files changed, 84 insertions(+), 170 deletions(-) delete mode 100644 plugin/src/sync-functions/sync-locally-renamed-file.ts delete mode 100644 plugin/src/sync-functions/sync-new-local-file.ts diff --git a/plugin/src/services/types.ts b/plugin/src/services/types.ts index 34e160c..2bb2122 100644 --- a/plugin/src/services/types.ts +++ b/plugin/src/services/types.ts @@ -71,40 +71,14 @@ export interface paths { }; }; put?: never; - post: { - parameters: { - query?: never; - header: { - authorization: string; - }; - path: { - vault_id: string; - }; - cookie?: never; - }; - requestBody: { - content: { - "application/json": components["schemas"]["CreateDocumentVersion"]; - }; - }; - responses: { - 200: { - headers: { - [name: string]: unknown; - }; - content: { - "application/json": components["schemas"]["DocumentVersion"]; - }; - }; - }; - }; + post?: never; delete?: never; options?: never; head?: never; patch?: never; trace?: never; }; - "/vaults/{vault_id}/documents/{document_id}": { + "/vaults/{vault_id}/documents/{relative_path}": { parameters: { query?: never; header?: never; @@ -118,7 +92,7 @@ export interface paths { authorization: string; }; path: { - document_id: string; + relative_path: string; vault_id: string; }; cookie?: never; @@ -142,7 +116,7 @@ export interface paths { authorization: string; }; path: { - document_id: string; + relative_path: string; vault_id: string; }; cookie?: never; @@ -171,7 +145,7 @@ export interface paths { authorization: string; }; path: { - document_id: string; + relative_path: string; vault_id: string; }; cookie?: never; @@ -237,12 +211,6 @@ export interface paths { export type webhooks = Record; export interface components { schemas: { - CreateDocumentVersion: { - contentBase64: string; - /** Format: date-time */ - createdDate: string; - relativePath: string; - }; DeleteDocumentVersion: { /** Format: date-time */ createdDate: string; @@ -251,8 +219,6 @@ export interface components { contentBase64: string; /** Format: date-time */ createdDate: string; - /** Format: uuid */ - documentId: string; isDeleted: boolean; relativePath: string; /** Format: date-time */ @@ -264,8 +230,6 @@ export interface components { DocumentVersionWithoutContent: { /** Format: date-time */ createdDate: string; - /** Format: uuid */ - documentId: string; isDeleted: boolean; relativePath: string; /** Format: date-time */ @@ -278,21 +242,15 @@ export interface components { vault_id: string; }; PathParams2: { + relative_path: string; vault_id: string; }; PathParams3: { - /** Format: uuid */ - document_id: string; + relative_path: string; vault_id: string; }; PathParams4: { - /** Format: uuid */ - document_id: string; - vault_id: string; - }; - PathParams5: { - /** Format: uuid */ - document_id: string; + relative_path: string; vault_id: string; }; PingResponse: { @@ -304,8 +262,7 @@ export interface components { /** Format: date-time */ createdDate: string; /** Format: int64 */ - parentVersionId: number; - relativePath: string; + parentVersionId?: number | null; }; }; responses: never; diff --git a/plugin/src/sync-functions/sync-locally-deleted-file.ts b/plugin/src/sync-functions/sync-locally-deleted-file.ts index f439e61..fe75c6c 100644 --- a/plugin/src/sync-functions/sync-locally-deleted-file.ts +++ b/plugin/src/sync-functions/sync-locally-deleted-file.ts @@ -1,22 +1,27 @@ import { Database } from "src/database/database"; import { RelativePath } from "src/database/document-metadata"; +import { Logger } from "src/logger"; import { SyncServer } from "src/services/sync_service"; export async function syncLocallyDeletedFile( database: Database, syncServer: SyncServer, - path: RelativePath + relativePath: RelativePath ) { - const metadata = database.getDocument(path); + const metadata = database.getDocument(relativePath); if (!metadata) { - throw `Document metadata not found for ${path}`; + Logger.getInstance().warn( + `Document metadata not found for ${relativePath}` + ); } await syncServer.delete({ - documentId: metadata.documentId, + relativePath, // We got the event now, so it must have been deleted just now createdDate: new Date(), }); - await database.removeDocument(path); + if (metadata) { + await database.removeDocument(relativePath); + } } diff --git a/plugin/src/sync-functions/sync-locally-renamed-file.ts b/plugin/src/sync-functions/sync-locally-renamed-file.ts deleted file mode 100644 index d6d4bc8..0000000 --- a/plugin/src/sync-functions/sync-locally-renamed-file.ts +++ /dev/null @@ -1,57 +0,0 @@ -import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js"; -import { TFile } from "obsidian"; -import { Database } from "src/database/database"; -import { Logger } from "src/logger"; -import { SyncServer } from "src/services/sync_service"; -import { hash } from "src/utils"; - -export async function syncLocallyRenamedFile( - database: Database, - syncServer: SyncServer, - file: TFile, - oldPath: string -) { - const metadata = database.getDocument(oldPath); - if (!metadata) { - throw `Document metadata not found for ${oldPath}`; - } - - const contentBytes = new Uint8Array(await file.vault.readBinary(file)); - const responsePromise = syncServer.update({ - documentId: metadata.documentId, - parentVersionId: metadata.parentVersionId, - relativePath: file.path, - contentBytes, - createdDate: new Date(file.stat.ctime), - }); - - const contentHash = hash(contentBytes); - const response = await responsePromise; - - const localDbUpdatePromise = database.moveDocument({ - oldRelativePath: oldPath, - relativePath: file.path, - documentId: response.documentId, - parentVersionId: response.versionId, - hash: contentHash, - }); - - if (file.path !== response.relativePath) { - await file.vault.rename(file, response.relativePath); - } - - const newContentBytes = new Uint8Array(await file.vault.readBinary(file)); - const responseBytes = lib.base64_to_bytes(response.contentBase64); - - if (contentBytes !== newContentBytes) { - Logger.getInstance().info( - `Content changed since sending original update request for ${file.path}` - ); - - const result = lib.merge(contentBytes, newContentBytes, responseBytes); - - await file.vault.modifyBinary(file, result); - } - - await localDbUpdatePromise; -} diff --git a/plugin/src/sync-functions/sync-locally-updated-file.ts b/plugin/src/sync-functions/sync-locally-updated-file.ts index ac7cf2c..b9e40c6 100644 --- a/plugin/src/sync-functions/sync-locally-updated-file.ts +++ b/plugin/src/sync-functions/sync-locally-updated-file.ts @@ -1,13 +1,70 @@ +import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js"; import { TFile } from "obsidian"; import { Database } from "src/database/database"; +import { Logger } from "src/logger"; import { SyncServer } from "src/services/sync_service"; -import { hash } from "src/utils"; -import { syncLocallyRenamedFile } from "./sync-locally-renamed-file"; +import { hash, isEqualBytes } from "src/utils"; -export async function syncLocallyUpdatedFile( - database: Database, - syncServer: SyncServer, - file: TFile -) { - syncLocallyRenamedFile(database, syncServer, file, file.path); +export async function syncLocallyUpdatedFile({ + database, + syncServer, + file, + oldPath, +}: { + database: Database; + syncServer: SyncServer; + file: TFile; + oldPath?: string; +}) { + const contentBytes = new Uint8Array(await file.vault.readBinary(file)); + const contentHash = hash(contentBytes); + + const metadata = database.getDocument(oldPath || file.path); + if (!metadata) { + Logger.getInstance().info( + `Document metadata not found for ${ + oldPath || file.path + }, it must be new` + ); + } else if (metadata.hash === contentHash) { + Logger.getInstance().info( + `Document hash matches, no need to sync ${file.path}` + ); + return; + } + + const response = await syncServer.put({ + parentVersionId: metadata?.parentVersionId, + relativePath: file.path, + contentBytes, + createdDate: new Date(file.stat.ctime), + }); + + const localDbUpdatePromise = database.moveDocument({ + oldRelativePath: oldPath || file.path, + relativePath: file.path, + parentVersionId: response.versionId, + hash: contentHash, + }); + + if (file.path !== response.relativePath) { + await file.vault.rename(file, response.relativePath); + } + + const newContentBytes = new Uint8Array(await file.vault.readBinary(file)); + const responseBytes = lib.base64_to_bytes(response.contentBase64); + + if (!isEqualBytes(contentBytes, newContentBytes)) { + Logger.getInstance().info( + `Content changed since sending original update request for ${file.path}` + ); + + const result = lib.merge(contentBytes, newContentBytes, responseBytes); + + await file.vault.modifyBinary(file, result); + } else { + await file.vault.modifyBinary(file, responseBytes); + } + + await localDbUpdatePromise; } diff --git a/plugin/src/sync-functions/sync-new-local-file.ts b/plugin/src/sync-functions/sync-new-local-file.ts deleted file mode 100644 index 2cc6baf..0000000 --- a/plugin/src/sync-functions/sync-new-local-file.ts +++ /dev/null @@ -1,48 +0,0 @@ -import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js"; -import { TFile } from "obsidian"; -import { Database } from "src/database/database"; -import { Logger } from "src/logger.js"; -import { SyncServer } from "src/services/sync_service"; -import { hash } from "src/utils"; - -export async function syncNewLocalFile( - database: Database, - syncServer: SyncServer, - file: TFile -) { - const contentBytes = new Uint8Array(await file.vault.readBinary(file)); - const responsePromise = syncServer.create({ - relativePath: file.path, - contentBytes, - createdDate: new Date(file.stat.ctime), - }); - - const contentHash = hash(contentBytes); - const response = await responsePromise; - - const localDbUpdatePromise = database.setDocument({ - relativePath: response.relativePath, - documentId: response.documentId, - parentVersionId: response.versionId, - hash: contentHash, - }); - - if (file.path !== response.relativePath) { - await file.vault.rename(file, response.relativePath); - } - - const newContentBytes = new Uint8Array(await file.vault.readBinary(file)); - const responseBytes = lib.base64_to_bytes(response.contentBase64); - - if (contentBytes !== newContentBytes) { - Logger.getInstance().info( - `Content changed since sending original create request for ${file.path}` - ); - - const result = lib.merge(contentBytes, newContentBytes, responseBytes); - - await file.vault.modifyBinary(file, result); - } - - await localDbUpdatePromise; -}