From a62ca2aadab766b4c4deb7c3a7bbde8e29ba0eb1 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Tue, 17 Jan 2023 11:44:34 +0500 Subject: [PATCH] core: add checks when accessing a note using `this._db.notes.note` --- packages/core/api/vault.js | 16 +++++++++++----- packages/core/collections/attachments.js | 8 ++++---- packages/core/collections/note-history.js | 14 ++++++++------ packages/core/collections/relations.js | 7 +++++-- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/packages/core/api/vault.js b/packages/core/api/vault.js index 7a47e991f..cd82621bb 100644 --- a/packages/core/api/vault.js +++ b/packages/core/api/vault.js @@ -176,8 +176,9 @@ export default class Vault { * @param {string} password The password to unlock note with */ async remove(noteId, password) { - const note = this._db.notes.note(noteId).data; - await this._unlockNote(note, password, true); + const note = this._db.notes.note(noteId); + if (!note) return; + await this._unlockNote(note.data, password, true); } /** @@ -186,8 +187,10 @@ export default class Vault { * @param {string} password The password to open note with */ async open(noteId, password) { - const note = this._db.notes.note(noteId).data; - const unlockedNote = await this._unlockNote(note, password, false); + const note = this._db.notes.note(noteId); + if (!note) return; + + const unlockedNote = await this._unlockNote(note.data, password, false); this._password = password; return unlockedNote; } @@ -271,7 +274,10 @@ export default class Vault { async _lockNote(note, password) { let { id, content: { type, data } = {}, sessionId, title } = note; - note = this._db.notes.note(id).data; + note = this._db.notes.note(id); + if (!note) return; + + note = note.data; const contentId = note.contentId; if (!contentId) throw new Error("Cannot lock note because it is empty."); diff --git a/packages/core/collections/attachments.js b/packages/core/collections/attachments.js index 478e4e957..54bff0e0d 100644 --- a/packages/core/collections/attachments.js +++ b/packages/core/collections/attachments.js @@ -183,7 +183,7 @@ export default class Attachments extends Collection { async detach(attachment) { await this._db.notes.init(); - for (let noteId of attachment.noteIds) { + for (const noteId of attachment.noteIds) { const note = this._db.notes.note(noteId); if (!note) continue; const contentId = note.data.contentId; @@ -195,9 +195,9 @@ export default class Attachments extends Collection { async _canDetach(attachment) { await this._db.notes.init(); - for (let noteId of attachment.noteIds) { - const note = this._db.notes.note(noteId).data; - if (note.locked) return false; + for (const noteId of attachment.noteIds) { + const note = this._db.notes.note(noteId); + if (note && note.data.locked) return false; } return true; diff --git a/packages/core/collections/note-history.js b/packages/core/collections/note-history.js index 37e8db32d..ec101c6c2 100644 --- a/packages/core/collections/note-history.js +++ b/packages/core/collections/note-history.js @@ -85,7 +85,6 @@ export default class NoteHistory extends Collection { if (!noteId || !dateEdited || !content) return; let sessionId = `${noteId}_${dateEdited}`; let oldSession = await this._collection.getItem(sessionId); - let locked = this._db.notes.note(noteId)?.data?.locked; let session = { type: "session", @@ -96,7 +95,8 @@ export default class NoteHistory extends Collection { localOnly: true }; - if (locked) { + const note = this._db.notes.note(noteId); + if (note && note.data.locked) { session.locked = true; } @@ -178,12 +178,14 @@ export default class NoteHistory extends Collection { /** * @type {Session} */ - let session = await this._collection.getItem(sessionId); - let content = await this.sessionContent.get(session.sessionContentId); - let note = this._db.notes.note(session.noteId).data; + const session = await this._collection.getItem(sessionId); + const content = await this.sessionContent.get(session.sessionContentId); + const note = this._db.notes.note(session.noteId); + if (!note) return; + if (session.locked) { await this._db.content.add({ - id: note.contentId, + id: note.data.contentId, data: content.data, type: content.type }); diff --git a/packages/core/collections/relations.js b/packages/core/collections/relations.js index c21a91f09..9356ee674 100644 --- a/packages/core/collections/relations.js +++ b/packages/core/collections/relations.js @@ -133,9 +133,12 @@ export default class Relations extends Collection { case "reminder": item = this._db.reminders.reminder(reference.id); break; - case "note": - item = this._db.notes.note(reference.id)?.data; + case "note": { + const note = this._db.notes.note(reference.id); + if (!note) continue; + item = note.data; break; + } } if (item) items.push(item); }