mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 22:49:45 +01:00
fix: repair notebook references in notes on sync
This commit is contained in:
@@ -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);
|
||||
}));
|
||||
|
||||
@@ -182,6 +182,7 @@ class Merger {
|
||||
)
|
||||
);
|
||||
|
||||
await this._db.notes.repairReferences();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user