core: simplify encryption content checks

This commit is contained in:
Abdullah Atta
2024-01-05 18:48:37 +05:00
parent 71fdc3f7fc
commit 5c99e0bc21
5 changed files with 15 additions and 21 deletions

View File

@@ -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}`,

View File

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

View File

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

View File

@@ -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),

View File

@@ -356,7 +356,7 @@ export interface BaseContentItem extends BaseItem<ContentType> {
localOnly: boolean;
dateResolved?: number;
sessionId?: string;
conflicted?: UnencryptedContentItem;
conflicted?: ContentItem;
}
export type UnencryptedContentItem = BaseContentItem & {