Skip to content

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

core: fix locking notes with empty content (#6545)

Ammar Ahmeda68f74bc1b2c
diff --git a/packages/core/__tests__/vault.test.js b/packages/core/__tests__/vault.test.js
index f5f2677a3..e0c437568 100644
--- a/packages/core/__tests__/vault.test.js
+++ b/packages/core/__tests__/vault.test.js
@@ -115,6 +115,23 @@ test("unlock a note permanently", () =>
expect(await db.relations.from(vault, "note").has(id)).toBe(false);
}));
+test("lock an empty note", () =>
+ noteTest({ title: "I am a note" }).then(async ({ db, id }) => {
+ await db.vault.create("password");
+
+ await db.vault.add(id);
+
+ const note = await db.notes.note(id);
+ const content = await db.content.get(note.contentId);
+ const vault = await db.vaults.default();
+ expect(note.headline).toBe("");
+ expect(content.locked).toBeTruthy();
+ expect(content.noteId).toBeDefined();
+ expect(content.data.iv).toBeDefined();
+ expect(content.data.cipher).toBeDefined();
+ expect(await db.relations.from(vault, "note").has(id)).toBe(true);
+ }));
+
test("save a locked note", () =>
noteTest().then(async ({ db, id }) => {
await db.vault.create("password");
diff --git a/packages/core/src/api/vault.ts b/packages/core/src/api/vault.ts
index 80d345ee9..20dbb9afb 100644
--- a/packages/core/src/api/vault.ts
+++ b/packages/core/src/api/vault.ts
@@ -251,7 +251,7 @@ export default class Vault {
.storage()
.encrypt({ password }, JSON.stringify(content.data));
- await this.db.content.add({
+ return await this.db.content.add({
noteId,
sessionId,
data: encryptedContent,
@@ -295,7 +295,7 @@ export default class Vault {
// Case: when note is being newly locked
if (!locked && (!data || !type)) {
const rawContent = await this.db.content.findByNoteId(id);
- if (!rawContent || rawContent.locked) {
+ if (rawContent?.locked) {
await this.db.relations.add(vault, {
id,
type: "note"
@@ -305,8 +305,8 @@ export default class Vault {
// NOTE:
// At this point, the note already has all the attachments extracted
// so we should just encrypt it as normal.
- data = rawContent.data;
- type = rawContent.type;
+ data = rawContent?.data || "<p></p>";
+ type = rawContent?.type || "tiptap";
} else if (data && type) {
data = await this.db.content.postProcess({
data,
@@ -315,13 +315,14 @@ export default class Vault {
});
}
- if (data && type)
- await this.encryptContent({ data, type }, id, password, sessionId);
-
await this.db.notes.add({
id,
headline: "",
- dateEdited: Date.now()
+ dateEdited: Date.now(),
+ contentId:
+ data && type
+ ? await this.encryptContent({ data, type }, id, password, sessionId)
+ : undefined
});
await this.db.relations.add(vault, {