fix: repair notebook references in notes on sync

This commit is contained in:
thecodrr
2021-11-12 10:32:25 +05:00
parent b139623420
commit e608eaaedf
6 changed files with 44 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ import {
TEST_NOTE, TEST_NOTE,
TEST_NOTEBOOK, TEST_NOTEBOOK,
IMG_CONTENT, IMG_CONTENT,
notebookTest,
} from "./utils"; } from "./utils";
beforeEach(async () => { 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="data:image/png;`);
expect(content).not.toContain(`src=`); 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);
}));

View File

@@ -182,6 +182,7 @@ class Merger {
) )
); );
await this._db.notes.repairReferences();
return true; return true;
} }
} }

View File

@@ -133,18 +133,4 @@ export default class Notebooks extends Collection {
await this._db.trash.add(notebookData); 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);
}
}
} }

View File

@@ -240,6 +240,30 @@ export default class Notes extends Collection {
if (!topic) throw new Error("No such topic exists."); if (!topic) throw new Error("No such topic exists.");
await topic.add(...noteIds); 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) { function isNoteEmpty(note, content) {

View File

@@ -141,7 +141,7 @@ export default class Backup {
]; ];
if (await this._migrator.migrate(collections, (id) => data[id], version)) { if (await this._migrator.migrate(collections, (id) => data[id], version)) {
await this._db.notebooks.cleanup(); await this._db.notes.repairReferences();
} }
} }

View File

@@ -37,7 +37,11 @@ export default class Topic {
array.push(notebook); array.push(notebook);
} else { } else {
const topicIndex = array[notebookIndex].topics.indexOf(topic.id); 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); array[notebookIndex].topics.push(topic.id);
} }