A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
sync: resolve lastSynced timestamp issues
thecodrr4679d98e537c
diff --git a/packages/core/api/sync/__tests__/prepare.test.js b/packages/core/api/sync/__tests__/prepare.test.jsindex 264dc2ee5..e392971cc 100644--- a/packages/core/api/sync/__tests__/prepare.test.js+++ b/packages/core/api/sync/__tests__/prepare.test.js@@ -69,7 +69,6 @@ describe.each(tests)("%s preparation", (collection, add, addExtra) => {const data = await prepare.get(0);expect(data[collection].length).toBe(MAX_ITEMS);expect(data[collection].every((item) => !!item.data)).toBeTruthy();- expect(data.lastSynced).toBeGreaterThan(0);});});@@ -85,7 +84,6 @@ describe.each(tests)("%s preparation", (collection, add, addExtra) => {const data = await prepare.get(10);expect(data[collection].length).toBe(MAX_ITEMS);expect(data[collection].every((item) => !!item.data)).toBeTruthy();- expect(data.lastSynced).toBeGreaterThan(0);});});});diff --git a/packages/core/api/sync/__tests__/sync.test.js b/packages/core/api/sync/__tests__/sync.test.jsindex 01dea6d9a..303f90e6c 100644--- a/packages/core/api/sync/__tests__/sync.test.js+++ b/packages/core/api/sync/__tests__/sync.test.js@@ -43,7 +43,7 @@ test("sync without merge conflicts, cause merge conflicts, resolve them and then// 3. start syncfetchMock.once(JSON.stringify({ notes: [], synced: false }))- .once(JSON.stringify({}), { status: 200 });+ .once(JSON.stringify({ lastSynced: Date.now() }), { status: 200 });await db.sync();const user = await db.user.get();@@ -70,7 +70,7 @@ test("sync without merge conflicts, cause merge conflicts, resolve them and thenfetchMock.once(JSON.stringify({ notes: [], delta: [delta], synced: false }))- .once(JSON.stringify({}), { status: 200 });+ .once(JSON.stringify({ lastSynced: Date.now() }), { status: 200 });await expect(db.sync()).rejects.toThrow("Merge conflicts detected. Please resolve all conflicts to continue syncing."@@ -96,7 +96,7 @@ test("sync without merge conflicts, cause merge conflicts, resolve them and then// 7. Resync (no conflicts should appear)fetchMock.once(JSON.stringify({ notes: [], delta: [delta], synced: false }))- .once(JSON.stringify({}), { status: 200 });+ .once(JSON.stringify({ lastSynced: Date.now() }), { status: 200 });await expect(db.sync()).resolves.toBeUndefined();});});diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.jsindex b99a3fcf8..232f2cbb8 100644--- a/packages/core/api/sync/index.js+++ b/packages/core/api/sync/index.js@@ -54,12 +54,9 @@ export default class Sync {return await response.json();}- async throwOnConflicts(lastSynced) {+ async throwOnConflicts() {let hasConflicts = await this.db.context.read("hasConflicts");if (hasConflicts) {- if (lastSynced) {- await this.db.user.set({ lastSynced });- }const mergeConflictError = new Error("Merge conflicts detected. Please resolve all conflicts to continue syncing.");@@ -84,15 +81,14 @@ export default class Sync {// merge the server responseconst merger = new Merger(this.db, lastSyncedTimestamp);- const mergeResult = await merger.merge(serverResponse);- await this.throwOnConflicts(data.lastSynced);+ await merger.merge(serverResponse);+ await this.throwOnConflicts();// send the data back to server- await this._send(data);+ const lastSynced = await this._send(data);// update our lastSynced time- if (mergeResult || !areAllEmpty(data))- await this.db.user.set({ lastSynced: data.lastSynced });+ if (lastSynced) await this.db.user.set({ lastSynced });}async _send(data) {@@ -104,6 +100,10 @@ export default class Sync {headers: { ...HEADERS, Authorization: `Bearer ${token}` },body: JSON.stringify(data),});- return response.ok;+ if (response.ok) {+ const json = await response.json();+ return json.lastSynced;+ }+ throw new Error("Failed to sync with the server.");}}diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.jsindex 74ba91ff0..d46b0e171 100644--- a/packages/core/api/sync/merger.js+++ b/packages/core/api/sync/merger.js@@ -31,10 +31,10 @@ class Merger {remoteItem = { ...JSON.parse(remoteItem.data), remote: true };- if (!localItem) {- await add(remoteItem);- } else if (localItem.dateEdited > this._lastSynced) {+ if (!localItem.resolved && localItem.dateEdited > this._lastSynced) {await resolve(localItem, remoteItem);+ } else if (localItem.resolved) {+ await add({ ...localItem, resolved: false });} else {await add(remoteItem);}diff --git a/packages/core/api/sync/prepare.js b/packages/core/api/sync/prepare.jsindex 36bc5d0a9..819f4f0e5 100644--- a/packages/core/api/sync/prepare.js+++ b/packages/core/api/sync/prepare.js@@ -23,17 +23,21 @@ class Prepare {tags: this._prepareForServer(this._db.tags.raw),colors: this._prepareForServer(this._db.colors.raw),trash: this._prepareForServer(this._db.trash.raw),- lastSynced: Date.now(),};}_prepareForServer(array) {return tfun.filter((item) => item.dateEdited > this._lastSyncedTimestamp)- .map((item) => ({- id: item.id,- data: JSON.stringify(item),- }))(array);+ .map((item) => {+ // in case of resolved delta, we do not want to send this key to the server+ if (item.resolved) delete item.resolved;++ return {+ id: item.id,+ data: JSON.stringify(item),+ };+ })(array);}}export default Prepare;diff --git a/packages/core/collections/content.js b/packages/core/collections/content.jsindex 0ada51c9d..2f27abc06 100644--- a/packages/core/collections/content.js+++ b/packages/core/collections/content.js@@ -18,7 +18,7 @@ export default class Content {id,data: content.data || content,conflicted: content.conflicted || false,- //resolved: !!content.resolved,+ resolved: !!content.resolved,dateEdited: content.dateEdited,dateCreated: content.dateCreated,remote: content.remote,