Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: simplify encryption content checks

Abdullah Attae14bb065dc97
diff --git a/packages/core/src/api/monographs.ts b/packages/core/src/api/monographs.ts
index 2070189c7..8344225fd 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 8a663eaa2..28fb16835 100644
--- a/packages/core/src/api/vault.ts
+++ b/packages/core/src/api/vault.ts
@@ -292,8 +292,8 @@ export default class Vault {
// Case: when note is being newly locked
if (!locked && (!data || !type)) {
- const content = await this.db.content.findByNoteId(id);
- if (!content || content.locked) {
+ const rawContent = await this.db.content.findByNoteId(id);
+ if (!rawContent || rawContent.locked) {
await this.db.relations.add(vault, {
id,
type: "note"
@@ -303,8 +303,8 @@ export default class Vault {
// NOTE:
// At this point, the note already has all the attachments extracted
// so we should just encrypt it as normal.
- data = content.data;
- type = content.type;
+ data = rawContent.data;
+ type = rawContent.type;
} else if (data && type) {
data = await this.db.content.extractAttachments({
data,
diff --git a/packages/core/src/collections/content.ts b/packages/core/src/collections/content.ts
index f95e50b56..9438df674 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
@@ -352,14 +351,14 @@ export class Content implements ICollection {
}
}
-export function isUnencryptedContent(
- content: ContentItem
-): content is UnencryptedContentItem {
- return content.locked === false;
+export function isDecryptedContent(
+ content: NoteContent<boolean>
+): content is NoteContent<false> {
+ return !isCipher(content.data);
}
export function isEncryptedContent(
- content: ContentItem
-): content is EncryptedContentItem {
- return content.locked === true;
+ content: NoteContent<boolean>
+): content is NoteContent<true> {
+ return isCipher(content.data);
}
diff --git a/packages/core/src/collections/notes.ts b/packages/core/src/collections/notes.ts
index 4c0cae56f..c08e76d55 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";
@@ -266,7 +266,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);
}
@@ -313,7 +313,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 062f50bea..73bb1eb1b 100644
--- a/packages/core/src/types.ts
+++ b/packages/core/src/types.ts
@@ -374,7 +374,7 @@ export interface BaseContentItem extends BaseItem<ContentType> {
localOnly: boolean;
dateResolved?: number;
sessionId?: string;
- conflicted?: UnencryptedContentItem;
+ conflicted?: ContentItem;
/**
* @deprecated only kept here for migration purposes