| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  | const SOURCE_ID = require('./source_id'); | 
					
						
							| 
									
										
										
										
											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-29 11:22:41 -04:00
										 |  |  | async function pullSync(cookieJar, syncLog) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |     let syncRows; | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         logSync("Pulling changes: " + SYNC_SERVER + '/api/sync/changed?lastSyncId=' + lastSyncedPull + "&sourceId=" + SOURCE_ID); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |         syncRows = await rp({ | 
					
						
							|  |  |  |             uri: SYNC_SERVER + '/api/sync/changed?lastSyncId=' + lastSyncedPull + "&sourceId=" + SOURCE_ID, | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |             jar: cookieJar, | 
					
						
							|  |  |  |             json: true | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         logSync("Pulled " + syncRows.length + " changes"); | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |         throw new Error("Can't pull changes, inner exception: " + e.stack); | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |     for (const sync of syncRows) { | 
					
						
							|  |  |  |         let resp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             resp = await rp({ | 
					
						
							|  |  |  |                 uri: SYNC_SERVER + "/api/sync/" + sync.entity_name + "/" + sync.entity_id, | 
					
						
							|  |  |  |                 json: true, | 
					
						
							|  |  |  |                 jar: cookieJar | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         catch (e) { | 
					
						
							|  |  |  |             throw new Error("Can't pull " + sync.entity_name + " " + sync.entity_id + ", inner exception: " + e.stack); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         // TODO: ideally this should be in transaction
 | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |         if (sync.entity_name === 'notes') { | 
					
						
							|  |  |  |             await updateNote(resp.entity, resp.links, sync.source_id, syncLog) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (sync.entity_name === 'notes_tree') { | 
					
						
							|  |  |  |             await updateNoteTree(resp, sync.source_id, syncLog) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (sync.entity_name === 'notes_history') { | 
					
						
							|  |  |  |             await updateNoteHistory(resp, sync.source_id, syncLog) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             logSync("Unrecognized entity type " + sync.entity_name, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |             throw new Error("Unrecognized entity type " + sync.entity_name); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await sql.setOption('last_synced_pull', sync.id); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     logSync("Finished pull"); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | async function syncEntity(entity, entityName, cookieJar, syncLog) { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |         const payload = { | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |             sourceId: SOURCE_ID, | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |             entity: entity | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |         if (entityName === 'notes') { | 
					
						
							|  |  |  |             payload.links = await sql.getResults('select * from links where note_id = ?', [entity.note_id]); | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         await rp({ | 
					
						
							|  |  |  |             method: 'PUT', | 
					
						
							|  |  |  |             uri: SYNC_SERVER + '/api/sync/' + entityName, | 
					
						
							|  |  |  |             body: payload, | 
					
						
							|  |  |  |             json: true, | 
					
						
							|  |  |  |             timeout: 60 * 1000, | 
					
						
							|  |  |  |             jar: cookieJar | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     catch (e) { | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |         logSync("Failed sending update for entity " + entityName + ", inner exception: " + e.stack, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |         throw new Error("Failed sending update for entity " + entityName + ", inner exception: " + e.stack); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function pushSync(cookieJar, syncLog) { | 
					
						
							|  |  |  |     let lastSyncedPush = parseInt(await sql.getOption('last_synced_push')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (true) { | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |         const sync = await sql.getSingleResultOrNull('SELECT * FROM sync WHERE id > ? LIMIT 1', [lastSyncedPush]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (sync === null) { | 
					
						
							|  |  |  |             // nothing to sync
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             logSync("Nothing to push", syncLog); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             let entity; | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             if (sync.entity_name === 'notes') { | 
					
						
							|  |  |  |                 entity = await sql.getSingleResult('SELECT * FROM notes WHERE note_id = ?', [sync.entity_id]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (sync.entity_name === 'notes_tree') { | 
					
						
							|  |  |  |                 entity = await sql.getSingleResult('SELECT * FROM notes_tree WHERE note_id = ?', [sync.entity_id]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (sync.entity_name === 'notes_history') { | 
					
						
							|  |  |  |                 entity = await sql.getSingleResult('SELECT * FROM notes_history WHERE note_history_id = ?', [sync.entity_id]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 logSync("Unrecognized entity type " + sync.entity_name, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |                 throw new Error("Unrecognized entity type " + sync.entity_name); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             await syncEntity(entity, sync.entity_name, cookieJar, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             lastSyncedPush = sync.id; | 
					
						
							| 
									
										
										
										
											2017-10-30 18:44:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |             await sql.setOption('last_synced_push', lastSyncedPush); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |     const documentSecret = await sql.getOption('document_secret'); | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     const hash = utils.hmac(documentSecret, timestamp); | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const cookieJar = rp.jar(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     try { | 
					
						
							|  |  |  |         await rp({ | 
					
						
							|  |  |  |             method: 'POST', | 
					
						
							|  |  |  |             uri: SYNC_SERVER + '/api/login', | 
					
						
							|  |  |  |             body: { | 
					
						
							|  |  |  |                 timestamp: timestamp, | 
					
						
							|  |  |  |                 dbVersion: migration.APP_DB_VERSION, | 
					
						
							|  |  |  |                 hash: hash | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             json: true, | 
					
						
							|  |  |  |             timeout: 5 * 1000, | 
					
						
							|  |  |  |             jar: cookieJar | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return cookieJar; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         throw new Error("Can't login to API for sync, inner exception: " + e.stack); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | async function sync() { | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     const syncLog = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 16:14:59 -04:00
										 |  |  |     if (syncInProgress) { | 
					
						
							|  |  |  |         syncLog.push("Sync already in progress"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return syncLog; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											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()) { | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |             syncLog.push("DB not up to date"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return syncLog; | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-28 22:17:00 -04:00
										 |  |  |         const cookieJar = await login(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |         await pushSync(cookieJar, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-30 18:44:26 -04:00
										 |  |  |         await pullSync(cookieJar, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |         logSync("sync failed: " + e.stack, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     finally { | 
					
						
							|  |  |  |         syncInProgress = false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return syncLog; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function logSync(message, syncLog) { | 
					
						
							|  |  |  |     log.info(message); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     if (syncLog) { | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |         syncLog.push(message); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     console.log(message); | 
					
						
							| 
									
										
										
										
											2017-10-22 20:22:09 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | async function getChanged(lastSyncId, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:09:07 -04:00
										 |  |  |     console.log([lastSyncId, sourceId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |     return await sql.getResults("SELECT * FROM sync WHERE id > ? AND source_id != ?", [lastSyncId, sourceId]); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | async function updateNote(entity, links, sourceId, syncLog) { | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     const origNote = await sql.getSingleResult("select * from notes where note_id = ?", [entity.note_id]); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     if (origNote === null || origNote.date_modified <= entity.date_modified) { | 
					
						
							|  |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await sql.replace("notes", entity); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |             await sql.remove("links", entity.note_id); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             for (const link of links) { | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |                 delete link['lnk_id']; | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |                 await sql.insert('link', link); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             await sql.addNoteSync(entity.note_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |         logSync("Update/sync note " + entity.note_id, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     else { | 
					
						
							|  |  |  |         logSync("Sync conflict in note " + entity.note_id, syncLog); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | async function updateNoteTree(entity, sourceId, syncLog) { | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     const orig = await sql.getSingleResultOrNull("select * from notes_tree where note_id = ?", [entity.note_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (orig === null || orig.date_modified < entity.date_modified) { | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await sql.replace('notes_tree', entity); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             await sql.addNoteTreeSync(entity.note_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         logSync("Update/sync note tree " + entity.note_id, syncLog); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         logSync("Sync conflict in note tree " + entity.note_id, syncLog); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  | async function updateNoteHistory(entity, sourceId, syncLog) { | 
					
						
							|  |  |  |     const orig = await sql.getSingleResultOrNull("select * from notes_history where note_history_id = ?", [entity.note_history_id]); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (orig === null || orig.date_modified_to < entity.date_modified_to) { | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             delete entity['id']; | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             await sql.replace('notes_history', entity); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |             await sql.addNoteHistorySync(entity.note_history_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         logSync("Update/sync note history " + entity.note_id, syncLog); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         logSync("Sync conflict in note history for " + entity.note_id + ", from=" + entity.date_modified_from + ", to=" + entity.date_modified_to, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 16:14:59 -04:00
										 |  |  | if (SYNC_SERVER) { | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  |     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 = { | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |     sync, | 
					
						
							| 
									
										
										
										
											2017-10-31 19:34:58 -04:00
										 |  |  |     getChanged, | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     updateNote, | 
					
						
							|  |  |  |     updateNoteTree, | 
					
						
							|  |  |  |     updateNoteHistory | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | }; |