Use new API

This commit is contained in:
Andras Schmelczer 2024-12-14 17:06:40 +00:00
parent 65a42a18bf
commit 570b5f9bfc
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
5 changed files with 31 additions and 19 deletions

View file

@ -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<void> {
this._documents.set(relativePath, {
@ -109,7 +109,7 @@ export class Database {
}: {
oldRelativePath: RelativePath;
relativePath: RelativePath;
parentVersionId: DocumentVersionId;
parentVersionId: VaultUpdateId;
hash: string;
}): Promise<void> {
this._documents.delete(oldRelativePath);

View file

@ -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;
}

View file

@ -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<components["schemas"]["DocumentVersion"]> {
@ -160,9 +157,9 @@ export class SyncServer {
return response.data;
}
public async getAll(): Promise<
components["schemas"]["DocumentVersionWithoutContent"][]
> {
public async getAll(
since?: VaultUpdateId
): Promise<components["schemas"]["FetchLatestDocumentsResponse"]> {
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,
},
},
});

View file

@ -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 */

View file

@ -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,
});