Skip to content

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

fix: duplicated note does not get added into original note's topic (fixes streetwriters/notesnook#552)

thecodrr2f814bd65605
diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js
index 1453fe85c..d29c23b79 100644
--- a/packages/core/collections/notes.js
+++ b/packages/core/collections/notes.js
@@ -98,8 +98,10 @@ export default class Notes extends Collection {
dateModified: note.dateModified,
};
- await this._resolveColorAndTags(note);
await this._collection.addItem(note);
+
+ await this._resolveColorAndTags(note);
+ await this._resolveNotebooks(note);
return note.id;
}
@@ -234,11 +236,12 @@ export default class Notes extends Collection {
);
let topic = this._db.notebooks.notebook(to.id).topics.topic(to.topic);
if (!topic) throw new Error("No such topic exists.");
+ console.log(topic, noteIds);
await topic.add(...noteIds);
}
- async repairReferences() {
- const notes = this.all;
+ async repairReferences(notes) {
+ notes = notes || this.all;
for (let note of notes) {
const { notebooks } = note;
if (!notebooks) continue;
@@ -286,6 +289,19 @@ export default class Notes extends Collection {
}
}
}
+
+ async _resolveNotebooks(note) {
+ const { notebooks, id } = note;
+ if (!notebooks) return;
+
+ for (const notebook of notebooks) {
+ const nb = this._db.notebooks.notebook(notebook.id);
+ if (!nb) continue;
+ for (const topic of notebook.topics) {
+ await this.move({ id: notebook.id, topic }, id);
+ }
+ }
+ }
}
function getNoteHeadline(note, content) {
diff --git a/packages/core/models/topic.js b/packages/core/models/topic.js
index 4713420b1..24e98a35c 100644
--- a/packages/core/models/topic.js
+++ b/packages/core/models/topic.js
@@ -31,23 +31,23 @@ export default class Topic {
const noteNotebook = notebooks.find((nb) => nb.id === this._notebookId);
const noteHasNotebook = !!noteNotebook;
- if (noteHasNotebook) {
+ const noteHasTopic = noteNotebook.topics.indexOf(topic.id) > -1;
+ if (noteHasNotebook && !noteHasTopic) {
// 1 note can be inside multiple topics
- const noteHasTopic = noteNotebook.topics.indexOf(topic.id) > -1;
- if (noteHasTopic) continue;
-
noteNotebook.topics.push(topic.id);
- } else {
+ } else if (!noteHasNotebook) {
notebooks.push({
id: this._notebookId,
topics: [topic.id],
});
}
- await this._db.notes.add({
- id: noteId,
- notebooks,
- });
+ if (!noteHasNotebook || !noteHasTopic) {
+ await this._db.notes.add({
+ id: noteId,
+ notebooks,
+ });
+ }
if (!this.has(noteId)) {
topic.notes.push(noteId);