Notes/src/becca/entities/note_revision.js

197 lines
6.5 KiB
JavaScript
Raw Normal View History

"use strict";
2021-05-17 22:09:49 +02:00
const protectedSessionService = require('../../services/protected_session');
const utils = require('../../services/utils');
const sql = require('../../services/sql');
const dateUtils = require('../../services/date_utils');
2022-01-10 17:09:20 +01:00
const becca = require('../becca');
2021-05-17 22:09:49 +02:00
const entityChangesService = require('../../services/entity_changes');
2022-01-10 17:09:20 +01:00
const AbstractEntity = require("./abstract_entity");
2018-08-22 23:37:06 +02:00
/**
* NoteRevision represents snapshot of note's title and content at some point in the past.
* It's used for seamless note versioning.
2022-04-16 00:17:32 +02:00
*
* @extends AbstractEntity
2018-08-22 23:37:06 +02:00
*/
class NoteRevision extends AbstractEntity {
static get entityName() { return "note_revisions"; }
2018-01-30 20:12:19 -05:00
static get primaryKeyName() { return "noteRevisionId"; }
2020-12-16 14:36:24 +01:00
static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; }
2018-01-29 23:35:36 -05:00
constructor(row) {
super();
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.noteRevisionId = row.noteRevisionId;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.noteId = row.noteId;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.type = row.type;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.mime = row.mime;
2021-10-29 21:37:12 +02:00
/** @type {boolean} */
this.isProtected = !!row.isProtected;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.title = row.title;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.dateLastEdited = row.dateLastEdited;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.dateCreated = row.dateCreated;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.utcDateLastEdited = row.utcDateLastEdited;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.utcDateCreated = row.utcDateCreated;
2021-10-29 21:37:12 +02:00
/** @type {string} */
this.utcDateModified = row.utcDateModified;
2021-10-29 21:37:12 +02:00
/** @type {number} */
2021-07-04 16:49:23 +02:00
this.contentLength = row.contentLength;
if (this.isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) {
2019-11-02 07:50:23 +01:00
this.title = protectedSessionService.decryptString(this.title);
}
else {
2021-10-12 22:36:22 +02:00
this.title = "[protected]";
}
}
}
2020-06-20 12:31:38 +02:00
getNote() {
return becca.notes[this.noteId];
}
/** @returns {boolean} true if the note has string content (not binary) */
isStringNote() {
return utils.isStringNote(this.type, this.mime);
}
/*
* Note revision content has quite special handling - it's not a separate entity, but a lazily loaded
* part of NoteRevision entity with it's own sync. 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.
*/
/** @returns {*} */
2020-06-20 12:31:38 +02:00
getContent(silentNotFoundError = false) {
const res = sql.getRow(`SELECT content FROM note_revision_contents WHERE noteRevisionId = ?`, [this.noteRevisionId]);
if (!res) {
if (silentNotFoundError) {
return undefined;
}
else {
throw new Error("Cannot find note revision content for noteRevisionId=" + this.noteRevisionId);
}
}
let content = res.content;
if (this.isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) {
content = protectedSessionService.decrypt(content);
}
else {
content = "";
}
}
2020-03-26 16:59:40 +01:00
if (this.isStringNote()) {
return content === null
2020-03-26 16:59:40 +01:00
? ""
: content.toString("UTF-8");
2020-03-26 16:59:40 +01:00
}
else {
return content;
2020-03-26 16:59:40 +01:00
}
}
2021-08-07 21:21:30 +02:00
setContent(content, ignoreMissingProtectedSession = false) {
const pojo = {
noteRevisionId: this.noteRevisionId,
content: content,
utcDateModified: dateUtils.utcNowDateTime()
};
if (this.isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) {
pojo.content = protectedSessionService.encrypt(pojo.content);
}
2021-08-07 21:21:30 +02:00
else if (!ignoreMissingProtectedSession) {
throw new Error(`Cannot update content of noteRevisionId=${this.noteRevisionId} since we're out of protected session.`);
}
}
2020-06-20 12:31:38 +02:00
sql.upsert("note_revision_contents", "noteRevisionId", pojo);
2021-08-07 21:21:30 +02:00
const hash = utils.hash(this.noteRevisionId + "|" + pojo.content.toString());
2020-12-14 22:12:26 +01:00
entityChangesService.addEntityChange({
entityName: 'note_revision_contents',
entityId: this.noteRevisionId,
hash: hash,
isErased: false,
2021-06-29 23:45:45 +02:00
utcDateChanged: this.getUtcDateChanged(),
isSynced: true
2021-06-30 20:54:15 +02:00
});
}
2021-08-07 21:21:30 +02:00
/** @returns {{contentLength, dateModified, utcDateModified}} */
getContentMetadata() {
return sql.getRow(`
SELECT
LENGTH(content) AS contentLength,
dateModified,
utcDateModified
FROM note_revision_contents
WHERE noteRevisionId = ?`, [this.noteRevisionId]);
}
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 {
noteRevisionId: this.noteRevisionId,
noteId: this.noteId,
type: this.type,
mime: this.mime,
isProtected: this.isProtected,
title: this.title,
dateLastEdited: this.dateLastEdited,
dateCreated: this.dateCreated,
utcDateLastEdited: this.utcDateLastEdited,
utcDateCreated: this.utcDateCreated,
utcDateModified: this.utcDateModified,
2021-10-03 10:42:46 +02:00
content: this.content,
contentLength: this.contentLength
};
}
getPojoToSave() {
const pojo = this.getPojo();
2021-10-03 10:42:46 +02: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);
}
else {
// updating protected note outside of protected session means we will keep original ciphertexts
delete pojo.title;
}
}
return pojo;
}
}
2020-06-20 12:31:38 +02:00
module.exports = NoteRevision;