| 
									
										
										
										
											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-27 17:18:19 -05:00
										 |  |  | 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') { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |         const maxNotePos = await sql.getValue('SELECT MAX(notePosition) FROM note_tree WHERE parentNoteId = ? AND isDeleted = 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') { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |         const afterNote = await sql.getRow('SELECT notePosition FROM note_tree WHERE noteTreeId = ?', [noteOpts.target_noteTreeId]); | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         newNotePos = afterNote.notePosition + 1; | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         // not updating dateModified to avoig having to sync whole rows
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         await sql.execute('UPDATE note_tree SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0', | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             [parentNoteId, afterNote.notePosition]); | 
					
						
							| 
									
										
										
										
											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') { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |         const parent = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [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 = { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         noteId: noteId, | 
					
						
							|  |  |  |         title: noteOpts.title, | 
					
						
							|  |  |  |         content: noteOpts.content ? noteOpts.content : '', | 
					
						
							|  |  |  |         isProtected: noteOpts.isProtected, | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |         type: noteOpts.type ? noteOpts.type : 'text', | 
					
						
							|  |  |  |         mime: noteOpts.mime ? noteOpts.mime : 'text/html', | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         dateCreated: now, | 
					
						
							|  |  |  |         dateModified: now | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (note.isProtected) { | 
					
						
							| 
									
										
										
										
											2018-01-27 17:18:19 -05:00
										 |  |  |         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-28 19:38:05 -05:00
										 |  |  |     await sql.insert("note_tree", { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         noteTreeId: noteTreeId, | 
					
						
							|  |  |  |         noteId: noteId, | 
					
						
							|  |  |  |         parentNoteId: parentNoteId, | 
					
						
							|  |  |  |         notePosition: newNotePos, | 
					
						
							|  |  |  |         isExpanded: 0, | 
					
						
							|  |  |  |         dateModified: now, | 
					
						
							|  |  |  |         isDeleted: 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 00:07:43 -05:00
										 |  |  | async function createNote(parentNoteId, title, content = "", extraOptions = {}) { | 
					
						
							| 
									
										
										
										
											2018-02-26 20:47:34 -05:00
										 |  |  |     if (!parentNoteId) throw new Error("Empty parentNoteId"); | 
					
						
							|  |  |  |     if (!title) throw new Error("Empty title"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-26 00:07:43 -05:00
										 |  |  |     const note = { | 
					
						
							|  |  |  |         title: title, | 
					
						
							|  |  |  |         content: extraOptions.json ? JSON.stringify(content, null, '\t') : content, | 
					
						
							|  |  |  |         target: 'into', | 
					
						
							|  |  |  |         isProtected: extraOptions.isProtected !== undefined ? extraOptions.isProtected : false, | 
					
						
							|  |  |  |         type: extraOptions.type, | 
					
						
							|  |  |  |         mime: extraOptions.mime | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (extraOptions.json) { | 
					
						
							|  |  |  |         note.type = "code"; | 
					
						
							|  |  |  |         note.mime = "application/json"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!note.type) { | 
					
						
							|  |  |  |         note.type = "text"; | 
					
						
							|  |  |  |         note.mime = "text/html"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const {noteId} = await createNewNote(parentNoteId, note, extraOptions.dataKey, extraOptions.sourceId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (extraOptions.attributes) { | 
					
						
							|  |  |  |         for (const attrName in extraOptions.attributes) { | 
					
						
							|  |  |  |             await attributes.createAttribute(noteId, attrName, extraOptions.attributes[attrName]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return noteId; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  | async function protectNoteRecursively(noteId, dataKey, protect, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [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-29 17:41:59 -05:00
										 |  |  |     const children = await sql.getColumn("SELECT noteId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (protect && !note.isProtected) { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |         protected_session.encryptNote(dataKey, note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         note.isProtected = true; | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         changed = true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     else if (!protect && note.isProtected) { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |         protected_session.decryptNote(dataKey, note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         note.isProtected = false; | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         changed = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (changed) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ? WHERE noteId = ?", | 
					
						
							|  |  |  |             [note.title, note.content, note.isProtected, note.noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sync_table.addNoteSync(note.noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     await protectNoteHistory(note.noteId, 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) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const historyToChange = await sql.getRows("SELECT * FROM note_revisions WHERE noteId = ? AND isProtected != ?", [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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             history.isProtected = true; | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |             protected_session.decryptNoteHistoryRow(dataKey, history); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             history.isProtected = false; | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         await sql.execute("UPDATE note_revisions SET title = ?, content = ?, isProtected = ? WHERE noteRevisionId = ?", | 
					
						
							|  |  |  |             [history.title, history.content, history.isProtected, history.noteRevisionId]); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         await sync_table.addNoteHistorySync(history.noteRevisionId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | async function saveNoteHistory(noteId, dataKey, sourceId, nowStr) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const oldNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-23 22:58:24 -05:00
										 |  |  |     if (oldNote.type === 'file') { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (oldNote.isProtected) { | 
					
						
							| 
									
										
										
										
											2018-01-24 22:13:41 -05:00
										 |  |  |         protected_session.decryptNote(dataKey, oldNote); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-23 22:58:24 -05:00
										 |  |  |         oldNote.isProtected = false; | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     const newNoteRevisionId = utils.newNoteRevisionId(); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     await sql.insert('note_revisions', { | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |         noteRevisionId: newNoteRevisionId, | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         noteId: noteId, | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |         // title and text should be decrypted now
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         title: oldNote.title, | 
					
						
							|  |  |  |         content: oldNote.content, | 
					
						
							|  |  |  |         isProtected: 0, // will be fixed in the protectNoteHistory() call
 | 
					
						
							|  |  |  |         dateModifiedFrom: oldNote.dateModified, | 
					
						
							|  |  |  |         dateModifiedTo: nowStr | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 00:18:59 -05:00
										 |  |  |     await sync_table.addNoteHistorySync(newNoteRevisionId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function saveNoteImages(noteId, noteText, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const existingNoteImages = await sql.getRows("SELECT * FROM note_images WHERE noteId = ?", [noteId]); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |     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]; | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         const existingNoteImage = existingNoteImages.find(ni => ni.imageId === imageId); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!existingNoteImage) { | 
					
						
							|  |  |  |             const noteImageId = utils.newNoteImageId(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |             await sql.insert("note_images", { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 noteImageId: noteImageId, | 
					
						
							|  |  |  |                 noteId: noteId, | 
					
						
							|  |  |  |                 imageId: imageId, | 
					
						
							|  |  |  |                 isDeleted: 0, | 
					
						
							|  |  |  |                 dateModified: now, | 
					
						
							|  |  |  |                 dateCreated: now | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             await sync_table.addNoteImageSync(noteImageId, sourceId); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         else if (existingNoteImage.isDeleted) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |             await sql.execute("UPDATE note_images SET isDeleted = 0, dateModified = ? WHERE noteImageId = ?", | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 [now, existingNoteImage.noteImageId]); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             await sync_table.addNoteImageSync(existingNoteImage.noteImageId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         // 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
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     const unusedNoteImages = existingNoteImages.filter(ni => !foundImageIds.includes(ni.imageId)); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (const unusedNoteImage of unusedNoteImages) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         await sql.execute("UPDATE note_images SET isDeleted = 1, dateModified = ? WHERE noteImageId = ?", | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             [now, unusedNoteImage.noteImageId]); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sync_table.addNoteImageSync(unusedNoteImage.noteImageId, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-23 22:58:24 -05:00
										 |  |  | async function loadFile(noteId, newNote, dataKey) { | 
					
						
							|  |  |  |     const oldNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (oldNote.isProtected) { | 
					
						
							|  |  |  |         await protected_session.decryptNote(dataKey, oldNote); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     newNote.detail.content = oldNote.content; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:23:35 -05:00
										 |  |  | async function updateNote(noteId, newNote, dataKey, sourceId) { | 
					
						
							| 
									
										
										
										
											2018-02-23 22:58:24 -05:00
										 |  |  |     if (newNote.detail.type === 'file') { | 
					
						
							|  |  |  |         await loadFile(noteId, newNote, dataKey); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (newNote.detail.isProtected) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const existingnoteRevisionId = await sql.getValue( | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |         "SELECT noteRevisionId FROM note_revisions WHERE noteId = ? AND dateModifiedTo >= ?", [noteId, historyCutoff]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 17:24:08 -05:00
										 |  |  |     await sql.doInTransaction(async () => { | 
					
						
							| 
									
										
										
										
											2018-02-10 13:53:35 -05:00
										 |  |  |         const msSinceDateCreated = now.getTime() - utils.parseDateTime(newNote.detail.dateCreated).getTime(); | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 22:11:03 -05:00
										 |  |  |         if (attributesMap.disable_versioning !== 'true' | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |             && !existingnoteRevisionId | 
					
						
							| 
									
										
										
										
											2018-01-23 22:11:03 -05:00
										 |  |  |             && 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-28 19:30:14 -05:00
										 |  |  |         await saveNoteImages(noteId, newNote.detail.content, sourceId); | 
					
						
							| 
									
										
										
										
											2018-01-06 22:38:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await protectNoteHistory(noteId, dataKey, newNote.detail.isProtected); | 
					
						
							| 
									
										
										
										
											2017-11-14 22:21:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET title = ?, content = ?, isProtected = ?, dateModified = ? WHERE noteId = ?", [ | 
					
						
							|  |  |  |             newNote.detail.title, | 
					
						
							|  |  |  |             newNote.detail.content, | 
					
						
							|  |  |  |             newNote.detail.isProtected, | 
					
						
							| 
									
										
										
										
											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-29 17:41:59 -05:00
										 |  |  |     const noteTree = await sql.getRowOrNull("SELECT * FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); | 
					
						
							| 
									
										
										
										
											2018-01-14 21:39:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |     if (!noteTree || noteTree.isDeleted === 1) { | 
					
						
							| 
									
										
										
										
											2018-01-14 21:39:21 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-10 12:56:59 -05:00
										 |  |  |     const now = utils.nowDate(); | 
					
						
							| 
									
										
										
										
											2017-11-29 21:04:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-28 19:38:05 -05:00
										 |  |  |     await sql.execute("UPDATE note_tree SET isDeleted = 1, dateModified = ? WHERE noteTreeId = ?", [now, noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |     await sync_table.addNoteTreeSync(noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const noteId = await sql.getValue("SELECT noteId FROM note_tree WHERE noteTreeId = ?", [noteTreeId]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |     const notDeletedNoteTreesCount = await sql.getValue("SELECT COUNT(*) FROM note_tree WHERE noteId = ? AND isDeleted = 0", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!notDeletedNoteTreesCount) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         await sql.execute("UPDATE notes SET isDeleted = 1, dateModified = ? WHERE noteId = ?", [now, noteId]); | 
					
						
							| 
									
										
										
										
											2017-12-16 20:48:34 -05:00
										 |  |  |         await sync_table.addNoteSync(noteId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 17:41:59 -05:00
										 |  |  |         const children = await sql.getRows("SELECT noteTreeId FROM note_tree WHERE parentNoteId = ? AND isDeleted = 0", [noteId]); | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  |         for (const child of children) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |             await deleteNote(child.noteTreeId, sourceId); | 
					
						
							| 
									
										
										
										
											2017-11-19 23:12:39 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     createNewNote, | 
					
						
							| 
									
										
										
										
											2018-02-26 00:07:43 -05:00
										 |  |  |     createNote, | 
					
						
							| 
									
										
										
										
											2017-11-05 10:41:54 -05:00
										 |  |  |     updateNote, | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |     deleteNote, | 
					
						
							|  |  |  |     protectNoteRecursively | 
					
						
							| 
									
										
										
										
											2017-11-05 17:58:55 -05:00
										 |  |  | }; |