2018-01-24 10:59:30 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const auth = require('../../services/auth');
|
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
2018-01-24 11:53:27 +08:00
|
|
|
const sql = require('../../services/sql');
|
2018-01-26 12:22:19 +08:00
|
|
|
const notes = require('../../services/notes');
|
2018-01-24 11:53:27 +08:00
|
|
|
const protected_session = require('../../services/protected_session');
|
2018-01-26 12:49:03 +08:00
|
|
|
const attributes = require('../../services/attributes');
|
2018-01-27 12:40:48 +08:00
|
|
|
const script = require('../../services/script');
|
2018-01-24 10:59:30 +08:00
|
|
|
|
2018-01-28 06:18:19 +08:00
|
|
|
router.post('/exec/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
|
|
|
const ret = await script.executeScript(noteId, req, req.body.script, req.body.params);
|
2018-01-24 10:59:30 +08:00
|
|
|
|
|
|
|
res.send(ret);
|
|
|
|
}));
|
|
|
|
|
2018-01-26 12:49:03 +08:00
|
|
|
router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const noteIds = await attributes.getNoteIdsWithAttribute("run_on_startup");
|
|
|
|
|
|
|
|
const scripts = [];
|
|
|
|
|
|
|
|
for (const noteId of noteIds) {
|
|
|
|
scripts.push(await getNoteWithSubtreeScript(noteId, req));
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send(scripts);
|
|
|
|
}));
|
|
|
|
|
2018-01-24 11:53:27 +08:00
|
|
|
router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
const noteScript = (await notes.getNoteById(noteId, req)).content;
|
2018-01-26 12:22:19 +08:00
|
|
|
|
|
|
|
const subTreeScripts = await getSubTreeScripts(noteId, [noteId], req);
|
|
|
|
|
|
|
|
res.send(subTreeScripts + noteScript);
|
2018-01-24 11:53:27 +08:00
|
|
|
}));
|
|
|
|
|
2018-01-26 12:49:03 +08:00
|
|
|
async function getNoteWithSubtreeScript(noteId, req) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const noteScript = (await notes.getNoteById(noteId, req)).content;
|
2018-01-26 12:49:03 +08:00
|
|
|
|
|
|
|
const subTreeScripts = await getSubTreeScripts(noteId, [noteId], req);
|
|
|
|
|
|
|
|
return subTreeScripts + noteScript;
|
|
|
|
}
|
|
|
|
|
2018-01-24 11:53:27 +08:00
|
|
|
async function getSubTreeScripts(parentId, includedNoteIds, dataKey) {
|
2018-01-29 08:30:14 +08:00
|
|
|
const children = await sql.getAll(`SELECT notes.noteId, notes.title, notes.content, notes.isProtected, notes.mime
|
2018-01-29 08:38:05 +08:00
|
|
|
FROM notes JOIN note_tree USING(noteId)
|
|
|
|
WHERE note_tree.isDeleted = 0 AND notes.isDeleted = 0
|
|
|
|
AND note_tree.parentNoteId = ? AND notes.type = 'code'
|
2018-01-24 12:41:22 +08:00
|
|
|
AND (notes.mime = 'application/javascript' OR notes.mime = 'text/html')`, [parentId]);
|
2018-01-24 11:53:27 +08:00
|
|
|
|
2018-01-25 11:13:41 +08:00
|
|
|
protected_session.decryptNotes(dataKey, children);
|
|
|
|
|
2018-01-24 11:53:27 +08:00
|
|
|
let script = "\r\n";
|
|
|
|
|
2018-01-25 11:13:41 +08:00
|
|
|
for (const child of children) {
|
2018-01-29 08:30:14 +08:00
|
|
|
if (includedNoteIds.includes(child.noteId)) {
|
2018-01-24 11:53:27 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
includedNoteIds.push(child.noteId);
|
2018-01-24 11:53:27 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
script += await getSubTreeScripts(child.noteId, includedNoteIds, dataKey);
|
2018-01-24 11:53:27 +08:00
|
|
|
|
2018-01-28 23:37:43 +08:00
|
|
|
if (child.mime === 'application/javascript') {
|
2018-01-29 08:30:14 +08:00
|
|
|
child.content = '<script>' + child.content + '</script>';
|
2018-01-28 23:37:43 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
script += child.content + "\r\n";
|
2018-01-24 11:53:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return script;
|
|
|
|
}
|
|
|
|
|
2018-01-24 10:59:30 +08:00
|
|
|
module.exports = router;
|