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
core: refactor collector to do everything in 1 step
Abdullah Atta349cd2765018
diff --git a/packages/core/api/sync/__tests__/collector.test.js b/packages/core/api/sync/__tests__/collector.test.jsindex a8001d5e2..1a069eaf3 100644--- a/packages/core/api/sync/__tests__/collector.test.js+++ b/packages/core/api/sync/__tests__/collector.test.js@@ -38,10 +38,13 @@ test("newly created note should get included in collector", () =>const noteId = await db.notes.add(TEST_NOTE);const data = await collector.collect(lastSyncedTime);- const notes = data.items.filter((i) => i.collectionId === "note");- expect(notes).toHaveLength(1);- expect(notes[0].id).toBe(noteId);+ expect(data.items).toHaveLength(2);+ expect(data.items[0].type).toBe("note");+ expect(data.items[0].id).toBe(noteId);+ expect(data.types[0]).toBe("note");+ expect(data.types[1]).toBe("content");+ expect(data.items[1].type).toBe("tiptap");}));test("edited note after last synced time should get included in collector", () =>@@ -56,10 +59,9 @@ test("edited note after last synced time should get included in collector", () =await db.notes.add({ id: noteId, pinned: true });const data = await collector.collect(lastSyncedTime);- const notes = data.items.filter((i) => i.collectionId === "note");- expect(notes).toHaveLength(1);- expect(notes[0].id).toBe(noteId);+ expect(data.items).toHaveLength(1);+ expect(data.items[0].id).toBe(noteId);}));test("note edited before last synced time should not get included in collector", () =>@@ -85,7 +87,9 @@ test("localOnly note should get included as a deleted item in collector", () =>await db.notes.add({ ...TEST_NOTE, localOnly: true });const data = await collector.collect(0);- const notes = data.items.filter((i) => i.collectionId === "note");- expect(notes).toHaveLength(1);- expect(notes[0].deleted).toBe(true);+ expect(data.items).toHaveLength(2);+ expect(data.items[0].deleted).toBe(true);+ expect(data.items[1].deleted).toBe(true);+ expect(data.types[0]).toBe("note");+ expect(data.types[1]).toBe("content");}));diff --git a/packages/core/api/sync/collector.js b/packages/core/api/sync/collector.jsindex a3a8f180a..378beb0b9 100644--- a/packages/core/api/sync/collector.js+++ b/packages/core/api/sync/collector.js@@ -35,21 +35,28 @@ class Collector {this._lastSyncedTimestamp = lastSyncedTimestamp;this.key = await this._db.user.getEncryptionKey();+ const vaultKey = await this._db.vault._getKey();++ const collections = {+ note: this._db.notes.raw,+ shortcut: this._db.shortcuts.raw,+ notebook: this._db.notebooks.raw,+ content: await this._db.content.all(),+ attachment: this._db.attachments.syncable,+ settings: [this._db.settings.raw]+ };++ const result = { items: [], types: [] };+ for (const type in collections) {+ this._collect(type, collections[type], result, isForceSync);+ }++ if (vaultKey) {+ result.items.push(vaultKey);+ result.types.push("vaultKey");+ }- const items = [- ...this._collect("note", this._db.notes.raw, isForceSync),- ...this._collect("shortcut", this._db.shortcuts.raw, isForceSync),- ...this._collect("notebook", this._db.notebooks.raw, isForceSync),- ...this._collect("content", await this._db.content.all(), isForceSync),- ...this._collect(- "attachment",- this._db.attachments.syncable,- isForceSync- ),- ...this._collect("settings", [this._db.settings.raw], isForceSync)- ];-- return { items, vaultKey: await this._db.vault._getKey() };+ return result;}_serialize(item) {@@ -62,38 +69,28 @@ class Collector {return Promise.all(array.map(this._map, this));}- /**- *- * @param {Array} array- * @returns {Array}- */- _collect(collectionId, array, isForceSync) {- if (!array.length) return [];+ _collect(itemType, items, result, isForceSync) {+ if (!items || !items.length) return;- const result = array.reduce((prev, item) => {- if (!item) return prev;+ for (const item of items) {+ if (!item) continue;const isSyncable = !item.synced || isForceSync;const isUnsynced =item.dateModified > this._lastSyncedTimestamp || isForceSync;if (item.localOnly) {- prev.push({+ result.items.push({id: item.id,- collectionId,deleted: true,dateModified: Date.now()});+ result.types.push(itemType);} else if (isUnsynced && isSyncable) {- prev.push({ ...item, collectionId });+ result.items.push(item);+ result.types.push(itemType);}-- return prev;- }, []);- this.logger.info(- `Collected items: ${collectionId} (${result.length}/${array.length})`- );- return result;+ }}// _map(item) {diff --git a/packages/core/api/sync/index.js b/packages/core/api/sync/index.jsindex 2564b3ae7..2cd3d852a 100644--- a/packages/core/api/sync/index.js+++ b/packages/core/api/sync/index.js@@ -274,46 +274,29 @@ class Sync {if (data.items.length <= 0) return false;- const arrays = data.items.reduce(- (arrays, item) => {- arrays.types.push(item.collectionId);-- delete item.collectionId;- arrays.items.push(item);- return arrays;- },- { items: [], types: [] }- );-- if (data.vaultKey) {- arrays.types.push("vaultKey");- arrays.items.push(data.vaultKey);- }-- let total = arrays.items.length;+ let total = data.items.length;- arrays.types = toChunks(arrays.types, 30);- arrays.items = toChunks(arrays.items, 30);+ const types = toChunks(data.types, 30);+ const items = toChunks(data.items, 30);let done = 0;- for (let i = 0; i < arrays.items.length; ++i) {+ for (let i = 0; i < items.length; ++i) {this.logger.info(`Sending batch ${done}/${total}`);- const items = (await this.collector.encrypt(arrays.items[i])).map(+ const encryptedItems = (await this.collector.encrypt(items[i])).map((item) => JSON.stringify(item));- const types = arrays.types[i];const result = await this.sendBatchToServer({lastSynced,current: i,total,- items,- types+ items: encryptedItems,+ types: types[i]});if (result) {- done += items.length;+ done += encryptedItems.length;sendSyncProgressEvent(this.db.eventManager, "upload", total, done);this.logger.info(`Batch sent (${done}/${total})`);