From 570b5f9bfce7a06f6f5289b4ff1ebd88678011bc Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 14 Dec 2024 17:06:40 +0000 Subject: [PATCH] Use new API --- plugin/src/database/database.ts | 6 +++--- plugin/src/database/document-metadata.ts | 4 ++-- plugin/src/services/sync_service.ts | 16 ++++++++-------- plugin/src/services/types.ts | 19 +++++++++++++++---- .../sync-locally-updated-file.ts | 5 +++-- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/plugin/src/database/database.ts b/plugin/src/database/database.ts index 2fa58a1..43d20e4 100644 --- a/plugin/src/database/database.ts +++ b/plugin/src/database/database.ts @@ -3,7 +3,7 @@ import { DEFAULT_SETTINGS, SyncSettings } from "./sync-settings"; import { RelativePath, DocumentMetadata, - DocumentVersionId, + VaultUpdateId, } from "./document-metadata"; interface StoredDatabase { @@ -91,7 +91,7 @@ export class Database { hash, }: { relativePath: RelativePath; - parentVersionId: DocumentVersionId; + parentVersionId: VaultUpdateId; hash: string; }): Promise { this._documents.set(relativePath, { @@ -109,7 +109,7 @@ export class Database { }: { oldRelativePath: RelativePath; relativePath: RelativePath; - parentVersionId: DocumentVersionId; + parentVersionId: VaultUpdateId; hash: string; }): Promise { this._documents.delete(oldRelativePath); diff --git a/plugin/src/database/document-metadata.ts b/plugin/src/database/document-metadata.ts index b74a7f9..80ec563 100644 --- a/plugin/src/database/document-metadata.ts +++ b/plugin/src/database/document-metadata.ts @@ -1,7 +1,7 @@ -export type DocumentVersionId = number; +export type VaultUpdateId = number; export type RelativePath = string; export interface DocumentMetadata { - parentVersionId: DocumentVersionId; + parentVersionId: VaultUpdateId; hash: string; } diff --git a/plugin/src/services/sync_service.ts b/plugin/src/services/sync_service.ts index b53d0f6..6bf43ca 100644 --- a/plugin/src/services/sync_service.ts +++ b/plugin/src/services/sync_service.ts @@ -5,10 +5,7 @@ import type { components, paths } from "./types"; // generated by openapi-typesc import { Logger } from "src/logger"; import { Database } from "src/database/database"; import { SyncSettings } from "src/database/sync-settings"; -import { - DocumentVersionId, - RelativePath, -} from "src/database/document-metadata.js"; +import { VaultUpdateId, RelativePath } from "src/database/document-metadata.js"; export class SyncServer { private static VAULT_ID = "default"; @@ -53,7 +50,7 @@ export class SyncServer { createdDate, }: { relativePath: RelativePath; - parentVersionId: DocumentVersionId | undefined; + parentVersionId: VaultUpdateId | undefined; contentBytes: Uint8Array; createdDate: Date; }): Promise { @@ -160,9 +157,9 @@ export class SyncServer { return response.data; } - public async getAll(): Promise< - components["schemas"]["DocumentVersionWithoutContent"][] - > { + public async getAll( + since?: VaultUpdateId + ): Promise { const response = await this.client.GET("/vaults/{vault_id}/documents", { params: { path: { @@ -172,6 +169,9 @@ export class SyncServer { authorization: "Bearer " + this.database.getSettings().token, }, + query: { + since_update_id: since, + }, }, }); diff --git a/plugin/src/services/types.ts b/plugin/src/services/types.ts index 2bb2122..e268123 100644 --- a/plugin/src/services/types.ts +++ b/plugin/src/services/types.ts @@ -49,7 +49,9 @@ export interface paths { }; get: { parameters: { - query?: never; + query?: { + since_update_id?: number | null; + }; header: { authorization: string; }; @@ -65,7 +67,7 @@ export interface paths { [name: string]: unknown; }; content: { - "application/json": components["schemas"]["DocumentVersionWithoutContent"][]; + "application/json": components["schemas"]["FetchLatestDocumentsResponse"]; }; }; }; @@ -225,7 +227,7 @@ export interface components { updatedDate: string; vaultId: string; /** Format: int64 */ - versionId: number; + vaultUpdateId: number; }; DocumentVersionWithoutContent: { /** Format: date-time */ @@ -236,7 +238,12 @@ export interface components { updatedDate: string; vaultId: string; /** Format: int64 */ - versionId: number; + vaultUpdateId: number; + }; + FetchLatestDocumentsResponse: { + /** Format: int64 */ + lastUpdateId: number; + latestDocuments: components["schemas"]["DocumentVersionWithoutContent"][]; }; PathParams: { vault_id: string; @@ -257,6 +264,10 @@ export interface components { isAuthenticated: boolean; serverVersion: string; }; + QueryParams: { + /** Format: int64 */ + since_update_id?: number | null; + }; UpdateDocumentVersion: { contentBase64: string; /** Format: date-time */ diff --git a/plugin/src/sync-functions/sync-locally-updated-file.ts b/plugin/src/sync-functions/sync-locally-updated-file.ts index b9e40c6..366c3e9 100644 --- a/plugin/src/sync-functions/sync-locally-updated-file.ts +++ b/plugin/src/sync-functions/sync-locally-updated-file.ts @@ -3,7 +3,8 @@ import { TFile } from "obsidian"; import { Database } from "src/database/database"; import { Logger } from "src/logger"; import { SyncServer } from "src/services/sync_service"; -import { hash, isEqualBytes } from "src/utils"; +import { hash } from "src/utils/hash.js"; +import { isEqualBytes } from "src/utils/is-equal-bytes.js"; export async function syncLocallyUpdatedFile({ database, @@ -43,7 +44,7 @@ export async function syncLocallyUpdatedFile({ const localDbUpdatePromise = database.moveDocument({ oldRelativePath: oldPath || file.path, relativePath: file.path, - parentVersionId: response.versionId, + parentVersionId: response.vaultUpdateId, hash: contentHash, });