Temporary conflict handling

This commit is contained in:
Silvano Cerza 2025-01-14 18:31:19 +01:00
parent 56a9fb29cd
commit 44cc1c6380
2 changed files with 54 additions and 5 deletions

View file

@ -2,6 +2,7 @@ import { EventRef, Plugin, FileView } from "obsidian";
import { GitHubSyncSettings, DEFAULT_SETTINGS } from "./settings/settings";
import GitHubSyncSettingsTab from "./settings/tab";
import SyncManager from "./sync-manager";
import { FileMetadata } from "./metadata-store";
export default class GitHubSyncPlugin extends Plugin {
settings: GitHubSyncSettings;
@ -24,7 +25,11 @@ export default class GitHubSyncPlugin extends Plugin {
this.addSettingTab(new GitHubSyncSettingsTab(this.app, this));
this.syncManager = new SyncManager(this.app.vault, this.settings);
this.syncManager = new SyncManager(
this.app.vault,
this.settings,
this.onConflicts.bind(this),
);
await this.syncManager.loadMetadata();
if (this.settings.uploadStrategy == "interval") {
@ -145,6 +150,26 @@ export default class GitHubSyncPlugin extends Plugin {
this.uploadModifiedFilesRibbonIcon = null;
}
async onConflicts(
conflicts: { remoteFile: FileMetadata; localFile: FileMetadata }[],
): Promise<boolean[]> {
return await Promise.all(
conflicts.map(
async ({
remoteFile,
localFile,
}: {
remoteFile: FileMetadata;
localFile: FileMetadata;
}) => {
// TODO: Add a proper conflict resolution view
// This way remote files are always preferred
return true;
},
),
);
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

View file

@ -12,6 +12,10 @@ interface SyncAction {
filePath: string;
}
type OnConflictsCallback = (
conflicts: { remoteFile: FileMetadata; localFile: FileMetadata }[],
) => Promise<boolean[]>;
export default class SyncManager {
private metadataStore: MetadataStore;
private client: GithubClient;
@ -21,6 +25,7 @@ export default class SyncManager {
constructor(
private vault: Vault,
private settings: GitHubSyncSettings,
private onConflicts: OnConflictsCallback,
) {
this.metadataStore = new MetadataStore(this.vault);
this.client = new GithubClient(
@ -131,6 +136,7 @@ export default class SyncManager {
remoteMetadata.files,
this.metadataStore.data.files,
);
let conflictResolutions: SyncAction[] = [];
if (conflicts.length > 0) {
// TODO: Show conflicts to the user with a callback
// Wait for response
@ -138,12 +144,30 @@ export default class SyncManager {
// Add the new action to the list later on
console.log("Conflicts");
console.log(conflicts);
(await this.onConflicts(conflicts)).forEach(
(resolution: boolean, index: number) => {
if (resolution) {
conflictResolutions.push({
type: "download",
filePath: conflicts[index].remoteFile.localPath,
});
} else {
conflictResolutions.push({
type: "upload",
filePath: conflicts[index].localFile.localPath,
});
}
},
);
}
const actions = this.determineSyncActions(
remoteMetadata.files,
this.metadataStore.data.files,
);
const actions = [
...this.determineSyncActions(
remoteMetadata.files,
this.metadataStore.data.files,
),
...conflictResolutions,
];
if (actions.length === 0) {
console.log("Nothing to sync");