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

@@ -1,7 +1,6 @@
const becca = require("../../becca/becca");
const NotFoundError = require("../../errors/not_found_error");
const utils = require("../../services/utils");
const noteService = require("../../services/notes");
function getAttachments(req) {
const includeContent = req.query.includeContent === 'true';
@@ -19,18 +18,12 @@ function getAttachments(req) {
function getAttachment(req) {
const includeContent = req.query.includeContent === 'true';
const {noteId, attachmentId} = req.params;
const {attachmentId} = req.params;
const note = becca.getNote(noteId);
if (!note) {
throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
}
const attachment = note.getAttachmentById(attachmentId);
const attachment = becca.getAttachment(attachmentId);
if (!attachment) {
throw new NotFoundError(`Attachment '${attachmentId} of note '${noteId}' doesn't exist.`);
throw new NotFoundError(`Attachment '${attachmentId}' doesn't exist.`);
}
return processAttachment(attachment, includeContent);
@@ -72,15 +65,9 @@ function saveAttachment(req) {
}
function deleteAttachment(req) {
const {noteId, attachmentId} = req.params;
const {attachmentId} = req.params;
const note = becca.getNote(noteId);
if (!note) {
throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
}
const attachment = note.getAttachmentById(attachmentId);
const attachment = becca.getAttachment(attachmentId);
if (attachment) {
attachment.markAsDeleted();
@@ -88,15 +75,15 @@ function deleteAttachment(req) {
}
function convertAttachmentToNote(req) {
const {noteId, attachmentId} = req.params;
const {attachmentId} = req.params;
const note = becca.getNote(noteId);
const attachment = becca.getAttachment(attachmentId);
if (!note) {
throw new NotFoundError(`Note '${noteId}' doesn't exist.`);
if (!attachment) {
throw new NotFoundError(`Attachment '${attachmentId}' doesn't exist.`);
}
return note.convertAttachmentToChildNote(attachmentId);
return attachment.convertToNote();
}
module.exports = {

View File

@@ -64,13 +64,7 @@ function getRevisionFilename(noteRevision) {
function downloadNoteRevision(req, res) {
const noteRevision = becca.getNoteRevision(req.params.noteRevisionId);
if (noteRevision.noteId !== req.params.noteId) {
return res.setHeader("Content-Type", "text/plain")
.status(400)
.send(`Note revision ${req.params.noteRevisionId} does not belong to note ${req.params.noteId}`);
}
if (noteRevision.isProtected && !protectedSessionService.isProtectedSessionAvailable()) {
if (!noteRevision.isContentAvailable()) {
return res.setHeader("Content-Type", "text/plain")
.status(401)
.send("Protected session not available");

View File

@@ -127,69 +127,6 @@ function setNoteTypeMime(req) {
note.save();
}
function getRelationMap(req) {
const {relationMapNoteId, noteIds} = req.body;
const resp = {
// noteId => title
noteTitles: {},
relations: [],
// relation name => inverse relation name
inverseRelations: {
'internalLink': 'internalLink'
}
};
if (noteIds.length === 0) {
return resp;
}
const questionMarks = noteIds.map(noteId => '?').join(',');
const relationMapNote = becca.getNote(relationMapNoteId);
const displayRelationsVal = relationMapNote.getLabelValue('displayRelations');
const displayRelations = !displayRelationsVal ? [] : displayRelationsVal
.split(",")
.map(token => token.trim());
const hideRelationsVal = relationMapNote.getLabelValue('hideRelations');
const hideRelations = !hideRelationsVal ? [] : hideRelationsVal
.split(",")
.map(token => token.trim());
const foundNoteIds = sql.getColumn(`SELECT noteId FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds);
const notes = becca.getNotes(foundNoteIds);
for (const note of notes) {
resp.noteTitles[note.noteId] = note.title;
resp.relations = resp.relations.concat(note.getRelations()
.filter(relation => !relation.isAutoLink() || displayRelations.includes(relation.name))
.filter(relation => displayRelations.length > 0
? displayRelations.includes(relation.name)
: !hideRelations.includes(relation.name))
.filter(relation => noteIds.includes(relation.value))
.map(relation => ({
attributeId: relation.attributeId,
sourceNoteId: relation.noteId,
targetNoteId: relation.value,
name: relation.name
})));
for (const relationDefinition of note.getRelationDefinitions()) {
const def = relationDefinition.getDefinition();
if (def.inverseRelation) {
resp.inverseRelations[relationDefinition.getDefinedName()] = def.inverseRelation;
resp.inverseRelations[def.inverseRelation] = relationDefinition.getDefinedName();
}
}
}
return resp;
}
function changeTitle(req) {
const noteId = req.params.noteId;
const title = req.body.title;
@@ -273,6 +210,7 @@ function getDeleteNotesPreview(req) {
if (noteIdsToBeDeleted.size > 0) {
sql.fillParamList(noteIdsToBeDeleted);
// FIXME: No need to do this in database, can be done with becca data
brokenRelations = sql.getRows(`
SELECT attr.noteId, attr.name, attr.value
FROM attributes attr
@@ -334,7 +272,6 @@ module.exports = {
sortChildNotes,
protectNote,
setNoteTypeMime,
getRelationMap,
changeTitle,
duplicateSubtree,
eraseDeletedNotesNow,

View File

@@ -0,0 +1,69 @@
const becca = require("../../becca/becca.js");
const sql = require("../../services/sql.js");
function getRelationMap(req) {
const {relationMapNoteId, noteIds} = req.body;
const resp = {
// noteId => title
noteTitles: {},
relations: [],
// relation name => inverse relation name
inverseRelations: {
'internalLink': 'internalLink'
}
};
if (noteIds.length === 0) {
return resp;
}
const questionMarks = noteIds.map(noteId => '?').join(',');
const relationMapNote = becca.getNote(relationMapNoteId);
const displayRelationsVal = relationMapNote.getLabelValue('displayRelations');
const displayRelations = !displayRelationsVal ? [] : displayRelationsVal
.split(",")
.map(token => token.trim());
const hideRelationsVal = relationMapNote.getLabelValue('hideRelations');
const hideRelations = !hideRelationsVal ? [] : hideRelationsVal
.split(",")
.map(token => token.trim());
const foundNoteIds = sql.getColumn(`SELECT noteId FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds);
const notes = becca.getNotes(foundNoteIds);
for (const note of notes) {
resp.noteTitles[note.noteId] = note.title;
resp.relations = resp.relations.concat(note.getRelations()
.filter(relation => !relation.isAutoLink() || displayRelations.includes(relation.name))
.filter(relation => displayRelations.length > 0
? displayRelations.includes(relation.name)
: !hideRelations.includes(relation.name))
.filter(relation => noteIds.includes(relation.value))
.map(relation => ({
attributeId: relation.attributeId,
sourceNoteId: relation.noteId,
targetNoteId: relation.value,
name: relation.name
})));
for (const relationDefinition of note.getRelationDefinitions()) {
const def = relationDefinition.getDefinition();
if (def.inverseRelation) {
resp.inverseRelations[relationDefinition.getDefinedName()] = def.inverseRelation;
resp.inverseRelations[def.inverseRelation] = relationDefinition.getDefinedName();
}
}
}
return resp;
}
module.exports = {
getRelationMap
};