|
|
|
|
@ -1,19 +1,20 @@
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const sql = require('../services/sql');
|
|
|
|
|
const eventService = require('../services/events');
|
|
|
|
|
const becca = require('./becca');
|
|
|
|
|
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');
|
|
|
|
|
const cls = require('../services/cls');
|
|
|
|
|
const entityConstructor = require('../becca/entity_constructor');
|
|
|
|
|
|
|
|
|
|
const beccaLoaded = new Promise((res, rej) => {
|
|
|
|
|
import sql = require('../services/sql');
|
|
|
|
|
import eventService = require('../services/events');
|
|
|
|
|
import becca = require('./becca');
|
|
|
|
|
import sqlInit = require('../services/sql_init');
|
|
|
|
|
import log = require('../services/log');
|
|
|
|
|
import BNote = require('./entities/bnote');
|
|
|
|
|
import BBranch = require('./entities/bbranch');
|
|
|
|
|
import BAttribute = require('./entities/battribute');
|
|
|
|
|
import BOption = require('./entities/boption');
|
|
|
|
|
import BEtapiToken = require('./entities/betapi_token');
|
|
|
|
|
import cls = require('../services/cls');
|
|
|
|
|
import entityConstructor = require('../becca/entity_constructor');
|
|
|
|
|
import { AttributeRow, BranchRow, EtapiTokenRow, NoteRow, OptionRow } from './entities/rows';
|
|
|
|
|
|
|
|
|
|
const beccaLoaded = new Promise<void>((res, rej) => {
|
|
|
|
|
sqlInit.dbReady.then(() => {
|
|
|
|
|
cls.init(() => {
|
|
|
|
|
load();
|
|
|
|
|
@ -38,7 +39,7 @@ function load() {
|
|
|
|
|
new BNote().update(row).init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const branchRows = sql.getRawRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`);
|
|
|
|
|
const branchRows = sql.getRawRows<{ notePosition: number}>(`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);
|
|
|
|
|
|
|
|
|
|
@ -46,15 +47,15 @@ function load() {
|
|
|
|
|
new BBranch().update(row).init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const row of sql.getRawRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`)) {
|
|
|
|
|
for (const row of sql.getRawRows<AttributeRow>(`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`)) {
|
|
|
|
|
new BAttribute().update(row).init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const row of sql.getRows(`SELECT name, value, isSynced, utcDateModified FROM options`)) {
|
|
|
|
|
for (const row of sql.getRows<OptionRow>(`SELECT name, value, isSynced, utcDateModified FROM options`)) {
|
|
|
|
|
new BOption(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const row of sql.getRows(`SELECT etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified FROM etapi_tokens WHERE isDeleted = 0`)) {
|
|
|
|
|
for (const row of sql.getRows<EtapiTokenRow>(`SELECT etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified FROM etapi_tokens WHERE isDeleted = 0`)) {
|
|
|
|
|
new BEtapiToken(row);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
@ -68,7 +69,7 @@ function load() {
|
|
|
|
|
log.info(`Becca (note cache) load took ${Date.now() - start}ms`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function reload(reason) {
|
|
|
|
|
function reload(reason: string) {
|
|
|
|
|
load();
|
|
|
|
|
|
|
|
|
|
require('../services/ws').reloadFrontend(reason || "becca reloaded");
|
|
|
|
|
@ -112,7 +113,7 @@ eventService.subscribeBeccaLoader(eventService.ENTITY_CHANGED, ({entityName, en
|
|
|
|
|
* @param entityRow - can be a becca entity (change comes from this trilium instance) or just a row (from sync).
|
|
|
|
|
* It should be therefore treated as a row.
|
|
|
|
|
*/
|
|
|
|
|
function postProcessEntityUpdate(entityName, entityRow) {
|
|
|
|
|
function postProcessEntityUpdate(entityName: string, entityRow: any) {
|
|
|
|
|
if (entityName === 'notes') {
|
|
|
|
|
noteUpdated(entityRow);
|
|
|
|
|
} else if (entityName === 'branches') {
|
|
|
|
|
@ -140,13 +141,13 @@ eventService.subscribeBeccaLoader([eventService.ENTITY_DELETED, eventService.ENT
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function noteDeleted(noteId) {
|
|
|
|
|
function noteDeleted(noteId: string) {
|
|
|
|
|
delete becca.notes[noteId];
|
|
|
|
|
|
|
|
|
|
becca.dirtyNoteSetCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function branchDeleted(branchId) {
|
|
|
|
|
function branchDeleted(branchId: string) {
|
|
|
|
|
const branch = becca.branches[branchId];
|
|
|
|
|
|
|
|
|
|
if (!branch) {
|
|
|
|
|
@ -173,23 +174,26 @@ function branchDeleted(branchId) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete becca.childParentToBranch[`${branch.noteId}-${branch.parentNoteId}`];
|
|
|
|
|
if (branch.branchId) {
|
|
|
|
|
delete becca.branches[branch.branchId];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function noteUpdated(entityRow) {
|
|
|
|
|
function noteUpdated(entityRow: NoteRow) {
|
|
|
|
|
const note = becca.notes[entityRow.noteId];
|
|
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
|
// FIXME, this wouldn't have worked in the original implementation since the variable was named __flatTextCache.
|
|
|
|
|
// type / mime could have been changed, and they are present in flatTextCache
|
|
|
|
|
note.flatTextCache = null;
|
|
|
|
|
note.__flatTextCache = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function branchUpdated(branchRow) {
|
|
|
|
|
function branchUpdated(branchRow: BranchRow) {
|
|
|
|
|
const childNote = becca.notes[branchRow.noteId];
|
|
|
|
|
|
|
|
|
|
if (childNote) {
|
|
|
|
|
childNote.flatTextCache = null;
|
|
|
|
|
childNote.__flatTextCache = null;
|
|
|
|
|
childNote.sortParents();
|
|
|
|
|
|
|
|
|
|
// notes in the subtree can get new inherited attributes
|
|
|
|
|
@ -204,7 +208,7 @@ function branchUpdated(branchRow) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function attributeDeleted(attributeId) {
|
|
|
|
|
function attributeDeleted(attributeId: string) {
|
|
|
|
|
const attribute = becca.attributes[attributeId];
|
|
|
|
|
|
|
|
|
|
if (!attribute) {
|
|
|
|
|
@ -239,8 +243,7 @@ function attributeDeleted(attributeId) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @param {BAttribute} attributeRow */
|
|
|
|
|
function attributeUpdated(attributeRow) {
|
|
|
|
|
function attributeUpdated(attributeRow: BAttribute) {
|
|
|
|
|
const attribute = becca.attributes[attributeRow.attributeId];
|
|
|
|
|
const note = becca.notes[attributeRow.noteId];
|
|
|
|
|
|
|
|
|
|
@ -253,7 +256,7 @@ function attributeUpdated(attributeRow) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function noteReorderingUpdated(branchIdList) {
|
|
|
|
|
function noteReorderingUpdated(branchIdList: number[]) {
|
|
|
|
|
const parentNoteIds = new Set();
|
|
|
|
|
|
|
|
|
|
for (const branchId in branchIdList) {
|
|
|
|
|
@ -267,7 +270,7 @@ function noteReorderingUpdated(branchIdList) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function etapiTokenDeleted(etapiTokenId) {
|
|
|
|
|
function etapiTokenDeleted(etapiTokenId: string) {
|
|
|
|
|
delete becca.etapiTokens[etapiTokenId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -275,14 +278,14 @@ eventService.subscribeBeccaLoader(eventService.ENTER_PROTECTED_SESSION, () => {
|
|
|
|
|
try {
|
|
|
|
|
becca.decryptProtectedNotes();
|
|
|
|
|
}
|
|
|
|
|
catch (e) {
|
|
|
|
|
catch (e: any) {
|
|
|
|
|
log.error(`Could not decrypt protected notes: ${e.message} ${e.stack}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
eventService.subscribeBeccaLoader(eventService.LEAVE_PROTECTED_SESSION, load);
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
export = {
|
|
|
|
|
load,
|
|
|
|
|
reload,
|
|
|
|
|
beccaLoaded
|