Add push operations to plugin

This commit is contained in:
Andras Schmelczer 2024-12-12 22:17:59 +00:00
parent a2066cfc1c
commit 9ce8245abc
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
7 changed files with 78 additions and 53 deletions

View file

@ -1,4 +1,3 @@
import { TFile } from "obsidian";
import { Database } from "src/database/database";
import { RelativePath } from "src/database/document-metadata";
import { SyncServer } from "src/services/sync_service";
@ -15,7 +14,8 @@ export async function syncLocallyDeletedFile(
await syncServer.delete({
documentId: metadata.documentId,
createdDate: new Date(), // We got the event now, so it must have been deleted now
// We got the event now, so it must have been deleted just now
createdDate: new Date(),
});
await database.removeDocument(path);

View file

@ -1,6 +1,9 @@
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,
@ -13,18 +16,42 @@ export async function syncLocallyRenamedFile(
throw `Document metadata not found for ${oldPath}`;
}
const response = await syncServer.update({
const contentBytes = new Uint8Array(await file.vault.readBinary(file));
const responsePromise = syncServer.update({
documentId: metadata.documentId,
parentVersionId: metadata.parentVersionId,
relativePath: file.path,
content: await file.vault.readBinary(file),
contentBytes,
createdDate: new Date(file.stat.ctime),
});
await database.moveDocument({
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;
}

View file

@ -1,36 +1,13 @@
import { TFile } from "obsidian";
import { Database } from "src/database/database";
import { SyncServer } from "src/services/sync_service";
import { hash } from "src/utils";
import { syncLocallyRenamedFile } from "./sync-locally-renamed-file";
export async function syncLocallyUpdatedFile(
database: Database,
syncServer: SyncServer,
file: TFile
) {
const metadata = database.getDocument(file.path);
if (!metadata) {
throw `Document metadata not found for ${file.path}`;
}
const response = await syncServer.update({
documentId: metadata.documentId,
parentVersionId: metadata.parentVersionId,
relativePath: file.path,
content: await file.vault.readBinary(file),
createdDate: new Date(file.stat.ctime),
});
if (file.path !== response.relativePath) {
file.vault.rename(file, response.relativePath);
}
if ((await file.vault.read(file)) !== response.contentBase64) {
// todo - reconcile
}
await database.setDocument({
relativePath: file.path,
documentId: response.documentId,
parentVersionId: response.versionId,
});
syncLocallyRenamedFile(database, syncServer, file, file.path);
}

View file

@ -1,29 +1,48 @@
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 response = await syncServer.create({
const contentBytes = new Uint8Array(await file.vault.readBinary(file));
const responsePromise = syncServer.create({
relativePath: file.path,
content: await file.vault.readBinary(file),
contentBytes,
createdDate: new Date(file.stat.ctime),
});
if (file.path !== response.relativePath) {
file.vault.rename(file, response.relativePath);
}
const contentHash = hash(contentBytes);
const response = await responsePromise;
if ((await file.vault.read(file)) !== response.contentBase64) {
// todo - reconcile
}
await database.setDocument({
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;
}