| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:09:49 +02:00
										 |  |  | const NoteRevision = require('../becca/entities/note_revision.js'); | 
					
						
							| 
									
										
										
										
											2020-12-16 14:36:24 +01:00
										 |  |  | const dateUtils = require('./date_utils'); | 
					
						
							|  |  |  | const log = require('./log'); | 
					
						
							|  |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {Note} note | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function protectNoteRevisions(note) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:00:55 +02:00
										 |  |  |     for (const revision of note.getNoteRevisions()) { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |         if (note.isProtected !== revision.isProtected) { | 
					
						
							| 
									
										
										
										
											2020-12-09 22:49:55 +01:00
										 |  |  |             try { | 
					
						
							|  |  |  |                 const content = revision.getContent(); | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-09 22:49:55 +01:00
										 |  |  |                 revision.isProtected = note.isProtected; | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-09 22:49:55 +01:00
										 |  |  |                 // this will force de/encryption
 | 
					
						
							|  |  |  |                 revision.setContent(content); | 
					
						
							| 
									
										
										
										
											2019-11-09 13:01:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-09 22:49:55 +01:00
										 |  |  |                 revision.save(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             catch (e) { | 
					
						
							|  |  |  |                 log.error("Could not un/protect note revision ID = " + revision.noteRevisionId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 throw e; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @param {Note} note | 
					
						
							| 
									
										
										
										
											2020-10-05 23:56:59 +02:00
										 |  |  |  * @return {NoteRevision|null} | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function createNoteRevision(note) { | 
					
						
							|  |  |  |     if (note.hasLabel("disableVersioning")) { | 
					
						
							| 
									
										
										
										
											2020-10-05 23:56:59 +02:00
										 |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2019-12-02 20:21:52 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 21:47:15 +02:00
										 |  |  |     const content = note.getContent(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 23:56:59 +02:00
										 |  |  |     if (!content || (Buffer.isBuffer(content) && content.byteLength === 0)) { | 
					
						
							|  |  |  |         return null; | 
					
						
							| 
									
										
										
										
											2020-09-18 21:47:15 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 21:47:59 +02:00
										 |  |  |     const contentMetadata = note.getContentMetadata(); | 
					
						
							| 
									
										
										
										
											2020-09-06 22:04:11 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const noteRevision = new NoteRevision({ | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |         noteId: note.noteId, | 
					
						
							|  |  |  |         // title and text should be decrypted now
 | 
					
						
							|  |  |  |         title: note.title, | 
					
						
							|  |  |  |         type: note.type, | 
					
						
							|  |  |  |         mime: note.mime, | 
					
						
							|  |  |  |         isProtected: false, // will be fixed in the protectNoteRevisions() call
 | 
					
						
							| 
									
										
										
										
											2020-09-06 22:04:11 +02:00
										 |  |  |         utcDateLastEdited: note.utcDateModified > contentMetadata.utcDateModified | 
					
						
							|  |  |  |             ? note.utcDateModified | 
					
						
							|  |  |  |             : contentMetadata.utcDateModified, | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |         utcDateCreated: dateUtils.utcNowDateTime(), | 
					
						
							|  |  |  |         utcDateModified: dateUtils.utcNowDateTime(), | 
					
						
							| 
									
										
										
										
											2020-09-06 22:04:11 +02:00
										 |  |  |         dateLastEdited: note.dateModified > contentMetadata.dateModified | 
					
						
							|  |  |  |             ? note.dateModified | 
					
						
							|  |  |  |             : contentMetadata.dateModified, | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  |         dateCreated: dateUtils.localNowDateTime() | 
					
						
							|  |  |  |     }).save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-18 21:47:15 +02:00
										 |  |  |     noteRevision.setContent(content); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return noteRevision; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 14:36:24 +01:00
										 |  |  | function eraseNoteRevisions(noteRevisionIdsToErase) { | 
					
						
							|  |  |  |     if (noteRevisionIdsToErase.length === 0) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-14 21:35:13 +01:00
										 |  |  |     log.info(`Removing note revisions: ${JSON.stringify(noteRevisionIdsToErase)}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 14:36:24 +01:00
										 |  |  |     sql.executeMany(`DELETE FROM note_revisions WHERE noteRevisionId IN (???)`, noteRevisionIdsToErase); | 
					
						
							|  |  |  |     sql.executeMany(`UPDATE entity_changes SET isErased = 1 WHERE entityName = 'note_revisions' AND entityId IN (???)`, noteRevisionIdsToErase); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sql.executeMany(`DELETE FROM note_revision_contents WHERE noteRevisionId IN (???)`, noteRevisionIdsToErase); | 
					
						
							|  |  |  |     sql.executeMany(`UPDATE entity_changes SET isErased = 1 WHERE entityName = 'note_revision_contents' AND entityId IN (???)`, noteRevisionIdsToErase); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:52 +01:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     protectNoteRevisions, | 
					
						
							| 
									
										
										
										
											2020-12-16 14:36:24 +01:00
										 |  |  |     createNoteRevision, | 
					
						
							|  |  |  |     eraseNoteRevisions | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |