| 
									
										
										
										
											2018-01-29 18:34:59 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Entity = require('./entity'); | 
					
						
							| 
									
										
										
										
											2018-04-20 00:12:01 -04:00
										 |  |  | const protectedSessionService = require('../services/protected_session'); | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  | const utils = require('../services/utils'); | 
					
						
							| 
									
										
										
										
											2019-11-01 19:21:48 +01:00
										 |  |  | const sql = require('../services/sql'); | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  | const dateUtils = require('../services/date_utils'); | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  | const entityChangesService = require('../services/entity_changes.js'); | 
					
						
							| 
									
										
										
										
											2018-01-29 18:34:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-22 23:37:06 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * NoteRevision represents snapshot of note's title and content at some point in the past. It's used for seamless note versioning. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-11-09 09:36:08 +01:00
										 |  |  |  * @property {string} noteRevisionId | 
					
						
							|  |  |  |  * @property {string} noteId | 
					
						
							|  |  |  |  * @property {string} type | 
					
						
							|  |  |  |  * @property {string} mime | 
					
						
							|  |  |  |  * @property {string} title | 
					
						
							| 
									
										
										
										
											2019-11-09 15:21:14 +01:00
										 |  |  |  * @property {boolean} isErased | 
					
						
							|  |  |  |  * @property {boolean} isProtected | 
					
						
							| 
									
										
										
										
											2019-11-09 09:36:08 +01:00
										 |  |  |  * @property {string} dateLastEdited | 
					
						
							|  |  |  |  * @property {string} dateCreated | 
					
						
							|  |  |  |  * @property {string} utcDateLastEdited | 
					
						
							|  |  |  |  * @property {string} utcDateCreated | 
					
						
							|  |  |  |  * @property {string} utcDateModified | 
					
						
							| 
									
										
										
										
											2018-08-22 23:37:06 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @extends Entity | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-01-29 18:34:59 -05:00
										 |  |  | class NoteRevision extends Entity { | 
					
						
							| 
									
										
										
										
											2018-08-16 23:00:04 +02:00
										 |  |  |     static get entityName() { return "note_revisions"; } | 
					
						
							| 
									
										
										
										
											2018-01-30 20:12:19 -05:00
										 |  |  |     static get primaryKeyName() { return "noteRevisionId"; } | 
					
						
							| 
									
										
										
										
											2020-08-16 22:57:48 +02:00
										 |  |  |     static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isErased", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; } | 
					
						
							| 
									
										
										
										
											2018-01-29 23:35:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  |     constructor(row) { | 
					
						
							|  |  |  |         super(row); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-13 00:19:50 +02:00
										 |  |  |         this.isErased = !!this.isErased; | 
					
						
							| 
									
										
										
										
											2018-08-07 11:38:00 +02:00
										 |  |  |         this.isProtected = !!this.isProtected; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  |         if (this.isProtected) { | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |             if (protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							| 
									
										
										
										
											2019-11-02 07:50:23 +01:00
										 |  |  |                 this.title = protectedSessionService.decryptString(this.title); | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 this.title = "[Protected]"; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     getNote() { | 
					
						
							|  |  |  |         return this.repository.getNote(this.noteId); | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |     /** @returns {boolean} true if the note has string content (not binary) */ | 
					
						
							|  |  |  |     isStringNote() { | 
					
						
							|  |  |  |         return utils.isStringNote(this.type, this.mime); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* | 
					
						
							|  |  |  |      * Note revision content has quite special handling - it's not a separate entity, but a lazily loaded | 
					
						
							|  |  |  |      * part of NoteRevision entity with it's own sync. Reason behind this hybrid design is that | 
					
						
							|  |  |  |      * content can be quite large and it's not necessary to load it / fill memory for any note access even | 
					
						
							|  |  |  |      * if we don't need a content, especially for bulk operations like search. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This is the same approach as is used for Note's content. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-25 23:24:48 +02:00
										 |  |  |     /** @returns {*} */ | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     getContent(silentNotFoundError = false) { | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |         if (this.content === undefined) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             const res = sql.getRow(`SELECT content, hash FROM note_revision_contents WHERE noteRevisionId = ?`, [this.noteRevisionId]); | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!res) { | 
					
						
							|  |  |  |                 if (silentNotFoundError) { | 
					
						
							|  |  |  |                     return undefined; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     throw new Error("Cannot find note revision content for noteRevisionId=" + this.noteRevisionId); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             this.content = res.content; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (this.isProtected) { | 
					
						
							|  |  |  |                 if (protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |                     this.content = protectedSessionService.decrypt(this.content); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     this.content = ""; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 16:59:40 +01:00
										 |  |  |         if (this.isStringNote()) { | 
					
						
							|  |  |  |             return this.content === null | 
					
						
							|  |  |  |                 ? "" | 
					
						
							|  |  |  |                 : this.content.toString("UTF-8"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return this.content; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     setContent(content) { | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |         this.content = content; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const pojo = { | 
					
						
							|  |  |  |             noteRevisionId: this.noteRevisionId, | 
					
						
							|  |  |  |             content: content, | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  |             utcDateModified: dateUtils.utcNowDateTime() | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  |         if (this.isProtected) { | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |             if (protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |                 pojo.content = protectedSessionService.encrypt(pojo.content); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 throw new Error(`Cannot update content of noteRevisionId=${this.noteRevisionId} since we're out of protected session.`); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.upsert("note_revision_contents", "noteRevisionId", pojo); | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  |         const hash = utils.hash(this.noteRevisionId + "|" + content); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         entityChangesService.addEntityChange('note_revision_contents', this.noteRevisionId, hash); | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 16:51:51 +01:00
										 |  |  |     beforeSaving() { | 
					
						
							|  |  |  |         super.beforeSaving(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-07 23:04:17 +01:00
										 |  |  |         this.utcDateModified = dateUtils.utcNowDateTime(); | 
					
						
							| 
									
										
										
										
											2019-11-09 16:51:51 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |     // cannot be static!
 | 
					
						
							|  |  |  |     updatePojo(pojo) { | 
					
						
							|  |  |  |         if (pojo.isProtected) { | 
					
						
							|  |  |  |             if (protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |                 pojo.title = protectedSessionService.encrypt(pojo.title); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 // updating protected note outside of protected session means we will keep original ciphertexts
 | 
					
						
							|  |  |  |                 delete pojo.title; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-06 08:59:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |         delete pojo.content; | 
					
						
							| 
									
										
										
										
											2018-01-29 18:34:59 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | module.exports = NoteRevision; |