| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2017-10-15 19:47:05 -04:00
										 |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | const notes = require('../../services/notes'); | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							| 
									
										
										
										
											2018-01-15 20:54:22 -05:00
										 |  |  | const utils = require('../../services/utils'); | 
					
						
							| 
									
										
										
										
											2017-11-12 21:40:26 -05:00
										 |  |  | const protected_session = require('../../services/protected_session'); | 
					
						
							| 
									
										
										
										
											2018-01-13 20:53:00 -05:00
										 |  |  | const tree = require('../../services/tree'); | 
					
						
							| 
									
										
										
										
											2018-01-20 21:56:03 -05:00
										 |  |  | const sync_table = require('../../services/sync_table'); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | const wrap = require('express-promise-wrap').wrap; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const detail = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |     if (!detail) { | 
					
						
							|  |  |  |         log.info("Note " + noteId + " has not been found."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return res.status(404).send({}); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |     protected_session.decryptNote(req, detail); | 
					
						
							| 
									
										
										
										
											2017-11-12 21:40:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |     if (detail.type === 'file') { | 
					
						
							| 
									
										
										
										
											2018-03-25 23:25:17 -04:00
										 |  |  |         // no need to transfer (potentially large) attachment payload for this request
 | 
					
						
							| 
									
										
										
										
											2018-02-18 21:28:24 -05:00
										 |  |  |         detail.content = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 23:25:17 -04:00
										 |  |  |     res.send(detail); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | })); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | router.post('/:parentNoteId/children', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |     const parentNoteId = req.params.parentNoteId; | 
					
						
							| 
									
										
										
										
											2018-01-28 10:37:43 -05:00
										 |  |  |     const newNote = req.body; | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |         const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req, sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |         res.send({ | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             'noteId': noteId, | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             'branchId': branchId, | 
					
						
							| 
									
										
										
										
											2018-01-28 10:37:43 -05:00
										 |  |  |             'note': note | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | })); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | router.put('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |     const note = req.body; | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |     const dataKey = protected_session.getDataKey(req); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |     await notes.updateNote(noteId, note, dataKey, sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							| 
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 |  |  | })); | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  | router.put('/:noteId/sort', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							|  |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  |     const dataKey = protected_session.getDataKey(req); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 20:53:00 -05:00
										 |  |  |     await tree.sortNotesAlphabetically(noteId, dataKey, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 20:53:00 -05:00
										 |  |  |     res.send({}); | 
					
						
							|  |  |  | })); | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 20:53:00 -05:00
										 |  |  | router.put('/:noteId/protect-sub-tree/:isProtected', auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							|  |  |  |     const noteId = req.params.noteId; | 
					
						
							|  |  |  |     const isProtected = !!parseInt(req.params.isProtected); | 
					
						
							|  |  |  |     const dataKey = protected_session.getDataKey(req); | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-13 20:53:00 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |         await notes.protectNoteRecursively(noteId, dataKey, isProtected, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 23:41:22 -05:00
										 |  |  | router.put(/\/(.*)\/type\/(.*)\/mime\/(.*)/, auth.checkApiAuth, wrap(async (req, res, next) => { | 
					
						
							|  |  |  |     const noteId = req.params[0]; | 
					
						
							|  |  |  |     const type = req.params[1]; | 
					
						
							|  |  |  |     const mime = req.params[2]; | 
					
						
							| 
									
										
										
										
											2018-01-28 21:57:46 -05:00
										 |  |  |     const sourceId = req.headers.source_id; | 
					
						
							| 
									
										
										
										
											2018-01-20 21:56:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |        await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?", | 
					
						
							| 
									
										
										
										
											2018-01-21 23:36:09 -05:00
										 |  |  |            [type, mime, utils.nowDate(), noteId]); | 
					
						
							| 
									
										
										
										
											2018-01-20 21:56:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |        await sync_table.addNoteSync(noteId, sourceId); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | module.exports = router; |