| 
									
										
										
										
											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-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-29 14:55:48 -04:00
										 |  |  |     let resp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |         resp = await rp({ | 
					
						
							|  |  |  |             uri: SYNC_SERVER + '/api/sync/changed/' + lastSyncedPull, | 
					
						
							|  |  |  |             headers: { | 
					
						
							|  |  |  |                 auth: 'sync' | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             jar: cookieJar, | 
					
						
							|  |  |  |             json: true | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         throw new Error("Can't pull changed, inner exception: " + e.stack); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  |     try { | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await putChanged(resp, syncLog); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const noteId of resp.notes) { | 
					
						
							|  |  |  |                 let note; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 try { | 
					
						
							|  |  |  |                     note = await rp({ | 
					
						
							|  |  |  |                         uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSyncedPull, | 
					
						
							|  |  |  |                         headers: { | 
					
						
							|  |  |  |                             auth: 'sync' | 
					
						
							|  |  |  |                         }, | 
					
						
							|  |  |  |                         json: true, | 
					
						
							|  |  |  |                         jar: cookieJar | 
					
						
							|  |  |  |                     }); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 catch (e) { | 
					
						
							|  |  |  |                     throw new Error("Can't pull note " + noteId + ", inner exception: " + e.stack); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 await putNote(note, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |             if (resp.notes.length > 0) { | 
					
						
							|  |  |  |                 await sql.addAudit(audit_category.SYNC); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-10-25 22:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 18:50:28 -04:00
										 |  |  |             await sql.setOption('last_synced_pull', resp.syncTimestamp); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-10-26 20:31:31 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         throw e; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |         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 syncEntities(entities, entityName, cookieJar, syncLog) { | 
					
						
							|  |  |  |     for (const entity of entities) { | 
					
						
							|  |  |  |         await syncEntity(entity, entityName, cookieJar, syncLog); | 
					
						
							| 
									
										
										
										
											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')); | 
					
						
							|  |  |  |     const syncStarted = utils.nowTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (true) { | 
					
						
							|  |  |  |         const oldestUnsyncedDateModified = await sql.getSingleValue(`
 | 
					
						
							|  |  |  |     SELECT MIN(date_modified) FROM ( | 
					
						
							|  |  |  |         SELECT MIN(date_modified) AS date_modified FROM notes_tree WHERE date_modified > ? | 
					
						
							|  |  |  |         UNION | 
					
						
							|  |  |  |         SELECT MIN(date_modified) AS date_modified FROM notes WHERE date_modified > ? | 
					
						
							|  |  |  |         UNION | 
					
						
							|  |  |  |         SELECT MIN(date_modified_to) AS date_modified FROM notes_history WHERE date_modified_to > ? | 
					
						
							|  |  |  |     )`, [lastSyncedPush, lastSyncedPush, lastSyncedPush]);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (oldestUnsyncedDateModified === null) { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             const notesTree = await sql.getResults('SELECT * FROM notes_tree WHERE date_modified = ?', [oldestUnsyncedDateModified]); | 
					
						
							|  |  |  |             await syncEntities(notesTree, 'notes_tree', cookieJar, syncLog); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const notes = await sql.getResults('SELECT * FROM notes WHERE date_modified = ?', [oldestUnsyncedDateModified]); | 
					
						
							|  |  |  |             await syncEntities(notes, 'notes', cookieJar, syncLog); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const notesHistory = await sql.getResults('SELECT * FROM notes_history WHERE date_modified_to = ?', [oldestUnsyncedDateModified]); | 
					
						
							|  |  |  |             await syncEntities(notesHistory, 'notes_history', cookieJar, syncLog); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             lastSyncedPush = oldestUnsyncedDateModified; | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-30 18:44:26 -04:00
										 |  |  |             // if the sync started in the same second as the last changes then it's possible we synced only parts
 | 
					
						
							|  |  |  |             // of this second's changes. In that case we'll leave the last_synced_push as it is and stop the sync
 | 
					
						
							|  |  |  |             // so next time we'll re-do this second again, this guaranteeing all changes have been pushed
 | 
					
						
							|  |  |  |             if (lastSyncedPush === syncStarted) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-26 21:16:21 -04:00
										 |  |  | async function getChangedSince(since) { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         '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]), | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |         'audit_log': await sql.getResults("select * from audit_log where category != 'SYNC' and date_modified >= ?", [since]) | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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]) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  | async function putChanged(changed, syncLog) { | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     for (const treeItem of changed.tree) { | 
					
						
							|  |  |  |         delete treeItem['id']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await sql.insert("notes_tree", treeItem, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |         logSync("Update/sync notes_tree " + treeItem.note_id, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const audit of changed.audit_log) { | 
					
						
							|  |  |  |         await sql.insert("audit_log", audit, true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 14:55:48 -04:00
										 |  |  |         logSync("Update/sync audit_log for category=" + audit.category + ", noteId=" + audit.note_id, syncLog); | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (changed.tree.length > 0 || changed.audit_log.length > 0) { | 
					
						
							| 
									
										
										
										
											2017-10-29 11:22:41 -04:00
										 |  |  |         logSync("Added final audit", syncLog); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-26 23:21:31 -04:00
										 |  |  |         await sql.addAudit(audit_category.SYNC); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | async function updateNote(body, syncLog) { | 
					
						
							|  |  |  |     const entity = body.entity; | 
					
						
							| 
									
										
										
										
											2017-10-28 19:55:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-29 22:22:30 -04:00
										 |  |  |             for (const link of body.links) { | 
					
						
							|  |  |  |                 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
										 |  |  | 
 | 
					
						
							|  |  |  |             await sql.addNoteSync(entity.note_id, body.source_id); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateNoteTree(body, syncLog) { | 
					
						
							|  |  |  |     const entity = body.entity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await sql.addNoteTreeSync(entity.note_id, body.source_id); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateNoteHistory(body, syncLog) { | 
					
						
							|  |  |  |     const entity = body.entity; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |     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 () => { | 
					
						
							|  |  |  |             await sql.execute("delete from notes_history where note_history_id", [entity.note_history_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             delete entity['id']; | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |             await sql.insert('notes_history', entity); | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 00:15:49 -04:00
										 |  |  |             await sql.addNoteHistorySync(entity.note_history_id, body.source_id); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											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-26 21:16:21 -04:00
										 |  |  |     getChangedSince, | 
					
						
							|  |  |  |     getNoteSince, | 
					
						
							|  |  |  |     putChanged, | 
					
						
							| 
									
										
										
										
											2017-10-29 22:22:30 -04:00
										 |  |  |     updateNote, | 
					
						
							|  |  |  |     updateNoteTree, | 
					
						
							|  |  |  |     updateNoteHistory | 
					
						
							| 
									
										
										
										
											2017-10-26 21:16:21 -04:00
										 |  |  | }; |