Extract event handlers

This commit is contained in:
Andras Schmelczer 2024-12-10 22:24:31 +00:00
parent 0c3e74e2b8
commit 5f1075879f
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
5 changed files with 130 additions and 61 deletions

View file

@ -3,6 +3,10 @@ import { FileEventHandler } from "./file-event-handler";
import { Logger } from "src/logger";
import { SyncServer } from "src/services/sync_service";
import { Database } from "src/database/database";
import { syncLocallyDeletedFile } from "src/sync-functions/sync-locally-deleted-file";
import { syncLocallyRenamedFile } from "src/sync-functions/sync-locally-renamed-file";
import { syncLocallyUpdatedFile } from "src/sync-functions/sync-locally-updated-file";
import { syncNewLocalFile } from "src/sync-functions/sync-new-local-file";
export class SyncEventHandler implements FileEventHandler {
constructor(private database: Database, private syncServer: SyncServer) {}
@ -10,18 +14,7 @@ export class SyncEventHandler implements FileEventHandler {
async onCreate(file: TAbstractFile): Promise<void> {
if (file instanceof TFile) {
Logger.getInstance().info(`File created: ${file.path}`);
const result = await this.syncServer.create({
relativePath: file.path,
content: await file.vault.readBinary(file),
createdDate: new Date(file.stat.ctime),
});
await this.database.setDocument({
relativePath: file.path,
documentId: result.documentId,
parentVersionId: result.versionId,
});
syncNewLocalFile(this.database, this.syncServer, file);
} else {
Logger.getInstance().info(`Folder created: ${file.path}, ignored`);
}
@ -30,18 +23,7 @@ export class SyncEventHandler implements FileEventHandler {
async onDelete(file: TAbstractFile): Promise<void> {
if (file instanceof TFile) {
Logger.getInstance().info(`File deleted: ${file.path}`);
const metadata = this.database.getDocument(file.path);
if (!metadata) {
throw `Document metadata not found for ${file.path}`;
}
await this.syncServer.delete({
documentId: metadata.documentId,
createdDate: new Date(), // We got the event now, so it must have been deleted now
});
await this.database.removeDocument(file.path);
syncLocallyDeletedFile(this.database, this.syncServer, file.path);
} else {
Logger.getInstance().info(`Folder deleted: ${file.path}, ignored`);
}
@ -51,25 +33,12 @@ export class SyncEventHandler implements FileEventHandler {
Logger.getInstance().info(`File renamed: ${oldPath} -> ${file.path}`);
if (file instanceof TFile) {
const metadata = this.database.getDocument(oldPath);
if (!metadata) {
throw `Document metadata not found for ${oldPath}`;
}
const response = await this.syncServer.update({
documentId: metadata.documentId,
parentVersionId: metadata.parentVersionId,
relativePath: file.path,
content: await file.vault.readBinary(file),
createdDate: new Date(file.stat.ctime),
});
await this.database.moveDocument({
oldRelativePath: oldPath,
relativePath: file.path,
documentId: response.documentId,
parentVersionId: response.versionId,
});
syncLocallyRenamedFile(
this.database,
this.syncServer,
file,
oldPath
);
} else {
Logger.getInstance().info(
`Folder renamed: ${oldPath} -> ${file.path}, ignored`
@ -81,24 +50,7 @@ export class SyncEventHandler implements FileEventHandler {
Logger.getInstance().info(`File modified: ${file.path}`);
if (file instanceof TFile) {
const metadata = this.database.getDocument(file.path);
if (!metadata) {
throw `Document metadata not found for ${file.path}`;
}
const response = await this.syncServer.update({
documentId: metadata.documentId,
parentVersionId: metadata.parentVersionId,
relativePath: file.path,
content: await file.vault.readBinary(file),
createdDate: new Date(file.stat.ctime),
});
await this.database.setDocument({
relativePath: file.path,
documentId: response.documentId,
parentVersionId: response.versionId,
});
syncLocallyUpdatedFile(this.database, this.syncServer, file);
} else {
Logger.getInstance().info(`Folder modified: ${file.path}, ignored`);
}