2020-05-17 09:48:24 +02:00
|
|
|
"use strict";
|
2020-05-16 23:12:29 +02:00
|
|
|
|
2021-04-26 22:18:14 +02:00
|
|
|
const sql = require("../sql.js");
|
|
|
|
const NoteRevision = require("./entities/note_revision.js");
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
class Becca {
|
2020-05-16 23:12:29 +02:00
|
|
|
constructor() {
|
2020-05-22 09:38:30 +02:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2020-05-16 23:12:29 +02:00
|
|
|
/** @type {Object.<String, Note>} */
|
2020-05-22 09:38:30 +02:00
|
|
|
this.notes = [];
|
2020-05-16 23:12:29 +02:00
|
|
|
/** @type {Object.<String, Branch>} */
|
2020-05-22 09:38:30 +02:00
|
|
|
this.branches = [];
|
2020-05-16 23:12:29 +02:00
|
|
|
/** @type {Object.<String, Branch>} */
|
|
|
|
this.childParentToBranch = {};
|
|
|
|
/** @type {Object.<String, Attribute>} */
|
2020-05-22 09:38:30 +02:00
|
|
|
this.attributes = [];
|
2020-12-12 12:07:15 +01:00
|
|
|
/** @type {Object.<String, Attribute[]>} Points from attribute type-name to list of attributes */
|
2020-05-22 09:38:30 +02:00
|
|
|
this.attributeIndex = {};
|
2020-05-16 23:12:29 +02:00
|
|
|
|
|
|
|
this.loaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return {Attribute[]} */
|
|
|
|
findAttributes(type, name) {
|
2020-12-12 12:07:15 +01:00
|
|
|
return this.attributeIndex[`${type}-${name.toLowerCase()}`] || [];
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 12:05:12 +02:00
|
|
|
/** @return {Attribute[]} */
|
|
|
|
findAttributesWithPrefix(type, name) {
|
|
|
|
const resArr = [];
|
|
|
|
const key = `${type}-${name}`;
|
|
|
|
|
|
|
|
for (const idx in this.attributeIndex) {
|
|
|
|
if (idx.startsWith(key)) {
|
|
|
|
resArr.push(this.attributeIndex[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resArr.flat();
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:12:29 +02:00
|
|
|
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
|
|
|
|
2021-04-26 22:18:14 +02:00
|
|
|
getNote(noteId) {
|
|
|
|
return this.notes[noteId];
|
|
|
|
}
|
|
|
|
|
2021-04-26 22:24:55 +02:00
|
|
|
getNotes(noteIds) {
|
|
|
|
return this.notes.filter(note => noteIds.includes(note.noteId));
|
|
|
|
}
|
|
|
|
|
2021-04-26 22:18:14 +02:00
|
|
|
getBranch(branchId) {
|
|
|
|
return this.branches[branchId];
|
|
|
|
}
|
|
|
|
|
|
|
|
getAttribute(attributeId) {
|
|
|
|
return this.attributes[attributeId];
|
|
|
|
}
|
|
|
|
|
|
|
|
getBranchFromChildAndParent(childNoteId, parentNoteId) {
|
2020-05-17 09:48:24 +02:00
|
|
|
return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];
|
|
|
|
}
|
2021-04-26 22:18:14 +02:00
|
|
|
|
|
|
|
getNoteRevision(noteRevisionId) {
|
|
|
|
const row = sql.getRow("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [noteRevisionId]);
|
|
|
|
|
|
|
|
return row ? new NoteRevision(row) : null;
|
|
|
|
}
|
2020-05-16 23:12:29 +02:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
const becca = new Becca();
|
2020-05-16 23:12:29 +02:00
|
|
|
|
2021-04-16 23:00:08 +02:00
|
|
|
module.exports = becca;
|