| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | const log = require('./log'); | 
					
						
							|  |  |  | const rp = require('request-promise'); | 
					
						
							|  |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const migration = require('./migration'); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | const config = require('./config'); | 
					
						
							|  |  |  | const audit_category = require('./audit_category'); | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  | const crypto = require('crypto'); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | const SYNC_SERVER = config['Sync']['syncServerHost']; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let syncInProgress = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  | async function pullSync(cookieJar) { | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     const lastSyncedPull = parseInt(await sql.getOption('last_synced_pull')); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const resp = await rp({ | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |         uri: SYNC_SERVER + '/api/sync/changed/' + lastSyncedPull, | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |         headers: { | 
					
						
							|  |  |  |             auth: 'sync' | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         json: true | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |         await sql.beginTransaction(); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |         await putChanged(resp); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |         for (const noteId of resp.notes) { | 
					
						
							|  |  |  |             const note = await rp({ | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |                 uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull, | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |                 headers: { | 
					
						
							|  |  |  |                     auth: 'sync' | 
					
						
							|  |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  |                 json: true, | 
					
						
							|  |  |  |                 jar: cookieJar | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |             await putNote(note); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:54:10 -04:00
										 |  |  |         await sql.setOption('last_synced_pull', resp.syncTimestamp); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |         await sql.commit(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         await sql.rollback(); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |         throw e; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  | async function pushSync(cookieJar) { | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     const lastSyncedPush = parseInt(await sql.getOption('last_synced_push')); | 
					
						
							|  |  |  |     const syncStarted = utils.nowTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const changed = await getChangedSince(lastSyncedPush); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     await rp({ | 
					
						
							|  |  |  |         method: 'PUT', | 
					
						
							|  |  |  |         uri: SYNC_SERVER + '/api/sync/changed', | 
					
						
							|  |  |  |         headers: { | 
					
						
							|  |  |  |             auth: 'sync' | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         body: changed, | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  |         json: true, | 
					
						
							|  |  |  |         jar: cookieJar | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const noteId of changed.notes) { | 
					
						
							|  |  |  |         const note = await getNoteSince(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await rp({ | 
					
						
							|  |  |  |             method: 'PUT', | 
					
						
							|  |  |  |             uri: SYNC_SERVER + '/api/sync/note', | 
					
						
							|  |  |  |             headers: { | 
					
						
							|  |  |  |                 auth: 'sync' | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             body: note, | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  |             json: true, | 
					
						
							|  |  |  |             jar: cookieJar | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  |     await sql.setOption('last_synced_push', syncStarted); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  | async function login() { | 
					
						
							|  |  |  |     const timestamp = utils.nowTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const hmac = crypto.createHmac('sha256', documentSecret); | 
					
						
							|  |  |  |     hmac.update(timestamp); | 
					
						
							|  |  |  |     const hash = hmac.digest('base64'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const cookieJar = rp.jar(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await rp({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         uri: SYNC_SERVER + '/api/login', | 
					
						
							|  |  |  |         body: { | 
					
						
							|  |  |  |             timestamp: timestamp, | 
					
						
							|  |  |  |             dbVersion: migration.APP_DB_VERSION, | 
					
						
							|  |  |  |             hash: hash | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         json: true, | 
					
						
							|  |  |  |         jar: cookieJar | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return cookieJar; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | async function sync() { | 
					
						
							|  |  |  |     if (syncInProgress) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |     syncInProgress = true; | 
					
						
							| 
									
										
										
										
											2017-10-26 19:22:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |     try { | 
					
						
							|  |  |  |         if (!await migration.isDbUpToDate()) { | 
					
						
							|  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  |         const cookieJar = await login(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await pushSync(cookieJar); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  |         await pullSync(cookieJar); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         log.error("sync failed: " + e.stack); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     finally { | 
					
						
							|  |  |  |         syncInProgress = false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | async function getChangedSince(since) { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2017-10-28 13:19:12 -04:00
										 |  |  |         'documentId': await getDocumentId(), | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |         'syncTimestamp': utils.nowTimestamp(), | 
					
						
							|  |  |  |         'tree': await sql.getResults("select * from notes_tree where date_modified >= ?", [since]), | 
					
						
							|  |  |  |         'notes': await sql.getFlattenedResults('note_id', "select note_id from notes where date_modified >= ?", [since]), | 
					
						
							|  |  |  |         'audit_log': await sql.getResults("select * from audit_log where date_modified >= ?", [since]) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function getNoteSince(noteId, since) { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         'detail': await sql.getSingleResult("select * from notes where note_id = ?", [noteId]), | 
					
						
							|  |  |  |         'images': await sql.getResults("select * from images where note_id = ? order by note_offset", [noteId]), | 
					
						
							|  |  |  |         'history': await sql.getResults("select * from notes_history where note_id = ? and date_modified_to >= ?", [noteId, since]) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function putChanged(changed) { | 
					
						
							|  |  |  |     for (const treeItem of changed.tree) { | 
					
						
							|  |  |  |         delete treeItem['id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await sql.insert("notes_tree", treeItem, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         log.info("Update/sync notes_tree " + treeItem.note_id); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const audit of changed.audit_log) { | 
					
						
							|  |  |  |         await sql.insert("audit_log", audit, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         log.info("Update/sync audit_log for noteId=" + audit.note_id); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (changed.tree.length > 0 || changed.audit_log.length > 0) { | 
					
						
							|  |  |  |         await sql.addAudit(audit_category.SYNC); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function putNote(note) { | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  |     const origNote = await sql.getSingleResult(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (origNote !== null && origNote.date_modified >= note.detail.date_modified) { | 
					
						
							|  |  |  |         // version we have in DB is actually newer than the one we're getting from sync
 | 
					
						
							|  |  |  |         // so we'll leave the current state as it is. The synced version should be stored in the history
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         await sql.insert("notes", note.detail, true); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  |     await sql.remove("images", note.detail.note_id); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const image of note.images) { | 
					
						
							|  |  |  |         await sql.insert("images", image); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const history of note.history) { | 
					
						
							|  |  |  |         delete history['id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await sql.insert("notes_history", history); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  |     await sql.addAudit(audit_category.SYNC); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     log.info("Update/sync note " + note.detail.note_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | if (SYNC_SERVER) { | 
					
						
							|  |  |  |     log.info("Setting up sync"); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  |     setInterval(sync, 60000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // kickoff initial sync immediately
 | 
					
						
							|  |  |  |     setTimeout(sync, 1000); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | else { | 
					
						
							|  |  |  |     log.info("Sync server not configured, sync timer not running.") | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     getChangedSince, | 
					
						
							|  |  |  |     getNoteSince, | 
					
						
							|  |  |  |     putChanged, | 
					
						
							|  |  |  |     putNote | 
					
						
							|  |  |  | }; |