Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: repair notebook references in notes on sync

thecodrre608eaaedf4b
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index df0f5b66e..f32806058 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -8,6 +8,7 @@ import {
TEST_NOTE,
TEST_NOTEBOOK,
IMG_CONTENT,
+ notebookTest,
} from "./utils";
beforeEach(async () => {
@@ -324,3 +325,15 @@ test("note content should not contain image base64 data after save", () =>
expect(content).not.toContain(`src="data:image/png;`);
expect(content).not.toContain(`src=`);
}));
+
+test("repairing notebook references should reinclude the missing noteIds", () =>
+ notebookTest().then(async ({ db, id }) => {
+ const notebook = db.notebooks.notebook(id);
+ const note = {
+ ...TEST_NOTE,
+ notebooks: [{ id, topics: [notebook.topics.all[0].id] }],
+ };
+ await db.notes.add(note);
+ await db.notes.repairReferences();
+ expect(notebook.topics.all[0].notes.length).toBe(1);
+ }));
diff --git a/packages/core/api/sync/merger.js b/packages/core/api/sync/merger.js
index 1ebff3214..e83d28f0a 100644
--- a/packages/core/api/sync/merger.js
+++ b/packages/core/api/sync/merger.js
@@ -182,6 +182,7 @@ class Merger {
)
);
+ await this._db.notes.repairReferences();
return true;
}
}
diff --git a/packages/core/collections/notebooks.js b/packages/core/collections/notebooks.js
index de5e76e11..cd5399a14 100644
--- a/packages/core/collections/notebooks.js
+++ b/packages/core/collections/notebooks.js
@@ -133,18 +133,4 @@ export default class Notebooks extends Collection {
await this._db.trash.add(notebookData);
}
}
-
- async cleanup() {
- await this._db.notes.init();
- for (let notebook of this.all) {
- for (let topic of notebook.topics) {
- const clonedIds = topic.notes.slice();
- topic.notes = [];
- for (let noteId of clonedIds) {
- if (!!this._db.notes.note(noteId)) topic.notes.push(noteId);
- }
- }
- await this._collection.updateItem(notebook);
- }
- }
}
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 2ad44c15f..19d2f1581 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -240,6 +240,30 @@ export default class Notes extends Collection {
if (!topic) throw new Error("No such topic exists.");
await topic.add(...noteIds);
}
+
+ async repairReferences() {
+ const notes = this.all;
+ for (let note of notes) {
+ const { notebooks } = note;
+ if (!notebooks) continue;
+
+ for (let notebook of notebooks) {
+ const nb = this._db.notebooks.notebook(notebook.id);
+ if (!nb) {
+ deleteItem(notebooks, notebook);
+ continue;
+ }
+ for (let topic of notebook.topics) {
+ const _topic = nb.topics.topic(topic);
+ if (!_topic || _topic.has(note.id)) {
+ deleteItem(notebook.topics, topic);
+ continue;
+ }
+ await _topic.add(note.id);
+ }
+ }
+ }
+ }
}
function isNoteEmpty(note, content) {
diff --git a/packages/core/database/backup.js b/packages/core/database/backup.js
index ecde82b38..255980b80 100644
--- a/packages/core/database/backup.js
+++ b/packages/core/database/backup.js
@@ -141,7 +141,7 @@ export default class Backup {
];
if (await this._migrator.migrate(collections, (id) => data[id], version)) {
- await this._db.notebooks.cleanup();
+ await this._db.notes.repairReferences();
}
}
diff --git a/packages/core/models/topic.js b/packages/core/models/topic.js
index 3c9867c09..8e6932431 100644
--- a/packages/core/models/topic.js
+++ b/packages/core/models/topic.js
@@ -37,7 +37,11 @@ export default class Topic {
array.push(notebook);
} else {
const topicIndex = array[notebookIndex].topics.indexOf(topic.id);
- if (topicIndex > -1) return;
+ if (topicIndex > -1) {
+ if (!this.has(noteId)) topic.notes.push(noteId);
+ continue;
+ }
+
array[notebookIndex].topics.push(topic.id);
}