mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 13:39:01 +01:00 
			
		
		
		
	smaller refactorings continued
This commit is contained in:
		
							parent
							
								
									fad0ec757b
								
							
						
					
					
						commit
						acc82f39c4
					
				@ -4,7 +4,7 @@ const utils = require('../services/utils');
 | 
				
			|||||||
const repository = require('../services/repository');
 | 
					const repository = require('../services/repository');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Entity {
 | 
					class Entity {
 | 
				
			||||||
    constructor(row) {
 | 
					    constructor(row = {}) {
 | 
				
			||||||
        utils.assertArguments(row);
 | 
					        utils.assertArguments(row);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        for (const key in row) {
 | 
					        for (const key in row) {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,6 @@
 | 
				
			|||||||
const Note = require('../entities/note');
 | 
					const Note = require('../entities/note');
 | 
				
			||||||
const NoteRevision = require('../entities/note_revision');
 | 
					const NoteRevision = require('../entities/note_revision');
 | 
				
			||||||
 | 
					const Image = require('../entities/image');
 | 
				
			||||||
const NoteImage = require('../entities/note_image');
 | 
					const NoteImage = require('../entities/note_image');
 | 
				
			||||||
const Branch = require('../entities/branch');
 | 
					const Branch = require('../entities/branch');
 | 
				
			||||||
const Label = require('../entities/label');
 | 
					const Label = require('../entities/label');
 | 
				
			||||||
@ -17,6 +18,9 @@ function createEntityFromRow(row) {
 | 
				
			|||||||
    else if (row.noteImageId) {
 | 
					    else if (row.noteImageId) {
 | 
				
			||||||
        entity = new NoteImage(row);
 | 
					        entity = new NoteImage(row);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    else if (row.imageId) {
 | 
				
			||||||
 | 
					        entity = new Image(row);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    else if (row.branchId) {
 | 
					    else if (row.branchId) {
 | 
				
			||||||
        entity = new Branch(row);
 | 
					        entity = new Branch(row);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -15,6 +15,18 @@ function LabelsModel() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    this.labels = ko.observableArray();
 | 
					    this.labels = ko.observableArray();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    this.updateLabelPositions = function() {
 | 
				
			||||||
 | 
					        let position = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // we need to update positions by searching in the DOM, because order of the
 | 
				
			||||||
 | 
					        // labels in the viewmodel (self.labels()) stays the same
 | 
				
			||||||
 | 
					        $labelsBody.find('input[name="position"]').each(function() {
 | 
				
			||||||
 | 
					            const label = self.getTargetLabel(this);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            label().position = position++;
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.loadLabels = async function() {
 | 
					    this.loadLabels = async function() {
 | 
				
			||||||
        const noteId = noteDetailService.getCurrentNoteId();
 | 
					        const noteId = noteDetailService.getCurrentNoteId();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -32,17 +44,7 @@ function LabelsModel() {
 | 
				
			|||||||
        $labelsBody.sortable({
 | 
					        $labelsBody.sortable({
 | 
				
			||||||
            handle: '.handle',
 | 
					            handle: '.handle',
 | 
				
			||||||
            containment: $labelsBody,
 | 
					            containment: $labelsBody,
 | 
				
			||||||
            update: function() {
 | 
					            update: this.updateLabelPositions
 | 
				
			||||||
                let position = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                // we need to update positions by searching in the DOM, because order of the
 | 
					 | 
				
			||||||
                // labels in the viewmodel (self.labels()) stays the same
 | 
					 | 
				
			||||||
                $labelsBody.find('input[name="position"]').each(function() {
 | 
					 | 
				
			||||||
                    const label = self.getTargetLabel(this);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    label().position = position++;
 | 
					 | 
				
			||||||
                });
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -80,6 +82,8 @@ function LabelsModel() {
 | 
				
			|||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.updateLabelPositions();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const noteId = noteDetailService.getCurrentNoteId();
 | 
					        const noteId = noteDetailService.getCurrentNoteId();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const labelsToSave = self.labels()
 | 
					        const labelsToSave = self.labels()
 | 
				
			||||||
 | 
				
			|||||||
@ -438,35 +438,28 @@ async function createNote(node, parentNoteId, target, isProtected) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const newNoteName = "new note";
 | 
					    const newNoteName = "new note";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const result = await server.post('notes/' + parentNoteId + '/children', {
 | 
					    const {note, branch} = await server.post('notes/' + parentNoteId + '/children', {
 | 
				
			||||||
        title: newNoteName,
 | 
					        title: newNoteName,
 | 
				
			||||||
        target: target,
 | 
					        target: target,
 | 
				
			||||||
        target_branchId: node.data.branchId,
 | 
					        target_branchId: node.data.branchId,
 | 
				
			||||||
        isProtected: isProtected
 | 
					        isProtected: isProtected
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const note = new NoteShort(treeCache, {
 | 
					    const noteEntity = new NoteShort(treeCache, note);
 | 
				
			||||||
        noteId: result.noteId,
 | 
					    const branchEntity = new Branch(treeCache, branch);
 | 
				
			||||||
        title: result.title,
 | 
					 | 
				
			||||||
        isProtected: result.isProtected,
 | 
					 | 
				
			||||||
        type: result.type,
 | 
					 | 
				
			||||||
        mime: result.mime
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const branch = new Branch(treeCache, result);
 | 
					    treeCache.add(noteEntity, branchEntity);
 | 
				
			||||||
 | 
					 | 
				
			||||||
    treeCache.add(note, branch);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    noteDetailService.newNoteCreated();
 | 
					    noteDetailService.newNoteCreated();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const newNode = {
 | 
					    const newNode = {
 | 
				
			||||||
        title: newNoteName,
 | 
					        title: newNoteName,
 | 
				
			||||||
        noteId: result.noteId,
 | 
					        noteId: branchEntity.noteId,
 | 
				
			||||||
        parentNoteId: parentNoteId,
 | 
					        parentNoteId: parentNoteId,
 | 
				
			||||||
        refKey: result.noteId,
 | 
					        refKey: branchEntity.noteId,
 | 
				
			||||||
        branchId: result.branchId,
 | 
					        branchId: branchEntity.branchId,
 | 
				
			||||||
        isProtected: isProtected,
 | 
					        isProtected: isProtected,
 | 
				
			||||||
        extraClasses: await treeBuilder.getExtraClasses(note)
 | 
					        extraClasses: await treeBuilder.getExtraClasses(noteEntity)
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (target === 'after') {
 | 
					    if (target === 'after') {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,9 +1,9 @@
 | 
				
			|||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const sql = require('../../services/sql');
 | 
					 | 
				
			||||||
const notes = require('../../services/notes');
 | 
					const notes = require('../../services/notes');
 | 
				
			||||||
const labels = require('../../services/labels');
 | 
					const labels = require('../../services/labels');
 | 
				
			||||||
const protected_session = require('../../services/protected_session');
 | 
					const protected_session = require('../../services/protected_session');
 | 
				
			||||||
 | 
					const repository = require('../../services/repository');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function uploadFile(req) {
 | 
					async function uploadFile(req) {
 | 
				
			||||||
    const parentNoteId = req.params.parentNoteId;
 | 
					    const parentNoteId = req.params.parentNoteId;
 | 
				
			||||||
@ -11,50 +11,46 @@ async function uploadFile(req) {
 | 
				
			|||||||
    const originalName = file.originalname;
 | 
					    const originalName = file.originalname;
 | 
				
			||||||
    const size = file.size;
 | 
					    const size = file.size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
 | 
					    const parentNote = await repository.getNote(parentNoteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!note) {
 | 
					    if (!parentNote) {
 | 
				
			||||||
        return [404, `Note ${parentNoteId} doesn't exist.`];
 | 
					        return [404, `Note ${parentNoteId} doesn't exist.`];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const {noteId} = await notes.createNewNote(parentNoteId, {
 | 
					    const {note} = await notes.createNewNote(parentNoteId, {
 | 
				
			||||||
        title: originalName,
 | 
					        title: originalName,
 | 
				
			||||||
        content: file.buffer,
 | 
					        content: file.buffer,
 | 
				
			||||||
        target: 'into',
 | 
					        target: 'into',
 | 
				
			||||||
        isProtected: false,
 | 
					        isProtected: false,
 | 
				
			||||||
        type: 'file',
 | 
					        type: 'file',
 | 
				
			||||||
        mime: file.mimetype
 | 
					        mime: file.mimetype
 | 
				
			||||||
    }, req);
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await labels.createLabel(noteId, "original_file_name", originalName);
 | 
					    await labels.createLabel(note.noteId, "original_file_name", originalName);
 | 
				
			||||||
    await labels.createLabel(noteId, "file_size", size);
 | 
					    await labels.createLabel(note.noteId, "file_size", size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        noteId: noteId
 | 
					        noteId: note.noteId
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function downloadFile(req, res) {
 | 
					async function downloadFile(req, res) {
 | 
				
			||||||
    const noteId = req.params.noteId;
 | 
					    const noteId = req.params.noteId;
 | 
				
			||||||
    const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
 | 
					    const note = await repository.getNote(noteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!note) {
 | 
					    if (!note) {
 | 
				
			||||||
        return res.status(404).send(`Note ${noteId} doesn't exist.`);
 | 
					        return res.status(404).send(`Note ${noteId} doesn't exist.`);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (note.isProtected) {
 | 
					    if (note.isProtected && !protected_session.isProtectedSessionAvailable()) {
 | 
				
			||||||
        if (!protected_session.isProtectedSessionAvailable()) {
 | 
					 | 
				
			||||||
        res.status(401).send("Protected session not available");
 | 
					        res.status(401).send("Protected session not available");
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        protected_session.decryptNote(note);
 | 
					    const labelMap = await note.getLabelMap();
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const labelMap = await labels.getNoteLabelMap(noteId);
 | 
					 | 
				
			||||||
    const fileName = labelMap.original_file_name ? labelMap.original_file_name : note.title;
 | 
					    const fileName = labelMap.original_file_name ? labelMap.original_file_name : note.title;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    res.setHeader('Content-Disposition', 'file; filename=' + fileName);
 | 
					    res.setHeader('Content-Disposition', 'file; filename="' + fileName + '"');
 | 
				
			||||||
    res.setHeader('Content-Type', note.mime);
 | 
					    res.setHeader('Content-Type', note.mime);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    res.send(note.content);
 | 
					    res.send(note.content);
 | 
				
			||||||
 | 
				
			|||||||
@ -1,12 +1,12 @@
 | 
				
			|||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const sql = require('../../services/sql');
 | 
					 | 
				
			||||||
const image = require('../../services/image');
 | 
					const image = require('../../services/image');
 | 
				
			||||||
 | 
					const repository = require('../../services/repository');
 | 
				
			||||||
const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
 | 
					const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR;
 | 
				
			||||||
const fs = require('fs');
 | 
					const fs = require('fs');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function returnImage(req, res) {
 | 
					async function returnImage(req, res) {
 | 
				
			||||||
    const image = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [req.params.imageId]);
 | 
					    const image = await repository.getImage(req.params.imageId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!image) {
 | 
					    if (!image) {
 | 
				
			||||||
        return res.sendStatus(404);
 | 
					        return res.sendStatus(404);
 | 
				
			||||||
@ -25,7 +25,7 @@ async function uploadImage(req) {
 | 
				
			|||||||
    const noteId = req.query.noteId;
 | 
					    const noteId = req.query.noteId;
 | 
				
			||||||
    const file = req.file;
 | 
					    const file = req.file;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
 | 
					    const note = await repository.getNote(noteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!note) {
 | 
					    if (!note) {
 | 
				
			||||||
        return [404, `Note ${noteId} doesn't exist.`];
 | 
					        return [404, `Note ${noteId} doesn't exist.`];
 | 
				
			||||||
 | 
				
			|||||||
@ -1,6 +1,6 @@
 | 
				
			|||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const sql = require('../../services/sql');
 | 
					const repository = require('../../services/repository');
 | 
				
			||||||
const labels = require('../../services/labels');
 | 
					const labels = require('../../services/labels');
 | 
				
			||||||
const notes = require('../../services/notes');
 | 
					const notes = require('../../services/notes');
 | 
				
			||||||
const tar = require('tar-stream');
 | 
					const tar = require('tar-stream');
 | 
				
			||||||
@ -89,9 +89,9 @@ async function importTar(req) {
 | 
				
			|||||||
    const parentNoteId = req.params.parentNoteId;
 | 
					    const parentNoteId = req.params.parentNoteId;
 | 
				
			||||||
    const file = req.file;
 | 
					    const file = req.file;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [parentNoteId]);
 | 
					    const parentNote = await repository.getNote(parentNoteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!note) {
 | 
					    if (!parentNote) {
 | 
				
			||||||
        return [404, `Note ${parentNoteId} doesn't exist.`];
 | 
					        return [404, `Note ${parentNoteId} doesn't exist.`];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -1,25 +1,26 @@
 | 
				
			|||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const sql = require('../../services/sql');
 | 
					const sql = require('../../services/sql');
 | 
				
			||||||
const sync_table = require('../../services/sync_table');
 | 
					 | 
				
			||||||
const utils = require('../../services/utils');
 | 
					const utils = require('../../services/utils');
 | 
				
			||||||
const labels = require('../../services/labels');
 | 
					const labels = require('../../services/labels');
 | 
				
			||||||
 | 
					const repository = require('../../services/repository');
 | 
				
			||||||
 | 
					const Label = require('../../entities/label');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getNoteLabels(req) {
 | 
					async function getNoteLabels(req) {
 | 
				
			||||||
    const noteId = req.params.noteId;
 | 
					    const noteId = req.params.noteId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
 | 
					    return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function updateNoteLabels(req, res, next) {
 | 
					async function updateNoteLabels(req) {
 | 
				
			||||||
    const noteId = req.params.noteId;
 | 
					    const noteId = req.params.noteId;
 | 
				
			||||||
    const labels = req.body;
 | 
					    const labels = req.body;
 | 
				
			||||||
    const now = utils.nowDate();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const label of labels) {
 | 
					    for (const label of labels) {
 | 
				
			||||||
 | 
					        let labelEntity;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (label.labelId) {
 | 
					        if (label.labelId) {
 | 
				
			||||||
            await sql.execute("UPDATE labels SET name = ?, value = ?, dateModified = ?, isDeleted = ?, position = ? WHERE labelId = ?",
 | 
					            labelEntity = await repository.getLabel(label.labelId);
 | 
				
			||||||
                [label.name, label.value, now, label.isDeleted, label.position, label.labelId]);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else {
 | 
					        else {
 | 
				
			||||||
            // if it was "created" and then immediatelly deleted, we just don't create it at all
 | 
					            // if it was "created" and then immediatelly deleted, we just don't create it at all
 | 
				
			||||||
@ -27,27 +28,23 @@ async function updateNoteLabels(req, res, next) {
 | 
				
			|||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            label.labelId = utils.newLabelId();
 | 
					            labelEntity = new Label();
 | 
				
			||||||
 | 
					            labelEntity.labelId = utils.newLabelId();
 | 
				
			||||||
            await sql.insert("labels", {
 | 
					            labelEntity.noteId = noteId;
 | 
				
			||||||
                labelId: label.labelId,
 | 
					 | 
				
			||||||
                noteId: noteId,
 | 
					 | 
				
			||||||
                name: label.name,
 | 
					 | 
				
			||||||
                value: label.value,
 | 
					 | 
				
			||||||
                position: label.position,
 | 
					 | 
				
			||||||
                dateCreated: now,
 | 
					 | 
				
			||||||
                dateModified: now,
 | 
					 | 
				
			||||||
                isDeleted: false
 | 
					 | 
				
			||||||
            });
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await sync_table.addLabelSync(label.labelId);
 | 
					        labelEntity.name = label.name;
 | 
				
			||||||
 | 
					        labelEntity.value = label.value;
 | 
				
			||||||
 | 
					        labelEntity.position = label.position;
 | 
				
			||||||
 | 
					        labelEntity.isDeleted = label.isDeleted;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await labelEntity.save();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return await sql.getRows("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
 | 
					    return await repository.getEntities("SELECT * FROM labels WHERE isDeleted = 0 AND noteId = ? ORDER BY position, dateCreated", [noteId]);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getAllLabelNames(req) {
 | 
					async function getAllLabelNames() {
 | 
				
			||||||
    const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
 | 
					    const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const label of labels.BUILTIN_LABELS) {
 | 
					    for (const label of labels.BUILTIN_LABELS) {
 | 
				
			||||||
 | 
				
			|||||||
@ -3,22 +3,18 @@
 | 
				
			|||||||
const sql = require('../../services/sql');
 | 
					const sql = require('../../services/sql');
 | 
				
			||||||
const notes = require('../../services/notes');
 | 
					const notes = require('../../services/notes');
 | 
				
			||||||
const utils = require('../../services/utils');
 | 
					const utils = require('../../services/utils');
 | 
				
			||||||
const protected_session = require('../../services/protected_session');
 | 
					 | 
				
			||||||
const tree = require('../../services/tree');
 | 
					const tree = require('../../services/tree');
 | 
				
			||||||
const sync_table = require('../../services/sync_table');
 | 
					const sync_table = require('../../services/sync_table');
 | 
				
			||||||
const repository = require('../../services/repository');
 | 
					const repository = require('../../services/repository');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getNote(req) {
 | 
					async function getNote(req) {
 | 
				
			||||||
    const noteId = req.params.noteId;
 | 
					    const noteId = req.params.noteId;
 | 
				
			||||||
 | 
					    const note = await repository.getNote(noteId);
 | 
				
			||||||
    const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!note) {
 | 
					    if (!note) {
 | 
				
			||||||
        return [404, "Note " + noteId + " has not been found."];
 | 
					        return [404, "Note " + noteId + " has not been found."];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected_session.decryptNote(note);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (note.type === 'file') {
 | 
					    if (note.type === 'file') {
 | 
				
			||||||
        // no need to transfer (potentially large) file payload for this request
 | 
					        // no need to transfer (potentially large) file payload for this request
 | 
				
			||||||
        note.content = null;
 | 
					        note.content = null;
 | 
				
			||||||
@ -31,12 +27,11 @@ async function createNote(req) {
 | 
				
			|||||||
    const parentNoteId = req.params.parentNoteId;
 | 
					    const parentNoteId = req.params.parentNoteId;
 | 
				
			||||||
    const newNote = req.body;
 | 
					    const newNote = req.body;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req);
 | 
					    const { note, branch } = await notes.createNewNote(parentNoteId, newNote, req);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        'noteId': noteId,
 | 
					        note,
 | 
				
			||||||
        'branchId': branchId,
 | 
					        branch
 | 
				
			||||||
        'note': note
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -47,7 +47,7 @@ async function uploadImage(req) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);
 | 
					    const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const {noteId} = await notes.createNewNote(parentNoteId, {
 | 
					    const {note} = await notes.createNewNote(parentNoteId, {
 | 
				
			||||||
        title: "Sender image",
 | 
					        title: "Sender image",
 | 
				
			||||||
        content: "",
 | 
					        content: "",
 | 
				
			||||||
        target: 'into',
 | 
					        target: 'into',
 | 
				
			||||||
@ -56,13 +56,13 @@ async function uploadImage(req) {
 | 
				
			|||||||
        mime: 'text/html'
 | 
					        mime: 'text/html'
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const {fileName, imageId} = await image.saveImage(file, null, noteId);
 | 
					    const {fileName, imageId} = await image.saveImage(file, null, note.noteId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const url = `/api/images/${imageId}/${fileName}`;
 | 
					    const url = `/api/images/${imageId}/${fileName}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const content = `<img src="${url}"/>`;
 | 
					    const content = `<img src="${url}"/>`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, noteId]);
 | 
					    await sql.execute("UPDATE notes SET content = ? WHERE noteId = ?", [content, note.noteId]);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function saveNote(req) {
 | 
					async function saveNote(req) {
 | 
				
			||||||
 | 
				
			|||||||
@ -14,12 +14,14 @@ const DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Satur
 | 
				
			|||||||
const MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December'];
 | 
					const MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function createNote(parentNoteId, noteTitle, noteText) {
 | 
					async function createNote(parentNoteId, noteTitle, noteText) {
 | 
				
			||||||
    return (await notes.createNewNote(parentNoteId, {
 | 
					    const {note} = await notes.createNewNote(parentNoteId, {
 | 
				
			||||||
        title: noteTitle,
 | 
					        title: noteTitle,
 | 
				
			||||||
        content: noteText,
 | 
					        content: noteText,
 | 
				
			||||||
        target: 'into',
 | 
					        target: 'into',
 | 
				
			||||||
        isProtected: false
 | 
					        isProtected: false
 | 
				
			||||||
    })).noteId;
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return note.noteId;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getNoteStartingWith(parentNoteId, startsWith) {
 | 
					async function getNoteStartingWith(parentNoteId, startsWith) {
 | 
				
			||||||
@ -34,11 +36,13 @@ async function getRootCalendarNoteId() {
 | 
				
			|||||||
              WHERE labels.name = '${CALENDAR_ROOT_LABEL}' AND notes.isDeleted = 0`);
 | 
					              WHERE labels.name = '${CALENDAR_ROOT_LABEL}' AND notes.isDeleted = 0`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!rootNoteId) {
 | 
					    if (!rootNoteId) {
 | 
				
			||||||
        rootNoteId = (await notes.createNewNote('root', {
 | 
					        const {rootNote} = await notes.createNewNote('root', {
 | 
				
			||||||
            title: 'Calendar',
 | 
					            title: 'Calendar',
 | 
				
			||||||
            target: 'into',
 | 
					            target: 'into',
 | 
				
			||||||
            isProtected: false
 | 
					            isProtected: false
 | 
				
			||||||
        })).noteId;
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const rootNoteId = rootNote.noteId;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await labels.createLabel(rootNoteId, CALENDAR_ROOT_LABEL);
 | 
					        await labels.createLabel(rootNoteId, CALENDAR_ROOT_LABEL);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -72,9 +72,7 @@ async function createNewNote(parentNoteId, noteOpts) {
 | 
				
			|||||||
    await branch.save();
 | 
					    await branch.save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        noteId: note.noteId,
 | 
					 | 
				
			||||||
        note,
 | 
					        note,
 | 
				
			||||||
        branchId: branch.branchId,
 | 
					 | 
				
			||||||
        branch
 | 
					        branch
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -83,7 +81,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
 | 
				
			|||||||
    if (!parentNoteId) throw new Error("Empty parentNoteId");
 | 
					    if (!parentNoteId) throw new Error("Empty parentNoteId");
 | 
				
			||||||
    if (!title) throw new Error("Empty title");
 | 
					    if (!title) throw new Error("Empty title");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const note = {
 | 
					    const noteData = {
 | 
				
			||||||
        title: title,
 | 
					        title: title,
 | 
				
			||||||
        content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
 | 
					        content: extraOptions.json ? JSON.stringify(content, null, '\t') : content,
 | 
				
			||||||
        target: 'into',
 | 
					        target: 'into',
 | 
				
			||||||
@ -92,25 +90,25 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})
 | 
				
			|||||||
        mime: extraOptions.mime
 | 
					        mime: extraOptions.mime
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (extraOptions.json && !note.type) {
 | 
					    if (extraOptions.json && !noteData.type) {
 | 
				
			||||||
        note.type = "code";
 | 
					        noteData.type = "code";
 | 
				
			||||||
        note.mime = "application/json";
 | 
					        noteData.mime = "application/json";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!note.type) {
 | 
					    if (!noteData.type) {
 | 
				
			||||||
        note.type = "text";
 | 
					        noteData.type = "text";
 | 
				
			||||||
        note.mime = "text/html";
 | 
					        noteData.mime = "text/html";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const {noteId} = await createNewNote(parentNoteId, note);
 | 
					    const {note} = await createNewNote(parentNoteId, noteData);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (extraOptions.labels) {
 | 
					    if (extraOptions.labels) {
 | 
				
			||||||
        for (const labelName in extraOptions.labels) {
 | 
					        for (const labelName in extraOptions.labels) {
 | 
				
			||||||
            await labels.createLabel(noteId, labelName, extraOptions.labels[labelName]);
 | 
					            await labels.createLabel(note.noteId, labelName, extraOptions.labels[labelName]);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return noteId;
 | 
					    return note.noteId;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function protectNoteRecursively(note, protect) {
 | 
					async function protectNoteRecursively(note, protect) {
 | 
				
			||||||
 | 
				
			|||||||
@ -37,6 +37,10 @@ async function getImage(imageId) {
 | 
				
			|||||||
    return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]);
 | 
					    return await getEntity("SELECT * FROM images WHERE imageId = ?", [imageId]);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function getLabel(labelId) {
 | 
				
			||||||
 | 
					    return await getEntity("SELECT * FROM labels WHERE labelId = ?", [labelId]);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function updateEntity(entity) {
 | 
					async function updateEntity(entity) {
 | 
				
			||||||
    if (entity.beforeSaving) {
 | 
					    if (entity.beforeSaving) {
 | 
				
			||||||
        entity.beforeSaving();
 | 
					        entity.beforeSaving();
 | 
				
			||||||
@ -59,6 +63,7 @@ module.exports = {
 | 
				
			|||||||
    getNote,
 | 
					    getNote,
 | 
				
			||||||
    getBranch,
 | 
					    getBranch,
 | 
				
			||||||
    getImage,
 | 
					    getImage,
 | 
				
			||||||
 | 
					    getLabel,
 | 
				
			||||||
    updateEntity,
 | 
					    updateEntity,
 | 
				
			||||||
    setEntityConstructor
 | 
					    setEntityConstructor
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user