Simplify change handlers
This commit is contained in:
parent
89156eb270
commit
47e976b0bb
5 changed files with 84 additions and 170 deletions
|
|
@ -71,40 +71,14 @@ export interface paths {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
put?: never;
|
put?: never;
|
||||||
post: {
|
post?: never;
|
||||||
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"];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
delete?: never;
|
delete?: never;
|
||||||
options?: never;
|
options?: never;
|
||||||
head?: never;
|
head?: never;
|
||||||
patch?: never;
|
patch?: never;
|
||||||
trace?: never;
|
trace?: never;
|
||||||
};
|
};
|
||||||
"/vaults/{vault_id}/documents/{document_id}": {
|
"/vaults/{vault_id}/documents/{relative_path}": {
|
||||||
parameters: {
|
parameters: {
|
||||||
query?: never;
|
query?: never;
|
||||||
header?: never;
|
header?: never;
|
||||||
|
|
@ -118,7 +92,7 @@ export interface paths {
|
||||||
authorization: string;
|
authorization: string;
|
||||||
};
|
};
|
||||||
path: {
|
path: {
|
||||||
document_id: string;
|
relative_path: string;
|
||||||
vault_id: string;
|
vault_id: string;
|
||||||
};
|
};
|
||||||
cookie?: never;
|
cookie?: never;
|
||||||
|
|
@ -142,7 +116,7 @@ export interface paths {
|
||||||
authorization: string;
|
authorization: string;
|
||||||
};
|
};
|
||||||
path: {
|
path: {
|
||||||
document_id: string;
|
relative_path: string;
|
||||||
vault_id: string;
|
vault_id: string;
|
||||||
};
|
};
|
||||||
cookie?: never;
|
cookie?: never;
|
||||||
|
|
@ -171,7 +145,7 @@ export interface paths {
|
||||||
authorization: string;
|
authorization: string;
|
||||||
};
|
};
|
||||||
path: {
|
path: {
|
||||||
document_id: string;
|
relative_path: string;
|
||||||
vault_id: string;
|
vault_id: string;
|
||||||
};
|
};
|
||||||
cookie?: never;
|
cookie?: never;
|
||||||
|
|
@ -237,12 +211,6 @@ export interface paths {
|
||||||
export type webhooks = Record<string, never>;
|
export type webhooks = Record<string, never>;
|
||||||
export interface components {
|
export interface components {
|
||||||
schemas: {
|
schemas: {
|
||||||
CreateDocumentVersion: {
|
|
||||||
contentBase64: string;
|
|
||||||
/** Format: date-time */
|
|
||||||
createdDate: string;
|
|
||||||
relativePath: string;
|
|
||||||
};
|
|
||||||
DeleteDocumentVersion: {
|
DeleteDocumentVersion: {
|
||||||
/** Format: date-time */
|
/** Format: date-time */
|
||||||
createdDate: string;
|
createdDate: string;
|
||||||
|
|
@ -251,8 +219,6 @@ export interface components {
|
||||||
contentBase64: string;
|
contentBase64: string;
|
||||||
/** Format: date-time */
|
/** Format: date-time */
|
||||||
createdDate: string;
|
createdDate: string;
|
||||||
/** Format: uuid */
|
|
||||||
documentId: string;
|
|
||||||
isDeleted: boolean;
|
isDeleted: boolean;
|
||||||
relativePath: string;
|
relativePath: string;
|
||||||
/** Format: date-time */
|
/** Format: date-time */
|
||||||
|
|
@ -264,8 +230,6 @@ export interface components {
|
||||||
DocumentVersionWithoutContent: {
|
DocumentVersionWithoutContent: {
|
||||||
/** Format: date-time */
|
/** Format: date-time */
|
||||||
createdDate: string;
|
createdDate: string;
|
||||||
/** Format: uuid */
|
|
||||||
documentId: string;
|
|
||||||
isDeleted: boolean;
|
isDeleted: boolean;
|
||||||
relativePath: string;
|
relativePath: string;
|
||||||
/** Format: date-time */
|
/** Format: date-time */
|
||||||
|
|
@ -278,21 +242,15 @@ export interface components {
|
||||||
vault_id: string;
|
vault_id: string;
|
||||||
};
|
};
|
||||||
PathParams2: {
|
PathParams2: {
|
||||||
|
relative_path: string;
|
||||||
vault_id: string;
|
vault_id: string;
|
||||||
};
|
};
|
||||||
PathParams3: {
|
PathParams3: {
|
||||||
/** Format: uuid */
|
relative_path: string;
|
||||||
document_id: string;
|
|
||||||
vault_id: string;
|
vault_id: string;
|
||||||
};
|
};
|
||||||
PathParams4: {
|
PathParams4: {
|
||||||
/** Format: uuid */
|
relative_path: string;
|
||||||
document_id: string;
|
|
||||||
vault_id: string;
|
|
||||||
};
|
|
||||||
PathParams5: {
|
|
||||||
/** Format: uuid */
|
|
||||||
document_id: string;
|
|
||||||
vault_id: string;
|
vault_id: string;
|
||||||
};
|
};
|
||||||
PingResponse: {
|
PingResponse: {
|
||||||
|
|
@ -304,8 +262,7 @@ export interface components {
|
||||||
/** Format: date-time */
|
/** Format: date-time */
|
||||||
createdDate: string;
|
createdDate: string;
|
||||||
/** Format: int64 */
|
/** Format: int64 */
|
||||||
parentVersionId: number;
|
parentVersionId?: number | null;
|
||||||
relativePath: string;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
responses: never;
|
responses: never;
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,27 @@
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
import { RelativePath } from "src/database/document-metadata";
|
import { RelativePath } from "src/database/document-metadata";
|
||||||
|
import { Logger } from "src/logger";
|
||||||
import { SyncServer } from "src/services/sync_service";
|
import { SyncServer } from "src/services/sync_service";
|
||||||
|
|
||||||
export async function syncLocallyDeletedFile(
|
export async function syncLocallyDeletedFile(
|
||||||
database: Database,
|
database: Database,
|
||||||
syncServer: SyncServer,
|
syncServer: SyncServer,
|
||||||
path: RelativePath
|
relativePath: RelativePath
|
||||||
) {
|
) {
|
||||||
const metadata = database.getDocument(path);
|
const metadata = database.getDocument(relativePath);
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
throw `Document metadata not found for ${path}`;
|
Logger.getInstance().warn(
|
||||||
|
`Document metadata not found for ${relativePath}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await syncServer.delete({
|
await syncServer.delete({
|
||||||
documentId: metadata.documentId,
|
relativePath,
|
||||||
// We got the event now, so it must have been deleted just now
|
// We got the event now, so it must have been deleted just now
|
||||||
createdDate: new Date(),
|
createdDate: new Date(),
|
||||||
});
|
});
|
||||||
|
|
||||||
await database.removeDocument(path);
|
if (metadata) {
|
||||||
|
await database.removeDocument(relativePath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
|
|
@ -1,13 +1,70 @@
|
||||||
|
import * as lib from "../../../backend/sync_lib/pkg/sync_lib.js";
|
||||||
import { TFile } from "obsidian";
|
import { TFile } from "obsidian";
|
||||||
import { Database } from "src/database/database";
|
import { Database } from "src/database/database";
|
||||||
|
import { Logger } from "src/logger";
|
||||||
import { SyncServer } from "src/services/sync_service";
|
import { SyncServer } from "src/services/sync_service";
|
||||||
import { hash } from "src/utils";
|
import { hash, isEqualBytes } from "src/utils";
|
||||||
import { syncLocallyRenamedFile } from "./sync-locally-renamed-file";
|
|
||||||
|
|
||||||
export async function syncLocallyUpdatedFile(
|
export async function syncLocallyUpdatedFile({
|
||||||
database: Database,
|
database,
|
||||||
syncServer: SyncServer,
|
syncServer,
|
||||||
file: TFile
|
file,
|
||||||
) {
|
oldPath,
|
||||||
syncLocallyRenamedFile(database, syncServer, file, file.path);
|
}: {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue