Extract event handlers
This commit is contained in:
parent
0c3e74e2b8
commit
5f1075879f
5 changed files with 130 additions and 61 deletions
|
|
@ -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`);
|
||||
}
|
||||
|
|
|
|||
22
plugin/src/sync-functions/sync-locally-deleted-file.ts
Normal file
22
plugin/src/sync-functions/sync-locally-deleted-file.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { TFile } from "obsidian";
|
||||
import { Database } from "src/database/database";
|
||||
import { RelativePath } from "src/database/document-metadata";
|
||||
import { SyncServer } from "src/services/sync_service";
|
||||
|
||||
export async function syncLocallyDeletedFile(
|
||||
database: Database,
|
||||
syncServer: SyncServer,
|
||||
path: RelativePath
|
||||
) {
|
||||
const metadata = database.getDocument(path);
|
||||
if (!metadata) {
|
||||
throw `Document metadata not found for ${path}`;
|
||||
}
|
||||
|
||||
await syncServer.delete({
|
||||
documentId: metadata.documentId,
|
||||
createdDate: new Date(), // We got the event now, so it must have been deleted now
|
||||
});
|
||||
|
||||
await database.removeDocument(path);
|
||||
}
|
||||
30
plugin/src/sync-functions/sync-locally-renamed-file.ts
Normal file
30
plugin/src/sync-functions/sync-locally-renamed-file.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import { TFile } from "obsidian";
|
||||
import { Database } from "src/database/database";
|
||||
import { SyncServer } from "src/services/sync_service";
|
||||
|
||||
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 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),
|
||||
});
|
||||
|
||||
await database.moveDocument({
|
||||
oldRelativePath: oldPath,
|
||||
relativePath: file.path,
|
||||
documentId: response.documentId,
|
||||
parentVersionId: response.versionId,
|
||||
});
|
||||
}
|
||||
36
plugin/src/sync-functions/sync-locally-updated-file.ts
Normal file
36
plugin/src/sync-functions/sync-locally-updated-file.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { TFile } from "obsidian";
|
||||
import { Database } from "src/database/database";
|
||||
import { SyncServer } from "src/services/sync_service";
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
29
plugin/src/sync-functions/sync-new-local-file.ts
Normal file
29
plugin/src/sync-functions/sync-new-local-file.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import { TFile } from "obsidian";
|
||||
import { Database } from "src/database/database";
|
||||
import { SyncServer } from "src/services/sync_service";
|
||||
|
||||
export async function syncNewLocalFile(
|
||||
database: Database,
|
||||
syncServer: SyncServer,
|
||||
file: TFile
|
||||
) {
|
||||
const response = await syncServer.create({
|
||||
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: response.relativePath,
|
||||
documentId: response.documentId,
|
||||
parentVersionId: response.versionId,
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue