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-12-23 09:35:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const sync_table = require('../../services/sync_table');
							 | 
						
					
						
							
								
									
										
										
										
											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');
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 13:16:18 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const log = require('../../services/log');
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const wrap = require('express-promise-wrap').wrap;
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-21 22:11:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/check', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-21 22:11:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    res.send({
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-15 21:14:10 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        'hashes': await content_hash.getHashes(),
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        'max_sync_id': await sql.getFirstValue('SELECT MAX(id) FROM sync')
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-21 22:11:27 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.post('/now', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-04 21:21:09 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    res.send(await sync.sync());
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.post('/fill-sync-rows', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-19 22:04:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    await sql.doInTransaction(async () => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 13:55:13 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        await sync_table.fillAllSyncRows();
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-19 22:04:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 13:16:18 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    log.info("Sync rows have been filled.");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-19 22:04:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    res.send({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-19 22:04:51 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.post('/force-full-sync', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-13 23:03:48 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    await sql.doInTransaction(async () => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        await options.setOption('last_synced_pull', 0);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        await options.setOption('last_synced_push', 0);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 13:16:18 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    log.info("Forcing full sync.");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-13 23:03:48 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    // not awaiting for the job to finish (will probably take a long time)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    sync.sync();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-13 23:03:48 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.post('/force-note-sync/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-30 21:44:26 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const noteId = req.params.noteId;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    await sql.doInTransaction(async () => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        await sync_table.addNoteSync(noteId);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for (const noteTreeId of await sql.getFirstColumn("SELECT note_tree_id FROM notes_tree WHERE is_deleted = 0 AND note_id = ?", [noteId])) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            await sync_table.addNoteTreeSync(noteTreeId);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            await sync_table.addRecentNoteSync(noteTreeId);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        for (const noteHistoryId of await sql.getFirstColumn("SELECT note_history_id FROM notes_history WHERE note_id = ?", [noteId])) {
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-30 21:55:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            await sync_table.addNoteHistorySync(noteHistoryId);
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-30 21:44:26 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    log.info("Forcing note sync for " + noteId);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // not awaiting for the job to finish (will probably take a long time)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    sync.sync();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-30 21:44:26 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/changed', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const lastSyncId = parseInt(req.query.lastSyncId);
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    res.send(await sql.getAll("SELECT * FROM sync WHERE id > ?", [lastSyncId]));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/notes/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const noteId = req.params.noteId;
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    res.send({
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        entity: await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId])
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/notes_tree/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-23 22:25:39 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const noteTreeId = req.params.noteTreeId;
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    res.send(await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/notes_history/:noteHistoryId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const noteHistoryId = req.params.noteHistoryId;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    res.send(await sql.getFirst("SELECT * FROM notes_history WHERE note_history_id = ?", [noteHistoryId]));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/options/:optName', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const optName = req.params.optName;
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-11 22:45:25 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const opt = await sql.getFirst("SELECT * FROM options WHERE opt_name = ?", [optName]);
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-11 22:45:25 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if (!opt.is_synced) {
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        res.send("This option can't be synced.");
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else {
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-11 22:45:25 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        res.send(opt);
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/notes_reordering/:noteTreeParentId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-18 17:17:46 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const noteTreeParentId = req.params.noteTreeParentId;
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send({
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        parent_note_id: noteTreeParentId,
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-03 22:13:02 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ordering: await sql.getMap("SELECT note_tree_id, note_position FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [noteTreeParentId])
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    });
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/recent_notes/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const noteTreeId = req.params.noteTreeId;
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    res.send(await sql.getFirst("SELECT * FROM recent_notes WHERE note_tree_id = ?", [noteTreeId]));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/images/:imageId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const imageId = req.params.imageId;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const entity = await sql.getFirst("SELECT * FROM images WHERE image_id = ?", [imageId]);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if (entity && entity.data !== null) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        entity.data = entity.data.toString('base64');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send(entity);
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/notes_image/:noteImageId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const noteImageId = req.params.noteImageId;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send(await sql.getFirst("SELECT * FROM notes_image WHERE note_image_id = ?", [noteImageId]));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.get('/attributes/:attributeId', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const attributeId = req.params.attributeId;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send(await sql.getFirst("SELECT * FROM attributes WHERE attribute_id = ?", [attributeId]));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/notes', auth.checkApiAuth, wrap(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({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/notes_tree', auth.checkApiAuth, wrap(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({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/notes_history', auth.checkApiAuth, wrap(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({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/notes_reordering', auth.checkApiAuth, wrap(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({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 22:55:22 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/options', auth.checkApiAuth, wrap(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({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-02 20:48:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/recent_notes', auth.checkApiAuth, wrap(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({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2017-11-05 00:16:02 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/images', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    await syncUpdate.updateImage(req.body.entity, req.body.sourceId);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/notes_image', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    await syncUpdate.updateNoteImage(req.body.entity, req.body.sourceId);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send({});
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-07 09:35:44 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								router.put('/attributes', auth.checkApiAuth, wrap(async (req, res, next) => {
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-11 22:45:25 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    await syncUpdate.updateAttribute(req.body.entity, req.body.sourceId);
							 | 
						
					
						
							
								
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    res.send({});
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-10-24 22:58:59 -04:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								module.exports = router;
							 |