From c452e1524d0fb4d12340c78e224c8dcb00c4bedb Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Fri, 24 Nov 2023 08:38:18 +0500 Subject: [PATCH] core: fix error on locking notes --- packages/core/src/api/vault.ts | 17 +++++++++++++++-- packages/core/src/collections/content.ts | 12 ++++-------- packages/core/src/collections/notes.ts | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/core/src/api/vault.ts b/packages/core/src/api/vault.ts index ec68344a8..615179964 100644 --- a/packages/core/src/api/vault.ts +++ b/packages/core/src/api/vault.ts @@ -129,7 +129,12 @@ export default class Vault { } for (const content of contentItems) { - await this.encryptContent(content, newPassword, content.id); + await this.encryptContent( + content, + content.noteId, + newPassword, + content.id + ); } await this.db.storage().remove("vaultKey"); @@ -223,6 +228,7 @@ export default class Vault { private async encryptContent( content: NoteContent, + noteId: string, password: string, contentId?: string, sessionId?: string @@ -233,6 +239,7 @@ export default class Vault { await this.db.content.add({ id: contentId, + noteId, sessionId, data: encryptedContent, type: content.type @@ -312,7 +319,13 @@ export default class Vault { } if (data && type) - await this.encryptContent({ data, type }, password, contentId, sessionId); + await this.encryptContent( + { data, type }, + id, + password, + contentId, + sessionId + ); return await this.db.notes.add({ id, diff --git a/packages/core/src/collections/content.ts b/packages/core/src/collections/content.ts index 977001d5c..0e32dee7c 100644 --- a/packages/core/src/collections/content.ts +++ b/packages/core/src/collections/content.ts @@ -75,15 +75,11 @@ export class Content implements ICollection { const id = content.id || getId(); - if (!content.noteId) - throw new Error("No noteId found to link the content to."); - if (!content.type) throw new Error("Please specify content's type."); - const encryptedData = isCipher(content.data) ? content.data : undefined; let unencryptedData = typeof content.data === "string" ? content.data : undefined; - if (unencryptedData) + if (unencryptedData && content.type && content.noteId) unencryptedData = await this.extractAttachments({ type: content.type, data: unencryptedData, @@ -105,13 +101,13 @@ export class Content implements ICollection { ...contentData }); - if (content.sessionId && contentData) + if (content.sessionId && contentData && content.type && content.noteId) await this.db.noteHistory.add(content.sessionId, { noteId: content.noteId, type: content.type, ...contentData }); - } else { + } else if (content.noteId) { const contentItem: ContentItem = { type: "tiptap", noteId: content.noteId, @@ -134,7 +130,7 @@ export class Content implements ICollection { if (content.sessionId) await this.db.noteHistory.add(content.sessionId, contentItem); - } + } else return; return id; } diff --git a/packages/core/src/collections/notes.ts b/packages/core/src/collections/notes.ts index bb7156aef..64c1552e7 100644 --- a/packages/core/src/collections/notes.ts +++ b/packages/core/src/collections/notes.ts @@ -95,7 +95,7 @@ export class Notes implements ICollection { if (oldNote) note.dateEdited = Date.now(); } - if (item.localOnly !== undefined) { + if (note.contentId && item.localOnly !== undefined) { await this.db.content.add({ id: note.contentId, localOnly: !!item.localOnly