From 2f96dc2d9defc13907e483edb5cc41056ce631d9 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 17 Feb 2024 11:39:29 +0200 Subject: [PATCH] server-ts: Fix most type errors in becca --- src/becca/entities/abstract_becca_entity.ts | 10 ++++++++-- src/becca/entities/battachment.ts | 2 +- src/becca/entities/bblob.ts | 1 + src/becca/entities/bbranch.ts | 20 ++++++++++++++------ src/becca/entities/betapi_token.ts | 8 ++++++-- src/becca/entities/bnote.ts | 18 +++++++++++------- 6 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/becca/entities/abstract_becca_entity.ts b/src/becca/entities/abstract_becca_entity.ts index 506e66564..53999be68 100644 --- a/src/becca/entities/abstract_becca_entity.ts +++ b/src/becca/entities/abstract_becca_entity.ts @@ -88,11 +88,17 @@ abstract class AbstractBeccaEntity> { return this.getPojo(); } - abstract hasStringContent(): boolean; + hasStringContent(): boolean { + // FIXME: Not sure why some entities don't implement it. + return true; + } abstract getPojo(): {}; - abstract get isDeleted(): boolean; + get isDeleted(): boolean { + // FIXME: Not sure why some entities don't implement it. + return false; + } /** * Saves entity - executes SQL, but doesn't commit the transaction on its own diff --git a/src/becca/entities/battachment.ts b/src/becca/entities/battachment.ts index 9c46d00b7..0d301494e 100644 --- a/src/becca/entities/battachment.ts +++ b/src/becca/entities/battachment.ts @@ -36,7 +36,7 @@ class BAttachment extends AbstractBeccaEntity { noteId?: number; attachmentId?: string; /** either noteId or revisionId to which this attachment belongs */ - ownerId?: string; + ownerId: string; role: string; mime: string; title: string; diff --git a/src/becca/entities/bblob.ts b/src/becca/entities/bblob.ts index 2ea67f044..27d559515 100644 --- a/src/becca/entities/bblob.ts +++ b/src/becca/entities/bblob.ts @@ -1,5 +1,6 @@ import { BlobRow } from "./rows"; +// FIXME: Why this does not extend the abstract becca? class BBlob { static get entityName() { return "blobs"; } static get primaryKeyName() { return "blobId"; } diff --git a/src/becca/entities/bbranch.ts b/src/becca/entities/bbranch.ts index 2086fc8a0..bf64d2ac6 100644 --- a/src/becca/entities/bbranch.ts +++ b/src/becca/entities/bbranch.ts @@ -180,7 +180,9 @@ class BBranch extends AbstractBeccaEntity { } for (const childBranch of note.getChildBranches()) { - childBranch.deleteBranch(deleteId, taskContext); + if (childBranch) { + childBranch.deleteBranch(deleteId, taskContext); + } } // first delete children and then parent - this will show up better in recent changes @@ -220,11 +222,17 @@ class BBranch extends AbstractBeccaEntity { if (this.notePosition === undefined || this.notePosition === null) { let maxNotePos = 0; - for (const childBranch of this.parentNote.getChildBranches()) { - if (maxNotePos < childBranch.notePosition - && childBranch.noteId !== '_hidden' // hidden has a very large notePosition to always stay last - ) { - maxNotePos = childBranch.notePosition; + if (this.parentNote) { + for (const childBranch of this.parentNote.getChildBranches()) { + if (!childBranch) { + continue; + } + + if (maxNotePos < childBranch.notePosition + && childBranch.noteId !== '_hidden' // hidden has a very large notePosition to always stay last + ) { + maxNotePos = childBranch.notePosition; + } } } diff --git a/src/becca/entities/betapi_token.ts b/src/becca/entities/betapi_token.ts index e4711c5fa..d128c1beb 100644 --- a/src/becca/entities/betapi_token.ts +++ b/src/becca/entities/betapi_token.ts @@ -22,7 +22,7 @@ class BEtapiToken extends AbstractBeccaEntity { etapiTokenId!: string; name!: string; tokenHash!: string; - isDeleted!: boolean; + private _isDeleted!: boolean; constructor(row: EtapiTokenRow) { super(); @@ -35,13 +35,17 @@ class BEtapiToken extends AbstractBeccaEntity { this.init(); } + get isDeleted() { + return this._isDeleted; + } + updateFromRow(row: EtapiTokenRow) { this.etapiTokenId = row.etapiTokenId; this.name = row.name; this.tokenHash = row.tokenHash; this.utcDateCreated = row.utcDateCreated || dateUtils.utcNowDateTime(); this.utcDateModified = row.utcDateModified || this.utcDateCreated; - this.isDeleted = !!row.isDeleted; + this._isDeleted = !!row.isDeleted; if (this.etapiTokenId) { this.becca.etapiTokens[this.etapiTokenId] = this; diff --git a/src/becca/entities/bnote.ts b/src/becca/entities/bnote.ts index 12c8ec593..a6ae85284 100644 --- a/src/becca/entities/bnote.ts +++ b/src/becca/entities/bnote.ts @@ -203,8 +203,7 @@ class BNote extends AbstractBeccaEntity { return this.children && this.children.length > 0; } - /** @returns {BBranch[]} */ - getChildBranches() { + getChildBranches(): (BBranch | null)[] { return this.children.map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId)); } @@ -757,7 +756,7 @@ class BNote extends AbstractBeccaEntity { this.parents = this.parentBranches .map(branch => branch.parentNote) - .filter(note => !!note); + .filter(note => !!note) as BNote[]; } sortChildren() { @@ -771,7 +770,7 @@ class BNote extends AbstractBeccaEntity { const aBranch = becca.getBranchFromChildAndParent(a.noteId, this.noteId); const bBranch = becca.getBranchFromChildAndParent(b.noteId, this.noteId); - return (aBranch?.notePosition - bBranch?.notePosition) || 0; + return ((aBranch?.notePosition || 0) - (bBranch?.notePosition || 0)) || 0; }); } @@ -900,7 +899,7 @@ class BNote extends AbstractBeccaEntity { const {searchResultNoteIds} = searchService.searchFromNote(this); const becca = this.becca; - return searchResultNoteIds + return (searchResultNoteIds as string[]) // FIXME: remove cast once search is converted .map(resultNoteId => becca.notes[resultNoteId]) .filter(note => !!note); } @@ -1445,9 +1444,9 @@ class BNote extends AbstractBeccaEntity { cloneTo(parentNoteId: string) { const cloningService = require('../../services/cloning'); - const branch = this.becca.getNote(parentNoteId).getParentBranches()[0]; + const branch = this.becca.getNote(parentNoteId)?.getParentBranches()[0]; - return cloningService.cloneNoteToBranch(this.noteId, branch.branchId); + return cloningService.cloneNoteToBranch(this.noteId, branch?.branchId); } isEligibleForConversionToAttachment(opts: ConvertOpts = { autoConversion: false }) { @@ -1603,6 +1602,11 @@ class BNote extends AbstractBeccaEntity { for (const noteAttachment of this.getAttachments()) { const revisionAttachment = noteAttachment.copy(); + + if (!revision.revisionId) { + throw new Error("Revision ID is missing."); + } + revisionAttachment.ownerId = revision.revisionId; revisionAttachment.setContent(noteAttachment.getContent(), { forceSave: true });