| 
									
										
										
										
											2018-01-23 21:59:30 -05: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-23 22:53:27 -05:00
										 |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2018-01-25 23:22:19 -05:00
										 |  |  | const notes = require('../../services/notes'); | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  | const protected_session = require('../../services/protected_session'); | 
					
						
							| 
									
										
										
										
											2018-01-25 23:49:03 -05:00
										 |  |  | const attributes = require('../../services/attributes'); | 
					
						
							| 
									
										
										
										
											2018-01-26 23:40:48 -05:00
										 |  |  | const script = require('../../services/script'); | 
					
						
							| 
									
										
										
										
											2018-01-23 21:59:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05: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-23 21:59:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send(ret); | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 23:49:03 -05: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-23 22:53:27 -05:00
										 |  |  | router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							|  |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 23:22:19 -05:00
										 |  |  |     const noteScript = (await notes.getNoteById(noteId, req)).note_text; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const subTreeScripts = await getSubTreeScripts(noteId, [noteId], req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send(subTreeScripts + noteScript); | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 23:49:03 -05:00
										 |  |  | async function getNoteWithSubtreeScript(noteId, req) { | 
					
						
							|  |  |  |     const noteScript = (await notes.getNoteById(noteId, req)).note_text; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const subTreeScripts = await getSubTreeScripts(noteId, [noteId], req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return subTreeScripts + noteScript; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  | async function getSubTreeScripts(parentId, includedNoteIds, dataKey) { | 
					
						
							| 
									
										
										
										
											2018-01-28 10:37:43 -05:00
										 |  |  |     const children = await sql.getAll(`SELECT notes.note_id, notes.note_title, notes.note_text, notes.is_protected, notes.mime 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  |                                      FROM notes JOIN notes_tree USING(note_id) | 
					
						
							|  |  |  |                                      WHERE notes_tree.is_deleted = 0 AND notes.is_deleted = 0 | 
					
						
							|  |  |  |                                            AND notes_tree.parent_note_id = ? AND notes.type = 'code' | 
					
						
							| 
									
										
										
										
											2018-01-23 23:41:22 -05:00
										 |  |  |                                            AND (notes.mime = 'application/javascript' OR notes.mime = 'text/html')`, [parentId]);
 | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |     protected_session.decryptNotes(dataKey, children); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  |     let script = "\r\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |     for (const child of children) { | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  |         if (includedNoteIds.includes(child.note_id)) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         includedNoteIds.push(child.note_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         script += await getSubTreeScripts(child.note_id, includedNoteIds, dataKey); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 10:37:43 -05:00
										 |  |  |         console.log('MIME: ', child.mime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (child.mime === 'application/javascript') { | 
					
						
							|  |  |  |             child.note_text = '<script>' + child.note_text + '</script>'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 22:53:27 -05:00
										 |  |  |         script += child.note_text + "\r\n"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return script; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 21:59:30 -05:00
										 |  |  | module.exports = router; |