| 
									
										
										
										
											2018-01-29 18:34:59 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:09:49 +02:00
										 |  |  | const protectedSessionService = require('../../services/protected_session'); | 
					
						
							|  |  |  | const utils = require('../../services/utils'); | 
					
						
							|  |  |  | const sql = require('../../services/sql'); | 
					
						
							|  |  |  | const dateUtils = require('../../services/date_utils'); | 
					
						
							|  |  |  | const becca = require('../becca.js'); | 
					
						
							|  |  |  | const entityChangesService = require('../../services/entity_changes'); | 
					
						
							|  |  |  | const AbstractEntity = require("./abstract_entity.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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @extends Entity | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  | class NoteRevision extends AbstractEntity { | 
					
						
							| 
									
										
										
										
											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-12-16 14:36:24 +01:00
										 |  |  |     static get hashedProperties() { return ["noteRevisionId", "noteId", "title", "isProtected", "dateLastEdited", "dateCreated", "utcDateLastEdited", "utcDateCreated", "utcDateModified"]; } | 
					
						
							| 
									
										
										
										
											2018-01-29 23:35:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 10:51:37 -04:00
										 |  |  |     constructor(row) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |         super(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.noteRevisionId = row.noteRevisionId; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.noteId = row.noteId; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.type = row.type; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.mime = row.mime; | 
					
						
							|  |  |  |         /** @param {boolean} */ | 
					
						
							|  |  |  |         this.isProtected = !!row.isProtected; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.title = row.title; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.dateLastEdited = row.dateLastEdited; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.dateCreated = row.dateCreated; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.utcDateLastEdited = row.utcDateLastEdited; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.utcDateCreated = row.utcDateCreated; | 
					
						
							|  |  |  |         /** @param {string} */ | 
					
						
							|  |  |  |         this.utcDateModified = row.utcDateModified; | 
					
						
							| 
									
										
										
										
											2018-08-07 11:38:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |         return becca.notes[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) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |         const res = sql.getRow(`SELECT content FROM note_revision_contents WHERE noteRevisionId = ?`, [this.noteRevisionId]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!res) { | 
					
						
							|  |  |  |             if (silentNotFoundError) { | 
					
						
							|  |  |  |                 return undefined; | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |             else { | 
					
						
							|  |  |  |                 throw new Error("Cannot find note revision content for noteRevisionId=" + this.noteRevisionId); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |         let content = res.content; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (this.isProtected) { | 
					
						
							|  |  |  |             if (protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |                 content = protectedSessionService.decrypt(content); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 content = ""; | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-26 16:59:40 +01:00
										 |  |  |         if (this.isStringNote()) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |             return content === null | 
					
						
							| 
									
										
										
										
											2020-03-26 16:59:40 +01:00
										 |  |  |                 ? "" | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |                 : content.toString("UTF-8"); | 
					
						
							| 
									
										
										
										
											2020-03-26 16:59:40 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |             return content; | 
					
						
							| 
									
										
										
										
											2020-03-26 16:59:40 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											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
										 |  |  |         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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-14 22:12:26 +01:00
										 |  |  |         entityChangesService.addEntityChange({ | 
					
						
							|  |  |  |             entityName: 'note_revision_contents', | 
					
						
							|  |  |  |             entityId: this.noteRevisionId, | 
					
						
							|  |  |  |             hash: hash, | 
					
						
							|  |  |  |             isErased: false, | 
					
						
							|  |  |  |             utcDateChanged: this.getUtcDateChanged() | 
					
						
							|  |  |  |         }, null); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-08 22:13:08 +02:00
										 |  |  |     getPojo() { | 
					
						
							|  |  |  |         const pojo = { | 
					
						
							|  |  |  |             noteRevisionId: this.noteRevisionId, | 
					
						
							|  |  |  |             noteId: this.noteId, | 
					
						
							|  |  |  |             type: this.type, | 
					
						
							|  |  |  |             mime: this.mime, | 
					
						
							|  |  |  |             isProtected: this.isProtected, | 
					
						
							|  |  |  |             title: this.title, | 
					
						
							|  |  |  |             dateLastEdited: this.dateLastEdited, | 
					
						
							|  |  |  |             dateCreated: this.dateCreated, | 
					
						
							|  |  |  |             utcDateLastEdited: this.utcDateLastEdited, | 
					
						
							|  |  |  |             utcDateCreated: this.utcDateCreated, | 
					
						
							|  |  |  |             utcDateModified: this.utcDateModified | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |         if (pojo.isProtected) { | 
					
						
							|  |  |  |             if (protectedSessionService.isProtectedSessionAvailable()) { | 
					
						
							| 
									
										
										
										
											2021-05-08 22:13:08 +02:00
										 |  |  |                 pojo.title = protectedSessionService.encrypt(this.title); | 
					
						
							| 
									
										
										
										
											2019-10-31 21:58:34 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-08 22:13:08 +02:00
										 |  |  |         return pojo; | 
					
						
							| 
									
										
										
										
											2018-01-29 18:34:59 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | module.exports = NoteRevision; |