| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const log = require('./log'); | 
					
						
							|  |  |  | const eventLog = require('./event_log'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const sync_table = require('./sync_table'); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 22:55:36 -05:00
										 |  |  | function deserializeNoteContentBuffer(note) { | 
					
						
							|  |  |  |     if (note.type === 'file') { | 
					
						
							|  |  |  |         note.content = new Buffer(note.content, 'binary'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 20:47:01 -05:00
										 |  |  | async function updateNote(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-02-18 22:55:36 -05:00
										 |  |  |     deserializeNoteContentBuffer(entity); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (!origNote || origNote.dateModified <= entity.dateModified) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await sql.replace("notes", entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             await sync_table.addNoteSync(entity.noteId, sourceId); | 
					
						
							|  |  |  |             await eventLog.addNoteEvent(entity.noteId, "Synced note <note>"); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         log.info("Update/sync note " + entity.noteId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  | async function updateBranch(entity, sourceId) { | 
					
						
							|  |  |  |     const orig = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [entity.branchId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         if (orig === null || orig.dateModified < entity.dateModified) { | 
					
						
							|  |  |  |             delete entity.isExpanded; | 
					
						
							| 
									
										
										
										
											2017-11-09 21:11:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             await sql.replace('branches', entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             await sync_table.addBranchSync(entity.branchId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             log.info("Update/sync note tree " + entity.branchId); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:04:47 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  | async function updateNoteRevision(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |         // we update note revision even if date modified to is the same because the only thing which might have changed
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         // is the protected status (and correnspondingly title and content) which doesn't affect the dateModifiedTo
 | 
					
						
							|  |  |  |         if (orig === null || orig.dateModifiedTo <= entity.dateModifiedTo) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |             await sql.replace('note_revisions', entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |             await sync_table.addNoteRevisionSync(entity.noteRevisionId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |             log.info("Update/sync note revision " + entity.noteRevisionId); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:04:47 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateNoteReordering(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         Object.keys(entity.ordering).forEach(async key => { | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity.ordering[key], key]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sync_table.addNoteReorderingSync(entity.parentNoteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateOptions(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const orig = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [entity.name]); | 
					
						
							| 
									
										
										
										
											2018-01-11 22:45:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (!orig.isSynced) { | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         if (orig === null || orig.dateModified < entity.dateModified) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |             await sql.replace('options', entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             await sync_table.addOptionsSync(entity.name, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             await eventLog.addEvent("Synced option " + entity.name); | 
					
						
							| 
									
										
										
										
											2017-11-28 17:04:47 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateRecentNotes(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE branchId = ?", [entity.branchId]); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (orig === null || orig.dateAccessed < entity.dateAccessed) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await sql.replace('recent_notes', entity); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |             await sync_table.addRecentNoteSync(entity.branchId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  | async function updateImage(entity, sourceId) { | 
					
						
							|  |  |  |     if (entity.data !== null) { | 
					
						
							|  |  |  |         entity.data = Buffer.from(entity.data, 'base64'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const origImage = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [entity.imageId]); | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (!origImage || origImage.dateModified <= entity.dateModified) { | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await sql.replace("images", entity); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             await sync_table.addImageSync(entity.imageId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         log.info("Update/sync image " + entity.imageId); | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  | async function updateNoteImage(entity, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const origNoteImage = await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [entity.noteImageId]); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (!origNoteImage || origNoteImage.dateModified <= entity.dateModified) { | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |             await sql.replace("note_images", entity); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             await sync_table.addNoteImageSync(entity.noteImageId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         log.info("Update/sync note image " + entity.noteImageId); | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  | async function updateLabel(entity, sourceId) { | 
					
						
							|  |  |  |     const origLabel = await sql.getRow("SELECT * FROM labels WHERE labelId = ?", [entity.labelId]); | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     if (!origLabel || origLabel.dateModified <= entity.dateModified) { | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  |         await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             await sql.replace("labels", entity); | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |             await sync_table.addLabelSync(entity.labelId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |         log.info("Update/sync label " + entity.labelId); | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  | async function updateApiToken(entity, sourceId) { | 
					
						
							|  |  |  |     const apiTokenId = await sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [entity.apiTokenId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!apiTokenId) { | 
					
						
							|  |  |  |         await sql.doInTransaction(async () => { | 
					
						
							|  |  |  |             await sql.replace("api_tokens", entity); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await sync_table.addApiTokenSync(entity.apiTokenId, sourceId); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         log.info("Update/sync API token " + entity.apiTokenId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     updateNote, | 
					
						
							| 
									
										
										
										
											2018-03-24 21:39:15 -04:00
										 |  |  |     updateBranch, | 
					
						
							| 
									
										
										
										
											2018-03-25 20:52:38 -04:00
										 |  |  |     updateNoteRevision, | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  |     updateNoteReordering, | 
					
						
							|  |  |  |     updateOptions, | 
					
						
							| 
									
										
										
										
											2018-01-06 15:56:00 -05:00
										 |  |  |     updateRecentNotes, | 
					
						
							| 
									
										
										
										
											2018-01-06 21:49:02 -05:00
										 |  |  |     updateImage, | 
					
						
							| 
									
										
										
										
											2018-01-09 22:09:45 -05:00
										 |  |  |     updateNoteImage, | 
					
						
							| 
									
										
										
										
											2018-03-24 22:02:26 -04:00
										 |  |  |     updateLabel, | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     updateApiToken | 
					
						
							| 
									
										
										
										
											2017-11-09 20:52:47 -05:00
										 |  |  | }; |