| 
									
										
										
										
											2017-10-21 21:10:33 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-17 22:09:49 +02:00
										 |  |  | const becca = require('../../becca/becca.js'); | 
					
						
							| 
									
										
										
										
											2021-02-19 22:58:53 +01:00
										 |  |  | const log = require('../../services/log'); | 
					
						
							| 
									
										
										
										
											2018-04-16 20:40:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getNotesAndBranchesAndAttributes(noteIds) { | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |     noteIds = new Set(noteIds); | 
					
						
							|  |  |  |     const collectedNoteIds = new Set(); | 
					
						
							|  |  |  |     const collectedAttributeIds = new Set(); | 
					
						
							|  |  |  |     const collectedBranchIds = new Set(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function collectEntityIds(note) { | 
					
						
							|  |  |  |         if (collectedNoteIds.has(note.noteId)) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         collectedNoteIds.add(note.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const branch of note.parentBranches) { | 
					
						
							|  |  |  |             collectedBranchIds.add(branch.branchId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             collectEntityIds(branch.parentNote); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-13 23:27:42 +01:00
										 |  |  |         for (const childNote of note.children) { | 
					
						
							| 
									
										
										
										
											2021-04-26 22:18:14 +02:00
										 |  |  |             const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, note.noteId); | 
					
						
							| 
									
										
										
										
											2020-12-13 23:27:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             collectedBranchIds.add(childBranch.branchId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |         for (const attr of note.ownedAttributes) { | 
					
						
							|  |  |  |             collectedAttributeIds.add(attr.attributeId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-30 21:39:42 +02:00
										 |  |  |             if (attr.type === 'relation' && attr.name === 'template' && attr.targetNote) { | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |                 collectEntityIds(attr.targetNote); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const noteId of noteIds) { | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         const note = becca.notes[noteId]; | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!note) { | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         collectEntityIds(note); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const notes = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const noteId of collectedNoteIds) { | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         const note = becca.notes[noteId]; | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         notes.push({ | 
					
						
							|  |  |  |             noteId: note.noteId, | 
					
						
							| 
									
										
										
										
											2020-12-21 23:10:08 +01:00
										 |  |  |             title: note.isDecrypted ? note.title : '[protected]', | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |             isProtected: note.isProtected, | 
					
						
							|  |  |  |             type: note.type, | 
					
						
							| 
									
										
										
										
											2020-12-14 23:04:56 +01:00
										 |  |  |             mime: note.mime | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const branches = []; | 
					
						
							| 
									
										
										
										
											2018-08-31 17:29:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 15:27:57 +01:00
										 |  |  |     if (noteIds.has('root')) { | 
					
						
							|  |  |  |         branches.push({ | 
					
						
							|  |  |  |             branchId: 'root', | 
					
						
							|  |  |  |             noteId: 'root', | 
					
						
							|  |  |  |             parentNoteId: 'none', | 
					
						
							|  |  |  |             notePosition: 0, | 
					
						
							|  |  |  |             prefix: '', | 
					
						
							|  |  |  |             isExpanded: true | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |     for (const branchId of collectedBranchIds) { | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         const branch = becca.branches[branchId]; | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-19 22:58:53 +01:00
										 |  |  |         if (!branch) { | 
					
						
							|  |  |  |             log.error(`Could not find branch for branchId=${branchId}`); | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |         branches.push({ | 
					
						
							|  |  |  |             branchId: branch.branchId, | 
					
						
							|  |  |  |             noteId: branch.noteId, | 
					
						
							|  |  |  |             parentNoteId: branch.parentNoteId, | 
					
						
							|  |  |  |             notePosition: branch.notePosition, | 
					
						
							|  |  |  |             prefix: branch.prefix, | 
					
						
							|  |  |  |             isExpanded: branch.isExpanded | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const attributes = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const attributeId of collectedAttributeIds) { | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |         const attribute = becca.attributes[attributeId]; | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         attributes.push({ | 
					
						
							|  |  |  |             attributeId: attribute.attributeId, | 
					
						
							|  |  |  |             noteId: attribute.noteId, | 
					
						
							|  |  |  |             type: attribute.type, | 
					
						
							|  |  |  |             name: attribute.name, | 
					
						
							|  |  |  |             value: attribute.value, | 
					
						
							|  |  |  |             position: attribute.position, | 
					
						
							|  |  |  |             isInheritable: attribute.isInheritable | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-12-10 21:27:21 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-25 11:52:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-25 21:47:14 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         branches, | 
					
						
							| 
									
										
										
										
											2020-01-25 13:46:55 +01:00
										 |  |  |         notes, | 
					
						
							|  |  |  |         attributes | 
					
						
							| 
									
										
										
										
											2019-10-25 21:47:14 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2018-04-16 20:40:18 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 16:50:16 +02:00
										 |  |  | function getTree(req) { | 
					
						
							| 
									
										
										
										
											2020-11-22 23:05:02 +01:00
										 |  |  |     const subTreeNoteId = req.query.subTreeNoteId || 'root'; | 
					
						
							| 
									
										
										
										
											2020-12-13 23:27:42 +01:00
										 |  |  |     const collectedNoteIds = new Set([subTreeNoteId]); | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function collect(parentNote) { | 
					
						
							| 
									
										
										
										
											2020-12-16 14:36:24 +01:00
										 |  |  |         if (!parentNote) { | 
					
						
							|  |  |  |             console.trace(parentNote); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-13 23:27:42 +01:00
										 |  |  |         for (const childNote of parentNote.children) { | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |             collectedNoteIds.add(childNote.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-26 22:18:14 +02:00
										 |  |  |             const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId); | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (childBranch.isExpanded) { | 
					
						
							| 
									
										
										
										
											2020-12-13 23:27:42 +01:00
										 |  |  |                 collect(childBranch.childNote); | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |     if (!(subTreeNoteId in becca.notes)) { | 
					
						
							| 
									
										
										
										
											2021-02-20 21:28:22 +01:00
										 |  |  |         return [404, `Note ${subTreeNoteId} not found in the cache`]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-16 23:00:08 +02:00
										 |  |  |     collect(becca.notes[subTreeNoteId]); | 
					
						
							| 
									
										
										
										
											2018-12-11 21:53:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 22:06:12 +01:00
										 |  |  |     return getNotesAndBranchesAndAttributes(collectedNoteIds); | 
					
						
							| 
									
										
										
										
											2018-04-16 20:40:18 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-16 16:26:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function load(req) { | 
					
						
							|  |  |  |     return getNotesAndBranchesAndAttributes(req.body.noteIds); | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-14 23:31:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2018-04-16 20:40:18 -04:00
										 |  |  |     getTree, | 
					
						
							|  |  |  |     load | 
					
						
							| 
									
										
										
										
											2018-03-30 12:57:22 -04:00
										 |  |  | }; |