Notes/src/becca/entities/bnote_revision.js

134 lines
4.4 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 dateUtils = require('../../services/date_utils');
2022-01-10 17:09:20 +01:00
const becca = require('../becca');
const AbstractBeccaEntity = require("./abstract_becca_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 AbstractBeccaEntity
2018-08-22 23:37:06 +02:00
*/
class BNoteRevision extends AbstractBeccaEntity {
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, titleDecrypted = false) {
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} */
2023-03-15 22:44:08 +01:00
this.blobId = row.blobId;
/** @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 && !titleDecrypted) {
this.title = protectedSessionService.isProtectedSessionAvailable()
? protectedSessionService.decryptString(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 its 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 {*} */
getContent() {
return this._getContent();
}
2023-03-16 16:37:31 +01:00
/**
* @param content
* @param {object} [opts]
* @param {object} [opts.forceSave=false] - will also save this BNoteRevision entity
*/
setContent(content, opts) {
this._setContent(content, opts);
2021-08-07 21:21:30 +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 {
noteRevisionId: this.noteRevisionId,
noteId: this.noteId,
type: this.type,
mime: this.mime,
isProtected: this.isProtected,
title: this.title,
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,
content: this.content, // used when retrieving full note revision to frontend
contentLength: this.contentLength
};
}
getPojoToSave() {
const pojo = this.getPojo();
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;
}
}
module.exports = BNoteRevision;