Basic syncing in the plugin

This commit is contained in:
Andras Schmelczer 2024-12-15 15:47:08 +00:00
commit d088d42a65
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -0,0 +1,38 @@
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";
import { tryLockDocument, unlockDocument, waitForDocumentLock } from "./locks";
export async function syncLocallyDeletedFile(
database: Database,
syncServer: SyncServer,
relativePath: RelativePath
): Promise<void> {
await waitForDocumentLock(relativePath);
try {
const metadata = database.getDocument(relativePath);
if (!metadata) {
Logger.getInstance().warn(
`Document metadata not found for ${relativePath}`
);
return;
}
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(relativePath);
} catch (e) {
Logger.getInstance().error(
`Failed to sync locally deleted file ${relativePath}: ${e}`
);
} finally {
unlockDocument(relativePath);
}
}