Files
Trilium/src/becca/entities/brevision.ts

206 lines
6.9 KiB
TypeScript
Raw Normal View History

"use strict";
import protectedSessionService = require('../../services/protected_session');
import utils = require('../../services/utils');
import dateUtils = require('../../services/date_utils');
import becca = require('../becca');
import AbstractBeccaEntity = require('./abstract_becca_entity');
import sql = require('../../services/sql');
import BAttachment = require('./battachment');
import { AttachmentRow, RevisionRow } from './rows';
interface ContentOpts {
/** will also save this BRevision entity */
2024-02-17 10:56:27 +02:00
forceSave?: boolean;
}
interface GetByIdOpts {
includeContentLength?: boolean;
}
2018-08-22 23:37:06 +02:00
/**
* Revision represents a snapshot of note's title and content at some point in the past.
* It's used for seamless note versioning.
2018-08-22 23:37:06 +02:00
*/
2024-02-17 10:56:27 +02:00
class BRevision extends AbstractBeccaEntity<BRevision> {
static get entityName() { return "revisions"; }
static get primaryKeyName() { return "revisionId"; }
2023-06-28 21:05:31 +02:00
static get hashedProperties() { return ["revisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated",
"utcDateLastEdited", "utcDateCreated", "utcDateModified", "blobId"]; }
2018-01-29 23:35:36 -05:00
revisionId?: string;
noteId: string;
type: string;
mime: string;
isProtected: boolean;
title: string;
blobId?: string;
dateLastEdited?: string;
dateCreated: string;
utcDateLastEdited?: string;
utcDateCreated: string;
contentLength?: number;
content?: string;
constructor(row: RevisionRow, titleDecrypted = false) {
super();
this.revisionId = row.revisionId;
this.noteId = row.noteId;
this.type = row.type;
this.mime = row.mime;
this.isProtected = !!row.isProtected;
this.title = row.title;
2023-03-15 22:44:08 +01:00
this.blobId = row.blobId;
this.dateLastEdited = row.dateLastEdited;
this.dateCreated = row.dateCreated;
this.utcDateLastEdited = row.utcDateLastEdited;
this.utcDateCreated = row.utcDateCreated;
this.utcDateModified = row.utcDateModified;
2021-07-04 16:49:23 +02:00
this.contentLength = row.contentLength;
if (this.isProtected && !titleDecrypted) {
const decryptedTitle = protectedSessionService.isProtectedSessionAvailable() ? protectedSessionService.decryptString(this.title) : null;
this.title = decryptedTitle || "[protected]";
}
}
2020-06-20 12:31:38 +02:00
getNote() {
return becca.notes[this.noteId];
}
/** @returns true if the note has string content (not binary) */
hasStringContent(): boolean {
return utils.isStringNote(this.type, this.mime);
}
2023-04-14 16:49:06 +02:00
isContentAvailable() {
return !this.revisionId // new note which was not encrypted yet
2023-04-14 16:49:06 +02:00
|| !this.isProtected
|| protectedSessionService.isProtectedSessionAvailable()
}
/*
* Note revision content has quite special handling - it's not a separate entity, but a lazily loaded
* part of Revision entity with its own sync. The reason behind this hybrid design is that
* content can be quite large, and it's not necessary to load it / fill memory for any note access even
* if we don't need a content, especially for bulk operations like search.
*
* This is the same approach as is used for Note's content.
*/
// FIXME: initial declaration included Buffer, but everywhere it's treated as a string.
getContent(): string {
return this._getContent() as string;
}
2023-10-02 15:24:40 +02:00
/**
* @throws Error in case of invalid JSON */
getJsonContent(): {} | null {
2023-10-02 15:24:40 +02:00
const content = this.getContent();
if (!content || !content.trim()) {
return null;
}
return JSON.parse(content);
}
/** @returns valid object or null if the content cannot be parsed as JSON */
getJsonContentSafely(): {} | null {
2023-10-02 15:24:40 +02:00
try {
return this.getJsonContent();
}
catch (e) {
return null;
}
}
2024-02-17 10:56:27 +02:00
setContent(content: any, opts: ContentOpts = {}) {
2023-03-16 16:37:31 +01:00
this._setContent(content, opts);
2021-08-07 21:21:30 +02:00
}
getAttachments(): BAttachment[] {
return sql.getRows<AttachmentRow>(`
2023-04-19 22:47:33 +02:00
SELECT attachments.*
FROM attachments
WHERE ownerId = ?
AND isDeleted = 0`, [this.revisionId])
2023-04-19 22:47:33 +02:00
.map(row => new BAttachment(row));
}
getAttachmentById(attachmentId: String, opts: GetByIdOpts = {}): BAttachment | null {
2023-10-02 15:24:40 +02:00
opts.includeContentLength = !!opts.includeContentLength;
const query = opts.includeContentLength
? `SELECT attachments.*, LENGTH(blobs.content) AS contentLength
FROM attachments
JOIN blobs USING (blobId)
WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`
: `SELECT * FROM attachments WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`;
return sql.getRows<AttachmentRow>(query, [this.revisionId, attachmentId])
2023-10-02 15:24:40 +02:00
.map(row => new BAttachment(row))[0];
}
getAttachmentsByRole(role: string): BAttachment[] {
return sql.getRows<AttachmentRow>(`
2023-10-02 15:24:40 +02:00
SELECT attachments.*
FROM attachments
WHERE ownerId = ?
AND role = ?
AND isDeleted = 0
ORDER BY position`, [this.revisionId, role])
.map(row => new BAttachment(row));
}
getAttachmentByTitle(title: string): BAttachment {
// cannot use SQL to filter by title since it can be encrypted
return this.getAttachments().filter(attachment => attachment.title === title)[0];
2023-10-02 15:24:40 +02:00
}
2019-11-09 16:51:51 +01:00
beforeSaving() {
super.beforeSaving();
this.utcDateModified = dateUtils.utcNowDateTime();
2019-11-09 16:51:51 +01:00
}
getPojo() {
return {
revisionId: this.revisionId,
noteId: this.noteId,
type: this.type,
mime: this.mime,
isProtected: this.isProtected,
title: this.title || undefined,
2023-03-15 22:44:08 +01:00
blobId: this.blobId,
dateLastEdited: this.dateLastEdited,
dateCreated: this.dateCreated,
utcDateLastEdited: this.utcDateLastEdited,
utcDateCreated: this.utcDateCreated,
utcDateModified: this.utcDateModified,
2023-03-16 17:49:35 +01:00
content: this.content, // used when retrieving full note revision to frontend
contentLength: this.contentLength
};
}
getPojoToSave() {
const pojo = this.getPojo();
2023-03-16 17:49:35 +01:00
delete pojo.content; // not getting persisted
delete pojo.contentLength; // not getting persisted
if (pojo.isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) {
pojo.title = protectedSessionService.encrypt(this.title) || undefined;
}
else {
// updating protected note outside of protected session means we will keep original ciphertexts
delete pojo.title;
}
}
return pojo;
}
}
2024-02-17 10:56:27 +02:00
export = BRevision;