Notes/src/becca/becca-interface.ts

282 lines
8.4 KiB
TypeScript
Raw Normal View History

2024-02-16 23:51:56 +02:00
import sql = require('../services/sql');
import NoteSet = require('../services/search/note_set');
import NotFoundError = require('../errors/not_found_error');
2024-02-17 01:00:38 +02:00
import BOption = require('./entities/boption');
import BNote = require('./entities/bnote');
2021-10-16 22:13:34 +02:00
/**
2023-06-29 23:32:19 +02:00
* Becca is a backend cache of all notes, branches, and attributes.
* There's a similar frontend cache Froca, and share cache Shaca.
2021-10-16 22:13:34 +02:00
*/
2021-04-16 23:00:08 +02:00
class Becca {
notes!: Record<string, BNote>;
2024-02-17 01:00:38 +02:00
options!: Record<string, BOption>;
2020-05-16 23:12:29 +02:00
constructor() {
2020-05-22 09:38:30 +02:00
this.reset();
}
reset() {
/** @type {Object.<String, BNote>} */
2021-04-30 23:10:25 +02:00
this.notes = {};
2023-01-06 20:31:55 +01:00
/** @type {Object.<String, BBranch>} */
2021-04-30 23:10:25 +02:00
this.branches = {};
2023-01-06 20:31:55 +01:00
/** @type {Object.<String, BBranch>} */
2020-05-16 23:12:29 +02:00
this.childParentToBranch = {};
2023-01-06 20:31:55 +01:00
/** @type {Object.<String, BAttribute>} */
2021-04-30 23:10:25 +02:00
this.attributes = {};
2023-01-06 20:31:55 +01:00
/** @type {Object.<String, BAttribute[]>} Points from attribute type-name to list of attributes */
2020-05-22 09:38:30 +02:00
this.attributeIndex = {};
2021-04-30 23:10:25 +02:00
this.options = {};
2023-01-06 20:31:55 +01:00
/** @type {Object.<String, BEtapiToken>} */
2022-01-10 17:09:20 +01:00
this.etapiTokens = {};
2020-05-16 23:12:29 +02:00
this.dirtyNoteSetCache();
2020-05-16 23:12:29 +02:00
this.loaded = false;
}
2022-12-23 15:07:48 +01:00
getRoot() {
return this.getNote('root');
}
2023-01-05 23:38:41 +01:00
/** @returns {BAttribute[]} */
2020-05-16 23:12:29 +02:00
findAttributes(type, name) {
name = name.trim().toLowerCase();
if (name.startsWith('#') || name.startsWith('~')) {
name = name.substr(1);
}
return this.attributeIndex[`${type}-${name}`] || [];
2020-05-16 23:12:29 +02:00
}
2023-01-05 23:38:41 +01:00
/** @returns {BAttribute[]} */
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
addNote(noteId, note) {
this.notes[noteId] = note;
this.dirtyNoteSetCache();
}
/** @returns {BNote|null} */
getNote(noteId) {
return this.notes[noteId];
}
/** @returns {BNote|null} */
getNoteOrThrow(noteId) {
const note = this.notes[noteId];
if (!note) {
throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
}
return note;
}
/** @returns {BNote[]} */
getNotes(noteIds, ignoreMissing = false) {
2021-05-08 11:20:20 +02:00
const filteredNotes = [];
for (const noteId of noteIds) {
const note = this.notes[noteId];
if (!note) {
if (ignoreMissing) {
continue;
}
2021-05-08 11:20:20 +02:00
throw new Error(`Note '${noteId}' was not found in becca.`);
}
filteredNotes.push(note);
}
return filteredNotes;
2021-04-26 22:24:55 +02:00
}
2023-01-03 14:31:46 +01:00
/** @returns {BBranch|null} */
getBranch(branchId) {
return this.branches[branchId];
}
/** @returns {BBranch|null} */
getBranchOrThrow(branchId) {
const branch = this.getBranch(branchId);
if (!branch) {
throw new NotFoundError(`Branch '${branchId}' was not found in becca.`);
}
return branch;
}
2023-01-05 23:38:41 +01:00
/** @returns {BAttribute|null} */
getAttribute(attributeId) {
return this.attributes[attributeId];
}
/** @returns {BAttribute} */
getAttributeOrThrow(attributeId) {
const attribute = this.getAttribute(attributeId);
if (!attribute) {
throw new NotFoundError(`Attribute '${attributeId}' does not exist.`);
}
return attribute;
}
2023-01-03 14:31:46 +01:00
/** @returns {BBranch|null} */
getBranchFromChildAndParent(childNoteId, parentNoteId) {
2020-05-17 09:48:24 +02:00
return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];
}
/** @returns {BRevision|null} */
getRevision(revisionId) {
const row = sql.getRow("SELECT * FROM revisions WHERE revisionId = ?", [revisionId]);
const BRevision = require('./entities/brevision.js'); // avoiding circular dependency problems
return row ? new BRevision(row) : null;
}
2021-05-01 21:52:22 +02:00
2023-03-16 12:17:55 +01:00
/** @returns {BAttachment|null} */
2023-05-21 18:14:17 +02:00
getAttachment(attachmentId, opts = {}) {
opts.includeContentLength = !!opts.includeContentLength;
const query = opts.includeContentLength
? `SELECT attachments.*, LENGTH(blobs.content) AS contentLength
FROM attachments
JOIN blobs USING (blobId)
WHERE attachmentId = ? AND isDeleted = 0`
: `SELECT * FROM attachments WHERE attachmentId = ? AND isDeleted = 0`;
const BAttachment = require('./entities/battachment.js'); // avoiding circular dependency problems
2023-05-21 18:14:17 +02:00
return sql.getRows(query, [attachmentId])
.map(row => new BAttachment(row))[0];
}
/** @returns {BAttachment} */
2023-05-21 18:14:17 +02:00
getAttachmentOrThrow(attachmentId, opts = {}) {
const attachment = this.getAttachment(attachmentId, opts);
if (!attachment) {
throw new NotFoundError(`Attachment '${attachmentId}' has not been found.`);
}
return attachment;
}
2023-04-25 00:01:58 +02:00
/** @returns {BAttachment[]} */
getAttachments(attachmentIds) {
const BAttachment = require('./entities/battachment.js'); // avoiding circular dependency problems
2023-04-25 00:01:58 +02:00
return sql.getManyRows("SELECT * FROM attachments WHERE attachmentId IN (???) AND isDeleted = 0", attachmentIds)
.map(row => new BAttachment(row));
}
2023-05-05 16:37:39 +02:00
/** @returns {BBlob|null} */
getBlob(entity) {
const row = sql.getRow("SELECT *, LENGTH(content) AS contentLength FROM blobs WHERE blobId = ?", [entity.blobId]);
2023-05-05 16:37:39 +02:00
const BBlob = require('./entities/bblob.js'); // avoiding circular dependency problems
2023-05-05 16:37:39 +02:00
return row ? new BBlob(row) : null;
}
/** @returns {BOption|null} */
2021-05-01 21:52:22 +02:00
getOption(name) {
return this.options[name];
}
2021-05-02 12:02:32 +02:00
/** @returns {BEtapiToken[]} */
2022-01-10 17:09:20 +01:00
getEtapiTokens() {
return Object.values(this.etapiTokens);
}
/** @returns {BEtapiToken|null} */
2022-01-10 17:09:20 +01:00
getEtapiToken(etapiTokenId) {
return this.etapiTokens[etapiTokenId];
}
2023-09-28 00:24:53 +02:00
/** @returns {AbstractBeccaEntity|null} */
2021-05-17 22:05:35 +02:00
getEntity(entityName, entityId) {
2021-05-02 12:02:32 +02:00
if (!entityName || !entityId) {
return null;
}
if (entityName === 'revisions') {
return this.getRevision(entityId);
2023-03-16 12:17:55 +01:00
} else if (entityName === 'attachments') {
return this.getAttachment(entityId);
2021-08-07 21:21:30 +02:00
}
2021-05-02 12:02:32 +02:00
const camelCaseEntityName = entityName.toLowerCase().replace(/(_[a-z])/g,
group =>
group
.toUpperCase()
.replace('_', '')
);
if (!(camelCaseEntityName in this)) {
throw new Error(`Unknown entity name '${camelCaseEntityName}' (original argument '${entityName}')`);
}
2021-05-02 12:02:32 +02:00
return this[camelCaseEntityName][entityId];
}
2021-05-02 19:59:16 +02:00
/** @returns {BRecentNote[]} */
2021-05-02 19:59:16 +02:00
getRecentNotesFromQuery(query, params = []) {
const rows = sql.getRows(query, params);
const BRecentNote = require('./entities/brecent_note.js'); // avoiding circular dependency problems
return rows.map(row => new BRecentNote(row));
2021-05-02 19:59:16 +02:00
}
/** @returns {BRevision[]} */
getRevisionsFromQuery(query, params = []) {
2021-05-02 19:59:16 +02:00
const rows = sql.getRows(query, params);
const BRevision = require('./entities/brevision.js'); // avoiding circular dependency problems
return rows.map(row => new BRevision(row));
2021-05-02 19:59:16 +02:00
}
/** Should be called when the set of all non-skeleton notes changes (added/removed) */
dirtyNoteSetCache() {
this.allNoteSetCache = null;
}
getAllNoteSet() {
// caching this since it takes 10s of milliseconds to fill this initial NoteSet for many notes
if (!this.allNoteSetCache) {
const allNotes = [];
for (const noteId in becca.notes) {
const note = becca.notes[noteId];
// in the process of loading data sometimes we create "skeleton" note instances which are expected to be filled later
// in case of inconsistent data this might not work and search will then crash on these
if (note.type !== undefined) {
allNotes.push(note);
}
}
this.allNoteSetCache = new NoteSet(allNotes);
}
return this.allNoteSetCache;
}
2020-05-16 23:12:29 +02:00
}
2024-02-16 23:51:56 +02:00
export = Becca;