refactorings

This commit is contained in:
zadam
2023-04-14 16:49:06 +02:00
parent 34ecd77bd4
commit f01657e1dd
20 changed files with 336 additions and 393 deletions

View File

@@ -23,12 +23,8 @@ function isHoistedInHiddenSubtree() {
return hoistedNote.isHiddenCompletely();
}
function getHoistedNote() {
return becca.getNote(cls.getHoistedNoteId());
}
function getWorkspaceNote() {
const hoistedNote = getHoistedNote();
const hoistedNote = becca.getNote(cls.getHoistedNoteId());
if (hoistedNote.isRoot() || hoistedNote.hasLabel('workspace')) {
return hoistedNote;
@@ -39,7 +35,6 @@ function getWorkspaceNote() {
module.exports = {
getHoistedNoteId,
getHoistedNote,
getWorkspaceNote,
isHoistedInHiddenSubtree
};

View File

@@ -2,35 +2,32 @@
const log = require('./log');
const sql = require('./sql');
const protectedSession = require("./protected_session");
const protectedSessionService = require("./protected_session");
/**
* @param {BNote} note
*/
function protectNoteRevisions(note) {
if (!protectedSessionService.isProtectedSessionAvailable()) {
throw new Error(`Cannot (un)protect revisions of note '${note.noteId}' without active protected session`);
}
for (const revision of note.getNoteRevisions()) {
if (note.isProtected !== revision.isProtected) {
if (!protectedSession.isProtectedSessionAvailable()) {
log.error("Protected session is not available to fix note revisions.");
if (note.isProtected === revision.isProtected) {
continue;
}
return;
}
try {
const content = revision.getContent();
try {
const content = revision.getContent();
revision.isProtected = note.isProtected;
revision.isProtected = note.isProtected;
// this will force de/encryption
revision.setContent(content, {forceSave: true});
} catch (e) {
log.error(`Could not un/protect note revision '${revision.noteRevisionId}'`);
// this will force de/encryption
revision.setContent(content);
revision.save();
}
catch (e) {
log.error(`Could not un/protect note revision ID = ${revision.noteRevisionId}`);
throw e;
}
throw e;
}
}
}

View File

@@ -9,7 +9,6 @@ const protectedSessionService = require('../services/protected_session');
const log = require('../services/log');
const utils = require('../services/utils');
const noteRevisionService = require('../services/note_revisions');
const attributeService = require('../services/attributes');
const request = require('./request');
const path = require('path');
const url = require('url');
@@ -21,8 +20,8 @@ const dayjs = require("dayjs");
const htmlSanitizer = require("./html_sanitizer");
const ValidationError = require("../errors/validation_error");
const noteTypesService = require("./note_types");
const {attach} = require("jsdom/lib/jsdom/living/helpers/svg/basic-types.js");
/** @param {BNote} parentNote */
function getNewNotePosition(parentNote) {
if (parentNote.isLabelTruthy('newNotesOnTop')) {
const minNotePos = parentNote.getChildBranches()
@@ -37,6 +36,7 @@ function getNewNotePosition(parentNote) {
}
}
/** @param {BNote} note */
function triggerNoteTitleChanged(note) {
eventService.emit(eventService.NOTE_TITLE_CHANGED, note);
}
@@ -53,6 +53,10 @@ function deriveMime(type, mime) {
return noteTypesService.getDefaultMimeForNoteType(type);
}
/**
* @param {BNote} parentNote
* @param {BNote} childNote
*/
function copyChildAttributes(parentNote, childNote) {
const hasAlreadyTemplate = childNote.hasRelation('template');
@@ -78,6 +82,7 @@ function copyChildAttributes(parentNote, childNote) {
}
}
/** @param {BNote} parentNote */
function getNewNoteTitle(parentNote) {
let title = "new note";
@@ -278,6 +283,12 @@ function createNewNoteWithTarget(target, targetBranchId, params) {
}
}
/**
* @param {BNote} note
* @param {boolean} protect
* @param {boolean} includingSubTree
* @param {TaskContext} taskContext
*/
function protectNoteRecursively(note, protect, includingSubTree, taskContext) {
protectNote(note, protect);
@@ -290,7 +301,15 @@ function protectNoteRecursively(note, protect, includingSubTree, taskContext) {
}
}
/**
* @param {BNote} note
* @param {boolean} protect
*/
function protectNote(note, protect) {
if (!protectedSessionService.isProtectedSessionAvailable()) {
throw new Error(`Cannot (un)protect note '${note.noteId}' with protect flag '${protect}' without active protected session`);
}
try {
if (protect !== note.isProtected) {
const content = note.getContent();
@@ -310,7 +329,7 @@ function protectNote(note, protect) {
noteRevisionService.protectNoteRevisions(note);
}
catch (e) {
log.error(`Could not un/protect note ID = ${note.noteId}`);
log.error(`Could not un/protect note '${note.noteId}'`);
throw e;
}
@@ -565,6 +584,7 @@ function saveLinks(note, content) {
return content;
}
/** @param {BNote} note */
function saveNoteRevisionIfNeeded(note) {
// files and images are versioned separately
if (note.type === 'file' || note.type === 'image' || note.hasLabel('disableVersioning')) {
@@ -709,6 +729,8 @@ function scanForLinks(note, content) {
}
/**
* @param {BNote} note
* @param {string} content
* Things which have to be executed after updating content, but asynchronously (separate transaction)
*/
async function asyncPostProcessContent(note, content) {

View File

@@ -4,7 +4,6 @@ const becca = require("../becca/becca");
const noteService = require("./notes");
const dateUtils = require("./date_utils");
const log = require("./log");
const hiddenSubtreeService = require("./hidden_subtree");
const hoistedNoteService = require("./hoisted_note");
const searchService = require("./search/services/search");
const SearchContext = require("./search/search_context");

View File

@@ -4,31 +4,8 @@ const sql = require('./sql');
const log = require('./log');
const BBranch = require('../becca/entities/bbranch');
const entityChangesService = require('./entity_changes');
const protectedSessionService = require('./protected_session');
const becca = require('../becca/becca');
function getNotes(noteIds) {
// we return also deleted notes which have been specifically asked for
const notes = sql.getManyRows(`
SELECT
noteId,
title,
isProtected,
type,
mime,
isDeleted
FROM notes
WHERE noteId IN (???)`, noteIds);
protectedSessionService.decryptNotes(notes);
notes.forEach(note => {
note.isProtected = !!note.isProtected
});
return notes;
}
function validateParentChild(parentNoteId, childNoteId, branchId = null) {
if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) {
return { success: false, message: `Cannot change this note's location.`};
@@ -39,7 +16,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
return { success: false, message: `Cannot move anything into 'none' parent.` };
}
const existing = getExistingBranch(parentNoteId, childNoteId);
const existing = becca.getBranchFromChildAndParent(childNoteId, parentNoteId);
if (existing && (branchId === null || existing.branchId !== branchId)) {
const parentNote = becca.getNote(parentNoteId);
@@ -51,7 +28,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
};
}
if (!checkTreeCycle(parentNoteId, childNoteId)) {
if (wouldAddingBranchCreateCycle(parentNoteId, childNoteId)) {
return {
success: false,
message: 'Moving/cloning note here would create cycle.'
@@ -68,59 +45,22 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
return { success: true };
}
function getExistingBranch(parentNoteId, childNoteId) {
const branchId = sql.getValue(`
SELECT branchId
FROM branches
WHERE noteId = ?
AND parentNoteId = ?
AND isDeleted = 0`, [childNoteId, parentNoteId]);
return becca.getBranch(branchId);
}
/**
* Tree cycle can be created when cloning or when moving existing clone. This method should detect both cases.
*/
function checkTreeCycle(parentNoteId, childNoteId) {
const subtreeNoteIds = [];
function wouldAddingBranchCreateCycle(parentNoteId, childNoteId) {
const childNote = becca.getNote(childNoteId);
const parentNote = becca.getNote(parentNoteId);
if (!childNote || !parentNote) {
return false;
}
// we'll load the whole subtree - because the cycle can start in one of the notes in the subtree
loadSubtreeNoteIds(childNoteId, subtreeNoteIds);
const childSubtreeNoteIds = new Set(childNote.getSubtreeNoteIds());
const parentAncestorNoteIds = parentNote.getAncestorNoteIds();
function checkTreeCycleInner(parentNoteId) {
if (parentNoteId === 'root') {
return true;
}
if (subtreeNoteIds.includes(parentNoteId)) {
// while towards the root of the tree we encountered noteId which is already present in the subtree
// joining parentNoteId with childNoteId would then clearly create a cycle
return false;
}
const parentNoteIds = sql.getColumn("SELECT DISTINCT parentNoteId FROM branches WHERE noteId = ? AND isDeleted = 0", [parentNoteId]);
for (const pid of parentNoteIds) {
if (!checkTreeCycleInner(pid)) {
return false;
}
}
return true;
}
return checkTreeCycleInner(parentNoteId);
}
function loadSubtreeNoteIds(parentNoteId, subtreeNoteIds) {
subtreeNoteIds.push(parentNoteId);
const children = sql.getColumn("SELECT noteId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
for (const childNoteId of children) {
loadSubtreeNoteIds(childNoteId, subtreeNoteIds);
}
return parentAncestorNoteIds.some(parentAncestorNoteId => childSubtreeNoteIds.has(parentAncestorNoteId));
}
function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, foldersFirst = false, sortNatural = false, sortLocale) {
@@ -295,7 +235,6 @@ function setNoteToParent(noteId, prefix, parentNoteId) {
}
module.exports = {
getNotes,
validateParentChild,
sortNotes,
sortNotesIfNeeded,