From 5c99e0bc21aefe0f4f8e19d285047b65521cf99d Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Fri, 5 Jan 2024 18:48:37 +0500 Subject: [PATCH] core: simplify encryption content checks --- packages/core/src/api/monographs.ts | 4 +--- packages/core/src/api/vault.ts | 7 ++----- packages/core/src/collections/content.ts | 17 ++++++++--------- packages/core/src/collections/notes.ts | 6 +++--- packages/core/src/types.ts | 2 +- 5 files changed, 15 insertions(+), 21 deletions(-) diff --git a/packages/core/src/api/monographs.ts b/packages/core/src/api/monographs.ts index 13b18636f..54ed21ccb 100644 --- a/packages/core/src/api/monographs.ts +++ b/packages/core/src/api/monographs.ts @@ -21,7 +21,6 @@ import http from "../utils/http"; import Constants from "../utils/constants"; import Database from "."; import { Note, isDeleted } from "../types"; -import { isUnencryptedContent } from "../collections/content"; import { Cipher } from "@notesnook/crypto"; import { isFalse } from "../database"; @@ -101,8 +100,7 @@ export class Monographs { if (!contentItem || isDeleted(contentItem)) throw new Error("Could not find content for this note."); - if (!isUnencryptedContent(contentItem)) - throw new Error("Cannot published locked notes."); + if (contentItem.locked) throw new Error("Cannot published locked notes."); const content = await this.db.content.downloadMedia( `monograph-${noteId}`, diff --git a/packages/core/src/api/vault.ts b/packages/core/src/api/vault.ts index 35fa2d15c..4511ab846 100644 --- a/packages/core/src/api/vault.ts +++ b/packages/core/src/api/vault.ts @@ -23,10 +23,7 @@ import { CHECK_IDS, EV, EVENTS, checkIsUserPremium } from "../common"; import { tinyToTiptap } from "../migrations"; import { isCipher } from "../database/crypto"; import { Note } from "../types"; -import { - isEncryptedContent, - isUnencryptedContent -} from "../collections/content"; +import { isEncryptedContent } from "../collections/content"; import { NoteContent } from "../collections/session-content"; export const VAULT_ERRORS = { @@ -296,7 +293,7 @@ export default class Vault { // Case: when note is being newly locked if (!note.locked && (!data || !type) && !!contentId) { const rawContent = await this.db.content.get(contentId); - if (!rawContent || !isUnencryptedContent(rawContent)) + if (!rawContent || rawContent.locked) return await this.db.notes.add({ id, locked: true diff --git a/packages/core/src/collections/content.ts b/packages/core/src/collections/content.ts index 698f6893d..b658a9371 100644 --- a/packages/core/src/collections/content.ts +++ b/packages/core/src/collections/content.ts @@ -26,7 +26,6 @@ import { Attachment, ContentItem, ContentType, - EncryptedContentItem, UnencryptedContentItem, isDeleted } from "../types"; @@ -113,7 +112,7 @@ export class Content implements ICollection { await this.collection.update([content.id], { dateEdited: content.dateEdited, localOnly: content.localOnly, - conflicted: content.conflicted, + conflicted: content.dateResolved ? null : content.conflicted, dateResolved: content.dateResolved, noteId: content.noteId, ...contentData @@ -370,14 +369,14 @@ export class Content implements ICollection { // } } -export function isUnencryptedContent( - content: ContentItem -): content is UnencryptedContentItem { - return content.locked === false; +export function isDecryptedContent( + content: NoteContent +): content is NoteContent { + return !isCipher(content.data); } export function isEncryptedContent( - content: ContentItem -): content is EncryptedContentItem { - return content.locked === true; + content: NoteContent +): content is NoteContent { + return isCipher(content.data); } diff --git a/packages/core/src/collections/notes.ts b/packages/core/src/collections/notes.ts index 5b0999825..ab7a48081 100644 --- a/packages/core/src/collections/notes.ts +++ b/packages/core/src/collections/notes.ts @@ -22,7 +22,7 @@ import { getContentFromData } from "../content-types"; import { NEWLINE_STRIP_REGEX, formatTitle } from "../utils/title-format"; import { clone } from "../utils/clone"; import { Tiptap } from "../content-types/tiptap"; -import { EMPTY_CONTENT, isUnencryptedContent } from "./content"; +import { EMPTY_CONTENT } from "./content"; import { CHECK_IDS, checkIsUserPremium } from "../common"; import { buildFromTemplate } from "../utils/templates"; import { Note, TrashOrItem, isTrashItem, isDeleted } from "../types"; @@ -279,7 +279,7 @@ export class Notes implements ICollection { const rawContent = note.contentId ? await this.db.content.get(note.contentId) : undefined; - if (rawContent && !isUnencryptedContent(rawContent)) return false; + if (rawContent && rawContent.locked) return false; options.contentItem = rawContent || EMPTY_CONTENT(note.id); } @@ -326,7 +326,7 @@ export class Notes implements ICollection { const content = note.contentId ? await this.db.content.get(note.contentId) : undefined; - if (content && (isDeleted(content) || !isUnencryptedContent(content))) + if (content && (isDeleted(content) || content.locked)) throw new Error("Cannot duplicate a locked or deleted note."); const duplicateId = await this.db.notes.add({ ...clone(note), diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 1b2543a41..b4ce3097a 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -356,7 +356,7 @@ export interface BaseContentItem extends BaseItem { localOnly: boolean; dateResolved?: number; sessionId?: string; - conflicted?: UnencryptedContentItem; + conflicted?: ContentItem; } export type UnencryptedContentItem = BaseContentItem & {