core: add checks when accessing a note using this._db.notes.note

This commit is contained in:
Abdullah Atta
2023-01-17 11:44:34 +05:00
committed by Abdullah Atta
parent ffacb8818c
commit a62ca2aada
4 changed files with 28 additions and 17 deletions

View File

@@ -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.");

View File

@@ -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;

View File

@@ -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
});

View File

@@ -133,10 +133,13 @@ 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);
}
return items;