server-ts: Port services/notes & hidden_subtree

This commit is contained in:
Elian Doran 2024-02-17 22:58:54 +02:00
parent 669988953d
commit de7f4de05b
No known key found for this signature in database
31 changed files with 267 additions and 202 deletions

View File

@ -104,7 +104,8 @@ abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> {
/** /**
* Saves entity - executes SQL, but doesn't commit the transaction on its own * Saves entity - executes SQL, but doesn't commit the transaction on its own
*/ */
save(): this { // FIXME: opts not used but called a few times, maybe should be used by derived classes or passed to beforeSaving.
save(opts?: {}): this {
const constructorData = (this.constructor as unknown as ConstructorData<T>); const constructorData = (this.constructor as unknown as ConstructorData<T>);
const entityName = constructorData.entityName; const entityName = constructorData.entityName;
const primaryKeyName = constructorData.primaryKeyName; const primaryKeyName = constructorData.primaryKeyName;

View File

@ -45,7 +45,7 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {
blobId?: string; blobId?: string;
isProtected?: boolean; isProtected?: boolean;
dateModified?: string; dateModified?: string;
utcDateScheduledForErasureSince?: string; utcDateScheduledForErasureSince?: string | null;
/** optionally added to the entity */ /** optionally added to the entity */
contentLength?: number; contentLength?: number;
isDecrypted?: boolean; isDecrypted?: boolean;
@ -126,8 +126,8 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {
} }
} }
getContent(): string | Buffer { getContent(): Buffer {
return this._getContent(); return this._getContent() as Buffer;
} }
setContent(content: any, opts: ContentOpts) { setContent(content: any, opts: ContentOpts) {
@ -170,6 +170,11 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {
if (this.role === 'image' && parentNote.type === 'text') { if (this.role === 'image' && parentNote.type === 'text') {
const origContent = parentNote.getContent(); const origContent = parentNote.getContent();
if (typeof origContent !== "string") {
throw new Error(`Note with ID '${note.noteId} has a text type but non-string content.`);
}
const oldAttachmentUrl = `api/attachments/${this.attachmentId}/image/`; const oldAttachmentUrl = `api/attachments/${this.attachmentId}/image/`;
const newNoteUrl = `api/images/${note.noteId}/`; const newNoteUrl = `api/images/${note.noteId}/`;

View File

@ -216,9 +216,8 @@ class BNote extends AbstractBeccaEntity<BNote> {
* - changes in the note metadata or title should not trigger note content sync (so we keep separate utcDateModified and entity changes records) * - changes in the note metadata or title should not trigger note content sync (so we keep separate utcDateModified and entity changes records)
* - but to the user note content and title changes are one and the same - single dateModified (so all changes must go through Note and content is not a separate entity) * - but to the user note content and title changes are one and the same - single dateModified (so all changes must go through Note and content is not a separate entity)
*/ */
// FIXME: original declaration was (string | Buffer), but everywhere it's used as a string. getContent() {
getContent(): string { return this._getContent();
return this._getContent() as string;
} }
/** /**
@ -226,7 +225,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
getJsonContent(): {} | null { getJsonContent(): {} | null {
const content = this.getContent(); const content = this.getContent();
if (!content || !content.trim()) { if (typeof content !== "string" || !content || !content.trim()) {
return null; return null;
} }
@ -243,7 +242,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
} }
} }
setContent(content: string, opts: ContentOpts = {}) { setContent(content: Buffer | string, opts: ContentOpts = {}) {
this._setContent(content, opts); this._setContent(content, opts);
eventService.emit(eventService.NOTE_CONTENT_CHANGE, { entity: this }); eventService.emit(eventService.NOTE_CONTENT_CHANGE, { entity: this });
@ -661,7 +660,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
* @param name - relation name to filter * @param name - relation name to filter
* @returns all note's relations (attributes with type relation), including inherited ones * @returns all note's relations (attributes with type relation), including inherited ones
*/ */
getRelations(name: string): BAttribute[] { getRelations(name?: string): BAttribute[] {
return this.getAttributes(RELATION, name); return this.getAttributes(RELATION, name);
} }
@ -1510,6 +1509,10 @@ class BNote extends AbstractBeccaEntity<BNote> {
const oldNoteUrl = `api/images/${this.noteId}/`; const oldNoteUrl = `api/images/${this.noteId}/`;
const newAttachmentUrl = `api/attachments/${attachment.attachmentId}/image/`; const newAttachmentUrl = `api/attachments/${attachment.attachmentId}/image/`;
if (typeof parentContent !== "string") {
throw new Error("Unable to convert image note into attachment because parent note does not have a string content.");
}
const fixedContent = utils.replaceAll(parentContent, oldNoteUrl, newAttachmentUrl); const fixedContent = utils.replaceAll(parentContent, oldNoteUrl, newAttachmentUrl);
parentNote.setContent(fixedContent); parentNote.setContent(fixedContent);
@ -1611,7 +1614,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
revisionAttachment.ownerId = revision.revisionId; revisionAttachment.ownerId = revision.revisionId;
revisionAttachment.setContent(noteAttachment.getContent(), { forceSave: true }); revisionAttachment.setContent(noteAttachment.getContent(), { forceSave: true });
if (this.type === 'text') { if (this.type === 'text' && typeof noteContent === "string") {
// content is rewritten to point to the revision attachments // content is rewritten to point to the revision attachments
noteContent = noteContent.replaceAll(`attachments/${noteAttachment.attachmentId}`, noteContent = noteContent.replaceAll(`attachments/${noteAttachment.attachmentId}`,
`attachments/${revisionAttachment.attachmentId}`); `attachments/${revisionAttachment.attachmentId}`);
@ -1654,7 +1657,6 @@ class BNote extends AbstractBeccaEntity<BNote> {
position position
}); });
content = content || "";
attachment.setContent(content, {forceSave: true}); attachment.setContent(content, {forceSave: true});
return attachment; return attachment;

View File

@ -13,7 +13,7 @@ export interface AttachmentRow {
utcDateModified?: string; utcDateModified?: string;
utcDateScheduledForErasureSince?: string; utcDateScheduledForErasureSince?: string;
contentLength?: number; contentLength?: number;
content?: string; content?: Buffer | string;
} }
export interface RevisionRow { export interface RevisionRow {
@ -69,9 +69,9 @@ export interface AttributeRow {
noteId: string; noteId: string;
type: AttributeType; type: AttributeType;
name: string; name: string;
position: number; position?: number;
value: string; value?: string;
isInheritable: boolean; isInheritable?: boolean;
utcDateModified?: string; utcDateModified?: string;
} }
@ -79,9 +79,10 @@ export interface BranchRow {
branchId?: string; branchId?: string;
noteId: string; noteId: string;
parentNoteId: string; parentNoteId: string;
prefix: string | null; prefix?: string | null;
notePosition: number; notePosition: number | null;
isExpanded: boolean; isExpanded?: boolean;
isDeleted?: boolean;
utcDateModified?: string; utcDateModified?: string;
} }
@ -94,13 +95,19 @@ export type NoteType = ("file" | "image" | "search" | "noteMap" | "launcher" | "
export interface NoteRow { export interface NoteRow {
noteId: string; noteId: string;
deleteId: string;
title: string; title: string;
type: NoteType; type: NoteType;
mime: string; mime: string;
isProtected: boolean; isProtected: boolean;
isDeleted: boolean;
blobId: string; blobId: string;
dateCreated: string; dateCreated: string;
dateModified: string; dateModified: string;
utcDateCreated: string; utcDateCreated: string;
utcDateModified: string; utcDateModified: string;
} }
export interface AttributeRow {
}

View File

@ -6,4 +6,4 @@ class ValidationError {
} }
} }
module.exports = ValidationError; export = ValidationError;

View File

@ -2,7 +2,7 @@ const becca = require('../becca/becca');
const utils = require('../services/utils'); const utils = require('../services/utils');
const eu = require('./etapi_utils'); const eu = require('./etapi_utils');
const mappers = require('./mappers.js'); const mappers = require('./mappers.js');
const noteService = require('../services/notes.js'); const noteService = require('../services/notes');
const TaskContext = require('../services/task_context'); const TaskContext = require('../services/task_context');
const v = require('./validators.js'); const v = require('./validators.js');
const searchService = require('../services/search/services/search.js'); const searchService = require('../services/search/services/search.js');

View File

@ -2,7 +2,7 @@
const attributeService = require('../../services/attributes.js'); const attributeService = require('../../services/attributes.js');
const cloneService = require('../../services/cloning.js'); const cloneService = require('../../services/cloning.js');
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes');
const dateNoteService = require('../../services/date_notes.js'); const dateNoteService = require('../../services/date_notes.js');
const dateUtils = require('../../services/date_utils'); const dateUtils = require('../../services/date_utils');
const imageService = require('../../services/image.js'); const imageService = require('../../services/image.js');

View File

@ -3,7 +3,7 @@
const protectedSessionService = require('../../services/protected_session'); const protectedSessionService = require('../../services/protected_session');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const log = require('../../services/log'); const log = require('../../services/log');
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes');
const tmp = require('tmp'); const tmp = require('tmp');
const fs = require('fs'); const fs = require('fs');
const { Readable } = require('stream'); const { Readable } = require('stream');

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes');
const eraseService = require('../../services/erase'); const eraseService = require('../../services/erase');
const treeService = require('../../services/tree.js'); const treeService = require('../../services/tree.js');
const sql = require('../../services/sql'); const sql = require('../../services/sql');

View File

@ -2,7 +2,7 @@
const sql = require('../../services/sql'); const sql = require('../../services/sql');
const protectedSessionService = require('../../services/protected_session'); const protectedSessionService = require('../../services/protected_session');
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes');
const becca = require('../../becca/becca'); const becca = require('../../becca/becca');
function getRecentChanges(req) { function getRecentChanges(req) {

View File

@ -2,7 +2,7 @@
const imageType = require('image-type'); const imageType = require('image-type');
const imageService = require('../../services/image.js'); const imageService = require('../../services/image.js');
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes');
const {sanitizeAttributeName} = require('../../services/sanitize_attribute_name'); const {sanitizeAttributeName} = require('../../services/sanitize_attribute_name');
const specialNotesService = require('../../services/special_notes.js'); const specialNotesService = require('../../services/special_notes.js');

View File

@ -1,5 +1,5 @@
const log = require('./log'); const log = require('./log');
const noteService = require('./notes.js'); const noteService = require('./notes');
const sql = require('./sql'); const sql = require('./sql');
const utils = require('./utils'); const utils = require('./utils');
const attributeService = require('./attributes.js'); const attributeService = require('./attributes.js');

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const noteService = require('./notes.js'); const noteService = require('./notes');
const attributeService = require('./attributes.js'); const attributeService = require('./attributes.js');
const dateUtils = require('./date_utils'); const dateUtils = require('./date_utils');
const sql = require('./sql'); const sql = require('./sql');

View File

@ -65,7 +65,7 @@ function getDateTimeForFile() {
return new Date().toISOString().substr(0, 19).replace(/:/g, ''); return new Date().toISOString().substr(0, 19).replace(/:/g, '');
} }
function validateLocalDateTime(str: string) { function validateLocalDateTime(str: string | null | undefined) {
if (!str) { if (!str) {
return; return;
} }
@ -80,7 +80,7 @@ function validateLocalDateTime(str: string) {
} }
} }
function validateUtcDateTime(str: string) { function validateUtcDateTime(str?: string) {
if (!str) { if (!str) {
return; return;
} }

View File

@ -45,7 +45,7 @@ function putEntityChange(origEntityChange: EntityChange) {
cls.putEntityChange(ec); cls.putEntityChange(ec);
} }
function putNoteReorderingEntityChange(parentNoteId: string, componentId: string) { function putNoteReorderingEntityChange(parentNoteId: string, componentId?: string) {
putEntityChange({ putEntityChange({
entityName: "note_reordering", entityName: "note_reordering",
entityId: parentNoteId, entityId: parentNoteId,

View File

@ -1,10 +1,10 @@
const eventService = require('./events'); const eventService = require('./events');
const scriptService = require('./script.js'); const scriptService = require('./script.js');
const treeService = require('./tree.js'); const treeService = require('./tree.js');
const noteService = require('./notes.js'); const noteService = require('./notes');
const becca = require('../becca/becca'); const becca = require('../becca/becca');
const BAttribute = require('../becca/entities/battribute'); const BAttribute = require('../becca/entities/battribute');
const hiddenSubtreeService = require('./hidden_subtree.js'); const hiddenSubtreeService = require('./hidden_subtree');
const oneTimeTimer = require('./one_time_timer.js'); const oneTimeTimer = require('./one_time_timer.js');
function runAttachedRelations(note, relationName, originEntity) { function runAttachedRelations(note, relationName, originEntity) {

View File

@ -1,8 +1,10 @@
const becca = require('../becca/becca'); import BAttribute = require("../becca/entities/battribute");
const noteService = require('./notes.js'); import { AttributeType, NoteType } from "../becca/entities/rows";
const BAttribute = require('../becca/entities/battribute');
const log = require('./log'); import becca = require('../becca/becca');
const migrationService = require('./migration'); import noteService = require('./notes');
import log = require('./log');
import migrationService = require('./migration');
const LBTPL_ROOT = "_lbTplRoot"; const LBTPL_ROOT = "_lbTplRoot";
const LBTPL_BASE = "_lbTplBase"; const LBTPL_BASE = "_lbTplBase";
@ -13,13 +15,36 @@ const LBTPL_BUILTIN_WIDGET = "_lbTplBuiltinWidget";
const LBTPL_SPACER = "_lbTplSpacer"; const LBTPL_SPACER = "_lbTplSpacer";
const LBTPL_CUSTOM_WIDGET = "_lbTplCustomWidget"; const LBTPL_CUSTOM_WIDGET = "_lbTplCustomWidget";
interface Attribute {
type: AttributeType;
name: string;
isInheritable?: boolean;
value?: string
}
interface Item {
notePosition?: number;
id: string;
title: string;
type: NoteType;
icon?: string;
attributes?: Attribute[];
children?: Item[];
isExpanded?: boolean;
baseSize?: string;
growthFactor?: string;
targetNoteId?: "_backendLog" | "_globalNoteMap";
builtinWidget?: "bookmarks" | "spacer" | "backInHistoryButton" | "forwardInHistoryButton" | "syncStatus" | "protectedSession" | "todayInJournal" | "calendar";
command?: "jumpToNote" | "searchNotes" | "createNoteIntoInbox" | "showRecentChanges";
}
/* /*
* Hidden subtree is generated as a "predictable structure" which means that it avoids generating random IDs to always * Hidden subtree is generated as a "predictable structure" which means that it avoids generating random IDs to always
* produce the same structure. This is needed because it is run on multiple instances in the sync cluster which might produce * produce the same structure. This is needed because it is run on multiple instances in the sync cluster which might produce
* duplicate subtrees. This way, all instances will generate the same structure with the same IDs. * duplicate subtrees. This way, all instances will generate the same structure with the same IDs.
*/ */
const HIDDEN_SUBTREE_DEFINITION = { const HIDDEN_SUBTREE_DEFINITION: Item = {
id: '_hidden', id: '_hidden',
title: 'Hidden Notes', title: 'Hidden Notes',
type: 'doc', type: 'doc',
@ -244,7 +269,7 @@ function checkHiddenSubtree(force = false) {
checkHiddenSubtreeRecursively('root', HIDDEN_SUBTREE_DEFINITION); checkHiddenSubtreeRecursively('root', HIDDEN_SUBTREE_DEFINITION);
} }
function checkHiddenSubtreeRecursively(parentNoteId, item) { function checkHiddenSubtreeRecursively(parentNoteId: string, item: Item) {
if (!item.id || !item.type || !item.title) { if (!item.id || !item.type || !item.title) {
throw new Error(`Item does not contain mandatory properties: ${JSON.stringify(item)}`); throw new Error(`Item does not contain mandatory properties: ${JSON.stringify(item)}`);
} }
@ -337,7 +362,7 @@ function checkHiddenSubtreeRecursively(parentNoteId, item) {
} }
} }
module.exports = { export = {
checkHiddenSubtree, checkHiddenSubtree,
LBTPL_ROOT, LBTPL_ROOT,
LBTPL_BASE, LBTPL_BASE,

View File

@ -47,7 +47,7 @@ function sanitize(dirtyHtml: string) {
}); });
} }
module.exports = { export = {
sanitize, sanitize,
sanitizeUrl: (url: string) => { sanitizeUrl: (url: string) => {
return sanitizeUrl.sanitizeUrl(url).trim(); return sanitizeUrl.sanitizeUrl(url).trim();

View File

@ -3,7 +3,7 @@
const becca = require('../becca/becca'); const becca = require('../becca/becca');
const log = require('./log'); const log = require('./log');
const protectedSessionService = require('./protected_session'); const protectedSessionService = require('./protected_session');
const noteService = require('./notes.js'); const noteService = require('./notes');
const optionService = require('./options'); const optionService = require('./options');
const sql = require('./sql'); const sql = require('./sql');
const jimp = require('jimp'); const jimp = require('jimp');
@ -154,7 +154,7 @@ function saveImageToAttachment(noteId, uploadBuffer, originalName, shrinkImageSw
setTimeout(() => { setTimeout(() => {
sql.transactional(() => { sql.transactional(() => {
const note = becca.getNoteOrThrow(noteId); const note = becca.getNoteOrThrow(noteId);
const noteService = require('../services/notes.js'); const noteService = require('../services/notes');
noteService.asyncPostProcessContent(note, note.getContent()); // to mark an unused attachment for deletion noteService.asyncPostProcessContent(note, note.getContent()); // to mark an unused attachment for deletion
}); });
}, 5000); }, 5000);

View File

@ -4,7 +4,7 @@ const {Throttle} = require('stream-throttle');
const log = require('../log'); const log = require('../log');
const utils = require('../utils'); const utils = require('../utils');
const sql = require('../sql'); const sql = require('../sql');
const noteService = require('../notes.js'); const noteService = require('../notes');
const imageService = require('../image.js'); const imageService = require('../image.js');
const protectedSessionService = require('../protected_session'); const protectedSessionService = require('../protected_session');
const htmlSanitizer = require('../html_sanitizer'); const htmlSanitizer = require('../html_sanitizer');

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes');
const parseString = require('xml2js').parseString; const parseString = require('xml2js').parseString;
const protectedSessionService = require('../protected_session'); const protectedSessionService = require('../protected_session');
const htmlSanitizer = require('../html_sanitizer'); const htmlSanitizer = require('../html_sanitizer');

View File

@ -1,6 +1,6 @@
"use strict"; "use strict";
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes');
const imageService = require('../../services/image.js'); const imageService = require('../../services/image.js');
const protectedSessionService = require('../protected_session'); const protectedSessionService = require('../protected_session');
const markdownService = require('./markdown.js'); const markdownService = require('./markdown.js');

View File

@ -3,7 +3,7 @@
const BAttribute = require('../../becca/entities/battribute'); const BAttribute = require('../../becca/entities/battribute');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const log = require('../../services/log'); const log = require('../../services/log');
const noteService = require('../../services/notes.js'); const noteService = require('../../services/notes');
const attributeService = require('../../services/attributes.js'); const attributeService = require('../../services/attributes.js');
const BBranch = require('../../becca/entities/bbranch'); const BBranch = require('../../becca/entities/bbranch');
const path = require('path'); const path = require('path');

View File

@ -1,52 +1,62 @@
const sql = require('./sql'); import sql = require('./sql');
const optionService = require('./options'); import optionService = require('./options');
const dateUtils = require('./date_utils'); import dateUtils = require('./date_utils');
const entityChangesService = require('./entity_changes'); import entityChangesService = require('./entity_changes');
const eventService = require('./events'); import eventService = require('./events');
const cls = require('../services/cls'); import cls = require('../services/cls');
const protectedSessionService = require('../services/protected_session'); import protectedSessionService = require('../services/protected_session');
const log = require('../services/log'); import log = require('../services/log');
const utils = require('../services/utils'); import utils = require('../services/utils');
const revisionService = require('./revisions'); import revisionService = require('./revisions');
const request = require('./request'); import request = require('./request');
const path = require('path'); import path = require('path');
const url = require('url'); import url = require('url');
const becca = require('../becca/becca'); import becca = require('../becca/becca');
const BBranch = require('../becca/entities/bbranch'); import BBranch = require('../becca/entities/bbranch');
const BNote = require('../becca/entities/bnote'); import BNote = require('../becca/entities/bnote');
const BAttribute = require('../becca/entities/battribute'); import BAttribute = require('../becca/entities/battribute');
const BAttachment = require('../becca/entities/battachment'); import BAttachment = require('../becca/entities/battachment');
const dayjs = require("dayjs"); import dayjs = require("dayjs");
const htmlSanitizer = require('./html_sanitizer'); import htmlSanitizer = require('./html_sanitizer');
const ValidationError = require('../errors/validation_error'); import ValidationError = require('../errors/validation_error');
const noteTypesService = require('./note_types'); import noteTypesService = require('./note_types');
const fs = require("fs"); import fs = require("fs");
const ws = require('./ws'); import ws = require('./ws');
const html2plaintext = require('html2plaintext') import html2plaintext = require('html2plaintext');
import { AttachmentRow, AttributeRow, BranchRow, NoteRow, NoteType } from '../becca/entities/rows';
import TaskContext = require('./task_context');
/** @param {BNote} parentNote */ interface FoundLink {
function getNewNotePosition(parentNote) { name: "imageLink" | "internalLink" | "includeNoteLink" | "relationMapLink",
value: string
}
interface Attachment {
attachmentId?: string;
title: string;
}
function getNewNotePosition(parentNote: BNote) {
if (parentNote.isLabelTruthy('newNotesOnTop')) { if (parentNote.isLabelTruthy('newNotesOnTop')) {
const minNotePos = parentNote.getChildBranches() const minNotePos = parentNote.getChildBranches()
.filter(branch => branch.noteId !== '_hidden') // has "always last" note position .filter(branch => branch?.noteId !== '_hidden') // has "always last" note position
.reduce((min, note) => Math.min(min, note.notePosition), 0); .reduce((min, note) => Math.min(min, note?.notePosition || 0), 0);
return minNotePos - 10; return minNotePos - 10;
} else { } else {
const maxNotePos = parentNote.getChildBranches() const maxNotePos = parentNote.getChildBranches()
.filter(branch => branch.noteId !== '_hidden') // has "always last" note position .filter(branch => branch?.noteId !== '_hidden') // has "always last" note position
.reduce((max, note) => Math.max(max, note.notePosition), 0); .reduce((max, note) => Math.max(max, note?.notePosition || 0), 0);
return maxNotePos + 10; return maxNotePos + 10;
} }
} }
/** @param {BNote} note */ function triggerNoteTitleChanged(note: BNote) {
function triggerNoteTitleChanged(note) {
eventService.emit(eventService.NOTE_TITLE_CHANGED, note); eventService.emit(eventService.NOTE_TITLE_CHANGED, note);
} }
function deriveMime(type, mime) { function deriveMime(type: string, mime?: string) {
if (!type) { if (!type) {
throw new Error(`Note type is a required param`); throw new Error(`Note type is a required param`);
} }
@ -58,11 +68,7 @@ function deriveMime(type, mime) {
return noteTypesService.getDefaultMimeForNoteType(type); return noteTypesService.getDefaultMimeForNoteType(type);
} }
/** function copyChildAttributes(parentNote: BNote, childNote: BNote) {
* @param {BNote} parentNote
* @param {BNote} childNote
*/
function copyChildAttributes(parentNote, childNote) {
for (const attr of parentNote.getAttributes()) { for (const attr of parentNote.getAttributes()) {
if (attr.name.startsWith("child:")) { if (attr.name.startsWith("child:")) {
const name = attr.name.substr(6); const name = attr.name.substr(6);
@ -86,8 +92,7 @@ function copyChildAttributes(parentNote, childNote) {
} }
} }
/** @param {BNote} parentNote */ function getNewNoteTitle(parentNote: BNote) {
function getNewNoteTitle(parentNote) {
let title = "new note"; let title = "new note";
const titleTemplate = parentNote.getLabelValue('titleTemplate'); const titleTemplate = parentNote.getLabelValue('titleTemplate');
@ -101,7 +106,7 @@ function getNewNoteTitle(parentNote) {
// - parentNote // - parentNote
title = eval(`\`${titleTemplate}\``); title = eval(`\`${titleTemplate}\``);
} catch (e) { } catch (e: any) {
log.error(`Title template of note '${parentNote.noteId}' failed with: ${e.message}`); log.error(`Title template of note '${parentNote.noteId}' failed with: ${e.message}`);
} }
} }
@ -114,7 +119,13 @@ function getNewNoteTitle(parentNote) {
return title; return title;
} }
function getAndValidateParent(params) { interface GetValidateParams {
parentNoteId: string;
type: string;
ignoreForbiddenParents?: boolean;
}
function getAndValidateParent(params: GetValidateParams) {
const parentNote = becca.notes[params.parentNoteId]; const parentNote = becca.notes[params.parentNoteId];
if (!parentNote) { if (!parentNote) {
@ -141,24 +152,33 @@ function getAndValidateParent(params) {
return parentNote; return parentNote;
} }
/** interface NoteParams {
* Following object properties are mandatory: /** optionally can force specific noteId */
* - {string} parentNoteId noteId?: string;
* - {string} title parentNoteId: string;
* - {*} content templateNoteId?: string;
* - {string} type - text, code, file, image, search, book, relationMap, canvas, render title: string;
* content: string;
* The following are optional (have defaults) type: NoteType;
* - {string} mime - value is derived from default mimes for type /** default value is derived from default mimes for type */
* - {boolean} isProtected - default is false mime?: string;
* - {boolean} isExpanded - default is false /** default is false */
* - {string} prefix - default is empty string isProtected?: boolean;
* - {int} notePosition - default is the last existing notePosition in a parent + 10 /** default is false */
* isExpanded?: boolean;
* @param params /** default is empty string */
* @returns {{note: BNote, branch: BBranch}} prefix?: string;
*/ /** default is the last existing notePosition in a parent + 10 */
function createNewNote(params) { notePosition?: number;
dateCreated?: string;
utcDateCreated?: string;
ignoreForbiddenParents?: boolean;
}
function createNewNote(params: NoteParams): {
note: BNote;
branch: BBranch;
} {
const parentNote = getAndValidateParent(params); const parentNote = getAndValidateParent(params);
if (params.title === null || params.title === undefined) { if (params.title === null || params.title === undefined) {
@ -209,7 +229,7 @@ function createNewNote(params) {
noteId: note.noteId, noteId: note.noteId,
parentNoteId: params.parentNoteId, parentNoteId: params.parentNoteId,
notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(parentNote), notePosition: params.notePosition !== undefined ? params.notePosition : getNewNotePosition(parentNote),
prefix: params.prefix, prefix: params.prefix || "",
isExpanded: !!params.isExpanded isExpanded: !!params.isExpanded
}).save(); }).save();
} }
@ -253,7 +273,7 @@ function createNewNote(params) {
}); });
} }
function createNewNoteWithTarget(target, targetBranchId, params) { function createNewNoteWithTarget(target: ("into" | "after"), targetBranchId: string, params: NoteParams) {
if (!params.type) { if (!params.type) {
const parentNote = becca.notes[params.parentNoteId]; const parentNote = becca.notes[params.parentNoteId];
@ -285,13 +305,7 @@ function createNewNoteWithTarget(target, targetBranchId, params) {
} }
} }
/** function protectNoteRecursively(note: BNote, protect: boolean, includingSubTree: boolean, taskContext: TaskContext) {
* @param {BNote} note
* @param {boolean} protect
* @param {boolean} includingSubTree
* @param {TaskContext} taskContext
*/
function protectNoteRecursively(note, protect, includingSubTree, taskContext) {
protectNote(note, protect); protectNote(note, protect);
taskContext.increaseProgressCount(); taskContext.increaseProgressCount();
@ -303,11 +317,7 @@ function protectNoteRecursively(note, protect, includingSubTree, taskContext) {
} }
} }
/** function protectNote(note: BNote, protect: boolean) {
* @param {BNote} note
* @param {boolean} protect
*/
function protectNote(note, protect) {
if (!protectedSessionService.isProtectedSessionAvailable()) { if (!protectedSessionService.isProtectedSessionAvailable()) {
throw new Error(`Cannot (un)protect note '${note.noteId}' with protect flag '${protect}' without active protected session`); throw new Error(`Cannot (un)protect note '${note.noteId}' with protect flag '${protect}' without active protected session`);
} }
@ -345,8 +355,8 @@ function protectNote(note, protect) {
} }
} }
function checkImageAttachments(note, content) { function checkImageAttachments(note: BNote, content: string) {
const foundAttachmentIds = new Set(); const foundAttachmentIds = new Set<string>();
let match; let match;
const imgRegExp = /src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image/g; const imgRegExp = /src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image/g;
@ -362,7 +372,7 @@ function checkImageAttachments(note, content) {
const attachments = note.getAttachments(); const attachments = note.getAttachments();
for (const attachment of attachments) { for (const attachment of attachments) {
const attachmentInContent = foundAttachmentIds.has(attachment.attachmentId); const attachmentInContent = attachment.attachmentId && foundAttachmentIds.has(attachment.attachmentId);
if (attachment.utcDateScheduledForErasureSince && attachmentInContent) { if (attachment.utcDateScheduledForErasureSince && attachmentInContent) {
attachment.utcDateScheduledForErasureSince = null; attachment.utcDateScheduledForErasureSince = null;
@ -373,7 +383,7 @@ function checkImageAttachments(note, content) {
} }
} }
const existingAttachmentIds = new Set(attachments.map(att => att.attachmentId)); const existingAttachmentIds = new Set<string | undefined>(attachments.map(att => att.attachmentId));
const unknownAttachmentIds = Array.from(foundAttachmentIds).filter(foundAttId => !existingAttachmentIds.has(foundAttId)); const unknownAttachmentIds = Array.from(foundAttachmentIds).filter(foundAttId => !existingAttachmentIds.has(foundAttId));
const unknownAttachments = becca.getAttachments(unknownAttachmentIds); const unknownAttachments = becca.getAttachments(unknownAttachmentIds);
@ -412,7 +422,7 @@ function checkImageAttachments(note, content) {
}; };
} }
function findImageLinks(content, foundLinks) { function findImageLinks(content: string, foundLinks: FoundLink[]) {
const re = /src="[^"]*api\/images\/([a-zA-Z0-9_]+)\//g; const re = /src="[^"]*api\/images\/([a-zA-Z0-9_]+)\//g;
let match; let match;
@ -428,7 +438,7 @@ function findImageLinks(content, foundLinks) {
return content.replace(/src="[^"]*\/api\/images\//g, 'src="api/images/'); return content.replace(/src="[^"]*\/api\/images\//g, 'src="api/images/');
} }
function findInternalLinks(content, foundLinks) { function findInternalLinks(content: string, foundLinks: FoundLink[]) {
const re = /href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)\/?"/g; const re = /href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)\/?"/g;
let match; let match;
@ -443,7 +453,7 @@ function findInternalLinks(content, foundLinks) {
return content.replace(/href="[^"]*#root/g, 'href="#root'); return content.replace(/href="[^"]*#root/g, 'href="#root');
} }
function findIncludeNoteLinks(content, foundLinks) { function findIncludeNoteLinks(content: string, foundLinks: FoundLink[]) {
const re = /<section class="include-note[^>]+data-note-id="([a-zA-Z0-9_]+)"[^>]*>/g; const re = /<section class="include-note[^>]+data-note-id="([a-zA-Z0-9_]+)"[^>]*>/g;
let match; let match;
@ -457,7 +467,7 @@ function findIncludeNoteLinks(content, foundLinks) {
return content; return content;
} }
function findRelationMapLinks(content, foundLinks) { function findRelationMapLinks(content: string, foundLinks: FoundLink[]) {
const obj = JSON.parse(content); const obj = JSON.parse(content);
for (const note of obj.notes) { for (const note of obj.notes) {
@ -468,9 +478,9 @@ function findRelationMapLinks(content, foundLinks) {
} }
} }
const imageUrlToAttachmentIdMapping = {}; const imageUrlToAttachmentIdMapping: Record<string, string> = {};
async function downloadImage(noteId, imageUrl) { async function downloadImage(noteId: string, imageUrl: string) {
const unescapedUrl = utils.unescapeHtml(imageUrl); const unescapedUrl = utils.unescapeHtml(imageUrl);
try { try {
@ -493,7 +503,7 @@ async function downloadImage(noteId, imageUrl) {
} }
const parsedUrl = url.parse(unescapedUrl); const parsedUrl = url.parse(unescapedUrl);
const title = path.basename(parsedUrl.pathname); const title = path.basename(parsedUrl.pathname || "");
const imageService = require('../services/image.js'); const imageService = require('../services/image.js');
const attachment = imageService.saveImageToAttachment(noteId, imageBuffer, title, true, true); const attachment = imageService.saveImageToAttachment(noteId, imageBuffer, title, true, true);
@ -502,21 +512,21 @@ async function downloadImage(noteId, imageUrl) {
log.info(`Download of '${imageUrl}' succeeded and was saved as image attachment '${attachment.attachmentId}' of note '${noteId}'`); log.info(`Download of '${imageUrl}' succeeded and was saved as image attachment '${attachment.attachmentId}' of note '${noteId}'`);
} }
catch (e) { catch (e: any) {
log.error(`Download of '${imageUrl}' for note '${noteId}' failed with error: ${e.message} ${e.stack}`); log.error(`Download of '${imageUrl}' for note '${noteId}' failed with error: ${e.message} ${e.stack}`);
} }
} }
/** url => download promise */ /** url => download promise */
const downloadImagePromises = {}; const downloadImagePromises: Record<string, Promise<void>> = {};
function replaceUrl(content, url, attachment) { function replaceUrl(content: string, url: string, attachment: Attachment) {
const quotedUrl = utils.quoteRegex(url); const quotedUrl = utils.quoteRegex(url);
return content.replace(new RegExp(`\\s+src=[\"']${quotedUrl}[\"']`, "ig"), ` src="api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(attachment.title)}"`); return content.replace(new RegExp(`\\s+src=[\"']${quotedUrl}[\"']`, "ig"), ` src="api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(attachment.title)}"`);
} }
function downloadImages(noteId, content) { function downloadImages(noteId: string, content: string) {
const imageRe = /<img[^>]*?\ssrc=['"]([^'">]+)['"]/ig; const imageRe = /<img[^>]*?\ssrc=['"]([^'">]+)['"]/ig;
let imageMatch; let imageMatch;
@ -589,6 +599,11 @@ function downloadImages(noteId, content) {
const origContent = origNote.getContent(); const origContent = origNote.getContent();
let updatedContent = origContent; let updatedContent = origContent;
if (typeof updatedContent !== "string") {
log.error(`Note '${noteId}' has a non-string content, cannot replace image link.`);
return;
}
for (const url in imageUrlToAttachmentIdMapping) { for (const url in imageUrlToAttachmentIdMapping) {
const imageNote = imageNotes.find(note => note.noteId === imageUrlToAttachmentIdMapping[url]); const imageNote = imageNotes.find(note => note.noteId === imageUrlToAttachmentIdMapping[url]);
@ -612,11 +627,7 @@ function downloadImages(noteId, content) {
return content; return content;
} }
/** function saveAttachments(note: BNote, content: string) {
* @param {BNote} note
* @param {string} content
*/
function saveAttachments(note, content) {
const inlineAttachmentRe = /<a[^>]*?\shref=['"]data:([^;'">]+);base64,([^'">]+)['"][^>]*>(.*?)<\/a>/igm; const inlineAttachmentRe = /<a[^>]*?\shref=['"]data:([^;'">]+);base64,([^'">]+)['"][^>]*>(.*?)<\/a>/igm;
let attachmentMatch; let attachmentMatch;
@ -645,11 +656,7 @@ function saveAttachments(note, content) {
return content; return content;
} }
/** function saveLinks(note: BNote, content: string) {
* @param {BNote} note
* @param {string} content
*/
function saveLinks(note, content) {
if ((note.type !== 'text' && note.type !== 'relationMap') if ((note.type !== 'text' && note.type !== 'relationMap')
|| (note.isProtected && !protectedSessionService.isProtectedSessionAvailable())) { || (note.isProtected && !protectedSessionService.isProtectedSessionAvailable())) {
return { return {
@ -658,7 +665,7 @@ function saveLinks(note, content) {
}; };
} }
const foundLinks = []; const foundLinks: FoundLink[] = [];
let forceFrontendReload = false; let forceFrontendReload = false;
if (note.type === 'text') { if (note.type === 'text') {
@ -716,8 +723,7 @@ function saveLinks(note, content) {
return { forceFrontendReload, content }; return { forceFrontendReload, content };
} }
/** @param {BNote} note */ function saveRevisionIfNeeded(note: BNote) {
function saveRevisionIfNeeded(note) {
// files and images are versioned separately // files and images are versioned separately
if (note.type === 'file' || note.type === 'image' || note.isLabelTruthy('disableVersioning')) { if (note.type === 'file' || note.type === 'image' || note.isLabelTruthy('disableVersioning')) {
return; return;
@ -738,10 +744,10 @@ function saveRevisionIfNeeded(note) {
} }
} }
function updateNoteData(noteId, content, attachments = []) { function updateNoteData(noteId: string, content: string, attachments: BAttachment[] = []) {
const note = becca.getNote(noteId); const note = becca.getNote(noteId);
if (!note.isContentAvailable()) { if (!note || !note.isContentAvailable()) {
throw new Error(`Note '${noteId}' is not available for change!`); throw new Error(`Note '${noteId}' is not available for change!`);
} }
@ -752,10 +758,13 @@ function updateNoteData(noteId, content, attachments = []) {
note.setContent(newContent, { forceFrontendReload }); note.setContent(newContent, { forceFrontendReload });
if (attachments?.length > 0) { if (attachments?.length > 0) {
/** @var {Object<string, BAttachment>} */
const existingAttachmentsByTitle = utils.toMap(note.getAttachments({includeContentLength: false}), 'title'); const existingAttachmentsByTitle = utils.toMap(note.getAttachments({includeContentLength: false}), 'title');
for (const {attachmentId, role, mime, title, content, position} of attachments) { for (const attachment of attachments) {
// FIXME: The content property was extracted directly instead of `getContent`. To investigate.
const {attachmentId, role, mime, title, position} = attachment;
const content = attachment.getContent();
if (attachmentId || !(title in existingAttachmentsByTitle)) { if (attachmentId || !(title in existingAttachmentsByTitle)) {
note.saveAttachment({attachmentId, role, mime, title, content, position}); note.saveAttachment({attachmentId, role, mime, title, content, position});
} else { } else {
@ -769,12 +778,8 @@ function updateNoteData(noteId, content, attachments = []) {
} }
} }
/** function undeleteNote(noteId: string, taskContext: TaskContext) {
* @param {string} noteId const noteRow = sql.getRow<NoteRow>("SELECT * FROM notes WHERE noteId = ?", [noteId]);
* @param {TaskContext} taskContext
*/
function undeleteNote(noteId, taskContext) {
const noteRow = sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
if (!noteRow.isDeleted) { if (!noteRow.isDeleted) {
log.error(`Note '${noteId}' is not deleted and thus cannot be undeleted.`); log.error(`Note '${noteId}' is not deleted and thus cannot be undeleted.`);
@ -793,19 +798,14 @@ function undeleteNote(noteId, taskContext) {
} }
} }
/** function undeleteBranch(branchId: string, deleteId: string, taskContext: TaskContext) {
* @param {string} branchId const branchRow = sql.getRow<BranchRow>("SELECT * FROM branches WHERE branchId = ?", [branchId])
* @param {string} deleteId
* @param {TaskContext} taskContext
*/
function undeleteBranch(branchId, deleteId, taskContext) {
const branchRow = sql.getRow("SELECT * FROM branches WHERE branchId = ?", [branchId])
if (!branchRow.isDeleted) { if (!branchRow.isDeleted) {
return; return;
} }
const noteRow = sql.getRow("SELECT * FROM notes WHERE noteId = ?", [branchRow.noteId]); const noteRow = sql.getRow<NoteRow>("SELECT * FROM notes WHERE noteId = ?", [branchRow.noteId]);
if (noteRow.isDeleted && noteRow.deleteId !== deleteId) { if (noteRow.isDeleted && noteRow.deleteId !== deleteId) {
return; return;
@ -818,10 +818,14 @@ function undeleteBranch(branchId, deleteId, taskContext) {
if (noteRow.isDeleted && noteRow.deleteId === deleteId) { if (noteRow.isDeleted && noteRow.deleteId === deleteId) {
// becca entity was already created as skeleton in "new Branch()" above // becca entity was already created as skeleton in "new Branch()" above
const noteEntity = becca.getNote(noteRow.noteId); const noteEntity = becca.getNote(noteRow.noteId);
if (!noteEntity) {
throw new Error("Unable to find the just restored branch.");
}
noteEntity.updateFromRow(noteRow); noteEntity.updateFromRow(noteRow);
noteEntity.save(); noteEntity.save();
const attributeRows = sql.getRows(` const attributeRows = sql.getRows<AttributeRow>(`
SELECT * FROM attributes SELECT * FROM attributes
WHERE isDeleted = 1 WHERE isDeleted = 1
AND deleteId = ? AND deleteId = ?
@ -830,10 +834,11 @@ function undeleteBranch(branchId, deleteId, taskContext) {
for (const attributeRow of attributeRows) { for (const attributeRow of attributeRows) {
// relation might point to a note which hasn't been undeleted yet and would thus throw up // relation might point to a note which hasn't been undeleted yet and would thus throw up
// FIXME: skipValidation is not used.
new BAttribute(attributeRow).save({skipValidation: true}); new BAttribute(attributeRow).save({skipValidation: true});
} }
const attachmentRows = sql.getRows(` const attachmentRows = sql.getRows<AttachmentRow>(`
SELECT * FROM attachments SELECT * FROM attachments
WHERE isDeleted = 1 WHERE isDeleted = 1
AND deleteId = ? AND deleteId = ?
@ -843,7 +848,7 @@ function undeleteBranch(branchId, deleteId, taskContext) {
new BAttachment(attachmentRow).save(); new BAttachment(attachmentRow).save();
} }
const childBranchIds = sql.getColumn(` const childBranchIds = sql.getColumn<string>(`
SELECT branches.branchId SELECT branches.branchId
FROM branches FROM branches
WHERE branches.isDeleted = 1 WHERE branches.isDeleted = 1
@ -859,8 +864,8 @@ function undeleteBranch(branchId, deleteId, taskContext) {
/** /**
* @returns return deleted branchIds of an undeleted parent note * @returns return deleted branchIds of an undeleted parent note
*/ */
function getUndeletedParentBranchIds(noteId, deleteId) { function getUndeletedParentBranchIds(noteId: string, deleteId: string) {
return sql.getColumn(` return sql.getColumn<string>(`
SELECT branches.branchId SELECT branches.branchId
FROM branches FROM branches
JOIN notes AS parentNote ON parentNote.noteId = branches.parentNoteId JOIN notes AS parentNote ON parentNote.noteId = branches.parentNoteId
@ -870,7 +875,7 @@ function getUndeletedParentBranchIds(noteId, deleteId) {
AND parentNote.isDeleted = 0`, [noteId, deleteId]); AND parentNote.isDeleted = 0`, [noteId, deleteId]);
} }
function scanForLinks(note, content) { function scanForLinks(note: BNote | null, content: string) {
if (!note || !['text', 'relationMap'].includes(note.type)) { if (!note || !['text', 'relationMap'].includes(note.type)) {
return; return;
} }
@ -884,17 +889,15 @@ function scanForLinks(note, content) {
} }
}); });
} }
catch (e) { catch (e: any) {
log.error(`Could not scan for links note '${note.noteId}': ${e.message} ${e.stack}`); log.error(`Could not scan for links note '${note.noteId}': ${e.message} ${e.stack}`);
} }
} }
/** /**
* @param {BNote} note
* @param {string} content
* Things which have to be executed after updating content, but asynchronously (separate transaction) * Things which have to be executed after updating content, but asynchronously (separate transaction)
*/ */
async function asyncPostProcessContent(note, content) { async function asyncPostProcessContent(note: BNote, content: string) {
if (cls.isMigrationRunning()) { if (cls.isMigrationRunning()) {
// this is rarely needed for migrations, but can cause trouble by e.g. triggering downloads // this is rarely needed for migrations, but can cause trouble by e.g. triggering downloads
return; return;
@ -908,7 +911,7 @@ async function asyncPostProcessContent(note, content) {
} }
// all keys should be replaced by the corresponding values // all keys should be replaced by the corresponding values
function replaceByMap(str, mapObj) { function replaceByMap(str: string, mapObj: Record<string, string>) {
if (!mapObj) { if (!mapObj) {
return str; return str;
} }
@ -918,7 +921,7 @@ function replaceByMap(str, mapObj) {
return str.replace(re, matched => mapObj[matched]); return str.replace(re, matched => mapObj[matched]);
} }
function duplicateSubtree(origNoteId, newParentNoteId) { function duplicateSubtree(origNoteId: string, newParentNoteId: string) {
if (origNoteId === 'root') { if (origNoteId === 'root') {
throw new Error('Duplicating root is not possible'); throw new Error('Duplicating root is not possible');
} }
@ -931,6 +934,10 @@ function duplicateSubtree(origNoteId, newParentNoteId) {
const noteIdMapping = getNoteIdMapping(origNote); const noteIdMapping = getNoteIdMapping(origNote);
if (!origBranch) {
throw new Error("Unable to find original branch to duplicate.");
}
const res = duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping); const res = duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping);
if (!res.note.title.endsWith('(dup)')) { if (!res.note.title.endsWith('(dup)')) {
@ -942,20 +949,25 @@ function duplicateSubtree(origNoteId, newParentNoteId) {
return res; return res;
} }
function duplicateSubtreeWithoutRoot(origNoteId, newNoteId) { function duplicateSubtreeWithoutRoot(origNoteId: string, newNoteId: string) {
if (origNoteId === 'root') { if (origNoteId === 'root') {
throw new Error('Duplicating root is not possible'); throw new Error('Duplicating root is not possible');
} }
const origNote = becca.getNote(origNoteId); const origNote = becca.getNote(origNoteId);
const noteIdMapping = getNoteIdMapping(origNote); if (origNote == null) {
throw new Error("Unable to find note to duplicate.");
}
const noteIdMapping = getNoteIdMapping(origNote);
for (const childBranch of origNote.getChildBranches()) { for (const childBranch of origNote.getChildBranches()) {
if (childBranch) {
duplicateSubtreeInner(childBranch.getNote(), childBranch, newNoteId, noteIdMapping); duplicateSubtreeInner(childBranch.getNote(), childBranch, newNoteId, noteIdMapping);
} }
} }
}
function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapping) { function duplicateSubtreeInner(origNote: BNote, origBranch: BBranch, newParentNoteId: string, noteIdMapping: Record<string, string>) {
if (origNote.isProtected && !protectedSessionService.isProtectedSessionAvailable()) { if (origNote.isProtected && !protectedSessionService.isProtectedSessionAvailable()) {
throw new Error(`Cannot duplicate note '${origNote.noteId}' because it is protected and protected session is not available. Enter protected session and try again.`); throw new Error(`Cannot duplicate note '${origNote.noteId}' because it is protected and protected session is not available. Enter protected session and try again.`);
} }
@ -981,7 +993,7 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp
let content = origNote.getContent(); let content = origNote.getContent();
if (['text', 'relationMap', 'search'].includes(origNote.type)) { if (typeof content === "string" && ['text', 'relationMap', 'search'].includes(origNote.type)) {
// fix links in the content // fix links in the content
content = replaceByMap(content, noteIdMapping); content = replaceByMap(content, noteIdMapping);
} }
@ -1002,12 +1014,15 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp
} }
// the relation targets may not be created yet, the mapping is pre-generated // the relation targets may not be created yet, the mapping is pre-generated
attr.save({skipValidation: true}); // FIXME: This used to be `attr.save({skipValidation: true});`, but skipValidation is in beforeSaving.
attr.save();
} }
for (const childBranch of origNote.getChildBranches()) { for (const childBranch of origNote.getChildBranches()) {
if (childBranch) {
duplicateSubtreeInner(childBranch.getNote(), childBranch, newNote.noteId, noteIdMapping); duplicateSubtreeInner(childBranch.getNote(), childBranch, newNote.noteId, noteIdMapping);
} }
}
return newNote; return newNote;
} }
@ -1031,8 +1046,8 @@ function duplicateSubtreeInner(origNote, origBranch, newParentNoteId, noteIdMapp
} }
} }
function getNoteIdMapping(origNote) { function getNoteIdMapping(origNote: BNote) {
const noteIdMapping = {}; const noteIdMapping: Record<string, string> = {};
// pregenerate new noteIds since we'll need to fix relation references even for not yet created notes // pregenerate new noteIds since we'll need to fix relation references even for not yet created notes
for (const origNoteId of origNote.getDescendantNoteIds()) { for (const origNoteId of origNote.getDescendantNoteIds()) {
@ -1042,7 +1057,7 @@ function getNoteIdMapping(origNote) {
return noteIdMapping; return noteIdMapping;
} }
module.exports = { export = {
createNewNote, createNewNote,
createNewNoteWithTarget, createNewNoteWithTarget,
updateNoteData, updateNoteData,

View File

@ -44,6 +44,6 @@ function protectRevisions(note: BNote) {
} }
} }
module.exports = { export = {
protectRevisions protectRevisions
}; };

View File

@ -5,7 +5,7 @@ const config = require('./config');
const log = require('./log'); const log = require('./log');
const attributeService = require('../services/attributes.js'); const attributeService = require('../services/attributes.js');
const protectedSessionService = require('../services/protected_session'); const protectedSessionService = require('../services/protected_session');
const hiddenSubtreeService = require('./hidden_subtree.js'); const hiddenSubtreeService = require('./hidden_subtree');
/** /**
* @param {BNote} note * @param {BNote} note

View File

@ -1,13 +1,13 @@
const attributeService = require('./attributes.js'); const attributeService = require('./attributes.js');
const dateNoteService = require('./date_notes.js'); const dateNoteService = require('./date_notes.js');
const becca = require('../becca/becca'); const becca = require('../becca/becca');
const noteService = require('./notes.js'); const noteService = require('./notes');
const dateUtils = require('./date_utils'); const dateUtils = require('./date_utils');
const log = require('./log'); const log = require('./log');
const hoistedNoteService = require('./hoisted_note.js'); const hoistedNoteService = require('./hoisted_note.js');
const searchService = require('./search/services/search.js'); const searchService = require('./search/services/search.js');
const SearchContext = require('./search/search_context.js'); const SearchContext = require('./search/search_context.js');
const {LBTPL_NOTE_LAUNCHER, LBTPL_CUSTOM_WIDGET, LBTPL_SPACER, LBTPL_SCRIPT} = require('./hidden_subtree.js'); const {LBTPL_NOTE_LAUNCHER, LBTPL_CUSTOM_WIDGET, LBTPL_SPACER, LBTPL_SCRIPT} = require('./hidden_subtree');
function getInboxNote(date) { function getInboxNote(date) {
const workspaceNote = hoistedNoteService.getWorkspaceNote(); const workspaceNote = hoistedNoteService.getWorkspaceNote();

View File

@ -5,7 +5,7 @@
require('../becca/entity_constructor'); require('../becca/entity_constructor');
const sqlInit = require('../services/sql_init'); const sqlInit = require('../services/sql_init');
const noteService = require('../services/notes.js'); const noteService = require('../services/notes');
const attributeService = require('../services/attributes.js'); const attributeService = require('../services/attributes.js');
const cls = require('../services/cls'); const cls = require('../services/cls');
const cloningService = require('../services/cloning.js'); const cloningService = require('../services/cloning.js');

14
src/types.d.ts vendored Normal file
View File

@ -0,0 +1,14 @@
/*
* This file contains type definitions for libraries that did not have one
* in its library or in `@types/*` packages.
*/
declare module 'unescape' {
function unescape(str: string, type?: string): string;
export = unescape;
}
declare module 'html2plaintext' {
function html2plaintext(htmlText: string): string;
export = html2plaintext;
}

View File

@ -1,4 +0,0 @@
declare module 'unescape' {
function unescape(str: string, type?: string): string;
export = unescape;
}

View File

@ -19,6 +19,6 @@
"files": true "files": true
}, },
"files": [ "files": [
"src/types/unescape.d.ts" "src/types.d.ts"
] ]
} }