| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | const sql = require('./sql'); | 
					
						
							|  |  |  | const options = require('./options'); | 
					
						
							|  |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2017-11-16 21:50:00 -05:00
										 |  |  | const sync_table = require('./sync_table'); | 
					
						
							| 
									
										
										
										
											2018-01-23 22:11:03 -05:00
										 |  |  | const attributes = require('./attributes'); | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  | const protected_session = require('./protected_session'); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 23:22:19 -05:00
										 |  |  | async function getNoteById(noteId, dataKey) { | 
					
						
							|  |  |  |     const note = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected_session.decryptNote(dataKey, note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return note; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  | async function getJsonNoteById(noteId, dataKey) { | 
					
						
							|  |  |  |     const note = await getNoteById(noteId, dataKey); | 
					
						
							|  |  |  |     note.data = JSON.parse(note.note_text); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return note; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function updateJsonNote(noteId, data) { | 
					
						
							|  |  |  |     const ret = await createNewNote(noteId, { | 
					
						
							|  |  |  |         note_title: name, | 
					
						
							|  |  |  |         note_text: JSON.stringify(data), | 
					
						
							|  |  |  |         target: 'into', | 
					
						
							|  |  |  |         is_protected: false, | 
					
						
							|  |  |  |         type: 'code', | 
					
						
							|  |  |  |         mime: 'application/json' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret.noteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function createNewJsonNote(parentNoteId, name, payload) { | 
					
						
							|  |  |  |     const ret = await createNewNote(parentNoteId, { | 
					
						
							|  |  |  |         note_title: name, | 
					
						
							|  |  |  |         note_text: JSON.stringify(payload), | 
					
						
							|  |  |  |         target: 'into', | 
					
						
							|  |  |  |         is_protected: false, | 
					
						
							|  |  |  |         type: 'code', | 
					
						
							|  |  |  |         mime: 'application/json' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret.noteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function createNewNote(parentNoteId, noteOpts, dataKey, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     const noteId = utils.newNoteId(); | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     const noteTreeId = utils.newNoteTreeId(); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let newNotePos = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     if (noteOpts.target === 'into') { | 
					
						
							|  |  |  |         const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |         newNotePos = maxNotePos === null ? 0 : maxNotePos + 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (noteOpts.target === 'after') { | 
					
						
							|  |  |  |         const afterNote = await sql.getFirst('SELECT note_position FROM notes_tree WHERE note_tree_id = ?', [noteOpts.target_note_tree_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         newNotePos = afterNote.note_position + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // not updating date_modified to avoig having to sync whole rows
 | 
					
						
							|  |  |  |         await sql.execute('UPDATE notes_tree SET note_position = note_position + 1 WHERE parent_note_id = ? AND note_position > ? AND is_deleted = 0', | 
					
						
							|  |  |  |             [parentNoteId, afterNote.note_position]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |         await sync_table.addNoteReorderingSync(parentNoteId, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         throw new Error('Unknown target: ' + noteOpts.target); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     if (parentNoteId !== 'root') { | 
					
						
							|  |  |  |         const parent = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [parentNoteId]); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |         if (!noteOpts.type) { | 
					
						
							|  |  |  |             noteOpts.type = parent.type; | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!noteOpts.mime) { | 
					
						
							|  |  |  |             noteOpts.mime = parent.mime; | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const now = utils.nowDate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const note = { | 
					
						
							|  |  |  |         note_id: noteId, | 
					
						
							|  |  |  |         note_title: noteOpts.note_title, | 
					
						
							|  |  |  |         note_text: noteOpts.note_text ? noteOpts.note_text : '', | 
					
						
							|  |  |  |         is_protected: noteOpts.is_protected, | 
					
						
							|  |  |  |         type: noteOpts.type ? noteOpts.type : 'text', | 
					
						
							|  |  |  |         mime: noteOpts.mime ? noteOpts.mime : 'text/html', | 
					
						
							|  |  |  |         date_created: now, | 
					
						
							|  |  |  |         date_modified: now | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     if (note.is_protected) { | 
					
						
							|  |  |  |         protected_session.encryptNote(dataKey, note); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-26 21:31:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     await sql.insert("notes", note); | 
					
						
							| 
									
										
										
										
											2018-01-26 21:31:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     await sync_table.addNoteSync(noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-26 21:31:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     await sql.insert("notes_tree", { | 
					
						
							|  |  |  |         note_tree_id: noteTreeId, | 
					
						
							|  |  |  |         note_id: noteId, | 
					
						
							|  |  |  |         parent_note_id: parentNoteId, | 
					
						
							|  |  |  |         note_position: newNotePos, | 
					
						
							|  |  |  |         is_expanded: 0, | 
					
						
							|  |  |  |         date_modified: now, | 
					
						
							|  |  |  |         is_deleted: 0 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     await sync_table.addNoteTreeSync(noteTreeId, sourceId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |     return { | 
					
						
							|  |  |  |         noteId, | 
					
						
							| 
									
										
										
										
											2018-01-28 10:37:43 -05:00
										 |  |  |         noteTreeId, | 
					
						
							|  |  |  |         note | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const note = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     await protectNote(note, dataKey, protect, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-03 22:13:02 -05:00
										 |  |  |     const children = await sql.getFirstColumn("SELECT note_id FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const childNoteId of children) { | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await protectNoteRecursively(childNoteId, dataKey, protect, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function protectNote(note, dataKey, protect, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     let changed = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (protect && !note.is_protected) { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |         protected_session.encryptNote(dataKey, note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |         note.is_protected = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         changed = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (!protect && note.is_protected) { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |         protected_session.decryptNote(dataKey, note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         note.is_protected = false; | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         changed = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (changed) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ? WHERE note_id = ?", | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             [note.note_title, note.note_text, note.is_protected, note.note_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteSync(note.note_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     await protectNoteHistory(note.note_id, dataKey, protect, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function protectNoteHistory(noteId, dataKey, protect, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const historyToChange = await sql.getAll("SELECT * FROM notes_history WHERE note_id = ? AND is_protected != ?", [noteId, protect]); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const history of historyToChange) { | 
					
						
							|  |  |  |         if (protect) { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |             protected_session.encryptNoteHistoryRow(dataKey, history); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             history.is_protected = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |             protected_session.decryptNoteHistoryRow(dataKey, history); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             history.is_protected = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.execute("UPDATE notes_history SET note_title = ?, note_text = ?, is_protected = ? WHERE note_history_id = ?", | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             [history.note_title, history.note_text, history.is_protected, history.note_history_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteHistorySync(history.note_history_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) { | 
					
						
							|  |  |  |     const oldNote = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (oldNote.is_protected) { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |         protected_session.decryptNote(dataKey, oldNote); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         note.is_protected = false; | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newNoteHistoryId = utils.newNoteHistoryId(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await sql.insert('notes_history', { | 
					
						
							|  |  |  |         note_history_id: newNoteHistoryId, | 
					
						
							|  |  |  |         note_id: noteId, | 
					
						
							|  |  |  |         // title and text should be decrypted now
 | 
					
						
							|  |  |  |         note_title: oldNote.note_title, | 
					
						
							|  |  |  |         note_text: oldNote.note_text, | 
					
						
							|  |  |  |         is_protected: 0, // will be fixed in the protectNoteHistory() call
 | 
					
						
							|  |  |  |         date_modified_from: oldNote.date_modified, | 
					
						
							|  |  |  |         date_modified_to: nowStr | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await sync_table.addNoteHistorySync(newNoteHistoryId, sourceId); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function saveNoteImages(noteId, noteText, sourceId) { | 
					
						
							|  |  |  |     const existingNoteImages = await sql.getAll("SELECT * FROM notes_image WHERE note_id = ?", [noteId]); | 
					
						
							|  |  |  |     const foundImageIds = []; | 
					
						
							|  |  |  |     const now = utils.nowDate(); | 
					
						
							|  |  |  |     const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g; | 
					
						
							|  |  |  |     let match; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (match = re.exec(noteText)) { | 
					
						
							|  |  |  |         const imageId = match[1]; | 
					
						
							|  |  |  |         const existingNoteImage = existingNoteImages.find(ni => ni.image_id === imageId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!existingNoteImage) { | 
					
						
							|  |  |  |             const noteImageId = utils.newNoteImageId(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await sql.insert("notes_image", { | 
					
						
							|  |  |  |                 note_image_id: noteImageId, | 
					
						
							|  |  |  |                 note_id: noteId, | 
					
						
							|  |  |  |                 image_id: imageId, | 
					
						
							|  |  |  |                 is_deleted: 0, | 
					
						
							|  |  |  |                 date_modified: now, | 
					
						
							|  |  |  |                 date_created: now | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await sync_table.addNoteImageSync(noteImageId, sourceId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (existingNoteImage.is_deleted) { | 
					
						
							|  |  |  |             await sql.execute("UPDATE notes_image SET is_deleted = 0, date_modified = ? WHERE note_image_id = ?", | 
					
						
							|  |  |  |                 [now, existingNoteImage.note_image_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await sync_table.addNoteImageSync(existingNoteImage.note_image_id, sourceId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // else we don't need to do anything
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foundImageIds.push(imageId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // marking note images as deleted if they are not present on the page anymore
 | 
					
						
							|  |  |  |     const unusedNoteImages = existingNoteImages.filter(ni => !foundImageIds.includes(ni.image_id)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const unusedNoteImage of unusedNoteImages) { | 
					
						
							|  |  |  |         await sql.execute("UPDATE notes_image SET is_deleted = 1, date_modified = ? WHERE note_image_id = ?", | 
					
						
							|  |  |  |             [now, unusedNoteImage.note_image_id]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await sync_table.addNoteImageSync(unusedNoteImage.note_image_id, sourceId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  | async function updateNote(noteId, newNote, dataKey, sourceId) { | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |     if (newNote.detail.is_protected) { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |         await protected_session.encryptNote(dataKey, newNote.detail); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 22:11:03 -05:00
										 |  |  |     const attributesMap = await attributes.getNoteAttributeMap(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const now = new Date(); | 
					
						
							| 
									
										
										
										
											2017-12-13 23:27:02 -05:00
										 |  |  |     const nowStr = utils.nowDate(); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const historySnapshotTimeInterval = parseInt(await options.getOption('history_snapshot_time_interval')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const historyCutoff = utils.dateStr(new Date(now.getTime() - historySnapshotTimeInterval * 1000)); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const existingNoteHistoryId = await sql.getFirstValue( | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |         "SELECT note_history_id FROM notes_history WHERE note_id = ? AND date_modified_to >= ?", [noteId, historyCutoff]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |         const msSinceDateCreated = now.getTime() - utils.parseDate(newNote.detail.date_created).getTime(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 22:11:03 -05:00
										 |  |  |         if (attributesMap.disable_versioning !== 'true' | 
					
						
							|  |  |  |             && !existingNoteHistoryId | 
					
						
							|  |  |  |             && msSinceDateCreated >= historySnapshotTimeInterval * 1000) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |             await saveNoteHistory(noteId, dataKey, sourceId, nowStr); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |         await saveNoteImages(noteId, newNote.detail.note_text, sourceId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |         await protectNoteHistory(noteId, dataKey, newNote.detail.is_protected); | 
					
						
							| 
									
										
										
										
											2017-11-14 22:21:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET note_title = ?, note_text = ?, is_protected = ?, date_modified = ? WHERE note_id = ?", [ | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |             newNote.detail.note_title, | 
					
						
							|  |  |  |             newNote.detail.note_text, | 
					
						
							| 
									
										
										
										
											2017-11-14 21:54:12 -05:00
										 |  |  |             newNote.detail.is_protected, | 
					
						
							| 
									
										
										
										
											2017-12-13 23:27:02 -05:00
										 |  |  |             nowStr, | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |             noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  |         await sync_table.addNoteSync(noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function deleteNote(noteTreeId, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-14 21:39:21 -05:00
										 |  |  |     const noteTree = await sql.getFirstOrNull("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!noteTree || noteTree.is_deleted === 1) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const now = utils.nowDate(); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.execute("UPDATE notes_tree SET is_deleted = 1, date_modified = ? WHERE note_tree_id = ?", [now, noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     await sync_table.addNoteTreeSync(noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const noteId = await sql.getFirstValue("SELECT note_id FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |     const notDeletedNoteTreesCount = await sql.getFirstValue("SELECT COUNT(*) FROM notes_tree WHERE note_id = ? AND is_deleted = 0", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!notDeletedNoteTreesCount) { | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET is_deleted = 1, date_modified = ? WHERE note_id = ?", [now, noteId]); | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteSync(noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         const children = await sql.getAll("SELECT note_tree_id FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  |         for (const child of children) { | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |             await deleteNote(child.note_tree_id, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-01-25 23:22:19 -05:00
										 |  |  |     getNoteById, | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     createNewNote, | 
					
						
							|  |  |  |     updateNote, | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     deleteNote, | 
					
						
							|  |  |  |     protectNoteRecursively | 
					
						
							| 
									
										
										
										
											2017-11-05 17:58:55 -05:00
										 |  |  | }; |