Notes/src/entities/note.js

941 lines
31 KiB
JavaScript
Raw Normal View History

2018-01-28 23:16:50 -05:00
"use strict";
const Entity = require('./entity');
2018-08-13 10:59:31 +02:00
const Attribute = require('./attribute');
const protectedSessionService = require('../services/protected_session');
const sql = require('../services/sql');
const utils = require('../services/utils');
2018-04-02 20:46:46 -04:00
const dateUtils = require('../services/date_utils');
const entityChangesService = require('../services/entity_changes.js');
2018-01-28 23:16:50 -05:00
const LABEL = 'label';
const LABEL_DEFINITION = 'label-definition';
const RELATION = 'relation';
const RELATION_DEFINITION = 'relation-definition';
2018-08-22 23:37:06 +02:00
/**
* This represents a Note which is a central object in the Trilium Notes project.
*
* @property {string} noteId - primary key
* @property {string} type - one of "text", "code", "file" or "render"
* @property {string} mime - MIME type, e.g. "text/html"
* @property {string} title - note title
* @property {boolean} isProtected - true if note is protected
* @property {boolean} isDeleted - true if note is deleted
2020-01-03 10:48:36 +01:00
* @property {string|null} deleteId - ID identifying delete transaction
2019-11-01 22:09:51 +01:00
* @property {boolean} isErased - true if note's content is erased after it has been deleted
* @property {string} dateCreated - local date time (with offset)
* @property {string} dateModified - local date time (with offset)
2019-03-12 20:58:31 +01:00
* @property {string} utcDateCreated
* @property {string} utcDateModified
2018-08-22 23:37:06 +02:00
*
* @extends Entity
*/
class Note extends Entity {
static get entityName() { return "notes"; }
2018-01-30 20:12:19 -05:00
static get primaryKeyName() { return "noteId"; }
2020-02-18 22:16:20 +01:00
static get hashedProperties() { return ["noteId", "title", "type", "mime", "isProtected", "isDeleted", "deleteId"]; }
2018-01-29 23:35:36 -05:00
2018-08-22 23:37:06 +02:00
/**
* @param row - object containing database row from "notes" table
*/
constructor(row) {
super(row);
2018-01-29 20:57:55 -05:00
this.isProtected = !!this.isProtected;
/* true if content is either not encrypted
* or encrypted, but with available protected session (so effectively decrypted) */
this.isContentAvailable = true;
// check if there's noteId, otherwise this is a new entity which wasn't encrypted yet
if (this.isProtected && this.noteId) {
this.isContentAvailable = protectedSessionService.isProtectedSessionAvailable();
if (this.isContentAvailable) {
2019-11-02 07:50:23 +01:00
this.title = protectedSessionService.decryptString(this.title);
}
else {
this.title = "[protected]";
}
2018-01-29 23:35:36 -05:00
}
2018-01-29 20:57:55 -05:00
}
/*
* Note content has quite special handling - it's not a separate entity, but a lazily loaded
* part of Note entity with it's own sync. Reasons behind this hybrid design has been:
*
* - 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
* - changes in the note metadata or title should not trigger note content sync (so we keep separate utcDateModified and entity changes records)
* - but to the user note content and title changes are one and the same - single dateModified (so all changes must go through Note and content is not a separate entity)
*/
/** @returns {*} */
2020-06-20 12:31:38 +02:00
getContent(silentNotFoundError = false) {
if (this.content === undefined) {
2020-06-20 12:31:38 +02:00
const res = sql.getRow(`SELECT content, hash FROM note_contents WHERE noteId = ?`, [this.noteId]);
2019-04-13 19:34:19 +02:00
if (!res) {
if (silentNotFoundError) {
return undefined;
}
else {
throw new Error("Cannot find note content for noteId=" + this.noteId);
}
}
this.content = res.content;
2019-02-08 21:01:26 +01:00
if (this.isProtected) {
if (this.isContentAvailable) {
this.content = this.content === null ? null : protectedSessionService.decrypt(this.content);
}
else {
this.content = "";
}
2019-02-08 21:01:26 +01:00
}
2018-04-08 08:31:19 -04:00
}
2020-03-26 16:59:40 +01:00
if (this.isStringNote()) {
return this.content === null
? ""
: this.content.toString("UTF-8");
}
else {
return this.content;
}
2019-02-06 21:29:23 +01:00
}
getContentMetadata() {
return sql.getRow(`
SELECT
LENGTH(content) AS contentLength,
dateModified,
utcDateModified
FROM note_contents
WHERE noteId = ?`, [this.noteId]);
}
/** @returns {*} */
2020-06-20 12:31:38 +02:00
getJsonContent() {
const content = this.getContent();
2019-02-07 22:16:40 +01:00
2020-01-13 19:35:06 +01:00
if (!content || !content.trim()) {
return null;
}
2019-02-07 22:16:40 +01:00
return JSON.parse(content);
}
2020-06-20 12:31:38 +02:00
setContent(content) {
2019-11-16 17:00:22 +01:00
if (content === null || content === undefined) {
throw new Error(`Cannot set null content to note ${this.noteId}`);
}
if (this.isStringNote()) {
content = content.toString();
}
else {
content = Buffer.isBuffer(content) ? content : Buffer.from(content);
}
2020-01-19 09:01:51 +01:00
this.content = content;
const pojo = {
noteId: this.noteId,
content: content,
dateModified: dateUtils.localNowDateTime(),
utcDateModified: dateUtils.utcNowDateTime(),
2020-01-19 09:01:51 +01:00
hash: utils.hash(this.noteId + "|" + content.toString())
};
if (this.isProtected) {
if (this.isContentAvailable) {
pojo.content = protectedSessionService.encrypt(pojo.content);
}
else {
throw new Error(`Cannot update content of noteId=${this.noteId} since we're out of protected session.`);
}
2019-02-08 21:01:26 +01:00
}
2020-06-20 12:31:38 +02:00
sql.upsert("note_contents", "noteId", pojo);
entityChangesService.addNoteContentEntityChange(this.noteId);
2019-02-08 21:01:26 +01:00
}
2020-06-20 12:31:38 +02:00
setJsonContent(content) {
this.setContent(JSON.stringify(content, null, '\t'));
2019-02-08 21:01:26 +01:00
}
2018-08-22 23:37:06 +02:00
/** @returns {boolean} true if this note is the root of the note tree. Root note has "root" noteId */
isRoot() {
return this.noteId === 'root';
}
2018-08-22 23:37:06 +02:00
/** @returns {boolean} true if this note is of application/json content type */
isJson() {
2018-03-25 23:25:17 -04:00
return this.mime === "application/json";
}
2018-08-22 23:37:06 +02:00
/** @returns {boolean} true if this note is JavaScript (code or attachment) */
isJavaScript() {
return (this.type === "code" || this.type === "file")
2018-11-30 22:28:30 +01:00
&& (this.mime.startsWith("application/javascript")
|| this.mime === "application/x-javascript"
|| this.mime === "text/javascript");
}
2018-08-22 23:37:06 +02:00
/** @returns {boolean} true if this note is HTML */
2018-03-04 21:05:14 -05:00
isHtml() {
return (this.type === "code" || this.type === "file" || this.type === "render") && this.mime === "text/html";
2018-03-04 21:05:14 -05:00
}
/** @returns {boolean} true if the note has string content (not binary) */
isStringNote() {
return utils.isStringNote(this.type, this.mime);
}
2018-08-22 23:37:06 +02:00
/** @returns {string} JS script environment - either "frontend" or "backend" */
getScriptEnv() {
if (this.isHtml() || (this.isJavaScript() && this.mime.endsWith('env=frontend'))) {
return "frontend";
}
2018-03-07 00:17:18 -05:00
if (this.type === 'render') {
return "frontend";
}
if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
return "backend";
}
return null;
}
2020-06-20 12:31:38 +02:00
loadOwnedAttributesToCache() {
this.__ownedAttributeCache = this.repository.getEntities(`SELECT * FROM attributes WHERE isDeleted = 0 AND noteId = ?`, [this.noteId]);
return this.__ownedAttributeCache;
}
2018-08-22 23:37:06 +02:00
/**
* This method is a faster variant of getAttributes() which looks for only owned attributes.
* Use when inheritance is not needed and/or in batch/performance sensitive operations.
*
* @param {string} [type] - (optional) attribute type to filter
* @param {string} [name] - (optional) attribute name to filter
* @returns {Attribute[]} note's "owned" attributes - excluding inherited ones
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getOwnedAttributes(type, name) {
if (!this.__ownedAttributeCache) {
2020-06-20 12:31:38 +02:00
this.loadOwnedAttributesToCache();
}
if (type && name) {
return this.__ownedAttributeCache.filter(attr => attr.type === type && attr.name === name);
}
else if (type) {
return this.__ownedAttributeCache.filter(attr => attr.type === type);
}
else if (name) {
return this.__ownedAttributeCache.filter(attr => attr.name === name);
}
else {
return this.__ownedAttributeCache.slice();
}
}
/**
* @returns {Attribute} attribute belonging to this specific note (excludes inherited attributes)
*
* This method can be significantly faster than the getAttribute()
*/
2020-06-20 12:31:38 +02:00
getOwnedAttribute(type, name) {
const attrs = this.getOwnedAttributes(type, name);
return attrs.length > 0 ? attrs[0] : null;
2018-01-28 23:16:50 -05:00
}
/**
* @returns {Attribute[]} relations targetting this specific note
*/
2020-06-20 12:31:38 +02:00
getTargetRelations() {
return this.repository.getEntities("SELECT * FROM attributes WHERE type = 'relation' AND isDeleted = 0 AND value = ?", [this.noteId]);
}
/**
* @param {string} [type] - (optional) attribute type to filter
* @param {string} [name] - (optional) attribute name to filter
* @returns {Attribute[]} all note's attributes, including inherited ones
*/
2020-06-20 12:31:38 +02:00
getAttributes(type, name) {
if (!this.__attributeCache) {
2020-06-20 12:31:38 +02:00
this.loadAttributesToCache();
}
if (type && name) {
return this.__attributeCache.filter(attr => attr.type === type && attr.name === name);
}
else if (type) {
return this.__attributeCache.filter(attr => attr.type === type);
}
else if (name) {
return this.__attributeCache.filter(attr => attr.name === name);
}
else {
return this.__attributeCache.slice();
}
}
/**
* @param {string} [name] - label name to filter
* @returns {Attribute[]} all note's labels (attributes with type label), including inherited ones
*/
2020-06-20 12:31:38 +02:00
getLabels(name) {
return this.getAttributes(LABEL, name);
}
/**
* @param {string} [name] - label name to filter
* @returns {Attribute[]} all note's labels (attributes with type label), excluding inherited ones
*/
2020-06-20 12:31:38 +02:00
getOwnedLabels(name) {
return this.getOwnedAttributes(LABEL, name);
}
/**
* @param {string} [name] - label name to filter
* @returns {Attribute[]} all note's label definitions, including inherited ones
*/
2020-06-20 12:31:38 +02:00
getLabelDefinitions(name) {
return this.getAttributes(LABEL_DEFINITION, name);
}
/**
* @param {string} [name] - relation name to filter
* @returns {Attribute[]} all note's relations (attributes with type relation), including inherited ones
*/
2020-06-20 12:31:38 +02:00
getRelations(name) {
return this.getAttributes(RELATION, name);
}
2019-08-17 11:28:36 +02:00
/**
* @param {string} [name] - relation name to filter
* @returns {Attribute[]} all note's relations (attributes with type relation), excluding inherited ones
*/
2020-06-20 12:31:38 +02:00
getOwnedRelations(name) {
return this.getOwnedAttributes(RELATION, name);
}
/**
* @param {string} [name] - relation name to filter
* @returns {Note[]}
2019-08-17 11:28:36 +02:00
*/
2020-06-20 12:31:38 +02:00
getRelationTargets(name) {
const relations = this.getRelations(name);
2019-08-17 11:28:36 +02:00
const targets = [];
for (const relation of relations) {
2020-06-20 12:31:38 +02:00
targets.push(relation.getTargetNote());
2019-08-17 11:28:36 +02:00
}
return targets;
}
/**
* @param {string} [name] - relation name to filter
* @returns {Attribute[]} all note's relation definitions including inherited ones
*/
2020-06-20 12:31:38 +02:00
getRelationDefinitions(name) {
return this.getAttributes(RELATION_DEFINITION, name);
}
2018-08-22 23:37:06 +02:00
/**
* Clear note's attributes cache to force fresh reload for next attribute request.
* Cache is note instance scoped.
*/
invalidateAttributeCache() {
this.__attributeCache = null;
this.__ownedAttributeCache = null;
}
2020-06-20 12:31:38 +02:00
loadAttributesToCache() {
const attributes = this.repository.getEntities(`
WITH RECURSIVE
tree(noteId, level) AS (
SELECT ?, 0
UNION
SELECT branches.parentNoteId, tree.level + 1
FROM branches
JOIN tree ON branches.noteId = tree.noteId
WHERE branches.isDeleted = 0
),
treeWithAttrs(noteId, level) AS (
SELECT * FROM tree
UNION
SELECT attributes.value, treeWithAttrs.level FROM attributes
JOIN treeWithAttrs ON treeWithAttrs.noteId = attributes.noteId
WHERE attributes.isDeleted = 0
AND attributes.type = 'relation'
2018-08-21 12:52:11 +02:00
AND attributes.name = 'template'
AND (treeWithAttrs.level = 0 OR attributes.isInheritable = 1)
)
SELECT attributes.* FROM attributes JOIN treeWithAttrs ON attributes.noteId = treeWithAttrs.noteId
WHERE attributes.isDeleted = 0 AND (attributes.isInheritable = 1 OR treeWithAttrs.level = 0)
ORDER BY level, noteId, position`, [this.noteId]);
// attributes are ordered so that "closest" attributes are first
// we order by noteId so that attributes from same note stay together. Actual noteId ordering doesn't matter.
const filteredAttributes = attributes.filter((attr, index) => {
// if this exact attribute already appears then don't include it (can happen via cloning)
if (attributes.findIndex(it => it.attributeId === attr.attributeId) !== index) {
return false;
}
if (attr.isDefinition()) {
const firstDefinitionIndex = attributes.findIndex(el => el.type === attr.type && el.name === attr.name);
// keep only if this element is the first definition for this type & name
return firstDefinitionIndex === index;
}
else {
const definitionAttr = attributes.find(el => el.type === attr.type + '-definition' && el.name === attr.name);
if (!definitionAttr) {
return true;
}
const definition = definitionAttr.value;
if (definition.multiplicityType === 'multi') {
return true;
}
else {
const firstAttrIndex = attributes.findIndex(el => el.type === attr.type && el.name === attr.name);
// in case of single-valued attribute we'll keep it only if it's first (closest)
return firstAttrIndex === index;
}
}
});
this.__attributeCache = filteredAttributes;
2018-03-03 09:11:41 -05:00
}
2018-08-22 23:37:06 +02:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {boolean} true if note has an attribute with given type and name (including inherited)
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
hasAttribute(type, name) {
return !!this.getAttribute(type, name);
2018-03-04 22:09:51 -05:00
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {boolean} true if note has an attribute with given type and name (excluding inherited)
*/
2020-06-20 12:31:38 +02:00
hasOwnedAttribute(type, name) {
return !!this.getOwnedAttribute(type, name);
}
2018-08-22 23:37:06 +02:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {Attribute} attribute of given type and name. If there's more such attributes, first is returned. Returns null if there's no such attribute belonging to this note.
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getAttribute(type, name) {
const attributes = this.getAttributes();
return attributes.find(attr => attr.type === type && attr.name === name);
}
2018-08-22 23:37:06 +02:00
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {string|null} attribute value of given type and name or null if no such attribute exists.
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getAttributeValue(type, name) {
const attr = this.getAttribute(type, name);
2018-08-13 10:59:31 +02:00
return attr ? attr.value : null;
2018-08-13 10:59:31 +02:00
}
/**
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @returns {string|null} attribute value of given type and name or null if no such attribute exists.
*/
2020-06-20 12:31:38 +02:00
getOwnedAttributeValue(type, name) {
const attr = this.getOwnedAttribute(type, name);
return attr ? attr.value : null;
}
2018-08-22 23:37:06 +02:00
/**
* Based on enabled, attribute is either set or removed.
*
* @param {string} type - attribute type ('relation', 'label' etc.)
* @param {boolean} enabled - toggle On or Off
* @param {string} name - attribute name
* @param {string} [value] - attribute value (optional)
*/
2020-06-20 12:31:38 +02:00
toggleAttribute(type, enabled, name, value) {
if (enabled) {
2020-06-20 12:31:38 +02:00
this.setAttribute(type, name, value);
}
else {
2020-06-20 12:31:38 +02:00
this.removeAttribute(type, name, value);
}
}
2018-08-22 23:37:06 +02:00
/**
2019-11-08 22:34:30 +01:00
* Update's given attribute's value or creates it if it doesn't exist
2018-08-22 23:37:06 +02:00
*
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @param {string} [value] - attribute value (optional)
*/
2020-06-20 12:31:38 +02:00
setAttribute(type, name, value) {
const attributes = this.loadOwnedAttributesToCache();
2019-11-08 22:34:30 +01:00
let attr = attributes.find(attr => attr.type === type && attr.name === name);
2018-08-13 10:59:31 +02:00
2019-11-08 22:34:30 +01:00
if (attr) {
if (attr.value !== value) {
attr.value = value;
2020-06-20 12:31:38 +02:00
attr.save();
2019-11-08 22:34:30 +01:00
this.invalidateAttributeCache();
}
}
else {
attr = new Attribute({
2018-08-13 10:59:31 +02:00
noteId: this.noteId,
type: type,
name: name,
2018-08-22 23:37:06 +02:00
value: value !== undefined ? value : ""
2018-08-13 10:59:31 +02:00
});
2020-06-20 12:31:38 +02:00
attr.save();
this.invalidateAttributeCache();
}
2018-08-13 10:59:31 +02:00
}
2018-08-22 23:37:06 +02:00
/**
* Removes given attribute name-value pair if it exists.
*
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @param {string} [value] - attribute value (optional)
*/
2020-06-20 12:31:38 +02:00
removeAttribute(type, name, value) {
const attributes = this.loadOwnedAttributesToCache();
2018-08-13 10:59:31 +02:00
for (const attribute of attributes) {
if (attribute.type === type && attribute.name === name && (value === undefined || value === attribute.value)) {
attribute.isDeleted = true;
2020-06-20 12:31:38 +02:00
attribute.save();
this.invalidateAttributeCache();
}
2018-08-13 10:59:31 +02:00
}
}
2019-11-14 23:10:56 +01:00
/**
* @return {Attribute}
2019-11-14 23:10:56 +01:00
*/
2020-06-20 12:31:38 +02:00
addAttribute(type, name, value = "", isInheritable = false, position = 1000) {
2019-11-14 23:10:56 +01:00
const attr = new Attribute({
noteId: this.noteId,
type: type,
name: name,
2020-06-02 23:13:55 +02:00
value: value,
isInheritable: isInheritable,
position: position
2019-11-14 23:10:56 +01:00
});
2020-06-20 12:31:38 +02:00
attr.save();
2019-11-14 23:10:56 +01:00
this.invalidateAttributeCache();
return attr;
}
2020-06-20 12:31:38 +02:00
addLabel(name, value = "", isInheritable = false) {
return this.addAttribute(LABEL, name, value, isInheritable);
2019-11-14 23:10:56 +01:00
}
2020-06-20 12:31:38 +02:00
addRelation(name, targetNoteId, isInheritable = false) {
return this.addAttribute(RELATION, name, targetNoteId, isInheritable);
2019-11-14 23:10:56 +01:00
}
2018-08-22 23:37:06 +02:00
/**
* @param {string} name - label name
* @returns {boolean} true if label exists (including inherited)
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
hasLabel(name) { return this.hasAttribute(LABEL, name); }
2018-08-22 23:37:06 +02:00
/**
* @param {string} name - label name
* @returns {boolean} true if label exists (excluding inherited)
*/
2020-06-20 12:31:38 +02:00
hasOwnedLabel(name) { return this.hasOwnedAttribute(LABEL, name); }
2018-08-22 23:37:06 +02:00
/**
* @param {string} name - relation name
* @returns {boolean} true if relation exists (including inherited)
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
hasRelation(name) { return this.hasAttribute(RELATION, name); }
/**
* @param {string} name - relation name
* @returns {boolean} true if relation exists (excluding inherited)
*/
2020-06-20 12:31:38 +02:00
hasOwnedRelation(name) { return this.hasOwnedAttribute(RELATION, name); }
2018-08-22 23:37:06 +02:00
/**
* @param {string} name - label name
* @returns {Attribute|null} label if it exists, null otherwise
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getLabel(name) { return this.getAttribute(LABEL, name); }
2018-08-22 23:37:06 +02:00
/**
* @param {string} name - label name
* @returns {Attribute|null} label if it exists, null otherwise
*/
2020-06-20 12:31:38 +02:00
getOwnedLabel(name) { return this.getOwnedAttribute(LABEL, name); }
2018-08-22 23:37:06 +02:00
/**
* @param {string} name - relation name
* @returns {Attribute|null} relation if it exists, null otherwise
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getRelation(name) { return this.getAttribute(RELATION, name); }
/**
* @param {string} name - relation name
* @returns {Attribute|null} relation if it exists, null otherwise
*/
2020-06-20 12:31:38 +02:00
getOwnedRelation(name) { return this.getOwnedAttribute(RELATION, name); }
2018-08-22 23:37:06 +02:00
/**
* @param {string} name - label name
* @returns {string|null} label value if label exists, null otherwise
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getLabelValue(name) { return this.getAttributeValue(LABEL, name); }
/**
* @param {string} name - label name
* @returns {string|null} label value if label exists, null otherwise
*/
2020-06-20 12:31:38 +02:00
getOwnedLabelValue(name) { return this.getOwnedAttributeValue(LABEL, name); }
2018-08-22 23:37:06 +02:00
/**
* @param {string} name - relation name
* @returns {string|null} relation value if relation exists, null otherwise
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getRelationValue(name) { return this.getAttributeValue(RELATION, name); }
/**
* @param {string} name - relation name
* @returns {string|null} relation value if relation exists, null otherwise
*/
2020-06-20 12:31:38 +02:00
getOwnedRelationValue(name) { return this.getOwnedAttributeValue(RELATION, name); }
/**
* @param {string} name
* @returns {Note|null} target note of the relation or null (if target is empty or note was not found)
*/
2020-06-20 12:31:38 +02:00
getRelationTarget(name) {
const relation = this.getRelation(name);
2020-06-20 12:31:38 +02:00
return relation ? this.repository.getNote(relation.value) : null;
}
/**
* @param {string} name
* @returns {Note|null} target note of the relation or null (if target is empty or note was not found)
*/
2020-06-20 12:31:38 +02:00
getOwnedRelationTarget(name) {
const relation = this.getOwnedRelation(name);
2020-06-20 12:31:38 +02:00
return relation ? this.repository.getNote(relation.value) : null;
}
2018-08-22 23:37:06 +02:00
/**
* Based on enabled, label is either set or removed.
*
* @param {boolean} enabled - toggle On or Off
* @param {string} name - label name
* @param {string} [value] - label value (optional)
*/
2020-06-20 12:31:38 +02:00
toggleLabel(enabled, name, value) { return this.toggleAttribute(LABEL, enabled, name, value); }
2018-08-22 23:37:06 +02:00
/**
* Based on enabled, relation is either set or removed.
*
* @param {boolean} enabled - toggle On or Off
* @param {string} name - relation name
* @param {string} [value] - relation value (noteId)
*/
2020-06-20 12:31:38 +02:00
toggleRelation(enabled, name, value) { return this.toggleAttribute(RELATION, enabled, name, value); }
2018-08-22 23:37:06 +02:00
/**
2019-11-08 22:34:30 +01:00
* Update's given label's value or creates it if it doesn't exist
2018-08-22 23:37:06 +02:00
*
* @param {string} name - label name
* @param {string} [value] - label value
*/
2020-06-20 12:31:38 +02:00
setLabel(name, value) { return this.setAttribute(LABEL, name, value); }
2018-08-22 23:37:06 +02:00
/**
2019-11-08 22:34:30 +01:00
* Update's given relation's value or creates it if it doesn't exist
2018-08-22 23:37:06 +02:00
*
* @param {string} name - relation name
* @param {string} [value] - relation value (noteId)
*/
2020-06-20 12:31:38 +02:00
setRelation(name, value) { return this.setAttribute(RELATION, name, value); }
2018-08-22 23:37:06 +02:00
/**
* Remove label name-value pair, if it exists.
*
* @param {string} name - label name
* @param {string} [value] - label value
*/
2020-06-20 12:31:38 +02:00
removeLabel(name, value) { return this.removeAttribute(LABEL, name, value); }
2018-08-22 23:37:06 +02:00
/**
* Remove relation name-value pair, if it exists.
*
* @param {string} name - relation name
* @param {string} [value] - relation value (noteId)
*/
2020-06-20 12:31:38 +02:00
removeRelation(name, value) { return this.removeAttribute(RELATION, name, value); }
2018-08-22 23:37:06 +02:00
/**
* @return {string[]} return list of all descendant noteIds of this note. Returning just noteIds because number of notes can be huge. Includes also this note's noteId
*/
2020-06-20 12:31:38 +02:00
getDescendantNoteIds() {
return sql.getColumn(`
WITH RECURSIVE
tree(noteId) AS (
SELECT ?
UNION
SELECT branches.noteId FROM branches
JOIN tree ON branches.parentNoteId = tree.noteId
JOIN notes ON notes.noteId = branches.noteId
WHERE notes.isDeleted = 0
AND branches.isDeleted = 0
)
SELECT noteId FROM tree`, [this.noteId]);
}
/**
* Finds descendant notes with given attribute name and value. Only own attributes are considered, not inherited ones
2018-08-22 23:37:06 +02:00
*
* @param {string} type - attribute type (label, relation, etc.)
* @param {string} name - attribute name
* @param {string} [value] - attribute value
* @returns {Note[]}
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getDescendantNotesWithAttribute(type, name, value) {
2018-08-21 12:50:43 +02:00
const params = [this.noteId, name];
let valueCondition = "";
if (value !== undefined) {
params.push(value);
valueCondition = " AND attributes.value = ?";
}
2020-06-20 12:31:38 +02:00
const notes = this.repository.getEntities(`
2018-08-21 12:50:43 +02:00
WITH RECURSIVE
tree(noteId) AS (
SELECT ?
UNION
SELECT branches.noteId FROM branches
JOIN tree ON branches.parentNoteId = tree.noteId
JOIN notes ON notes.noteId = branches.noteId
WHERE notes.isDeleted = 0
AND branches.isDeleted = 0
)
SELECT notes.* FROM notes
JOIN tree ON tree.noteId = notes.noteId
JOIN attributes ON attributes.noteId = notes.noteId
WHERE attributes.isDeleted = 0
AND attributes.name = ?
${valueCondition}
ORDER BY noteId, position`, params);
return notes;
}
2018-08-22 23:37:06 +02:00
/**
* Finds descendant notes with given label name and value. Only own labels are considered, not inherited ones
2018-08-22 23:37:06 +02:00
*
* @param {string} name - label name
* @param {string} [value] - label value
* @returns {Note[]}
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getDescendantNotesWithLabel(name, value) { return this.getDescendantNotesWithAttribute(LABEL, name, value); }
2018-08-22 23:37:06 +02:00
/**
* Finds descendant notes with given relation name and value. Only own relations are considered, not inherited ones
2018-08-22 23:37:06 +02:00
*
* @param {string} name - relation name
* @param {string} [value] - relation value
* @returns {Note[]}
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getDescendantNotesWithRelation(name, value) { return this.getDescendantNotesWithAttribute(RELATION, name, value); }
2018-08-21 12:50:43 +02:00
2018-08-22 23:37:06 +02:00
/**
* Returns note revisions of this note.
*
* @returns {NoteRevision[]}
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getRevisions() {
return this.repository.getEntities("SELECT * FROM note_revisions WHERE noteId = ?", [this.noteId]);
}
2018-08-22 23:37:06 +02:00
/**
* Get list of links coming out of this note.
*
2019-08-19 20:12:00 +02:00
* @deprecated - not intended for general use
* @returns {Attribute[]}
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getLinks() {
return this.repository.getEntities(`
2019-08-19 20:12:00 +02:00
SELECT *
FROM attributes
WHERE noteId = ? AND
isDeleted = 0 AND
type = 'relation' AND
name IN ('internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink')`, [this.noteId]);
2018-03-31 22:15:06 -04:00
}
2018-08-22 23:37:06 +02:00
/**
* @returns {Branch[]}
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getBranches() {
return this.repository.getEntities("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ?", [this.noteId]);
2018-01-28 23:16:50 -05:00
}
2018-01-29 23:35:36 -05:00
2018-09-03 09:40:22 +02:00
/**
* @returns {boolean} - true if note has children
*/
2020-06-20 12:31:38 +02:00
hasChildren() {
return (this.getChildNotes()).length > 0;
2018-09-03 09:40:22 +02:00
}
2018-08-22 23:37:06 +02:00
/**
* @returns {Note[]} child notes of this note
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getChildNotes() {
return this.repository.getEntities(`
SELECT notes.*
2018-03-24 21:39:15 -04:00
FROM branches
JOIN notes USING(noteId)
WHERE notes.isDeleted = 0
2018-03-24 21:39:15 -04:00
AND branches.isDeleted = 0
AND branches.parentNoteId = ?
ORDER BY branches.notePosition`, [this.noteId]);
}
2018-08-22 23:37:06 +02:00
/**
* @returns {Branch[]} child branches of this note
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getChildBranches() {
return this.repository.getEntities(`
2018-03-31 23:08:22 -04:00
SELECT branches.*
FROM branches
WHERE branches.isDeleted = 0
AND branches.parentNoteId = ?
ORDER BY branches.notePosition`, [this.noteId]);
}
2018-08-22 23:37:06 +02:00
/**
* @returns {Note[]} parent notes of this note (note can have multiple parents because of cloning)
2018-08-22 23:37:06 +02:00
*/
2020-06-20 12:31:38 +02:00
getParentNotes() {
return this.repository.getEntities(`
SELECT parent_notes.*
FROM
2018-03-24 21:39:15 -04:00
branches AS child_tree
JOIN notes AS parent_notes ON parent_notes.noteId = child_tree.parentNoteId
WHERE child_tree.noteId = ?
AND child_tree.isDeleted = 0
AND parent_notes.isDeleted = 0`, [this.noteId]);
}
2019-11-16 19:07:32 +01:00
/**
* @return {string[][]} - array of notePaths (each represented by array of noteIds constituting the particular note path)
2019-11-16 19:07:32 +01:00
*/
2020-06-20 12:31:38 +02:00
getAllNotePaths() {
2019-11-16 19:07:32 +01:00
if (this.noteId === 'root') {
return [['root']];
}
const notePaths = [];
2020-06-20 12:31:38 +02:00
for (const parentNote of this.getParentNotes()) {
for (const parentPath of parentNote.getAllNotePaths()) {
2019-11-16 19:07:32 +01:00
parentPath.push(this.noteId);
notePaths.push(parentPath);
}
}
return notePaths;
}
2019-11-27 23:07:10 +01:00
/**
* @param ancestorNoteId
* @return {boolean} - true if ancestorNoteId occurs in at least one of the note's paths
2019-11-27 23:07:10 +01:00
*/
2020-06-20 12:31:38 +02:00
isDescendantOfNote(ancestorNoteId) {
const notePaths = this.getAllNotePaths();
2019-11-27 23:07:10 +01:00
return notePaths.some(path => path.includes(ancestorNoteId));
}
2018-01-29 23:35:36 -05:00
beforeSaving() {
2018-04-01 17:38:24 -04:00
if (!this.isDeleted) {
this.isDeleted = false;
}
if (!this.dateCreated) {
this.dateCreated = dateUtils.localNowDateTime();
}
2019-03-12 20:58:31 +01:00
if (!this.utcDateCreated) {
this.utcDateCreated = dateUtils.utcNowDateTime();
2018-03-31 22:15:06 -04:00
}
super.beforeSaving();
if (this.isChanged) {
this.dateModified = dateUtils.localNowDateTime();
this.utcDateModified = dateUtils.utcNowDateTime();
}
2018-01-29 23:35:36 -05:00
}
// cannot be static!
updatePojo(pojo) {
if (pojo.isProtected) {
if (this.isContentAvailable) {
pojo.title = protectedSessionService.encrypt(pojo.title);
}
else {
// updating protected note outside of protected session means we will keep original ciphertexts
delete pojo.title;
}
}
delete pojo.isContentAvailable;
delete pojo.__attributeCache;
2019-12-08 09:41:31 +01:00
delete pojo.__ownedAttributeCache;
delete pojo.content;
/** zero references to contentHash, probably can be removed */
2019-03-28 21:17:40 +01:00
delete pojo.contentHash;
}
2018-01-28 23:16:50 -05:00
}
module.exports = Note;