| 
									
										
										
										
											2019-06-03 22:55:59 +02:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 23:38:47 +02:00
										 |  |  | const becca = require("../../becca/becca"); | 
					
						
							| 
									
										
										
										
											2019-06-03 22:55:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  | function buildDescendantCountMap() { | 
					
						
							|  |  |  |     const noteIdToCountMap = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getCount(noteId) { | 
					
						
							|  |  |  |         if (!(noteId in noteIdToCountMap)) { | 
					
						
							|  |  |  |             const note = becca.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             noteIdToCountMap[noteId] = note.children.length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const child of note.children) { | 
					
						
							|  |  |  |                 noteIdToCountMap[noteId] += getCount(child.noteId); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return noteIdToCountMap[noteId]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     getCount('root'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return noteIdToCountMap; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 23:02:38 +02:00
										 |  |  | function getLinkMap(req) { | 
					
						
							| 
									
										
										
										
											2021-09-20 23:04:41 +02:00
										 |  |  |     const mapRootNote = becca.getNote(req.params.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 20:02:23 +02:00
										 |  |  |     const noteIds = new Set(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 23:04:41 +02:00
										 |  |  |     const notes = mapRootNote.getSubtreeNotes(false) | 
					
						
							| 
									
										
										
										
											2021-09-20 20:02:23 +02:00
										 |  |  |         .map(note => [ | 
					
						
							|  |  |  |             note.noteId, | 
					
						
							|  |  |  |             note.isContentAvailable() ? note.title : '[protected]', | 
					
						
							|  |  |  |             note.type | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     notes.forEach(([noteId]) => noteIds.add(noteId)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const links = Object.values(becca.attributes).filter(rel => { | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |         if (rel.type !== 'relation' || rel.name === 'relationMapLink' || rel.name === 'template') { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-20 20:02:23 +02:00
										 |  |  |         else if (!noteIds.has(rel.noteId) || !noteIds.has(rel.value)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |         else if (rel.name === 'imageLink') { | 
					
						
							|  |  |  |             const parentNote = becca.getNote(rel.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return !parentNote.getChildNotes().find(childNote => childNote.noteId === rel.value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-20 20:02:23 +02:00
										 |  |  |     }) | 
					
						
							|  |  |  |         .map(rel => ({ | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |         id: rel.noteId + "-" + rel.name + "-" + rel.value, | 
					
						
							|  |  |  |         sourceNoteId: rel.noteId, | 
					
						
							|  |  |  |         targetNoteId: rel.value, | 
					
						
							|  |  |  |         name: rel.name | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 20:02:23 +02:00
										 |  |  |     return { | 
					
						
							|  |  |  |         notes: notes, | 
					
						
							|  |  |  |         noteIdToDescendantCountMap: buildDescendantCountMap(), | 
					
						
							|  |  |  |         links: links | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 23:02:38 +02:00
										 |  |  | function getTreeMap(req) { | 
					
						
							| 
									
										
										
										
											2021-09-20 23:04:41 +02:00
										 |  |  |     const mapRootNote = becca.getNote(req.params.noteId); | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |     const noteIds = new Set(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 23:04:41 +02:00
										 |  |  |     const notes = mapRootNote.getSubtreeNotes(false) | 
					
						
							|  |  |  |         .filter(note => !note.hasLabel('excludeFromTreeMap')) | 
					
						
							| 
									
										
										
										
											2021-09-21 22:45:06 +02:00
										 |  |  |         .filter(note => { | 
					
						
							|  |  |  |             if (note.type !== 'image' || note.getChildNotes().length > 0) { | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const imageLinkRelation = note.getTargetRelations().find(rel => rel.name === 'imageLink'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!imageLinkRelation) { | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return !note.getParentNotes().find(parentNote => parentNote.noteId === imageLinkRelation.noteId); | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |         .map(note => [ | 
					
						
							|  |  |  |             note.noteId, | 
					
						
							|  |  |  |             note.isContentAvailable() ? note.title : '[protected]', | 
					
						
							|  |  |  |             note.type | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     notes.forEach(([noteId]) => noteIds.add(noteId)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 20:02:23 +02:00
										 |  |  |     const links = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |     for (const branch of Object.values(becca.branches)) { | 
					
						
							|  |  |  |         if (!noteIds.has(branch.parentNoteId) || !noteIds.has(branch.noteId)) { | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         links.push({ | 
					
						
							|  |  |  |             id: branch.branchId, | 
					
						
							|  |  |  |             sourceNoteId: branch.parentNoteId, | 
					
						
							| 
									
										
										
										
											2021-09-21 22:45:06 +02:00
										 |  |  |             targetNoteId: branch.noteId | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         notes: notes, | 
					
						
							|  |  |  |         noteIdToDescendantCountMap: buildDescendantCountMap(), | 
					
						
							|  |  |  |         links: links | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-03 22:55:59 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2021-09-17 22:34:23 +02:00
										 |  |  |     getLinkMap, | 
					
						
							| 
									
										
										
										
											2021-09-22 23:02:38 +02:00
										 |  |  |     getTreeMap | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |