2020-05-17 09:48:24 +02:00
|
|
|
"use strict";
|
2020-05-16 23:12:29 +02:00
|
|
|
|
2020-05-17 09:48:24 +02:00
|
|
|
const Note = require('./entities/note');
|
|
|
|
const Branch = require('./entities/branch');
|
|
|
|
const Attribute = require('./entities/attribute');
|
2020-05-16 23:12:29 +02:00
|
|
|
const sql = require('../sql.js');
|
|
|
|
const sqlInit = require('../sql_init.js');
|
|
|
|
const eventService = require('../events.js');
|
|
|
|
|
|
|
|
class NoteCache {
|
|
|
|
constructor() {
|
|
|
|
/** @type {Object.<String, Note>} */
|
|
|
|
this.notes = null;
|
|
|
|
/** @type {Object.<String, Branch>} */
|
|
|
|
this.branches = null;
|
|
|
|
/** @type {Object.<String, Branch>} */
|
|
|
|
this.childParentToBranch = {};
|
|
|
|
/** @type {Object.<String, Attribute>} */
|
|
|
|
this.attributes = null;
|
|
|
|
/** @type {Object.<String, Attribute[]>} Points from attribute type-name to list of attributes them */
|
|
|
|
this.attributeIndex = null;
|
|
|
|
|
|
|
|
this.loaded = false;
|
2020-05-17 09:48:24 +02:00
|
|
|
this.loadedPromise = this.load();
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @return {Attribute[]} */
|
|
|
|
findAttributes(type, name) {
|
|
|
|
return this.attributeIndex[`${type}-${name}`] || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2020-05-17 09:48:24 +02:00
|
|
|
await sqlInit.dbReady;
|
|
|
|
|
2020-05-16 23:12:29 +02:00
|
|
|
this.notes = await this.getMappedRows(`SELECT noteId, title, isProtected FROM notes WHERE isDeleted = 0`,
|
2020-05-17 10:11:19 +02:00
|
|
|
row => new Note(this, row));
|
2020-05-16 23:12:29 +02:00
|
|
|
|
|
|
|
this.branches = await this.getMappedRows(`SELECT branchId, noteId, parentNoteId, prefix FROM branches WHERE isDeleted = 0`,
|
2020-05-17 10:11:19 +02:00
|
|
|
row => new Branch(this, row));
|
2020-05-16 23:12:29 +02:00
|
|
|
|
|
|
|
this.attributeIndex = [];
|
|
|
|
|
|
|
|
this.attributes = await this.getMappedRows(`SELECT attributeId, noteId, type, name, value, isInheritable FROM attributes WHERE isDeleted = 0`,
|
2020-05-17 10:11:19 +02:00
|
|
|
row => new Attribute(this, row));
|
2020-05-16 23:12:29 +02:00
|
|
|
|
|
|
|
this.loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getMappedRows(query, cb) {
|
|
|
|
const map = {};
|
|
|
|
const results = await sql.getRows(query, []);
|
|
|
|
|
|
|
|
for (const row of results) {
|
|
|
|
const keys = Object.keys(row);
|
|
|
|
|
|
|
|
map[row[keys[0]]] = cb(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
decryptProtectedNotes() {
|
|
|
|
for (const note of Object.values(this.notes)) {
|
2020-05-17 09:48:24 +02:00
|
|
|
note.decrypt();
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-17 09:48:24 +02:00
|
|
|
|
|
|
|
getBranch(childNoteId, parentNoteId) {
|
|
|
|
return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];
|
|
|
|
}
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const noteCache = new NoteCache();
|
|
|
|
|
|
|
|
eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_SYNCED], async ({entityName, entity}) => {
|
|
|
|
// note that entity can also be just POJO without methods if coming from sync
|
|
|
|
|
|
|
|
if (!noteCache.loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entityName === 'notes') {
|
|
|
|
const {noteId} = entity;
|
|
|
|
|
|
|
|
if (entity.isDeleted) {
|
|
|
|
delete noteCache.notes[noteId];
|
|
|
|
}
|
|
|
|
else if (noteId in noteCache.notes) {
|
|
|
|
const note = noteCache.notes[noteId];
|
|
|
|
|
|
|
|
// we can assume we have protected session since we managed to update
|
|
|
|
note.title = entity.title;
|
|
|
|
note.isProtected = entity.isProtected;
|
|
|
|
note.isDecrypted = !entity.isProtected || !!entity.isContentAvailable;
|
|
|
|
note.flatTextCache = null;
|
|
|
|
|
2020-05-17 10:11:19 +02:00
|
|
|
note.decrypt();
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const note = new Note(entity);
|
|
|
|
noteCache.notes[noteId] = note;
|
|
|
|
|
2020-05-17 10:11:19 +02:00
|
|
|
note.decrypt();
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (entityName === 'branches') {
|
|
|
|
const {branchId, noteId, parentNoteId} = entity;
|
|
|
|
const childNote = noteCache.notes[noteId];
|
|
|
|
|
|
|
|
if (entity.isDeleted) {
|
|
|
|
if (childNote) {
|
|
|
|
childNote.parents = childNote.parents.filter(parent => parent.noteId !== parentNoteId);
|
|
|
|
childNote.parentBranches = childNote.parentBranches.filter(branch => branch.branchId !== branchId);
|
|
|
|
|
|
|
|
if (childNote.parents.length > 0) {
|
|
|
|
childNote.invalidateSubtreeCaches();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentNote = noteCache.notes[parentNoteId];
|
|
|
|
|
|
|
|
if (parentNote) {
|
|
|
|
parentNote.children = parentNote.children.filter(child => child.noteId !== noteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete noteCache.childParentToBranch[`${noteId}-${parentNoteId}`];
|
|
|
|
delete noteCache.branches[branchId];
|
|
|
|
}
|
|
|
|
else if (branchId in noteCache.branches) {
|
|
|
|
// only relevant thing which can change in a branch is prefix
|
|
|
|
noteCache.branches[branchId].prefix = entity.prefix;
|
|
|
|
|
|
|
|
if (childNote) {
|
|
|
|
childNote.flatTextCache = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
noteCache.branches[branchId] = new Branch(entity);
|
|
|
|
|
|
|
|
if (childNote) {
|
|
|
|
childNote.resortParents();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (entityName === 'attributes') {
|
|
|
|
const {attributeId, noteId} = entity;
|
|
|
|
const note = noteCache.notes[noteId];
|
|
|
|
const attr = noteCache.attributes[attributeId];
|
|
|
|
|
|
|
|
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.invalidateSubtreeCaches();
|
|
|
|
}
|
|
|
|
|
|
|
|
note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attributeId);
|
|
|
|
|
|
|
|
const targetNote = attr.targetNote;
|
|
|
|
|
|
|
|
if (targetNote) {
|
|
|
|
targetNote.targetRelations = targetNote.targetRelations.filter(rel => rel.attributeId !== attributeId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete noteCache.attributes[attributeId];
|
|
|
|
delete noteCache.attributeIndex[`${attr.type}-${attr.name}`];
|
|
|
|
}
|
|
|
|
else if (attributeId in noteCache.attributes) {
|
|
|
|
const attr = noteCache.attributes[attributeId];
|
|
|
|
|
|
|
|
// attr name and isInheritable are immutable
|
|
|
|
attr.value = entity.value;
|
|
|
|
|
|
|
|
if (attr.isAffectingSubtree || note.isTemplate) {
|
|
|
|
note.invalidateSubtreeFlatText();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
note.flatTextCache = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const attr = new Attribute(entity);
|
|
|
|
noteCache.attributes[attributeId] = attr;
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
if (attr.isAffectingSubtree || note.isTemplate) {
|
|
|
|
note.invalidateSubtreeCaches();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.invalidateThisCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => {
|
|
|
|
noteCache.loadedPromise.then(() => noteCache.decryptProtectedNotes());
|
|
|
|
});
|
|
|
|
|
2020-05-17 09:48:24 +02:00
|
|
|
module.exports = noteCache;
|