trilium/docs/backend_api/entities_note_revision.js.html

203 lines
6.4 KiB
HTML
Raw Normal View History

2018-08-30 02:44:35 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: entities/note_revision.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: entities/note_revision.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
const Entity = require('./entity');
const protectedSessionService = require('../services/protected_session');
2019-11-09 05:34:30 +08:00
const utils = require('../services/utils');
const sql = require('../services/sql');
const dateUtils = require('../services/date_utils');
2020-10-01 04:48:30 +08:00
const entityChangesService = require('../services/entity_changes.js');
2018-08-30 02:44:35 +08:00
/**
* NoteRevision represents snapshot of note's title and content at some point in the past. It's used for seamless note versioning.
*
2019-11-17 02:07:32 +08:00
* @property {string} noteRevisionId
* @property {string} noteId
* @property {string} type
* @property {string} mime
* @property {string} title
* @property {boolean} isErased
* @property {boolean} isProtected
* @property {string} dateLastEdited
* @property {string} dateCreated
* @property {string} utcDateLastEdited
* @property {string} utcDateCreated
* @property {string} utcDateModified
2018-08-30 02:44:35 +08:00
*
* @extends Entity
*/
class NoteRevision extends Entity {
static get entityName() { return "note_revisions"; }
static get primaryKeyName() { return "noteRevisionId"; }
2020-10-01 04:48:30 +08:00
static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isErased", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; }
2018-08-30 02:44:35 +08:00
constructor(row) {
super(row);
2020-10-01 04:48:30 +08:00
this.isErased = !!this.isErased;
2018-08-30 02:44:35 +08:00
this.isProtected = !!this.isProtected;
if (this.isProtected) {
2019-11-09 05:34:30 +08:00
if (protectedSessionService.isProtectedSessionAvailable()) {
this.title = protectedSessionService.decryptString(this.title);
}
else {
this.title = "[Protected]";
}
2018-08-30 02:44:35 +08:00
}
}
2020-10-01 04:48:30 +08:00
getNote() {
return this.repository.getNote(this.noteId);
2018-08-30 02:44:35 +08:00
}
2019-11-09 05:34:30 +08:00
/** @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.
*/
2020-10-01 04:48:30 +08:00
/** @returns {*} */
getContent(silentNotFoundError = false) {
2019-11-09 05:34:30 +08:00
if (this.content === undefined) {
2020-10-01 04:48:30 +08:00
const res = sql.getRow(`SELECT content, hash FROM note_revision_contents WHERE noteRevisionId = ?`, [this.noteRevisionId]);
2019-11-09 05:34:30 +08:00
if (!res) {
if (silentNotFoundError) {
return undefined;
}
else {
throw new Error("Cannot find note revision content for noteRevisionId=" + this.noteRevisionId);
}
}
this.content = res.content;
if (this.isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) {
this.content = protectedSessionService.decrypt(this.content);
}
else {
this.content = "";
}
}
}
2020-04-08 01:19:20 +08:00
if (this.isStringNote()) {
return this.content === null
? ""
: this.content.toString("UTF-8");
}
else {
return this.content;
}
2019-11-09 05:34:30 +08:00
}
2020-10-01 04:48:30 +08:00
setContent(content) {
2019-11-09 05:34:30 +08:00
this.content = content;
const pojo = {
noteRevisionId: this.noteRevisionId,
content: content,
utcDateModified: dateUtils.utcNowDateTime(),
hash: utils.hash(this.noteRevisionId + "|" + content)
};
2018-08-30 02:44:35 +08:00
if (this.isProtected) {
2019-11-09 05:34:30 +08:00
if (protectedSessionService.isProtectedSessionAvailable()) {
pojo.content = protectedSessionService.encrypt(pojo.content);
}
else {
throw new Error(`Cannot update content of noteRevisionId=${this.noteRevisionId} since we're out of protected session.`);
}
}
2020-10-01 04:48:30 +08:00
sql.upsert("note_revision_contents", "noteRevisionId", pojo);
2019-11-09 05:34:30 +08:00
2020-10-01 04:48:30 +08:00
entityChangesService.addNoteRevisionContentEntityChange(this.noteRevisionId);
2019-11-09 05:34:30 +08:00
}
2019-11-17 02:07:32 +08:00
beforeSaving() {
super.beforeSaving();
if (this.isChanged) {
this.utcDateModified = dateUtils.utcNowDateTime();
}
}
2019-11-09 05:34:30 +08:00
// cannot be static!
updatePojo(pojo) {
if (pojo.isProtected) {
if (protectedSessionService.isProtectedSessionAvailable()) {
pojo.title = protectedSessionService.encrypt(pojo.title);
}
else {
// updating protected note outside of protected session means we will keep original ciphertexts
delete pojo.title;
}
2018-08-30 02:44:35 +08:00
}
2019-11-09 05:34:30 +08:00
delete pojo.content;
2018-08-30 02:44:35 +08:00
}
}
2020-10-01 04:48:30 +08:00
module.exports = NoteRevision;
</code></pre>
2018-08-30 02:44:35 +08:00
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="ApiToken.html">ApiToken</a></li><li><a href="Attribute.html">Attribute</a></li><li><a href="BackendScriptApi.html">BackendScriptApi</a></li><li><a href="Branch.html">Branch</a></li><li><a href="Entity.html">Entity</a></li><li><a href="Note.html">Note</a></li><li><a href="NoteRevision.html">NoteRevision</a></li><li><a href="Option.html">Option</a></li><li><a href="RecentNote.html">RecentNote</a></li></ul><h3><a href="global.html">Global</a></h3>
2018-08-30 02:44:35 +08:00
</nav>
<br class="clear">
<footer>
2020-10-01 04:48:30 +08:00
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a>
2018-08-30 02:44:35 +08:00
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>