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
core: ensure session content remains consistent when new session is created
Ammar Ahmed6b2bf369ff8c
diff --git a/packages/core/__tests__/note-history.test.js b/packages/core/__tests__/note-history.test.jsindex c1dcd7561..7fc57c73d 100644--- a/packages/core/__tests__/note-history.test.js+++ b/packages/core/__tests__/note-history.test.js@@ -251,7 +251,6 @@ test("note history item can be created by setting note title and content both",content = await db.noteHistory.sessionContent.get(items[0].sessionContentId);- console.log(content);expect(content.data).toBe(TEST_NOTE.content.data);expect(content.title).toBe("Test note");@@ -314,3 +313,48 @@ test("restoring an old session should replace note's content and title", () =>);}));++test.only("note history item has consistent note and title data between sessions", () =>+ noteTest({+ ...TEST_NOTE,+ title: "Test note",+ sessionId: "notesession1"+ }).then(async ({ db, id }) => {+ expect(await db.noteHistory.get(id).count()).toBe(1);+ const history = db.noteHistory.get(id);+ let items = await history.items();+ let content = await db.noteHistory.sessionContent.get(+ items[0].sessionContentId+ );+ expect(await db.noteHistory.collection.count()).toBe(1);+ expect(content.data).toBe(TEST_NOTE.content.data);+ expect(content.title).toBe("Test note");++ await db.notes.add({+ id: id,+ content: TEST_NOTE.content,+ sessionId: "notesession2"+ });++ items = await history.items();+ content = await db.noteHistory.sessionContent.get(+ items[1].sessionContentId+ );++ expect(content.data).toBe(TEST_NOTE.content.data);+ expect(content.title).toBe("Test note");++ await db.notes.add({+ id: id,+ title: "Test note updated",+ sessionId: "notesession3"+ });++ items = await history.items();+ content = await db.noteHistory.sessionContent.get(+ items[2].sessionContentId+ );++ expect(content.data).toBe(TEST_NOTE.content.data);+ expect(content.title).toBe("Test note updated");+ }));diff --git a/packages/core/src/collections/notes.ts b/packages/core/src/collections/notes.tsindex 7d3364f4d..ad5c868e5 100644--- a/packages/core/src/collections/notes.ts+++ b/packages/core/src/collections/notes.ts@@ -206,7 +206,7 @@ export class Notes implements ICollection {this.totalNotes++;}- if (item.sessionId) {+ if (item.sessionId && typeof item.title === "string") {await this.db.noteHistory.add(item.sessionId, {title: item.title,noteId: iddiff --git a/packages/core/src/collections/session-content.ts b/packages/core/src/collections/session-content.tsindex 1931b0f7f..b5d112656 100644--- a/packages/core/src/collections/session-content.ts+++ b/packages/core/src/collections/session-content.ts@@ -44,7 +44,7 @@ export class SessionContent implements ICollection {async add<TLocked extends boolean>(sessionId: string,- content: Partial<NoteContent<TLocked>> & { title?: string },+ content: Partial<NoteContent<TLocked>> & { title?: string; noteId: string },locked?: TLocked) {if (!sessionId || !content) return;@@ -52,8 +52,10 @@ export class SessionContent implements ICollection {// locked || isCipher(content.data)// ? content.data// : await this.db.compressor().compress(content.data);-const sessionContentItemId = makeSessionContentId(sessionId);+ const sessionContentExists = await this.collection.exists(+ sessionContentItemId+ );const sessionItem: Partial<SessionContentItem> = {type: "sessioncontent",id: sessionContentItemId,@@ -67,13 +69,29 @@ export class SessionContent implements ICollection {if (content.data && content.type) {sessionItem.data = content.data;sessionItem.contentType = content.type;++ if (typeof content.title !== "string" && !sessionContentExists) {+ const note = await this.db.notes.note(content.noteId);+ sessionItem.title = note?.title;+ }}if (content.title) {sessionItem.title = content.title;++ if (!content.data && !content.type && !sessionContentExists) {+ const note = await this.db.notes.note(content.noteId);+ if (note?.contentId) {+ const noteContent = await this.db.content.get(note?.contentId);+ if (noteContent) {+ sessionItem.data = noteContent?.data;+ sessionItem.contentType = noteContent?.type;+ }+ }+ }}- if (await this.collection.exists(sessionContentItemId)) {+ if (sessionContentExists) {this.collection.update([sessionContentItemId], sessionItem);} else {await this.collection.upsert(sessionItem as SessionContentItem);