From f3322cc8f1a25e3556cdc845fbc760dd6decec68 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Mon, 11 Mar 2024 14:25:35 +0500 Subject: [PATCH] core: remove note from vault if its content is not locked --- packages/core/src/api/vault.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/core/src/api/vault.ts b/packages/core/src/api/vault.ts index 45f5be803..fa72c26da 100644 --- a/packages/core/src/api/vault.ts +++ b/packages/core/src/api/vault.ts @@ -20,9 +20,9 @@ along with this program. If not, see . import { Cipher } from "@notesnook/crypto"; import Database from "."; import { CHECK_IDS, EV, EVENTS, checkIsUserPremium } from "../common"; -import { tinyToTiptap } from "../migrations"; import { isCipher } from "../database/crypto"; import { NoteContent } from "../collections/session-content"; +import { Note } from "../types"; export const VAULT_ERRORS = { noVault: "ERR_NO_VAULT", @@ -187,7 +187,10 @@ export default class Vault { /** * Temporarily unlock (open) a note */ - async open(noteId: string, password?: string) { + async open( + noteId: string, + password?: string + ): Promise<(Note & { content?: NoteContent }) | undefined> { const note = await this.db.notes.note(noteId); if (!note) return; @@ -201,7 +204,7 @@ export default class Vault { this.password = password; if (!(await this.exists())) await this.create(password); } - return { ...note, ...content }; + return { ...note, content }; } /** @@ -327,9 +330,18 @@ export default class Vault { return id; } - private async unlockNote(noteId: string, password?: string, perm = false) { + private async unlockNote( + noteId: string, + password?: string, + perm = false + ): Promise | undefined> { const content = await this.db.content.findByNoteId(noteId); - if (!content || !content.locked) return; + if (!content || !content.locked) { + await this.db.relations + .to({ id: noteId, type: "note" }, "vault") + .unlink(); + return content ? { data: content.data, type: content.type } : undefined; + } const decryptedContent = await this.decryptContent(content, password); if (this.db.content.preProcess(decryptedContent)) { @@ -352,9 +364,7 @@ export default class Vault { return; } - return { - content: decryptedContent - }; + return decryptedContent; } async getKey() {