From 8bbd5f9262648e440d698f30e0790f3fd20a6b8a Mon Sep 17 00:00:00 2001 From: drelich Date: Thu, 26 Mar 2026 09:18:26 +0100 Subject: [PATCH] Fix bidirectional favorite sync - use timestamp matching in syncFavoriteStatus The syncFavoriteStatus method was using title matching which fails when API and WebDAV titles differ. Now uses modified timestamp + category matching (with title fallback) for reliable bidirectional sync. --- src/services/syncManager.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/services/syncManager.ts b/src/services/syncManager.ts index bb042ba..ad7f324 100644 --- a/src/services/syncManager.ts +++ b/src/services/syncManager.ts @@ -143,17 +143,30 @@ export class SyncManager { const apiMetadata = await this.api.fetchNotesMetadata(); const cachedNotes = await localDB.getAllNotes(); - // Map API notes by title and category for matching - const apiMap = new Map(); + // Map API notes by modified timestamp + category for reliable matching + // (titles can differ between API and WebDAV) + const apiByTimestamp = new Map(); + const apiByTitle = new Map(); + for (const apiNote of apiMetadata) { - const key = `${apiNote.category}/${apiNote.title}`; - apiMap.set(key, { id: apiNote.id, favorite: apiNote.favorite }); + const timestampKey = `${apiNote.modified}:${apiNote.category}`; + const titleKey = `${apiNote.category}/${apiNote.title}`; + const noteData = { id: apiNote.id, title: apiNote.title, favorite: apiNote.favorite }; + apiByTimestamp.set(timestampKey, noteData); + apiByTitle.set(titleKey, noteData); } // Update favorite status in cache for matching notes for (const cachedNote of cachedNotes) { - const key = `${cachedNote.category}/${cachedNote.title}`; - const apiData = apiMap.get(key); + // Try timestamp match first (most reliable) + const timestampKey = `${cachedNote.modified}:${cachedNote.category}`; + let apiData = apiByTimestamp.get(timestampKey); + + // Fallback to title match if timestamp doesn't work + if (!apiData) { + const titleKey = `${cachedNote.category}/${cachedNote.title}`; + apiData = apiByTitle.get(titleKey); + } if (apiData && cachedNote.favorite !== apiData.favorite) { console.log(`Updating favorite status for "${cachedNote.title}": ${cachedNote.favorite} -> ${apiData.favorite}`);