| 
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const express = require('express'); | 
					
						
							|  |  |  | const router = express.Router(); | 
					
						
							|  |  |  | const auth = require('../../services/auth'); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | const sync = require('../../services/sync'); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | const syncUpdate = require('../../services/sync_update'); | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  | const options = require('../../services/options'); | 
					
						
							| 
									
										
										
										
											2017-11-21 22:11:27 -05:00
										 |  |  | const content_hash = require('../../services/content_hash'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | router.get('/check', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     res.send({ | 
					
						
							|  |  |  |         'content_hash': await content_hash.getContentHash(), | 
					
						
							|  |  |  |         'max_sync_id': await sql.getSingleValue('SELECT MAX(id) FROM sync') | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  | router.post('/now', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-04 21:21:09 -04:00
										 |  |  |     res.send(await sync.sync()); | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | router.get('/changed', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const lastSyncId = parseInt(req.query.lastSyncId); | 
					
						
							| 
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  |     res.send(await sql.getResults("SELECT * FROM sync WHERE id > ?", [lastSyncId])); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | router.get('/notes/:noteId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const noteId = req.params.noteId; | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |     res.send({ | 
					
						
							| 
									
										
										
										
											2017-11-29 20:47:01 -05:00
										 |  |  |         entity: await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]) | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 22:25:39 -05:00
										 |  |  | router.get('/notes_tree/:noteTreeId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const noteTreeId = req.params.noteTreeId; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 22:25:39 -05:00
										 |  |  |     res.send(await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId])); | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | router.get('/notes_history/:noteHistoryId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const noteHistoryId = req.params.noteHistoryId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     res.send(await sql.getSingleResult("SELECT * FROM notes_history WHERE note_history_id = ?", [noteHistoryId])); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  | router.get('/options/:optName', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const optName = req.params.optName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!options.SYNCED_OPTIONS.includes(optName)) { | 
					
						
							|  |  |  |         res.send("This option can't be synced."); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         res.send(await sql.getSingleResult("SELECT * FROM options WHERE opt_name = ?", [optName])); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-18 17:17:46 -05:00
										 |  |  | router.get('/notes_reordering/:noteTreeParentId', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const noteTreeParentId = req.params.noteTreeParentId; | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({ | 
					
						
							| 
									
										
										
										
											2017-11-18 17:17:46 -05:00
										 |  |  |         note_pid: noteTreeParentId, | 
					
						
							|  |  |  |         ordering: await sql.getMap("SELECT note_tree_id, note_pos FROM notes_tree WHERE note_pid = ?", [noteTreeParentId]) | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 22:11:27 -05:00
										 |  |  | router.get('/recent_notes/:notePath', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							|  |  |  |     const notePath = req.params.notePath; | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 22:11:27 -05:00
										 |  |  |     res.send(await sql.getSingleResult("SELECT * FROM recent_notes WHERE note_path = ?", [notePath])); | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | router.put('/notes', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-29 20:47:01 -05:00
										 |  |  |     await syncUpdate.updateNote(req.body.entity, req.body.sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | router.put('/notes_tree', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     await syncUpdate.updateNoteTree(req.body.entity, req.body.sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | router.put('/notes_history', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     await syncUpdate.updateNoteHistory(req.body.entity, req.body.sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     res.send({}); | 
					
						
							| 
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  | router.put('/notes_reordering', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     await syncUpdate.updateNoteReordering(req.body.entity, req.body.sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  | router.put('/options', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     await syncUpdate.updateOptions(req.body.entity, req.body.sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  | router.put('/recent_notes', auth.checkApiAuth, async (req, res, next) => { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     await syncUpdate.updateRecentNotes(req.body.entity, req.body.sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     res.send({}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 |  |  | module.exports = router; |