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
feat: migrate topic ids to proper random ids
thecodrr043cbe34a822
diff --git a/packages/core/__tests__/topics.test.js b/packages/core/__tests__/topics.test.jsindex ec49de659..65fcefccb 100644--- a/packages/core/__tests__/topics.test.js+++ b/packages/core/__tests__/topics.test.js@@ -1,4 +1,4 @@-import { notebookTest, StorageInterface, TEST_NOTE } from "./utils";+import { notebookTest, noteTest, StorageInterface, TEST_NOTE } from "./utils";beforeEach(() => StorageInterface.clear());@@ -18,7 +18,7 @@ test("add topic to notebook", () =>let topics = db.notebooks.notebook(id).topics;await topics.add("Home");expect(topics.all.length).toBeGreaterThan(1);- expect(topics.all.findIndex(v => v.title === "Home")).toBeGreaterThan(-1);+ expect(topics.all.findIndex((v) => v.title === "Home")).toBeGreaterThan(-1);}));test("update topic", () =>@@ -28,7 +28,7 @@ test("update topic", () =>let topic = topics.topic("Home");let noteId = await db.notes.add(TEST_NOTE);await topic.add(noteId);- expect(topics.all.find(v => v.title === "Home").notes.length).toBe(1);+ expect(topics.all.find((v) => v.title === "Home").notes.length).toBe(1);}));test("edit topic title", () =>@@ -39,8 +39,7 @@ test("edit topic title", () =>expect(topics.all.length).toBeGreaterThan(1);await topics.add({ id: topic._topic.id, title: "Hello22" });expect(topics.all.length).toBeGreaterThan(1);- expect(topics.topic("Home")).toBeUndefined();- expect(topics.topic("Hello22")).toBeDefined();+ expect(topics.topic("Hello22")._topic.title).toBe("Hello22");}));test("duplicate topic to notebook should not be added", () =>@@ -68,6 +67,18 @@ test("delete a topic", () =>notebookTest().then(async ({ db, id }) => {let topics = db.notebooks.notebook(id).topics;await topics.add("Home");- await topics.delete("Home");- expect(topics.all.findIndex(v => v.title === "Home")).toBe(-1);+ await topics.delete(topics.topic("Home")._topic.id);+ expect(topics.all.findIndex((v) => v.title === "Home")).toBe(-1);}));++test("delete note from edited topic", () =>+ notebookTest().then(async ({ id }) =>+ noteTest().then(async ({ db, id: noteId }) => {+ let topics = db.notebooks.notebook(id).topics;+ await topics.add("Home");+ let topic = topics.topic("Home");+ await db.notes.move({ id, topic: topic._topic.title }, noteId);+ await topics.add({ id: topic._topic.id, title: "Hello22" });+ await db.notes.delete(noteId);+ })+ ));diff --git a/packages/core/__tests__/trash.test.js b/packages/core/__tests__/trash.test.jsindex 8add0665f..43ed025ff 100644--- a/packages/core/__tests__/trash.test.js+++ b/packages/core/__tests__/trash.test.js@@ -26,22 +26,26 @@ test("permanently delete a note", () =>expect(delta.deleted).toBe(true);}));-test("restore a deleted note", () =>+test("restore a deleted note that was in a notebook", () =>noteTest().then(async ({ db, id }) => {let nbId = await db.notebooks.add(TEST_NOTEBOOK);await db.notebooks.notebook(nbId).topics.topic("General").add(id);await db.notes.delete(id);await db.trash.restore(db.trash.all[0].id);expect(db.trash.all.length).toBe(0);+let note = db.notes.note(id);+expect(note).toBeDefined();expect(await note.text()).toBe(TEST_NOTE.content.text);expect(await note.delta()).toStrictEqual(TEST_NOTE.content.delta);- expect(db.notebooks.notebook(nbId).topics.topic("General").has(id)).toBe(- true- );- expect(db.notes.note(id).notebook.id).toBe(nbId);- expect(db.notes.note(id).notebook.topic).toBe("General");++ const notebook = db.notebooks.notebook(nbId);+ expect(notebook.topics.topic("General").has(id)).toBe(true);++ expect(note.notebook.id).toBe(nbId);+ expect(note.notebook.topic).toBeDefined();+ expect(notebook.topics.has("General")).toBeDefined();}));test("delete a locked note", () =>@@ -100,8 +104,10 @@ test("restore a deleted notebook", () =>await db.trash.restore(db.trash.all[0].id);let notebook = db.notebooks.notebook(id);expect(notebook).toBeDefined();- expect(db.notes.note(noteId).notebook.id).toBe(id);- expect(db.notes.note(noteId).notebook.topic).toBe("General");+ let note = db.notes.note(noteId);+ expect(note.notebook.id).toBe(id);+ expect(note.notebook.topic).toBeDefined();+ expect(notebook.topics.topic(note.notebook.topic)).toBeDefined();}));test("restore a notebook that has deleted notes", () =>diff --git a/packages/core/collections/topics.js b/packages/core/collections/topics.jsindex 8f3e18aff..842cc7459 100644--- a/packages/core/collections/topics.js+++ b/packages/core/collections/topics.js@@ -1,5 +1,6 @@import Topic from "../models/topic";import { qclone } from "qclone";+import id from "../utils/id";export default class Topics {/**@@ -13,7 +14,11 @@ export default class Topics {}has(topic) {- return this.all.findIndex((v) => v.title === (topic.title || topic)) > -1;+ return (+ this.all.findIndex(+ (v) => v.id === topic || v.title === (topic.title || topic)+ ) > -1+ );}_dedupe(source) {@@ -22,10 +27,9 @@ export default class Topics {for (let index = 0; index < length; index++) {let value = source[index];if (value.id) {- seen.set(value.id, {- ...seen.get(value.id),+ seen.set(value.title, {+ ...seen.get(value.title),...value,- id: value.title,});continue;}@@ -67,7 +71,7 @@ export default class Topics {*/topic(topic) {if (typeof topic === "string") {- topic = this.all.find((t) => t.title === topic);+ topic = this.all.find((t) => t.id === topic || t.title === topic);}if (!topic) return;return new Topic(topic, this._notebookId, this._db);@@ -93,7 +97,7 @@ function makeTopic(topic, notebookId) {if (typeof topic !== "string") return { ...topic, dateEdited: Date.now() };return {type: "topic",- id: topic,+ id: id(), //topic,notebookId,title: topic,dateCreated: Date.now(),diff --git a/packages/core/models/topic.js b/packages/core/models/topic.jsindex d3774978c..50a7d13f4 100644--- a/packages/core/models/topic.js+++ b/packages/core/models/topic.js@@ -38,7 +38,7 @@ export default class Topic {if (note.notebook && note.notebook.id && note.notebook.topic) {if (note.notebook.id === this._notebookId &&- note.notebook.topic === topic.title+ note.notebook.topic === topic.id)return this;await this._db.notebooks@@ -48,7 +48,7 @@ export default class Topic {}await this._db.notes.add({id: noteId,- notebook: { id: this._notebookId, topic: topic.title },+ notebook: { id: this._notebookId, topic: topic.id },});topic.totalNotes++;}