2023-02-17 23:30:14 +08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>JSDoc: Source: becca/entities/abstract_becca_entity.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: becca/entities/abstract_becca_entity.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<article>
|
|
|
|
<pre class="prettyprint source linenums"><code>"use strict";
|
|
|
|
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const entityChangesService = require('../../services/entity_changes');
|
|
|
|
const eventService = require("../../services/events");
|
|
|
|
const dateUtils = require("../../services/date_utils");
|
|
|
|
const cls = require("../../services/cls");
|
|
|
|
const log = require("../../services/log");
|
2023-06-30 02:54:58 +08:00
|
|
|
const protectedSessionService = require("../../services/protected_session");
|
|
|
|
const blobService = require("../../services/blob");
|
2023-02-17 23:30:14 +08:00
|
|
|
|
|
|
|
let becca = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for all backend entities.
|
|
|
|
*/
|
|
|
|
class AbstractBeccaEntity {
|
|
|
|
/** @protected */
|
|
|
|
beforeSaving() {
|
|
|
|
if (!this[this.constructor.primaryKeyName]) {
|
|
|
|
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @protected */
|
|
|
|
getUtcDateChanged() {
|
|
|
|
return this.utcDateModified || this.utcDateCreated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* @returns {Becca}
|
|
|
|
*/
|
|
|
|
get becca() {
|
|
|
|
if (!becca) {
|
|
|
|
becca = require('../becca');
|
|
|
|
}
|
|
|
|
|
|
|
|
return becca;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @protected */
|
2023-10-08 01:57:53 +08:00
|
|
|
putEntityChange(isDeleted) {
|
2023-08-10 19:40:26 +08:00
|
|
|
entityChangesService.putEntityChange({
|
2023-02-17 23:30:14 +08:00
|
|
|
entityName: this.constructor.entityName,
|
|
|
|
entityId: this[this.constructor.primaryKeyName],
|
|
|
|
hash: this.generateHash(isDeleted),
|
|
|
|
isErased: false,
|
|
|
|
utcDateChanged: this.getUtcDateChanged(),
|
|
|
|
isSynced: this.constructor.entityName !== 'options' || !!this.isSynced
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-08 01:57:53 +08:00
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
generateHash(isDeleted) {
|
|
|
|
let contentToHash = "";
|
|
|
|
|
|
|
|
for (const propertyName of this.constructor.hashedProperties) {
|
|
|
|
contentToHash += `|${this[propertyName]}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDeleted) {
|
|
|
|
contentToHash += "|deleted";
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.hash(contentToHash).substr(0, 10);
|
|
|
|
}
|
|
|
|
|
2023-02-17 23:30:14 +08:00
|
|
|
/** @protected */
|
|
|
|
getPojoToSave() {
|
|
|
|
return this.getPojo();
|
|
|
|
}
|
|
|
|
|
2023-10-08 01:57:53 +08:00
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
getPojo() {
|
|
|
|
throw new Error(`Unimplemented getPojo() for entity '${this.constructor.name}'`)
|
|
|
|
}
|
|
|
|
|
2023-02-17 23:30:14 +08:00
|
|
|
/**
|
|
|
|
* Saves entity - executes SQL, but doesn't commit the transaction on its own
|
|
|
|
*
|
|
|
|
* @returns {this}
|
|
|
|
*/
|
|
|
|
save(opts = {}) {
|
|
|
|
const entityName = this.constructor.entityName;
|
|
|
|
const primaryKeyName = this.constructor.primaryKeyName;
|
|
|
|
|
|
|
|
const isNewEntity = !this[primaryKeyName];
|
|
|
|
|
2023-10-08 01:57:53 +08:00
|
|
|
this.beforeSaving(opts);
|
2023-02-17 23:30:14 +08:00
|
|
|
|
|
|
|
const pojo = this.getPojoToSave();
|
|
|
|
|
|
|
|
sql.transactional(() => {
|
|
|
|
sql.upsert(entityName, primaryKeyName, pojo);
|
|
|
|
|
|
|
|
if (entityName === 'recent_notes') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-08 01:57:53 +08:00
|
|
|
this.putEntityChange(!!this.isDeleted);
|
2023-02-17 23:30:14 +08:00
|
|
|
|
|
|
|
if (!cls.isEntityEventsDisabled()) {
|
|
|
|
const eventPayload = {
|
|
|
|
entityName,
|
|
|
|
entity: this
|
|
|
|
};
|
|
|
|
|
|
|
|
if (isNewEntity) {
|
|
|
|
eventService.emit(eventService.ENTITY_CREATED, eventPayload);
|
|
|
|
}
|
|
|
|
|
|
|
|
eventService.emit(eventService.ENTITY_CHANGED, eventPayload);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2023-06-30 02:54:58 +08:00
|
|
|
/** @protected */
|
|
|
|
_setContent(content, opts = {}) {
|
|
|
|
// client code asks to save entity even if blobId didn't change (something else was changed)
|
|
|
|
opts.forceSave = !!opts.forceSave;
|
|
|
|
opts.forceFrontendReload = !!opts.forceFrontendReload;
|
|
|
|
|
|
|
|
if (content === null || content === undefined) {
|
|
|
|
throw new Error(`Cannot set null content to ${this.constructor.primaryKeyName} '${this[this.constructor.primaryKeyName]}'`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hasStringContent()) {
|
|
|
|
content = content.toString();
|
2023-07-28 05:22:08 +08:00
|
|
|
} else {
|
2023-06-30 02:54:58 +08:00
|
|
|
content = Buffer.isBuffer(content) ? content : Buffer.from(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
const unencryptedContentForHashCalculation = this.#getUnencryptedContentForHashCalculation(content);
|
|
|
|
|
|
|
|
if (this.isProtected) {
|
|
|
|
if (protectedSessionService.isProtectedSessionAvailable()) {
|
|
|
|
content = protectedSessionService.encrypt(content);
|
|
|
|
} else {
|
|
|
|
throw new Error(`Cannot update content of blob since protected session is not available.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sql.transactional(() => {
|
|
|
|
const newBlobId = this.#saveBlob(content, unencryptedContentForHashCalculation, opts);
|
|
|
|
const oldBlobId = this.blobId;
|
|
|
|
|
|
|
|
if (newBlobId !== oldBlobId || opts.forceSave) {
|
|
|
|
this.blobId = newBlobId;
|
|
|
|
this.save();
|
|
|
|
|
|
|
|
if (newBlobId !== oldBlobId) {
|
2023-07-28 05:22:08 +08:00
|
|
|
this.#deleteBlobIfNotUsed(oldBlobId);
|
2023-06-30 02:54:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-28 05:22:08 +08:00
|
|
|
#deleteBlobIfNotUsed(oldBlobId) {
|
|
|
|
if (sql.getValue("SELECT 1 FROM notes WHERE blobId = ? LIMIT 1", [oldBlobId])) {
|
2023-06-30 02:54:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-28 05:22:08 +08:00
|
|
|
if (sql.getValue("SELECT 1 FROM attachments WHERE blobId = ? LIMIT 1", [oldBlobId])) {
|
2023-06-30 02:54:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-28 05:22:08 +08:00
|
|
|
if (sql.getValue("SELECT 1 FROM revisions WHERE blobId = ? LIMIT 1", [oldBlobId])) {
|
2023-06-30 02:54:58 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-28 05:22:08 +08:00
|
|
|
sql.execute("DELETE FROM blobs WHERE blobId = ?", [oldBlobId]);
|
2023-08-10 19:40:26 +08:00
|
|
|
// blobs are not marked as erased in entity_changes, they are just purged completely
|
|
|
|
// this is because technically every keystroke can create a new blob and there would be just too many
|
2023-07-28 05:22:08 +08:00
|
|
|
sql.execute("DELETE FROM entity_changes WHERE entityName = 'blobs' AND entityId = ?", [oldBlobId]);
|
2023-06-30 02:54:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#getUnencryptedContentForHashCalculation(unencryptedContent) {
|
|
|
|
if (this.isProtected) {
|
2023-07-28 05:22:08 +08:00
|
|
|
// a "random" prefix makes sure that the calculated hash/blobId is different for a decrypted/encrypted content
|
2023-06-30 02:54:58 +08:00
|
|
|
const encryptedPrefixSuffix = "t$[nvQg7q)&_ENCRYPTED_?M:Bf&j3jr_";
|
|
|
|
return Buffer.isBuffer(unencryptedContent)
|
|
|
|
? Buffer.concat([Buffer.from(encryptedPrefixSuffix), unencryptedContent])
|
|
|
|
: `${encryptedPrefixSuffix}${unencryptedContent}`;
|
|
|
|
} else {
|
|
|
|
return unencryptedContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#saveBlob(content, unencryptedContentForHashCalculation, opts = {}) {
|
|
|
|
/*
|
|
|
|
* We're using the unencrypted blob for the hash calculation, because otherwise the random IV would
|
|
|
|
* cause every content blob to be unique which would balloon the database size (esp. with revisioning).
|
|
|
|
* This has minor security implications (it's easy to infer that given content is shared between different
|
2023-07-28 05:22:08 +08:00
|
|
|
* notes/attachments), but the trade-off comes out clearly positive.
|
2023-06-30 02:54:58 +08:00
|
|
|
*/
|
|
|
|
const newBlobId = utils.hashedBlobId(unencryptedContentForHashCalculation);
|
|
|
|
const blobNeedsInsert = !sql.getValue('SELECT 1 FROM blobs WHERE blobId = ?', [newBlobId]);
|
|
|
|
|
|
|
|
if (!blobNeedsInsert) {
|
|
|
|
return newBlobId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pojo = {
|
|
|
|
blobId: newBlobId,
|
|
|
|
content: content,
|
|
|
|
dateModified: dateUtils.localNowDateTime(),
|
|
|
|
utcDateModified: dateUtils.utcNowDateTime()
|
|
|
|
};
|
|
|
|
|
|
|
|
sql.upsert("blobs", "blobId", pojo);
|
|
|
|
|
2023-07-28 05:22:08 +08:00
|
|
|
// we can't reuse blobId as an entity_changes hash, because this one has to be calculatable without having
|
|
|
|
// access to the decrypted content
|
|
|
|
const hash = blobService.calculateContentHash(pojo);
|
2023-06-30 02:54:58 +08:00
|
|
|
|
2023-08-10 19:40:26 +08:00
|
|
|
entityChangesService.putEntityChange({
|
2023-06-30 02:54:58 +08:00
|
|
|
entityName: 'blobs',
|
|
|
|
entityId: newBlobId,
|
|
|
|
hash: hash,
|
|
|
|
isErased: false,
|
|
|
|
utcDateChanged: pojo.utcDateModified,
|
|
|
|
isSynced: true,
|
|
|
|
// overriding componentId will cause frontend to think the change is coming from a different component
|
|
|
|
// and thus reload
|
|
|
|
componentId: opts.forceFrontendReload ? utils.randomString(10) : null
|
|
|
|
});
|
|
|
|
|
|
|
|
eventService.emit(eventService.ENTITY_CHANGED, {
|
|
|
|
entityName: 'blobs',
|
|
|
|
entity: this
|
|
|
|
});
|
|
|
|
|
|
|
|
return newBlobId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @protected
|
|
|
|
* @returns {string|Buffer}
|
|
|
|
*/
|
|
|
|
_getContent() {
|
|
|
|
const row = sql.getRow(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
throw new Error(`Cannot find content for ${this.constructor.primaryKeyName} '${this[this.constructor.primaryKeyName]}', blobId '${this.blobId}'`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobService.processContent(row.content, this.isProtected, this.hasStringContent());
|
|
|
|
}
|
|
|
|
|
2023-02-17 23:30:14 +08:00
|
|
|
/**
|
|
|
|
* Mark the entity as (soft) deleted. It will be completely erased later.
|
|
|
|
*
|
2023-06-30 02:54:58 +08:00
|
|
|
* This is a low-level method, for notes and branches use `note.deleteNote()` and 'branch.deleteBranch()` instead.
|
2023-02-17 23:30:14 +08:00
|
|
|
*
|
|
|
|
* @param [deleteId=null]
|
|
|
|
*/
|
|
|
|
markAsDeleted(deleteId = null) {
|
|
|
|
const entityId = this[this.constructor.primaryKeyName];
|
|
|
|
const entityName = this.constructor.entityName;
|
|
|
|
|
|
|
|
this.utcDateModified = dateUtils.utcNowDateTime();
|
|
|
|
|
|
|
|
sql.execute(`UPDATE ${entityName} SET isDeleted = 1, deleteId = ?, utcDateModified = ?
|
|
|
|
WHERE ${this.constructor.primaryKeyName} = ?`,
|
|
|
|
[deleteId, this.utcDateModified, entityId]);
|
|
|
|
|
|
|
|
if (this.dateModified) {
|
|
|
|
this.dateModified = dateUtils.localNowDateTime();
|
|
|
|
|
|
|
|
sql.execute(`UPDATE ${entityName} SET dateModified = ? WHERE ${this.constructor.primaryKeyName} = ?`,
|
|
|
|
[this.dateModified, entityId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info(`Marking ${entityName} ${entityId} as deleted`);
|
|
|
|
|
2023-08-10 19:40:26 +08:00
|
|
|
this.putEntityChange(true);
|
2023-02-17 23:30:14 +08:00
|
|
|
|
|
|
|
eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this });
|
|
|
|
}
|
|
|
|
|
|
|
|
markAsDeletedSimple() {
|
|
|
|
const entityId = this[this.constructor.primaryKeyName];
|
|
|
|
const entityName = this.constructor.entityName;
|
|
|
|
|
|
|
|
this.utcDateModified = dateUtils.utcNowDateTime();
|
|
|
|
|
|
|
|
sql.execute(`UPDATE ${entityName} SET isDeleted = 1, utcDateModified = ?
|
|
|
|
WHERE ${this.constructor.primaryKeyName} = ?`,
|
|
|
|
[this.utcDateModified, entityId]);
|
|
|
|
|
|
|
|
log.info(`Marking ${entityName} ${entityId} as deleted`);
|
|
|
|
|
2023-08-10 19:40:26 +08:00
|
|
|
this.putEntityChange(true);
|
2023-02-17 23:30:14 +08:00
|
|
|
|
|
|
|
eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AbstractBeccaEntity;
|
|
|
|
</code></pre>
|
|
|
|
</article>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<nav>
|
2023-08-21 16:17:16 +08:00
|
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-sql.html">sql</a></li></ul><h3>Classes</h3><ul><li><a href="AbstractBeccaEntity.html">AbstractBeccaEntity</a></li><li><a href="BAttachment.html">BAttachment</a></li><li><a href="BAttribute.html">BAttribute</a></li><li><a href="BBranch.html">BBranch</a></li><li><a href="BEtapiToken.html">BEtapiToken</a></li><li><a href="BNote.html">BNote</a></li><li><a href="BOption.html">BOption</a></li><li><a href="BRecentNote.html">BRecentNote</a></li><li><a href="BRevision.html">BRevision</a></li><li><a href="BackendScriptApi.html">BackendScriptApi</a></li></ul><h3>Global</h3><ul><li><a href="global.html#api">api</a></li></ul>
|
2023-02-17 23:30:14 +08:00
|
|
|
</nav>
|
|
|
|
|
|
|
|
<br class="clear">
|
|
|
|
|
|
|
|
<footer>
|
2023-05-31 20:00:37 +08:00
|
|
|
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a>
|
2023-02-17 23:30:14 +08:00
|
|
|
</footer>
|
|
|
|
|
|
|
|
<script> prettyPrint(); </script>
|
|
|
|
<script src="scripts/linenumber.js"> </script>
|
|
|
|
</body>
|
|
|
|
</html>
|