| 
									
										
										
										
											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-22 20:22:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | const SYNC_SERVER = 'http://localhost:3000'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let syncInProgress = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | async function pullSync() { | 
					
						
							|  |  |  |     const lastSynced = parseInt(await sql.getOption('last_synced_pull')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const resp = await rp({ | 
					
						
							|  |  |  |         uri: SYNC_SERVER + '/api/sync/changed/' + lastSynced, | 
					
						
							|  |  |  |         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 20:31:31 -04:00
										 |  |  |         for (const treeItem of resp.tree) { | 
					
						
							|  |  |  |             delete treeItem['id']; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             await sql.insert("notes_tree", treeItem, true); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             log.info("Syncing notes_tree " + treeItem.note_id); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |         for (const audit of resp.audit_log) { | 
					
						
							|  |  |  |             delete audit['id']; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             await sql.insert("audit_log", audit, true); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             log.info("Syncing audit_log for noteId=" + audit.note_id); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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({ | 
					
						
							|  |  |  |                 uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSynced, | 
					
						
							|  |  |  |                 headers: { | 
					
						
							|  |  |  |                     auth: 'sync' | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |                 json: true | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             await sql.insert("notes", note.detail, true); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             await sql.remove("images", noteId); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             for (const image of note.images) { | 
					
						
							|  |  |  |                 await sql.insert("images", image); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |             for (const history of note.history) { | 
					
						
							|  |  |  |                 delete history['id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 await sql.insert("notes_history", history); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-10-26 20:54:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             log.info("Syncing note" + noteId); | 
					
						
							| 
									
										
										
										
											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-26 20:31:31 -04:00
										 |  |  | async function pushSync() { | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-26 20:31:31 -04:00
										 |  |  |         await pushSync(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await pullSync(); | 
					
						
							| 
									
										
										
										
											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-25 22:39:21 -04:00
										 |  |  | setInterval(sync, 60000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | // kickoff initial sync immediately
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | setTimeout(sync, 1000); |