Notes/src/becca/becca_loader.js

260 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-05-20 00:03:33 +02:00
"use strict";
2021-08-07 21:21:30 +02:00
const sql = require('../services/sql');
const eventService = require('../services/events');
const becca = require('./becca');
2021-05-17 22:09:49 +02:00
const sqlInit = require('../services/sql_init');
const log = require('../services/log');
const BNote = require('./entities/bnote');
const BBranch = require('./entities/bbranch');
const BAttribute = require('./entities/battribute');
const BOption = require('./entities/boption');
const BEtapiToken = require("./entities/betapi_token");
2021-08-07 21:21:30 +02:00
const cls = require("../services/cls");
const entityConstructor = require("../becca/entity_constructor");
2020-05-20 00:03:33 +02:00
2021-05-01 21:52:22 +02:00
const beccaLoaded = new Promise((res, rej) => {
sqlInit.dbReady.then(() => {
load();
2021-05-17 22:09:49 +02:00
cls.init(() => require('../services/options_init').initStartupOptions());
2021-05-01 21:52:22 +02:00
res();
});
2020-06-20 21:42:41 +02:00
});
2020-06-20 12:31:38 +02:00
function load() {
const start = Date.now();
2021-04-16 23:00:08 +02:00
becca.reset();
2020-05-20 00:03:33 +02:00
// using raw query and passing arrays to avoid allocating new objects
// this is worth it for becca load since it happens every run and blocks the app until finished
2021-10-17 14:44:59 +02:00
for (const row of sql.getRawRows(`SELECT noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes WHERE isDeleted = 0`)) {
new BNote().update(row).init();
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
const branchRows = sql.getRawRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`);
// in-memory sort is faster than in the DB
branchRows.sort((a, b) => a.notePosition - b.notePosition);
for (const row of branchRows) {
new BBranch().update(row).init();
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
2021-10-17 14:44:59 +02:00
for (const row of sql.getRawRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`)) {
new BAttribute().update(row).init();
2021-04-30 23:10:25 +02:00
}
for (const row of sql.getRows(`SELECT name, value, isSynced, utcDateModified FROM options`)) {
new BOption(row);
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
2022-01-10 17:09:20 +01:00
for (const row of sql.getRows(`SELECT etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified FROM etapi_tokens WHERE isDeleted = 0`)) {
new BEtapiToken(row);
2022-01-10 17:09:20 +01:00
}
for (const noteId in becca.notes) {
becca.notes[noteId].sortParents();
}
2021-04-16 23:00:08 +02:00
becca.loaded = true;
2021-04-16 23:00:08 +02:00
log.info(`Becca (note cache) load took ${Date.now() - start}ms`);
2020-05-20 00:03:33 +02:00
}
function reload() {
load();
require('../services/ws').reloadFrontend();
}
2021-08-07 21:21:30 +02:00
function postProcessEntityUpdate(entityName, entity) {
2021-05-01 11:38:20 +02:00
if (entityName === 'branches') {
branchUpdated(entity);
} else if (entityName === 'attributes') {
attributeUpdated(entity);
} else if (entityName === 'note_reordering') {
noteReorderingUpdated(entity);
}
2021-08-07 21:21:30 +02:00
}
eventService.subscribeBeccaLoader([eventService.ENTITY_CHANGE_SYNCED], ({entityName, entityRow}) => {
2021-08-07 21:21:30 +02:00
if (!becca.loaded) {
return;
}
2022-01-10 17:09:20 +01:00
if (["notes", "branches", "attributes", "etapi_tokens"].includes(entityName)) {
2021-08-07 21:21:30 +02:00
const EntityClass = entityConstructor.getEntityFromEntityName(entityName);
const primaryKeyName = EntityClass.primaryKeyName;
2021-08-26 22:10:59 +02:00
let beccaEntity = becca.getEntity(entityName, entityRow[primaryKeyName]);
2021-08-07 21:21:30 +02:00
2021-08-14 10:13:59 +02:00
if (beccaEntity) {
2021-08-26 22:10:59 +02:00
beccaEntity.updateFromRow(entityRow);
2021-08-07 21:21:30 +02:00
} else {
2021-08-14 10:13:59 +02:00
beccaEntity = new EntityClass();
2021-08-26 22:10:59 +02:00
beccaEntity.updateFromRow(entityRow);
2021-08-14 10:13:59 +02:00
beccaEntity.init();
2021-08-07 21:21:30 +02:00
}
}
2021-08-26 22:10:59 +02:00
postProcessEntityUpdate(entityName, entityRow);
2021-08-07 21:21:30 +02:00
});
eventService.subscribeBeccaLoader(eventService.ENTITY_CHANGED, ({entityName, entity}) => {
2021-08-07 21:21:30 +02:00
if (!becca.loaded) {
return;
}
postProcessEntityUpdate(entityName, entity);
2021-05-01 11:38:20 +02:00
});
2020-05-20 00:03:33 +02:00
eventService.subscribeBeccaLoader([eventService.ENTITY_DELETED, eventService.ENTITY_DELETE_SYNCED], ({entityName, entityId}) => {
2021-05-01 11:38:20 +02:00
if (!becca.loaded) {
return;
2020-05-20 00:03:33 +02:00
}
2021-05-01 11:38:20 +02:00
if (entityName === 'notes') {
noteDeleted(entityId);
2021-05-01 11:38:20 +02:00
} else if (entityName === 'branches') {
branchDeleted(entityId);
2021-05-01 11:38:20 +02:00
} else if (entityName === 'attributes') {
attributeDeleted(entityId);
2022-01-10 17:09:20 +01:00
} else if (entityName === 'etapi_tokens') {
etapiTokenDeleted(entityId);
2021-05-01 11:38:20 +02:00
}
});
function noteDeleted(noteId) {
delete becca.notes[noteId];
becca.dirtyNoteSetCache();
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
function branchDeleted(branchId) {
const branch = becca.branches[branchId];
if (!branch) {
return;
}
2021-05-08 23:31:20 +02:00
const childNote = becca.notes[branch.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote) {
childNote.parents = childNote.parents.filter(parent => parent.noteId !== branch.parentNoteId);
childNote.parentBranches = childNote.parentBranches
.filter(parentBranch => parentBranch.branchId !== branch.branchId);
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote.parents.length > 0) {
childNote.invalidateSubTree();
2021-04-30 23:10:25 +02:00
}
2021-05-01 11:38:20 +02:00
}
2021-04-30 23:10:25 +02:00
2021-05-01 11:38:20 +02:00
const parentNote = becca.notes[branch.parentNoteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (parentNote) {
parentNote.children = parentNote.children.filter(child => child.noteId !== branch.noteId);
}
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
delete becca.childParentToBranch[`${branch.noteId}-${branch.parentNoteId}`];
delete becca.branches[branch.branchId];
}
2021-04-30 23:10:25 +02:00
2021-05-01 11:38:20 +02:00
function branchUpdated(branch) {
const childNote = becca.notes[branch.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (childNote) {
childNote.flatTextCache = null;
childNote.sortParents();
2020-05-20 00:03:33 +02:00
}
const parentNote = becca.notes[branch.parentNoteId];
if (parentNote) {
parentNote.sortChildren();
}
2021-04-30 23:10:25 +02:00
}
function attributeDeleted(attributeId) {
const attribute = becca.attributes[attributeId];
if (!attribute) {
return;
}
2021-05-01 11:38:20 +02:00
const note = becca.notes[attribute.noteId];
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
if (note) {
// first invalidate and only then remove the attribute (otherwise invalidation wouldn't be complete)
2021-05-17 22:35:36 +02:00
if (attribute.isAffectingSubtree || note.isTemplate()) {
2021-05-01 11:38:20 +02:00
note.invalidateSubTree();
} else {
note.invalidateThisCache();
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
2021-05-08 23:31:20 +02:00
note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attribute.attributeId);
2021-05-01 11:38:20 +02:00
const targetNote = attribute.targetNote;
2021-05-01 11:38:20 +02:00
if (targetNote) {
2021-05-08 23:31:20 +02:00
targetNote.targetRelations = targetNote.targetRelations.filter(rel => rel.attributeId !== attribute.attributeId);
2020-05-20 00:03:33 +02:00
}
2021-05-01 11:38:20 +02:00
}
2021-05-08 23:31:20 +02:00
delete becca.attributes[attribute.attributeId];
2021-05-01 11:38:20 +02:00
const key = `${attribute.type}-${attribute.name.toLowerCase()}`;
if (key in becca.attributeIndex) {
2021-05-08 23:31:20 +02:00
becca.attributeIndex[key] = becca.attributeIndex[key].filter(attr => attr.attributeId !== attribute.attributeId);
2021-05-01 11:38:20 +02:00
}
}
2020-05-20 00:03:33 +02:00
2021-05-01 11:38:20 +02:00
function attributeUpdated(attribute) {
const note = becca.notes[attribute.noteId];
2021-04-30 23:10:25 +02:00
2021-05-01 11:38:20 +02:00
if (note) {
2021-05-17 22:35:36 +02:00
if (attribute.isAffectingSubtree || note.isTemplate()) {
2021-05-01 11:38:20 +02:00
note.invalidateSubTree();
2021-04-30 23:10:25 +02:00
} else {
note.invalidateThisCache();
}
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
}
2021-05-01 11:38:20 +02:00
function noteReorderingUpdated(branchIdList) {
2021-04-30 23:10:25 +02:00
const parentNoteIds = new Set();
2021-05-01 11:38:20 +02:00
for (const branchId in branchIdList) {
2021-04-30 23:10:25 +02:00
const branch = becca.branches[branchId];
2021-04-30 23:10:25 +02:00
if (branch) {
2021-05-01 11:38:20 +02:00
branch.notePosition = branchIdList[branchId];
2021-04-30 23:10:25 +02:00
parentNoteIds.add(branch.parentNoteId);
}
}
2021-04-30 23:10:25 +02:00
}
2022-01-10 17:09:20 +01:00
function etapiTokenDeleted(etapiTokenId) {
delete becca.etapiTokens[etapiTokenId];
}
eventService.subscribeBeccaLoader(eventService.ENTER_PROTECTED_SESSION, () => {
try {
2021-04-16 23:00:08 +02:00
becca.decryptProtectedNotes();
}
catch (e) {
log.error(`Could not decrypt protected notes: ${e.message} ${e.stack}`);
}
2020-05-20 00:03:33 +02:00
});
2020-07-27 23:40:14 +02:00
eventService.subscribeBeccaLoader(eventService.LEAVE_PROTECTED_SESSION, load);
2020-07-27 23:40:14 +02:00
module.exports = {
2021-05-01 21:52:22 +02:00
load,
reload,
2021-05-01 21:52:22 +02:00
beccaLoaded
2020-07-27 23:40:14 +02:00
};