| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const sql = require('../sql.js'); | 
					
						
							|  |  |  | const eventService = require('../events.js'); | 
					
						
							|  |  |  | const noteCache = require('./note_cache'); | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  | const sqlInit = require('../sql_init'); | 
					
						
							| 
									
										
										
										
											2020-09-25 20:55:45 +02:00
										 |  |  | const log = require('../log'); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | const Note = require('./entities/note'); | 
					
						
							|  |  |  | const Branch = require('./entities/branch'); | 
					
						
							|  |  |  | const Attribute = require('./entities/attribute'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  | sqlInit.dbReady.then(() => { | 
					
						
							|  |  |  |     load(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function load() { | 
					
						
							| 
									
										
										
										
											2020-12-12 12:07:15 +01:00
										 |  |  |     const start = Date.now(); | 
					
						
							| 
									
										
										
										
											2020-05-22 09:38:30 +02:00
										 |  |  |     noteCache.reset(); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 22:57:48 +02:00
										 |  |  |     for (const row of sql.iterateRows(`SELECT noteId, title, type, mime, isProtected, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes WHERE isDeleted = 0`, [])) { | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  |         new Note(noteCache, row); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |     for (const row of sql.iterateRows(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded FROM branches WHERE isDeleted = 0`, [])) { | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  |         new Branch(noteCache, row); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |     for (const row of sql.iterateRows(`SELECT attributeId, noteId, type, name, value, isInheritable, position FROM attributes WHERE isDeleted = 0`, [])) { | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  |         new Attribute(noteCache, row); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     noteCache.loaded = true; | 
					
						
							| 
									
										
										
										
											2020-12-12 12:07:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     log.info(`Note cache load took ${Date.now() - start}ms`); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | eventService.subscribe([eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_SYNCED],  ({entityName, entity}) => { | 
					
						
							| 
									
										
										
										
											2020-09-03 21:37:06 +02:00
										 |  |  |     // note that entity can also be just POJO without methods if coming from sync
 | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!noteCache.loaded) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (entityName === 'notes') { | 
					
						
							|  |  |  |         const {noteId} = entity; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (entity.isDeleted) { | 
					
						
							|  |  |  |             delete noteCache.notes[noteId]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (noteId in noteCache.notes) { | 
					
						
							| 
									
										
										
										
											2020-09-03 21:37:06 +02:00
										 |  |  |             noteCache.notes[noteId].update(entity); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  |             const note = new Note(noteCache, entity); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             note.decrypt(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'branches') { | 
					
						
							|  |  |  |         const {branchId, noteId, parentNoteId} = entity; | 
					
						
							|  |  |  |         const childNote = noteCache.notes[noteId]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (entity.isDeleted) { | 
					
						
							|  |  |  |             if (childNote) { | 
					
						
							|  |  |  |                 childNote.parents = childNote.parents.filter(parent => parent.noteId !== parentNoteId); | 
					
						
							|  |  |  |                 childNote.parentBranches = childNote.parentBranches.filter(branch => branch.branchId !== branchId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (childNote.parents.length > 0) { | 
					
						
							|  |  |  |                     childNote.invalidateSubtreeCaches(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const parentNote = noteCache.notes[parentNoteId]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (parentNote) { | 
					
						
							|  |  |  |                 parentNote.children = parentNote.children.filter(child => child.noteId !== noteId); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             delete noteCache.childParentToBranch[`${noteId}-${parentNoteId}`]; | 
					
						
							|  |  |  |             delete noteCache.branches[branchId]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (branchId in noteCache.branches) { | 
					
						
							| 
									
										
										
										
											2021-02-04 22:05:32 +01:00
										 |  |  |             // only relevant properties which can change in a branch are prefix and isExpanded
 | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  |             noteCache.branches[branchId].prefix = entity.prefix; | 
					
						
							| 
									
										
										
										
											2021-02-04 22:05:32 +01:00
										 |  |  |             noteCache.branches[branchId].isExpanded = entity.isExpanded; | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (childNote) { | 
					
						
							|  |  |  |                 childNote.flatTextCache = null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  |             noteCache.branches[branchId] = new Branch(noteCache, entity); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (childNote) { | 
					
						
							|  |  |  |                 childNote.resortParents(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (entityName === 'attributes') { | 
					
						
							|  |  |  |         const {attributeId, noteId} = entity; | 
					
						
							|  |  |  |         const note = noteCache.notes[noteId]; | 
					
						
							|  |  |  |         const attr = noteCache.attributes[attributeId]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (entity.isDeleted) { | 
					
						
							|  |  |  |             if (note && attr) { | 
					
						
							|  |  |  |                 // first invalidate and only then remove the attribute (otherwise invalidation wouldn't be complete)
 | 
					
						
							|  |  |  |                 if (attr.isAffectingSubtree || note.isTemplate) { | 
					
						
							|  |  |  |                     note.invalidateSubtreeCaches(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 note.ownedAttributes = note.ownedAttributes.filter(attr => attr.attributeId !== attributeId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const targetNote = attr.targetNote; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (targetNote) { | 
					
						
							|  |  |  |                     targetNote.targetRelations = targetNote.targetRelations.filter(rel => rel.attributeId !== attributeId); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             delete noteCache.attributes[attributeId]; | 
					
						
							| 
									
										
										
										
											2020-08-12 00:02:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (attr) { | 
					
						
							| 
									
										
										
										
											2020-12-14 22:12:26 +01:00
										 |  |  |                 delete noteCache.attributeIndex[`${attr.type}-${attr.name.toLowerCase()}`]; | 
					
						
							| 
									
										
										
										
											2020-08-12 00:02:19 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (attributeId in noteCache.attributes) { | 
					
						
							|  |  |  |             const attr = noteCache.attributes[attributeId]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // attr name and isInheritable are immutable
 | 
					
						
							|  |  |  |             attr.value = entity.value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (attr.isAffectingSubtree || note.isTemplate) { | 
					
						
							|  |  |  |                 note.invalidateSubtreeFlatText(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 note.flatTextCache = null; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2020-06-03 16:24:41 +02:00
										 |  |  |             const attr = new Attribute(noteCache, entity); | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (note) { | 
					
						
							|  |  |  |                 if (attr.isAffectingSubtree || note.isTemplate) { | 
					
						
							|  |  |  |                     note.invalidateSubtreeCaches(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     note.invalidateThisCache(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | eventService.subscribe(eventService.ENTER_PROTECTED_SESSION, () => { | 
					
						
							| 
									
										
										
										
											2020-09-25 20:55:45 +02:00
										 |  |  |     try { | 
					
						
							|  |  |  |         noteCache.decryptProtectedNotes(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     catch (e) { | 
					
						
							|  |  |  |         log.error(`Could not decrypt protected notes: ${e.message} ${e.stack}`); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-05-20 00:03:33 +02:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-07-27 23:40:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							|  |  |  |     load | 
					
						
							|  |  |  | }; |