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