Notes/src/services/becca/becca_loader.js

205 lines
5.9 KiB
JavaScript
Raw Normal View History

2020-05-20 00:03:33 +02:00
"use strict";
const sql = require('../sql.js');
const eventService = require('../events.js');
2021-04-16 23:01:56 +02:00
const becca = require('./becca.js');
2020-06-20 21:42:41 +02:00
const sqlInit = require('../sql_init');
const log = require('../log');
2020-05-20 00:03:33 +02:00
const Note = require('./entities/note');
const Branch = require('./entities/branch');
const Attribute = require('./entities/attribute');
2021-04-30 23:10:25 +02:00
const Option = require('./entities/option');
2020-05-20 00:03:33 +02:00
2020-06-20 21:42:41 +02:00
sqlInit.dbReady.then(() => {
load();
});
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
for (const row of sql.iterateRows(`SELECT noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes`, [])) {
2021-04-30 23:10:25 +02:00
new Note(row);
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
2020-12-11 22:06:12 +01:00
for (const row of sql.iterateRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded FROM branches WHERE isDeleted = 0`, [])) {
2021-04-30 23:10:25 +02:00
new Branch(row);
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
2020-12-11 22:06:12 +01:00
for (const row of sql.iterateRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position FROM attributes WHERE isDeleted = 0`, [])) {
2021-04-30 23:10:25 +02:00
new Attribute(row);
}
for (const row of sql.getRows(`SELECT name, value, isSynced, utcDateModified FROM options`)) {
new Option(row);
2020-06-20 21:42:41 +02:00
}
2020-05-20 00:03:33 +02:00
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
}
2021-04-30 23:10:25 +02:00
function updateNote(entity) {
const {noteId} = entity;
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
if (entity.isDeleted) {
delete becca.notes[noteId];
} else if (noteId in becca.notes) {
becca.notes[noteId].update(entity);
} else {
const note = new Note(entity);
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
note.decrypt();
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
function updateBranch(entity) {
const {branchId, noteId, parentNoteId} = entity;
const childNote = becca.notes[noteId];
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
if (entity.isDeleted) {
if (childNote) {
childNote.parents = childNote.parents.filter(parent => parent.noteId !== parentNoteId);
childNote.parentBranches = childNote.parentBranches.filter(branch => branch.branchId !== branchId);
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
if (childNote.parents.length > 0) {
childNote.invalidateSubTree();
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
}
const parentNote = becca.notes[parentNoteId];
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
if (parentNote) {
parentNote.children = parentNote.children.filter(child => child.noteId !== noteId);
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
delete becca.childParentToBranch[`${noteId}-${parentNoteId}`];
delete becca.branches[branchId];
} else if (branchId in becca.branches) {
// only relevant properties which can change in a branch are prefix and isExpanded
becca.branches[branchId].prefix = entity.prefix;
becca.branches[branchId].isExpanded = entity.isExpanded;
if (childNote) {
childNote.flatTextCache = null;
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
} else {
becca.branches[branchId] = new Branch(entity);
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
if (childNote) {
childNote.resortParents();
2020-05-20 00:03:33 +02:00
}
}
2021-04-30 23:10:25 +02:00
}
function updateAttribute(entity) {
const {attributeId, noteId} = entity;
const note = becca.notes[noteId];
const attr = becca.attributes[attributeId];
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
if (entity.isDeleted) {
if (note && attr) {
// first invalidate and only then remove the attribute (otherwise invalidation wouldn't be complete)
if (attr.isAffectingSubtree || note.isTemplate) {
note.invalidateSubTree();
} else {
note.invalidateThisCache();
}
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attributeId);
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
const targetNote = attr.targetNote;
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
if (targetNote) {
targetNote.targetRelations = targetNote.targetRelations.filter(rel => rel.attributeId !== attributeId);
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
}
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
delete becca.attributes[attributeId];
2021-04-30 23:10:25 +02:00
if (attr) {
const key = `${attr.type}-${attr.name.toLowerCase()}`;
2021-04-30 23:10:25 +02:00
if (key in becca.attributeIndex) {
becca.attributeIndex[key] = becca.attributeIndex[key].filter(attr => attr.attributeId !== attributeId);
}
2020-05-20 00:03:33 +02:00
}
2021-04-30 23:10:25 +02:00
} else if (attributeId in becca.attributes) {
const attr = becca.attributes[attributeId];
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
// attr name and isInheritable are immutable
attr.value = entity.value;
if (attr.isAffectingSubtree || note.isTemplate) {
note.invalidateSubtreeFlatText();
} else {
note.invalidateThisCache();
}
} else {
const attr = new Attribute(entity);
2020-05-20 00:03:33 +02:00
2021-04-30 23:10:25 +02:00
if (note) {
2020-05-20 00:03:33 +02:00
if (attr.isAffectingSubtree || note.isTemplate) {
2021-04-30 23:10:25 +02:00
note.invalidateSubTree();
} else {
2021-03-30 21:39:42 +02:00
note.invalidateThisCache();
2020-05-20 00:03:33 +02:00
}
}
}
2021-04-30 23:10:25 +02:00
}
2021-04-30 23:10:25 +02:00
function updateNoteReordering(entity) {
const parentNoteIds = new Set();
2021-04-30 23:10:25 +02:00
for (const branchId in entity) {
const branch = becca.branches[branchId];
2021-04-30 23:10:25 +02:00
if (branch) {
branch.notePosition = entity[branchId];
parentNoteIds.add(branch.parentNoteId);
}
}
2021-04-30 23:10:25 +02:00
}
eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_SYNCED], ({entityName, entity}) => {
// note that entity can also be just POJO without methods if coming from sync
if (!becca.loaded) {
return;
}
if (entityName === 'notes') {
updateNote(entity);
}
else if (entityName === 'branches') {
updateBranch(entity);
}
else if (entityName === 'attributes') {
updateAttribute(entity);
}
else if (entityName === 'options') {
// nothing needed
}
else if (entityName === 'note_reordering') {
updateNoteReordering(entity);
}
2020-05-20 00:03:33 +02:00
});
eventService.subscribe(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.subscribe(eventService.LEAVE_PROTECTED_SESSION, () => {
load();
});
2020-07-27 23:40:14 +02:00
module.exports = {
load
};